super.c 25 KB

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