super.c 29 KB

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