super.c 40 KB

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