super.c 36 KB

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