super.c 35 KB

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