super.c 48 KB

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