Index: src/wp-admin/includes/ms.php
===================================================================
--- src/wp-admin/includes/ms.php	(revision 41307)
+++ src/wp-admin/includes/ms.php	(working copy)
@@ -126,7 +126,12 @@
 		foreach ( (array) $drop_tables as $table ) {
 			$wpdb->query( "DROP TABLE IF EXISTS `$table`" );
 		}
-
+		if ( is_site_meta_supported() ) {
+			$blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $blog_id ) );
+			foreach ( $blog_meta_ids as $mid ) {
+				delete_metadata_by_mid( 'blog', $mid );
+			}
+		}
 		$wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
 
 		/**
Index: src/wp-admin/includes/schema.php
===================================================================
--- src/wp-admin/includes/schema.php	(revision 41307)
+++ src/wp-admin/includes/schema.php	(working copy)
@@ -265,6 +265,15 @@
   PRIMARY KEY  (blog_id),
   KEY db_version (db_version)
 ) $charset_collate;
+CREATE TABLE $wpdb->blogmeta (
+  meta_id bigint(20) NOT NULL auto_increment,
+  blog_id bigint(20) NOT NULL default '0',
+  meta_key varchar(255) default NULL,
+  meta_value longtext,
+  PRIMARY KEY  (meta_id),
+  KEY meta_key (meta_key($max_index_length)),
+  KEY blog_id (blog_id)
+) $charset_collate;
 CREATE TABLE $wpdb->registration_log (
   ID bigint(20) NOT NULL auto_increment,
   email varchar(255) NOT NULL default '',
@@ -1018,6 +1027,7 @@
 		'subdomain_install' => intval( $subdomain_install ),
 		'global_terms_enabled' => global_terms_enabled() ? '1' : '0',
 		'ms_files_rewriting' => is_multisite() ? get_site_option( 'ms_files_rewriting' ) : '0',
+		'site_meta_supported' => is_site_meta_supported(),
 		'initial_db_version' => get_option( 'initial_db_version' ),
 		'active_sitewide_plugins' => array(),
 		'WPLANG' => get_locale(),
Index: src/wp-admin/includes/upgrade.php
===================================================================
--- src/wp-admin/includes/upgrade.php	(revision 41307)
+++ src/wp-admin/includes/upgrade.php	(working copy)
@@ -1881,6 +1881,12 @@
 			}
 		}
 	}
+
+	// 4.9
+	if ( $wp_current_db_version < 40001 ) {
+		$supported = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->blogmeta}'" ) ? 1 : 0;
+		update_network_option( null, 'site_meta_supported', $supported );
+	}
 }
 
 //
Index: src/wp-includes/class-wp-site-query.php
===================================================================
--- src/wp-includes/class-wp-site-query.php	(revision 41307)
+++ src/wp-includes/class-wp-site-query.php	(working copy)
@@ -137,6 +137,8 @@
 	 *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
 	 *                                           Default empty array.
 	 *     @type bool         $update_site_cache Whether to prime the cache for found sites. Default false.
+	 *     @type bool         $update_site_meta_cache Whether to prime the metadata cache for found sites.
+	 *                                                   Default true.
 	 * }
 	 */
 	public function __construct( $query = '' ) {
@@ -172,6 +174,7 @@
 			'count'             => false,
 			'date_query'        => null, // See WP_Date_Query
 			'update_site_cache' => true,
+			'update_site_meta_cache' => true,
 		);
 
 		if ( ! empty( $query ) ) {
@@ -286,7 +289,7 @@
 
 		// Prime site network caches.
 		if ( $this->query_vars['update_site_cache'] ) {
-			_prime_site_caches( $site_ids );
+			_prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] );
 		}
 
 		// Fetch full site objects from the primed cache.
@@ -302,8 +305,8 @@
 		 *
 		 * @since 4.6.0
 		 *
-		 * @param array         $_sites An array of WP_Site objects.
-		 * @param WP_Site_Query &$this  Current instance of WP_Site_Query, passed by reference.
+		 * @param array         $results An array of sites.
+		 * @param WP_Site_Query &$this   Current instance of WP_Site_Query, passed by reference.
 		 */
 		$_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
 
