super.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. unlock_kernel();
  499. return -EBUSY;
  500. }
  501. retval = vfs_dq_off(sb, 1);
  502. if (retval < 0 && retval != -ENOSYS) {
  503. unlock_kernel();
  504. return -EBUSY;
  505. }
  506. }
  507. remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
  508. if (sb->s_op->remount_fs) {
  509. retval = sb->s_op->remount_fs(sb, &flags, data);
  510. if (retval) {
  511. unlock_kernel();
  512. return retval;
  513. }
  514. }
  515. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  516. if (remount_rw)
  517. vfs_dq_quota_on_remount(sb);
  518. return 0;
  519. }
  520. static void do_emergency_remount(struct work_struct *work)
  521. {
  522. struct super_block *sb;
  523. spin_lock(&sb_lock);
  524. list_for_each_entry(sb, &super_blocks, s_list) {
  525. sb->s_count++;
  526. spin_unlock(&sb_lock);
  527. down_write(&sb->s_umount);
  528. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  529. /*
  530. * ->remount_fs needs lock_kernel().
  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. put_super(sb);
  538. spin_lock(&sb_lock);
  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. int set_anon_super(struct super_block *s, void *data)
  560. {
  561. int dev;
  562. int error;
  563. retry:
  564. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  565. return -ENOMEM;
  566. spin_lock(&unnamed_dev_lock);
  567. error = ida_get_new(&unnamed_dev_ida, &dev);
  568. spin_unlock(&unnamed_dev_lock);
  569. if (error == -EAGAIN)
  570. /* We raced and lost with another CPU. */
  571. goto retry;
  572. else if (error)
  573. return -EAGAIN;
  574. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  575. spin_lock(&unnamed_dev_lock);
  576. ida_remove(&unnamed_dev_ida, dev);
  577. spin_unlock(&unnamed_dev_lock);
  578. return -EMFILE;
  579. }
  580. s->s_dev = MKDEV(0, dev & MINORMASK);
  581. return 0;
  582. }
  583. EXPORT_SYMBOL(set_anon_super);
  584. void kill_anon_super(struct super_block *sb)
  585. {
  586. int slot = MINOR(sb->s_dev);
  587. generic_shutdown_super(sb);
  588. spin_lock(&unnamed_dev_lock);
  589. ida_remove(&unnamed_dev_ida, slot);
  590. spin_unlock(&unnamed_dev_lock);
  591. }
  592. EXPORT_SYMBOL(kill_anon_super);
  593. void kill_litter_super(struct super_block *sb)
  594. {
  595. if (sb->s_root)
  596. d_genocide(sb->s_root);
  597. kill_anon_super(sb);
  598. }
  599. EXPORT_SYMBOL(kill_litter_super);
  600. static int ns_test_super(struct super_block *sb, void *data)
  601. {
  602. return sb->s_fs_info == data;
  603. }
  604. static int ns_set_super(struct super_block *sb, void *data)
  605. {
  606. sb->s_fs_info = data;
  607. return set_anon_super(sb, NULL);
  608. }
  609. int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
  610. int (*fill_super)(struct super_block *, void *, int),
  611. struct vfsmount *mnt)
  612. {
  613. struct super_block *sb;
  614. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  615. if (IS_ERR(sb))
  616. return PTR_ERR(sb);
  617. if (!sb->s_root) {
  618. int err;
  619. sb->s_flags = flags;
  620. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  621. if (err) {
  622. deactivate_locked_super(sb);
  623. return err;
  624. }
  625. sb->s_flags |= MS_ACTIVE;
  626. }
  627. simple_set_mnt(mnt, sb);
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(get_sb_ns);
  631. #ifdef CONFIG_BLOCK
  632. static int set_bdev_super(struct super_block *s, void *data)
  633. {
  634. s->s_bdev = data;
  635. s->s_dev = s->s_bdev->bd_dev;
  636. return 0;
  637. }
  638. static int test_bdev_super(struct super_block *s, void *data)
  639. {
  640. return (void *)s->s_bdev == data;
  641. }
  642. int get_sb_bdev(struct file_system_type *fs_type,
  643. int flags, const char *dev_name, void *data,
  644. int (*fill_super)(struct super_block *, void *, int),
  645. struct vfsmount *mnt)
  646. {
  647. struct block_device *bdev;
  648. struct super_block *s;
  649. fmode_t mode = FMODE_READ;
  650. int error = 0;
  651. if (!(flags & MS_RDONLY))
  652. mode |= FMODE_WRITE;
  653. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  654. if (IS_ERR(bdev))
  655. return PTR_ERR(bdev);
  656. /*
  657. * once the super is inserted into the list by sget, s_umount
  658. * will protect the lockfs code from trying to start a snapshot
  659. * while we are mounting
  660. */
  661. down(&bdev->bd_mount_sem);
  662. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  663. up(&bdev->bd_mount_sem);
  664. if (IS_ERR(s))
  665. goto error_s;
  666. if (s->s_root) {
  667. if ((flags ^ s->s_flags) & MS_RDONLY) {
  668. deactivate_locked_super(s);
  669. error = -EBUSY;
  670. goto error_bdev;
  671. }
  672. close_bdev_exclusive(bdev, mode);
  673. } else {
  674. char b[BDEVNAME_SIZE];
  675. s->s_flags = flags;
  676. s->s_mode = mode;
  677. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  678. sb_set_blocksize(s, block_size(bdev));
  679. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  680. if (error) {
  681. deactivate_locked_super(s);
  682. goto error;
  683. }
  684. s->s_flags |= MS_ACTIVE;
  685. bdev->bd_super = s;
  686. }
  687. simple_set_mnt(mnt, s);
  688. return 0;
  689. error_s:
  690. error = PTR_ERR(s);
  691. error_bdev:
  692. close_bdev_exclusive(bdev, mode);
  693. error:
  694. return error;
  695. }
  696. EXPORT_SYMBOL(get_sb_bdev);
  697. void kill_block_super(struct super_block *sb)
  698. {
  699. struct block_device *bdev = sb->s_bdev;
  700. fmode_t mode = sb->s_mode;
  701. bdev->bd_super = NULL;
  702. generic_shutdown_super(sb);
  703. sync_blockdev(bdev);
  704. close_bdev_exclusive(bdev, mode);
  705. }
  706. EXPORT_SYMBOL(kill_block_super);
  707. #endif
  708. int get_sb_nodev(struct file_system_type *fs_type,
  709. int flags, void *data,
  710. int (*fill_super)(struct super_block *, void *, int),
  711. struct vfsmount *mnt)
  712. {
  713. int error;
  714. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  715. if (IS_ERR(s))
  716. return PTR_ERR(s);
  717. s->s_flags = flags;
  718. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  719. if (error) {
  720. deactivate_locked_super(s);
  721. return error;
  722. }
  723. s->s_flags |= MS_ACTIVE;
  724. simple_set_mnt(mnt, s);
  725. return 0;
  726. }
  727. EXPORT_SYMBOL(get_sb_nodev);
  728. static int compare_single(struct super_block *s, void *p)
  729. {
  730. return 1;
  731. }
  732. int get_sb_single(struct file_system_type *fs_type,
  733. int flags, void *data,
  734. int (*fill_super)(struct super_block *, void *, int),
  735. struct vfsmount *mnt)
  736. {
  737. struct super_block *s;
  738. int error;
  739. s = sget(fs_type, compare_single, set_anon_super, NULL);
  740. if (IS_ERR(s))
  741. return PTR_ERR(s);
  742. if (!s->s_root) {
  743. s->s_flags = flags;
  744. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  745. if (error) {
  746. deactivate_locked_super(s);
  747. return error;
  748. }
  749. s->s_flags |= MS_ACTIVE;
  750. }
  751. do_remount_sb(s, flags, data, 0);
  752. simple_set_mnt(mnt, s);
  753. return 0;
  754. }
  755. EXPORT_SYMBOL(get_sb_single);
  756. struct vfsmount *
  757. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  758. {
  759. struct vfsmount *mnt;
  760. char *secdata = NULL;
  761. int error;
  762. if (!type)
  763. return ERR_PTR(-ENODEV);
  764. error = -ENOMEM;
  765. mnt = alloc_vfsmnt(name);
  766. if (!mnt)
  767. goto out;
  768. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  769. secdata = alloc_secdata();
  770. if (!secdata)
  771. goto out_mnt;
  772. error = security_sb_copy_data(data, secdata);
  773. if (error)
  774. goto out_free_secdata;
  775. }
  776. error = type->get_sb(type, flags, name, data, mnt);
  777. if (error < 0)
  778. goto out_free_secdata;
  779. BUG_ON(!mnt->mnt_sb);
  780. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  781. if (error)
  782. goto out_sb;
  783. mnt->mnt_mountpoint = mnt->mnt_root;
  784. mnt->mnt_parent = mnt;
  785. up_write(&mnt->mnt_sb->s_umount);
  786. free_secdata(secdata);
  787. return mnt;
  788. out_sb:
  789. dput(mnt->mnt_root);
  790. deactivate_locked_super(mnt->mnt_sb);
  791. out_free_secdata:
  792. free_secdata(secdata);
  793. out_mnt:
  794. free_vfsmnt(mnt);
  795. out:
  796. return ERR_PTR(error);
  797. }
  798. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  799. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  800. {
  801. int err;
  802. const char *subtype = strchr(fstype, '.');
  803. if (subtype) {
  804. subtype++;
  805. err = -EINVAL;
  806. if (!subtype[0])
  807. goto err;
  808. } else
  809. subtype = "";
  810. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  811. err = -ENOMEM;
  812. if (!mnt->mnt_sb->s_subtype)
  813. goto err;
  814. return mnt;
  815. err:
  816. mntput(mnt);
  817. return ERR_PTR(err);
  818. }
  819. struct vfsmount *
  820. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  821. {
  822. struct file_system_type *type = get_fs_type(fstype);
  823. struct vfsmount *mnt;
  824. if (!type)
  825. return ERR_PTR(-ENODEV);
  826. mnt = vfs_kern_mount(type, flags, name, data);
  827. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  828. !mnt->mnt_sb->s_subtype)
  829. mnt = fs_set_subtype(mnt, fstype);
  830. put_filesystem(type);
  831. return mnt;
  832. }
  833. EXPORT_SYMBOL_GPL(do_kern_mount);
  834. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  835. {
  836. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  837. }
  838. EXPORT_SYMBOL_GPL(kern_mount_data);