super.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  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/fscrypt.h>
  34. #include <linux/fsnotify.h>
  35. #include <linux/lockdep.h>
  36. #include <linux/user_namespace.h>
  37. #include <linux/fs_context.h>
  38. #include <uapi/linux/mount.h>
  39. #include "internal.h"
  40. static int thaw_super_locked(struct super_block *sb, enum freeze_holder who);
  41. static LIST_HEAD(super_blocks);
  42. static DEFINE_SPINLOCK(sb_lock);
  43. static char *sb_writers_name[SB_FREEZE_LEVELS] = {
  44. "sb_writers",
  45. "sb_pagefaults",
  46. "sb_internal",
  47. };
  48. static inline void __super_lock(struct super_block *sb, bool excl)
  49. {
  50. if (excl)
  51. down_write(&sb->s_umount);
  52. else
  53. down_read(&sb->s_umount);
  54. }
  55. static inline void super_unlock(struct super_block *sb, bool excl)
  56. {
  57. if (excl)
  58. up_write(&sb->s_umount);
  59. else
  60. up_read(&sb->s_umount);
  61. }
  62. static inline void __super_lock_excl(struct super_block *sb)
  63. {
  64. __super_lock(sb, true);
  65. }
  66. static inline void super_unlock_excl(struct super_block *sb)
  67. {
  68. super_unlock(sb, true);
  69. }
  70. static inline void super_unlock_shared(struct super_block *sb)
  71. {
  72. super_unlock(sb, false);
  73. }
  74. static bool super_flags(const struct super_block *sb, unsigned int flags)
  75. {
  76. /*
  77. * Pairs with smp_store_release() in super_wake() and ensures
  78. * that we see @flags after we're woken.
  79. */
  80. return smp_load_acquire(&sb->s_flags) & flags;
  81. }
  82. /**
  83. * super_lock - wait for superblock to become ready and lock it
  84. * @sb: superblock to wait for
  85. * @excl: whether exclusive access is required
  86. *
  87. * If the superblock has neither passed through vfs_get_tree() or
  88. * generic_shutdown_super() yet wait for it to happen. Either superblock
  89. * creation will succeed and SB_BORN is set by vfs_get_tree() or we're
  90. * woken and we'll see SB_DYING.
  91. *
  92. * The caller must have acquired a temporary reference on @sb->s_count.
  93. *
  94. * Return: The function returns true if SB_BORN was set and with
  95. * s_umount held. The function returns false if SB_DYING was
  96. * set and without s_umount held.
  97. */
  98. static __must_check bool super_lock(struct super_block *sb, bool excl)
  99. {
  100. lockdep_assert_not_held(&sb->s_umount);
  101. /* wait until the superblock is ready or dying */
  102. wait_var_event(&sb->s_flags, super_flags(sb, SB_BORN | SB_DYING));
  103. /* Don't pointlessly acquire s_umount. */
  104. if (super_flags(sb, SB_DYING))
  105. return false;
  106. __super_lock(sb, excl);
  107. /*
  108. * Has gone through generic_shutdown_super() in the meantime.
  109. * @sb->s_root is NULL and @sb->s_active is 0. No one needs to
  110. * grab a reference to this. Tell them so.
  111. */
  112. if (sb->s_flags & SB_DYING) {
  113. super_unlock(sb, excl);
  114. return false;
  115. }
  116. WARN_ON_ONCE(!(sb->s_flags & SB_BORN));
  117. return true;
  118. }
  119. /* wait and try to acquire read-side of @sb->s_umount */
  120. static inline bool super_lock_shared(struct super_block *sb)
  121. {
  122. return super_lock(sb, false);
  123. }
  124. /* wait and try to acquire write-side of @sb->s_umount */
  125. static inline bool super_lock_excl(struct super_block *sb)
  126. {
  127. return super_lock(sb, true);
  128. }
  129. /* wake waiters */
  130. #define SUPER_WAKE_FLAGS (SB_BORN | SB_DYING | SB_DEAD)
  131. static void super_wake(struct super_block *sb, unsigned int flag)
  132. {
  133. WARN_ON_ONCE((flag & ~SUPER_WAKE_FLAGS));
  134. WARN_ON_ONCE(hweight32(flag & SUPER_WAKE_FLAGS) > 1);
  135. /*
  136. * Pairs with smp_load_acquire() in super_lock() to make sure
  137. * all initializations in the superblock are seen by the user
  138. * seeing SB_BORN sent.
  139. */
  140. smp_store_release(&sb->s_flags, sb->s_flags | flag);
  141. /*
  142. * Pairs with the barrier in prepare_to_wait_event() to make sure
  143. * ___wait_var_event() either sees SB_BORN set or
  144. * waitqueue_active() check in wake_up_var() sees the waiter.
  145. */
  146. smp_mb();
  147. wake_up_var(&sb->s_flags);
  148. }
  149. /*
  150. * One thing we have to be careful of with a per-sb shrinker is that we don't
  151. * drop the last active reference to the superblock from within the shrinker.
  152. * If that happens we could trigger unregistering the shrinker from within the
  153. * shrinker path and that leads to deadlock on the shrinker_mutex. Hence we
  154. * take a passive reference to the superblock to avoid this from occurring.
  155. */
  156. static unsigned long super_cache_scan(struct shrinker *shrink,
  157. struct shrink_control *sc)
  158. {
  159. struct super_block *sb;
  160. long fs_objects = 0;
  161. long total_objects;
  162. long freed = 0;
  163. long dentries;
  164. long inodes;
  165. sb = shrink->private_data;
  166. /*
  167. * Deadlock avoidance. We may hold various FS locks, and we don't want
  168. * to recurse into the FS that called us in clear_inode() and friends..
  169. */
  170. if (!(sc->gfp_mask & __GFP_FS))
  171. return SHRINK_STOP;
  172. if (!super_trylock_shared(sb))
  173. return SHRINK_STOP;
  174. if (sb->s_op->nr_cached_objects)
  175. fs_objects = sb->s_op->nr_cached_objects(sb, sc);
  176. inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
  177. dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc);
  178. total_objects = dentries + inodes + fs_objects + 1;
  179. if (!total_objects)
  180. total_objects = 1;
  181. /* proportion the scan between the caches */
  182. dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
  183. inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
  184. fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
  185. /*
  186. * prune the dcache first as the icache is pinned by it, then
  187. * prune the icache, followed by the filesystem specific caches
  188. *
  189. * Ensure that we always scan at least one object - memcg kmem
  190. * accounting uses this to fully empty the caches.
  191. */
  192. sc->nr_to_scan = dentries + 1;
  193. freed = prune_dcache_sb(sb, sc);
  194. sc->nr_to_scan = inodes + 1;
  195. freed += prune_icache_sb(sb, sc);
  196. if (fs_objects) {
  197. sc->nr_to_scan = fs_objects + 1;
  198. freed += sb->s_op->free_cached_objects(sb, sc);
  199. }
  200. super_unlock_shared(sb);
  201. return freed;
  202. }
  203. static unsigned long super_cache_count(struct shrinker *shrink,
  204. struct shrink_control *sc)
  205. {
  206. struct super_block *sb;
  207. long total_objects = 0;
  208. sb = shrink->private_data;
  209. /*
  210. * We don't call super_trylock_shared() here as it is a scalability
  211. * bottleneck, so we're exposed to partial setup state. The shrinker
  212. * rwsem does not protect filesystem operations backing
  213. * list_lru_shrink_count() or s_op->nr_cached_objects(). Counts can
  214. * change between super_cache_count and super_cache_scan, so we really
  215. * don't need locks here.
  216. *
  217. * However, if we are currently mounting the superblock, the underlying
  218. * filesystem might be in a state of partial construction and hence it
  219. * is dangerous to access it. super_trylock_shared() uses a SB_BORN check
  220. * to avoid this situation, so do the same here. The memory barrier is
  221. * matched with the one in mount_fs() as we don't hold locks here.
  222. */
  223. if (!(sb->s_flags & SB_BORN))
  224. return 0;
  225. smp_rmb();
  226. if (sb->s_op && sb->s_op->nr_cached_objects)
  227. total_objects = sb->s_op->nr_cached_objects(sb, sc);
  228. total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
  229. total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
  230. if (!total_objects)
  231. return SHRINK_EMPTY;
  232. total_objects = vfs_pressure_ratio(total_objects);
  233. return total_objects;
  234. }
  235. static void destroy_super_work(struct work_struct *work)
  236. {
  237. struct super_block *s = container_of(work, struct super_block,
  238. destroy_work);
  239. fsnotify_sb_free(s);
  240. security_sb_free(s);
  241. put_user_ns(s->s_user_ns);
  242. kfree(s->s_subtype);
  243. for (int i = 0; i < SB_FREEZE_LEVELS; i++)
  244. percpu_free_rwsem(&s->s_writers.rw_sem[i]);
  245. kfree(s);
  246. }
  247. static void destroy_super_rcu(struct rcu_head *head)
  248. {
  249. struct super_block *s = container_of(head, struct super_block, rcu);
  250. INIT_WORK(&s->destroy_work, destroy_super_work);
  251. schedule_work(&s->destroy_work);
  252. }
  253. /* Free a superblock that has never been seen by anyone */
  254. static void destroy_unused_super(struct super_block *s)
  255. {
  256. if (!s)
  257. return;
  258. super_unlock_excl(s);
  259. list_lru_destroy(&s->s_dentry_lru);
  260. list_lru_destroy(&s->s_inode_lru);
  261. shrinker_free(s->s_shrink);
  262. /* no delays needed */
  263. destroy_super_work(&s->destroy_work);
  264. }
  265. /**
  266. * alloc_super - create new superblock
  267. * @type: filesystem type superblock should belong to
  268. * @flags: the mount flags
  269. * @user_ns: User namespace for the super_block
  270. *
  271. * Allocates and initializes a new &struct super_block. alloc_super()
  272. * returns a pointer new superblock or %NULL if allocation had failed.
  273. */
  274. static struct super_block *alloc_super(struct file_system_type *type, int flags,
  275. struct user_namespace *user_ns)
  276. {
  277. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_KERNEL);
  278. static const struct super_operations default_op;
  279. int i;
  280. if (!s)
  281. return NULL;
  282. INIT_LIST_HEAD(&s->s_mounts);
  283. s->s_user_ns = get_user_ns(user_ns);
  284. init_rwsem(&s->s_umount);
  285. lockdep_set_class(&s->s_umount, &type->s_umount_key);
  286. /*
  287. * sget() can have s_umount recursion.
  288. *
  289. * When it cannot find a suitable sb, it allocates a new
  290. * one (this one), and tries again to find a suitable old
  291. * one.
  292. *
  293. * In case that succeeds, it will acquire the s_umount
  294. * lock of the old one. Since these are clearly distrinct
  295. * locks, and this object isn't exposed yet, there's no
  296. * risk of deadlocks.
  297. *
  298. * Annotate this by putting this lock in a different
  299. * subclass.
  300. */
  301. down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
  302. if (security_sb_alloc(s))
  303. goto fail;
  304. for (i = 0; i < SB_FREEZE_LEVELS; i++) {
  305. if (__percpu_init_rwsem(&s->s_writers.rw_sem[i],
  306. sb_writers_name[i],
  307. &type->s_writers_key[i]))
  308. goto fail;
  309. }
  310. s->s_bdi = &noop_backing_dev_info;
  311. s->s_flags = flags;
  312. if (s->s_user_ns != &init_user_ns)
  313. s->s_iflags |= SB_I_NODEV;
  314. INIT_HLIST_NODE(&s->s_instances);
  315. INIT_HLIST_BL_HEAD(&s->s_roots);
  316. mutex_init(&s->s_sync_lock);
  317. INIT_LIST_HEAD(&s->s_inodes);
  318. spin_lock_init(&s->s_inode_list_lock);
  319. INIT_LIST_HEAD(&s->s_inodes_wb);
  320. spin_lock_init(&s->s_inode_wblist_lock);
  321. s->s_count = 1;
  322. atomic_set(&s->s_active, 1);
  323. mutex_init(&s->s_vfs_rename_mutex);
  324. lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
  325. init_rwsem(&s->s_dquot.dqio_sem);
  326. s->s_maxbytes = MAX_NON_LFS;
  327. s->s_op = &default_op;
  328. s->s_time_gran = 1000000000;
  329. s->s_time_min = TIME64_MIN;
  330. s->s_time_max = TIME64_MAX;
  331. s->s_shrink = shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE,
  332. "sb-%s", type->name);
  333. if (!s->s_shrink)
  334. goto fail;
  335. s->s_shrink->scan_objects = super_cache_scan;
  336. s->s_shrink->count_objects = super_cache_count;
  337. s->s_shrink->batch = 1024;
  338. s->s_shrink->private_data = s;
  339. if (list_lru_init_memcg(&s->s_dentry_lru, s->s_shrink))
  340. goto fail;
  341. if (list_lru_init_memcg(&s->s_inode_lru, s->s_shrink))
  342. goto fail;
  343. return s;
  344. fail:
  345. destroy_unused_super(s);
  346. return NULL;
  347. }
  348. /* Superblock refcounting */
  349. /*
  350. * Drop a superblock's refcount. The caller must hold sb_lock.
  351. */
  352. static void __put_super(struct super_block *s)
  353. {
  354. if (!--s->s_count) {
  355. list_del_init(&s->s_list);
  356. WARN_ON(s->s_dentry_lru.node);
  357. WARN_ON(s->s_inode_lru.node);
  358. WARN_ON(!list_empty(&s->s_mounts));
  359. call_rcu(&s->rcu, destroy_super_rcu);
  360. }
  361. }
  362. /**
  363. * put_super - drop a temporary reference to superblock
  364. * @sb: superblock in question
  365. *
  366. * Drops a temporary reference, frees superblock if there's no
  367. * references left.
  368. */
  369. void put_super(struct super_block *sb)
  370. {
  371. spin_lock(&sb_lock);
  372. __put_super(sb);
  373. spin_unlock(&sb_lock);
  374. }
  375. static void kill_super_notify(struct super_block *sb)
  376. {
  377. lockdep_assert_not_held(&sb->s_umount);
  378. /* already notified earlier */
  379. if (sb->s_flags & SB_DEAD)
  380. return;
  381. /*
  382. * Remove it from @fs_supers so it isn't found by new
  383. * sget{_fc}() walkers anymore. Any concurrent mounter still
  384. * managing to grab a temporary reference is guaranteed to
  385. * already see SB_DYING and will wait until we notify them about
  386. * SB_DEAD.
  387. */
  388. spin_lock(&sb_lock);
  389. hlist_del_init(&sb->s_instances);
  390. spin_unlock(&sb_lock);
  391. /*
  392. * Let concurrent mounts know that this thing is really dead.
  393. * We don't need @sb->s_umount here as every concurrent caller
  394. * will see SB_DYING and either discard the superblock or wait
  395. * for SB_DEAD.
  396. */
  397. super_wake(sb, SB_DEAD);
  398. }
  399. /**
  400. * deactivate_locked_super - drop an active reference to superblock
  401. * @s: superblock to deactivate
  402. *
  403. * Drops an active reference to superblock, converting it into a temporary
  404. * one if there is no other active references left. In that case we
  405. * tell fs driver to shut it down and drop the temporary reference we
  406. * had just acquired.
  407. *
  408. * Caller holds exclusive lock on superblock; that lock is released.
  409. */
  410. void deactivate_locked_super(struct super_block *s)
  411. {
  412. struct file_system_type *fs = s->s_type;
  413. if (atomic_dec_and_test(&s->s_active)) {
  414. shrinker_free(s->s_shrink);
  415. fs->kill_sb(s);
  416. kill_super_notify(s);
  417. /*
  418. * Since list_lru_destroy() may sleep, we cannot call it from
  419. * put_super(), where we hold the sb_lock. Therefore we destroy
  420. * the lru lists right now.
  421. */
  422. list_lru_destroy(&s->s_dentry_lru);
  423. list_lru_destroy(&s->s_inode_lru);
  424. put_filesystem(fs);
  425. put_super(s);
  426. } else {
  427. super_unlock_excl(s);
  428. }
  429. }
  430. EXPORT_SYMBOL(deactivate_locked_super);
  431. /**
  432. * deactivate_super - drop an active reference to superblock
  433. * @s: superblock to deactivate
  434. *
  435. * Variant of deactivate_locked_super(), except that superblock is *not*
  436. * locked by caller. If we are going to drop the final active reference,
  437. * lock will be acquired prior to that.
  438. */
  439. void deactivate_super(struct super_block *s)
  440. {
  441. if (!atomic_add_unless(&s->s_active, -1, 1)) {
  442. __super_lock_excl(s);
  443. deactivate_locked_super(s);
  444. }
  445. }
  446. EXPORT_SYMBOL(deactivate_super);
  447. /**
  448. * grab_super - acquire an active reference to a superblock
  449. * @sb: superblock to acquire
  450. *
  451. * Acquire a temporary reference on a superblock and try to trade it for
  452. * an active reference. This is used in sget{_fc}() to wait for a
  453. * superblock to either become SB_BORN or for it to pass through
  454. * sb->kill() and be marked as SB_DEAD.
  455. *
  456. * Return: This returns true if an active reference could be acquired,
  457. * false if not.
  458. */
  459. static bool grab_super(struct super_block *sb)
  460. {
  461. bool locked;
  462. sb->s_count++;
  463. spin_unlock(&sb_lock);
  464. locked = super_lock_excl(sb);
  465. if (locked) {
  466. if (atomic_inc_not_zero(&sb->s_active)) {
  467. put_super(sb);
  468. return true;
  469. }
  470. super_unlock_excl(sb);
  471. }
  472. wait_var_event(&sb->s_flags, super_flags(sb, SB_DEAD));
  473. put_super(sb);
  474. return false;
  475. }
  476. /*
  477. * super_trylock_shared - try to grab ->s_umount shared
  478. * @sb: reference we are trying to grab
  479. *
  480. * Try to prevent fs shutdown. This is used in places where we
  481. * cannot take an active reference but we need to ensure that the
  482. * filesystem is not shut down while we are working on it. It returns
  483. * false if we cannot acquire s_umount or if we lose the race and
  484. * filesystem already got into shutdown, and returns true with the s_umount
  485. * lock held in read mode in case of success. On successful return,
  486. * the caller must drop the s_umount lock when done.
  487. *
  488. * Note that unlike get_super() et.al. this one does *not* bump ->s_count.
  489. * The reason why it's safe is that we are OK with doing trylock instead
  490. * of down_read(). There's a couple of places that are OK with that, but
  491. * it's very much not a general-purpose interface.
  492. */
  493. bool super_trylock_shared(struct super_block *sb)
  494. {
  495. if (down_read_trylock(&sb->s_umount)) {
  496. if (!(sb->s_flags & SB_DYING) && sb->s_root &&
  497. (sb->s_flags & SB_BORN))
  498. return true;
  499. super_unlock_shared(sb);
  500. }
  501. return false;
  502. }
  503. /**
  504. * retire_super - prevents superblock from being reused
  505. * @sb: superblock to retire
  506. *
  507. * The function marks superblock to be ignored in superblock test, which
  508. * prevents it from being reused for any new mounts. If the superblock has
  509. * a private bdi, it also unregisters it, but doesn't reduce the refcount
  510. * of the superblock to prevent potential races. The refcount is reduced
  511. * by generic_shutdown_super(). The function can not be called
  512. * concurrently with generic_shutdown_super(). It is safe to call the
  513. * function multiple times, subsequent calls have no effect.
  514. *
  515. * The marker will affect the re-use only for block-device-based
  516. * superblocks. Other superblocks will still get marked if this function
  517. * is used, but that will not affect their reusability.
  518. */
  519. void retire_super(struct super_block *sb)
  520. {
  521. WARN_ON(!sb->s_bdev);
  522. __super_lock_excl(sb);
  523. if (sb->s_iflags & SB_I_PERSB_BDI) {
  524. bdi_unregister(sb->s_bdi);
  525. sb->s_iflags &= ~SB_I_PERSB_BDI;
  526. }
  527. sb->s_iflags |= SB_I_RETIRED;
  528. super_unlock_excl(sb);
  529. }
  530. EXPORT_SYMBOL(retire_super);
  531. /**
  532. * generic_shutdown_super - common helper for ->kill_sb()
  533. * @sb: superblock to kill
  534. *
  535. * generic_shutdown_super() does all fs-independent work on superblock
  536. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  537. * that need destruction out of superblock, call generic_shutdown_super()
  538. * and release aforementioned objects. Note: dentries and inodes _are_
  539. * taken care of and do not need specific handling.
  540. *
  541. * Upon calling this function, the filesystem may no longer alter or
  542. * rearrange the set of dentries belonging to this super_block, nor may it
  543. * change the attachments of dentries to inodes.
  544. */
  545. void generic_shutdown_super(struct super_block *sb)
  546. {
  547. const struct super_operations *sop = sb->s_op;
  548. if (sb->s_root) {
  549. shrink_dcache_for_umount(sb);
  550. sync_filesystem(sb);
  551. sb->s_flags &= ~SB_ACTIVE;
  552. cgroup_writeback_umount(sb);
  553. /* Evict all inodes with zero refcount. */
  554. evict_inodes(sb);
  555. /*
  556. * Clean up and evict any inodes that still have references due
  557. * to fsnotify or the security policy.
  558. */
  559. fsnotify_sb_delete(sb);
  560. security_sb_delete(sb);
  561. if (sb->s_dio_done_wq) {
  562. destroy_workqueue(sb->s_dio_done_wq);
  563. sb->s_dio_done_wq = NULL;
  564. }
  565. if (sop->put_super)
  566. sop->put_super(sb);
  567. /*
  568. * Now that all potentially-encrypted inodes have been evicted,
  569. * the fscrypt keyring can be destroyed.
  570. */
  571. fscrypt_destroy_keyring(sb);
  572. if (CHECK_DATA_CORRUPTION(!list_empty(&sb->s_inodes),
  573. "VFS: Busy inodes after unmount of %s (%s)",
  574. sb->s_id, sb->s_type->name)) {
  575. /*
  576. * Adding a proper bailout path here would be hard, but
  577. * we can at least make it more likely that a later
  578. * iput_final() or such crashes cleanly.
  579. */
  580. struct inode *inode;
  581. spin_lock(&sb->s_inode_list_lock);
  582. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  583. inode->i_op = VFS_PTR_POISON;
  584. inode->i_sb = VFS_PTR_POISON;
  585. inode->i_mapping = VFS_PTR_POISON;
  586. }
  587. spin_unlock(&sb->s_inode_list_lock);
  588. }
  589. }
  590. /*
  591. * Broadcast to everyone that grabbed a temporary reference to this
  592. * superblock before we removed it from @fs_supers that the superblock
  593. * is dying. Every walker of @fs_supers outside of sget{_fc}() will now
  594. * discard this superblock and treat it as dead.
  595. *
  596. * We leave the superblock on @fs_supers so it can be found by
  597. * sget{_fc}() until we passed sb->kill_sb().
  598. */
  599. super_wake(sb, SB_DYING);
  600. super_unlock_excl(sb);
  601. if (sb->s_bdi != &noop_backing_dev_info) {
  602. if (sb->s_iflags & SB_I_PERSB_BDI)
  603. bdi_unregister(sb->s_bdi);
  604. bdi_put(sb->s_bdi);
  605. sb->s_bdi = &noop_backing_dev_info;
  606. }
  607. }
  608. EXPORT_SYMBOL(generic_shutdown_super);
  609. bool mount_capable(struct fs_context *fc)
  610. {
  611. if (!(fc->fs_type->fs_flags & FS_USERNS_MOUNT))
  612. return capable(CAP_SYS_ADMIN);
  613. else
  614. return ns_capable(fc->user_ns, CAP_SYS_ADMIN);
  615. }
  616. /**
  617. * sget_fc - Find or create a superblock
  618. * @fc: Filesystem context.
  619. * @test: Comparison callback
  620. * @set: Setup callback
  621. *
  622. * Create a new superblock or find an existing one.
  623. *
  624. * The @test callback is used to find a matching existing superblock.
  625. * Whether or not the requested parameters in @fc are taken into account
  626. * is specific to the @test callback that is used. They may even be
  627. * completely ignored.
  628. *
  629. * If an extant superblock is matched, it will be returned unless:
  630. *
  631. * (1) the namespace the filesystem context @fc and the extant
  632. * superblock's namespace differ
  633. *
  634. * (2) the filesystem context @fc has requested that reusing an extant
  635. * superblock is not allowed
  636. *
  637. * In both cases EBUSY will be returned.
  638. *
  639. * If no match is made, a new superblock will be allocated and basic
  640. * initialisation will be performed (s_type, s_fs_info and s_id will be
  641. * set and the @set callback will be invoked), the superblock will be
  642. * published and it will be returned in a partially constructed state
  643. * with SB_BORN and SB_ACTIVE as yet unset.
  644. *
  645. * Return: On success, an extant or newly created superblock is
  646. * returned. On failure an error pointer is returned.
  647. */
  648. struct super_block *sget_fc(struct fs_context *fc,
  649. int (*test)(struct super_block *, struct fs_context *),
  650. int (*set)(struct super_block *, struct fs_context *))
  651. {
  652. struct super_block *s = NULL;
  653. struct super_block *old;
  654. struct user_namespace *user_ns = fc->global ? &init_user_ns : fc->user_ns;
  655. int err;
  656. /*
  657. * Never allow s_user_ns != &init_user_ns when FS_USERNS_MOUNT is
  658. * not set, as the filesystem is likely unprepared to handle it.
  659. * This can happen when fsconfig() is called from init_user_ns with
  660. * an fs_fd opened in another user namespace.
  661. */
  662. if (user_ns != &init_user_ns && !(fc->fs_type->fs_flags & FS_USERNS_MOUNT)) {
  663. errorfc(fc, "VFS: Mounting from non-initial user namespace is not allowed");
  664. return ERR_PTR(-EPERM);
  665. }
  666. retry:
  667. spin_lock(&sb_lock);
  668. if (test) {
  669. hlist_for_each_entry(old, &fc->fs_type->fs_supers, s_instances) {
  670. if (test(old, fc))
  671. goto share_extant_sb;
  672. }
  673. }
  674. if (!s) {
  675. spin_unlock(&sb_lock);
  676. s = alloc_super(fc->fs_type, fc->sb_flags, user_ns);
  677. if (!s)
  678. return ERR_PTR(-ENOMEM);
  679. goto retry;
  680. }
  681. s->s_fs_info = fc->s_fs_info;
  682. err = set(s, fc);
  683. if (err) {
  684. s->s_fs_info = NULL;
  685. spin_unlock(&sb_lock);
  686. destroy_unused_super(s);
  687. return ERR_PTR(err);
  688. }
  689. fc->s_fs_info = NULL;
  690. s->s_type = fc->fs_type;
  691. s->s_iflags |= fc->s_iflags;
  692. strscpy(s->s_id, s->s_type->name, sizeof(s->s_id));
  693. /*
  694. * Make the superblock visible on @super_blocks and @fs_supers.
  695. * It's in a nascent state and users should wait on SB_BORN or
  696. * SB_DYING to be set.
  697. */
  698. list_add_tail(&s->s_list, &super_blocks);
  699. hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
  700. spin_unlock(&sb_lock);
  701. get_filesystem(s->s_type);
  702. shrinker_register(s->s_shrink);
  703. return s;
  704. share_extant_sb:
  705. if (user_ns != old->s_user_ns || fc->exclusive) {
  706. spin_unlock(&sb_lock);
  707. destroy_unused_super(s);
  708. if (fc->exclusive)
  709. warnfc(fc, "reusing existing filesystem not allowed");
  710. else
  711. warnfc(fc, "reusing existing filesystem in another namespace not allowed");
  712. return ERR_PTR(-EBUSY);
  713. }
  714. if (!grab_super(old))
  715. goto retry;
  716. destroy_unused_super(s);
  717. return old;
  718. }
  719. EXPORT_SYMBOL(sget_fc);
  720. /**
  721. * sget - find or create a superblock
  722. * @type: filesystem type superblock should belong to
  723. * @test: comparison callback
  724. * @set: setup callback
  725. * @flags: mount flags
  726. * @data: argument to each of them
  727. */
  728. struct super_block *sget(struct file_system_type *type,
  729. int (*test)(struct super_block *,void *),
  730. int (*set)(struct super_block *,void *),
  731. int flags,
  732. void *data)
  733. {
  734. struct user_namespace *user_ns = current_user_ns();
  735. struct super_block *s = NULL;
  736. struct super_block *old;
  737. int err;
  738. /* We don't yet pass the user namespace of the parent
  739. * mount through to here so always use &init_user_ns
  740. * until that changes.
  741. */
  742. if (flags & SB_SUBMOUNT)
  743. user_ns = &init_user_ns;
  744. retry:
  745. spin_lock(&sb_lock);
  746. if (test) {
  747. hlist_for_each_entry(old, &type->fs_supers, s_instances) {
  748. if (!test(old, data))
  749. continue;
  750. if (user_ns != old->s_user_ns) {
  751. spin_unlock(&sb_lock);
  752. destroy_unused_super(s);
  753. return ERR_PTR(-EBUSY);
  754. }
  755. if (!grab_super(old))
  756. goto retry;
  757. destroy_unused_super(s);
  758. return old;
  759. }
  760. }
  761. if (!s) {
  762. spin_unlock(&sb_lock);
  763. s = alloc_super(type, (flags & ~SB_SUBMOUNT), user_ns);
  764. if (!s)
  765. return ERR_PTR(-ENOMEM);
  766. goto retry;
  767. }
  768. err = set(s, data);
  769. if (err) {
  770. spin_unlock(&sb_lock);
  771. destroy_unused_super(s);
  772. return ERR_PTR(err);
  773. }
  774. s->s_type = type;
  775. strscpy(s->s_id, type->name, sizeof(s->s_id));
  776. list_add_tail(&s->s_list, &super_blocks);
  777. hlist_add_head(&s->s_instances, &type->fs_supers);
  778. spin_unlock(&sb_lock);
  779. get_filesystem(type);
  780. shrinker_register(s->s_shrink);
  781. return s;
  782. }
  783. EXPORT_SYMBOL(sget);
  784. void drop_super(struct super_block *sb)
  785. {
  786. super_unlock_shared(sb);
  787. put_super(sb);
  788. }
  789. EXPORT_SYMBOL(drop_super);
  790. void drop_super_exclusive(struct super_block *sb)
  791. {
  792. super_unlock_excl(sb);
  793. put_super(sb);
  794. }
  795. EXPORT_SYMBOL(drop_super_exclusive);
  796. static void __iterate_supers(void (*f)(struct super_block *))
  797. {
  798. struct super_block *sb, *p = NULL;
  799. spin_lock(&sb_lock);
  800. list_for_each_entry(sb, &super_blocks, s_list) {
  801. if (super_flags(sb, SB_DYING))
  802. continue;
  803. sb->s_count++;
  804. spin_unlock(&sb_lock);
  805. f(sb);
  806. spin_lock(&sb_lock);
  807. if (p)
  808. __put_super(p);
  809. p = sb;
  810. }
  811. if (p)
  812. __put_super(p);
  813. spin_unlock(&sb_lock);
  814. }
  815. /**
  816. * iterate_supers - call function for all active superblocks
  817. * @f: function to call
  818. * @arg: argument to pass to it
  819. *
  820. * Scans the superblock list and calls given function, passing it
  821. * locked superblock and given argument.
  822. */
  823. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  824. {
  825. struct super_block *sb, *p = NULL;
  826. spin_lock(&sb_lock);
  827. list_for_each_entry(sb, &super_blocks, s_list) {
  828. bool locked;
  829. sb->s_count++;
  830. spin_unlock(&sb_lock);
  831. locked = super_lock_shared(sb);
  832. if (locked) {
  833. if (sb->s_root)
  834. f(sb, arg);
  835. super_unlock_shared(sb);
  836. }
  837. spin_lock(&sb_lock);
  838. if (p)
  839. __put_super(p);
  840. p = sb;
  841. }
  842. if (p)
  843. __put_super(p);
  844. spin_unlock(&sb_lock);
  845. }
  846. /**
  847. * iterate_supers_type - call function for superblocks of given type
  848. * @type: fs type
  849. * @f: function to call
  850. * @arg: argument to pass to it
  851. *
  852. * Scans the superblock list and calls given function, passing it
  853. * locked superblock and given argument.
  854. */
  855. void iterate_supers_type(struct file_system_type *type,
  856. void (*f)(struct super_block *, void *), void *arg)
  857. {
  858. struct super_block *sb, *p = NULL;
  859. spin_lock(&sb_lock);
  860. hlist_for_each_entry(sb, &type->fs_supers, s_instances) {
  861. bool locked;
  862. sb->s_count++;
  863. spin_unlock(&sb_lock);
  864. locked = super_lock_shared(sb);
  865. if (locked) {
  866. if (sb->s_root)
  867. f(sb, arg);
  868. super_unlock_shared(sb);
  869. }
  870. spin_lock(&sb_lock);
  871. if (p)
  872. __put_super(p);
  873. p = sb;
  874. }
  875. if (p)
  876. __put_super(p);
  877. spin_unlock(&sb_lock);
  878. }
  879. EXPORT_SYMBOL(iterate_supers_type);
  880. struct super_block *user_get_super(dev_t dev, bool excl)
  881. {
  882. struct super_block *sb;
  883. spin_lock(&sb_lock);
  884. list_for_each_entry(sb, &super_blocks, s_list) {
  885. if (sb->s_dev == dev) {
  886. bool locked;
  887. sb->s_count++;
  888. spin_unlock(&sb_lock);
  889. /* still alive? */
  890. locked = super_lock(sb, excl);
  891. if (locked) {
  892. if (sb->s_root)
  893. return sb;
  894. super_unlock(sb, excl);
  895. }
  896. /* nope, got unmounted */
  897. spin_lock(&sb_lock);
  898. __put_super(sb);
  899. break;
  900. }
  901. }
  902. spin_unlock(&sb_lock);
  903. return NULL;
  904. }
  905. /**
  906. * reconfigure_super - asks filesystem to change superblock parameters
  907. * @fc: The superblock and configuration
  908. *
  909. * Alters the configuration parameters of a live superblock.
  910. */
  911. int reconfigure_super(struct fs_context *fc)
  912. {
  913. struct super_block *sb = fc->root->d_sb;
  914. int retval;
  915. bool remount_ro = false;
  916. bool remount_rw = false;
  917. bool force = fc->sb_flags & SB_FORCE;
  918. if (fc->sb_flags_mask & ~MS_RMT_MASK)
  919. return -EINVAL;
  920. if (sb->s_writers.frozen != SB_UNFROZEN)
  921. return -EBUSY;
  922. retval = security_sb_remount(sb, fc->security);
  923. if (retval)
  924. return retval;
  925. if (fc->sb_flags_mask & SB_RDONLY) {
  926. #ifdef CONFIG_BLOCK
  927. if (!(fc->sb_flags & SB_RDONLY) && sb->s_bdev &&
  928. bdev_read_only(sb->s_bdev))
  929. return -EACCES;
  930. #endif
  931. remount_rw = !(fc->sb_flags & SB_RDONLY) && sb_rdonly(sb);
  932. remount_ro = (fc->sb_flags & SB_RDONLY) && !sb_rdonly(sb);
  933. }
  934. if (remount_ro) {
  935. if (!hlist_empty(&sb->s_pins)) {
  936. super_unlock_excl(sb);
  937. group_pin_kill(&sb->s_pins);
  938. __super_lock_excl(sb);
  939. if (!sb->s_root)
  940. return 0;
  941. if (sb->s_writers.frozen != SB_UNFROZEN)
  942. return -EBUSY;
  943. remount_ro = !sb_rdonly(sb);
  944. }
  945. }
  946. shrink_dcache_sb(sb);
  947. /* If we are reconfiguring to RDONLY and current sb is read/write,
  948. * make sure there are no files open for writing.
  949. */
  950. if (remount_ro) {
  951. if (force) {
  952. sb_start_ro_state_change(sb);
  953. } else {
  954. retval = sb_prepare_remount_readonly(sb);
  955. if (retval)
  956. return retval;
  957. }
  958. } else if (remount_rw) {
  959. /*
  960. * Protect filesystem's reconfigure code from writes from
  961. * userspace until reconfigure finishes.
  962. */
  963. sb_start_ro_state_change(sb);
  964. }
  965. if (fc->ops->reconfigure) {
  966. retval = fc->ops->reconfigure(fc);
  967. if (retval) {
  968. if (!force)
  969. goto cancel_readonly;
  970. /* If forced remount, go ahead despite any errors */
  971. WARN(1, "forced remount of a %s fs returned %i\n",
  972. sb->s_type->name, retval);
  973. }
  974. }
  975. WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
  976. (fc->sb_flags & fc->sb_flags_mask)));
  977. sb_end_ro_state_change(sb);
  978. /*
  979. * Some filesystems modify their metadata via some other path than the
  980. * bdev buffer cache (eg. use a private mapping, or directories in
  981. * pagecache, etc). Also file data modifications go via their own
  982. * mappings. So If we try to mount readonly then copy the filesystem
  983. * from bdev, we could get stale data, so invalidate it to give a best
  984. * effort at coherency.
  985. */
  986. if (remount_ro && sb->s_bdev)
  987. invalidate_bdev(sb->s_bdev);
  988. return 0;
  989. cancel_readonly:
  990. sb_end_ro_state_change(sb);
  991. return retval;
  992. }
  993. static void do_emergency_remount_callback(struct super_block *sb)
  994. {
  995. bool locked = super_lock_excl(sb);
  996. if (locked && sb->s_root && sb->s_bdev && !sb_rdonly(sb)) {
  997. struct fs_context *fc;
  998. fc = fs_context_for_reconfigure(sb->s_root,
  999. SB_RDONLY | SB_FORCE, SB_RDONLY);
  1000. if (!IS_ERR(fc)) {
  1001. if (parse_monolithic_mount_data(fc, NULL) == 0)
  1002. (void)reconfigure_super(fc);
  1003. put_fs_context(fc);
  1004. }
  1005. }
  1006. if (locked)
  1007. super_unlock_excl(sb);
  1008. }
  1009. static void do_emergency_remount(struct work_struct *work)
  1010. {
  1011. __iterate_supers(do_emergency_remount_callback);
  1012. kfree(work);
  1013. printk("Emergency Remount complete\n");
  1014. }
  1015. void emergency_remount(void)
  1016. {
  1017. struct work_struct *work;
  1018. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  1019. if (work) {
  1020. INIT_WORK(work, do_emergency_remount);
  1021. schedule_work(work);
  1022. }
  1023. }
  1024. static void do_thaw_all_callback(struct super_block *sb)
  1025. {
  1026. bool locked = super_lock_excl(sb);
  1027. if (locked && sb->s_root) {
  1028. if (IS_ENABLED(CONFIG_BLOCK))
  1029. while (sb->s_bdev && !bdev_thaw(sb->s_bdev))
  1030. pr_warn("Emergency Thaw on %pg\n", sb->s_bdev);
  1031. thaw_super_locked(sb, FREEZE_HOLDER_USERSPACE);
  1032. return;
  1033. }
  1034. if (locked)
  1035. super_unlock_excl(sb);
  1036. }
  1037. static void do_thaw_all(struct work_struct *work)
  1038. {
  1039. __iterate_supers(do_thaw_all_callback);
  1040. kfree(work);
  1041. printk(KERN_WARNING "Emergency Thaw complete\n");
  1042. }
  1043. /**
  1044. * emergency_thaw_all -- forcibly thaw every frozen filesystem
  1045. *
  1046. * Used for emergency unfreeze of all filesystems via SysRq
  1047. */
  1048. void emergency_thaw_all(void)
  1049. {
  1050. struct work_struct *work;
  1051. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  1052. if (work) {
  1053. INIT_WORK(work, do_thaw_all);
  1054. schedule_work(work);
  1055. }
  1056. }
  1057. static DEFINE_IDA(unnamed_dev_ida);
  1058. /**
  1059. * get_anon_bdev - Allocate a block device for filesystems which don't have one.
  1060. * @p: Pointer to a dev_t.
  1061. *
  1062. * Filesystems which don't use real block devices can call this function
  1063. * to allocate a virtual block device.
  1064. *
  1065. * Context: Any context. Frequently called while holding sb_lock.
  1066. * Return: 0 on success, -EMFILE if there are no anonymous bdevs left
  1067. * or -ENOMEM if memory allocation failed.
  1068. */
  1069. int get_anon_bdev(dev_t *p)
  1070. {
  1071. int dev;
  1072. /*
  1073. * Many userspace utilities consider an FSID of 0 invalid.
  1074. * Always return at least 1 from get_anon_bdev.
  1075. */
  1076. dev = ida_alloc_range(&unnamed_dev_ida, 1, (1 << MINORBITS) - 1,
  1077. GFP_ATOMIC);
  1078. if (dev == -ENOSPC)
  1079. dev = -EMFILE;
  1080. if (dev < 0)
  1081. return dev;
  1082. *p = MKDEV(0, dev);
  1083. return 0;
  1084. }
  1085. EXPORT_SYMBOL(get_anon_bdev);
  1086. void free_anon_bdev(dev_t dev)
  1087. {
  1088. ida_free(&unnamed_dev_ida, MINOR(dev));
  1089. }
  1090. EXPORT_SYMBOL(free_anon_bdev);
  1091. int set_anon_super(struct super_block *s, void *data)
  1092. {
  1093. return get_anon_bdev(&s->s_dev);
  1094. }
  1095. EXPORT_SYMBOL(set_anon_super);
  1096. void kill_anon_super(struct super_block *sb)
  1097. {
  1098. dev_t dev = sb->s_dev;
  1099. generic_shutdown_super(sb);
  1100. kill_super_notify(sb);
  1101. free_anon_bdev(dev);
  1102. }
  1103. EXPORT_SYMBOL(kill_anon_super);
  1104. void kill_litter_super(struct super_block *sb)
  1105. {
  1106. if (sb->s_root)
  1107. d_genocide(sb->s_root);
  1108. kill_anon_super(sb);
  1109. }
  1110. EXPORT_SYMBOL(kill_litter_super);
  1111. int set_anon_super_fc(struct super_block *sb, struct fs_context *fc)
  1112. {
  1113. return set_anon_super(sb, NULL);
  1114. }
  1115. EXPORT_SYMBOL(set_anon_super_fc);
  1116. static int test_keyed_super(struct super_block *sb, struct fs_context *fc)
  1117. {
  1118. return sb->s_fs_info == fc->s_fs_info;
  1119. }
  1120. static int test_single_super(struct super_block *s, struct fs_context *fc)
  1121. {
  1122. return 1;
  1123. }
  1124. static int vfs_get_super(struct fs_context *fc,
  1125. int (*test)(struct super_block *, struct fs_context *),
  1126. int (*fill_super)(struct super_block *sb,
  1127. struct fs_context *fc))
  1128. {
  1129. struct super_block *sb;
  1130. int err;
  1131. sb = sget_fc(fc, test, set_anon_super_fc);
  1132. if (IS_ERR(sb))
  1133. return PTR_ERR(sb);
  1134. if (!sb->s_root) {
  1135. err = fill_super(sb, fc);
  1136. if (err)
  1137. goto error;
  1138. sb->s_flags |= SB_ACTIVE;
  1139. }
  1140. fc->root = dget(sb->s_root);
  1141. return 0;
  1142. error:
  1143. deactivate_locked_super(sb);
  1144. return err;
  1145. }
  1146. int get_tree_nodev(struct fs_context *fc,
  1147. int (*fill_super)(struct super_block *sb,
  1148. struct fs_context *fc))
  1149. {
  1150. return vfs_get_super(fc, NULL, fill_super);
  1151. }
  1152. EXPORT_SYMBOL(get_tree_nodev);
  1153. int get_tree_single(struct fs_context *fc,
  1154. int (*fill_super)(struct super_block *sb,
  1155. struct fs_context *fc))
  1156. {
  1157. return vfs_get_super(fc, test_single_super, fill_super);
  1158. }
  1159. EXPORT_SYMBOL(get_tree_single);
  1160. int get_tree_keyed(struct fs_context *fc,
  1161. int (*fill_super)(struct super_block *sb,
  1162. struct fs_context *fc),
  1163. void *key)
  1164. {
  1165. fc->s_fs_info = key;
  1166. return vfs_get_super(fc, test_keyed_super, fill_super);
  1167. }
  1168. EXPORT_SYMBOL(get_tree_keyed);
  1169. static int set_bdev_super(struct super_block *s, void *data)
  1170. {
  1171. s->s_dev = *(dev_t *)data;
  1172. return 0;
  1173. }
  1174. static int super_s_dev_set(struct super_block *s, struct fs_context *fc)
  1175. {
  1176. return set_bdev_super(s, fc->sget_key);
  1177. }
  1178. static int super_s_dev_test(struct super_block *s, struct fs_context *fc)
  1179. {
  1180. return !(s->s_iflags & SB_I_RETIRED) &&
  1181. s->s_dev == *(dev_t *)fc->sget_key;
  1182. }
  1183. /**
  1184. * sget_dev - Find or create a superblock by device number
  1185. * @fc: Filesystem context.
  1186. * @dev: device number
  1187. *
  1188. * Find or create a superblock using the provided device number that
  1189. * will be stored in fc->sget_key.
  1190. *
  1191. * If an extant superblock is matched, then that will be returned with
  1192. * an elevated reference count that the caller must transfer or discard.
  1193. *
  1194. * If no match is made, a new superblock will be allocated and basic
  1195. * initialisation will be performed (s_type, s_fs_info, s_id, s_dev will
  1196. * be set). The superblock will be published and it will be returned in
  1197. * a partially constructed state with SB_BORN and SB_ACTIVE as yet
  1198. * unset.
  1199. *
  1200. * Return: an existing or newly created superblock on success, an error
  1201. * pointer on failure.
  1202. */
  1203. struct super_block *sget_dev(struct fs_context *fc, dev_t dev)
  1204. {
  1205. fc->sget_key = &dev;
  1206. return sget_fc(fc, super_s_dev_test, super_s_dev_set);
  1207. }
  1208. EXPORT_SYMBOL(sget_dev);
  1209. #ifdef CONFIG_BLOCK
  1210. /*
  1211. * Lock the superblock that is holder of the bdev. Returns the superblock
  1212. * pointer if we successfully locked the superblock and it is alive. Otherwise
  1213. * we return NULL and just unlock bdev->bd_holder_lock.
  1214. *
  1215. * The function must be called with bdev->bd_holder_lock and releases it.
  1216. */
  1217. static struct super_block *bdev_super_lock(struct block_device *bdev, bool excl)
  1218. __releases(&bdev->bd_holder_lock)
  1219. {
  1220. struct super_block *sb = bdev->bd_holder;
  1221. bool locked;
  1222. lockdep_assert_held(&bdev->bd_holder_lock);
  1223. lockdep_assert_not_held(&sb->s_umount);
  1224. lockdep_assert_not_held(&bdev->bd_disk->open_mutex);
  1225. /* Make sure sb doesn't go away from under us */
  1226. spin_lock(&sb_lock);
  1227. sb->s_count++;
  1228. spin_unlock(&sb_lock);
  1229. mutex_unlock(&bdev->bd_holder_lock);
  1230. locked = super_lock(sb, excl);
  1231. /*
  1232. * If the superblock wasn't already SB_DYING then we hold
  1233. * s_umount and can safely drop our temporary reference.
  1234. */
  1235. put_super(sb);
  1236. if (!locked)
  1237. return NULL;
  1238. if (!sb->s_root || !(sb->s_flags & SB_ACTIVE)) {
  1239. super_unlock(sb, excl);
  1240. return NULL;
  1241. }
  1242. return sb;
  1243. }
  1244. static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
  1245. {
  1246. struct super_block *sb;
  1247. sb = bdev_super_lock(bdev, false);
  1248. if (!sb)
  1249. return;
  1250. if (!surprise)
  1251. sync_filesystem(sb);
  1252. shrink_dcache_sb(sb);
  1253. invalidate_inodes(sb);
  1254. if (sb->s_op->shutdown)
  1255. sb->s_op->shutdown(sb);
  1256. super_unlock_shared(sb);
  1257. }
  1258. static void fs_bdev_sync(struct block_device *bdev)
  1259. {
  1260. struct super_block *sb;
  1261. sb = bdev_super_lock(bdev, false);
  1262. if (!sb)
  1263. return;
  1264. sync_filesystem(sb);
  1265. super_unlock_shared(sb);
  1266. }
  1267. static struct super_block *get_bdev_super(struct block_device *bdev)
  1268. {
  1269. bool active = false;
  1270. struct super_block *sb;
  1271. sb = bdev_super_lock(bdev, true);
  1272. if (sb) {
  1273. active = atomic_inc_not_zero(&sb->s_active);
  1274. super_unlock_excl(sb);
  1275. }
  1276. if (!active)
  1277. return NULL;
  1278. return sb;
  1279. }
  1280. /**
  1281. * fs_bdev_freeze - freeze owning filesystem of block device
  1282. * @bdev: block device
  1283. *
  1284. * Freeze the filesystem that owns this block device if it is still
  1285. * active.
  1286. *
  1287. * A filesystem that owns multiple block devices may be frozen from each
  1288. * block device and won't be unfrozen until all block devices are
  1289. * unfrozen. Each block device can only freeze the filesystem once as we
  1290. * nest freezes for block devices in the block layer.
  1291. *
  1292. * Return: If the freeze was successful zero is returned. If the freeze
  1293. * failed a negative error code is returned.
  1294. */
  1295. static int fs_bdev_freeze(struct block_device *bdev)
  1296. {
  1297. struct super_block *sb;
  1298. int error = 0;
  1299. lockdep_assert_held(&bdev->bd_fsfreeze_mutex);
  1300. sb = get_bdev_super(bdev);
  1301. if (!sb)
  1302. return -EINVAL;
  1303. if (sb->s_op->freeze_super)
  1304. error = sb->s_op->freeze_super(sb,
  1305. FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE);
  1306. else
  1307. error = freeze_super(sb,
  1308. FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE);
  1309. if (!error)
  1310. error = sync_blockdev(bdev);
  1311. deactivate_super(sb);
  1312. return error;
  1313. }
  1314. /**
  1315. * fs_bdev_thaw - thaw owning filesystem of block device
  1316. * @bdev: block device
  1317. *
  1318. * Thaw the filesystem that owns this block device.
  1319. *
  1320. * A filesystem that owns multiple block devices may be frozen from each
  1321. * block device and won't be unfrozen until all block devices are
  1322. * unfrozen. Each block device can only freeze the filesystem once as we
  1323. * nest freezes for block devices in the block layer.
  1324. *
  1325. * Return: If the thaw was successful zero is returned. If the thaw
  1326. * failed a negative error code is returned. If this function
  1327. * returns zero it doesn't mean that the filesystem is unfrozen
  1328. * as it may have been frozen multiple times (kernel may hold a
  1329. * freeze or might be frozen from other block devices).
  1330. */
  1331. static int fs_bdev_thaw(struct block_device *bdev)
  1332. {
  1333. struct super_block *sb;
  1334. int error;
  1335. lockdep_assert_held(&bdev->bd_fsfreeze_mutex);
  1336. /*
  1337. * The block device may have been frozen before it was claimed by a
  1338. * filesystem. Concurrently another process might try to mount that
  1339. * frozen block device and has temporarily claimed the block device for
  1340. * that purpose causing a concurrent fs_bdev_thaw() to end up here. The
  1341. * mounter is already about to abort mounting because they still saw an
  1342. * elevanted bdev->bd_fsfreeze_count so get_bdev_super() will return
  1343. * NULL in that case.
  1344. */
  1345. sb = get_bdev_super(bdev);
  1346. if (!sb)
  1347. return -EINVAL;
  1348. if (sb->s_op->thaw_super)
  1349. error = sb->s_op->thaw_super(sb,
  1350. FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE);
  1351. else
  1352. error = thaw_super(sb,
  1353. FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE);
  1354. deactivate_super(sb);
  1355. return error;
  1356. }
  1357. const struct blk_holder_ops fs_holder_ops = {
  1358. .mark_dead = fs_bdev_mark_dead,
  1359. .sync = fs_bdev_sync,
  1360. .freeze = fs_bdev_freeze,
  1361. .thaw = fs_bdev_thaw,
  1362. };
  1363. EXPORT_SYMBOL_GPL(fs_holder_ops);
  1364. int setup_bdev_super(struct super_block *sb, int sb_flags,
  1365. struct fs_context *fc)
  1366. {
  1367. blk_mode_t mode = sb_open_mode(sb_flags);
  1368. struct file *bdev_file;
  1369. struct block_device *bdev;
  1370. bdev_file = bdev_file_open_by_dev(sb->s_dev, mode, sb, &fs_holder_ops);
  1371. if (IS_ERR(bdev_file)) {
  1372. if (fc)
  1373. errorf(fc, "%s: Can't open blockdev", fc->source);
  1374. return PTR_ERR(bdev_file);
  1375. }
  1376. bdev = file_bdev(bdev_file);
  1377. /*
  1378. * This really should be in blkdev_get_by_dev, but right now can't due
  1379. * to legacy issues that require us to allow opening a block device node
  1380. * writable from userspace even for a read-only block device.
  1381. */
  1382. if ((mode & BLK_OPEN_WRITE) && bdev_read_only(bdev)) {
  1383. bdev_fput(bdev_file);
  1384. return -EACCES;
  1385. }
  1386. /*
  1387. * It is enough to check bdev was not frozen before we set
  1388. * s_bdev as freezing will wait until SB_BORN is set.
  1389. */
  1390. if (atomic_read(&bdev->bd_fsfreeze_count) > 0) {
  1391. if (fc)
  1392. warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev);
  1393. bdev_fput(bdev_file);
  1394. return -EBUSY;
  1395. }
  1396. spin_lock(&sb_lock);
  1397. sb->s_bdev_file = bdev_file;
  1398. sb->s_bdev = bdev;
  1399. sb->s_bdi = bdi_get(bdev->bd_disk->bdi);
  1400. if (bdev_stable_writes(bdev))
  1401. sb->s_iflags |= SB_I_STABLE_WRITES;
  1402. spin_unlock(&sb_lock);
  1403. snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev);
  1404. shrinker_debugfs_rename(sb->s_shrink, "sb-%s:%s", sb->s_type->name,
  1405. sb->s_id);
  1406. sb_set_blocksize(sb, block_size(bdev));
  1407. return 0;
  1408. }
  1409. EXPORT_SYMBOL_GPL(setup_bdev_super);
  1410. /**
  1411. * get_tree_bdev_flags - Get a superblock based on a single block device
  1412. * @fc: The filesystem context holding the parameters
  1413. * @fill_super: Helper to initialise a new superblock
  1414. * @flags: GET_TREE_BDEV_* flags
  1415. */
  1416. int get_tree_bdev_flags(struct fs_context *fc,
  1417. int (*fill_super)(struct super_block *sb,
  1418. struct fs_context *fc), unsigned int flags)
  1419. {
  1420. struct super_block *s;
  1421. int error = 0;
  1422. dev_t dev;
  1423. if (!fc->source)
  1424. return invalf(fc, "No source specified");
  1425. error = lookup_bdev(fc->source, &dev);
  1426. if (error) {
  1427. if (!(flags & GET_TREE_BDEV_QUIET_LOOKUP))
  1428. errorf(fc, "%s: Can't lookup blockdev", fc->source);
  1429. return error;
  1430. }
  1431. fc->sb_flags |= SB_NOSEC;
  1432. s = sget_dev(fc, dev);
  1433. if (IS_ERR(s))
  1434. return PTR_ERR(s);
  1435. if (s->s_root) {
  1436. /* Don't summarily change the RO/RW state. */
  1437. if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
  1438. warnf(fc, "%pg: Can't mount, would change RO state", s->s_bdev);
  1439. deactivate_locked_super(s);
  1440. return -EBUSY;
  1441. }
  1442. } else {
  1443. error = setup_bdev_super(s, fc->sb_flags, fc);
  1444. if (!error)
  1445. error = fill_super(s, fc);
  1446. if (error) {
  1447. deactivate_locked_super(s);
  1448. return error;
  1449. }
  1450. s->s_flags |= SB_ACTIVE;
  1451. }
  1452. BUG_ON(fc->root);
  1453. fc->root = dget(s->s_root);
  1454. return 0;
  1455. }
  1456. EXPORT_SYMBOL_GPL(get_tree_bdev_flags);
  1457. /**
  1458. * get_tree_bdev - Get a superblock based on a single block device
  1459. * @fc: The filesystem context holding the parameters
  1460. * @fill_super: Helper to initialise a new superblock
  1461. */
  1462. int get_tree_bdev(struct fs_context *fc,
  1463. int (*fill_super)(struct super_block *,
  1464. struct fs_context *))
  1465. {
  1466. return get_tree_bdev_flags(fc, fill_super, 0);
  1467. }
  1468. EXPORT_SYMBOL(get_tree_bdev);
  1469. static int test_bdev_super(struct super_block *s, void *data)
  1470. {
  1471. return !(s->s_iflags & SB_I_RETIRED) && s->s_dev == *(dev_t *)data;
  1472. }
  1473. struct dentry *mount_bdev(struct file_system_type *fs_type,
  1474. int flags, const char *dev_name, void *data,
  1475. int (*fill_super)(struct super_block *, void *, int))
  1476. {
  1477. struct super_block *s;
  1478. int error;
  1479. dev_t dev;
  1480. error = lookup_bdev(dev_name, &dev);
  1481. if (error)
  1482. return ERR_PTR(error);
  1483. flags |= SB_NOSEC;
  1484. s = sget(fs_type, test_bdev_super, set_bdev_super, flags, &dev);
  1485. if (IS_ERR(s))
  1486. return ERR_CAST(s);
  1487. if (s->s_root) {
  1488. if ((flags ^ s->s_flags) & SB_RDONLY) {
  1489. deactivate_locked_super(s);
  1490. return ERR_PTR(-EBUSY);
  1491. }
  1492. } else {
  1493. error = setup_bdev_super(s, flags, NULL);
  1494. if (!error)
  1495. error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
  1496. if (error) {
  1497. deactivate_locked_super(s);
  1498. return ERR_PTR(error);
  1499. }
  1500. s->s_flags |= SB_ACTIVE;
  1501. }
  1502. return dget(s->s_root);
  1503. }
  1504. EXPORT_SYMBOL(mount_bdev);
  1505. void kill_block_super(struct super_block *sb)
  1506. {
  1507. struct block_device *bdev = sb->s_bdev;
  1508. generic_shutdown_super(sb);
  1509. if (bdev) {
  1510. sync_blockdev(bdev);
  1511. bdev_fput(sb->s_bdev_file);
  1512. }
  1513. }
  1514. EXPORT_SYMBOL(kill_block_super);
  1515. #endif
  1516. struct dentry *mount_nodev(struct file_system_type *fs_type,
  1517. int flags, void *data,
  1518. int (*fill_super)(struct super_block *, void *, int))
  1519. {
  1520. int error;
  1521. struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  1522. if (IS_ERR(s))
  1523. return ERR_CAST(s);
  1524. error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
  1525. if (error) {
  1526. deactivate_locked_super(s);
  1527. return ERR_PTR(error);
  1528. }
  1529. s->s_flags |= SB_ACTIVE;
  1530. return dget(s->s_root);
  1531. }
  1532. EXPORT_SYMBOL(mount_nodev);
  1533. int reconfigure_single(struct super_block *s,
  1534. int flags, void *data)
  1535. {
  1536. struct fs_context *fc;
  1537. int ret;
  1538. /* The caller really need to be passing fc down into mount_single(),
  1539. * then a chunk of this can be removed. [Bollocks -- AV]
  1540. * Better yet, reconfiguration shouldn't happen, but rather the second
  1541. * mount should be rejected if the parameters are not compatible.
  1542. */
  1543. fc = fs_context_for_reconfigure(s->s_root, flags, MS_RMT_MASK);
  1544. if (IS_ERR(fc))
  1545. return PTR_ERR(fc);
  1546. ret = parse_monolithic_mount_data(fc, data);
  1547. if (ret < 0)
  1548. goto out;
  1549. ret = reconfigure_super(fc);
  1550. out:
  1551. put_fs_context(fc);
  1552. return ret;
  1553. }
  1554. static int compare_single(struct super_block *s, void *p)
  1555. {
  1556. return 1;
  1557. }
  1558. struct dentry *mount_single(struct file_system_type *fs_type,
  1559. int flags, void *data,
  1560. int (*fill_super)(struct super_block *, void *, int))
  1561. {
  1562. struct super_block *s;
  1563. int error;
  1564. s = sget(fs_type, compare_single, set_anon_super, flags, NULL);
  1565. if (IS_ERR(s))
  1566. return ERR_CAST(s);
  1567. if (!s->s_root) {
  1568. error = fill_super(s, data, flags & SB_SILENT ? 1 : 0);
  1569. if (!error)
  1570. s->s_flags |= SB_ACTIVE;
  1571. } else {
  1572. error = reconfigure_single(s, flags, data);
  1573. }
  1574. if (unlikely(error)) {
  1575. deactivate_locked_super(s);
  1576. return ERR_PTR(error);
  1577. }
  1578. return dget(s->s_root);
  1579. }
  1580. EXPORT_SYMBOL(mount_single);
  1581. /**
  1582. * vfs_get_tree - Get the mountable root
  1583. * @fc: The superblock configuration context.
  1584. *
  1585. * The filesystem is invoked to get or create a superblock which can then later
  1586. * be used for mounting. The filesystem places a pointer to the root to be
  1587. * used for mounting in @fc->root.
  1588. */
  1589. int vfs_get_tree(struct fs_context *fc)
  1590. {
  1591. struct super_block *sb;
  1592. int error;
  1593. if (fc->root)
  1594. return -EBUSY;
  1595. /* Get the mountable root in fc->root, with a ref on the root and a ref
  1596. * on the superblock.
  1597. */
  1598. error = fc->ops->get_tree(fc);
  1599. if (error < 0)
  1600. return error;
  1601. if (!fc->root) {
  1602. pr_err("Filesystem %s get_tree() didn't set fc->root, returned %i\n",
  1603. fc->fs_type->name, error);
  1604. /* We don't know what the locking state of the superblock is -
  1605. * if there is a superblock.
  1606. */
  1607. BUG();
  1608. }
  1609. sb = fc->root->d_sb;
  1610. WARN_ON(!sb->s_bdi);
  1611. /*
  1612. * super_wake() contains a memory barrier which also care of
  1613. * ordering for super_cache_count(). We place it before setting
  1614. * SB_BORN as the data dependency between the two functions is
  1615. * the superblock structure contents that we just set up, not
  1616. * the SB_BORN flag.
  1617. */
  1618. super_wake(sb, SB_BORN);
  1619. error = security_sb_set_mnt_opts(sb, fc->security, 0, NULL);
  1620. if (unlikely(error)) {
  1621. fc_drop_locked(fc);
  1622. return error;
  1623. }
  1624. /*
  1625. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  1626. * but s_maxbytes was an unsigned long long for many releases. Throw
  1627. * this warning for a little while to try and catch filesystems that
  1628. * violate this rule.
  1629. */
  1630. WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  1631. "negative value (%lld)\n", fc->fs_type->name, sb->s_maxbytes);
  1632. return 0;
  1633. }
  1634. EXPORT_SYMBOL(vfs_get_tree);
  1635. /*
  1636. * Setup private BDI for given superblock. It gets automatically cleaned up
  1637. * in generic_shutdown_super().
  1638. */
  1639. int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
  1640. {
  1641. struct backing_dev_info *bdi;
  1642. int err;
  1643. va_list args;
  1644. bdi = bdi_alloc(NUMA_NO_NODE);
  1645. if (!bdi)
  1646. return -ENOMEM;
  1647. va_start(args, fmt);
  1648. err = bdi_register_va(bdi, fmt, args);
  1649. va_end(args);
  1650. if (err) {
  1651. bdi_put(bdi);
  1652. return err;
  1653. }
  1654. WARN_ON(sb->s_bdi != &noop_backing_dev_info);
  1655. sb->s_bdi = bdi;
  1656. sb->s_iflags |= SB_I_PERSB_BDI;
  1657. return 0;
  1658. }
  1659. EXPORT_SYMBOL(super_setup_bdi_name);
  1660. /*
  1661. * Setup private BDI for given superblock. I gets automatically cleaned up
  1662. * in generic_shutdown_super().
  1663. */
  1664. int super_setup_bdi(struct super_block *sb)
  1665. {
  1666. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  1667. return super_setup_bdi_name(sb, "%.28s-%ld", sb->s_type->name,
  1668. atomic_long_inc_return(&bdi_seq));
  1669. }
  1670. EXPORT_SYMBOL(super_setup_bdi);
  1671. /**
  1672. * sb_wait_write - wait until all writers to given file system finish
  1673. * @sb: the super for which we wait
  1674. * @level: type of writers we wait for (normal vs page fault)
  1675. *
  1676. * This function waits until there are no writers of given type to given file
  1677. * system.
  1678. */
  1679. static void sb_wait_write(struct super_block *sb, int level)
  1680. {
  1681. percpu_down_write(sb->s_writers.rw_sem + level-1);
  1682. }
  1683. /*
  1684. * We are going to return to userspace and forget about these locks, the
  1685. * ownership goes to the caller of thaw_super() which does unlock().
  1686. */
  1687. static void lockdep_sb_freeze_release(struct super_block *sb)
  1688. {
  1689. int level;
  1690. for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--)
  1691. percpu_rwsem_release(sb->s_writers.rw_sem + level, _THIS_IP_);
  1692. }
  1693. /*
  1694. * Tell lockdep we are holding these locks before we call ->unfreeze_fs(sb).
  1695. */
  1696. static void lockdep_sb_freeze_acquire(struct super_block *sb)
  1697. {
  1698. int level;
  1699. for (level = 0; level < SB_FREEZE_LEVELS; ++level)
  1700. percpu_rwsem_acquire(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
  1701. }
  1702. static void sb_freeze_unlock(struct super_block *sb, int level)
  1703. {
  1704. for (level--; level >= 0; level--)
  1705. percpu_up_write(sb->s_writers.rw_sem + level);
  1706. }
  1707. static int wait_for_partially_frozen(struct super_block *sb)
  1708. {
  1709. int ret = 0;
  1710. do {
  1711. unsigned short old = sb->s_writers.frozen;
  1712. up_write(&sb->s_umount);
  1713. ret = wait_var_event_killable(&sb->s_writers.frozen,
  1714. sb->s_writers.frozen != old);
  1715. down_write(&sb->s_umount);
  1716. } while (ret == 0 &&
  1717. sb->s_writers.frozen != SB_UNFROZEN &&
  1718. sb->s_writers.frozen != SB_FREEZE_COMPLETE);
  1719. return ret;
  1720. }
  1721. #define FREEZE_HOLDERS (FREEZE_HOLDER_KERNEL | FREEZE_HOLDER_USERSPACE)
  1722. #define FREEZE_FLAGS (FREEZE_HOLDERS | FREEZE_MAY_NEST)
  1723. static inline int freeze_inc(struct super_block *sb, enum freeze_holder who)
  1724. {
  1725. WARN_ON_ONCE((who & ~FREEZE_FLAGS));
  1726. WARN_ON_ONCE(hweight32(who & FREEZE_HOLDERS) > 1);
  1727. if (who & FREEZE_HOLDER_KERNEL)
  1728. ++sb->s_writers.freeze_kcount;
  1729. if (who & FREEZE_HOLDER_USERSPACE)
  1730. ++sb->s_writers.freeze_ucount;
  1731. return sb->s_writers.freeze_kcount + sb->s_writers.freeze_ucount;
  1732. }
  1733. static inline int freeze_dec(struct super_block *sb, enum freeze_holder who)
  1734. {
  1735. WARN_ON_ONCE((who & ~FREEZE_FLAGS));
  1736. WARN_ON_ONCE(hweight32(who & FREEZE_HOLDERS) > 1);
  1737. if ((who & FREEZE_HOLDER_KERNEL) && sb->s_writers.freeze_kcount)
  1738. --sb->s_writers.freeze_kcount;
  1739. if ((who & FREEZE_HOLDER_USERSPACE) && sb->s_writers.freeze_ucount)
  1740. --sb->s_writers.freeze_ucount;
  1741. return sb->s_writers.freeze_kcount + sb->s_writers.freeze_ucount;
  1742. }
  1743. static inline bool may_freeze(struct super_block *sb, enum freeze_holder who)
  1744. {
  1745. WARN_ON_ONCE((who & ~FREEZE_FLAGS));
  1746. WARN_ON_ONCE(hweight32(who & FREEZE_HOLDERS) > 1);
  1747. if (who & FREEZE_HOLDER_KERNEL)
  1748. return (who & FREEZE_MAY_NEST) ||
  1749. sb->s_writers.freeze_kcount == 0;
  1750. if (who & FREEZE_HOLDER_USERSPACE)
  1751. return (who & FREEZE_MAY_NEST) ||
  1752. sb->s_writers.freeze_ucount == 0;
  1753. return false;
  1754. }
  1755. /**
  1756. * freeze_super - lock the filesystem and force it into a consistent state
  1757. * @sb: the super to lock
  1758. * @who: context that wants to freeze
  1759. *
  1760. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  1761. * freeze_fs. Subsequent calls to this without first thawing the fs may return
  1762. * -EBUSY.
  1763. *
  1764. * @who should be:
  1765. * * %FREEZE_HOLDER_USERSPACE if userspace wants to freeze the fs;
  1766. * * %FREEZE_HOLDER_KERNEL if the kernel wants to freeze the fs.
  1767. * * %FREEZE_MAY_NEST whether nesting freeze and thaw requests is allowed.
  1768. *
  1769. * The @who argument distinguishes between the kernel and userspace trying to
  1770. * freeze the filesystem. Although there cannot be multiple kernel freezes or
  1771. * multiple userspace freezes in effect at any given time, the kernel and
  1772. * userspace can both hold a filesystem frozen. The filesystem remains frozen
  1773. * until there are no kernel or userspace freezes in effect.
  1774. *
  1775. * A filesystem may hold multiple devices and thus a filesystems may be
  1776. * frozen through the block layer via multiple block devices. In this
  1777. * case the request is marked as being allowed to nest by passing
  1778. * FREEZE_MAY_NEST. The filesystem remains frozen until all block
  1779. * devices are unfrozen. If multiple freezes are attempted without
  1780. * FREEZE_MAY_NEST -EBUSY will be returned.
  1781. *
  1782. * During this function, sb->s_writers.frozen goes through these values:
  1783. *
  1784. * SB_UNFROZEN: File system is normal, all writes progress as usual.
  1785. *
  1786. * SB_FREEZE_WRITE: The file system is in the process of being frozen. New
  1787. * writes should be blocked, though page faults are still allowed. We wait for
  1788. * all writes to complete and then proceed to the next stage.
  1789. *
  1790. * SB_FREEZE_PAGEFAULT: Freezing continues. Now also page faults are blocked
  1791. * but internal fs threads can still modify the filesystem (although they
  1792. * should not dirty new pages or inodes), writeback can run etc. After waiting
  1793. * for all running page faults we sync the filesystem which will clean all
  1794. * dirty pages and inodes (no new dirty pages or inodes can be created when
  1795. * sync is running).
  1796. *
  1797. * SB_FREEZE_FS: The file system is frozen. Now all internal sources of fs
  1798. * modification are blocked (e.g. XFS preallocation truncation on inode
  1799. * reclaim). This is usually implemented by blocking new transactions for
  1800. * filesystems that have them and need this additional guard. After all
  1801. * internal writers are finished we call ->freeze_fs() to finish filesystem
  1802. * freezing. Then we transition to SB_FREEZE_COMPLETE state. This state is
  1803. * mostly auxiliary for filesystems to verify they do not modify frozen fs.
  1804. *
  1805. * sb->s_writers.frozen is protected by sb->s_umount.
  1806. *
  1807. * Return: If the freeze was successful zero is returned. If the freeze
  1808. * failed a negative error code is returned.
  1809. */
  1810. int freeze_super(struct super_block *sb, enum freeze_holder who)
  1811. {
  1812. int ret;
  1813. if (!super_lock_excl(sb)) {
  1814. WARN_ON_ONCE("Dying superblock while freezing!");
  1815. return -EINVAL;
  1816. }
  1817. atomic_inc(&sb->s_active);
  1818. retry:
  1819. if (sb->s_writers.frozen == SB_FREEZE_COMPLETE) {
  1820. if (may_freeze(sb, who))
  1821. ret = !!WARN_ON_ONCE(freeze_inc(sb, who) == 1);
  1822. else
  1823. ret = -EBUSY;
  1824. /* All freezers share a single active reference. */
  1825. deactivate_locked_super(sb);
  1826. return ret;
  1827. }
  1828. if (sb->s_writers.frozen != SB_UNFROZEN) {
  1829. ret = wait_for_partially_frozen(sb);
  1830. if (ret) {
  1831. deactivate_locked_super(sb);
  1832. return ret;
  1833. }
  1834. goto retry;
  1835. }
  1836. if (sb_rdonly(sb)) {
  1837. /* Nothing to do really... */
  1838. WARN_ON_ONCE(freeze_inc(sb, who) > 1);
  1839. sb->s_writers.frozen = SB_FREEZE_COMPLETE;
  1840. wake_up_var(&sb->s_writers.frozen);
  1841. super_unlock_excl(sb);
  1842. return 0;
  1843. }
  1844. sb->s_writers.frozen = SB_FREEZE_WRITE;
  1845. /* Release s_umount to preserve sb_start_write -> s_umount ordering */
  1846. super_unlock_excl(sb);
  1847. sb_wait_write(sb, SB_FREEZE_WRITE);
  1848. __super_lock_excl(sb);
  1849. /* Now we go and block page faults... */
  1850. sb->s_writers.frozen = SB_FREEZE_PAGEFAULT;
  1851. sb_wait_write(sb, SB_FREEZE_PAGEFAULT);
  1852. /* All writers are done so after syncing there won't be dirty data */
  1853. ret = sync_filesystem(sb);
  1854. if (ret) {
  1855. sb->s_writers.frozen = SB_UNFROZEN;
  1856. sb_freeze_unlock(sb, SB_FREEZE_PAGEFAULT);
  1857. wake_up_var(&sb->s_writers.frozen);
  1858. deactivate_locked_super(sb);
  1859. return ret;
  1860. }
  1861. /* Now wait for internal filesystem counter */
  1862. sb->s_writers.frozen = SB_FREEZE_FS;
  1863. sb_wait_write(sb, SB_FREEZE_FS);
  1864. if (sb->s_op->freeze_fs) {
  1865. ret = sb->s_op->freeze_fs(sb);
  1866. if (ret) {
  1867. printk(KERN_ERR
  1868. "VFS:Filesystem freeze failed\n");
  1869. sb->s_writers.frozen = SB_UNFROZEN;
  1870. sb_freeze_unlock(sb, SB_FREEZE_FS);
  1871. wake_up_var(&sb->s_writers.frozen);
  1872. deactivate_locked_super(sb);
  1873. return ret;
  1874. }
  1875. }
  1876. /*
  1877. * For debugging purposes so that fs can warn if it sees write activity
  1878. * when frozen is set to SB_FREEZE_COMPLETE, and for thaw_super().
  1879. */
  1880. WARN_ON_ONCE(freeze_inc(sb, who) > 1);
  1881. sb->s_writers.frozen = SB_FREEZE_COMPLETE;
  1882. wake_up_var(&sb->s_writers.frozen);
  1883. lockdep_sb_freeze_release(sb);
  1884. super_unlock_excl(sb);
  1885. return 0;
  1886. }
  1887. EXPORT_SYMBOL(freeze_super);
  1888. /*
  1889. * Undoes the effect of a freeze_super_locked call. If the filesystem is
  1890. * frozen both by userspace and the kernel, a thaw call from either source
  1891. * removes that state without releasing the other state or unlocking the
  1892. * filesystem.
  1893. */
  1894. static int thaw_super_locked(struct super_block *sb, enum freeze_holder who)
  1895. {
  1896. int error = -EINVAL;
  1897. if (sb->s_writers.frozen != SB_FREEZE_COMPLETE)
  1898. goto out_unlock;
  1899. /*
  1900. * All freezers share a single active reference.
  1901. * So just unlock in case there are any left.
  1902. */
  1903. if (freeze_dec(sb, who))
  1904. goto out_unlock;
  1905. if (sb_rdonly(sb)) {
  1906. sb->s_writers.frozen = SB_UNFROZEN;
  1907. wake_up_var(&sb->s_writers.frozen);
  1908. goto out_deactivate;
  1909. }
  1910. lockdep_sb_freeze_acquire(sb);
  1911. if (sb->s_op->unfreeze_fs) {
  1912. error = sb->s_op->unfreeze_fs(sb);
  1913. if (error) {
  1914. pr_err("VFS: Filesystem thaw failed\n");
  1915. freeze_inc(sb, who);
  1916. lockdep_sb_freeze_release(sb);
  1917. goto out_unlock;
  1918. }
  1919. }
  1920. sb->s_writers.frozen = SB_UNFROZEN;
  1921. wake_up_var(&sb->s_writers.frozen);
  1922. sb_freeze_unlock(sb, SB_FREEZE_FS);
  1923. out_deactivate:
  1924. deactivate_locked_super(sb);
  1925. return 0;
  1926. out_unlock:
  1927. super_unlock_excl(sb);
  1928. return error;
  1929. }
  1930. /**
  1931. * thaw_super -- unlock filesystem
  1932. * @sb: the super to thaw
  1933. * @who: context that wants to freeze
  1934. *
  1935. * Unlocks the filesystem and marks it writeable again after freeze_super()
  1936. * if there are no remaining freezes on the filesystem.
  1937. *
  1938. * @who should be:
  1939. * * %FREEZE_HOLDER_USERSPACE if userspace wants to thaw the fs;
  1940. * * %FREEZE_HOLDER_KERNEL if the kernel wants to thaw the fs.
  1941. * * %FREEZE_MAY_NEST whether nesting freeze and thaw requests is allowed
  1942. *
  1943. * A filesystem may hold multiple devices and thus a filesystems may
  1944. * have been frozen through the block layer via multiple block devices.
  1945. * The filesystem remains frozen until all block devices are unfrozen.
  1946. */
  1947. int thaw_super(struct super_block *sb, enum freeze_holder who)
  1948. {
  1949. if (!super_lock_excl(sb)) {
  1950. WARN_ON_ONCE("Dying superblock while thawing!");
  1951. return -EINVAL;
  1952. }
  1953. return thaw_super_locked(sb, who);
  1954. }
  1955. EXPORT_SYMBOL(thaw_super);
  1956. /*
  1957. * Create workqueue for deferred direct IO completions. We allocate the
  1958. * workqueue when it's first needed. This avoids creating workqueue for
  1959. * filesystems that don't need it and also allows us to create the workqueue
  1960. * late enough so the we can include s_id in the name of the workqueue.
  1961. */
  1962. int sb_init_dio_done_wq(struct super_block *sb)
  1963. {
  1964. struct workqueue_struct *old;
  1965. struct workqueue_struct *wq = alloc_workqueue("dio/%s",
  1966. WQ_MEM_RECLAIM, 0,
  1967. sb->s_id);
  1968. if (!wq)
  1969. return -ENOMEM;
  1970. /*
  1971. * This has to be atomic as more DIOs can race to create the workqueue
  1972. */
  1973. old = cmpxchg(&sb->s_dio_done_wq, NULL, wq);
  1974. /* Someone created workqueue before us? Free ours... */
  1975. if (old)
  1976. destroy_workqueue(wq);
  1977. return 0;
  1978. }
  1979. EXPORT_SYMBOL_GPL(sb_init_dio_done_wq);