super.c 61 KB

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