super.c 35 KB

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