super.c 37 KB

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