super.c 37 KB

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