Index: src/wp-includes/formatting.php
===================================================================
--- src/wp-includes/formatting.php	(revision 41307)
+++ src/wp-includes/formatting.php	(working copy)
@@ -1888,32 +1888,6 @@
 }
 
 /**
- * Sanitizes a string key.
- *
- * Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
- *
- * @since 3.0.0
- *
- * @param string $key String key
- * @return string Sanitized key
- */
-function sanitize_key( $key ) {
-	$raw_key = $key;
-	$key = strtolower( $key );
-	$key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
-
-	/**
-	 * Filters a sanitized key string.
-	 *
-	 * @since 3.0.0
-	 *
-	 * @param string $key     Sanitized key.
-	 * @param string $raw_key The key prior to sanitization.
-	 */
-	return apply_filters( 'sanitize_key', $key, $raw_key );
-}
-
-/**
  * Sanitizes a title, or returns a fallback title.
  *
  * Specifically, HTML and PHP tags are stripped. Further actions can be added
Index: src/wp-includes/functions.php
===================================================================
--- src/wp-includes/functions.php	(revision 41307)
+++ src/wp-includes/functions.php	(working copy)
@@ -4499,6 +4499,31 @@
 }
 
 /**
+ * Determine whether site meta is enabled.
+ *
+ * @since  4.9.0
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @return int
+ */
+function is_site_meta_supported() {
+	global $wpdb;
+
+	if ( ! is_multisite() ) {
+		return 0;
+	}
+
+	$network_id = get_main_network_id();
+
+	$supported = get_network_option( $network_id, 'site_meta_supported', false );
+	if ( false === $supported ) {
+		$supported = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->blogmeta}'" ) ? 1 : 0;
+		update_network_option( $network_id, 'site_meta_supported', $supported );
+	}
+
+	return apply_filters( 'site_meta_supported', $supported );
+}
+
+/**
  * gmt_offset modification for smart timezone handling.
  *
  * Overrides the gmt_offset option if we have a timezone_string available.
Index: src/wp-includes/load.php
===================================================================
--- src/wp-includes/load.php	(revision 41307)
+++ src/wp-includes/load.php	(working copy)
@@ -517,7 +517,7 @@
 	}
 
 	if ( function_exists( 'wp_cache_add_global_groups' ) ) {
-		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) );
+		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'blog_meta' ) );
 		wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 	}
 }
@@ -1112,3 +1112,29 @@
 	 */
 	return apply_filters( 'file_mod_allowed', ! defined( 'DISALLOW_FILE_MODS' ) || ! DISALLOW_FILE_MODS, $context );
 }
+
+/**
+ * Sanitizes a string key.
+ *
+ * Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes and underscores are allowed.
+ *
+ * @since 3.0.0
+ *
+ * @param string $key String key
+ * @return string Sanitized key
+ */
+function sanitize_key( $key ) {
+	$raw_key = $key;
+	$key = strtolower( $key );
+	$key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
+
+	/**
+	 * Filters a sanitized key string.
+	 *
+	 * @since 3.0.0
+	 *
+	 * @param string $key     Sanitized key.
+	 * @param string $raw_key The key prior to sanitization.
+	 */
+	return apply_filters( 'sanitize_key', $key, $raw_key );
+}
\ No newline at end of file
Index: src/wp-includes/ms-blogs.php
===================================================================
--- src/wp-includes/ms-blogs.php	(revision 41307)
+++ src/wp-includes/ms-blogs.php	(working copy)
@@ -465,6 +465,7 @@
 	wp_cache_delete( $domain_path_key, 'blog-id-cache' );
 	wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
 	wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
+	wp_cache_delete( $blog_id, 'blog_meta' );
 
 	/**
 	 * Fires immediately after a site has been removed from the object cache.
@@ -541,21 +542,25 @@
  * Adds any sites from the given ids to the cache that do not already exist in cache.
  *
  * @since 4.6.0
+ * @since 4.9.0 The $update_meta_cache parameter was added.
  * @access private
  *
  * @see update_site_cache()
+ * @see update_sitemeta_cache()
+ *
  * @global wpdb $wpdb WordPress database abstraction object.
  *
- * @param array $ids ID list.
+ * @param array $ids               ID list.
+ * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
  */
