super.c 35 KB

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