super.c 22 KB

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