super.c 35 KB

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