super.c 35 KB

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