super.c 35 KB

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