super.c 34 KB

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