super.c 47 KB

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