super.c 30 KB

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