super.c 36 KB

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