super.c 35 KB

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