super.c 40 KB

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