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