-function _prime_site_caches( $ids ) {
+function _prime_site_caches( $ids, $update_meta_cache = true ) {
 	global $wpdb;
 
 	$non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
 	if ( ! empty( $non_cached_ids ) ) {
 		$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
 
-		update_site_cache( $fresh_sites );
+		update_site_cache( $fresh_sites, $update_meta_cache );
 	}
 }
 
@@ -566,15 +571,38 @@
  *
  * @param array $sites Array of site objects.
  */
-function update_site_cache( $sites ) {
+function update_site_cache( $sites, $update_meta_cache = true ) {
 	if ( ! $sites ) {
 		return;
 	}
-
+	$site_ids = array();
 	foreach ( $sites as $site ) {
+		$site_ids[] = $site->blog_id;
 		wp_cache_add( $site->blog_id, $site, 'sites' );
 		wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
 	}
+
+	if ( $update_meta_cache ) {
+		update_sitemeta_cache( $site_ids );
+	}
+}
+
+/**
+ * Updates metadata cache for list of site IDs.
+ *
+ * Performs SQL query to retrieve all metadata for the sites matching `$site_ids` and stores them in the cache.
+ * Subsequent calls to `get_site_meta()` will not need to query the database.
+ *
+ * @since 4.9.0
+ *
+ * @param array $site_ids List of site IDs.
+ * @return array|false Returns false if there is nothing to update. Returns an array of metadata on success.
+ */
+function update_sitemeta_cache( $site_ids ) {
+	if ( ! is_site_meta_supported() ) {
+		return false;
+	}
+	return update_meta_cache( 'blog', $site_ids );
 }
 
 /**
@@ -638,6 +666,7 @@
 	return $query->query( $args );
 }
 
+
 /**
  * Retrieve option value for a given blog id based on name of option.
  *
@@ -768,6 +797,148 @@
 	return $return;
 }
 
+
+
+/**
+ * Add meta data field to a site.
+ *
+ * Site meta data is called "Custom Fields" on the Administration Screen.
+ *
+ * @since 4.9.0
+ *
+ * @param int    $site_id    Site ID.
+ * @param string $meta_key   Metadata name.
+ * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
+ * @param bool   $unique     Optional. Whether the same key should not be added.
+ *                           Default false.
+ * @return int|false Meta ID on success, false on failure.
+ */
+function add_site_meta( $site_id, $meta_key, $meta_value, $unique = false ) {
+	// If site meta not installed, quit early
+	if ( ! is_site_meta_supported() ) {
+		return false;
+	}
+
+	$added = add_metadata( 'blog', $site_id, $meta_key, $meta_value, $unique );
+
+	// Bust site query cache.
+	if ( $added ) {
+		wp_cache_set( 'last_changed', microtime(), 'sites' );
+	}
+
+	return $added;
+}
+
+/**
+ * Remove metadata matching criteria from a site.
+ *
+ * You can match based on the key, or key and value. Removing based on key and
+ * value, will keep from removing duplicate metadata with the same key. It also
+ * allows removing all metadata matching key, if needed.
+ *
+ * @since 4.9.0
+ *
+ * @param int    $site_id    Site ID.
+ * @param string $meta_key   Metadata name.
+ * @param mixed  $meta_value Optional. Metadata value. Must be serializable if
+ *                           non-scalar. Default empty.
+ * @return bool True on success, false on failure.
+ */
+function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) {
+	// If site meta not installed, quit early
+	if ( ! is_site_meta_supported() ) {
+		return false;
+	}
+
+	$deleted = delete_metadata( 'blog', $site_id, $meta_key, $meta_value );
+
+	// Bust site query cache.
+	if ( $deleted ) {
+		wp_cache_set( 'last_changed', microtime(), 'sites' );
+	}
+
+	return $deleted;
+}
+
+/**
+ * Retrieve site meta field for a site.
+ *
+ * @since 4.9.0
+ *
+ * @param int    $site_id Site ID.
+ * @param string $key     Optional. The meta key to retrieve. By default, returns
+ *                        data for all keys. Default empty.
+ * @param bool   $single  Optional. Whether to return a single value. Default false.
+ * @return mixed Will be an array if $single is false. Will be value of meta data
+ *               field if $single is true.
+ */
+function get_site_meta( $site_id, $key = '', $single = false ) {
+	// If site meta not installed, quit early
+	if ( ! is_site_meta_supported() ) {
+		return false;
+	}
+
+	return get_metadata( 'blog', $site_id, $key, $single );
+}
+
+/**
+ * Update site meta field based on site ID.
+ *
+ * Use the $prev_value parameter to differentiate between meta fields with the
+ * same key and site ID.
+ *
+ * If the meta field for the site does not exist, it will be added.
+ *
+ * @since 4.9.0
+ *
+ * @param int    $site_id    Site ID.
+ * @param string $meta_key   Metadata key.
+ * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
+ * @param mixed  $prev_value Optional. Previous value to check before removing.
+ *                           Default empty.
+ * @return int|bool Meta ID if the key didn't exist, true on successful update,
+ *                  false on failure.
+ */
+function update_site_meta( $site_id, $meta_key, $meta_value, $prev_value = '' ) {
+	// If site meta not installed, quit early
+	if ( ! is_site_meta_supported() ) {
+		return false;
+	}
+
+	$updated = update_metadata( 'blog', $site_id, $meta_key, $meta_value, $prev_value );
+
+	// Bust site query cache.
+	if ( $updated ) {
+		wp_cache_set( 'last_changed', microtime(), 'sites' );
+	}
+
+	return $updated;
+}
+
+/**
+ * Delete everything from site meta matching meta key.
+ *
+ * @since 4.9.0
+ *
+ * @param string $meta_key Metadata key to search for when deleting.
+ * @return bool Whether the site meta key was deleted from the database.
+ */
+function delete_site_meta_by_key( $meta_key ) {
+	// If site meta not installed, quit early
+	if ( ! is_site_meta_supported() ) {
+		return false;
+	}
+
+	$deleted = delete_metadata( 'blog', null, $meta_key, '', true );
+
+	// Bust site query cache.
+	if ( $deleted ) {
+		wp_cache_set( 'last_changed', microtime(), 'sites' );
+	}
+
+	return $deleted;
+}
+
 /**
  * Switch the current blog.
  *
@@ -842,7 +1013,7 @@
 			if ( is_array( $global_groups ) ) {
 				wp_cache_add_global_groups( $global_groups );
 			} else {
-				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
+				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
 			}
 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 		}
@@ -916,7 +1087,7 @@
 			if ( is_array( $global_groups ) ) {
 				wp_cache_add_global_groups( $global_groups );
 			} else {
-				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
+				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
 			}
 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 		}
Index: src/wp-includes/option.php
===================================================================
--- src/wp-includes/option.php	(revision 41307)
+++ src/wp-includes/option.php	(working copy)
@@ -225,7 +225,7 @@
 	if ( empty($network_id) )
 		$network_id = get_current_network_id();
 
-	$core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting' );
+	$core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting', 'site_meta_supported' );
 
 	$core_options_in = "'" . implode("', '", $core_options) . "'";
 	$options = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $network_id ) );
Index: src/wp-includes/version.php
===================================================================
--- src/wp-includes/version.php	(revision 41307)
+++ src/wp-includes/version.php	(working copy)
@@ -11,7 +11,7 @@
  *
  * @global int $wp_db_version
  */
-$wp_db_version = 38590;
+$wp_db_version = 40001;
 
 /**
  * Holds the TinyMCE version
Index: src/wp-includes/wp-db.php
===================================================================
--- src/wp-includes/wp-db.php	(revision 41307)
+++ src/wp-includes/wp-db.php	(working copy)
@@ -271,7 +271,7 @@
 	 * @see wpdb::tables()
 	 * @var array
 	 */
-	var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
+	var $ms_global_tables = array( 'blogs', 'blogmeta', 'signups', 'site', 'sitemeta',
 		'sitecategories', 'registration_log', 'blog_versions' );
 
 	/**
@@ -383,6 +383,14 @@
 	public $blogs;
 
 	/**
+	 * Multisite Blog Metadata table
+	 *
+	 * @since 4.9.0
+	 * @var string
+	 */
+	public $blogmeta;
+
+	/**
 	 * Multisite Blog Versions table
 	 *
 	 * @since 3.0.0
Index: src/wp-settings.php
===================================================================
--- src/wp-settings.php	(revision 41307)
+++ src/wp-settings.php	(working copy)
@@ -95,6 +95,7 @@
 // Load early WordPress files.
 require( ABSPATH . WPINC . '/compat.php' );
 require( ABSPATH . WPINC . '/class-wp-list-util.php' );
+require( ABSPATH . WPINC . '/meta.php' );
 require( ABSPATH . WPINC . '/functions.php' );
 require( ABSPATH . WPINC . '/class-wp-matchesmapregex.php' );
 require( ABSPATH . WPINC . '/class-wp.php' );
@@ -157,7 +158,6 @@
 require( ABSPATH . WPINC . '/class-wp-user-query.php' );
 require( ABSPATH . WPINC . '/class-wp-session-tokens.php' );
 require( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' );
-require( ABSPATH . WPINC . '/meta.php' );
 require( ABSPATH . WPINC . '/class-wp-meta-query.php' );
 require( ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php' );
 require( ABSPATH . WPINC . '/general-template.php' );
Index: tests/phpunit/includes/testcase.php
===================================================================
--- tests/phpunit/includes/testcase.php	(revision 41307)
+++ tests/phpunit/includes/testcase.php	(working copy)
@@ -323,7 +323,7 @@
 			$wp_object_cache->__remoteset();
 		}
 		wp_cache_flush();
-		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
+		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
 		wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
 	}
 
