1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 64b82d6e4SYan #include <linux/blkdev.h> 72e635a27SChris Mason #include <linux/module.h> 82e635a27SChris Mason #include <linux/fs.h> 92e635a27SChris Mason #include <linux/pagemap.h> 102e635a27SChris Mason #include <linux/highmem.h> 112e635a27SChris Mason #include <linux/time.h> 122e635a27SChris Mason #include <linux/init.h> 13a9572a15SEric Paris #include <linux/seq_file.h> 142e635a27SChris Mason #include <linux/string.h> 152e635a27SChris Mason #include <linux/backing-dev.h> 164b82d6e4SYan #include <linux/mount.h> 1775dfe396SChris Mason #include <linux/writeback.h> 188fd17795SChris Mason #include <linux/statfs.h> 1908607c1bSChris Mason #include <linux/compat.h> 2095e05289SChris Mason #include <linux/parser.h> 21c59f8951SChris Mason #include <linux/ctype.h> 226da6abaeSChris Mason #include <linux/namei.h> 23a9218f6bSChris Mason #include <linux/miscdevice.h> 241bcbf313SQinghuang Feng #include <linux/magic.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 2690a887c9SDan Magenheimer #include <linux/cleancache.h> 2722c44fe6SJosef Bacik #include <linux/ratelimit.h> 289678c543SNikolay Borisov #include <linux/crc32c.h> 2955e301fdSFilipe Brandenburger #include <linux/btrfs.h> 3016cdcec7SMiao Xie #include "delayed-inode.h" 312e635a27SChris Mason #include "ctree.h" 32e20d96d6SChris Mason #include "disk-io.h" 33d5719762SChris Mason #include "transaction.h" 342c90e5d6SChris Mason #include "btrfs_inode.h" 353a686375SChris Mason #include "print-tree.h" 3663541927SFilipe David Borba Manana #include "props.h" 375103e947SJosef Bacik #include "xattr.h" 388a4b83ccSChris Mason #include "volumes.h" 39be6e8dc0SBalaji Rao #include "export.h" 40c8b97818SChris Mason #include "compression.h" 419c5085c1SJosef Bacik #include "rcu-string.h" 428dabb742SStefan Behrens #include "dev-replace.h" 4374255aa0SJosef Bacik #include "free-space-cache.h" 44b9e9a6cbSWang Shilong #include "backref.h" 458719aaaeSJosef Bacik #include "space-info.h" 4689439109SDavid Sterba #include "sysfs.h" 47dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 48aac0023cSJosef Bacik #include "block-group.h" 492e635a27SChris Mason 50d3982100SMark Fasheh #include "qgroup.h" 511abe9b8aSliubo #define CREATE_TRACE_POINTS 521abe9b8aSliubo #include <trace/events/btrfs.h> 531abe9b8aSliubo 54b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 5572fa39f5SMisono, Tomohiro 5672fa39f5SMisono, Tomohiro /* 5772fa39f5SMisono, Tomohiro * Types for mounting the default subvolume and a subvolume explicitly 5872fa39f5SMisono, Tomohiro * requested by subvol=/path. That way the callchain is straightforward and we 5972fa39f5SMisono, Tomohiro * don't have to play tricks with the mount options and recursive calls to 6072fa39f5SMisono, Tomohiro * btrfs_mount. 61312c89fbSMisono, Tomohiro * 62312c89fbSMisono, Tomohiro * The new btrfs_root_fs_type also servers as a tag for the bdev_holder. 6372fa39f5SMisono, Tomohiro */ 64830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 6572fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type; 66e20d96d6SChris Mason 670723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 680723a047SHarald Hoyer 694143cb8bSDavid Sterba const char * __attribute_const__ btrfs_decode_error(int errno) 70acce952bSliubo { 7108748810SDavid Sterba char *errstr = "unknown"; 72acce952bSliubo 73acce952bSliubo switch (errno) { 74acce952bSliubo case -EIO: 75acce952bSliubo errstr = "IO failure"; 76acce952bSliubo break; 77acce952bSliubo case -ENOMEM: 78acce952bSliubo errstr = "Out of memory"; 79acce952bSliubo break; 80acce952bSliubo case -EROFS: 81acce952bSliubo errstr = "Readonly filesystem"; 82acce952bSliubo break; 838c342930SJeff Mahoney case -EEXIST: 848c342930SJeff Mahoney errstr = "Object already exists"; 858c342930SJeff Mahoney break; 8694ef7280SDavid Sterba case -ENOSPC: 8794ef7280SDavid Sterba errstr = "No space left"; 8894ef7280SDavid Sterba break; 8994ef7280SDavid Sterba case -ENOENT: 9094ef7280SDavid Sterba errstr = "No such entry"; 9194ef7280SDavid Sterba break; 92acce952bSliubo } 93acce952bSliubo 94acce952bSliubo return errstr; 95acce952bSliubo } 96acce952bSliubo 97acce952bSliubo /* 9834d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 9952042d8eSAndrea Gelmini * invokes the appropriate error response. 100acce952bSliubo */ 101c0d19e2bSDavid Sterba __cold 10234d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1034da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 104acce952bSliubo { 105acce952bSliubo struct super_block *sb = fs_info->sb; 10657d816a1SAnand Jain #ifdef CONFIG_PRINTK 107acce952bSliubo const char *errstr; 10857d816a1SAnand Jain #endif 109acce952bSliubo 110acce952bSliubo /* 111acce952bSliubo * Special case: if the error is EROFS, and we're already 1121751e8a6SLinus Torvalds * under SB_RDONLY, then it is safe here. 113acce952bSliubo */ 114bc98a42cSDavid Howells if (errno == -EROFS && sb_rdonly(sb)) 115acce952bSliubo return; 116acce952bSliubo 11757d816a1SAnand Jain #ifdef CONFIG_PRINTK 11808748810SDavid Sterba errstr = btrfs_decode_error(errno); 1194da35113SJeff Mahoney if (fmt) { 12037252a66SEric Sandeen struct va_format vaf; 12137252a66SEric Sandeen va_list args; 12237252a66SEric Sandeen 12337252a66SEric Sandeen va_start(args, fmt); 12437252a66SEric Sandeen vaf.fmt = fmt; 12537252a66SEric Sandeen vaf.va = &args; 1264da35113SJeff Mahoney 12762e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 12808748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 12937252a66SEric Sandeen va_end(args); 1304da35113SJeff Mahoney } else { 13162e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 13208748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1334da35113SJeff Mahoney } 13457d816a1SAnand Jain #endif 135acce952bSliubo 1360713d90cSAnand Jain /* 1370713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1380713d90cSAnand Jain * also send it down to the disk 1390713d90cSAnand Jain */ 1400713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1410713d90cSAnand Jain 1424da35113SJeff Mahoney /* Don't go through full error handling during mount */ 143922ea899SAnand Jain if (!(sb->s_flags & SB_BORN)) 144922ea899SAnand Jain return; 145922ea899SAnand Jain 146922ea899SAnand Jain if (sb_rdonly(sb)) 147922ea899SAnand Jain return; 148922ea899SAnand Jain 149922ea899SAnand Jain /* btrfs handle error by forcing the filesystem readonly */ 150922ea899SAnand Jain sb->s_flags |= SB_RDONLY; 151922ea899SAnand Jain btrfs_info(fs_info, "forced readonly"); 152922ea899SAnand Jain /* 153922ea899SAnand Jain * Note that a running device replace operation is not canceled here 154922ea899SAnand Jain * although there is no way to update the progress. It would add the 155922ea899SAnand Jain * risk of a deadlock, therefore the canceling is omitted. The only 156922ea899SAnand Jain * penalty is that some I/O remains active until the procedure 15752042d8eSAndrea Gelmini * completes. The next time when the filesystem is mounted writable 158922ea899SAnand Jain * again, the device replace operation continues. 159922ea899SAnand Jain */ 160acce952bSliubo } 1614da35113SJeff Mahoney 16257d816a1SAnand Jain #ifdef CONFIG_PRINTK 163533574c6SJoe Perches static const char * const logtypes[] = { 1644da35113SJeff Mahoney "emergency", 1654da35113SJeff Mahoney "alert", 1664da35113SJeff Mahoney "critical", 1674da35113SJeff Mahoney "error", 1684da35113SJeff Mahoney "warning", 1694da35113SJeff Mahoney "notice", 1704da35113SJeff Mahoney "info", 1714da35113SJeff Mahoney "debug", 1724da35113SJeff Mahoney }; 1734da35113SJeff Mahoney 17435f4e5e6SNikolay Borisov 17535f4e5e6SNikolay Borisov /* 17635f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 17735f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 17835f4e5e6SNikolay Borisov */ 17935f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 18035f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 18135f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 18235f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 18335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 18435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 18535f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 18635f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 18735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 18835f4e5e6SNikolay Borisov }; 18935f4e5e6SNikolay Borisov 190b105e927SDavid Sterba void __cold btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 1914da35113SJeff Mahoney { 19240f7828bSPetr Mladek char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 1934da35113SJeff Mahoney struct va_format vaf; 1944da35113SJeff Mahoney va_list args; 195533574c6SJoe Perches int kern_level; 19640f7828bSPetr Mladek const char *type = logtypes[4]; 19740f7828bSPetr Mladek struct ratelimit_state *ratelimit = &printk_limits[4]; 1984da35113SJeff Mahoney 1994da35113SJeff Mahoney va_start(args, fmt); 2004da35113SJeff Mahoney 201262c5e86SPetr Mladek while ((kern_level = printk_get_level(fmt)) != 0) { 202533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 203262c5e86SPetr Mladek 204262c5e86SPetr Mladek if (kern_level >= '0' && kern_level <= '7') { 205533574c6SJoe Perches memcpy(lvl, fmt, size); 206533574c6SJoe Perches lvl[size] = '\0'; 207533574c6SJoe Perches type = logtypes[kern_level - '0']; 20835f4e5e6SNikolay Borisov ratelimit = &printk_limits[kern_level - '0']; 209262c5e86SPetr Mladek } 210262c5e86SPetr Mladek fmt += size; 211262c5e86SPetr Mladek } 212262c5e86SPetr Mladek 2134da35113SJeff Mahoney vaf.fmt = fmt; 2144da35113SJeff Mahoney vaf.va = &args; 215533574c6SJoe Perches 21635f4e5e6SNikolay Borisov if (__ratelimit(ratelimit)) 2173993b112SColin Ian King printk("%sBTRFS %s (device %s): %pV\n", lvl, type, 2183993b112SColin Ian King fs_info ? fs_info->sb->s_id : "<unknown>", &vaf); 219533574c6SJoe Perches 220533574c6SJoe Perches va_end(args); 2214da35113SJeff Mahoney } 222533574c6SJoe Perches #endif 223533574c6SJoe Perches 2248c342930SJeff Mahoney /* 22549b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 22649b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 22749b25e05SJeff Mahoney * one. 22849b25e05SJeff Mahoney * 22949b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 23049b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 23149b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 23249b25e05SJeff Mahoney * in the call path but will return -EIO. 23349b25e05SJeff Mahoney * 23449b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 23549b25e05SJeff Mahoney * btrfs_commit_transaction. 23649b25e05SJeff Mahoney */ 237c0d19e2bSDavid Sterba __cold 23849b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 23966642832SJeff Mahoney const char *function, 24049b25e05SJeff Mahoney unsigned int line, int errno) 24149b25e05SJeff Mahoney { 24266642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 24366642832SJeff Mahoney 24449b25e05SJeff Mahoney trans->aborted = errno; 24549b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 24649b25e05SJeff Mahoney * transaction may be able to continue. */ 24764c12921SJeff Mahoney if (!trans->dirty && list_empty(&trans->new_bgs)) { 24869ce977aSMiao Xie const char *errstr; 24969ce977aSMiao Xie 25008748810SDavid Sterba errstr = btrfs_decode_error(errno); 25166642832SJeff Mahoney btrfs_warn(fs_info, 252c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 25369ce977aSMiao Xie function, line, errstr); 25449b25e05SJeff Mahoney return; 25549b25e05SJeff Mahoney } 25620c7bcecSSeraphime Kirkovski WRITE_ONCE(trans->transaction->aborted, errno); 257501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 25866642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 25966642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 26066642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 26149b25e05SJeff Mahoney } 26249b25e05SJeff Mahoney /* 2638c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2648c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2658c342930SJeff Mahoney */ 266c0d19e2bSDavid Sterba __cold 2678c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2688c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2698c342930SJeff Mahoney { 2708c342930SJeff Mahoney char *s_id = "<unknown>"; 2718c342930SJeff Mahoney const char *errstr; 2728c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2738c342930SJeff Mahoney va_list args; 2748c342930SJeff Mahoney 2758c342930SJeff Mahoney if (fs_info) 2768c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2778c342930SJeff Mahoney 2788c342930SJeff Mahoney va_start(args, fmt); 2798c342930SJeff Mahoney vaf.va = &args; 2808c342930SJeff Mahoney 28108748810SDavid Sterba errstr = btrfs_decode_error(errno); 282d8953d69SSatoru Takeuchi if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 28308748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 28408748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2858c342930SJeff Mahoney 286efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 287efe120a0SFrank Holton function, line, &vaf, errno, errstr); 2888c342930SJeff Mahoney va_end(args); 2898c342930SJeff Mahoney /* Caller calls BUG() */ 2908c342930SJeff Mahoney } 291acce952bSliubo 292e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 293e20d96d6SChris Mason { 2946bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 295e20d96d6SChris Mason } 2962e635a27SChris Mason 29795e05289SChris Mason enum { 298416a7202SDavid Sterba Opt_acl, Opt_noacl, 299416a7202SDavid Sterba Opt_clear_cache, 300416a7202SDavid Sterba Opt_commit_interval, 301416a7202SDavid Sterba Opt_compress, 302416a7202SDavid Sterba Opt_compress_force, 303416a7202SDavid Sterba Opt_compress_force_type, 304416a7202SDavid Sterba Opt_compress_type, 305416a7202SDavid Sterba Opt_degraded, 306416a7202SDavid Sterba Opt_device, 307416a7202SDavid Sterba Opt_fatal_errors, 308416a7202SDavid Sterba Opt_flushoncommit, Opt_noflushoncommit, 309416a7202SDavid Sterba Opt_inode_cache, Opt_noinode_cache, 310416a7202SDavid Sterba Opt_max_inline, 311416a7202SDavid Sterba Opt_barrier, Opt_nobarrier, 312416a7202SDavid Sterba Opt_datacow, Opt_nodatacow, 313416a7202SDavid Sterba Opt_datasum, Opt_nodatasum, 314416a7202SDavid Sterba Opt_defrag, Opt_nodefrag, 315416a7202SDavid Sterba Opt_discard, Opt_nodiscard, 316416a7202SDavid Sterba Opt_nologreplay, 317416a7202SDavid Sterba Opt_norecovery, 318416a7202SDavid Sterba Opt_ratio, 319416a7202SDavid Sterba Opt_rescan_uuid_tree, 320416a7202SDavid Sterba Opt_skip_balance, 321416a7202SDavid Sterba Opt_space_cache, Opt_no_space_cache, 322416a7202SDavid Sterba Opt_space_cache_version, 323416a7202SDavid Sterba Opt_ssd, Opt_nossd, 324416a7202SDavid Sterba Opt_ssd_spread, Opt_nossd_spread, 325416a7202SDavid Sterba Opt_subvol, 32637becec9SOmar Sandoval Opt_subvol_empty, 327416a7202SDavid Sterba Opt_subvolid, 328416a7202SDavid Sterba Opt_thread_pool, 329416a7202SDavid Sterba Opt_treelog, Opt_notreelog, 330416a7202SDavid Sterba Opt_usebackuproot, 331416a7202SDavid Sterba Opt_user_subvol_rm_allowed, 332416a7202SDavid Sterba 333416a7202SDavid Sterba /* Deprecated options */ 334416a7202SDavid Sterba Opt_alloc_start, 335416a7202SDavid Sterba Opt_recovery, 336416a7202SDavid Sterba Opt_subvolrootid, 337416a7202SDavid Sterba 338416a7202SDavid Sterba /* Debugging options */ 339416a7202SDavid Sterba Opt_check_integrity, 34070f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 341416a7202SDavid Sterba Opt_check_integrity_print_mask, 342416a7202SDavid Sterba Opt_enospc_debug, Opt_noenospc_debug, 343d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 344d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 345d0bd4560SJosef Bacik #endif 346fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 347fb592373SJosef Bacik Opt_ref_verify, 348fb592373SJosef Bacik #endif 3499555c6c1SIlya Dryomov Opt_err, 35095e05289SChris Mason }; 35195e05289SChris Mason 3524d4ab6d6SDavid Sterba static const match_table_t tokens = { 353416a7202SDavid Sterba {Opt_acl, "acl"}, 354416a7202SDavid Sterba {Opt_noacl, "noacl"}, 355416a7202SDavid Sterba {Opt_clear_cache, "clear_cache"}, 356416a7202SDavid Sterba {Opt_commit_interval, "commit=%u"}, 357c8b97818SChris Mason {Opt_compress, "compress"}, 358261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 359a555f810SChris Mason {Opt_compress_force, "compress-force"}, 360261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 361416a7202SDavid Sterba {Opt_degraded, "degraded"}, 362416a7202SDavid Sterba {Opt_device, "device=%s"}, 363416a7202SDavid Sterba {Opt_fatal_errors, "fatal_errors=%s"}, 364dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3652c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 3664b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3673818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 368416a7202SDavid Sterba {Opt_max_inline, "max_inline=%s"}, 369416a7202SDavid Sterba {Opt_barrier, "barrier"}, 370416a7202SDavid Sterba {Opt_nobarrier, "nobarrier"}, 371416a7202SDavid Sterba {Opt_datacow, "datacow"}, 372416a7202SDavid Sterba {Opt_nodatacow, "nodatacow"}, 373416a7202SDavid Sterba {Opt_datasum, "datasum"}, 374416a7202SDavid Sterba {Opt_nodatasum, "nodatasum"}, 375416a7202SDavid Sterba {Opt_defrag, "autodefrag"}, 376416a7202SDavid Sterba {Opt_nodefrag, "noautodefrag"}, 377416a7202SDavid Sterba {Opt_discard, "discard"}, 378416a7202SDavid Sterba {Opt_nodiscard, "nodiscard"}, 379416a7202SDavid Sterba {Opt_nologreplay, "nologreplay"}, 380416a7202SDavid Sterba {Opt_norecovery, "norecovery"}, 381416a7202SDavid Sterba {Opt_ratio, "metadata_ratio=%u"}, 382416a7202SDavid Sterba {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3839555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 384416a7202SDavid Sterba {Opt_space_cache, "space_cache"}, 385416a7202SDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 386416a7202SDavid Sterba {Opt_space_cache_version, "space_cache=%s"}, 387416a7202SDavid Sterba {Opt_ssd, "ssd"}, 388416a7202SDavid Sterba {Opt_nossd, "nossd"}, 389416a7202SDavid Sterba {Opt_ssd_spread, "ssd_spread"}, 390416a7202SDavid Sterba {Opt_nossd_spread, "nossd_spread"}, 391416a7202SDavid Sterba {Opt_subvol, "subvol=%s"}, 39237becec9SOmar Sandoval {Opt_subvol_empty, "subvol="}, 393416a7202SDavid Sterba {Opt_subvolid, "subvolid=%s"}, 394416a7202SDavid Sterba {Opt_thread_pool, "thread_pool=%u"}, 395416a7202SDavid Sterba {Opt_treelog, "treelog"}, 396416a7202SDavid Sterba {Opt_notreelog, "notreelog"}, 397416a7202SDavid Sterba {Opt_usebackuproot, "usebackuproot"}, 398416a7202SDavid Sterba {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 399416a7202SDavid Sterba 400416a7202SDavid Sterba /* Deprecated options */ 401416a7202SDavid Sterba {Opt_alloc_start, "alloc_start=%s"}, 402416a7202SDavid Sterba {Opt_recovery, "recovery"}, 403416a7202SDavid Sterba {Opt_subvolrootid, "subvolrootid=%d"}, 404416a7202SDavid Sterba 405416a7202SDavid Sterba /* Debugging options */ 40621adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 40721adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 40802453bdeSAnand Jain {Opt_check_integrity_print_mask, "check_int_print_mask=%u"}, 409416a7202SDavid Sterba {Opt_enospc_debug, "enospc_debug"}, 410416a7202SDavid Sterba {Opt_noenospc_debug, "noenospc_debug"}, 411d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 412d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 413d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 414d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 415d0bd4560SJosef Bacik #endif 416fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 417fb592373SJosef Bacik {Opt_ref_verify, "ref_verify"}, 418fb592373SJosef Bacik #endif 41933268eafSJosef Bacik {Opt_err, NULL}, 42095e05289SChris Mason }; 42195e05289SChris Mason 422edf24abeSChristoph Hellwig /* 423edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 424edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 42549b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 426edf24abeSChristoph Hellwig */ 4272ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 42896da0919SQu Wenruo unsigned long new_flags) 42995e05289SChris Mason { 43095e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 431e215772cSMisono, Tomohiro char *p, *num; 43273bc1876SJosef Bacik u64 cache_gen; 4334543df7eSChris Mason int intarg; 434a7a3f7caSSage Weil int ret = 0; 435261507a0SLi Zefan char *compress_type; 436261507a0SLi Zefan bool compress_force = false; 437b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 438b7c47bbbSTsutomu Itoh bool saved_compress_force; 439b7c47bbbSTsutomu Itoh int no_compress = 0; 440b6cda9bcSChris Mason 4410b246afaSJeff Mahoney cache_gen = btrfs_super_cache_generation(info->super_copy); 4420b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 44370f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 44470f6d82eSOmar Sandoval else if (cache_gen) 44573bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 44673bc1876SJosef Bacik 44796da0919SQu Wenruo /* 44896da0919SQu Wenruo * Even the options are empty, we still need to do extra check 44996da0919SQu Wenruo * against new flags 45096da0919SQu Wenruo */ 45195e05289SChris Mason if (!options) 45296da0919SQu Wenruo goto check; 45395e05289SChris Mason 45495e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 45595e05289SChris Mason int token; 45695e05289SChris Mason if (!*p) 45795e05289SChris Mason continue; 45895e05289SChris Mason 45995e05289SChris Mason token = match_token(p, tokens, args); 46095e05289SChris Mason switch (token) { 461dfe25020SChris Mason case Opt_degraded: 4620b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 463dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 464dfe25020SChris Mason break; 46595e05289SChris Mason case Opt_subvol: 46637becec9SOmar Sandoval case Opt_subvol_empty: 46773f73415SJosef Bacik case Opt_subvolid: 468e15d0542SXin Zhong case Opt_subvolrootid: 46943e570b0SChristoph Hellwig case Opt_device: 470edf24abeSChristoph Hellwig /* 471fa59f27cSAnand Jain * These are parsed by btrfs_parse_subvol_options or 472fa59f27cSAnand Jain * btrfs_parse_device_options and can be ignored here. 473edf24abeSChristoph Hellwig */ 47495e05289SChris Mason break; 475b6cda9bcSChris Mason case Opt_nodatasum: 4763cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 47707802534SQu Wenruo "setting nodatasum"); 478be20aa9dSChris Mason break; 479d399167dSQu Wenruo case Opt_datasum: 4803cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 4813cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 4820b246afaSJeff Mahoney btrfs_info(info, 4835d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 484d399167dSQu Wenruo else 4850b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 48607802534SQu Wenruo } 487d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 488d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 489d399167dSQu Wenruo break; 490be20aa9dSChris Mason case Opt_nodatacow: 4913cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 4923cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 4933cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 4940b246afaSJeff Mahoney btrfs_info(info, 495efe120a0SFrank Holton "setting nodatacow, compression disabled"); 496bedb2ccaSAndrei Popa } else { 4970b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 498bedb2ccaSAndrei Popa } 49907802534SQu Wenruo } 500bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 501bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 502be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 503be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 504b6cda9bcSChris Mason break; 505a258af7aSQu Wenruo case Opt_datacow: 5063cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 50707802534SQu Wenruo "setting datacow"); 508a258af7aSQu Wenruo break; 509a555f810SChris Mason case Opt_compress_force: 510261507a0SLi Zefan case Opt_compress_force_type: 511261507a0SLi Zefan compress_force = true; 5121c697d4aSEric Sandeen /* Fallthrough */ 513261507a0SLi Zefan case Opt_compress: 514261507a0SLi Zefan case Opt_compress_type: 5153cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 5163cdde224SJeff Mahoney COMPRESS) ? 517b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 518b7c47bbbSTsutomu Itoh saved_compress_force = 5193cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 520261507a0SLi Zefan if (token == Opt_compress || 521261507a0SLi Zefan token == Opt_compress_force || 522a7164fa4SDavid Sterba strncmp(args[0].from, "zlib", 4) == 0) { 523261507a0SLi Zefan compress_type = "zlib"; 524eae8d825SQu Wenruo 525261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 526eae8d825SQu Wenruo info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL; 527eae8d825SQu Wenruo /* 528eae8d825SQu Wenruo * args[0] contains uninitialized data since 529eae8d825SQu Wenruo * for these tokens we don't expect any 530eae8d825SQu Wenruo * parameter. 531eae8d825SQu Wenruo */ 532eae8d825SQu Wenruo if (token != Opt_compress && 533eae8d825SQu Wenruo token != Opt_compress_force) 534f51d2b59SDavid Sterba info->compress_level = 535d0ab62ceSDennis Zhou btrfs_compress_str2level( 536d0ab62ceSDennis Zhou BTRFS_COMPRESS_ZLIB, 537d0ab62ceSDennis Zhou args[0].from + 4); 538063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 539bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 540bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 541b7c47bbbSTsutomu Itoh no_compress = 0; 542a7164fa4SDavid Sterba } else if (strncmp(args[0].from, "lzo", 3) == 0) { 543a6fa6faeSLi Zefan compress_type = "lzo"; 544a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 545063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 546bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 547bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5482b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 549b7c47bbbSTsutomu Itoh no_compress = 0; 5503f93aef5SDennis Zhou } else if (strncmp(args[0].from, "zstd", 4) == 0) { 5515c1aab1dSNick Terrell compress_type = "zstd"; 5525c1aab1dSNick Terrell info->compress_type = BTRFS_COMPRESS_ZSTD; 5533f93aef5SDennis Zhou info->compress_level = 5543f93aef5SDennis Zhou btrfs_compress_str2level( 5553f93aef5SDennis Zhou BTRFS_COMPRESS_ZSTD, 5563f93aef5SDennis Zhou args[0].from + 4); 5575c1aab1dSNick Terrell btrfs_set_opt(info->mount_opt, COMPRESS); 5585c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATACOW); 5595c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATASUM); 5605c1aab1dSNick Terrell btrfs_set_fs_incompat(info, COMPRESS_ZSTD); 5615c1aab1dSNick Terrell no_compress = 0; 562063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 563063849eaSArnd Hannemann compress_type = "no"; 564063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 565063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 566063849eaSArnd Hannemann compress_force = false; 567b7c47bbbSTsutomu Itoh no_compress++; 568261507a0SLi Zefan } else { 569261507a0SLi Zefan ret = -EINVAL; 570261507a0SLi Zefan goto out; 571261507a0SLi Zefan } 572261507a0SLi Zefan 573261507a0SLi Zefan if (compress_force) { 574b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 575143f3636SDavid Sterba } else { 5764027e0f4SWang Shilong /* 5774027e0f4SWang Shilong * If we remount from compress-force=xxx to 5784027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5794027e0f4SWang Shilong * flag, otherwise, there is no way for users 5804027e0f4SWang Shilong * to disable forcible compression separately. 5814027e0f4SWang Shilong */ 5824027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 583a7e252afSMiao Xie } 5843cdde224SJeff Mahoney if ((btrfs_test_opt(info, COMPRESS) && 585b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 586b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 5873cdde224SJeff Mahoney (!btrfs_test_opt(info, COMPRESS) && 588b7c47bbbSTsutomu Itoh no_compress == 1)) { 589f51d2b59SDavid Sterba btrfs_info(info, "%s %s compression, level %d", 590b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 591f51d2b59SDavid Sterba compress_type, info->compress_level); 592b7c47bbbSTsutomu Itoh } 593b7c47bbbSTsutomu Itoh compress_force = false; 594a555f810SChris Mason break; 595e18e4809SChris Mason case Opt_ssd: 5963cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 597583b7231SHans van Kranenburg "enabling ssd optimizations"); 598951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 599e18e4809SChris Mason break; 600451d7585SChris Mason case Opt_ssd_spread: 601583b7231SHans van Kranenburg btrfs_set_and_info(info, SSD, 602583b7231SHans van Kranenburg "enabling ssd optimizations"); 6033cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 604583b7231SHans van Kranenburg "using spread ssd allocation scheme"); 605951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 606451d7585SChris Mason break; 6073b30c22fSChris Mason case Opt_nossd: 608583b7231SHans van Kranenburg btrfs_set_opt(info->mount_opt, NOSSD); 609583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD, 610583b7231SHans van Kranenburg "not using ssd optimizations"); 61162b8e077SHoward McLauchlan /* Fallthrough */ 61262b8e077SHoward McLauchlan case Opt_nossd_spread: 613583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD_SPREAD, 614583b7231SHans van Kranenburg "not using spread ssd allocation scheme"); 6153b30c22fSChris Mason break; 616842bef58SQu Wenruo case Opt_barrier: 6173cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 61807802534SQu Wenruo "turning on barriers"); 619842bef58SQu Wenruo break; 62021ad10cfSChris Mason case Opt_nobarrier: 6213cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 62207802534SQu Wenruo "turning off barriers"); 62321ad10cfSChris Mason break; 6244543df7eSChris Mason case Opt_thread_pool: 6252c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6262c334e87SWang Shilong if (ret) { 6272c334e87SWang Shilong goto out; 628f7b885beSAnand Jain } else if (intarg == 0) { 6292c334e87SWang Shilong ret = -EINVAL; 6302c334e87SWang Shilong goto out; 6312c334e87SWang Shilong } 632f7b885beSAnand Jain info->thread_pool_size = intarg; 6334543df7eSChris Mason break; 6346f568d35SChris Mason case Opt_max_inline: 635edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 6366f568d35SChris Mason if (num) { 63791748467SAkinobu Mita info->max_inline = memparse(num, NULL); 6386f568d35SChris Mason kfree(num); 6396f568d35SChris Mason 64015ada040SChris Mason if (info->max_inline) { 641feb5f965SMitch Harder info->max_inline = min_t(u64, 64215ada040SChris Mason info->max_inline, 6430b246afaSJeff Mahoney info->sectorsize); 64415ada040SChris Mason } 6450b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 646c1c9ff7cSGeert Uytterhoeven info->max_inline); 6472c334e87SWang Shilong } else { 6482c334e87SWang Shilong ret = -ENOMEM; 6492c334e87SWang Shilong goto out; 6506f568d35SChris Mason } 6516f568d35SChris Mason break; 6528f662a76SChris Mason case Opt_alloc_start: 6530d0c71b3SDavid Sterba btrfs_info(info, 6540d0c71b3SDavid Sterba "option alloc_start is obsolete, ignored"); 6558f662a76SChris Mason break; 656bd0330adSQu Wenruo case Opt_acl: 65745ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 6581751e8a6SLinus Torvalds info->sb->s_flags |= SB_POSIXACL; 659bd0330adSQu Wenruo break; 66045ff35d6SGuangliang Zhao #else 6610b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 66245ff35d6SGuangliang Zhao ret = -EINVAL; 66345ff35d6SGuangliang Zhao goto out; 66445ff35d6SGuangliang Zhao #endif 66533268eafSJosef Bacik case Opt_noacl: 6661751e8a6SLinus Torvalds info->sb->s_flags &= ~SB_POSIXACL; 66733268eafSJosef Bacik break; 6683a5e1404SSage Weil case Opt_notreelog: 6693cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 67007802534SQu Wenruo "disabling tree log"); 6713a5e1404SSage Weil break; 672a88998f2SQu Wenruo case Opt_treelog: 6733cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 67407802534SQu Wenruo "enabling tree log"); 675a88998f2SQu Wenruo break; 676fed8f166SQu Wenruo case Opt_norecovery: 67796da0919SQu Wenruo case Opt_nologreplay: 6783cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 67996da0919SQu Wenruo "disabling log replay at mount time"); 68096da0919SQu Wenruo break; 681dccae999SSage Weil case Opt_flushoncommit: 6823cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 68307802534SQu Wenruo "turning on flush-on-commit"); 684dccae999SSage Weil break; 6852c9ee856SQu Wenruo case Opt_noflushoncommit: 6863cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 68707802534SQu Wenruo "turning off flush-on-commit"); 6882c9ee856SQu Wenruo break; 68997e728d4SJosef Bacik case Opt_ratio: 6902c334e87SWang Shilong ret = match_int(&args[0], &intarg); 691764cb8b4SAnand Jain if (ret) 6922c334e87SWang Shilong goto out; 69397e728d4SJosef Bacik info->metadata_ratio = intarg; 694764cb8b4SAnand Jain btrfs_info(info, "metadata ratio %u", 69597e728d4SJosef Bacik info->metadata_ratio); 69697e728d4SJosef Bacik break; 697e244a0aeSChristoph Hellwig case Opt_discard: 6983cdde224SJeff Mahoney btrfs_set_and_info(info, DISCARD, 69907802534SQu Wenruo "turning on discard"); 700e244a0aeSChristoph Hellwig break; 701e07a2adeSQu Wenruo case Opt_nodiscard: 7023cdde224SJeff Mahoney btrfs_clear_and_info(info, DISCARD, 70307802534SQu Wenruo "turning off discard"); 704e07a2adeSQu Wenruo break; 7050af3d00bSJosef Bacik case Opt_space_cache: 70670f6d82eSOmar Sandoval case Opt_space_cache_version: 70770f6d82eSOmar Sandoval if (token == Opt_space_cache || 70870f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 7090b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 71070f6d82eSOmar Sandoval FREE_SPACE_TREE); 7113cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 71207802534SQu Wenruo "enabling disk space caching"); 71370f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 7140b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 71570f6d82eSOmar Sandoval SPACE_CACHE); 7160b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 71770f6d82eSOmar Sandoval "enabling free space tree"); 71870f6d82eSOmar Sandoval } else { 71970f6d82eSOmar Sandoval ret = -EINVAL; 72070f6d82eSOmar Sandoval goto out; 72170f6d82eSOmar Sandoval } 7220de90876SJosef Bacik break; 723f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 724f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 725f420ee1eSStefan Behrens break; 72673bc1876SJosef Bacik case Opt_no_space_cache: 7273cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 7280b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 72907802534SQu Wenruo "disabling disk space caching"); 73070f6d82eSOmar Sandoval } 7313cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 7320b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 73370f6d82eSOmar Sandoval "disabling free space tree"); 73470f6d82eSOmar Sandoval } 73573bc1876SJosef Bacik break; 7364b9465cbSChris Mason case Opt_inode_cache: 7377e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 73807802534SQu Wenruo "enabling inode map caching"); 7393818aea2SQu Wenruo break; 7403818aea2SQu Wenruo case Opt_noinode_cache: 7417e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 74207802534SQu Wenruo "disabling inode map caching"); 7434b9465cbSChris Mason break; 74488c2ba3bSJosef Bacik case Opt_clear_cache: 7453cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 74607802534SQu Wenruo "force clearing of disk cache"); 7470af3d00bSJosef Bacik break; 7484260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 7494260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 7504260f7c7SSage Weil break; 75191435650SChris Mason case Opt_enospc_debug: 75291435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 75391435650SChris Mason break; 75453036293SQu Wenruo case Opt_noenospc_debug: 75553036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 75653036293SQu Wenruo break; 7574cb5300bSChris Mason case Opt_defrag: 7583cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 75907802534SQu Wenruo "enabling auto defrag"); 7604cb5300bSChris Mason break; 761fc0ca9afSQu Wenruo case Opt_nodefrag: 7623cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 76307802534SQu Wenruo "disabling auto defrag"); 764fc0ca9afSQu Wenruo break; 765af31f5e5SChris Mason case Opt_recovery: 7660b246afaSJeff Mahoney btrfs_warn(info, 7678dcddfa0SQu Wenruo "'recovery' is deprecated, use 'usebackuproot' instead"); 768acd43e3cSBart Van Assche /* fall through */ 7698dcddfa0SQu Wenruo case Opt_usebackuproot: 7700b246afaSJeff Mahoney btrfs_info(info, 7718dcddfa0SQu Wenruo "trying to use backup root at mount time"); 7728dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 773af31f5e5SChris Mason break; 7749555c6c1SIlya Dryomov case Opt_skip_balance: 7759555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7769555c6c1SIlya Dryomov break; 77721adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 77821adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 7790b246afaSJeff Mahoney btrfs_info(info, 780efe120a0SFrank Holton "enabling check integrity including extent data"); 78121adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 78221adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 78321adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 78421adbd5cSStefan Behrens break; 78521adbd5cSStefan Behrens case Opt_check_integrity: 7860b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 78721adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 78821adbd5cSStefan Behrens break; 78921adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7902c334e87SWang Shilong ret = match_int(&args[0], &intarg); 79102453bdeSAnand Jain if (ret) 7922c334e87SWang Shilong goto out; 79321adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 79402453bdeSAnand Jain btrfs_info(info, "check_integrity_print_mask 0x%x", 79521adbd5cSStefan Behrens info->check_integrity_print_mask); 79621adbd5cSStefan Behrens break; 79721adbd5cSStefan Behrens #else 79821adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 79921adbd5cSStefan Behrens case Opt_check_integrity: 80021adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 8010b246afaSJeff Mahoney btrfs_err(info, 802efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 80321adbd5cSStefan Behrens ret = -EINVAL; 80421adbd5cSStefan Behrens goto out; 80521adbd5cSStefan Behrens #endif 8068c342930SJeff Mahoney case Opt_fatal_errors: 8078c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 8088c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 8098c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 8108c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 8118c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 8128c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 8138c342930SJeff Mahoney else { 8148c342930SJeff Mahoney ret = -EINVAL; 8158c342930SJeff Mahoney goto out; 8168c342930SJeff Mahoney } 8178c342930SJeff Mahoney break; 8188b87dc17SDavid Sterba case Opt_commit_interval: 8198b87dc17SDavid Sterba intarg = 0; 8208b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 821d3740608SAnand Jain if (ret) 8228b87dc17SDavid Sterba goto out; 823d3740608SAnand Jain if (intarg == 0) { 824d3740608SAnand Jain btrfs_info(info, 825d3740608SAnand Jain "using default commit interval %us", 826d3740608SAnand Jain BTRFS_DEFAULT_COMMIT_INTERVAL); 827d3740608SAnand Jain intarg = BTRFS_DEFAULT_COMMIT_INTERVAL; 828d3740608SAnand Jain } else if (intarg > 300) { 829d3740608SAnand Jain btrfs_warn(info, "excessive commit interval %d", 8308b87dc17SDavid Sterba intarg); 8318b87dc17SDavid Sterba } 8328b87dc17SDavid Sterba info->commit_interval = intarg; 8338b87dc17SDavid Sterba break; 834d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 835d0bd4560SJosef Bacik case Opt_fragment_all: 8360b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 837d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 838d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 839d0bd4560SJosef Bacik break; 840d0bd4560SJosef Bacik case Opt_fragment_metadata: 8410b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 842d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 843d0bd4560SJosef Bacik FRAGMENT_METADATA); 844d0bd4560SJosef Bacik break; 845d0bd4560SJosef Bacik case Opt_fragment_data: 8460b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 847d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 848d0bd4560SJosef Bacik break; 849d0bd4560SJosef Bacik #endif 850fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 851fb592373SJosef Bacik case Opt_ref_verify: 852fb592373SJosef Bacik btrfs_info(info, "doing ref verification"); 853fb592373SJosef Bacik btrfs_set_opt(info->mount_opt, REF_VERIFY); 854fb592373SJosef Bacik break; 855fb592373SJosef Bacik #endif 856a7a3f7caSSage Weil case Opt_err: 8570b246afaSJeff Mahoney btrfs_info(info, "unrecognized mount option '%s'", p); 858a7a3f7caSSage Weil ret = -EINVAL; 859a7a3f7caSSage Weil goto out; 86095e05289SChris Mason default: 861be20aa9dSChris Mason break; 86295e05289SChris Mason } 86395e05289SChris Mason } 86496da0919SQu Wenruo check: 86596da0919SQu Wenruo /* 86696da0919SQu Wenruo * Extra check for current option against current flag 86796da0919SQu Wenruo */ 8681751e8a6SLinus Torvalds if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) { 8690b246afaSJeff Mahoney btrfs_err(info, 87096da0919SQu Wenruo "nologreplay must be used with ro mount option"); 87196da0919SQu Wenruo ret = -EINVAL; 87296da0919SQu Wenruo } 873a7a3f7caSSage Weil out: 8740b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 8753cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 8763cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 8770b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 87870f6d82eSOmar Sandoval ret = -EINVAL; 87970f6d82eSOmar Sandoval 88070f6d82eSOmar Sandoval } 8813cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 8820b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 8833cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 8840b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 885a7a3f7caSSage Weil return ret; 886edf24abeSChristoph Hellwig } 887edf24abeSChristoph Hellwig 888edf24abeSChristoph Hellwig /* 889edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 890edf24abeSChristoph Hellwig * 891edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 892edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 893edf24abeSChristoph Hellwig */ 894fa59f27cSAnand Jain static int btrfs_parse_device_options(const char *options, fmode_t flags, 895d64dcbd1SGu Jinxiang void *holder) 896edf24abeSChristoph Hellwig { 897edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 89883c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 89936350e95SGu Jinxiang struct btrfs_device *device = NULL; 900d7407606SMisono, Tomohiro int error = 0; 901d7407606SMisono, Tomohiro 9025139cff5SDavid Sterba lockdep_assert_held(&uuid_mutex); 9035139cff5SDavid Sterba 904d7407606SMisono, Tomohiro if (!options) 905d7407606SMisono, Tomohiro return 0; 906d7407606SMisono, Tomohiro 907d7407606SMisono, Tomohiro /* 908d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because btrfs_parse_options 909d7407606SMisono, Tomohiro * gets called later 910d7407606SMisono, Tomohiro */ 911d7407606SMisono, Tomohiro opts = kstrdup(options, GFP_KERNEL); 912d7407606SMisono, Tomohiro if (!opts) 913d7407606SMisono, Tomohiro return -ENOMEM; 914d7407606SMisono, Tomohiro orig = opts; 915d7407606SMisono, Tomohiro 916d7407606SMisono, Tomohiro while ((p = strsep(&opts, ",")) != NULL) { 917d7407606SMisono, Tomohiro int token; 918d7407606SMisono, Tomohiro 919d7407606SMisono, Tomohiro if (!*p) 920d7407606SMisono, Tomohiro continue; 921d7407606SMisono, Tomohiro 922d7407606SMisono, Tomohiro token = match_token(p, tokens, args); 923d7407606SMisono, Tomohiro if (token == Opt_device) { 924d7407606SMisono, Tomohiro device_name = match_strdup(&args[0]); 925d7407606SMisono, Tomohiro if (!device_name) { 926d7407606SMisono, Tomohiro error = -ENOMEM; 927d7407606SMisono, Tomohiro goto out; 928d7407606SMisono, Tomohiro } 92936350e95SGu Jinxiang device = btrfs_scan_one_device(device_name, flags, 93036350e95SGu Jinxiang holder); 931d7407606SMisono, Tomohiro kfree(device_name); 93236350e95SGu Jinxiang if (IS_ERR(device)) { 93336350e95SGu Jinxiang error = PTR_ERR(device); 934d7407606SMisono, Tomohiro goto out; 935d7407606SMisono, Tomohiro } 936d7407606SMisono, Tomohiro } 93736350e95SGu Jinxiang } 938d7407606SMisono, Tomohiro 939d7407606SMisono, Tomohiro out: 940d7407606SMisono, Tomohiro kfree(orig); 941d7407606SMisono, Tomohiro return error; 942d7407606SMisono, Tomohiro } 943d7407606SMisono, Tomohiro 944d7407606SMisono, Tomohiro /* 945d7407606SMisono, Tomohiro * Parse mount options that are related to subvolume id 946d7407606SMisono, Tomohiro * 947d7407606SMisono, Tomohiro * The value is later passed to mount_subvol() 948d7407606SMisono, Tomohiro */ 94993b9bcdfSGu Jinxiang static int btrfs_parse_subvol_options(const char *options, char **subvol_name, 95093b9bcdfSGu Jinxiang u64 *subvol_objectid) 951d7407606SMisono, Tomohiro { 952d7407606SMisono, Tomohiro substring_t args[MAX_OPT_ARGS]; 953d7407606SMisono, Tomohiro char *opts, *orig, *p; 954edf24abeSChristoph Hellwig int error = 0; 955ccb0e7d1SAnand Jain u64 subvolid; 956edf24abeSChristoph Hellwig 957edf24abeSChristoph Hellwig if (!options) 958830c4adbSJosef Bacik return 0; 959edf24abeSChristoph Hellwig 960edf24abeSChristoph Hellwig /* 961d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because 962fa59f27cSAnand Jain * btrfs_parse_device_options gets called later 963edf24abeSChristoph Hellwig */ 964edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 965edf24abeSChristoph Hellwig if (!opts) 966edf24abeSChristoph Hellwig return -ENOMEM; 9673f3d0bc0STero Roponen orig = opts; 968edf24abeSChristoph Hellwig 969edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 970edf24abeSChristoph Hellwig int token; 971edf24abeSChristoph Hellwig if (!*p) 972edf24abeSChristoph Hellwig continue; 973edf24abeSChristoph Hellwig 974edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 975edf24abeSChristoph Hellwig switch (token) { 976edf24abeSChristoph Hellwig case Opt_subvol: 977a90e8b6fSIlya Dryomov kfree(*subvol_name); 978edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 9792c334e87SWang Shilong if (!*subvol_name) { 9802c334e87SWang Shilong error = -ENOMEM; 9812c334e87SWang Shilong goto out; 9822c334e87SWang Shilong } 983edf24abeSChristoph Hellwig break; 98473f73415SJosef Bacik case Opt_subvolid: 985ccb0e7d1SAnand Jain error = match_u64(&args[0], &subvolid); 986ccb0e7d1SAnand Jain if (error) 9872c334e87SWang Shilong goto out; 988ccb0e7d1SAnand Jain 989ccb0e7d1SAnand Jain /* we want the original fs_tree */ 990ccb0e7d1SAnand Jain if (subvolid == 0) 991ccb0e7d1SAnand Jain subvolid = BTRFS_FS_TREE_OBJECTID; 992ccb0e7d1SAnand Jain 993ccb0e7d1SAnand Jain *subvol_objectid = subvolid; 99473f73415SJosef Bacik break; 995e15d0542SXin Zhong case Opt_subvolrootid: 99662e85577SJeff Mahoney pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n"); 997e15d0542SXin Zhong break; 998edf24abeSChristoph Hellwig default: 999edf24abeSChristoph Hellwig break; 1000edf24abeSChristoph Hellwig } 1001edf24abeSChristoph Hellwig } 1002edf24abeSChristoph Hellwig 1003edf24abeSChristoph Hellwig out: 1004830c4adbSJosef Bacik kfree(orig); 1005edf24abeSChristoph Hellwig return error; 100695e05289SChris Mason } 100795e05289SChris Mason 100805dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 100973f73415SJosef Bacik u64 subvol_objectid) 101073f73415SJosef Bacik { 1011815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 101205dbe683SOmar Sandoval struct btrfs_root *fs_root; 101305dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 101405dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 101505dbe683SOmar Sandoval struct btrfs_key key; 101605dbe683SOmar Sandoval struct btrfs_path *path = NULL; 101705dbe683SOmar Sandoval char *name = NULL, *ptr; 101805dbe683SOmar Sandoval u64 dirid; 101905dbe683SOmar Sandoval int len; 102005dbe683SOmar Sandoval int ret; 102105dbe683SOmar Sandoval 102205dbe683SOmar Sandoval path = btrfs_alloc_path(); 102305dbe683SOmar Sandoval if (!path) { 102405dbe683SOmar Sandoval ret = -ENOMEM; 102505dbe683SOmar Sandoval goto err; 102605dbe683SOmar Sandoval } 102705dbe683SOmar Sandoval path->leave_spinning = 1; 102805dbe683SOmar Sandoval 10293ec83621SDavid Sterba name = kmalloc(PATH_MAX, GFP_KERNEL); 103005dbe683SOmar Sandoval if (!name) { 103105dbe683SOmar Sandoval ret = -ENOMEM; 103205dbe683SOmar Sandoval goto err; 103305dbe683SOmar Sandoval } 103405dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 103505dbe683SOmar Sandoval ptr[0] = '\0'; 103605dbe683SOmar Sandoval 103705dbe683SOmar Sandoval /* 103805dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 103905dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 104005dbe683SOmar Sandoval */ 104105dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 104205dbe683SOmar Sandoval key.objectid = subvol_objectid; 104305dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 104405dbe683SOmar Sandoval key.offset = (u64)-1; 104505dbe683SOmar Sandoval 104605dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 104705dbe683SOmar Sandoval if (ret < 0) { 104805dbe683SOmar Sandoval goto err; 104905dbe683SOmar Sandoval } else if (ret > 0) { 105005dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 105105dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 105205dbe683SOmar Sandoval if (ret < 0) { 105305dbe683SOmar Sandoval goto err; 105405dbe683SOmar Sandoval } else if (ret > 0) { 105505dbe683SOmar Sandoval ret = -ENOENT; 105605dbe683SOmar Sandoval goto err; 105705dbe683SOmar Sandoval } 105805dbe683SOmar Sandoval } 105905dbe683SOmar Sandoval 106005dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 106105dbe683SOmar Sandoval subvol_objectid = key.offset; 106205dbe683SOmar Sandoval 106305dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 106405dbe683SOmar Sandoval struct btrfs_root_ref); 106505dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 106605dbe683SOmar Sandoval ptr -= len + 1; 106705dbe683SOmar Sandoval if (ptr < name) { 106805dbe683SOmar Sandoval ret = -ENAMETOOLONG; 106905dbe683SOmar Sandoval goto err; 107005dbe683SOmar Sandoval } 107105dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 107205dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 107305dbe683SOmar Sandoval ptr[0] = '/'; 107405dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 107505dbe683SOmar Sandoval btrfs_release_path(path); 107605dbe683SOmar Sandoval 107705dbe683SOmar Sandoval key.objectid = subvol_objectid; 107805dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 107905dbe683SOmar Sandoval key.offset = (u64)-1; 108005dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 108105dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 108205dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 108305dbe683SOmar Sandoval goto err; 108405dbe683SOmar Sandoval } 108505dbe683SOmar Sandoval 108605dbe683SOmar Sandoval /* 108705dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 108805dbe683SOmar Sandoval * root directory. 108905dbe683SOmar Sandoval */ 109005dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 109105dbe683SOmar Sandoval key.objectid = dirid; 109205dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 109305dbe683SOmar Sandoval key.offset = (u64)-1; 109405dbe683SOmar Sandoval 109505dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 109605dbe683SOmar Sandoval if (ret < 0) { 109705dbe683SOmar Sandoval goto err; 109805dbe683SOmar Sandoval } else if (ret > 0) { 109905dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 110005dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 110105dbe683SOmar Sandoval if (ret < 0) { 110205dbe683SOmar Sandoval goto err; 110305dbe683SOmar Sandoval } else if (ret > 0) { 110405dbe683SOmar Sandoval ret = -ENOENT; 110505dbe683SOmar Sandoval goto err; 110605dbe683SOmar Sandoval } 110705dbe683SOmar Sandoval } 110805dbe683SOmar Sandoval 110905dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 111005dbe683SOmar Sandoval dirid = key.offset; 111105dbe683SOmar Sandoval 111205dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 111305dbe683SOmar Sandoval path->slots[0], 111405dbe683SOmar Sandoval struct btrfs_inode_ref); 111505dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 111605dbe683SOmar Sandoval inode_ref); 111705dbe683SOmar Sandoval ptr -= len + 1; 111805dbe683SOmar Sandoval if (ptr < name) { 111905dbe683SOmar Sandoval ret = -ENAMETOOLONG; 112005dbe683SOmar Sandoval goto err; 112105dbe683SOmar Sandoval } 112205dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 112305dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 112405dbe683SOmar Sandoval ptr[0] = '/'; 112505dbe683SOmar Sandoval btrfs_release_path(path); 112605dbe683SOmar Sandoval } 112705dbe683SOmar Sandoval } 112805dbe683SOmar Sandoval 112905dbe683SOmar Sandoval btrfs_free_path(path); 113005dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 113105dbe683SOmar Sandoval name[0] = '/'; 113205dbe683SOmar Sandoval name[1] = '\0'; 113305dbe683SOmar Sandoval } else { 113405dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 113505dbe683SOmar Sandoval } 113605dbe683SOmar Sandoval return name; 113705dbe683SOmar Sandoval 113805dbe683SOmar Sandoval err: 113905dbe683SOmar Sandoval btrfs_free_path(path); 114005dbe683SOmar Sandoval kfree(name); 114105dbe683SOmar Sandoval return ERR_PTR(ret); 114205dbe683SOmar Sandoval } 114305dbe683SOmar Sandoval 114405dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 114505dbe683SOmar Sandoval { 114605dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 114773f73415SJosef Bacik struct btrfs_dir_item *di; 114873f73415SJosef Bacik struct btrfs_path *path; 114973f73415SJosef Bacik struct btrfs_key location; 115073f73415SJosef Bacik u64 dir_id; 115173f73415SJosef Bacik 115273f73415SJosef Bacik path = btrfs_alloc_path(); 115373f73415SJosef Bacik if (!path) 115405dbe683SOmar Sandoval return -ENOMEM; 115573f73415SJosef Bacik path->leave_spinning = 1; 115673f73415SJosef Bacik 115773f73415SJosef Bacik /* 115873f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 115973f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 116073f73415SJosef Bacik * to mount. 116173f73415SJosef Bacik */ 1162815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 116373f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1164b0839166SJulia Lawall if (IS_ERR(di)) { 1165b0839166SJulia Lawall btrfs_free_path(path); 116605dbe683SOmar Sandoval return PTR_ERR(di); 1167b0839166SJulia Lawall } 116873f73415SJosef Bacik if (!di) { 116973f73415SJosef Bacik /* 117073f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 117173f73415SJosef Bacik * it's always been there, but don't freak out, just try and 117205dbe683SOmar Sandoval * mount the top-level subvolume. 117373f73415SJosef Bacik */ 117473f73415SJosef Bacik btrfs_free_path(path); 117505dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 117605dbe683SOmar Sandoval return 0; 117773f73415SJosef Bacik } 117873f73415SJosef Bacik 117973f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 118073f73415SJosef Bacik btrfs_free_path(path); 118105dbe683SOmar Sandoval *objectid = location.objectid; 118205dbe683SOmar Sandoval return 0; 118373f73415SJosef Bacik } 118473f73415SJosef Bacik 11858a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 11868a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 118756e033a7SDavid Sterba void *data) 11882e635a27SChris Mason { 11892e635a27SChris Mason struct inode *inode; 1190815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 11915d4f98a2SYan Zheng struct btrfs_key key; 119239279cc3SChris Mason int err; 11932e635a27SChris Mason 11942e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 11952e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1196e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1197af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1198be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 11995103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 12002e635a27SChris Mason sb->s_time_gran = 1; 12010eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 12021751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 120349cf6f45SChris Ball #endif 1204357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 1205da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 12069e11ceeeSJan Kara 12079e11ceeeSJan Kara err = super_setup_bdi(sb); 12089e11ceeeSJan Kara if (err) { 12099e11ceeeSJan Kara btrfs_err(fs_info, "super_setup_bdi failed"); 12109e11ceeeSJan Kara return err; 12119e11ceeeSJan Kara } 12129e11ceeeSJan Kara 1213ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1214ad2b2c80SAl Viro if (err) { 1215ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1216ad2b2c80SAl Viro return err; 1217e20d96d6SChris Mason } 1218b888db2bSChris Mason 12195d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 12205d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 12215d4f98a2SYan Zheng key.offset = 0; 12224c66e0d4SDavid Sterba inode = btrfs_iget(sb, &key, fs_info->fs_root); 12235d4f98a2SYan Zheng if (IS_ERR(inode)) { 12245d4f98a2SYan Zheng err = PTR_ERR(inode); 122539279cc3SChris Mason goto fail_close; 122639279cc3SChris Mason } 12272e635a27SChris Mason 122848fde701SAl Viro sb->s_root = d_make_root(inode); 122948fde701SAl Viro if (!sb->s_root) { 123039279cc3SChris Mason err = -ENOMEM; 123139279cc3SChris Mason goto fail_close; 12322e635a27SChris Mason } 123358176a96SJosef Bacik 123490a887c9SDan Magenheimer cleancache_init_fs(sb); 12351751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 12362e635a27SChris Mason return 0; 12372e635a27SChris Mason 123839279cc3SChris Mason fail_close: 12396bccf3abSJeff Mahoney close_ctree(fs_info); 1240d5719762SChris Mason return err; 1241d5719762SChris Mason } 1242d5719762SChris Mason 12436bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1244d5719762SChris Mason { 1245d5719762SChris Mason struct btrfs_trans_handle *trans; 1246815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1247815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1248df2ce34cSChris Mason 1249bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 12501abe9b8aSliubo 1251d561c025SChris Mason if (!wait) { 1252815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1253df2ce34cSChris Mason return 0; 1254d561c025SChris Mason } 1255771ed689SChris Mason 12566374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1257771ed689SChris Mason 1258d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 125960376ce4SJosef Bacik if (IS_ERR(trans)) { 1260354aa0fbSMiao Xie /* no transaction, don't bother */ 12616b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 12626b5fe46dSDavid Sterba /* 12636b5fe46dSDavid Sterba * Exit unless we have some pending changes 12646b5fe46dSDavid Sterba * that need to go through commit 12656b5fe46dSDavid Sterba */ 12666b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1267bd7de2c9SJosef Bacik return 0; 1268a53f4f8eSQu Wenruo /* 1269a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1270a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1271a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1272a53f4f8eSQu Wenruo * next commit after thawing. 1273a53f4f8eSQu Wenruo */ 1274a7e3c5f2SRakesh Pandit if (sb_start_write_trylock(sb)) 1275a7e3c5f2SRakesh Pandit sb_end_write(sb); 1276a53f4f8eSQu Wenruo else 1277a53f4f8eSQu Wenruo return 0; 12786b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 127960376ce4SJosef Bacik } 128098bd5c54SDavid Sterba if (IS_ERR(trans)) 128198bd5c54SDavid Sterba return PTR_ERR(trans); 12826b5fe46dSDavid Sterba } 12833a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1284d5719762SChris Mason } 1285d5719762SChris Mason 128634c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1287a9572a15SEric Paris { 1288815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 12890f628c63SDavid Sterba const char *compress_type; 1290a9572a15SEric Paris 12913cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1292a9572a15SEric Paris seq_puts(seq, ",degraded"); 12933cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1294a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 12953cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1296a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 12973cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1298a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 129995ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1300c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1301a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1302a9572a15SEric Paris num_online_cpus() + 2, 8)) 1303f7b885beSAnand Jain seq_printf(seq, ",thread_pool=%u", info->thread_pool_size); 13043cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 13050f628c63SDavid Sterba compress_type = btrfs_compress_type2str(info->compress_type); 13063cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1307200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1308200da64eSTsutomu Itoh else 1309200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1310f51d2b59SDavid Sterba if (info->compress_level) 1311fa4d885aSAdam Borowski seq_printf(seq, ":%d", info->compress_level); 1312200da64eSTsutomu Itoh } 13133cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1314c289811cSChris Mason seq_puts(seq, ",nossd"); 13153cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1316451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 13173cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1318a9572a15SEric Paris seq_puts(seq, ",ssd"); 13193cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 13206b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 13213cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 132296da0919SQu Wenruo seq_puts(seq, ",nologreplay"); 13233cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 13246b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 13253cdde224SJeff Mahoney if (btrfs_test_opt(info, DISCARD)) 132620a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 13271751e8a6SLinus Torvalds if (!(info->sb->s_flags & SB_POSIXACL)) 1328a9572a15SEric Paris seq_puts(seq, ",noacl"); 13293cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) 1330200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 13313cdde224SJeff Mahoney else if (btrfs_test_opt(info, FREE_SPACE_TREE)) 133270f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 133373bc1876SJosef Bacik else 13348965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 13353cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1336f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 13373cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1338200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 13393cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1340200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 13413cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 13420942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 13433cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 13440942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 13453cdde224SJeff Mahoney if (btrfs_test_opt(info, INODE_MAP_CACHE)) 13460942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 13473cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 13489555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 13498507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 13503cdde224SJeff Mahoney if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 13518507d216SWang Shilong seq_puts(seq, ",check_int_data"); 13523cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 13538507d216SWang Shilong seq_puts(seq, ",check_int"); 13548507d216SWang Shilong if (info->check_integrity_print_mask) 13558507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 13568507d216SWang Shilong info->check_integrity_print_mask); 13578507d216SWang Shilong #endif 13588507d216SWang Shilong if (info->metadata_ratio) 1359764cb8b4SAnand Jain seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio); 13603cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 13618c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 13628b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 1363d3740608SAnand Jain seq_printf(seq, ",commit=%u", info->commit_interval); 1364d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 13653cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1366d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 13673cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1368d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1369d0bd4560SJosef Bacik #endif 1370fb592373SJosef Bacik if (btrfs_test_opt(info, REF_VERIFY)) 1371fb592373SJosef Bacik seq_puts(seq, ",ref_verify"); 1372c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1373c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1374c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1375c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1376a9572a15SEric Paris return 0; 1377a9572a15SEric Paris } 1378a9572a15SEric Paris 1379a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 13802e635a27SChris Mason { 1381815745cfSAl Viro struct btrfs_fs_info *p = data; 1382815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 13834b82d6e4SYan 1384815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 13854b82d6e4SYan } 13864b82d6e4SYan 1387450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1388450ba0eaSJosef Bacik { 13896de1d09dSAl Viro int err = set_anon_super(s, data); 13906de1d09dSAl Viro if (!err) 1391450ba0eaSJosef Bacik s->s_fs_info = data; 13926de1d09dSAl Viro return err; 1393450ba0eaSJosef Bacik } 1394450ba0eaSJosef Bacik 1395830c4adbSJosef Bacik /* 1396f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1397f9d9ef62SDavid Sterba */ 1398f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1399f9d9ef62SDavid Sterba { 1400f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1401f9d9ef62SDavid Sterba return 1; 1402f9d9ef62SDavid Sterba return 0; 1403f9d9ef62SDavid Sterba } 1404f9d9ef62SDavid Sterba 1405bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1406ae0bc863SAnand Jain struct vfsmount *mnt) 1407830c4adbSJosef Bacik { 1408830c4adbSJosef Bacik struct dentry *root; 1409fa330659SOmar Sandoval int ret; 1410830c4adbSJosef Bacik 141105dbe683SOmar Sandoval if (!subvol_name) { 141205dbe683SOmar Sandoval if (!subvol_objectid) { 141305dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 141405dbe683SOmar Sandoval &subvol_objectid); 141505dbe683SOmar Sandoval if (ret) { 141605dbe683SOmar Sandoval root = ERR_PTR(ret); 141705dbe683SOmar Sandoval goto out; 141805dbe683SOmar Sandoval } 141905dbe683SOmar Sandoval } 142005dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 142105dbe683SOmar Sandoval subvol_objectid); 142205dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 142305dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 142405dbe683SOmar Sandoval subvol_name = NULL; 142505dbe683SOmar Sandoval goto out; 142605dbe683SOmar Sandoval } 142705dbe683SOmar Sandoval 142805dbe683SOmar Sandoval } 142905dbe683SOmar Sandoval 1430ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1431fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1432fa330659SOmar Sandoval mnt = NULL; 1433830c4adbSJosef Bacik 1434bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1435ea441d11SAl Viro struct super_block *s = root->d_sb; 1436ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1437bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1438bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1439bb289b7bSOmar Sandoval 1440bb289b7bSOmar Sandoval ret = 0; 1441bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1442ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1443bb289b7bSOmar Sandoval subvol_name); 1444bb289b7bSOmar Sandoval ret = -EINVAL; 1445bb289b7bSOmar Sandoval } 1446bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 144705dbe683SOmar Sandoval /* 144805dbe683SOmar Sandoval * This will also catch a race condition where a 144905dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 145005dbe683SOmar Sandoval * another subvolume is renamed over the old location. 145105dbe683SOmar Sandoval */ 1452ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1453ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1454bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1455bb289b7bSOmar Sandoval ret = -EINVAL; 1456bb289b7bSOmar Sandoval } 1457bb289b7bSOmar Sandoval if (ret) { 1458ea441d11SAl Viro dput(root); 1459bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1460ea441d11SAl Viro deactivate_locked_super(s); 1461bb289b7bSOmar Sandoval } 1462f9d9ef62SDavid Sterba } 1463f9d9ef62SDavid Sterba 1464fa330659SOmar Sandoval out: 1465fa330659SOmar Sandoval mntput(mnt); 1466fa330659SOmar Sandoval kfree(subvol_name); 1467830c4adbSJosef Bacik return root; 1468830c4adbSJosef Bacik } 1469450ba0eaSJosef Bacik 1470312c89fbSMisono, Tomohiro /* 1471312c89fbSMisono, Tomohiro * Find a superblock for the given device / mount point. 1472312c89fbSMisono, Tomohiro * 1473312c89fbSMisono, Tomohiro * Note: This is based on mount_bdev from fs/super.c with a few additions 1474312c89fbSMisono, Tomohiro * for multiple device setup. Make sure to keep it in sync. 1475312c89fbSMisono, Tomohiro */ 147672fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, 147772fa39f5SMisono, Tomohiro int flags, const char *device_name, void *data) 147872fa39f5SMisono, Tomohiro { 147972fa39f5SMisono, Tomohiro struct block_device *bdev = NULL; 148072fa39f5SMisono, Tomohiro struct super_block *s; 148136350e95SGu Jinxiang struct btrfs_device *device = NULL; 148272fa39f5SMisono, Tomohiro struct btrfs_fs_devices *fs_devices = NULL; 148372fa39f5SMisono, Tomohiro struct btrfs_fs_info *fs_info = NULL; 1484204cc0ccSAl Viro void *new_sec_opts = NULL; 148572fa39f5SMisono, Tomohiro fmode_t mode = FMODE_READ; 148672fa39f5SMisono, Tomohiro int error = 0; 148772fa39f5SMisono, Tomohiro 148872fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY)) 148972fa39f5SMisono, Tomohiro mode |= FMODE_WRITE; 149072fa39f5SMisono, Tomohiro 149172fa39f5SMisono, Tomohiro if (data) { 1492a65001e8SAl Viro error = security_sb_eat_lsm_opts(data, &new_sec_opts); 149372fa39f5SMisono, Tomohiro if (error) 149472fa39f5SMisono, Tomohiro return ERR_PTR(error); 149572fa39f5SMisono, Tomohiro } 149672fa39f5SMisono, Tomohiro 149772fa39f5SMisono, Tomohiro /* 149872fa39f5SMisono, Tomohiro * Setup a dummy root and fs_info for test/set super. This is because 149972fa39f5SMisono, Tomohiro * we don't actually fill this stuff out until open_ctree, but we need 150072fa39f5SMisono, Tomohiro * it for searching for existing supers, so this lets us do that and 150172fa39f5SMisono, Tomohiro * then open_ctree will properly initialize everything later. 150272fa39f5SMisono, Tomohiro */ 1503a8fd1f71SJeff Mahoney fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 150472fa39f5SMisono, Tomohiro if (!fs_info) { 150572fa39f5SMisono, Tomohiro error = -ENOMEM; 150672fa39f5SMisono, Tomohiro goto error_sec_opts; 150772fa39f5SMisono, Tomohiro } 150872fa39f5SMisono, Tomohiro 150972fa39f5SMisono, Tomohiro fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 151072fa39f5SMisono, Tomohiro fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 151172fa39f5SMisono, Tomohiro if (!fs_info->super_copy || !fs_info->super_for_commit) { 151272fa39f5SMisono, Tomohiro error = -ENOMEM; 151372fa39f5SMisono, Tomohiro goto error_fs_info; 151472fa39f5SMisono, Tomohiro } 151572fa39f5SMisono, Tomohiro 1516f5194e34SDavid Sterba mutex_lock(&uuid_mutex); 1517fa59f27cSAnand Jain error = btrfs_parse_device_options(data, mode, fs_type); 151881ffd56bSDavid Sterba if (error) { 1519399f7f4cSDavid Sterba mutex_unlock(&uuid_mutex); 1520399f7f4cSDavid Sterba goto error_fs_info; 152181ffd56bSDavid Sterba } 1522399f7f4cSDavid Sterba 152336350e95SGu Jinxiang device = btrfs_scan_one_device(device_name, mode, fs_type); 152436350e95SGu Jinxiang if (IS_ERR(device)) { 1525399f7f4cSDavid Sterba mutex_unlock(&uuid_mutex); 152636350e95SGu Jinxiang error = PTR_ERR(device); 1527399f7f4cSDavid Sterba goto error_fs_info; 152881ffd56bSDavid Sterba } 1529399f7f4cSDavid Sterba 153036350e95SGu Jinxiang fs_devices = device->fs_devices; 1531399f7f4cSDavid Sterba fs_info->fs_devices = fs_devices; 1532399f7f4cSDavid Sterba 153372fa39f5SMisono, Tomohiro error = btrfs_open_devices(fs_devices, mode, fs_type); 1534f5194e34SDavid Sterba mutex_unlock(&uuid_mutex); 153572fa39f5SMisono, Tomohiro if (error) 153672fa39f5SMisono, Tomohiro goto error_fs_info; 153772fa39f5SMisono, Tomohiro 153872fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { 153972fa39f5SMisono, Tomohiro error = -EACCES; 154072fa39f5SMisono, Tomohiro goto error_close_devices; 154172fa39f5SMisono, Tomohiro } 154272fa39f5SMisono, Tomohiro 154372fa39f5SMisono, Tomohiro bdev = fs_devices->latest_bdev; 154472fa39f5SMisono, Tomohiro s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, 154572fa39f5SMisono, Tomohiro fs_info); 154672fa39f5SMisono, Tomohiro if (IS_ERR(s)) { 154772fa39f5SMisono, Tomohiro error = PTR_ERR(s); 154872fa39f5SMisono, Tomohiro goto error_close_devices; 154972fa39f5SMisono, Tomohiro } 155072fa39f5SMisono, Tomohiro 155172fa39f5SMisono, Tomohiro if (s->s_root) { 155272fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 155372fa39f5SMisono, Tomohiro free_fs_info(fs_info); 155472fa39f5SMisono, Tomohiro if ((flags ^ s->s_flags) & SB_RDONLY) 155572fa39f5SMisono, Tomohiro error = -EBUSY; 155672fa39f5SMisono, Tomohiro } else { 155772fa39f5SMisono, Tomohiro snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 155872fa39f5SMisono, Tomohiro btrfs_sb(s)->bdev_holder = fs_type; 15599b4e675aSDavid Sterba if (!strstr(crc32c_impl(), "generic")) 15609b4e675aSDavid Sterba set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags); 156172fa39f5SMisono, Tomohiro error = btrfs_fill_super(s, fs_devices, data); 156272fa39f5SMisono, Tomohiro } 1563a65001e8SAl Viro if (!error) 1564204cc0ccSAl Viro error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL); 1565a65001e8SAl Viro security_free_mnt_opts(&new_sec_opts); 156672fa39f5SMisono, Tomohiro if (error) { 156772fa39f5SMisono, Tomohiro deactivate_locked_super(s); 1568a65001e8SAl Viro return ERR_PTR(error); 156972fa39f5SMisono, Tomohiro } 157072fa39f5SMisono, Tomohiro 157172fa39f5SMisono, Tomohiro return dget(s->s_root); 157272fa39f5SMisono, Tomohiro 157372fa39f5SMisono, Tomohiro error_close_devices: 157472fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 157572fa39f5SMisono, Tomohiro error_fs_info: 157672fa39f5SMisono, Tomohiro free_fs_info(fs_info); 157772fa39f5SMisono, Tomohiro error_sec_opts: 157872fa39f5SMisono, Tomohiro security_free_mnt_opts(&new_sec_opts); 157972fa39f5SMisono, Tomohiro return ERR_PTR(error); 158072fa39f5SMisono, Tomohiro } 1581312c89fbSMisono, Tomohiro 1582edf24abeSChristoph Hellwig /* 1583312c89fbSMisono, Tomohiro * Mount function which is called by VFS layer. 1584edf24abeSChristoph Hellwig * 1585312c89fbSMisono, Tomohiro * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() 1586312c89fbSMisono, Tomohiro * which needs vfsmount* of device's root (/). This means device's root has to 1587312c89fbSMisono, Tomohiro * be mounted internally in any case. 1588312c89fbSMisono, Tomohiro * 1589312c89fbSMisono, Tomohiro * Operation flow: 1590312c89fbSMisono, Tomohiro * 1. Parse subvol id related options for later use in mount_subvol(). 1591312c89fbSMisono, Tomohiro * 1592312c89fbSMisono, Tomohiro * 2. Mount device's root (/) by calling vfs_kern_mount(). 1593312c89fbSMisono, Tomohiro * 1594312c89fbSMisono, Tomohiro * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the 1595312c89fbSMisono, Tomohiro * first place. In order to avoid calling btrfs_mount() again, we use 1596312c89fbSMisono, Tomohiro * different file_system_type which is not registered to VFS by 1597312c89fbSMisono, Tomohiro * register_filesystem() (btrfs_root_fs_type). As a result, 1598312c89fbSMisono, Tomohiro * btrfs_mount_root() is called. The return value will be used by 1599312c89fbSMisono, Tomohiro * mount_subtree() in mount_subvol(). 1600312c89fbSMisono, Tomohiro * 1601312c89fbSMisono, Tomohiro * 3. Call mount_subvol() to get the dentry of subvolume. Since there is 1602312c89fbSMisono, Tomohiro * "btrfs subvolume set-default", mount_subvol() is called always. 1603edf24abeSChristoph Hellwig */ 1604061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1605306e16ceSDavid Sterba const char *device_name, void *data) 16064b82d6e4SYan { 1607312c89fbSMisono, Tomohiro struct vfsmount *mnt_root; 1608312c89fbSMisono, Tomohiro struct dentry *root; 160973f73415SJosef Bacik char *subvol_name = NULL; 161073f73415SJosef Bacik u64 subvol_objectid = 0; 16114b82d6e4SYan int error = 0; 16124b82d6e4SYan 161393b9bcdfSGu Jinxiang error = btrfs_parse_subvol_options(data, &subvol_name, 161493b9bcdfSGu Jinxiang &subvol_objectid); 1615f23c8af8SIlya Dryomov if (error) { 1616f23c8af8SIlya Dryomov kfree(subvol_name); 1617061dbc6bSAl Viro return ERR_PTR(error); 1618f23c8af8SIlya Dryomov } 1619edf24abeSChristoph Hellwig 1620312c89fbSMisono, Tomohiro /* mount device's root (/) */ 1621312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); 1622312c89fbSMisono, Tomohiro if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { 1623312c89fbSMisono, Tomohiro if (flags & SB_RDONLY) { 1624312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1625312c89fbSMisono, Tomohiro flags & ~SB_RDONLY, device_name, data); 16264b82d6e4SYan } else { 1627312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1628312c89fbSMisono, Tomohiro flags | SB_RDONLY, device_name, data); 1629312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1630312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1631532b618bSEric W. Biederman kfree(subvol_name); 1632312c89fbSMisono, Tomohiro goto out; 1633f667aef6SQu Wenruo } 1634f667aef6SQu Wenruo 1635312c89fbSMisono, Tomohiro down_write(&mnt_root->mnt_sb->s_umount); 1636312c89fbSMisono, Tomohiro error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); 1637312c89fbSMisono, Tomohiro up_write(&mnt_root->mnt_sb->s_umount); 1638312c89fbSMisono, Tomohiro if (error < 0) { 1639312c89fbSMisono, Tomohiro root = ERR_PTR(error); 1640312c89fbSMisono, Tomohiro mntput(mnt_root); 1641532b618bSEric W. Biederman kfree(subvol_name); 1642312c89fbSMisono, Tomohiro goto out; 1643312c89fbSMisono, Tomohiro } 1644312c89fbSMisono, Tomohiro } 1645312c89fbSMisono, Tomohiro } 1646312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1647312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1648532b618bSEric W. Biederman kfree(subvol_name); 1649312c89fbSMisono, Tomohiro goto out; 1650f667aef6SQu Wenruo } 16514b82d6e4SYan 1652312c89fbSMisono, Tomohiro /* mount_subvol() will free subvol_name and mnt_root */ 1653ae0bc863SAnand Jain root = mount_subvol(subvol_name, subvol_objectid, mnt_root); 16544b82d6e4SYan 1655312c89fbSMisono, Tomohiro out: 1656312c89fbSMisono, Tomohiro return root; 16574b82d6e4SYan } 16582e635a27SChris Mason 16590d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 1660f7b885beSAnand Jain u32 new_pool_size, u32 old_pool_size) 16610d2450abSSergei Trofimovich { 16620d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 16630d2450abSSergei Trofimovich return; 16640d2450abSSergei Trofimovich 16650d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 16660d2450abSSergei Trofimovich 1667efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 16680d2450abSSergei Trofimovich old_pool_size, new_pool_size); 16690d2450abSSergei Trofimovich 16705cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1671afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1672e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1673fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1674fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1675fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1676fccb5d86SQu Wenruo new_pool_size); 1677fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1678fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 16795b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1680736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 16810339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1682ff023aacSStefan Behrens new_pool_size); 16830d2450abSSergei Trofimovich } 16840d2450abSSergei Trofimovich 1685f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1686dc81cdc5SMiao Xie { 1687dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1688f42a34b2SMiao Xie } 1689dc81cdc5SMiao Xie 1690f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1691f42a34b2SMiao Xie unsigned long old_opts, int flags) 1692f42a34b2SMiao Xie { 1693dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1694dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 16951751e8a6SLinus Torvalds (flags & SB_RDONLY))) { 1696dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1697dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1698dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 16991751e8a6SLinus Torvalds if (flags & SB_RDONLY) 1700dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1701dc81cdc5SMiao Xie } 1702dc81cdc5SMiao Xie } 1703dc81cdc5SMiao Xie 1704dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1705dc81cdc5SMiao Xie unsigned long old_opts) 1706dc81cdc5SMiao Xie { 1707dc81cdc5SMiao Xie /* 1708180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1709180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1710dc81cdc5SMiao Xie */ 1711dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1712bc98a42cSDavid Howells (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1713dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1714dc81cdc5SMiao Xie } 1715dc81cdc5SMiao Xie 1716dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1717dc81cdc5SMiao Xie } 1718dc81cdc5SMiao Xie 1719c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1720c146afadSYan Zheng { 1721815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1722815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 172349b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 172449b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 172549b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 172649b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 1727f7b885beSAnand Jain u32 old_thread_pool_size = fs_info->thread_pool_size; 1728d612ac59SAnand Jain u32 old_metadata_ratio = fs_info->metadata_ratio; 1729c146afadSYan Zheng int ret; 1730c146afadSYan Zheng 173102b9984dSTheodore Ts'o sync_filesystem(sb); 1732f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1733dc81cdc5SMiao Xie 1734f667aef6SQu Wenruo if (data) { 1735204cc0ccSAl Viro void *new_sec_opts = NULL; 1736f667aef6SQu Wenruo 1737a65001e8SAl Viro ret = security_sb_eat_lsm_opts(data, &new_sec_opts); 1738a65001e8SAl Viro if (!ret) 1739204cc0ccSAl Viro ret = security_sb_remount(sb, new_sec_opts); 1740a65001e8SAl Viro security_free_mnt_opts(&new_sec_opts); 1741f667aef6SQu Wenruo if (ret) 1742f667aef6SQu Wenruo goto restore; 1743f667aef6SQu Wenruo } 1744f667aef6SQu Wenruo 17452ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 1746891f41cbSChengguang Xu if (ret) 174749b25e05SJeff Mahoney goto restore; 1748b288052eSChris Mason 1749f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 17500d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 17510d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 17520d2450abSSergei Trofimovich 17531751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) 1754dc81cdc5SMiao Xie goto out; 1755c146afadSYan Zheng 17561751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 17578dabb742SStefan Behrens /* 17588dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 17598dabb742SStefan Behrens * the filesystem is busy. 17608dabb742SStefan Behrens */ 176121c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1762361c093dSStefan Behrens 1763361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1764361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1765361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1766361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1767361c093dSStefan Behrens 17681751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 1769c146afadSYan Zheng 1770e44163e1SJeff Mahoney /* 17711751e8a6SLinus Torvalds * Setting SB_RDONLY will put the cleaner thread to 1772e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1773e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1774e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1775e44163e1SJeff Mahoney * unless we clean them up here. 1776e44163e1SJeff Mahoney */ 1777e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1778e44163e1SJeff Mahoney 17798dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 17808dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1781061594efSMiao Xie btrfs_pause_balance(fs_info); 17828dabb742SStefan Behrens 17836bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 178449b25e05SJeff Mahoney if (ret) 178549b25e05SJeff Mahoney goto restore; 1786c146afadSYan Zheng } else { 17870b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 17886ef3de9cSDavid Sterba btrfs_err(fs_info, 1789efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 17906ef3de9cSDavid Sterba ret = -EINVAL; 17916ef3de9cSDavid Sterba goto restore; 17926ef3de9cSDavid Sterba } 17938a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 179449b25e05SJeff Mahoney ret = -EACCES; 179549b25e05SJeff Mahoney goto restore; 17968a3db184SSergei Trofimovich } 17972b82032cSYan Zheng 17986528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) { 1799efe120a0SFrank Holton btrfs_warn(fs_info, 180052042d8eSAndrea Gelmini "too many missing devices, writable remount is not allowed"); 1801292fd7fcSStefan Behrens ret = -EACCES; 1802292fd7fcSStefan Behrens goto restore; 1803292fd7fcSStefan Behrens } 1804292fd7fcSStefan Behrens 18058a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 180649b25e05SJeff Mahoney ret = -EINVAL; 180749b25e05SJeff Mahoney goto restore; 18088a3db184SSergei Trofimovich } 1809c146afadSYan Zheng 1810815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 181149b25e05SJeff Mahoney if (ret) 181249b25e05SJeff Mahoney goto restore; 1813c146afadSYan Zheng 1814d68fc57bSYan, Zheng /* recover relocation */ 18155f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1816d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 18175f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 181849b25e05SJeff Mahoney if (ret) 181949b25e05SJeff Mahoney goto restore; 1820c146afadSYan Zheng 18212b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 18222b6ba629SIlya Dryomov if (ret) 18232b6ba629SIlya Dryomov goto restore; 18242b6ba629SIlya Dryomov 18258dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 18268dabb742SStefan Behrens if (ret) { 1827efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 18288dabb742SStefan Behrens goto restore; 18298dabb742SStefan Behrens } 183094aebfb2SJosef Bacik 18316c6b5a39SAleksa Sarai btrfs_qgroup_rescan_resume(fs_info); 18326c6b5a39SAleksa Sarai 183394aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1834efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 183594aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 183694aebfb2SJosef Bacik if (ret) { 18375d163e0eSJeff Mahoney btrfs_warn(fs_info, 18385d163e0eSJeff Mahoney "failed to create the UUID tree %d", 18395d163e0eSJeff Mahoney ret); 184094aebfb2SJosef Bacik goto restore; 184194aebfb2SJosef Bacik } 184294aebfb2SJosef Bacik } 18431751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 184490c711abSZygo Blaxell 1845afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 1846c146afadSYan Zheng } 1847dc81cdc5SMiao Xie out: 18482c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1849dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1850c146afadSYan Zheng return 0; 185149b25e05SJeff Mahoney 185249b25e05SJeff Mahoney restore: 18531751e8a6SLinus Torvalds /* We've hit an error - don't reset SB_RDONLY */ 1854bc98a42cSDavid Howells if (sb_rdonly(sb)) 18551751e8a6SLinus Torvalds old_flags |= SB_RDONLY; 185649b25e05SJeff Mahoney sb->s_flags = old_flags; 185749b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 185849b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 185949b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 18600d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 18610d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 186249b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1863dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 186449b25e05SJeff Mahoney return ret; 1865c146afadSYan Zheng } 1866c146afadSYan Zheng 1867bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 18687e17916bSArnd Bergmann static inline int btrfs_cmp_device_free_bytes(const void *dev_info1, 1869bcd53741SArne Jansen const void *dev_info2) 1870bcd53741SArne Jansen { 1871bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1872bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1873bcd53741SArne Jansen return -1; 1874bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1875bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1876bcd53741SArne Jansen return 1; 1877bcd53741SArne Jansen else 1878bcd53741SArne Jansen return 0; 1879bcd53741SArne Jansen } 1880bcd53741SArne Jansen 1881bcd53741SArne Jansen /* 1882bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1883bcd53741SArne Jansen * is stored.(Descending Sort) 1884bcd53741SArne Jansen */ 1885bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1886bcd53741SArne Jansen struct btrfs_device_info *devices, 1887bcd53741SArne Jansen size_t nr_devices) 1888bcd53741SArne Jansen { 1889bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1890bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1891bcd53741SArne Jansen } 1892bcd53741SArne Jansen 18936d07bcecSMiao Xie /* 18946d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 18956d07bcecSMiao Xie * file data. 18966d07bcecSMiao Xie */ 18977e17916bSArnd Bergmann static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 18986bccf3abSJeff Mahoney u64 *free_bytes) 18996d07bcecSMiao Xie { 19006d07bcecSMiao Xie struct btrfs_device_info *devices_info; 19016d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 19026d07bcecSMiao Xie struct btrfs_device *device; 19036d07bcecSMiao Xie u64 type; 19046d07bcecSMiao Xie u64 avail_space; 19056d07bcecSMiao Xie u64 min_stripe_size; 1906559ca6eaSNikolay Borisov int num_stripes = 1; 19076d07bcecSMiao Xie int i = 0, nr_devices; 19084f080f57SDavid Sterba const struct btrfs_raid_attr *rattr; 19096d07bcecSMiao Xie 19107e33fd99SJosef Bacik /* 191101327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 19127e33fd99SJosef Bacik * enough for our purposes. 19137e33fd99SJosef Bacik */ 1914b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 19157e33fd99SJosef Bacik if (!nr_devices) { 19167e33fd99SJosef Bacik smp_mb(); 19177e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 19187e33fd99SJosef Bacik ASSERT(nr_devices); 19197e33fd99SJosef Bacik if (!nr_devices) { 19207e33fd99SJosef Bacik *free_bytes = 0; 19217e33fd99SJosef Bacik return 0; 19227e33fd99SJosef Bacik } 19237e33fd99SJosef Bacik } 19246d07bcecSMiao Xie 1925d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 19266a44517dSDavid Sterba GFP_KERNEL); 19276d07bcecSMiao Xie if (!devices_info) 19286d07bcecSMiao Xie return -ENOMEM; 19296d07bcecSMiao Xie 193001327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 19311b86826dSJeff Mahoney type = btrfs_data_alloc_profile(fs_info); 19324f080f57SDavid Sterba rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)]; 19334f080f57SDavid Sterba 1934e1ea2beeSDavid Sterba if (type & BTRFS_BLOCK_GROUP_RAID0) 193539fb26c3SMiao Xie num_stripes = nr_devices; 1936e1ea2beeSDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1) 193739fb26c3SMiao Xie num_stripes = 2; 193847e6f742SDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1C3) 193947e6f742SDavid Sterba num_stripes = 3; 1940*8d6fac00SDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID1C4) 1941*8d6fac00SDavid Sterba num_stripes = 4; 1942e1ea2beeSDavid Sterba else if (type & BTRFS_BLOCK_GROUP_RAID10) 194339fb26c3SMiao Xie num_stripes = 4; 19446d07bcecSMiao Xie 19454f080f57SDavid Sterba /* Adjust for more than 1 stripe per device */ 19464f080f57SDavid Sterba min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN; 19476d07bcecSMiao Xie 19487e33fd99SJosef Bacik rcu_read_lock(); 19497e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 1950e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, 1951e12c9621SAnand Jain &device->dev_state) || 1952401e29c1SAnand Jain !device->bdev || 1953401e29c1SAnand Jain test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) 19546d07bcecSMiao Xie continue; 19556d07bcecSMiao Xie 19567e33fd99SJosef Bacik if (i >= nr_devices) 19577e33fd99SJosef Bacik break; 19587e33fd99SJosef Bacik 19596d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 19606d07bcecSMiao Xie 19616d07bcecSMiao Xie /* align with stripe_len */ 1962559ca6eaSNikolay Borisov avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN); 19636d07bcecSMiao Xie 19646d07bcecSMiao Xie /* 196501327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 19666d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 19676d07bcecSMiao Xie * allocation. 1968559ca6eaSNikolay Borisov * 1969559ca6eaSNikolay Borisov * This ensures we have at least min_stripe_size free space 1970559ca6eaSNikolay Borisov * after excluding 1MB. 19716d07bcecSMiao Xie */ 1972559ca6eaSNikolay Borisov if (avail_space <= SZ_1M + min_stripe_size) 19736d07bcecSMiao Xie continue; 19746d07bcecSMiao Xie 1975559ca6eaSNikolay Borisov avail_space -= SZ_1M; 1976559ca6eaSNikolay Borisov 19776d07bcecSMiao Xie devices_info[i].dev = device; 19786d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 19796d07bcecSMiao Xie 19806d07bcecSMiao Xie i++; 19816d07bcecSMiao Xie } 19827e33fd99SJosef Bacik rcu_read_unlock(); 19836d07bcecSMiao Xie 19846d07bcecSMiao Xie nr_devices = i; 19856d07bcecSMiao Xie 19866d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 19876d07bcecSMiao Xie 19886d07bcecSMiao Xie i = nr_devices - 1; 19896d07bcecSMiao Xie avail_space = 0; 1990559ca6eaSNikolay Borisov while (nr_devices >= rattr->devs_min) { 1991559ca6eaSNikolay Borisov num_stripes = min(num_stripes, nr_devices); 199239fb26c3SMiao Xie 19936d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 19946d07bcecSMiao Xie int j; 19956d07bcecSMiao Xie u64 alloc_size; 19966d07bcecSMiao Xie 199739fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 19986d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 199939fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 20006d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 20016d07bcecSMiao Xie } 20026d07bcecSMiao Xie i--; 20036d07bcecSMiao Xie nr_devices--; 20046d07bcecSMiao Xie } 20056d07bcecSMiao Xie 20066d07bcecSMiao Xie kfree(devices_info); 20076d07bcecSMiao Xie *free_bytes = avail_space; 20086d07bcecSMiao Xie return 0; 20096d07bcecSMiao Xie } 20106d07bcecSMiao Xie 2011ba7b6e62SDavid Sterba /* 2012ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2013ba7b6e62SDavid Sterba * 2014ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2015ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2016ba7b6e62SDavid Sterba * 2017ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 20180d0c71b3SDavid Sterba * algorithm that respects the device sizes and order of allocations. This is 20190d0c71b3SDavid Sterba * a close approximation of the actual use but there are other factors that may 20200d0c71b3SDavid Sterba * change the result (like a new metadata chunk). 2021ba7b6e62SDavid Sterba * 2022ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2023ba7b6e62SDavid Sterba */ 20248fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20258fd17795SChris Mason { 2026815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2027815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2028bd4d1088SJosef Bacik struct btrfs_space_info *found; 2029bd4d1088SJosef Bacik u64 total_used = 0; 20306d07bcecSMiao Xie u64 total_free_data = 0; 2031ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2032db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2033de37aa51SNikolay Borisov __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid; 2034ba7b6e62SDavid Sterba unsigned factor = 1; 2035ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 20366d07bcecSMiao Xie int ret; 2037ca8a51b3SDavid Sterba u64 thresh = 0; 2038ae02d1bdSLuis de Bethencourt int mixed = 0; 20398fd17795SChris Mason 2040bd4d1088SJosef Bacik rcu_read_lock(); 2041f5389f33SJohannes Thumshirn list_for_each_entry_rcu(found, &fs_info->space_info, list) { 20426d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2043ba7b6e62SDavid Sterba int i; 2044ba7b6e62SDavid Sterba 20456d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 20466d07bcecSMiao Xie total_free_data -= 20476d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2048ba7b6e62SDavid Sterba 2049ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 205046df06b8SDavid Sterba if (!list_empty(&found->block_groups[i])) 205146df06b8SDavid Sterba factor = btrfs_bg_type_to_factor( 205246df06b8SDavid Sterba btrfs_raid_array[i].bg_flag); 2053ba7b6e62SDavid Sterba } 20546d07bcecSMiao Xie } 2055ae02d1bdSLuis de Bethencourt 2056ae02d1bdSLuis de Bethencourt /* 2057ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2058ae02d1bdSLuis de Bethencourt */ 2059ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2060ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2061ae02d1bdSLuis de Bethencourt mixed = 1; 2062ae02d1bdSLuis de Bethencourt else 2063ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2064ae02d1bdSLuis de Bethencourt found->disk_used; 2065ae02d1bdSLuis de Bethencourt } 20666d07bcecSMiao Xie 2067b742bb82SYan, Zheng total_used += found->disk_used; 206889a55897SJosef Bacik } 2069ba7b6e62SDavid Sterba 2070bd4d1088SJosef Bacik rcu_read_unlock(); 2071bd4d1088SJosef Bacik 2072ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2073ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2074ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2075ba7b6e62SDavid Sterba 2076ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2077ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 207841b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 207941b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2080ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 208141b34accSLuis de Bethencourt else 208241b34accSLuis de Bethencourt buf->f_bfree = 0; 2083ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2084ba7b6e62SDavid Sterba 20850d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 20866bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 20877e33fd99SJosef Bacik if (ret) 20886d07bcecSMiao Xie return ret; 2089ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 20906d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2091d397712bSChris Mason 2092ca8a51b3SDavid Sterba /* 2093ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2094ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2095ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2096ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2097ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2098ca8a51b3SDavid Sterba * 2099ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2100ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2101ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2102ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2103ca8a51b3SDavid Sterba * way around. 2104ca8a51b3SDavid Sterba */ 2105d4417e22SNikolay Borisov thresh = SZ_4M; 2106ca8a51b3SDavid Sterba 2107ae02d1bdSLuis de Bethencourt if (!mixed && total_free_meta - thresh < block_rsv->size) 2108ca8a51b3SDavid Sterba buf->f_bavail = 0; 2109ca8a51b3SDavid Sterba 2110ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2111ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2112ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2113ba7b6e62SDavid Sterba 21149d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 21159d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21169d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21179d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21189d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 211932d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 21204fd786e6SMisono Tomohiro buf->f_fsid.val[0] ^= 21214fd786e6SMisono Tomohiro BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32; 21224fd786e6SMisono Tomohiro buf->f_fsid.val[1] ^= 21234fd786e6SMisono Tomohiro BTRFS_I(d_inode(dentry))->root->root_key.objectid; 212432d48fa1SDavid Woodhouse 21258fd17795SChris Mason return 0; 21268fd17795SChris Mason } 2127b5133862SChris Mason 2128aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2129aea52e19SAl Viro { 2130815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2131aea52e19SAl Viro kill_anon_super(sb); 2132aea52e19SAl Viro free_fs_info(fs_info); 2133aea52e19SAl Viro } 2134aea52e19SAl Viro 21352e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 21362e635a27SChris Mason .owner = THIS_MODULE, 21372e635a27SChris Mason .name = "btrfs", 2138061dbc6bSAl Viro .mount = btrfs_mount, 2139aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2140f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 21412e635a27SChris Mason }; 214272fa39f5SMisono, Tomohiro 214372fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = { 214472fa39f5SMisono, Tomohiro .owner = THIS_MODULE, 214572fa39f5SMisono, Tomohiro .name = "btrfs", 214672fa39f5SMisono, Tomohiro .mount = btrfs_mount_root, 214772fa39f5SMisono, Tomohiro .kill_sb = btrfs_kill_super, 214872fa39f5SMisono, Tomohiro .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 214972fa39f5SMisono, Tomohiro }; 215072fa39f5SMisono, Tomohiro 21517f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2152a9218f6bSChris Mason 2153d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2154d8620958STom Van Braeckel { 2155d8620958STom Van Braeckel /* 2156d8620958STom Van Braeckel * The control file's private_data is used to hold the 2157d8620958STom Van Braeckel * transaction when it is started and is used to keep 2158d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2159d8620958STom Van Braeckel */ 2160d8620958STom Van Braeckel file->private_data = NULL; 2161d8620958STom Van Braeckel return 0; 2162d8620958STom Van Braeckel } 2163d8620958STom Van Braeckel 2164d352ac68SChris Mason /* 2165d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2166d352ac68SChris Mason */ 21678a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 21688a4b83ccSChris Mason unsigned long arg) 21698a4b83ccSChris Mason { 21708a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 217136350e95SGu Jinxiang struct btrfs_device *device = NULL; 2172c071fcfdSChris Mason int ret = -ENOTTY; 21738a4b83ccSChris Mason 2174e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2175e441d54dSChris Mason return -EPERM; 2176e441d54dSChris Mason 2177dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2178dae7b665SLi Zefan if (IS_ERR(vol)) 2179dae7b665SLi Zefan return PTR_ERR(vol); 2180f505754fSFilipe Manana vol->name[BTRFS_PATH_NAME_MAX] = '\0'; 2181c071fcfdSChris Mason 21828a4b83ccSChris Mason switch (cmd) { 21838a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 2184899f9307SDavid Sterba mutex_lock(&uuid_mutex); 218536350e95SGu Jinxiang device = btrfs_scan_one_device(vol->name, FMODE_READ, 218636350e95SGu Jinxiang &btrfs_root_fs_type); 218736350e95SGu Jinxiang ret = PTR_ERR_OR_ZERO(device); 2188899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 21898a4b83ccSChris Mason break; 2190228a73abSAnand Jain case BTRFS_IOC_FORGET_DEV: 2191228a73abSAnand Jain ret = btrfs_forget_devices(vol->name); 2192228a73abSAnand Jain break; 219302db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 2194899f9307SDavid Sterba mutex_lock(&uuid_mutex); 219536350e95SGu Jinxiang device = btrfs_scan_one_device(vol->name, FMODE_READ, 219636350e95SGu Jinxiang &btrfs_root_fs_type); 219736350e95SGu Jinxiang if (IS_ERR(device)) { 2198899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 219936350e95SGu Jinxiang ret = PTR_ERR(device); 220002db0844SJosef Bacik break; 2201899f9307SDavid Sterba } 220236350e95SGu Jinxiang ret = !(device->fs_devices->num_devices == 220336350e95SGu Jinxiang device->fs_devices->total_devices); 2204899f9307SDavid Sterba mutex_unlock(&uuid_mutex); 220502db0844SJosef Bacik break; 2206c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2207d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2208c5868f83SDavid Sterba break; 22098a4b83ccSChris Mason } 2210dae7b665SLi Zefan 22118a4b83ccSChris Mason kfree(vol); 2212f819d837SLinda Knippers return ret; 22138a4b83ccSChris Mason } 22148a4b83ccSChris Mason 22150176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2216ed0dab6bSYan { 2217354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 22180b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 22190b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2220354aa0fbSMiao Xie 2221fac03c8dSDavid Sterba set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22229e7cc91aSWang Xiaoguang /* 22239e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 22249e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 22259e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 22269e7cc91aSWang Xiaoguang * ourselves. 22279e7cc91aSWang Xiaoguang */ 2228d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2229354aa0fbSMiao Xie if (IS_ERR(trans)) { 2230354aa0fbSMiao Xie /* no transaction, don't bother */ 2231354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 22320176260fSLinus Torvalds return 0; 2233354aa0fbSMiao Xie return PTR_ERR(trans); 2234354aa0fbSMiao Xie } 22353a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2236ed0dab6bSYan } 2237ed0dab6bSYan 22389e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 22399e7cc91aSWang Xiaoguang { 2240fac03c8dSDavid Sterba struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2241fac03c8dSDavid Sterba 2242fac03c8dSDavid Sterba clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22439e7cc91aSWang Xiaoguang return 0; 22449e7cc91aSWang Xiaoguang } 22459e7cc91aSWang Xiaoguang 22469c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 22479c5085c1SJosef Bacik { 22489c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 22499c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 22509c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 22519c5085c1SJosef Bacik struct list_head *head; 22529c5085c1SJosef Bacik 225388c14590SDavid Sterba /* 225488c14590SDavid Sterba * Lightweight locking of the devices. We should not need 225588c14590SDavid Sterba * device_list_mutex here as we only read the device data and the list 225688c14590SDavid Sterba * is protected by RCU. Even if a device is deleted during the list 225788c14590SDavid Sterba * traversals, we'll get valid data, the freeing callback will wait at 225852042d8eSAndrea Gelmini * least until the rcu_read_unlock. 225988c14590SDavid Sterba */ 226088c14590SDavid Sterba rcu_read_lock(); 22619c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 22629c5085c1SJosef Bacik while (cur_devices) { 22639c5085c1SJosef Bacik head = &cur_devices->devices; 226488c14590SDavid Sterba list_for_each_entry_rcu(dev, head, dev_list) { 2265e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 2266aa9ddcd4SJosef Bacik continue; 22670aeb8a6eSAnand Jain if (!dev->name) 22680aeb8a6eSAnand Jain continue; 22699c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 22709c5085c1SJosef Bacik first_dev = dev; 22719c5085c1SJosef Bacik } 22729c5085c1SJosef Bacik cur_devices = cur_devices->seed; 22739c5085c1SJosef Bacik } 22749c5085c1SJosef Bacik 2275672d5990SMisono Tomohiro if (first_dev) 2276672d5990SMisono Tomohiro seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\"); 2277672d5990SMisono Tomohiro else 22789c5085c1SJosef Bacik WARN_ON(1); 227988c14590SDavid Sterba rcu_read_unlock(); 22809c5085c1SJosef Bacik return 0; 22819c5085c1SJosef Bacik } 22829c5085c1SJosef Bacik 2283b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 228476dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2285bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2286e20d96d6SChris Mason .put_super = btrfs_put_super, 2287d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2288a9572a15SEric Paris .show_options = btrfs_show_options, 22899c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 22902c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 22912c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 229226602cabSAl Viro .free_inode = btrfs_free_inode, 22938fd17795SChris Mason .statfs = btrfs_statfs, 2294c146afadSYan Zheng .remount_fs = btrfs_remount, 22950176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 22969e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2297e20d96d6SChris Mason }; 2298a9218f6bSChris Mason 2299a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2300d8620958STom Van Braeckel .open = btrfs_control_open, 2301a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2302a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2303a9218f6bSChris Mason .owner = THIS_MODULE, 23046038f373SArnd Bergmann .llseek = noop_llseek, 2305a9218f6bSChris Mason }; 2306a9218f6bSChris Mason 2307a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2308578454ffSKay Sievers .minor = BTRFS_MINOR, 2309a9218f6bSChris Mason .name = "btrfs-control", 2310a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2311a9218f6bSChris Mason }; 2312a9218f6bSChris Mason 2313578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2314578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2315578454ffSKay Sievers 2316f5c29bd9SLiu Bo static int __init btrfs_interface_init(void) 2317a9218f6bSChris Mason { 2318a9218f6bSChris Mason return misc_register(&btrfs_misc); 2319a9218f6bSChris Mason } 2320a9218f6bSChris Mason 2321e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void) 2322a9218f6bSChris Mason { 2323f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2324a9218f6bSChris Mason } 2325a9218f6bSChris Mason 2326f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void) 232785965600SDavid Sterba { 2328edf57cbfSBart Van Assche static const char options[] = "" 232985965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 233085965600SDavid Sterba ", debug=on" 233185965600SDavid Sterba #endif 233279556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 233379556c3dSStefan Behrens ", assert=on" 233479556c3dSStefan Behrens #endif 233585965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 233685965600SDavid Sterba ", integrity-checker=on" 233785965600SDavid Sterba #endif 2338fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 2339fb592373SJosef Bacik ", ref-verify=on" 2340fb592373SJosef Bacik #endif 2341edf57cbfSBart Van Assche ; 2342edf57cbfSBart Van Assche pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options); 234385965600SDavid Sterba } 234485965600SDavid Sterba 23452e635a27SChris Mason static int __init init_btrfs_fs(void) 23462e635a27SChris Mason { 23472c90e5d6SChris Mason int err; 234858176a96SJosef Bacik 234963541927SFilipe David Borba Manana btrfs_props_init(); 235063541927SFilipe David Borba Manana 235158176a96SJosef Bacik err = btrfs_init_sysfs(); 235258176a96SJosef Bacik if (err) 23539678c543SNikolay Borisov return err; 235458176a96SJosef Bacik 2355143bede5SJeff Mahoney btrfs_init_compress(); 2356d1310b2eSChris Mason 2357261507a0SLi Zefan err = btrfs_init_cachep(); 2358261507a0SLi Zefan if (err) 2359261507a0SLi Zefan goto free_compress; 2360261507a0SLi Zefan 2361d1310b2eSChris Mason err = extent_io_init(); 23622f4cbe64SWyatt Banks if (err) 23632f4cbe64SWyatt Banks goto free_cachep; 23642f4cbe64SWyatt Banks 23656f0d04f8SJosef Bacik err = extent_state_cache_init(); 2366d1310b2eSChris Mason if (err) 2367d1310b2eSChris Mason goto free_extent_io; 2368d1310b2eSChris Mason 23696f0d04f8SJosef Bacik err = extent_map_init(); 23706f0d04f8SJosef Bacik if (err) 23716f0d04f8SJosef Bacik goto free_extent_state_cache; 23726f0d04f8SJosef Bacik 23736352b91dSMiao Xie err = ordered_data_init(); 23742f4cbe64SWyatt Banks if (err) 23752f4cbe64SWyatt Banks goto free_extent_map; 2376c8b97818SChris Mason 23776352b91dSMiao Xie err = btrfs_delayed_inode_init(); 23786352b91dSMiao Xie if (err) 23796352b91dSMiao Xie goto free_ordered_data; 23806352b91dSMiao Xie 23819247f317SMiao Xie err = btrfs_auto_defrag_init(); 238216cdcec7SMiao Xie if (err) 238316cdcec7SMiao Xie goto free_delayed_inode; 238416cdcec7SMiao Xie 238578a6184aSMiao Xie err = btrfs_delayed_ref_init(); 23869247f317SMiao Xie if (err) 23879247f317SMiao Xie goto free_auto_defrag; 23889247f317SMiao Xie 2389b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2390b9e9a6cbSWang Shilong if (err) 2391af13b492SDavid Sterba goto free_delayed_ref; 2392b9e9a6cbSWang Shilong 239397eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 239478a6184aSMiao Xie if (err) 2395af13b492SDavid Sterba goto free_prelim_ref; 239678a6184aSMiao Xie 239797eb6b69SDavid Sterba err = btrfs_interface_init(); 239897eb6b69SDavid Sterba if (err) 239997eb6b69SDavid Sterba goto free_end_io_wq; 240097eb6b69SDavid Sterba 2401e565d4b9SJan Schmidt btrfs_init_lockdep(); 2402e565d4b9SJan Schmidt 24038ae1af3cSAnand Jain btrfs_print_mod_info(); 2404dc11dd5dSJosef Bacik 2405dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2406dc11dd5dSJosef Bacik if (err) 2407dc11dd5dSJosef Bacik goto unregister_ioctl; 2408dc11dd5dSJosef Bacik 2409dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2410dc11dd5dSJosef Bacik if (err) 2411dc11dd5dSJosef Bacik goto unregister_ioctl; 241274255aa0SJosef Bacik 24132f4cbe64SWyatt Banks return 0; 24142f4cbe64SWyatt Banks 2415a9218f6bSChris Mason unregister_ioctl: 2416a9218f6bSChris Mason btrfs_interface_exit(); 241797eb6b69SDavid Sterba free_end_io_wq: 241897eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2419b9e9a6cbSWang Shilong free_prelim_ref: 2420b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 242178a6184aSMiao Xie free_delayed_ref: 242278a6184aSMiao Xie btrfs_delayed_ref_exit(); 24239247f317SMiao Xie free_auto_defrag: 24249247f317SMiao Xie btrfs_auto_defrag_exit(); 242516cdcec7SMiao Xie free_delayed_inode: 242616cdcec7SMiao Xie btrfs_delayed_inode_exit(); 24276352b91dSMiao Xie free_ordered_data: 24286352b91dSMiao Xie ordered_data_exit(); 24292f4cbe64SWyatt Banks free_extent_map: 24302f4cbe64SWyatt Banks extent_map_exit(); 24316f0d04f8SJosef Bacik free_extent_state_cache: 24326f0d04f8SJosef Bacik extent_state_cache_exit(); 2433d1310b2eSChris Mason free_extent_io: 2434d1310b2eSChris Mason extent_io_exit(); 24352f4cbe64SWyatt Banks free_cachep: 24362f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2437261507a0SLi Zefan free_compress: 2438261507a0SLi Zefan btrfs_exit_compress(); 24392f4cbe64SWyatt Banks btrfs_exit_sysfs(); 24409678c543SNikolay Borisov 24412c90e5d6SChris Mason return err; 24422e635a27SChris Mason } 24432e635a27SChris Mason 24442e635a27SChris Mason static void __exit exit_btrfs_fs(void) 24452e635a27SChris Mason { 244639279cc3SChris Mason btrfs_destroy_cachep(); 244778a6184aSMiao Xie btrfs_delayed_ref_exit(); 24489247f317SMiao Xie btrfs_auto_defrag_exit(); 244916cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2450b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 24516352b91dSMiao Xie ordered_data_exit(); 2452a52d9a80SChris Mason extent_map_exit(); 24536f0d04f8SJosef Bacik extent_state_cache_exit(); 2454d1310b2eSChris Mason extent_io_exit(); 2455a9218f6bSChris Mason btrfs_interface_exit(); 24565ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 24572e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 245858176a96SJosef Bacik btrfs_exit_sysfs(); 24598a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2460261507a0SLi Zefan btrfs_exit_compress(); 24612e635a27SChris Mason } 24622e635a27SChris Mason 246360efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 24642e635a27SChris Mason module_exit(exit_btrfs_fs) 24652e635a27SChris Mason 24662e635a27SChris Mason MODULE_LICENSE("GPL"); 2467d5178578SJohannes Thumshirn MODULE_SOFTDEP("pre: crc32c"); 24683951e7f0SJohannes Thumshirn MODULE_SOFTDEP("pre: xxhash64"); 24693831bf00SJohannes Thumshirn MODULE_SOFTDEP("pre: sha256"); 2470352ae07bSDavid Sterba MODULE_SOFTDEP("pre: blake2b-256"); 2471