super.c 39 KB

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