super.c 25 KB

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