16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 194b82d6e4SYan #include <linux/blkdev.h> 202e635a27SChris Mason #include <linux/module.h> 21e20d96d6SChris Mason #include <linux/buffer_head.h> 222e635a27SChris Mason #include <linux/fs.h> 232e635a27SChris Mason #include <linux/pagemap.h> 242e635a27SChris Mason #include <linux/highmem.h> 252e635a27SChris Mason #include <linux/time.h> 262e635a27SChris Mason #include <linux/init.h> 27a9572a15SEric Paris #include <linux/seq_file.h> 282e635a27SChris Mason #include <linux/string.h> 292e635a27SChris Mason #include <linux/backing-dev.h> 304b82d6e4SYan #include <linux/mount.h> 31dee26a9fSChris Mason #include <linux/mpage.h> 3275dfe396SChris Mason #include <linux/swap.h> 3375dfe396SChris Mason #include <linux/writeback.h> 348fd17795SChris Mason #include <linux/statfs.h> 3508607c1bSChris Mason #include <linux/compat.h> 3695e05289SChris Mason #include <linux/parser.h> 37c59f8951SChris Mason #include <linux/ctype.h> 386da6abaeSChris Mason #include <linux/namei.h> 39a9218f6bSChris Mason #include <linux/miscdevice.h> 401bcbf313SQinghuang Feng #include <linux/magic.h> 415a0e3ad6STejun Heo #include <linux/slab.h> 4290a887c9SDan Magenheimer #include <linux/cleancache.h> 4322c44fe6SJosef Bacik #include <linux/ratelimit.h> 449678c543SNikolay Borisov #include <linux/crc32c.h> 4555e301fdSFilipe Brandenburger #include <linux/btrfs.h> 4616cdcec7SMiao Xie #include "delayed-inode.h" 472e635a27SChris Mason #include "ctree.h" 48e20d96d6SChris Mason #include "disk-io.h" 49d5719762SChris Mason #include "transaction.h" 502c90e5d6SChris Mason #include "btrfs_inode.h" 513a686375SChris Mason #include "print-tree.h" 5263541927SFilipe David Borba Manana #include "props.h" 535103e947SJosef Bacik #include "xattr.h" 548a4b83ccSChris Mason #include "volumes.h" 55be6e8dc0SBalaji Rao #include "export.h" 56c8b97818SChris Mason #include "compression.h" 579c5085c1SJosef Bacik #include "rcu-string.h" 588dabb742SStefan Behrens #include "dev-replace.h" 5974255aa0SJosef Bacik #include "free-space-cache.h" 60b9e9a6cbSWang Shilong #include "backref.h" 61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 622e635a27SChris Mason 63d3982100SMark Fasheh #include "qgroup.h" 641abe9b8aSliubo #define CREATE_TRACE_POINTS 651abe9b8aSliubo #include <trace/events/btrfs.h> 661abe9b8aSliubo 67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 6872fa39f5SMisono, Tomohiro 6972fa39f5SMisono, Tomohiro /* 7072fa39f5SMisono, Tomohiro * Types for mounting the default subvolume and a subvolume explicitly 7172fa39f5SMisono, Tomohiro * requested by subvol=/path. That way the callchain is straightforward and we 7272fa39f5SMisono, Tomohiro * don't have to play tricks with the mount options and recursive calls to 7372fa39f5SMisono, Tomohiro * btrfs_mount. 74312c89fbSMisono, Tomohiro * 75312c89fbSMisono, Tomohiro * The new btrfs_root_fs_type also servers as a tag for the bdev_holder. 7672fa39f5SMisono, Tomohiro */ 77830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 7872fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type; 79e20d96d6SChris Mason 800723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 810723a047SHarald Hoyer 82e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno) 83acce952bSliubo { 8408748810SDavid Sterba char *errstr = "unknown"; 85acce952bSliubo 86acce952bSliubo switch (errno) { 87acce952bSliubo case -EIO: 88acce952bSliubo errstr = "IO failure"; 89acce952bSliubo break; 90acce952bSliubo case -ENOMEM: 91acce952bSliubo errstr = "Out of memory"; 92acce952bSliubo break; 93acce952bSliubo case -EROFS: 94acce952bSliubo errstr = "Readonly filesystem"; 95acce952bSliubo break; 968c342930SJeff Mahoney case -EEXIST: 978c342930SJeff Mahoney errstr = "Object already exists"; 988c342930SJeff Mahoney break; 9994ef7280SDavid Sterba case -ENOSPC: 10094ef7280SDavid Sterba errstr = "No space left"; 10194ef7280SDavid Sterba break; 10294ef7280SDavid Sterba case -ENOENT: 10394ef7280SDavid Sterba errstr = "No such entry"; 10494ef7280SDavid Sterba break; 105acce952bSliubo } 106acce952bSliubo 107acce952bSliubo return errstr; 108acce952bSliubo } 109acce952bSliubo 110acce952bSliubo /* 11134d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 112acce952bSliubo * invokes the approciate error response. 113acce952bSliubo */ 114c0d19e2bSDavid Sterba __cold 11534d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1164da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 117acce952bSliubo { 118acce952bSliubo struct super_block *sb = fs_info->sb; 11957d816a1SAnand Jain #ifdef CONFIG_PRINTK 120acce952bSliubo const char *errstr; 12157d816a1SAnand Jain #endif 122acce952bSliubo 123acce952bSliubo /* 124acce952bSliubo * Special case: if the error is EROFS, and we're already 1251751e8a6SLinus Torvalds * under SB_RDONLY, then it is safe here. 126acce952bSliubo */ 127bc98a42cSDavid Howells if (errno == -EROFS && sb_rdonly(sb)) 128acce952bSliubo return; 129acce952bSliubo 13057d816a1SAnand Jain #ifdef CONFIG_PRINTK 13108748810SDavid Sterba errstr = btrfs_decode_error(errno); 1324da35113SJeff Mahoney if (fmt) { 13337252a66SEric Sandeen struct va_format vaf; 13437252a66SEric Sandeen va_list args; 13537252a66SEric Sandeen 13637252a66SEric Sandeen va_start(args, fmt); 13737252a66SEric Sandeen vaf.fmt = fmt; 13837252a66SEric Sandeen vaf.va = &args; 1394da35113SJeff Mahoney 14062e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 14108748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 14237252a66SEric Sandeen va_end(args); 1434da35113SJeff Mahoney } else { 14462e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 14508748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1464da35113SJeff Mahoney } 14757d816a1SAnand Jain #endif 148acce952bSliubo 1490713d90cSAnand Jain /* 1500713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1510713d90cSAnand Jain * also send it down to the disk 1520713d90cSAnand Jain */ 1530713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1540713d90cSAnand Jain 1554da35113SJeff Mahoney /* Don't go through full error handling during mount */ 156922ea899SAnand Jain if (!(sb->s_flags & SB_BORN)) 157922ea899SAnand Jain return; 158922ea899SAnand Jain 159922ea899SAnand Jain if (sb_rdonly(sb)) 160922ea899SAnand Jain return; 161922ea899SAnand Jain 162922ea899SAnand Jain /* btrfs handle error by forcing the filesystem readonly */ 163922ea899SAnand Jain sb->s_flags |= SB_RDONLY; 164922ea899SAnand Jain btrfs_info(fs_info, "forced readonly"); 165922ea899SAnand Jain /* 166922ea899SAnand Jain * Note that a running device replace operation is not canceled here 167922ea899SAnand Jain * although there is no way to update the progress. It would add the 168922ea899SAnand Jain * risk of a deadlock, therefore the canceling is omitted. The only 169922ea899SAnand Jain * penalty is that some I/O remains active until the procedure 170922ea899SAnand Jain * completes. The next time when the filesystem is mounted writeable 171922ea899SAnand Jain * again, the device replace operation continues. 172922ea899SAnand Jain */ 173acce952bSliubo } 1744da35113SJeff Mahoney 17557d816a1SAnand Jain #ifdef CONFIG_PRINTK 176533574c6SJoe Perches static const char * const logtypes[] = { 1774da35113SJeff Mahoney "emergency", 1784da35113SJeff Mahoney "alert", 1794da35113SJeff Mahoney "critical", 1804da35113SJeff Mahoney "error", 1814da35113SJeff Mahoney "warning", 1824da35113SJeff Mahoney "notice", 1834da35113SJeff Mahoney "info", 1844da35113SJeff Mahoney "debug", 1854da35113SJeff Mahoney }; 1864da35113SJeff Mahoney 18735f4e5e6SNikolay Borisov 18835f4e5e6SNikolay Borisov /* 18935f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 19035f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 19135f4e5e6SNikolay Borisov */ 19235f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 19335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 19435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 19535f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 19635f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 19735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 19835f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 19935f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 20035f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 20135f4e5e6SNikolay Borisov }; 20235f4e5e6SNikolay Borisov 203c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 2044da35113SJeff Mahoney { 20540f7828bSPetr Mladek char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 2064da35113SJeff Mahoney struct va_format vaf; 2074da35113SJeff Mahoney va_list args; 208533574c6SJoe Perches int kern_level; 20940f7828bSPetr Mladek const char *type = logtypes[4]; 21040f7828bSPetr Mladek struct ratelimit_state *ratelimit = &printk_limits[4]; 2114da35113SJeff Mahoney 2124da35113SJeff Mahoney va_start(args, fmt); 2134da35113SJeff Mahoney 214262c5e86SPetr Mladek while ((kern_level = printk_get_level(fmt)) != 0) { 215533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 216262c5e86SPetr Mladek 217262c5e86SPetr Mladek if (kern_level >= '0' && kern_level <= '7') { 218533574c6SJoe Perches memcpy(lvl, fmt, size); 219533574c6SJoe Perches lvl[size] = '\0'; 220533574c6SJoe Perches type = logtypes[kern_level - '0']; 22135f4e5e6SNikolay Borisov ratelimit = &printk_limits[kern_level - '0']; 222262c5e86SPetr Mladek } 223262c5e86SPetr Mladek fmt += size; 224262c5e86SPetr Mladek } 225262c5e86SPetr Mladek 2264da35113SJeff Mahoney vaf.fmt = fmt; 2274da35113SJeff Mahoney vaf.va = &args; 228533574c6SJoe Perches 22935f4e5e6SNikolay Borisov if (__ratelimit(ratelimit)) 2303993b112SColin Ian King printk("%sBTRFS %s (device %s): %pV\n", lvl, type, 2313993b112SColin Ian King fs_info ? fs_info->sb->s_id : "<unknown>", &vaf); 232533574c6SJoe Perches 233533574c6SJoe Perches va_end(args); 2344da35113SJeff Mahoney } 235533574c6SJoe Perches #endif 236533574c6SJoe Perches 2378c342930SJeff Mahoney /* 23849b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 23949b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24049b25e05SJeff Mahoney * one. 24149b25e05SJeff Mahoney * 24249b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24349b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24449b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24549b25e05SJeff Mahoney * in the call path but will return -EIO. 24649b25e05SJeff Mahoney * 24749b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24849b25e05SJeff Mahoney * btrfs_commit_transaction. 24949b25e05SJeff Mahoney */ 250c0d19e2bSDavid Sterba __cold 25149b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25266642832SJeff Mahoney const char *function, 25349b25e05SJeff Mahoney unsigned int line, int errno) 25449b25e05SJeff Mahoney { 25566642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 25666642832SJeff Mahoney 25749b25e05SJeff Mahoney trans->aborted = errno; 25849b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25949b25e05SJeff Mahoney * transaction may be able to continue. */ 26064c12921SJeff Mahoney if (!trans->dirty && list_empty(&trans->new_bgs)) { 26169ce977aSMiao Xie const char *errstr; 26269ce977aSMiao Xie 26308748810SDavid Sterba errstr = btrfs_decode_error(errno); 26466642832SJeff Mahoney btrfs_warn(fs_info, 265c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26669ce977aSMiao Xie function, line, errstr); 26749b25e05SJeff Mahoney return; 26849b25e05SJeff Mahoney } 26920c7bcecSSeraphime Kirkovski WRITE_ONCE(trans->transaction->aborted, errno); 270501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 27166642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 27266642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 27366642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 27449b25e05SJeff Mahoney } 27549b25e05SJeff Mahoney /* 2768c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2778c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2788c342930SJeff Mahoney */ 279c0d19e2bSDavid Sterba __cold 2808c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2818c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2828c342930SJeff Mahoney { 2838c342930SJeff Mahoney char *s_id = "<unknown>"; 2848c342930SJeff Mahoney const char *errstr; 2858c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2868c342930SJeff Mahoney va_list args; 2878c342930SJeff Mahoney 2888c342930SJeff Mahoney if (fs_info) 2898c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2908c342930SJeff Mahoney 2918c342930SJeff Mahoney va_start(args, fmt); 2928c342930SJeff Mahoney vaf.va = &args; 2938c342930SJeff Mahoney 29408748810SDavid Sterba errstr = btrfs_decode_error(errno); 295d8953d69SSatoru Takeuchi if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 29608748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29708748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2988c342930SJeff Mahoney 299efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 300efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3018c342930SJeff Mahoney va_end(args); 3028c342930SJeff Mahoney /* Caller calls BUG() */ 3038c342930SJeff Mahoney } 304acce952bSliubo 305e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 306e20d96d6SChris Mason { 3076bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 308e20d96d6SChris Mason } 3092e635a27SChris Mason 31095e05289SChris Mason enum { 31173f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 312287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 313*62b8e077SHoward McLauchlan Opt_nossd, Opt_ssd_spread, Opt_nossd_spread, Opt_thread_pool, Opt_noacl, 314*62b8e077SHoward McLauchlan Opt_compress, Opt_compress_type, Opt_compress_force, 315*62b8e077SHoward McLauchlan Opt_compress_force_type, Opt_notreelog, Opt_ratio, Opt_flushoncommit, 316*62b8e077SHoward McLauchlan Opt_discard, Opt_space_cache, Opt_space_cache_version, Opt_clear_cache, 31770f6d82eSOmar Sandoval Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid, 31870f6d82eSOmar Sandoval Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery, 31970f6d82eSOmar Sandoval Opt_skip_balance, Opt_check_integrity, 32070f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 321f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 322e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 323a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3248dcddfa0SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot, 325fed8f166SQu Wenruo Opt_nologreplay, Opt_norecovery, 326d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 327d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 328d0bd4560SJosef Bacik #endif 329fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 330fb592373SJosef Bacik Opt_ref_verify, 331fb592373SJosef Bacik #endif 3329555c6c1SIlya Dryomov Opt_err, 33395e05289SChris Mason }; 33495e05289SChris Mason 3354d4ab6d6SDavid Sterba static const match_table_t tokens = { 336dfe25020SChris Mason {Opt_degraded, "degraded"}, 33795e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3381493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33943e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 340b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 341d399167dSQu Wenruo {Opt_datasum, "datasum"}, 342be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 343a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 34421ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 345842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3466f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 347eceff22aSAnand Jain {Opt_alloc_start, "alloc_start=%s"}, /* deprecated */ 348f7b885beSAnand Jain {Opt_thread_pool, "thread_pool=%u"}, 349c8b97818SChris Mason {Opt_compress, "compress"}, 350261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 351a555f810SChris Mason {Opt_compress_force, "compress-force"}, 352261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 353e18e4809SChris Mason {Opt_ssd, "ssd"}, 354451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3553b30c22fSChris Mason {Opt_nossd, "nossd"}, 356*62b8e077SHoward McLauchlan {Opt_nossd_spread, "nossd_spread"}, 357bd0330adSQu Wenruo {Opt_acl, "acl"}, 35833268eafSJosef Bacik {Opt_noacl, "noacl"}, 3593a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 360a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 36196da0919SQu Wenruo {Opt_nologreplay, "nologreplay"}, 362fed8f166SQu Wenruo {Opt_norecovery, "norecovery"}, 363dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3642c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 365764cb8b4SAnand Jain {Opt_ratio, "metadata_ratio=%u"}, 366e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 367e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3680af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 36970f6d82eSOmar Sandoval {Opt_space_cache_version, "space_cache=%s"}, 37088c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3714260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 37291435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 37353036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 374eceff22aSAnand Jain {Opt_subvolrootid, "subvolrootid=%d"}, /* deprecated */ 3754cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 376fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3774b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3783818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3798965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 3808dcddfa0SQu Wenruo {Opt_recovery, "recovery"}, /* deprecated */ 3818dcddfa0SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 3829555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 38321adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 38421adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 38502453bdeSAnand Jain {Opt_check_integrity_print_mask, "check_int_print_mask=%u"}, 386f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3878c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 388d3740608SAnand Jain {Opt_commit_interval, "commit=%u"}, 389d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 390d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 391d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 392d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 393d0bd4560SJosef Bacik #endif 394fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 395fb592373SJosef Bacik {Opt_ref_verify, "ref_verify"}, 396fb592373SJosef Bacik #endif 39733268eafSJosef Bacik {Opt_err, NULL}, 39895e05289SChris Mason }; 39995e05289SChris Mason 400edf24abeSChristoph Hellwig /* 401edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 402edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 40349b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 404edf24abeSChristoph Hellwig */ 4052ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 40696da0919SQu Wenruo unsigned long new_flags) 40795e05289SChris Mason { 40895e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 409e215772cSMisono, Tomohiro char *p, *num; 41073bc1876SJosef Bacik u64 cache_gen; 4114543df7eSChris Mason int intarg; 412a7a3f7caSSage Weil int ret = 0; 413261507a0SLi Zefan char *compress_type; 414261507a0SLi Zefan bool compress_force = false; 415b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 416b7c47bbbSTsutomu Itoh bool saved_compress_force; 417b7c47bbbSTsutomu Itoh int no_compress = 0; 418b6cda9bcSChris Mason 4190b246afaSJeff Mahoney cache_gen = btrfs_super_cache_generation(info->super_copy); 4200b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 42170f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 42270f6d82eSOmar Sandoval else if (cache_gen) 42373bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 42473bc1876SJosef Bacik 42596da0919SQu Wenruo /* 42696da0919SQu Wenruo * Even the options are empty, we still need to do extra check 42796da0919SQu Wenruo * against new flags 42896da0919SQu Wenruo */ 42995e05289SChris Mason if (!options) 43096da0919SQu Wenruo goto check; 43195e05289SChris Mason 43295e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 43395e05289SChris Mason int token; 43495e05289SChris Mason if (!*p) 43595e05289SChris Mason continue; 43695e05289SChris Mason 43795e05289SChris Mason token = match_token(p, tokens, args); 43895e05289SChris Mason switch (token) { 439dfe25020SChris Mason case Opt_degraded: 4400b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 441dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 442dfe25020SChris Mason break; 44395e05289SChris Mason case Opt_subvol: 44473f73415SJosef Bacik case Opt_subvolid: 445e15d0542SXin Zhong case Opt_subvolrootid: 44643e570b0SChristoph Hellwig case Opt_device: 447edf24abeSChristoph Hellwig /* 448d7407606SMisono, Tomohiro * These are parsed by btrfs_parse_subvol_options 449d7407606SMisono, Tomohiro * and btrfs_parse_early_options 450edf24abeSChristoph Hellwig * and can be happily ignored here. 451edf24abeSChristoph Hellwig */ 45295e05289SChris Mason break; 453b6cda9bcSChris Mason case Opt_nodatasum: 4543cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 45507802534SQu Wenruo "setting nodatasum"); 456be20aa9dSChris Mason break; 457d399167dSQu Wenruo case Opt_datasum: 4583cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 4593cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 4600b246afaSJeff Mahoney btrfs_info(info, 4615d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 462d399167dSQu Wenruo else 4630b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 46407802534SQu Wenruo } 465d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 466d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 467d399167dSQu Wenruo break; 468be20aa9dSChris Mason case Opt_nodatacow: 4693cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 4703cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 4713cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 4720b246afaSJeff Mahoney btrfs_info(info, 473efe120a0SFrank Holton "setting nodatacow, compression disabled"); 474bedb2ccaSAndrei Popa } else { 4750b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 476bedb2ccaSAndrei Popa } 47707802534SQu Wenruo } 478bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 479bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 480be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 481be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 482b6cda9bcSChris Mason break; 483a258af7aSQu Wenruo case Opt_datacow: 4843cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 48507802534SQu Wenruo "setting datacow"); 486a258af7aSQu Wenruo break; 487a555f810SChris Mason case Opt_compress_force: 488261507a0SLi Zefan case Opt_compress_force_type: 489261507a0SLi Zefan compress_force = true; 4901c697d4aSEric Sandeen /* Fallthrough */ 491261507a0SLi Zefan case Opt_compress: 492261507a0SLi Zefan case Opt_compress_type: 4933cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 4943cdde224SJeff Mahoney COMPRESS) ? 495b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 496b7c47bbbSTsutomu Itoh saved_compress_force = 4973cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 498261507a0SLi Zefan if (token == Opt_compress || 499261507a0SLi Zefan token == Opt_compress_force || 500a7164fa4SDavid Sterba strncmp(args[0].from, "zlib", 4) == 0) { 501261507a0SLi Zefan compress_type = "zlib"; 502eae8d825SQu Wenruo 503261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 504eae8d825SQu Wenruo info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL; 505eae8d825SQu Wenruo /* 506eae8d825SQu Wenruo * args[0] contains uninitialized data since 507eae8d825SQu Wenruo * for these tokens we don't expect any 508eae8d825SQu Wenruo * parameter. 509eae8d825SQu Wenruo */ 510eae8d825SQu Wenruo if (token != Opt_compress && 511eae8d825SQu Wenruo token != Opt_compress_force) 512f51d2b59SDavid Sterba info->compress_level = 513f51d2b59SDavid Sterba btrfs_compress_str2level(args[0].from); 514063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 515bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 516bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 517b7c47bbbSTsutomu Itoh no_compress = 0; 518a7164fa4SDavid Sterba } else if (strncmp(args[0].from, "lzo", 3) == 0) { 519a6fa6faeSLi Zefan compress_type = "lzo"; 520a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 521063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 522bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 523bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5242b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 525b7c47bbbSTsutomu Itoh no_compress = 0; 5265c1aab1dSNick Terrell } else if (strcmp(args[0].from, "zstd") == 0) { 5275c1aab1dSNick Terrell compress_type = "zstd"; 5285c1aab1dSNick Terrell info->compress_type = BTRFS_COMPRESS_ZSTD; 5295c1aab1dSNick Terrell btrfs_set_opt(info->mount_opt, COMPRESS); 5305c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATACOW); 5315c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATASUM); 5325c1aab1dSNick Terrell btrfs_set_fs_incompat(info, COMPRESS_ZSTD); 5335c1aab1dSNick Terrell no_compress = 0; 534063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 535063849eaSArnd Hannemann compress_type = "no"; 536063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 537063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 538063849eaSArnd Hannemann compress_force = false; 539b7c47bbbSTsutomu Itoh no_compress++; 540261507a0SLi Zefan } else { 541261507a0SLi Zefan ret = -EINVAL; 542261507a0SLi Zefan goto out; 543261507a0SLi Zefan } 544261507a0SLi Zefan 545261507a0SLi Zefan if (compress_force) { 546b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 547143f3636SDavid Sterba } else { 5484027e0f4SWang Shilong /* 5494027e0f4SWang Shilong * If we remount from compress-force=xxx to 5504027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5514027e0f4SWang Shilong * flag, otherwise, there is no way for users 5524027e0f4SWang Shilong * to disable forcible compression separately. 5534027e0f4SWang Shilong */ 5544027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 555a7e252afSMiao Xie } 5563cdde224SJeff Mahoney if ((btrfs_test_opt(info, COMPRESS) && 557b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 558b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 5593cdde224SJeff Mahoney (!btrfs_test_opt(info, COMPRESS) && 560b7c47bbbSTsutomu Itoh no_compress == 1)) { 561f51d2b59SDavid Sterba btrfs_info(info, "%s %s compression, level %d", 562b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 563f51d2b59SDavid Sterba compress_type, info->compress_level); 564b7c47bbbSTsutomu Itoh } 565b7c47bbbSTsutomu Itoh compress_force = false; 566a555f810SChris Mason break; 567e18e4809SChris Mason case Opt_ssd: 5683cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 569583b7231SHans van Kranenburg "enabling ssd optimizations"); 570951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 571e18e4809SChris Mason break; 572451d7585SChris Mason case Opt_ssd_spread: 573583b7231SHans van Kranenburg btrfs_set_and_info(info, SSD, 574583b7231SHans van Kranenburg "enabling ssd optimizations"); 5753cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 576583b7231SHans van Kranenburg "using spread ssd allocation scheme"); 577951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 578451d7585SChris Mason break; 5793b30c22fSChris Mason case Opt_nossd: 580583b7231SHans van Kranenburg btrfs_set_opt(info->mount_opt, NOSSD); 581583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD, 582583b7231SHans van Kranenburg "not using ssd optimizations"); 583*62b8e077SHoward McLauchlan /* Fallthrough */ 584*62b8e077SHoward McLauchlan case Opt_nossd_spread: 585583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD_SPREAD, 586583b7231SHans van Kranenburg "not using spread ssd allocation scheme"); 5873b30c22fSChris Mason break; 588842bef58SQu Wenruo case Opt_barrier: 5893cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 59007802534SQu Wenruo "turning on barriers"); 591842bef58SQu Wenruo break; 59221ad10cfSChris Mason case Opt_nobarrier: 5933cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 59407802534SQu Wenruo "turning off barriers"); 59521ad10cfSChris Mason break; 5964543df7eSChris Mason case Opt_thread_pool: 5972c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5982c334e87SWang Shilong if (ret) { 5992c334e87SWang Shilong goto out; 600f7b885beSAnand Jain } else if (intarg == 0) { 6012c334e87SWang Shilong ret = -EINVAL; 6022c334e87SWang Shilong goto out; 6032c334e87SWang Shilong } 604f7b885beSAnand Jain info->thread_pool_size = intarg; 6054543df7eSChris Mason break; 6066f568d35SChris Mason case Opt_max_inline: 607edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 6086f568d35SChris Mason if (num) { 60991748467SAkinobu Mita info->max_inline = memparse(num, NULL); 6106f568d35SChris Mason kfree(num); 6116f568d35SChris Mason 61215ada040SChris Mason if (info->max_inline) { 613feb5f965SMitch Harder info->max_inline = min_t(u64, 61415ada040SChris Mason info->max_inline, 6150b246afaSJeff Mahoney info->sectorsize); 61615ada040SChris Mason } 6170b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 618c1c9ff7cSGeert Uytterhoeven info->max_inline); 6192c334e87SWang Shilong } else { 6202c334e87SWang Shilong ret = -ENOMEM; 6212c334e87SWang Shilong goto out; 6226f568d35SChris Mason } 6236f568d35SChris Mason break; 6248f662a76SChris Mason case Opt_alloc_start: 6250d0c71b3SDavid Sterba btrfs_info(info, 6260d0c71b3SDavid Sterba "option alloc_start is obsolete, ignored"); 6278f662a76SChris Mason break; 628bd0330adSQu Wenruo case Opt_acl: 62945ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 6301751e8a6SLinus Torvalds info->sb->s_flags |= SB_POSIXACL; 631bd0330adSQu Wenruo break; 63245ff35d6SGuangliang Zhao #else 6330b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 63445ff35d6SGuangliang Zhao ret = -EINVAL; 63545ff35d6SGuangliang Zhao goto out; 63645ff35d6SGuangliang Zhao #endif 63733268eafSJosef Bacik case Opt_noacl: 6381751e8a6SLinus Torvalds info->sb->s_flags &= ~SB_POSIXACL; 63933268eafSJosef Bacik break; 6403a5e1404SSage Weil case Opt_notreelog: 6413cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 64207802534SQu Wenruo "disabling tree log"); 6433a5e1404SSage Weil break; 644a88998f2SQu Wenruo case Opt_treelog: 6453cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 64607802534SQu Wenruo "enabling tree log"); 647a88998f2SQu Wenruo break; 648fed8f166SQu Wenruo case Opt_norecovery: 64996da0919SQu Wenruo case Opt_nologreplay: 6503cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 65196da0919SQu Wenruo "disabling log replay at mount time"); 65296da0919SQu Wenruo break; 653dccae999SSage Weil case Opt_flushoncommit: 6543cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 65507802534SQu Wenruo "turning on flush-on-commit"); 656dccae999SSage Weil break; 6572c9ee856SQu Wenruo case Opt_noflushoncommit: 6583cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 65907802534SQu Wenruo "turning off flush-on-commit"); 6602c9ee856SQu Wenruo break; 66197e728d4SJosef Bacik case Opt_ratio: 6622c334e87SWang Shilong ret = match_int(&args[0], &intarg); 663764cb8b4SAnand Jain if (ret) 6642c334e87SWang Shilong goto out; 66597e728d4SJosef Bacik info->metadata_ratio = intarg; 666764cb8b4SAnand Jain btrfs_info(info, "metadata ratio %u", 66797e728d4SJosef Bacik info->metadata_ratio); 66897e728d4SJosef Bacik break; 669e244a0aeSChristoph Hellwig case Opt_discard: 6703cdde224SJeff Mahoney btrfs_set_and_info(info, DISCARD, 67107802534SQu Wenruo "turning on discard"); 672e244a0aeSChristoph Hellwig break; 673e07a2adeSQu Wenruo case Opt_nodiscard: 6743cdde224SJeff Mahoney btrfs_clear_and_info(info, DISCARD, 67507802534SQu Wenruo "turning off discard"); 676e07a2adeSQu Wenruo break; 6770af3d00bSJosef Bacik case Opt_space_cache: 67870f6d82eSOmar Sandoval case Opt_space_cache_version: 67970f6d82eSOmar Sandoval if (token == Opt_space_cache || 68070f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 6810b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 68270f6d82eSOmar Sandoval FREE_SPACE_TREE); 6833cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 68407802534SQu Wenruo "enabling disk space caching"); 68570f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 6860b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 68770f6d82eSOmar Sandoval SPACE_CACHE); 6880b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 68970f6d82eSOmar Sandoval "enabling free space tree"); 69070f6d82eSOmar Sandoval } else { 69170f6d82eSOmar Sandoval ret = -EINVAL; 69270f6d82eSOmar Sandoval goto out; 69370f6d82eSOmar Sandoval } 6940de90876SJosef Bacik break; 695f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 696f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 697f420ee1eSStefan Behrens break; 69873bc1876SJosef Bacik case Opt_no_space_cache: 6993cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 7000b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 70107802534SQu Wenruo "disabling disk space caching"); 70270f6d82eSOmar Sandoval } 7033cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 7040b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 70570f6d82eSOmar Sandoval "disabling free space tree"); 70670f6d82eSOmar Sandoval } 70773bc1876SJosef Bacik break; 7084b9465cbSChris Mason case Opt_inode_cache: 7097e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 71007802534SQu Wenruo "enabling inode map caching"); 7113818aea2SQu Wenruo break; 7123818aea2SQu Wenruo case Opt_noinode_cache: 7137e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 71407802534SQu Wenruo "disabling inode map caching"); 7154b9465cbSChris Mason break; 71688c2ba3bSJosef Bacik case Opt_clear_cache: 7173cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 71807802534SQu Wenruo "force clearing of disk cache"); 7190af3d00bSJosef Bacik break; 7204260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 7214260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 7224260f7c7SSage Weil break; 72391435650SChris Mason case Opt_enospc_debug: 72491435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 72591435650SChris Mason break; 72653036293SQu Wenruo case Opt_noenospc_debug: 72753036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 72853036293SQu Wenruo break; 7294cb5300bSChris Mason case Opt_defrag: 7303cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 73107802534SQu Wenruo "enabling auto defrag"); 7324cb5300bSChris Mason break; 733fc0ca9afSQu Wenruo case Opt_nodefrag: 7343cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 73507802534SQu Wenruo "disabling auto defrag"); 736fc0ca9afSQu Wenruo break; 737af31f5e5SChris Mason case Opt_recovery: 7380b246afaSJeff Mahoney btrfs_warn(info, 7398dcddfa0SQu Wenruo "'recovery' is deprecated, use 'usebackuproot' instead"); 7408dcddfa0SQu Wenruo case Opt_usebackuproot: 7410b246afaSJeff Mahoney btrfs_info(info, 7428dcddfa0SQu Wenruo "trying to use backup root at mount time"); 7438dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 744af31f5e5SChris Mason break; 7459555c6c1SIlya Dryomov case Opt_skip_balance: 7469555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7479555c6c1SIlya Dryomov break; 74821adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 74921adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 7500b246afaSJeff Mahoney btrfs_info(info, 751efe120a0SFrank Holton "enabling check integrity including extent data"); 75221adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 75321adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 75421adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 75521adbd5cSStefan Behrens break; 75621adbd5cSStefan Behrens case Opt_check_integrity: 7570b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 75821adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 75921adbd5cSStefan Behrens break; 76021adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7612c334e87SWang Shilong ret = match_int(&args[0], &intarg); 76202453bdeSAnand Jain if (ret) 7632c334e87SWang Shilong goto out; 76421adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 76502453bdeSAnand Jain btrfs_info(info, "check_integrity_print_mask 0x%x", 76621adbd5cSStefan Behrens info->check_integrity_print_mask); 76721adbd5cSStefan Behrens break; 76821adbd5cSStefan Behrens #else 76921adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 77021adbd5cSStefan Behrens case Opt_check_integrity: 77121adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7720b246afaSJeff Mahoney btrfs_err(info, 773efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 77421adbd5cSStefan Behrens ret = -EINVAL; 77521adbd5cSStefan Behrens goto out; 77621adbd5cSStefan Behrens #endif 7778c342930SJeff Mahoney case Opt_fatal_errors: 7788c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7798c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7808c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7818c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7828c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7838c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7848c342930SJeff Mahoney else { 7858c342930SJeff Mahoney ret = -EINVAL; 7868c342930SJeff Mahoney goto out; 7878c342930SJeff Mahoney } 7888c342930SJeff Mahoney break; 7898b87dc17SDavid Sterba case Opt_commit_interval: 7908b87dc17SDavid Sterba intarg = 0; 7918b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 792d3740608SAnand Jain if (ret) 7938b87dc17SDavid Sterba goto out; 794d3740608SAnand Jain if (intarg == 0) { 795d3740608SAnand Jain btrfs_info(info, 796d3740608SAnand Jain "using default commit interval %us", 797d3740608SAnand Jain BTRFS_DEFAULT_COMMIT_INTERVAL); 798d3740608SAnand Jain intarg = BTRFS_DEFAULT_COMMIT_INTERVAL; 799d3740608SAnand Jain } else if (intarg > 300) { 800d3740608SAnand Jain btrfs_warn(info, "excessive commit interval %d", 8018b87dc17SDavid Sterba intarg); 8028b87dc17SDavid Sterba } 8038b87dc17SDavid Sterba info->commit_interval = intarg; 8048b87dc17SDavid Sterba break; 805d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 806d0bd4560SJosef Bacik case Opt_fragment_all: 8070b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 808d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 809d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 810d0bd4560SJosef Bacik break; 811d0bd4560SJosef Bacik case Opt_fragment_metadata: 8120b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 813d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 814d0bd4560SJosef Bacik FRAGMENT_METADATA); 815d0bd4560SJosef Bacik break; 816d0bd4560SJosef Bacik case Opt_fragment_data: 8170b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 818d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 819d0bd4560SJosef Bacik break; 820d0bd4560SJosef Bacik #endif 821fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 822fb592373SJosef Bacik case Opt_ref_verify: 823fb592373SJosef Bacik btrfs_info(info, "doing ref verification"); 824fb592373SJosef Bacik btrfs_set_opt(info->mount_opt, REF_VERIFY); 825fb592373SJosef Bacik break; 826fb592373SJosef Bacik #endif 827a7a3f7caSSage Weil case Opt_err: 8280b246afaSJeff Mahoney btrfs_info(info, "unrecognized mount option '%s'", p); 829a7a3f7caSSage Weil ret = -EINVAL; 830a7a3f7caSSage Weil goto out; 83195e05289SChris Mason default: 832be20aa9dSChris Mason break; 83395e05289SChris Mason } 83495e05289SChris Mason } 83596da0919SQu Wenruo check: 83696da0919SQu Wenruo /* 83796da0919SQu Wenruo * Extra check for current option against current flag 83896da0919SQu Wenruo */ 8391751e8a6SLinus Torvalds if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) { 8400b246afaSJeff Mahoney btrfs_err(info, 84196da0919SQu Wenruo "nologreplay must be used with ro mount option"); 84296da0919SQu Wenruo ret = -EINVAL; 84396da0919SQu Wenruo } 844a7a3f7caSSage Weil out: 8450b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 8463cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 8473cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 8480b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 84970f6d82eSOmar Sandoval ret = -EINVAL; 85070f6d82eSOmar Sandoval 85170f6d82eSOmar Sandoval } 8523cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 8530b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 8543cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 8550b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 856a7a3f7caSSage Weil return ret; 857edf24abeSChristoph Hellwig } 858edf24abeSChristoph Hellwig 859edf24abeSChristoph Hellwig /* 860edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 861edf24abeSChristoph Hellwig * 862edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 863edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 864edf24abeSChristoph Hellwig */ 86597288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 866d7407606SMisono, Tomohiro void *holder, struct btrfs_fs_devices **fs_devices) 867edf24abeSChristoph Hellwig { 868edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 86983c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 870d7407606SMisono, Tomohiro int error = 0; 871d7407606SMisono, Tomohiro 872d7407606SMisono, Tomohiro if (!options) 873d7407606SMisono, Tomohiro return 0; 874d7407606SMisono, Tomohiro 875d7407606SMisono, Tomohiro /* 876d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because btrfs_parse_options 877d7407606SMisono, Tomohiro * gets called later 878d7407606SMisono, Tomohiro */ 879d7407606SMisono, Tomohiro opts = kstrdup(options, GFP_KERNEL); 880d7407606SMisono, Tomohiro if (!opts) 881d7407606SMisono, Tomohiro return -ENOMEM; 882d7407606SMisono, Tomohiro orig = opts; 883d7407606SMisono, Tomohiro 884d7407606SMisono, Tomohiro while ((p = strsep(&opts, ",")) != NULL) { 885d7407606SMisono, Tomohiro int token; 886d7407606SMisono, Tomohiro 887d7407606SMisono, Tomohiro if (!*p) 888d7407606SMisono, Tomohiro continue; 889d7407606SMisono, Tomohiro 890d7407606SMisono, Tomohiro token = match_token(p, tokens, args); 891d7407606SMisono, Tomohiro if (token == Opt_device) { 892d7407606SMisono, Tomohiro device_name = match_strdup(&args[0]); 893d7407606SMisono, Tomohiro if (!device_name) { 894d7407606SMisono, Tomohiro error = -ENOMEM; 895d7407606SMisono, Tomohiro goto out; 896d7407606SMisono, Tomohiro } 897d7407606SMisono, Tomohiro error = btrfs_scan_one_device(device_name, 898d7407606SMisono, Tomohiro flags, holder, fs_devices); 899d7407606SMisono, Tomohiro kfree(device_name); 900d7407606SMisono, Tomohiro if (error) 901d7407606SMisono, Tomohiro goto out; 902d7407606SMisono, Tomohiro } 903d7407606SMisono, Tomohiro } 904d7407606SMisono, Tomohiro 905d7407606SMisono, Tomohiro out: 906d7407606SMisono, Tomohiro kfree(orig); 907d7407606SMisono, Tomohiro return error; 908d7407606SMisono, Tomohiro } 909d7407606SMisono, Tomohiro 910d7407606SMisono, Tomohiro /* 911d7407606SMisono, Tomohiro * Parse mount options that are related to subvolume id 912d7407606SMisono, Tomohiro * 913d7407606SMisono, Tomohiro * The value is later passed to mount_subvol() 914d7407606SMisono, Tomohiro */ 915d7407606SMisono, Tomohiro static int btrfs_parse_subvol_options(const char *options, fmode_t flags, 91678f6beacSMisono, Tomohiro char **subvol_name, u64 *subvol_objectid) 917d7407606SMisono, Tomohiro { 918d7407606SMisono, Tomohiro substring_t args[MAX_OPT_ARGS]; 919d7407606SMisono, Tomohiro char *opts, *orig, *p; 920edf24abeSChristoph Hellwig int error = 0; 921ccb0e7d1SAnand Jain u64 subvolid; 922edf24abeSChristoph Hellwig 923edf24abeSChristoph Hellwig if (!options) 924830c4adbSJosef Bacik return 0; 925edf24abeSChristoph Hellwig 926edf24abeSChristoph Hellwig /* 927d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because 928d7407606SMisono, Tomohiro * btrfs_parse_early_options gets called later 929edf24abeSChristoph Hellwig */ 930edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 931edf24abeSChristoph Hellwig if (!opts) 932edf24abeSChristoph Hellwig return -ENOMEM; 9333f3d0bc0STero Roponen orig = opts; 934edf24abeSChristoph Hellwig 935edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 936edf24abeSChristoph Hellwig int token; 937edf24abeSChristoph Hellwig if (!*p) 938edf24abeSChristoph Hellwig continue; 939edf24abeSChristoph Hellwig 940edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 941edf24abeSChristoph Hellwig switch (token) { 942edf24abeSChristoph Hellwig case Opt_subvol: 943a90e8b6fSIlya Dryomov kfree(*subvol_name); 944edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 9452c334e87SWang Shilong if (!*subvol_name) { 9462c334e87SWang Shilong error = -ENOMEM; 9472c334e87SWang Shilong goto out; 9482c334e87SWang Shilong } 949edf24abeSChristoph Hellwig break; 95073f73415SJosef Bacik case Opt_subvolid: 951ccb0e7d1SAnand Jain error = match_u64(&args[0], &subvolid); 952ccb0e7d1SAnand Jain if (error) 9532c334e87SWang Shilong goto out; 954ccb0e7d1SAnand Jain 955ccb0e7d1SAnand Jain /* we want the original fs_tree */ 956ccb0e7d1SAnand Jain if (subvolid == 0) 957ccb0e7d1SAnand Jain subvolid = BTRFS_FS_TREE_OBJECTID; 958ccb0e7d1SAnand Jain 959ccb0e7d1SAnand Jain *subvol_objectid = subvolid; 96073f73415SJosef Bacik break; 961e15d0542SXin Zhong case Opt_subvolrootid: 96262e85577SJeff Mahoney pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n"); 963e15d0542SXin Zhong break; 964edf24abeSChristoph Hellwig default: 965edf24abeSChristoph Hellwig break; 966edf24abeSChristoph Hellwig } 967edf24abeSChristoph Hellwig } 968edf24abeSChristoph Hellwig 969edf24abeSChristoph Hellwig out: 970830c4adbSJosef Bacik kfree(orig); 971edf24abeSChristoph Hellwig return error; 97295e05289SChris Mason } 97395e05289SChris Mason 97405dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 97573f73415SJosef Bacik u64 subvol_objectid) 97673f73415SJosef Bacik { 977815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 97805dbe683SOmar Sandoval struct btrfs_root *fs_root; 97905dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 98005dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 98105dbe683SOmar Sandoval struct btrfs_key key; 98205dbe683SOmar Sandoval struct btrfs_path *path = NULL; 98305dbe683SOmar Sandoval char *name = NULL, *ptr; 98405dbe683SOmar Sandoval u64 dirid; 98505dbe683SOmar Sandoval int len; 98605dbe683SOmar Sandoval int ret; 98705dbe683SOmar Sandoval 98805dbe683SOmar Sandoval path = btrfs_alloc_path(); 98905dbe683SOmar Sandoval if (!path) { 99005dbe683SOmar Sandoval ret = -ENOMEM; 99105dbe683SOmar Sandoval goto err; 99205dbe683SOmar Sandoval } 99305dbe683SOmar Sandoval path->leave_spinning = 1; 99405dbe683SOmar Sandoval 9953ec83621SDavid Sterba name = kmalloc(PATH_MAX, GFP_KERNEL); 99605dbe683SOmar Sandoval if (!name) { 99705dbe683SOmar Sandoval ret = -ENOMEM; 99805dbe683SOmar Sandoval goto err; 99905dbe683SOmar Sandoval } 100005dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 100105dbe683SOmar Sandoval ptr[0] = '\0'; 100205dbe683SOmar Sandoval 100305dbe683SOmar Sandoval /* 100405dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 100505dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 100605dbe683SOmar Sandoval */ 100705dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 100805dbe683SOmar Sandoval key.objectid = subvol_objectid; 100905dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 101005dbe683SOmar Sandoval key.offset = (u64)-1; 101105dbe683SOmar Sandoval 101205dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 101305dbe683SOmar Sandoval if (ret < 0) { 101405dbe683SOmar Sandoval goto err; 101505dbe683SOmar Sandoval } else if (ret > 0) { 101605dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 101705dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 101805dbe683SOmar Sandoval if (ret < 0) { 101905dbe683SOmar Sandoval goto err; 102005dbe683SOmar Sandoval } else if (ret > 0) { 102105dbe683SOmar Sandoval ret = -ENOENT; 102205dbe683SOmar Sandoval goto err; 102305dbe683SOmar Sandoval } 102405dbe683SOmar Sandoval } 102505dbe683SOmar Sandoval 102605dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 102705dbe683SOmar Sandoval subvol_objectid = key.offset; 102805dbe683SOmar Sandoval 102905dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 103005dbe683SOmar Sandoval struct btrfs_root_ref); 103105dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 103205dbe683SOmar Sandoval ptr -= len + 1; 103305dbe683SOmar Sandoval if (ptr < name) { 103405dbe683SOmar Sandoval ret = -ENAMETOOLONG; 103505dbe683SOmar Sandoval goto err; 103605dbe683SOmar Sandoval } 103705dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 103805dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 103905dbe683SOmar Sandoval ptr[0] = '/'; 104005dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 104105dbe683SOmar Sandoval btrfs_release_path(path); 104205dbe683SOmar Sandoval 104305dbe683SOmar Sandoval key.objectid = subvol_objectid; 104405dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 104505dbe683SOmar Sandoval key.offset = (u64)-1; 104605dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 104705dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 104805dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 104905dbe683SOmar Sandoval goto err; 105005dbe683SOmar Sandoval } 105105dbe683SOmar Sandoval 105205dbe683SOmar Sandoval /* 105305dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 105405dbe683SOmar Sandoval * root directory. 105505dbe683SOmar Sandoval */ 105605dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 105705dbe683SOmar Sandoval key.objectid = dirid; 105805dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 105905dbe683SOmar Sandoval key.offset = (u64)-1; 106005dbe683SOmar Sandoval 106105dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 106205dbe683SOmar Sandoval if (ret < 0) { 106305dbe683SOmar Sandoval goto err; 106405dbe683SOmar Sandoval } else if (ret > 0) { 106505dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 106605dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 106705dbe683SOmar Sandoval if (ret < 0) { 106805dbe683SOmar Sandoval goto err; 106905dbe683SOmar Sandoval } else if (ret > 0) { 107005dbe683SOmar Sandoval ret = -ENOENT; 107105dbe683SOmar Sandoval goto err; 107205dbe683SOmar Sandoval } 107305dbe683SOmar Sandoval } 107405dbe683SOmar Sandoval 107505dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 107605dbe683SOmar Sandoval dirid = key.offset; 107705dbe683SOmar Sandoval 107805dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 107905dbe683SOmar Sandoval path->slots[0], 108005dbe683SOmar Sandoval struct btrfs_inode_ref); 108105dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 108205dbe683SOmar Sandoval inode_ref); 108305dbe683SOmar Sandoval ptr -= len + 1; 108405dbe683SOmar Sandoval if (ptr < name) { 108505dbe683SOmar Sandoval ret = -ENAMETOOLONG; 108605dbe683SOmar Sandoval goto err; 108705dbe683SOmar Sandoval } 108805dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 108905dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 109005dbe683SOmar Sandoval ptr[0] = '/'; 109105dbe683SOmar Sandoval btrfs_release_path(path); 109205dbe683SOmar Sandoval } 109305dbe683SOmar Sandoval } 109405dbe683SOmar Sandoval 109505dbe683SOmar Sandoval btrfs_free_path(path); 109605dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 109705dbe683SOmar Sandoval name[0] = '/'; 109805dbe683SOmar Sandoval name[1] = '\0'; 109905dbe683SOmar Sandoval } else { 110005dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 110105dbe683SOmar Sandoval } 110205dbe683SOmar Sandoval return name; 110305dbe683SOmar Sandoval 110405dbe683SOmar Sandoval err: 110505dbe683SOmar Sandoval btrfs_free_path(path); 110605dbe683SOmar Sandoval kfree(name); 110705dbe683SOmar Sandoval return ERR_PTR(ret); 110805dbe683SOmar Sandoval } 110905dbe683SOmar Sandoval 111005dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 111105dbe683SOmar Sandoval { 111205dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 111373f73415SJosef Bacik struct btrfs_dir_item *di; 111473f73415SJosef Bacik struct btrfs_path *path; 111573f73415SJosef Bacik struct btrfs_key location; 111673f73415SJosef Bacik u64 dir_id; 111773f73415SJosef Bacik 111873f73415SJosef Bacik path = btrfs_alloc_path(); 111973f73415SJosef Bacik if (!path) 112005dbe683SOmar Sandoval return -ENOMEM; 112173f73415SJosef Bacik path->leave_spinning = 1; 112273f73415SJosef Bacik 112373f73415SJosef Bacik /* 112473f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 112573f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 112673f73415SJosef Bacik * to mount. 112773f73415SJosef Bacik */ 1128815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 112973f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1130b0839166SJulia Lawall if (IS_ERR(di)) { 1131b0839166SJulia Lawall btrfs_free_path(path); 113205dbe683SOmar Sandoval return PTR_ERR(di); 1133b0839166SJulia Lawall } 113473f73415SJosef Bacik if (!di) { 113573f73415SJosef Bacik /* 113673f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 113773f73415SJosef Bacik * it's always been there, but don't freak out, just try and 113805dbe683SOmar Sandoval * mount the top-level subvolume. 113973f73415SJosef Bacik */ 114073f73415SJosef Bacik btrfs_free_path(path); 114105dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 114205dbe683SOmar Sandoval return 0; 114373f73415SJosef Bacik } 114473f73415SJosef Bacik 114573f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 114673f73415SJosef Bacik btrfs_free_path(path); 114705dbe683SOmar Sandoval *objectid = location.objectid; 114805dbe683SOmar Sandoval return 0; 114973f73415SJosef Bacik } 115073f73415SJosef Bacik 11518a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 11528a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 115356e033a7SDavid Sterba void *data) 11542e635a27SChris Mason { 11552e635a27SChris Mason struct inode *inode; 1156815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 11575d4f98a2SYan Zheng struct btrfs_key key; 115839279cc3SChris Mason int err; 11592e635a27SChris Mason 11602e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 11612e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1162e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1163af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1164be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 11655103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 11662e635a27SChris Mason sb->s_time_gran = 1; 11670eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 11681751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 116949cf6f45SChris Ball #endif 1170357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 1171da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 11729e11ceeeSJan Kara 11739e11ceeeSJan Kara err = super_setup_bdi(sb); 11749e11ceeeSJan Kara if (err) { 11759e11ceeeSJan Kara btrfs_err(fs_info, "super_setup_bdi failed"); 11769e11ceeeSJan Kara return err; 11779e11ceeeSJan Kara } 11789e11ceeeSJan Kara 1179ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1180ad2b2c80SAl Viro if (err) { 1181ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1182ad2b2c80SAl Viro return err; 1183e20d96d6SChris Mason } 1184b888db2bSChris Mason 11855d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 11865d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 11875d4f98a2SYan Zheng key.offset = 0; 118898c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 11895d4f98a2SYan Zheng if (IS_ERR(inode)) { 11905d4f98a2SYan Zheng err = PTR_ERR(inode); 119139279cc3SChris Mason goto fail_close; 119239279cc3SChris Mason } 11932e635a27SChris Mason 119448fde701SAl Viro sb->s_root = d_make_root(inode); 119548fde701SAl Viro if (!sb->s_root) { 119639279cc3SChris Mason err = -ENOMEM; 119739279cc3SChris Mason goto fail_close; 11982e635a27SChris Mason } 119958176a96SJosef Bacik 120090a887c9SDan Magenheimer cleancache_init_fs(sb); 12011751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 12022e635a27SChris Mason return 0; 12032e635a27SChris Mason 120439279cc3SChris Mason fail_close: 12056bccf3abSJeff Mahoney close_ctree(fs_info); 1206d5719762SChris Mason return err; 1207d5719762SChris Mason } 1208d5719762SChris Mason 12096bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1210d5719762SChris Mason { 1211d5719762SChris Mason struct btrfs_trans_handle *trans; 1212815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1213815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1214df2ce34cSChris Mason 1215bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 12161abe9b8aSliubo 1217d561c025SChris Mason if (!wait) { 1218815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1219df2ce34cSChris Mason return 0; 1220d561c025SChris Mason } 1221771ed689SChris Mason 12226374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1223771ed689SChris Mason 1224d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 122560376ce4SJosef Bacik if (IS_ERR(trans)) { 1226354aa0fbSMiao Xie /* no transaction, don't bother */ 12276b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 12286b5fe46dSDavid Sterba /* 12296b5fe46dSDavid Sterba * Exit unless we have some pending changes 12306b5fe46dSDavid Sterba * that need to go through commit 12316b5fe46dSDavid Sterba */ 12326b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1233bd7de2c9SJosef Bacik return 0; 1234a53f4f8eSQu Wenruo /* 1235a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1236a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1237a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1238a53f4f8eSQu Wenruo * next commit after thawing. 1239a53f4f8eSQu Wenruo */ 1240a7e3c5f2SRakesh Pandit if (sb_start_write_trylock(sb)) 1241a7e3c5f2SRakesh Pandit sb_end_write(sb); 1242a53f4f8eSQu Wenruo else 1243a53f4f8eSQu Wenruo return 0; 12446b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 124560376ce4SJosef Bacik } 124698bd5c54SDavid Sterba if (IS_ERR(trans)) 124798bd5c54SDavid Sterba return PTR_ERR(trans); 12486b5fe46dSDavid Sterba } 12493a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1250d5719762SChris Mason } 1251d5719762SChris Mason 125234c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1253a9572a15SEric Paris { 1254815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 12550f628c63SDavid Sterba const char *compress_type; 1256a9572a15SEric Paris 12573cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1258a9572a15SEric Paris seq_puts(seq, ",degraded"); 12593cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1260a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 12613cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1262a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 12633cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1264a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 126595ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1266c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1267a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1268a9572a15SEric Paris num_online_cpus() + 2, 8)) 1269f7b885beSAnand Jain seq_printf(seq, ",thread_pool=%u", info->thread_pool_size); 12703cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 12710f628c63SDavid Sterba compress_type = btrfs_compress_type2str(info->compress_type); 12723cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1273200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1274200da64eSTsutomu Itoh else 1275200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1276f51d2b59SDavid Sterba if (info->compress_level) 1277fa4d885aSAdam Borowski seq_printf(seq, ":%d", info->compress_level); 1278200da64eSTsutomu Itoh } 12793cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1280c289811cSChris Mason seq_puts(seq, ",nossd"); 12813cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1282451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 12833cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1284a9572a15SEric Paris seq_puts(seq, ",ssd"); 12853cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 12866b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 12873cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 128896da0919SQu Wenruo seq_puts(seq, ",nologreplay"); 12893cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 12906b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 12913cdde224SJeff Mahoney if (btrfs_test_opt(info, DISCARD)) 129220a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 12931751e8a6SLinus Torvalds if (!(info->sb->s_flags & SB_POSIXACL)) 1294a9572a15SEric Paris seq_puts(seq, ",noacl"); 12953cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) 1296200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 12973cdde224SJeff Mahoney else if (btrfs_test_opt(info, FREE_SPACE_TREE)) 129870f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 129973bc1876SJosef Bacik else 13008965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 13013cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1302f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 13033cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1304200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 13053cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1306200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 13073cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 13080942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 13093cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 13100942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 13113cdde224SJeff Mahoney if (btrfs_test_opt(info, INODE_MAP_CACHE)) 13120942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 13133cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 13149555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 13158507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 13163cdde224SJeff Mahoney if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 13178507d216SWang Shilong seq_puts(seq, ",check_int_data"); 13183cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 13198507d216SWang Shilong seq_puts(seq, ",check_int"); 13208507d216SWang Shilong if (info->check_integrity_print_mask) 13218507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 13228507d216SWang Shilong info->check_integrity_print_mask); 13238507d216SWang Shilong #endif 13248507d216SWang Shilong if (info->metadata_ratio) 1325764cb8b4SAnand Jain seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio); 13263cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 13278c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 13288b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 1329d3740608SAnand Jain seq_printf(seq, ",commit=%u", info->commit_interval); 1330d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 13313cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1332d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 13333cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1334d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1335d0bd4560SJosef Bacik #endif 1336fb592373SJosef Bacik if (btrfs_test_opt(info, REF_VERIFY)) 1337fb592373SJosef Bacik seq_puts(seq, ",ref_verify"); 1338c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1339c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1340c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1341c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1342a9572a15SEric Paris return 0; 1343a9572a15SEric Paris } 1344a9572a15SEric Paris 1345a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 13462e635a27SChris Mason { 1347815745cfSAl Viro struct btrfs_fs_info *p = data; 1348815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 13494b82d6e4SYan 1350815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 13514b82d6e4SYan } 13524b82d6e4SYan 1353450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1354450ba0eaSJosef Bacik { 13556de1d09dSAl Viro int err = set_anon_super(s, data); 13566de1d09dSAl Viro if (!err) 1357450ba0eaSJosef Bacik s->s_fs_info = data; 13586de1d09dSAl Viro return err; 1359450ba0eaSJosef Bacik } 1360450ba0eaSJosef Bacik 1361830c4adbSJosef Bacik /* 1362f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1363f9d9ef62SDavid Sterba */ 1364f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1365f9d9ef62SDavid Sterba { 1366f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1367f9d9ef62SDavid Sterba return 1; 1368f9d9ef62SDavid Sterba return 0; 1369f9d9ef62SDavid Sterba } 1370f9d9ef62SDavid Sterba 1371bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 13725bedc48aSDavid Sterba const char *device_name, struct vfsmount *mnt) 1373830c4adbSJosef Bacik { 1374830c4adbSJosef Bacik struct dentry *root; 1375fa330659SOmar Sandoval int ret; 1376830c4adbSJosef Bacik 137705dbe683SOmar Sandoval if (!subvol_name) { 137805dbe683SOmar Sandoval if (!subvol_objectid) { 137905dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 138005dbe683SOmar Sandoval &subvol_objectid); 138105dbe683SOmar Sandoval if (ret) { 138205dbe683SOmar Sandoval root = ERR_PTR(ret); 138305dbe683SOmar Sandoval goto out; 138405dbe683SOmar Sandoval } 138505dbe683SOmar Sandoval } 138605dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 138705dbe683SOmar Sandoval subvol_objectid); 138805dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 138905dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 139005dbe683SOmar Sandoval subvol_name = NULL; 139105dbe683SOmar Sandoval goto out; 139205dbe683SOmar Sandoval } 139305dbe683SOmar Sandoval 139405dbe683SOmar Sandoval } 139505dbe683SOmar Sandoval 1396ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1397fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1398fa330659SOmar Sandoval mnt = NULL; 1399830c4adbSJosef Bacik 1400bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1401ea441d11SAl Viro struct super_block *s = root->d_sb; 1402ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1403bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1404bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1405bb289b7bSOmar Sandoval 1406bb289b7bSOmar Sandoval ret = 0; 1407bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1408ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1409bb289b7bSOmar Sandoval subvol_name); 1410bb289b7bSOmar Sandoval ret = -EINVAL; 1411bb289b7bSOmar Sandoval } 1412bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 141305dbe683SOmar Sandoval /* 141405dbe683SOmar Sandoval * This will also catch a race condition where a 141505dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 141605dbe683SOmar Sandoval * another subvolume is renamed over the old location. 141705dbe683SOmar Sandoval */ 1418ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1419ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1420bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1421bb289b7bSOmar Sandoval ret = -EINVAL; 1422bb289b7bSOmar Sandoval } 1423bb289b7bSOmar Sandoval if (ret) { 1424ea441d11SAl Viro dput(root); 1425bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1426ea441d11SAl Viro deactivate_locked_super(s); 1427bb289b7bSOmar Sandoval } 1428f9d9ef62SDavid Sterba } 1429f9d9ef62SDavid Sterba 1430fa330659SOmar Sandoval out: 1431fa330659SOmar Sandoval mntput(mnt); 1432fa330659SOmar Sandoval kfree(subvol_name); 1433830c4adbSJosef Bacik return root; 1434830c4adbSJosef Bacik } 1435450ba0eaSJosef Bacik 1436f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1437f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1438f667aef6SQu Wenruo { 1439f667aef6SQu Wenruo char *secdata = NULL; 1440f667aef6SQu Wenruo int ret = 0; 1441f667aef6SQu Wenruo 1442f667aef6SQu Wenruo secdata = alloc_secdata(); 1443f667aef6SQu Wenruo if (!secdata) 1444f667aef6SQu Wenruo return -ENOMEM; 1445f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1446f667aef6SQu Wenruo if (ret) { 1447f667aef6SQu Wenruo free_secdata(secdata); 1448f667aef6SQu Wenruo return ret; 1449f667aef6SQu Wenruo } 1450f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1451f667aef6SQu Wenruo free_secdata(secdata); 1452f667aef6SQu Wenruo return ret; 1453f667aef6SQu Wenruo } 1454f667aef6SQu Wenruo 1455f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1456f667aef6SQu Wenruo struct super_block *sb, 1457f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1458f667aef6SQu Wenruo { 1459f667aef6SQu Wenruo int ret = 0; 1460f667aef6SQu Wenruo 1461f667aef6SQu Wenruo /* 1462f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1463f667aef6SQu Wenruo * is valid. 1464f667aef6SQu Wenruo */ 1465f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1466f667aef6SQu Wenruo if (ret) 1467f667aef6SQu Wenruo return ret; 1468f667aef6SQu Wenruo 1469a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1470f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1471f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1472f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1473f667aef6SQu Wenruo } else { 1474f667aef6SQu Wenruo /* 1475180e4d47SLuis de Bethencourt * Since SELinux (the only one supporting security_mnt_opts) 1476180e4d47SLuis de Bethencourt * does NOT support changing context during remount/mount of 1477180e4d47SLuis de Bethencourt * the same sb, this must be the same or part of the same 1478180e4d47SLuis de Bethencourt * security options, just free it. 1479f667aef6SQu Wenruo */ 1480f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1481f667aef6SQu Wenruo } 1482a43bb39bSQu Wenruo #endif 1483f667aef6SQu Wenruo return ret; 1484f667aef6SQu Wenruo } 1485f667aef6SQu Wenruo 1486312c89fbSMisono, Tomohiro /* 1487312c89fbSMisono, Tomohiro * Find a superblock for the given device / mount point. 1488312c89fbSMisono, Tomohiro * 1489312c89fbSMisono, Tomohiro * Note: This is based on mount_bdev from fs/super.c with a few additions 1490312c89fbSMisono, Tomohiro * for multiple device setup. Make sure to keep it in sync. 1491312c89fbSMisono, Tomohiro */ 149272fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, 149372fa39f5SMisono, Tomohiro int flags, const char *device_name, void *data) 149472fa39f5SMisono, Tomohiro { 149572fa39f5SMisono, Tomohiro struct block_device *bdev = NULL; 149672fa39f5SMisono, Tomohiro struct super_block *s; 149772fa39f5SMisono, Tomohiro struct btrfs_fs_devices *fs_devices = NULL; 149872fa39f5SMisono, Tomohiro struct btrfs_fs_info *fs_info = NULL; 149972fa39f5SMisono, Tomohiro struct security_mnt_opts new_sec_opts; 150072fa39f5SMisono, Tomohiro fmode_t mode = FMODE_READ; 150172fa39f5SMisono, Tomohiro int error = 0; 150272fa39f5SMisono, Tomohiro 150372fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY)) 150472fa39f5SMisono, Tomohiro mode |= FMODE_WRITE; 150572fa39f5SMisono, Tomohiro 150672fa39f5SMisono, Tomohiro error = btrfs_parse_early_options(data, mode, fs_type, 150772fa39f5SMisono, Tomohiro &fs_devices); 150872fa39f5SMisono, Tomohiro if (error) { 150972fa39f5SMisono, Tomohiro return ERR_PTR(error); 151072fa39f5SMisono, Tomohiro } 151172fa39f5SMisono, Tomohiro 151272fa39f5SMisono, Tomohiro security_init_mnt_opts(&new_sec_opts); 151372fa39f5SMisono, Tomohiro if (data) { 151472fa39f5SMisono, Tomohiro error = parse_security_options(data, &new_sec_opts); 151572fa39f5SMisono, Tomohiro if (error) 151672fa39f5SMisono, Tomohiro return ERR_PTR(error); 151772fa39f5SMisono, Tomohiro } 151872fa39f5SMisono, Tomohiro 151972fa39f5SMisono, Tomohiro error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 152072fa39f5SMisono, Tomohiro if (error) 152172fa39f5SMisono, Tomohiro goto error_sec_opts; 152272fa39f5SMisono, Tomohiro 152372fa39f5SMisono, Tomohiro /* 152472fa39f5SMisono, Tomohiro * Setup a dummy root and fs_info for test/set super. This is because 152572fa39f5SMisono, Tomohiro * we don't actually fill this stuff out until open_ctree, but we need 152672fa39f5SMisono, Tomohiro * it for searching for existing supers, so this lets us do that and 152772fa39f5SMisono, Tomohiro * then open_ctree will properly initialize everything later. 152872fa39f5SMisono, Tomohiro */ 1529a8fd1f71SJeff Mahoney fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 153072fa39f5SMisono, Tomohiro if (!fs_info) { 153172fa39f5SMisono, Tomohiro error = -ENOMEM; 153272fa39f5SMisono, Tomohiro goto error_sec_opts; 153372fa39f5SMisono, Tomohiro } 153472fa39f5SMisono, Tomohiro 153572fa39f5SMisono, Tomohiro fs_info->fs_devices = fs_devices; 153672fa39f5SMisono, Tomohiro 153772fa39f5SMisono, Tomohiro fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 153872fa39f5SMisono, Tomohiro fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 153972fa39f5SMisono, Tomohiro security_init_mnt_opts(&fs_info->security_opts); 154072fa39f5SMisono, Tomohiro if (!fs_info->super_copy || !fs_info->super_for_commit) { 154172fa39f5SMisono, Tomohiro error = -ENOMEM; 154272fa39f5SMisono, Tomohiro goto error_fs_info; 154372fa39f5SMisono, Tomohiro } 154472fa39f5SMisono, Tomohiro 154572fa39f5SMisono, Tomohiro error = btrfs_open_devices(fs_devices, mode, fs_type); 154672fa39f5SMisono, Tomohiro if (error) 154772fa39f5SMisono, Tomohiro goto error_fs_info; 154872fa39f5SMisono, Tomohiro 154972fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { 155072fa39f5SMisono, Tomohiro error = -EACCES; 155172fa39f5SMisono, Tomohiro goto error_close_devices; 155272fa39f5SMisono, Tomohiro } 155372fa39f5SMisono, Tomohiro 155472fa39f5SMisono, Tomohiro bdev = fs_devices->latest_bdev; 155572fa39f5SMisono, Tomohiro s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, 155672fa39f5SMisono, Tomohiro fs_info); 155772fa39f5SMisono, Tomohiro if (IS_ERR(s)) { 155872fa39f5SMisono, Tomohiro error = PTR_ERR(s); 155972fa39f5SMisono, Tomohiro goto error_close_devices; 156072fa39f5SMisono, Tomohiro } 156172fa39f5SMisono, Tomohiro 156272fa39f5SMisono, Tomohiro if (s->s_root) { 156372fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 156472fa39f5SMisono, Tomohiro free_fs_info(fs_info); 156572fa39f5SMisono, Tomohiro if ((flags ^ s->s_flags) & SB_RDONLY) 156672fa39f5SMisono, Tomohiro error = -EBUSY; 156772fa39f5SMisono, Tomohiro } else { 156872fa39f5SMisono, Tomohiro snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 156972fa39f5SMisono, Tomohiro btrfs_sb(s)->bdev_holder = fs_type; 157072fa39f5SMisono, Tomohiro error = btrfs_fill_super(s, fs_devices, data); 157172fa39f5SMisono, Tomohiro } 157272fa39f5SMisono, Tomohiro if (error) { 157372fa39f5SMisono, Tomohiro deactivate_locked_super(s); 157472fa39f5SMisono, Tomohiro goto error_sec_opts; 157572fa39f5SMisono, Tomohiro } 157672fa39f5SMisono, Tomohiro 157772fa39f5SMisono, Tomohiro fs_info = btrfs_sb(s); 157872fa39f5SMisono, Tomohiro error = setup_security_options(fs_info, s, &new_sec_opts); 157972fa39f5SMisono, Tomohiro if (error) { 158072fa39f5SMisono, Tomohiro deactivate_locked_super(s); 158172fa39f5SMisono, Tomohiro goto error_sec_opts; 158272fa39f5SMisono, Tomohiro } 158372fa39f5SMisono, Tomohiro 158472fa39f5SMisono, Tomohiro return dget(s->s_root); 158572fa39f5SMisono, Tomohiro 158672fa39f5SMisono, Tomohiro error_close_devices: 158772fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 158872fa39f5SMisono, Tomohiro error_fs_info: 158972fa39f5SMisono, Tomohiro free_fs_info(fs_info); 159072fa39f5SMisono, Tomohiro error_sec_opts: 159172fa39f5SMisono, Tomohiro security_free_mnt_opts(&new_sec_opts); 159272fa39f5SMisono, Tomohiro return ERR_PTR(error); 159372fa39f5SMisono, Tomohiro } 1594312c89fbSMisono, Tomohiro 1595edf24abeSChristoph Hellwig /* 1596312c89fbSMisono, Tomohiro * Mount function which is called by VFS layer. 1597edf24abeSChristoph Hellwig * 1598312c89fbSMisono, Tomohiro * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() 1599312c89fbSMisono, Tomohiro * which needs vfsmount* of device's root (/). This means device's root has to 1600312c89fbSMisono, Tomohiro * be mounted internally in any case. 1601312c89fbSMisono, Tomohiro * 1602312c89fbSMisono, Tomohiro * Operation flow: 1603312c89fbSMisono, Tomohiro * 1. Parse subvol id related options for later use in mount_subvol(). 1604312c89fbSMisono, Tomohiro * 1605312c89fbSMisono, Tomohiro * 2. Mount device's root (/) by calling vfs_kern_mount(). 1606312c89fbSMisono, Tomohiro * 1607312c89fbSMisono, Tomohiro * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the 1608312c89fbSMisono, Tomohiro * first place. In order to avoid calling btrfs_mount() again, we use 1609312c89fbSMisono, Tomohiro * different file_system_type which is not registered to VFS by 1610312c89fbSMisono, Tomohiro * register_filesystem() (btrfs_root_fs_type). As a result, 1611312c89fbSMisono, Tomohiro * btrfs_mount_root() is called. The return value will be used by 1612312c89fbSMisono, Tomohiro * mount_subtree() in mount_subvol(). 1613312c89fbSMisono, Tomohiro * 1614312c89fbSMisono, Tomohiro * 3. Call mount_subvol() to get the dentry of subvolume. Since there is 1615312c89fbSMisono, Tomohiro * "btrfs subvolume set-default", mount_subvol() is called always. 1616edf24abeSChristoph Hellwig */ 1617061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1618306e16ceSDavid Sterba const char *device_name, void *data) 16194b82d6e4SYan { 1620312c89fbSMisono, Tomohiro struct vfsmount *mnt_root; 1621312c89fbSMisono, Tomohiro struct dentry *root; 162297288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 162373f73415SJosef Bacik char *subvol_name = NULL; 162473f73415SJosef Bacik u64 subvol_objectid = 0; 16254b82d6e4SYan int error = 0; 16264b82d6e4SYan 16271751e8a6SLinus Torvalds if (!(flags & SB_RDONLY)) 162897288f2cSChristoph Hellwig mode |= FMODE_WRITE; 162997288f2cSChristoph Hellwig 163078f6beacSMisono, Tomohiro error = btrfs_parse_subvol_options(data, mode, 1631d7407606SMisono, Tomohiro &subvol_name, &subvol_objectid); 1632f23c8af8SIlya Dryomov if (error) { 1633f23c8af8SIlya Dryomov kfree(subvol_name); 1634061dbc6bSAl Viro return ERR_PTR(error); 1635f23c8af8SIlya Dryomov } 1636edf24abeSChristoph Hellwig 1637312c89fbSMisono, Tomohiro /* mount device's root (/) */ 1638312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); 1639312c89fbSMisono, Tomohiro if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { 1640312c89fbSMisono, Tomohiro if (flags & SB_RDONLY) { 1641312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1642312c89fbSMisono, Tomohiro flags & ~SB_RDONLY, device_name, data); 16434b82d6e4SYan } else { 1644312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1645312c89fbSMisono, Tomohiro flags | SB_RDONLY, device_name, data); 1646312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1647312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1648312c89fbSMisono, Tomohiro goto out; 1649f667aef6SQu Wenruo } 1650f667aef6SQu Wenruo 1651312c89fbSMisono, Tomohiro down_write(&mnt_root->mnt_sb->s_umount); 1652312c89fbSMisono, Tomohiro error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); 1653312c89fbSMisono, Tomohiro up_write(&mnt_root->mnt_sb->s_umount); 1654312c89fbSMisono, Tomohiro if (error < 0) { 1655312c89fbSMisono, Tomohiro root = ERR_PTR(error); 1656312c89fbSMisono, Tomohiro mntput(mnt_root); 1657312c89fbSMisono, Tomohiro goto out; 1658312c89fbSMisono, Tomohiro } 1659312c89fbSMisono, Tomohiro } 1660312c89fbSMisono, Tomohiro } 1661312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1662312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1663312c89fbSMisono, Tomohiro goto out; 1664f667aef6SQu Wenruo } 16654b82d6e4SYan 1666312c89fbSMisono, Tomohiro /* mount_subvol() will free subvol_name and mnt_root */ 16675bedc48aSDavid Sterba root = mount_subvol(subvol_name, subvol_objectid, device_name, mnt_root); 16684b82d6e4SYan 1669312c89fbSMisono, Tomohiro out: 1670312c89fbSMisono, Tomohiro return root; 16714b82d6e4SYan } 16722e635a27SChris Mason 16730d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 1674f7b885beSAnand Jain u32 new_pool_size, u32 old_pool_size) 16750d2450abSSergei Trofimovich { 16760d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 16770d2450abSSergei Trofimovich return; 16780d2450abSSergei Trofimovich 16790d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 16800d2450abSSergei Trofimovich 1681efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 16820d2450abSSergei Trofimovich old_pool_size, new_pool_size); 16830d2450abSSergei Trofimovich 16845cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1685afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1686a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1687e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1688fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1689fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1690fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1691fccb5d86SQu Wenruo new_pool_size); 1692fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1693fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 16945b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1695736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 16960339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1697ff023aacSStefan Behrens new_pool_size); 16980d2450abSSergei Trofimovich } 16990d2450abSSergei Trofimovich 1700f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1701dc81cdc5SMiao Xie { 1702dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1703f42a34b2SMiao Xie } 1704dc81cdc5SMiao Xie 1705f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1706f42a34b2SMiao Xie unsigned long old_opts, int flags) 1707f42a34b2SMiao Xie { 1708dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1709dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 17101751e8a6SLinus Torvalds (flags & SB_RDONLY))) { 1711dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1712dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1713dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 17141751e8a6SLinus Torvalds if (flags & SB_RDONLY) 1715dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1716dc81cdc5SMiao Xie } 1717dc81cdc5SMiao Xie } 1718dc81cdc5SMiao Xie 1719dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1720dc81cdc5SMiao Xie unsigned long old_opts) 1721dc81cdc5SMiao Xie { 1722dc81cdc5SMiao Xie /* 1723180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1724180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1725dc81cdc5SMiao Xie */ 1726dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1727bc98a42cSDavid Howells (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1728dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1729dc81cdc5SMiao Xie } 1730dc81cdc5SMiao Xie 1731dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1732dc81cdc5SMiao Xie } 1733dc81cdc5SMiao Xie 1734c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1735c146afadSYan Zheng { 1736815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1737815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 173849b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 173949b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 174049b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 174149b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 1742f7b885beSAnand Jain u32 old_thread_pool_size = fs_info->thread_pool_size; 1743d612ac59SAnand Jain u32 old_metadata_ratio = fs_info->metadata_ratio; 1744c146afadSYan Zheng int ret; 1745c146afadSYan Zheng 174602b9984dSTheodore Ts'o sync_filesystem(sb); 1747f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1748dc81cdc5SMiao Xie 1749f667aef6SQu Wenruo if (data) { 1750f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1751f667aef6SQu Wenruo 1752f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1753f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1754f667aef6SQu Wenruo if (ret) 1755f667aef6SQu Wenruo goto restore; 1756f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1757f667aef6SQu Wenruo &new_sec_opts); 1758f667aef6SQu Wenruo if (ret) { 1759f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1760f667aef6SQu Wenruo goto restore; 1761f667aef6SQu Wenruo } 1762f667aef6SQu Wenruo } 1763f667aef6SQu Wenruo 17642ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 176549b25e05SJeff Mahoney if (ret) { 176649b25e05SJeff Mahoney ret = -EINVAL; 176749b25e05SJeff Mahoney goto restore; 176849b25e05SJeff Mahoney } 1769b288052eSChris Mason 1770f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 17710d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 17720d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 17730d2450abSSergei Trofimovich 17741751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) 1775dc81cdc5SMiao Xie goto out; 1776c146afadSYan Zheng 17771751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 17788dabb742SStefan Behrens /* 17798dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 17808dabb742SStefan Behrens * the filesystem is busy. 17818dabb742SStefan Behrens */ 178221c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1783361c093dSStefan Behrens 1784361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1785361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1786361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1787361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1788361c093dSStefan Behrens 17891751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 1790c146afadSYan Zheng 1791e44163e1SJeff Mahoney /* 17921751e8a6SLinus Torvalds * Setting SB_RDONLY will put the cleaner thread to 1793e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1794e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1795e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1796e44163e1SJeff Mahoney * unless we clean them up here. 1797e44163e1SJeff Mahoney */ 1798e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1799e44163e1SJeff Mahoney 18008dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 18018dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1802061594efSMiao Xie btrfs_pause_balance(fs_info); 18038dabb742SStefan Behrens 18046bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 180549b25e05SJeff Mahoney if (ret) 180649b25e05SJeff Mahoney goto restore; 1807c146afadSYan Zheng } else { 18080b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 18096ef3de9cSDavid Sterba btrfs_err(fs_info, 1810efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 18116ef3de9cSDavid Sterba ret = -EINVAL; 18126ef3de9cSDavid Sterba goto restore; 18136ef3de9cSDavid Sterba } 18148a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 181549b25e05SJeff Mahoney ret = -EACCES; 181649b25e05SJeff Mahoney goto restore; 18178a3db184SSergei Trofimovich } 18182b82032cSYan Zheng 18196528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) { 1820efe120a0SFrank Holton btrfs_warn(fs_info, 1821efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1822292fd7fcSStefan Behrens ret = -EACCES; 1823292fd7fcSStefan Behrens goto restore; 1824292fd7fcSStefan Behrens } 1825292fd7fcSStefan Behrens 18268a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 182749b25e05SJeff Mahoney ret = -EINVAL; 182849b25e05SJeff Mahoney goto restore; 18298a3db184SSergei Trofimovich } 1830c146afadSYan Zheng 1831815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 183249b25e05SJeff Mahoney if (ret) 183349b25e05SJeff Mahoney goto restore; 1834c146afadSYan Zheng 1835d68fc57bSYan, Zheng /* recover relocation */ 18365f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1837d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 18385f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 183949b25e05SJeff Mahoney if (ret) 184049b25e05SJeff Mahoney goto restore; 1841c146afadSYan Zheng 18422b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 18432b6ba629SIlya Dryomov if (ret) 18442b6ba629SIlya Dryomov goto restore; 18452b6ba629SIlya Dryomov 18468dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 18478dabb742SStefan Behrens if (ret) { 1848efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 18498dabb742SStefan Behrens goto restore; 18508dabb742SStefan Behrens } 185194aebfb2SJosef Bacik 18526c6b5a39SAleksa Sarai btrfs_qgroup_rescan_resume(fs_info); 18536c6b5a39SAleksa Sarai 185494aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1855efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 185694aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 185794aebfb2SJosef Bacik if (ret) { 18585d163e0eSJeff Mahoney btrfs_warn(fs_info, 18595d163e0eSJeff Mahoney "failed to create the UUID tree %d", 18605d163e0eSJeff Mahoney ret); 186194aebfb2SJosef Bacik goto restore; 186294aebfb2SJosef Bacik } 186394aebfb2SJosef Bacik } 18641751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 186590c711abSZygo Blaxell 1866afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 1867c146afadSYan Zheng } 1868dc81cdc5SMiao Xie out: 18692c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1870dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1871c146afadSYan Zheng return 0; 187249b25e05SJeff Mahoney 187349b25e05SJeff Mahoney restore: 18741751e8a6SLinus Torvalds /* We've hit an error - don't reset SB_RDONLY */ 1875bc98a42cSDavid Howells if (sb_rdonly(sb)) 18761751e8a6SLinus Torvalds old_flags |= SB_RDONLY; 187749b25e05SJeff Mahoney sb->s_flags = old_flags; 187849b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 187949b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 188049b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 18810d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 18820d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 188349b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1884dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 188549b25e05SJeff Mahoney return ret; 1886c146afadSYan Zheng } 1887c146afadSYan Zheng 1888bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1889bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1890bcd53741SArne Jansen const void *dev_info2) 1891bcd53741SArne Jansen { 1892bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1893bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1894bcd53741SArne Jansen return -1; 1895bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1896bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1897bcd53741SArne Jansen return 1; 1898bcd53741SArne Jansen else 1899bcd53741SArne Jansen return 0; 1900bcd53741SArne Jansen } 1901bcd53741SArne Jansen 1902bcd53741SArne Jansen /* 1903bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1904bcd53741SArne Jansen * is stored.(Descending Sort) 1905bcd53741SArne Jansen */ 1906bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1907bcd53741SArne Jansen struct btrfs_device_info *devices, 1908bcd53741SArne Jansen size_t nr_devices) 1909bcd53741SArne Jansen { 1910bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1911bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1912bcd53741SArne Jansen } 1913bcd53741SArne Jansen 19146d07bcecSMiao Xie /* 19156d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 19166d07bcecSMiao Xie * file data. 19176d07bcecSMiao Xie */ 19186bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 19196bccf3abSJeff Mahoney u64 *free_bytes) 19206d07bcecSMiao Xie { 19216d07bcecSMiao Xie struct btrfs_device_info *devices_info; 19226d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 19236d07bcecSMiao Xie struct btrfs_device *device; 19246d07bcecSMiao Xie u64 skip_space; 19256d07bcecSMiao Xie u64 type; 19266d07bcecSMiao Xie u64 avail_space; 19276d07bcecSMiao Xie u64 min_stripe_size; 192839fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 19296d07bcecSMiao Xie int i = 0, nr_devices; 19306d07bcecSMiao Xie 19317e33fd99SJosef Bacik /* 193201327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 19337e33fd99SJosef Bacik * enough for our purposes. 19347e33fd99SJosef Bacik */ 1935b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 19367e33fd99SJosef Bacik if (!nr_devices) { 19377e33fd99SJosef Bacik smp_mb(); 19387e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 19397e33fd99SJosef Bacik ASSERT(nr_devices); 19407e33fd99SJosef Bacik if (!nr_devices) { 19417e33fd99SJosef Bacik *free_bytes = 0; 19427e33fd99SJosef Bacik return 0; 19437e33fd99SJosef Bacik } 19447e33fd99SJosef Bacik } 19456d07bcecSMiao Xie 1946d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 19476a44517dSDavid Sterba GFP_KERNEL); 19486d07bcecSMiao Xie if (!devices_info) 19496d07bcecSMiao Xie return -ENOMEM; 19506d07bcecSMiao Xie 195101327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 19521b86826dSJeff Mahoney type = btrfs_data_alloc_profile(fs_info); 195339fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 19546d07bcecSMiao Xie min_stripes = 2; 195539fb26c3SMiao Xie num_stripes = nr_devices; 195639fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 19576d07bcecSMiao Xie min_stripes = 2; 195839fb26c3SMiao Xie num_stripes = 2; 195939fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 19606d07bcecSMiao Xie min_stripes = 4; 196139fb26c3SMiao Xie num_stripes = 4; 196239fb26c3SMiao Xie } 19636d07bcecSMiao Xie 19646d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 19656d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 19666d07bcecSMiao Xie else 19676d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 19686d07bcecSMiao Xie 19697e33fd99SJosef Bacik rcu_read_lock(); 19707e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 1971e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, 1972e12c9621SAnand Jain &device->dev_state) || 1973401e29c1SAnand Jain !device->bdev || 1974401e29c1SAnand Jain test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) 19756d07bcecSMiao Xie continue; 19766d07bcecSMiao Xie 19777e33fd99SJosef Bacik if (i >= nr_devices) 19787e33fd99SJosef Bacik break; 19797e33fd99SJosef Bacik 19806d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 19816d07bcecSMiao Xie 19826d07bcecSMiao Xie /* align with stripe_len */ 1983f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 19846d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 19856d07bcecSMiao Xie 19866d07bcecSMiao Xie /* 198701327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 19886d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 19896d07bcecSMiao Xie * allocation. 19906d07bcecSMiao Xie */ 1991ee22184bSByongho Lee skip_space = SZ_1M; 19926d07bcecSMiao Xie 19936d07bcecSMiao Xie /* 19946d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 19956d07bcecSMiao Xie * it from the total. 19966d07bcecSMiao Xie */ 19976d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 19986d07bcecSMiao Xie avail_space -= skip_space; 19996d07bcecSMiao Xie else 20006d07bcecSMiao Xie avail_space = 0; 20016d07bcecSMiao Xie 20026d07bcecSMiao Xie if (avail_space < min_stripe_size) 20036d07bcecSMiao Xie continue; 20046d07bcecSMiao Xie 20056d07bcecSMiao Xie devices_info[i].dev = device; 20066d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 20076d07bcecSMiao Xie 20086d07bcecSMiao Xie i++; 20096d07bcecSMiao Xie } 20107e33fd99SJosef Bacik rcu_read_unlock(); 20116d07bcecSMiao Xie 20126d07bcecSMiao Xie nr_devices = i; 20136d07bcecSMiao Xie 20146d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 20156d07bcecSMiao Xie 20166d07bcecSMiao Xie i = nr_devices - 1; 20176d07bcecSMiao Xie avail_space = 0; 20186d07bcecSMiao Xie while (nr_devices >= min_stripes) { 201939fb26c3SMiao Xie if (num_stripes > nr_devices) 202039fb26c3SMiao Xie num_stripes = nr_devices; 202139fb26c3SMiao Xie 20226d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 20236d07bcecSMiao Xie int j; 20246d07bcecSMiao Xie u64 alloc_size; 20256d07bcecSMiao Xie 202639fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 20276d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 202839fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 20296d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 20306d07bcecSMiao Xie } 20316d07bcecSMiao Xie i--; 20326d07bcecSMiao Xie nr_devices--; 20336d07bcecSMiao Xie } 20346d07bcecSMiao Xie 20356d07bcecSMiao Xie kfree(devices_info); 20366d07bcecSMiao Xie *free_bytes = avail_space; 20376d07bcecSMiao Xie return 0; 20386d07bcecSMiao Xie } 20396d07bcecSMiao Xie 2040ba7b6e62SDavid Sterba /* 2041ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2042ba7b6e62SDavid Sterba * 2043ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2044ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2045ba7b6e62SDavid Sterba * 2046ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 20470d0c71b3SDavid Sterba * algorithm that respects the device sizes and order of allocations. This is 20480d0c71b3SDavid Sterba * a close approximation of the actual use but there are other factors that may 20490d0c71b3SDavid Sterba * change the result (like a new metadata chunk). 2050ba7b6e62SDavid Sterba * 2051ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2052ba7b6e62SDavid Sterba */ 20538fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20548fd17795SChris Mason { 2055815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2056815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2057815745cfSAl Viro struct list_head *head = &fs_info->space_info; 2058bd4d1088SJosef Bacik struct btrfs_space_info *found; 2059bd4d1088SJosef Bacik u64 total_used = 0; 20606d07bcecSMiao Xie u64 total_free_data = 0; 2061ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2062db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2063815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 2064ba7b6e62SDavid Sterba unsigned factor = 1; 2065ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 20666d07bcecSMiao Xie int ret; 2067ca8a51b3SDavid Sterba u64 thresh = 0; 2068ae02d1bdSLuis de Bethencourt int mixed = 0; 20698fd17795SChris Mason 2070bd4d1088SJosef Bacik rcu_read_lock(); 207189a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 20726d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2073ba7b6e62SDavid Sterba int i; 2074ba7b6e62SDavid Sterba 20756d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 20766d07bcecSMiao Xie total_free_data -= 20776d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2078ba7b6e62SDavid Sterba 2079ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2080ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 2081ba7b6e62SDavid Sterba switch (i) { 2082ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 2083ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 2084ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 2085ba7b6e62SDavid Sterba factor = 2; 2086ba7b6e62SDavid Sterba } 2087ba7b6e62SDavid Sterba } 2088ba7b6e62SDavid Sterba } 20896d07bcecSMiao Xie } 2090ae02d1bdSLuis de Bethencourt 2091ae02d1bdSLuis de Bethencourt /* 2092ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2093ae02d1bdSLuis de Bethencourt */ 2094ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2095ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2096ae02d1bdSLuis de Bethencourt mixed = 1; 2097ae02d1bdSLuis de Bethencourt else 2098ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2099ae02d1bdSLuis de Bethencourt found->disk_used; 2100ae02d1bdSLuis de Bethencourt } 21016d07bcecSMiao Xie 2102b742bb82SYan, Zheng total_used += found->disk_used; 210389a55897SJosef Bacik } 2104ba7b6e62SDavid Sterba 2105bd4d1088SJosef Bacik rcu_read_unlock(); 2106bd4d1088SJosef Bacik 2107ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2108ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2109ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2110ba7b6e62SDavid Sterba 2111ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2112ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 211341b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 211441b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2115ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 211641b34accSLuis de Bethencourt else 211741b34accSLuis de Bethencourt buf->f_bfree = 0; 2118ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2119ba7b6e62SDavid Sterba 21200d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 21216bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 21227e33fd99SJosef Bacik if (ret) 21236d07bcecSMiao Xie return ret; 2124ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 21256d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2126d397712bSChris Mason 2127ca8a51b3SDavid Sterba /* 2128ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2129ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2130ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2131ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2132ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2133ca8a51b3SDavid Sterba * 2134ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2135ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2136ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2137ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2138ca8a51b3SDavid Sterba * way around. 2139ca8a51b3SDavid Sterba */ 2140d4417e22SNikolay Borisov thresh = SZ_4M; 2141ca8a51b3SDavid Sterba 2142ae02d1bdSLuis de Bethencourt if (!mixed && total_free_meta - thresh < block_rsv->size) 2143ca8a51b3SDavid Sterba buf->f_bavail = 0; 2144ca8a51b3SDavid Sterba 2145ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2146ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2147ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2148ba7b6e62SDavid Sterba 21499d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 21509d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21519d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21529d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21539d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 215432d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 21552b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 21562b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 215732d48fa1SDavid Woodhouse 21588fd17795SChris Mason return 0; 21598fd17795SChris Mason } 2160b5133862SChris Mason 2161aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2162aea52e19SAl Viro { 2163815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2164aea52e19SAl Viro kill_anon_super(sb); 2165aea52e19SAl Viro free_fs_info(fs_info); 2166aea52e19SAl Viro } 2167aea52e19SAl Viro 21682e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 21692e635a27SChris Mason .owner = THIS_MODULE, 21702e635a27SChris Mason .name = "btrfs", 2171061dbc6bSAl Viro .mount = btrfs_mount, 2172aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2173f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 21742e635a27SChris Mason }; 217572fa39f5SMisono, Tomohiro 217672fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = { 217772fa39f5SMisono, Tomohiro .owner = THIS_MODULE, 217872fa39f5SMisono, Tomohiro .name = "btrfs", 217972fa39f5SMisono, Tomohiro .mount = btrfs_mount_root, 218072fa39f5SMisono, Tomohiro .kill_sb = btrfs_kill_super, 218172fa39f5SMisono, Tomohiro .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 218272fa39f5SMisono, Tomohiro }; 218372fa39f5SMisono, Tomohiro 21847f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2185a9218f6bSChris Mason 2186d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2187d8620958STom Van Braeckel { 2188d8620958STom Van Braeckel /* 2189d8620958STom Van Braeckel * The control file's private_data is used to hold the 2190d8620958STom Van Braeckel * transaction when it is started and is used to keep 2191d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2192d8620958STom Van Braeckel */ 2193d8620958STom Van Braeckel file->private_data = NULL; 2194d8620958STom Van Braeckel return 0; 2195d8620958STom Van Braeckel } 2196d8620958STom Van Braeckel 2197d352ac68SChris Mason /* 2198d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2199d352ac68SChris Mason */ 22008a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 22018a4b83ccSChris Mason unsigned long arg) 22028a4b83ccSChris Mason { 22038a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 22048a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2205c071fcfdSChris Mason int ret = -ENOTTY; 22068a4b83ccSChris Mason 2207e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2208e441d54dSChris Mason return -EPERM; 2209e441d54dSChris Mason 2210dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2211dae7b665SLi Zefan if (IS_ERR(vol)) 2212dae7b665SLi Zefan return PTR_ERR(vol); 2213c071fcfdSChris Mason 22148a4b83ccSChris Mason switch (cmd) { 22158a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 221697288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 2217312c89fbSMisono, Tomohiro &btrfs_root_fs_type, &fs_devices); 22188a4b83ccSChris Mason break; 221902db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 222002db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 2221312c89fbSMisono, Tomohiro &btrfs_root_fs_type, &fs_devices); 222202db0844SJosef Bacik if (ret) 222302db0844SJosef Bacik break; 222402db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 222502db0844SJosef Bacik break; 2226c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2227d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2228c5868f83SDavid Sterba break; 22298a4b83ccSChris Mason } 2230dae7b665SLi Zefan 22318a4b83ccSChris Mason kfree(vol); 2232f819d837SLinda Knippers return ret; 22338a4b83ccSChris Mason } 22348a4b83ccSChris Mason 22350176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2236ed0dab6bSYan { 2237354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 22380b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 22390b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2240354aa0fbSMiao Xie 2241fac03c8dSDavid Sterba set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22429e7cc91aSWang Xiaoguang /* 22439e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 22449e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 22459e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 22469e7cc91aSWang Xiaoguang * ourselves. 22479e7cc91aSWang Xiaoguang */ 2248d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2249354aa0fbSMiao Xie if (IS_ERR(trans)) { 2250354aa0fbSMiao Xie /* no transaction, don't bother */ 2251354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 22520176260fSLinus Torvalds return 0; 2253354aa0fbSMiao Xie return PTR_ERR(trans); 2254354aa0fbSMiao Xie } 22553a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2256ed0dab6bSYan } 2257ed0dab6bSYan 22589e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 22599e7cc91aSWang Xiaoguang { 2260fac03c8dSDavid Sterba struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2261fac03c8dSDavid Sterba 2262fac03c8dSDavid Sterba clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22639e7cc91aSWang Xiaoguang return 0; 22649e7cc91aSWang Xiaoguang } 22659e7cc91aSWang Xiaoguang 22669c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 22679c5085c1SJosef Bacik { 22689c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 22699c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 22709c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 22719c5085c1SJosef Bacik struct list_head *head; 22729c5085c1SJosef Bacik struct rcu_string *name; 22739c5085c1SJosef Bacik 22749c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 22759c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 22769c5085c1SJosef Bacik while (cur_devices) { 22779c5085c1SJosef Bacik head = &cur_devices->devices; 22789c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2279e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 2280aa9ddcd4SJosef Bacik continue; 22810aeb8a6eSAnand Jain if (!dev->name) 22820aeb8a6eSAnand Jain continue; 22839c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 22849c5085c1SJosef Bacik first_dev = dev; 22859c5085c1SJosef Bacik } 22869c5085c1SJosef Bacik cur_devices = cur_devices->seed; 22879c5085c1SJosef Bacik } 22889c5085c1SJosef Bacik 22899c5085c1SJosef Bacik if (first_dev) { 22909c5085c1SJosef Bacik rcu_read_lock(); 22919c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 22929c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 22939c5085c1SJosef Bacik rcu_read_unlock(); 22949c5085c1SJosef Bacik } else { 22959c5085c1SJosef Bacik WARN_ON(1); 22969c5085c1SJosef Bacik } 22979c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 22989c5085c1SJosef Bacik return 0; 22999c5085c1SJosef Bacik } 23009c5085c1SJosef Bacik 2301b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 230276dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2303bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2304e20d96d6SChris Mason .put_super = btrfs_put_super, 2305d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2306a9572a15SEric Paris .show_options = btrfs_show_options, 23079c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 23084730a4bcSChris Mason .write_inode = btrfs_write_inode, 23092c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 23102c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 23118fd17795SChris Mason .statfs = btrfs_statfs, 2312c146afadSYan Zheng .remount_fs = btrfs_remount, 23130176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 23149e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2315e20d96d6SChris Mason }; 2316a9218f6bSChris Mason 2317a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2318d8620958STom Van Braeckel .open = btrfs_control_open, 2319a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2320a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2321a9218f6bSChris Mason .owner = THIS_MODULE, 23226038f373SArnd Bergmann .llseek = noop_llseek, 2323a9218f6bSChris Mason }; 2324a9218f6bSChris Mason 2325a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2326578454ffSKay Sievers .minor = BTRFS_MINOR, 2327a9218f6bSChris Mason .name = "btrfs-control", 2328a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2329a9218f6bSChris Mason }; 2330a9218f6bSChris Mason 2331578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2332578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2333578454ffSKay Sievers 2334f5c29bd9SLiu Bo static int __init btrfs_interface_init(void) 2335a9218f6bSChris Mason { 2336a9218f6bSChris Mason return misc_register(&btrfs_misc); 2337a9218f6bSChris Mason } 2338a9218f6bSChris Mason 2339e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void) 2340a9218f6bSChris Mason { 2341f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2342a9218f6bSChris Mason } 2343a9218f6bSChris Mason 2344f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void) 234585965600SDavid Sterba { 234662e85577SJeff Mahoney pr_info("Btrfs loaded, crc32c=%s" 234785965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 234885965600SDavid Sterba ", debug=on" 234985965600SDavid Sterba #endif 235079556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 235179556c3dSStefan Behrens ", assert=on" 235279556c3dSStefan Behrens #endif 235385965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 235485965600SDavid Sterba ", integrity-checker=on" 235585965600SDavid Sterba #endif 2356fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 2357fb592373SJosef Bacik ", ref-verify=on" 2358fb592373SJosef Bacik #endif 23595f9e1059SJeff Mahoney "\n", 23609678c543SNikolay Borisov crc32c_impl()); 236185965600SDavid Sterba } 236285965600SDavid Sterba 23632e635a27SChris Mason static int __init init_btrfs_fs(void) 23642e635a27SChris Mason { 23652c90e5d6SChris Mason int err; 236658176a96SJosef Bacik 236763541927SFilipe David Borba Manana btrfs_props_init(); 236863541927SFilipe David Borba Manana 236958176a96SJosef Bacik err = btrfs_init_sysfs(); 237058176a96SJosef Bacik if (err) 23719678c543SNikolay Borisov return err; 237258176a96SJosef Bacik 2373143bede5SJeff Mahoney btrfs_init_compress(); 2374d1310b2eSChris Mason 2375261507a0SLi Zefan err = btrfs_init_cachep(); 2376261507a0SLi Zefan if (err) 2377261507a0SLi Zefan goto free_compress; 2378261507a0SLi Zefan 2379d1310b2eSChris Mason err = extent_io_init(); 23802f4cbe64SWyatt Banks if (err) 23812f4cbe64SWyatt Banks goto free_cachep; 23822f4cbe64SWyatt Banks 2383d1310b2eSChris Mason err = extent_map_init(); 2384d1310b2eSChris Mason if (err) 2385d1310b2eSChris Mason goto free_extent_io; 2386d1310b2eSChris Mason 23876352b91dSMiao Xie err = ordered_data_init(); 23882f4cbe64SWyatt Banks if (err) 23892f4cbe64SWyatt Banks goto free_extent_map; 2390c8b97818SChris Mason 23916352b91dSMiao Xie err = btrfs_delayed_inode_init(); 23926352b91dSMiao Xie if (err) 23936352b91dSMiao Xie goto free_ordered_data; 23946352b91dSMiao Xie 23959247f317SMiao Xie err = btrfs_auto_defrag_init(); 239616cdcec7SMiao Xie if (err) 239716cdcec7SMiao Xie goto free_delayed_inode; 239816cdcec7SMiao Xie 239978a6184aSMiao Xie err = btrfs_delayed_ref_init(); 24009247f317SMiao Xie if (err) 24019247f317SMiao Xie goto free_auto_defrag; 24029247f317SMiao Xie 2403b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2404b9e9a6cbSWang Shilong if (err) 2405af13b492SDavid Sterba goto free_delayed_ref; 2406b9e9a6cbSWang Shilong 240797eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 240878a6184aSMiao Xie if (err) 2409af13b492SDavid Sterba goto free_prelim_ref; 241078a6184aSMiao Xie 241197eb6b69SDavid Sterba err = btrfs_interface_init(); 241297eb6b69SDavid Sterba if (err) 241397eb6b69SDavid Sterba goto free_end_io_wq; 241497eb6b69SDavid Sterba 2415e565d4b9SJan Schmidt btrfs_init_lockdep(); 2416e565d4b9SJan Schmidt 24178ae1af3cSAnand Jain btrfs_print_mod_info(); 2418dc11dd5dSJosef Bacik 2419dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2420dc11dd5dSJosef Bacik if (err) 2421dc11dd5dSJosef Bacik goto unregister_ioctl; 2422dc11dd5dSJosef Bacik 2423dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2424dc11dd5dSJosef Bacik if (err) 2425dc11dd5dSJosef Bacik goto unregister_ioctl; 242674255aa0SJosef Bacik 24272f4cbe64SWyatt Banks return 0; 24282f4cbe64SWyatt Banks 2429a9218f6bSChris Mason unregister_ioctl: 2430a9218f6bSChris Mason btrfs_interface_exit(); 243197eb6b69SDavid Sterba free_end_io_wq: 243297eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2433b9e9a6cbSWang Shilong free_prelim_ref: 2434b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 243578a6184aSMiao Xie free_delayed_ref: 243678a6184aSMiao Xie btrfs_delayed_ref_exit(); 24379247f317SMiao Xie free_auto_defrag: 24389247f317SMiao Xie btrfs_auto_defrag_exit(); 243916cdcec7SMiao Xie free_delayed_inode: 244016cdcec7SMiao Xie btrfs_delayed_inode_exit(); 24416352b91dSMiao Xie free_ordered_data: 24426352b91dSMiao Xie ordered_data_exit(); 24432f4cbe64SWyatt Banks free_extent_map: 24442f4cbe64SWyatt Banks extent_map_exit(); 2445d1310b2eSChris Mason free_extent_io: 2446d1310b2eSChris Mason extent_io_exit(); 24472f4cbe64SWyatt Banks free_cachep: 24482f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2449261507a0SLi Zefan free_compress: 2450261507a0SLi Zefan btrfs_exit_compress(); 24512f4cbe64SWyatt Banks btrfs_exit_sysfs(); 24529678c543SNikolay Borisov 24532c90e5d6SChris Mason return err; 24542e635a27SChris Mason } 24552e635a27SChris Mason 24562e635a27SChris Mason static void __exit exit_btrfs_fs(void) 24572e635a27SChris Mason { 245839279cc3SChris Mason btrfs_destroy_cachep(); 245978a6184aSMiao Xie btrfs_delayed_ref_exit(); 24609247f317SMiao Xie btrfs_auto_defrag_exit(); 246116cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2462b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 24636352b91dSMiao Xie ordered_data_exit(); 2464a52d9a80SChris Mason extent_map_exit(); 2465d1310b2eSChris Mason extent_io_exit(); 2466a9218f6bSChris Mason btrfs_interface_exit(); 24675ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 24682e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 246958176a96SJosef Bacik btrfs_exit_sysfs(); 24708a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2471261507a0SLi Zefan btrfs_exit_compress(); 24722e635a27SChris Mason } 24732e635a27SChris Mason 247460efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 24752e635a27SChris Mason module_exit(exit_btrfs_fs) 24762e635a27SChris Mason 24772e635a27SChris Mason MODULE_LICENSE("GPL"); 2478