super.c 41 KB

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