super.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/super.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * super.c contains code to handle: - mount structures
  8. * - super-block tables
  9. * - filesystem drivers list
  10. * - mount system call
  11. * - umount system call
  12. * - ustat system call
  13. *
  14. * GK 2/5/95 - Changed to support mounting the root fs via NFS
  15. *
  16. * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
  17. * Added change_root: Werner Almesberger & Hans Lermen, Feb '96
  18. * Added options to /proc/mounts:
  19. * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996.
  20. * Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998
  21. * Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000
  22. */
  23. #include <linux/export.h>
  24. #include <linux/slab.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mount.h>
  27. #include <linux/security.h>
  28. #include <linux/writeback.h> /* for the emergency remount stuff */
  29. #include <linux/idr.h>
  30. #include <linux/mutex.h>
  31. #include <linux/backing-dev.h>
  32. #include <linux/rculist_bl.h>
  33. #include <linux/cleancache.h>
  34. #include <linux/fsnotify.h>
  35. #include <linux/lockdep.h>
  36. #include <linux/user_namespace.h>
  37. #include "internal.h"
  38. static LIST_HEAD(super_blocks);
  39. static DEFINE_SPINLOCK(sb_lock);
  40. static char *sb_writers_name[SB_FREEZE_LEVELS] = {
  41. "sb_writers",
  42. "sb_pagefaults",
  43. "sb_internal",
  44. };
  45. /*
  46. * One thing we have to be careful of with a per-sb shrinker is that we don't
  47. * drop the last active reference to the superblock from within the shrinker.
  48. * If that happens we could trigger unregistering the shrinker from within the
  49. * shrinker path and that leads to deadlock on the shrinker_rwsem. Hence we
  50. * take a passive reference to the superblock to avoid this from occurring.
  51. */
  52. static unsigned long super_cache_scan(struct shrinker *shrink,
  53. struct shrink_control *sc)
  54. {
  55. struct super_block *sb;
  56. long fs_objects = 0;
  57. long total_objects;
  58. long freed = 0;
  59. long dentries;
  60. long inodes;
  61. sb = container_of(shrink, struct super_block, s_shrink);
  62. /*
  63. * Deadlock avoidance. We may hold various FS locks, and we don't want
  64. * to recurse into the FS that called us in clear_inode() and friends..
  65. */
  66. if (!(sc->gfp_mask & __GFP_FS))
  67. return SHRINK_STOP;
  68. if (!trylock_super(sb))
  69. return SHRINK_STOP;
  70. if (sb->s_op->nr_cached_objects)
  71. fs_objects = sb->s_op->nr_cached_objects(sb, sc);
  72. inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
  73. dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc);
  74. total_objects = dentries + inodes + fs_objects + 1;
  75. if (!total_objects)
  76. total_objects = 1;
  77. /* proportion the scan between the caches */
  78. dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
  79. inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
  80. fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
  81. /*
  82. * prune the dcache first as the icache is pinned by it, then
  83. * prune the icache, followed by the filesystem specific caches
  84. *
  85. * Ensure that we always scan at least one object - memcg kmem
  86. * accounting uses this to fully empty the caches.
  87. */
  88. sc->nr_to_scan = dentries + 1;
  89. freed = prune_dcache_sb(sb, sc);
  90. sc->nr_to_scan = inodes + 1;
  91. freed += prune_icache_sb(sb, sc);
  92. if (fs_objects) {
  93. sc->nr_to_scan = fs_objects + 1;
  94. freed += sb->s_op->free_cached_objects(sb, sc);
  95. }
  96. up_read(&sb->s_umount);
  97. return freed;
  98. }
  99. static unsigned long super_cache_count(struct shrinker *shrink,
  100. struct shrink_control *sc)
  101. {
  102. struct super_block *sb;
  103. long total_objects = 0;
  104. sb = container_of(shrink, struct super_block, s_shrink);
  105. /*
  106. * Don't call trylock_super as it is a potential
  107. * scalability bottleneck. The counts could get updated
  108. * between super_cache_count and super_cache_scan anyway.
  109. * Call to super_cache_count with shrinker_rwsem held
  110. * ensures the safety of call to list_lru_shrink_count() and
  111. * s_op->nr_cached_objects().
  112. */
  113. if (sb->s_op && sb->s_op->nr_cached_objects)
  114. total_objects = sb->s_op->nr_cached_objects(sb, sc);
  115. total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
  116. total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
  117. total_objects = vfs_pressure_ratio(total_objects);
  118. return total_objects;
  119. }
  120. static void destroy_super_work(struct work_struct *work)
  121. {
  122. struct super_block *s = container_of(work, struct super_block,
  123. destroy_work);
  124. int i;
  125. for (i = 0; i < SB_FREEZE_LEVELS; i++)
  126. percpu_free_rwsem(&s->s_writers.rw_sem[i]);
  127. kfree(s);
  128. }
  129. static void destroy_super_rcu(struct rcu_head *head)
  130. {
  131. struct super_block *s = container_of(head, struct super_block, rcu);
  132. INIT_WORK(&s->destroy_work, destroy_super_work);
  133. schedule_work(&s->destroy_work);
  134. }
  135. /* Free a superblock that has never been seen by anyone */
  136. static void destroy_unused_super(struct super_block *s)
  137. {
  138. if (!s)
  139. return;
  140. up_write(&s->s_umount);
  141. list_lru_destroy(&s->s_dentry_lru);
  142. list_lru_destroy(&s->s_inode_lru);
  143. security_sb_free(s);
  144. put_user_ns(s->s_user_ns);
  145. kfree(s->s_subtype);
  146. /* no delays needed */
  147. destroy_super_work(&s->destroy_work);
  148. }
  149. /**
  150. * alloc_super - create new superblock
  151. * @type: filesystem type superblock should belong to
  152. * @flags: the mount flags
  153. * @user_ns: User namespace for the super_block
  154. *
  155. * Allocates and initializes a new &struct super_block. alloc_super()
  156. * returns a pointer new superblock or %NULL if allocation had failed.
  157. */
  158. static struct super_block *alloc_super(struct file_system_type *type, int flags,
  159. struct user_namespace *user_ns)
  160. {
  161. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
  162. static const struct super_operations default_op;
  163. int i;
  164. if (!s)
  165. return NULL;
  166. INIT_LIST_HEAD(&s->s_mounts);
  167. s->s_user_ns = get_user_ns(user_ns);
  168. init_rwsem(&s->s_umount);
  169. lockdep_set_class(&s->s_umount, &type->s_umount_key);
  170. /*
  171. * sget() can have s_umount recursion.
  172. *
  173. * When it cannot find a suitable sb, it allocates a new
  174. * one (this one), and tries again to find a suitable old
  175. * one.
  176. *
  177. * In case that succeeds, it will acquire the s_umount
  178. * lock of the old one. Since these are clearly distrinct
  179. * locks, and this object isn't exposed yet, there's no
  180. * risk of deadlocks.
  181. *
  182. * Annotate this by putting this lock in a different
  183. * subclass.
  184. */
  185. down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
  186. if (security_sb_alloc(s))
  187. goto fail;
  188. for (i = 0; i < SB_FREEZE_LEVELS; i++) {
  189. if (__percpu_init_rwsem(&s->s_writers.rw_sem[i],
  190. sb_writers_name[i],
  191. &type->s_writers_key[i]))
  192. goto fail;
  193. }
  194. init_waitqueue_head(&s->s_writers.wait_unfrozen);
  195. s->s_bdi = &noop_backing_dev_info;
  196. s->s_flags = flags;
  197. if (s->s_user_ns != &init_user_ns)
  198. s->s_iflags |= SB_I_NODEV;
  199. INIT_HLIST_NODE(&s->s_instances);
  200. INIT_HLIST_BL_HEAD(&s->s_roots);
  201. mutex_init(&s->s_sync_lock);
  202. INIT_LIST_HEAD(&s->s_inodes);
  203. spin_lock_init(&s->s_inode_list_lock);
  204. INIT_LIST_HEAD(&s->s_inodes_wb);
  205. spin_lock_init(&s->s_inode_wblist_lock);
  206. if (list_lru_init_memcg(&s->s_dentry_lru))
  207. goto fail;
  208. if (list_lru_init_memcg(&s->s_inode_lru))
  209. goto fail;
  210. s->s_count = 1;
  211. atomic_set(&s->s_active, 1);
  212. mutex_init(&s->s_vfs_rename_mutex);
  213. lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
  214. init_rwsem(&s->s_dquot.dqio_sem);
  215. s->s_maxbytes = MAX_NON_LFS;
  216. s->s_op = &default_op;
  217. s->s_time_gran = 1000000000;
  218. s->cleancache_poolid = CLEANCACHE_NO_POOL;
  219. s->s_shrink.seeks = DEFAULT_SEEKS;
  220. s->s_shrink.scan_objects = super_cache_scan;
  221. s->s_shrink.count_objects = super_cache_count;
  222. s->s_shrink.batch = 1024;
  223. s->s_shrink.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE;
  224. return s;
  225. fail:
  226. destroy_unused_super(s);
  227. return NULL;
  228. }
  229. /* Superblock refcounting */
  230. /*
  231. * Drop a superblock's refcount. The caller must hold sb_lock.
  232. */
  233. static void __put_super(struct super_block *s)
  234. {
  235. if (!--s->s_count) {
  236. list_del_init(&s->s_list);
  237. WARN_ON(s->s_dentry_lru.node);
  238. WARN_ON(s->s_inode_lru.node);
  239. WARN_ON(!list_empty(&s->s_mounts));
  240. security_sb_free(s);
  241. put_user_ns(s->s_user_ns);
  242. kfree(s->s_subtype);
  243. call_rcu(&s->rcu, destroy_super_rcu);
  244. }
  245. }
  246. /**
  247. * put_super - drop a temporary reference to superblock
  248. * @sb: superblock in question
  249. *
  250. * Drops a temporary reference, frees superblock if there's no
  251. * references left.
  252. */
  253. static void put_super(struct super_block *sb)
  254. {
  255. spin_lock(&sb_lock);
  256. __put_super(sb);
  257. spin_unlock(&sb_lock);
  258. }
  259. /**
  260. * deactivate_locked_super - drop an active reference to superblock
  261. * @s: superblock to deactivate
  262. *
  263. * Drops an active reference to superblock, converting it into a temporary
  264. * one if there is no other active references left. In that case we
  265. * tell fs driver to shut it down and drop the temporary reference we
  266. * had just acquired.
  267. *
  268. * Caller holds exclusive lock on superblock; that lock is released.
  269. */
  270. void deactivate_locked_super(struct super_block *s)
  271. {
  272. struct file_system_type *fs = s->s_type;
  273. if (atomic_dec_and_test(&s->s_active)) {
  274. cleancache_invalidate_fs(s);
  275. unregister_shrinker(&s->s_shrink);
  276. fs->kill_sb(s);
  277. /*
  278. * Since list_lru_destroy() may sleep, we cannot call it from
  279. * put_super(), where we hold the sb_lock. Therefore we destroy
  280. * the lru lists right now.
  281. */
  282. list_lru_destroy(&s->s_dentry_lru);
  283. list_lru_destroy(&s->s_inode_lru);
  284. put_filesystem(fs);
  285. put_super(s);
  286. } else {
  287. up_write(&s->s_umount);
  288. }
  289. }
  290. EXPORT_SYMBOL(deactivate_locked_super);
  291. /**
  292. * deactivate_super - drop an active reference to superblock
  293. * @s: superblock to deactivate
  294. *
  295. * Variant of deactivate_locked_super(), except that superblock is *not*
  296. * locked by caller. If we are going to drop the final active reference,
  297. * lock will be acquired prior to that.
  298. */
  299. void deactivate_super(struct super_block *s)
  300. {
  301. if (!atomic_add_unless(&s->s_active, -1, 1)) {
  302. down_write(&s->s_umount);
  303. deactivate_locked_super(s);
  304. }
  305. }
  306. EXPORT_SYMBOL(deactivate_super);
  307. /**
  308. * grab_super - acquire an active reference
  309. * @s: reference we are trying to make active
  310. *
  311. * Tries to acquire an active reference. grab_super() is used when we
  312. * had just found a superblock in super_blocks or fs_type->fs_supers
  313. * and want to turn it into a full-blown active reference. grab_super()
  314. * is called with sb_lock held and drops it. Returns 1 in case of
  315. * success, 0 if we had failed (superblock contents was already dead or
  316. * dying when grab_super() had been called). Note that this is only
  317. * called for superblocks not in rundown mode (== ones still on ->fs_supers
  318. * of their type), so increment of ->s_count is OK here.
  319. */
  320. static int grab_super(struct super_block *s) __releases(sb_lock)
  321. {
  322. s->s_count++;
  323. spin_unlock(&sb_lock);
  324. down_write(&s->s_umount);
  325. if ((s->s_flags & SB_BORN) && atomic_inc_not_zero(&s->s_active)) {
  326. put_super(s);
  327. return 1;
  328. }
  329. up_write(&s->s_umount);
  330. put_super(s);
  331. return 0;
  332. }
  333. /*
  334. * trylock_super - try to grab ->s_umount shared
  335. * @sb: reference we are trying to grab
  336. *
  337. * Try to prevent fs shutdown. This is used in places where we
  338. * cannot take an active reference but we need to ensure that the
  339. * filesystem is not shut down while we are working on it. It returns
  340. * false if we cannot acquire s_umount or if we lose the race and
  341. * filesystem already got into shutdown, and returns true with the s_umount
  342. * lock held in read mode in case of success. On successful return,
  343. * the caller must drop the s_umount lock when done.
  344. *
  345. * Note that unlike get_super() et.al. this one does *not* bump ->s_count.
  346. * The reason why it's safe is that we are OK with doing trylock instead
  347. * of down_read(). There's a couple of places that are OK with that, but
  348. * it's very much not a general-purpose interface.
  349. */
  350. bool trylock_super(struct super_block *sb)
  351. {
  352. if (down_read_trylock(&sb->s_umount)) {
  353. if (!hlist_unhashed(&sb->s_instances) &&
  354. sb->s_root && (sb->s_flags & SB_BORN))
  355. return true;
  356. up_read(&sb->s_umount);
  357. }
  358. return false;
  359. }
  360. /**
  361. * generic_shutdown_super - common helper for ->kill_sb()
  362. * @sb: superblock to kill
  363. *
  364. * generic_shutdown_super() does all fs-independent work on superblock
  365. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  366. * that need destruction out of superblock, call generic_shutdown_super()
  367. * and release aforementioned objects. Note: dentries and inodes _are_
  368. * taken care of and do not need specific handling.
  369. *
  370. * Upon calling this function, the filesystem may no longer alter or
  371. * rearrange the set of dentries belonging to this super_block, nor may it
  372. * change the attachments of dentries to inodes.
  373. */
  374. void generic_shutdown_super(struct super_block *sb)
  375. {
  376. const struct super_operations *sop = sb->s_op;
  377. if (sb->s_root) {
  378. shrink_dcache_for_umount(sb);
  379. sync_filesystem(sb);
  380. sb->s_flags &= ~SB_ACTIVE;
  381. fsnotify_unmount_inodes(sb);
  382. cgroup_writeback_umount();
  383. evict_inodes(sb);
  384. if (sb->s_dio_done_wq) {
  385. destroy_workqueue(sb->s_dio_done_wq);
  386. sb->s_dio_done_wq = NULL;
  387. }
  388. if (sop->put_super)
  389. sop->put_super(sb);
  390. if (!list_empty(&sb->s_inodes)) {
  391. printk("VFS: Busy inodes after unmount of %s. "
  392. "Self-destruct in 5 seconds. Have a nice day...\n",
  393. sb->s_id);
  394. }
  395. }
  396. spin_lock(&sb_lock);
  397. /* should be initialized for __put_super_and_need_restart() */
  398. hlist_del_init(&sb->s_instances);
  399. spin_unlock(&sb_lock);
  400. up_write(&sb->s_umount);
  401. if (sb->s_bdi != &noop_backing_dev_info) {
  402. bdi_put(sb->s_bdi);
  403. sb->s_bdi = &noop_backing_dev_info;
  404. }
  405. }
  406. EXPORT_SYMBOL(generic_shutdown_super);
  407. /**
  408. * sget_userns - find or create a superblock
  409. * @type: filesystem type superblock should belong to
  410. * @test: comparison callback
  411. * @set: setup callback
  412. * @flags: mount flags
  413. * @user_ns: User namespace for the super_block
  414. * @data: argument to each of them
  415. */
  416. struct super_block *sget_userns(struct file_system_type *type,
  417. int (*test)(struct super_block *,void *),
  418. int (*set)(struct super_block *,void *),
  419. int flags, struct user_namespace *user_ns,
  420. void *data)
  421. {
  422. struct super_block *s = NULL;
  423. struct super_block *old;
  424. int err;
  425. if (!(flags & (SB_KERNMOUNT|SB_SUBMOUNT)) &&
  426. !(type->fs_flags & FS_USERNS_MOUNT) &&
  427. !capable(CAP_SYS_ADMIN))
  428. return ERR_PTR(-EPERM);
  429. retry:
  430. spin_lock(&sb_lock);
  431. if (test) {
  432. hlist_for_each_entry(old, &type->fs_supers, s_instances) {
  433. if (!test(old, data))
  434. continue;
  435. if (user_ns != old->s_user_ns) {
  436. spin_unlock(&sb_lock);
  437. destroy_unused_super(s);
  438. return ERR_PTR(-EBUSY);
  439. }
  440. if (!grab_super(old))
  441. goto retry;
  442. destroy_unused_super(s);
  443. return old;
  444. }
  445. }
  446. if (!s) {
  447. spin_unlock(&sb_lock);
  448. s = alloc_super(type, (flags & ~SB_SUBMOUNT), user_ns);
  449. if (!s)
  450. return ERR_PTR(-ENOMEM);
  451. goto retry;
  452. }
  453. err = set(s, data);
  454. if (err) {
  455. spin_unlock(&sb_lock);
  456. destroy_unused_super(s);
  457. return ERR_PTR(err);
  458. }
  459. s->s_type = type;
  460. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  461. list_add_tail(&s->s_list, &super_blocks);
  462. hlist_add_head(&s->s_instances, &type->fs_supers);
  463. spin_unlock(&sb_lock);
  464. get_filesystem(type);
  465. err = register_shrinker(&s->s_shrink);
  466. if (err) {
  467. deactivate_locked_super(s);
  468. s = ERR_PTR(err);
  469. }
  470. return s;
  471. }
  472. EXPORT_SYMBOL(sget_userns);
  473. /**
  474. * sget - find or create a superblock
  475. * @type: filesystem type superblock should belong to
  476. * @test: comparison callback
  477. * @set: setup callback
  478. * @flags: mount flags
  479. * @data: argument to each of them
  480. */
  481. struct super_block *sget(struct file_system_type *type,
  482. int (*test)(struct super_block *,void *),
  483. int (*set)(struct super_block *,void *),
  484. int flags,
  485. void *data)
  486. {
  487. struct user_namespace *user_ns = current_user_ns();
  488. /* We don't yet pass the user namespace of the parent
  489. * mount through to here so always use &init_user_ns
  490. * until that changes.
  491. */
  492. if (flags & SB_SUBMOUNT)
  493. user_ns = &init_user_ns;
  494. /* Ensure the requestor has permissions over the target filesystem */
  495. if (!(flags & (SB_KERNMOUNT|SB_SUBMOUNT)) && !ns_capable(user_ns, CAP_SYS_ADMIN))
  496. return ERR_PTR(-EPERM);
  497. return sget_userns(type, test, set, flags, user_ns, data);
  498. }
  499. EXPORT_SYMBOL(sget);
  500. void drop_super(struct super_block *sb)
  501. {
  502. up_read(&sb->s_umount);
  503. put_super(sb);
  504. }
  505. EXPORT_SYMBOL(drop_super);
  506. void drop_super_exclusive(struct super_block *sb)
  507. {
  508. up_write(&sb->s_umount);
  509. put_super(sb);
  510. }
  511. EXPORT_SYMBOL(drop_super_exclusive);
  512. /**
  513. * iterate_supers - call function for all active superblocks
  514. * @f: function to call
  515. * @arg: argument to pass to it
  516. *
  517. * Scans the superblock list and calls given function, passing it
  518. * locked superblock and given argument.
  519. */
  520. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  521. {
  522. struct super_block *sb, *p = NULL;
  523. spin_lock(&sb_lock);
  524. list_for_each_entry(sb, &super_blocks, s_list) {
  525. if (hlist_unhashed(&sb->s_instances))
  526. continue;
  527. sb->s_count++;
  528. spin_unlock(&sb_lock);
  529. down_read(&sb->s_umount);
  530. if (sb->s_root && (sb->s_flags & SB_BORN))
  531. f(sb, arg);
  532. up_read(&sb->s_umount);
  533. spin_lock(&sb_lock);
  534. if (p)
  535. __put_super(p);
  536. p = sb;
  537. }
  538. if (p)
  539. __put_super(p);
  540. spin_unlock(&sb_lock);
  541. }
  542. /**
  543. * iterate_supers_type - call function for superblocks of given type
  544. * @type: fs type
  545. * @f: function to call
  546. * @arg: argument to pass to it
  547. *
  548. * Scans the superblock list and calls given function, passing it
  549. * locked superblock and given argument.
  550. */
  551. void iterate_supers_type(struct file_system_type *type,
  552. void (*f)(struct super_block *, void *), void *arg)
  553. {
  554. struct super_block *sb, *p = NULL;
  555. spin_lock(&sb_lock);
  556. hlist_for_each_entry(sb, &type->fs_supers, s_instances) {
  557. sb->s_count++;
  558. spin_unlock(&sb_lock);
  559. down_read(&sb->s_umount);
  560. if (sb->s_root && (sb->s_flags & SB_BORN))
  561. f(sb, arg);
  562. up_read(&sb->s_umount);
  563. spin_lock(&sb_lock);
  564. if (p)
  565. __put_super(p);
  566. p = sb;
  567. }
  568. if (p)
  569. __put_super(p);
  570. spin_unlock(&sb_lock);
  571. }
  572. EXPORT_SYMBOL(iterate_supers_type);
  573. static struct super_block *__get_super(struct block_device *bdev, bool excl)
  574. {
  575. struct super_block *sb;
  576. if (!bdev)
  577. return NULL;
  578. spin_lock(&sb_lock);
  579. rescan:
  580. list_for_each_entry(sb, &super_blocks, s_list) {
  581. if (hlist_unhashed(&sb->s_instances))
  582. continue;
  583. if (sb->s_bdev == bdev) {
  584. sb->s_count++;
  585. spin_unlock(&sb_lock);
  586. if (!excl)
  587. down_read(&sb->s_umount);
  588. else
  589. down_write(&sb->s_umount);
  590. /* still alive? */
  591. if (sb->s_root && (sb->s_flags & SB_BORN))
  592. return sb;
  593. if (!excl)
  594. up_read(&sb->s_umount);
  595. else
  596. up_write(&sb->s_umount);
  597. /* nope, got unmounted */
  598. spin_lock(&sb_lock);
  599. __put_super(sb);
  600. goto rescan;
  601. }
  602. }
  603. spin_unlock(&sb_lock);
  604. return NULL;
  605. }
  606. /**
  607. * get_super - get the superblock of a device
  608. * @bdev: device to get the superblock for
  609. *
  610. * Scans the superblock list and finds the superblock of the file system
  611. * mounted on the device given. %NULL is returned if no match is found.
  612. */
  613. struct super_block *get_super(struct block_device *bdev)
  614. {
  615. return __get_super(bdev, false);
  616. }
  617. EXPORT_SYMBOL(get_super);
  618. static struct super_block *__get_super_thawed(struct block_device *bdev,
  619. bool excl)
  620. {
  621. while (1) {
  622. struct super_block *s = __get_super(bdev, excl);
  623. if (!s || s->s_writers.frozen == SB_UNFROZEN)
  624. return s;
  625. if (!excl)
  626. up_read(&s->s_umount);
  627. else
  628. up_write(&s->s_umount);
  629. wait_event(s->s_writers.wait_unfrozen,
  630. s->s_writers.frozen == SB_UNFROZEN);
  631. put_super(s);
  632. }
  633. }
  634. /**
  635. * get_super_thawed - get thawed superblock of a device
  636. * @bdev: device to get the superblock for
  637. *
  638. * Scans the superblock list and finds the superblock of the file system
  639. * mounted on the device. The superblock is returned once it is thawed
  640. * (or immediately if it was not frozen). %NULL is returned if no match
  641. * is found.
  642. */
  643. struct super_block *get_super_thawed(struct block_device *bdev)
  644. {
  645. return __get_super_thawed(bdev, false);
  646. }
  647. EXPORT_SYMBOL(get_super_thawed);
  648. /**
  649. * get_super_exclusive_thawed - get thawed superblock of a device
  650. * @bdev: device to get the superblock for
  651. *
  652. * Scans the superblock list and finds the superblock of the file system
  653. * mounted on the device. The superblock is returned once it is thawed
  654. * (or immediately if it was not frozen) and s_umount semaphore is held
  655. * in exclusive mode. %NULL is returned if no match is found.
  656. */
  657. struct super_block *get_super_exclusive_thawed(struct block_device *bdev)
  658. {
  659. return __get_super_thawed(bdev, true);
  660. }
  661. EXPORT_SYMBOL(get_super_exclusive_thawed);
  662. /**
  663. * get_active_super - get an active reference to the superblock of a device
  664. * @bdev: device to get the superblock for
  665. *
  666. * Scans the superblock list and finds the superblock of the file system
  667. * mounted on the device given. Returns the superblock with an active
  668. * reference or %NULL if none was found.
  669. */
  670. struct super_block *get_active_super(struct block_device *bdev)
  671. {
  672. struct super_block *sb;
  673. if (!bdev)
  674. return NULL;
  675. restart:
  676. spin_lock(&sb_lock);
  677. list_for_each_entry(sb, &super_blocks, s_list) {
  678. if (hlist_unhashed(&sb->s_instances))
  679. continue;
  680. if (sb->s_bdev == bdev) {
  681. if (!grab_super(sb))
  682. goto restart;
  683. up_write(&sb->s_umount);
  684. return sb;
  685. }
  686. }
  687. spin_unlock(&sb_lock);
  688. return NULL;
  689. }
  690. struct super_block *user_get_super(dev_t dev)
  691. {
  692. struct super_block *sb;
  693. spin_lock(&sb_lock);
  694. rescan:
  695. list_for_each_entry(sb, &super_blocks, s_list) {
  696. if (hlist_unhashed(&sb->s_instances))
  697. continue;
  698. if (sb->s_dev == dev) {
  699. sb->s_count++;
  700. spin_unlock(&sb_lock);
  701. down_read(&sb->s_umount);
  702. /* still alive? */
  703. if (sb->s_root && (sb->s_flags & SB_BORN))
  704. return sb;
  705. up_read(&sb->s_umount);
  706. /* nope, got unmounted */
  707. spin_lock(&sb_lock);
  708. __put_super(sb);
  709. goto rescan;
  710. }
  711. }
  712. spin_unlock(&sb_lock);
  713. return NULL;
  714. }
  715. /**
  716. * do_remount_sb - asks filesystem to change mount options.
  717. * @sb: superblock in question
  718. * @sb_flags: revised superblock flags
  719. * @data: the rest of options
  720. * @force: whether or not to force the change
  721. *
  722. * Alters the mount options of a mounted file system.
  723. */
  724. int do_remount_sb(struct super_block *sb, int sb_flags, void *data, int force)
  725. {
  726. int retval;
  727. int remount_ro;
  728. if (sb->s_writers.frozen != SB_UNFROZEN)
  729. return -EBUSY;
  730. #ifdef CONFIG_BLOCK
  731. if (!(sb_flags & SB_RDONLY) && bdev_read_only(sb->s_bdev))
  732. return -EACCES;
  733. #endif
  734. remount_ro = (sb_flags & SB_RDONLY) && !sb_rdonly(sb);
  735. if (remount_ro) {
  736. if (!hlist_empty(&sb->s_pins)) {
  737. up_write(&sb->s_umount);
  738. group_pin_kill(&sb->s_pins);
  739. down_write(&sb->s_umount);
  740. if (!sb->s_root)
  741. return 0;
  742. if (sb->s_writers.frozen != SB_UNFROZEN)
  743. return -EBUSY;
  744. remount_ro = (sb_flags & SB_RDONLY) && !sb_rdonly(sb);
  745. }
  746. }
  747. shrink_dcache_sb(sb);
  748. /* If we are remounting RDONLY and current sb is read/write,
  749. make sure there are no rw files opened */
  750. if (remount_ro) {
  751. if (force) {
  752. sb->s_readonly_remount = 1;
  753. smp_wmb();
  754. } else {
  755. retval = sb_prepare_remount_readonly(sb);
  756. if (retval)
  757. return retval;
  758. }
  759. }
  760. if (sb->s_op->remount_fs) {
  761. retval = sb->s_op->remount_fs(sb, &sb_flags, data);
  762. if (retval) {
  763. if (!force)
  764. goto cancel_readonly;
  765. /* If forced remount, go ahead despite any errors */
  766. WARN(1, "forced remount of a %s fs returned %i\n",
  767. sb->s_type->name, retval);
  768. }
  769. }
  770. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (sb_flags & MS_RMT_MASK);
  771. /* Needs to be ordered wrt mnt_is_readonly() */
  772. smp_wmb();
  773. sb->s_readonly_remount = 0;
  774. /*
  775. * Some filesystems modify their metadata via some other path than the
  776. * bdev buffer cache (eg. use a private mapping, or directories in
  777. * pagecache, etc). Also file data modifications go via their own
  778. * mappings. So If we try to mount readonly then copy the filesystem
  779. * from bdev, we could get stale data, so invalidate it to give a best
  780. * effort at coherency.
  781. */
  782. if (remount_ro && sb->s_bdev)
  783. invalidate_bdev(sb->s_bdev);
  784. return 0;
  785. cancel_readonly:
  786. sb->s_readonly_remount = 0;
  787. return retval;
  788. }
  789. static void do_emergency_remount(struct work_struct *work)
  790. {
  791. struct super_block *sb, *p = NULL;
  792. spin_lock(&sb_lock);
  793. list_for_each_entry(sb, &super_blocks, s_list) {
  794. if (hlist_unhashed(&sb->s_instances))
  795. continue;
  796. sb->s_count++;
  797. spin_unlock(&sb_lock);
  798. down_write(&sb->s_umount);
  799. if (sb->s_root && sb->s_bdev && (sb->s_flags & SB_BORN) &&
  800. !sb_rdonly(sb)) {
  801. /*
  802. * What lock protects sb->s_flags??
  803. */
  804. do_remount_sb(sb, SB_RDONLY, NULL, 1);
  805. }
  806. up_write(&sb->s_umount);
  807. spin_lock(&sb_lock);
  808. if (p)
  809. __put_super(p);
  810. p = sb;
  811. }
  812. if (p)
  813. __put_super(p);
  814. spin_unlock(&sb_lock);
  815. kfree(work);
  816. printk("Emergency Remount complete\n");
  817. }
  818. void emergency_remount(void)
  819. {
  820. struct work_struct *work;
  821. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  822. if (work) {
  823. INIT_WORK(work, do_emergency_remount);
  824. schedule_work(work);
  825. }
  826. }
  827. /*
  828. * Unnamed block devices are dummy devices used by virtual
  829. * filesystems which don't use real block-devices. -- jrs
  830. */
  831. static DEFINE_IDA(unnamed_dev_ida);
  832. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  833. /* Many userspace utilities consider an FSID of 0 invalid.
  834. * Always return at least 1 from get_anon_bdev.
  835. */
  836. static int unnamed_dev_start = 1;
  837. int get_anon_bdev(dev_t *p)
  838. {
  839. int dev;
  840. int error;
  841. retry:
  842. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  843. return -ENOMEM;
  844. spin_lock(&unnamed_dev_lock);
  845. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  846. if (!error)
  847. unnamed_dev_start = dev + 1;
  848. spin_unlock(&unnamed_dev_lock);
  849. if (error == -EAGAIN)
  850. /* We raced and lost with another CPU. */
  851. goto retry;
  852. else if (error)
  853. return -EAGAIN;
  854. if (dev >= (1 << MINORBITS)) {
  855. spin_lock(&unnamed_dev_lock);
  856. ida_remove(&unnamed_dev_ida, dev);
  857. if (unnamed_dev_start > dev)
  858. unnamed_dev_start = dev;
  859. spin_unlock(&unnamed_dev_lock);
  860. return -EMFILE;
  861. }
  862. *p = MKDEV(0, dev & MINORMASK);
  863. return 0;
  864. }
  865. EXPORT_SYMBOL(get_anon_bdev);
  866. void free_anon_bdev(dev_t dev)
  867. {
  868. int slot = MINOR(dev);
  869. spin_lock(&unnamed_dev_lock);
  870. ida_remove(&unnamed_dev_ida, slot);
  871. if (slot < unnamed_dev_start)
  872. unnamed_dev_start = slot;
  873. spin_unlock(&unnamed_dev_lock);
  874. }
  875. EXPORT_SYMBOL(free_anon_bdev);
  876. int set_anon_super(struct super_block *s, void *data)
  877. {
  878. return get_anon_bdev(&s->s_dev);
  879. }
  880. EXPORT_SYMBOL(set_anon_super);
  881. void kill_anon_super(struct super_block *sb)
  882. {
  883. dev_t dev = sb->s_dev;
  884. generic_shutdown_super(sb);
  885. free_anon_bdev(dev);
  886. }
  887. EXPORT_SYMBOL(kill_anon_super);
  888. void kill_litter_super(struct super_block *sb)
  889. {
  890. if (sb->s_root)
  891. d_genocide(sb->s_root);
  892. kill_anon_super(sb);
  893. }
  894. EXPORT_SYMBOL(kill_litter_super);
  895. static int ns_test_super(struct super_block *sb, void *data)
  896. {
  897. return sb->s_fs_info == data;
  898. }
  899. static int ns_set_super(struct super_block *sb, void *data)
  900. {
  901. sb->s_fs_info = data;
  902. return set_anon_super(sb, NULL);
  903. }
  904. struct dentry *mount_ns(struct file_system_type *fs_type,
  905. int flags, void *data, void *ns, struct user_namespace *user_ns,
  906. int (*fill_super)(struct super_block *, void *, int))
  907. {
  908. struct super_block *sb;
  909. /* Don't allow mounting unless the caller has CAP_SYS_ADMIN
  910. * over the namespace.
  911. */
  912. if (!(flags & SB_KERNMOUNT) && !ns_capable(user_ns, CAP_SYS_ADMIN))
  913. return ERR_PTR(-EPERM);
  914. sb = sget_userns(fs_type, ns_test_super, ns_set_super, flags,
  915. user_ns, ns);
  916. if (IS_ERR(sb))
  917. return ERR_CAST(sb);
  918. if (!sb->s_root) {
  919. int err;
  920. err = fill_super(sb, data, flags & SB_SILENT ? 1 : 0);
  921. if (err) {
  922. deactivate_locked_super(sb);
  923. return ERR_PTR(err);
  924. }
  925. sb->s_flags |= SB_ACTIVE;
  926. }
  927. return dget(sb->s_root);
  928. }
  929. EXPORT_SYMBOL(mount_ns);
  930. #ifdef CONFIG_BLOCK
  931. static int set_bdev_super(struct super_block *s, void *data)
  932. {
  933. s->s_bdev = data;
  934. s->s_dev = s->s_bdev->bd_dev;
  935. s->s_bdi = bdi_get(s->s_bdev->bd_bdi);
  936. return 0;
  937. }
  938. static int test_bdev_super(struct super_block *s, void *data)
  939. {
  940. return (void *)s->s_bdev == data;
  941. }
  942. struct dentry *mount_bdev(struct file_system_type *fs_type,
  943. int flags, const char *dev_name, void *data,
  944. int (*fill_super)(struct super_block *, void *, int))
  945. {
  946. struct block_device *bdev;
  947. struct super_block *s;
  948. fmode_t mode = FMODE_READ | FMODE_EXCL;
  949. int error = 0;
  950. if (!(flags & SB_RDONLY))
  951. mode |= FMODE_WRITE;
  952. bdev = blkdev_get_by_path(dev_name, mode, fs_type);
  953. if (IS_ERR(bdev))
  954. return ERR_CAST(bdev);
  955. /*
  956. * once the super is inserted into the list by sget, s_umount
  957. * will protect the lockfs code from trying to start a snapshot
  958. * while we are mounting
  959. */
  960. mutex_lock(&bdev->bd_fsfreeze_mutex);
  961. if (bdev->bd_fsfreeze_count > 0) {
  962. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  963. error = -EBUSY;
  964. goto error_bdev;
  965. }
  966. s = sget(fs_type, test_bdev_super, set_bdev_super, flags | SB_NOSEC,
  967. bdev);
  968. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  969. if (IS_ERR(s))
  970. goto error_s;
  971. if (s->s_root) {
  972. if ((flags ^ s->s_flags) & SB_RDONLY) {
  973. deactivate_locked_super(s);
  974. error = -EBUSY;
  975. goto error_bdev;
  976. }
  977. /*
  978. * s_umount nests inside bd_mutex during
  979. * __invalidate_device(). blkdev_put() acquires
  980. * bd_mutex and can't be called under s_umount. Drop
  981. * s_umount temporarily. This is safe as we're
  982. * holding an active reference.
  983. */
  984. up_write(&s->s_umount);
  985. blkdev_put(bdev, mode);
  986. down_write(&s->s_umount);
  987. } else {
  988. s->s_mode = mode;
  989. snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
  990. sb_set_blocksize(s, block_size(bdev));
  991. error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
  992. if (error) {
  993. deactivate_locked_super(s);
  994. goto error;
  995. }
  996. s->s_flags |= SB_ACTIVE;
  997. bdev->bd_super = s;
  998. }
  999. return dget(s->s_root);
  1000. error_s:
  1001. error = PTR_ERR(s);
  1002. error_bdev:
  1003. blkdev_put(bdev, mode);
  1004. error:
  1005. return ERR_PTR(error);
  1006. }
  1007. EXPORT_SYMBOL(mount_bdev);
  1008. void kill_block_super(struct super_block *sb)
  1009. {
  1010. struct block_device *bdev = sb->s_bdev;
  1011. fmode_t mode = sb->s_mode;
  1012. bdev->bd_super = NULL;
  1013. generic_shutdown_super(sb);
  1014. sync_blockdev(bdev);
  1015. WARN_ON_ONCE(!(mode & FMODE_EXCL));
  1016. blkdev_put(bdev, mode | FMODE_EXCL);
  1017. }
  1018. EXPORT_SYMBOL(kill_block_super);
  1019. #endif
  1020. struct dentry *mount_nodev(struct file_system_type *fs_type,
  1021. int flags, void *data,
  1022. int (*fill_super)(struct super_block *, void *, int))
  1023. {
  1024. int error;
  1025. struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  1026. if (IS_ERR(s))
  1027. return ERR_CAST(s);
  1028. error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
  1029. if (error) {
  1030. deactivate_locked_super(s);
  1031. return ERR_PTR(error);
  1032. }
  1033. s->s_flags |= SB_ACTIVE;
  1034. return dget(s->s_root);
  1035. }
  1036. EXPORT_SYMBOL(mount_nodev);
  1037. static int compare_single(struct super_block *s, void *p)
  1038. {
  1039. return 1;
  1040. }
  1041. struct dentry *mount_single(struct file_system_type *fs_type,
  1042. int flags, void *data,
  1043. int (*fill_super)(struct super_block *, void *, int))
  1044. {
  1045. struct super_block *s;
  1046. int error;
  1047. s = sget(fs_type, compare_single, set_anon_super, flags, NULL);
  1048. if (IS_ERR(s))
  1049. return ERR_CAST(s);
  1050. if (!s->s_root) {
  1051. error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
  1052. if (error) {
  1053. deactivate_locked_super(s);
  1054. return ERR_PTR(error);
  1055. }
  1056. s->s_flags |= SB_ACTIVE;
  1057. } else {
  1058. do_remount_sb(s, flags, data, 0);
  1059. }
  1060. return dget(s->s_root);
  1061. }
  1062. EXPORT_SYMBOL(mount_single);
  1063. struct dentry *
  1064. mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
  1065. {
  1066. struct dentry *root;
  1067. struct super_block *sb;
  1068. char *secdata = NULL;
  1069. int error = -ENOMEM;
  1070. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  1071. secdata = alloc_secdata();
  1072. if (!secdata)
  1073. goto out;
  1074. error = security_sb_copy_data(data, secdata);
  1075. if (error)
  1076. goto out_free_secdata;
  1077. }
  1078. root = type->mount(type, flags, name, data);
  1079. if (IS_ERR(root)) {
  1080. error = PTR_ERR(root);
  1081. goto out_free_secdata;
  1082. }
  1083. sb = root->d_sb;
  1084. BUG_ON(!sb);
  1085. WARN_ON(!sb->s_bdi);
  1086. sb->s_flags |= SB_BORN;
  1087. error = security_sb_kern_mount(sb, flags, secdata);
  1088. if (error)
  1089. goto out_sb;
  1090. /*
  1091. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  1092. * but s_maxbytes was an unsigned long long for many releases. Throw
  1093. * this warning for a little while to try and catch filesystems that
  1094. * violate this rule.
  1095. */
  1096. WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  1097. "negative value (%lld)\n", type->name, sb->s_maxbytes);
  1098. up_write(&sb->s_umount);
  1099. free_secdata(secdata);
  1100. return root;
  1101. out_sb:
  1102. dput(root);
  1103. deactivate_locked_super(sb);
  1104. out_free_secdata:
  1105. free_secdata(secdata);
  1106. out:
  1107. return ERR_PTR(error);
  1108. }
  1109. /*
  1110. * Setup private BDI for given superblock. It gets automatically cleaned up
  1111. * in generic_shutdown_super().
  1112. */
  1113. int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
  1114. {
  1115. struct backing_dev_info *bdi;
  1116. int err;
  1117. va_list args;
  1118. bdi = bdi_alloc(GFP_KERNEL);
  1119. if (!bdi)
  1120. return -ENOMEM;
  1121. bdi->name = sb->s_type->name;
  1122. va_start(args, fmt);
  1123. err = bdi_register_va(bdi, fmt, args);
  1124. va_end(args);
  1125. if (err) {
  1126. bdi_put(bdi);
  1127. return err;
  1128. }
  1129. WARN_ON(sb->s_bdi != &noop_backing_dev_info);
  1130. sb->s_bdi = bdi;
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL(super_setup_bdi_name);
  1134. /*
  1135. * Setup private BDI for given superblock. I gets automatically cleaned up
  1136. * in generic_shutdown_super().
  1137. */
  1138. int super_setup_bdi(struct super_block *sb)
  1139. {
  1140. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  1141. return super_setup_bdi_name(sb, "%.28s-%ld", sb->s_type->name,
  1142. atomic_long_inc_return(&bdi_seq));
  1143. }
  1144. EXPORT_SYMBOL(super_setup_bdi);
  1145. /*
  1146. * This is an internal function, please use sb_end_{write,pagefault,intwrite}
  1147. * instead.
  1148. */
  1149. void __sb_end_write(struct super_block *sb, int level)
  1150. {
  1151. percpu_up_read(sb->s_writers.rw_sem + level-1);
  1152. }
  1153. EXPORT_SYMBOL(__sb_end_write);
  1154. /*
  1155. * This is an internal function, please use sb_start_{write,pagefault,intwrite}
  1156. * instead.
  1157. */
  1158. int __sb_start_write(struct super_block *sb, int level, bool wait)
  1159. {
  1160. bool force_trylock = false;
  1161. int ret = 1;
  1162. #ifdef CONFIG_LOCKDEP
  1163. /*
  1164. * We want lockdep to tell us about possible deadlocks with freezing
  1165. * but it's it bit tricky to properly instrument it. Getting a freeze
  1166. * protection works as getting a read lock but there are subtle
  1167. * problems. XFS for example gets freeze protection on internal level
  1168. * twice in some cases, which is OK only because we already hold a
  1169. * freeze protection also on higher level. Due to these cases we have
  1170. * to use wait == F (trylock mode) which must not fail.
  1171. */
  1172. if (wait) {
  1173. int i;
  1174. for (i = 0; i < level - 1; i++)
  1175. if (percpu_rwsem_is_held(sb->s_writers.rw_sem + i)) {
  1176. force_trylock = true;
  1177. break;
  1178. }
  1179. }
  1180. #endif
  1181. if (wait && !force_trylock)
  1182. percpu_down_read(sb->s_writers.rw_sem + level-1);
  1183. else
  1184. ret = percpu_down_read_trylock(sb->s_writers.rw_sem + level-1);
  1185. WARN_ON(force_trylock && !ret);
  1186. return ret;
  1187. }
  1188. EXPORT_SYMBOL(__sb_start_write);
  1189. /**
  1190. * sb_wait_write - wait until all writers to given file system finish
  1191. * @sb: the super for which we wait
  1192. * @level: type of writers we wait for (normal vs page fault)
  1193. *
  1194. * This function waits until there are no writers of given type to given file
  1195. * system.
  1196. */
  1197. static void sb_wait_write(struct super_block *sb, int level)
  1198. {
  1199. percpu_down_write(sb->s_writers.rw_sem + level-1);
  1200. }
  1201. /*
  1202. * We are going to return to userspace and forget about these locks, the
  1203. * ownership goes to the caller of thaw_super() which does unlock().
  1204. */
  1205. static void lockdep_sb_freeze_release(struct super_block *sb)
  1206. {
  1207. int level;
  1208. for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--)
  1209. percpu_rwsem_release(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
  1210. }
  1211. /*
  1212. * Tell lockdep we are holding these locks before we call ->unfreeze_fs(sb).
  1213. */
  1214. static void lockdep_sb_freeze_acquire(struct super_block *sb)
  1215. {
  1216. int level;
  1217. for (level = 0; level < SB_FREEZE_LEVELS; ++level)
  1218. percpu_rwsem_acquire(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
  1219. }
  1220. static void sb_freeze_unlock(struct super_block *sb)
  1221. {
  1222. int level;
  1223. for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--)
  1224. percpu_up_write(sb->s_writers.rw_sem + level);
  1225. }
  1226. /**
  1227. * freeze_super - lock the filesystem and force it into a consistent state
  1228. * @sb: the super to lock
  1229. *
  1230. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  1231. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  1232. * -EBUSY.
  1233. *
  1234. * During this function, sb->s_writers.frozen goes through these values:
  1235. *
  1236. * SB_UNFROZEN: File system is normal, all writes progress as usual.
  1237. *
  1238. * SB_FREEZE_WRITE: The file system is in the process of being frozen. New
  1239. * writes should be blocked, though page faults are still allowed. We wait for
  1240. * all writes to complete and then proceed to the next stage.
  1241. *
  1242. * SB_FREEZE_PAGEFAULT: Freezing continues. Now also page faults are blocked
  1243. * but internal fs threads can still modify the filesystem (although they
  1244. * should not dirty new pages or inodes), writeback can run etc. After waiting
  1245. * for all running page faults we sync the filesystem which will clean all
  1246. * dirty pages and inodes (no new dirty pages or inodes can be created when
  1247. * sync is running).
  1248. *
  1249. * SB_FREEZE_FS: The file system is frozen. Now all internal sources of fs
  1250. * modification are blocked (e.g. XFS preallocation truncation on inode
  1251. * reclaim). This is usually implemented by blocking new transactions for
  1252. * filesystems that have them and need this additional guard. After all
  1253. * internal writers are finished we call ->freeze_fs() to finish filesystem
  1254. * freezing. Then we transition to SB_FREEZE_COMPLETE state. This state is
  1255. * mostly auxiliary for filesystems to verify they do not modify frozen fs.
  1256. *
  1257. * sb->s_writers.frozen is protected by sb->s_umount.
  1258. */
  1259. int freeze_super(struct super_block *sb)
  1260. {
  1261. int ret;
  1262. atomic_inc(&sb->s_active);
  1263. down_write(&sb->s_umount);
  1264. if (sb->s_writers.frozen != SB_UNFROZEN) {
  1265. deactivate_locked_super(sb);
  1266. return -EBUSY;
  1267. }
  1268. if (!(sb->s_flags & SB_BORN)) {
  1269. up_write(&sb->s_umount);
  1270. return 0; /* sic - it's "nothing to do" */
  1271. }
  1272. if (sb_rdonly(sb)) {
  1273. /* Nothing to do really... */
  1274. sb->s_writers.frozen = SB_FREEZE_COMPLETE;
  1275. up_write(&sb->s_umount);
  1276. return 0;
  1277. }
  1278. sb->s_writers.frozen = SB_FREEZE_WRITE;
  1279. /* Release s_umount to preserve sb_start_write -> s_umount ordering */
  1280. up_write(&sb->s_umount);
  1281. sb_wait_write(sb, SB_FREEZE_WRITE);
  1282. down_write(&sb->s_umount);
  1283. /* Now we go and block page faults... */
  1284. sb->s_writers.frozen = SB_FREEZE_PAGEFAULT;
  1285. sb_wait_write(sb, SB_FREEZE_PAGEFAULT);
  1286. /* All writers are done so after syncing there won't be dirty data */
  1287. sync_filesystem(sb);
  1288. /* Now wait for internal filesystem counter */
  1289. sb->s_writers.frozen = SB_FREEZE_FS;
  1290. sb_wait_write(sb, SB_FREEZE_FS);
  1291. if (sb->s_op->freeze_fs) {
  1292. ret = sb->s_op->freeze_fs(sb);
  1293. if (ret) {
  1294. printk(KERN_ERR
  1295. "VFS:Filesystem freeze failed\n");
  1296. sb->s_writers.frozen = SB_UNFROZEN;
  1297. sb_freeze_unlock(sb);
  1298. wake_up(&sb->s_writers.wait_unfrozen);
  1299. deactivate_locked_super(sb);
  1300. return ret;
  1301. }
  1302. }
  1303. /*
  1304. * For debugging purposes so that fs can warn if it sees write activity
  1305. * when frozen is set to SB_FREEZE_COMPLETE, and for thaw_super().
  1306. */
  1307. sb->s_writers.frozen = SB_FREEZE_COMPLETE;
  1308. lockdep_sb_freeze_release(sb);
  1309. up_write(&sb->s_umount);
  1310. return 0;
  1311. }
  1312. EXPORT_SYMBOL(freeze_super);
  1313. /**
  1314. * thaw_super -- unlock filesystem
  1315. * @sb: the super to thaw
  1316. *
  1317. * Unlocks the filesystem and marks it writeable again after freeze_super().
  1318. */
  1319. int thaw_super(struct super_block *sb)
  1320. {
  1321. int error;
  1322. down_write(&sb->s_umount);
  1323. if (sb->s_writers.frozen != SB_FREEZE_COMPLETE) {
  1324. up_write(&sb->s_umount);
  1325. return -EINVAL;
  1326. }
  1327. if (sb_rdonly(sb)) {
  1328. sb->s_writers.frozen = SB_UNFROZEN;
  1329. goto out;
  1330. }
  1331. lockdep_sb_freeze_acquire(sb);
  1332. if (sb->s_op->unfreeze_fs) {
  1333. error = sb->s_op->unfreeze_fs(sb);
  1334. if (error) {
  1335. printk(KERN_ERR
  1336. "VFS:Filesystem thaw failed\n");
  1337. lockdep_sb_freeze_release(sb);
  1338. up_write(&sb->s_umount);
  1339. return error;
  1340. }
  1341. }
  1342. sb->s_writers.frozen = SB_UNFROZEN;
  1343. sb_freeze_unlock(sb);
  1344. out:
  1345. wake_up(&sb->s_writers.wait_unfrozen);
  1346. deactivate_locked_super(sb);
  1347. return 0;
  1348. }
  1349. EXPORT_SYMBOL(thaw_super);