super.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. lock_super(sb);
  285. sb->s_flags &= ~MS_ACTIVE;
  286. /* bad name - it should be evict_inodes() */
  287. invalidate_inodes(sb);
  288. lock_kernel();
  289. if (sop->put_super)
  290. sop->put_super(sb);
  291. /* Forget any remaining inodes */
  292. if (invalidate_inodes(sb)) {
  293. printk("VFS: Busy inodes after unmount of %s. "
  294. "Self-destruct in 5 seconds. Have a nice day...\n",
  295. sb->s_id);
  296. }
  297. unlock_kernel();
  298. unlock_super(sb);
  299. }
  300. spin_lock(&sb_lock);
  301. /* should be initialized for __put_super_and_need_restart() */
  302. list_del_init(&sb->s_list);
  303. list_del(&sb->s_instances);
  304. spin_unlock(&sb_lock);
  305. up_write(&sb->s_umount);
  306. }
  307. EXPORT_SYMBOL(generic_shutdown_super);
  308. /**
  309. * sget - find or create a superblock
  310. * @type: filesystem type superblock should belong to
  311. * @test: comparison callback
  312. * @set: setup callback
  313. * @data: argument to each of them
  314. */
  315. struct super_block *sget(struct file_system_type *type,
  316. int (*test)(struct super_block *,void *),
  317. int (*set)(struct super_block *,void *),
  318. void *data)
  319. {
  320. struct super_block *s = NULL;
  321. struct super_block *old;
  322. int err;
  323. retry:
  324. spin_lock(&sb_lock);
  325. if (test) {
  326. list_for_each_entry(old, &type->fs_supers, s_instances) {
  327. if (!test(old, data))
  328. continue;
  329. if (!grab_super(old))
  330. goto retry;
  331. if (s) {
  332. up_write(&s->s_umount);
  333. destroy_super(s);
  334. }
  335. return old;
  336. }
  337. }
  338. if (!s) {
  339. spin_unlock(&sb_lock);
  340. s = alloc_super(type);
  341. if (!s)
  342. return ERR_PTR(-ENOMEM);
  343. goto retry;
  344. }
  345. err = set(s, data);
  346. if (err) {
  347. spin_unlock(&sb_lock);
  348. up_write(&s->s_umount);
  349. destroy_super(s);
  350. return ERR_PTR(err);
  351. }
  352. s->s_type = type;
  353. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  354. list_add_tail(&s->s_list, &super_blocks);
  355. list_add(&s->s_instances, &type->fs_supers);
  356. spin_unlock(&sb_lock);
  357. get_filesystem(type);
  358. return s;
  359. }
  360. EXPORT_SYMBOL(sget);
  361. void drop_super(struct super_block *sb)
  362. {
  363. up_read(&sb->s_umount);
  364. put_super(sb);
  365. }
  366. EXPORT_SYMBOL(drop_super);
  367. static inline void write_super(struct super_block *sb)
  368. {
  369. lock_super(sb);
  370. if (sb->s_root && sb->s_dirt)
  371. if (sb->s_op->write_super)
  372. sb->s_op->write_super(sb);
  373. unlock_super(sb);
  374. }
  375. /*
  376. * Note: check the dirty flag before waiting, so we don't
  377. * hold up the sync while mounting a device. (The newly
  378. * mounted device won't need syncing.)
  379. */
  380. void sync_supers(void)
  381. {
  382. struct super_block *sb;
  383. spin_lock(&sb_lock);
  384. restart:
  385. list_for_each_entry(sb, &super_blocks, s_list) {
  386. if (sb->s_dirt) {
  387. sb->s_count++;
  388. spin_unlock(&sb_lock);
  389. down_read(&sb->s_umount);
  390. write_super(sb);
  391. up_read(&sb->s_umount);
  392. spin_lock(&sb_lock);
  393. if (__put_super_and_need_restart(sb))
  394. goto restart;
  395. }
  396. }
  397. spin_unlock(&sb_lock);
  398. }
  399. /**
  400. * get_super - get the superblock of a device
  401. * @bdev: device to get the superblock for
  402. *
  403. * Scans the superblock list and finds the superblock of the file system
  404. * mounted on the device given. %NULL is returned if no match is found.
  405. */
  406. struct super_block * get_super(struct block_device *bdev)
  407. {
  408. struct super_block *sb;
  409. if (!bdev)
  410. return NULL;
  411. spin_lock(&sb_lock);
  412. rescan:
  413. list_for_each_entry(sb, &super_blocks, s_list) {
  414. if (sb->s_bdev == bdev) {
  415. sb->s_count++;
  416. spin_unlock(&sb_lock);
  417. down_read(&sb->s_umount);
  418. if (sb->s_root)
  419. return sb;
  420. up_read(&sb->s_umount);
  421. /* restart only when sb is no longer on the list */
  422. spin_lock(&sb_lock);
  423. if (__put_super_and_need_restart(sb))
  424. goto rescan;
  425. }
  426. }
  427. spin_unlock(&sb_lock);
  428. return NULL;
  429. }
  430. EXPORT_SYMBOL(get_super);
  431. struct super_block * user_get_super(dev_t dev)
  432. {
  433. struct super_block *sb;
  434. spin_lock(&sb_lock);
  435. rescan:
  436. list_for_each_entry(sb, &super_blocks, s_list) {
  437. if (sb->s_dev == dev) {
  438. sb->s_count++;
  439. spin_unlock(&sb_lock);
  440. down_read(&sb->s_umount);
  441. if (sb->s_root)
  442. return sb;
  443. up_read(&sb->s_umount);
  444. /* restart only when sb is no longer on the list */
  445. spin_lock(&sb_lock);
  446. if (__put_super_and_need_restart(sb))
  447. goto rescan;
  448. }
  449. }
  450. spin_unlock(&sb_lock);
  451. return NULL;
  452. }
  453. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  454. {
  455. struct super_block *s;
  456. struct ustat tmp;
  457. struct kstatfs sbuf;
  458. int err = -EINVAL;
  459. s = user_get_super(new_decode_dev(dev));
  460. if (s == NULL)
  461. goto out;
  462. err = vfs_statfs(s->s_root, &sbuf);
  463. drop_super(s);
  464. if (err)
  465. goto out;
  466. memset(&tmp,0,sizeof(struct ustat));
  467. tmp.f_tfree = sbuf.f_bfree;
  468. tmp.f_tinode = sbuf.f_ffree;
  469. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  470. out:
  471. return err;
  472. }
  473. /**
  474. * do_remount_sb - asks filesystem to change mount options.
  475. * @sb: superblock in question
  476. * @flags: numeric part of options
  477. * @data: the rest of options
  478. * @force: whether or not to force the change
  479. *
  480. * Alters the mount options of a mounted file system.
  481. */
  482. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  483. {
  484. int retval;
  485. int remount_rw;
  486. #ifdef CONFIG_BLOCK
  487. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  488. return -EACCES;
  489. #endif
  490. if (flags & MS_RDONLY)
  491. acct_auto_close(sb);
  492. shrink_dcache_sb(sb);
  493. sync_filesystem(sb);
  494. /* If we are remounting RDONLY and current sb is read/write,
  495. make sure there are no rw files opened */
  496. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  497. if (force)
  498. mark_files_ro(sb);
  499. else if (!fs_may_remount_ro(sb))
  500. return -EBUSY;
  501. retval = vfs_dq_off(sb, 1);
  502. if (retval < 0 && retval != -ENOSYS)
  503. return -EBUSY;
  504. }
  505. remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
  506. if (sb->s_op->remount_fs) {
  507. lock_super(sb);
  508. retval = sb->s_op->remount_fs(sb, &flags, data);
  509. unlock_super(sb);
  510. if (retval)
  511. return retval;
  512. }
  513. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  514. if (remount_rw)
  515. vfs_dq_quota_on_remount(sb);
  516. return 0;
  517. }
  518. static void do_emergency_remount(struct work_struct *work)
  519. {
  520. struct super_block *sb;
  521. spin_lock(&sb_lock);
  522. list_for_each_entry(sb, &super_blocks, s_list) {
  523. sb->s_count++;
  524. spin_unlock(&sb_lock);
  525. down_read(&sb->s_umount);
  526. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  527. /*
  528. * ->remount_fs needs lock_kernel().
  529. *
  530. * What lock protects sb->s_flags??
  531. */
  532. lock_kernel();
  533. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  534. unlock_kernel();
  535. }
  536. drop_super(sb);
  537. spin_lock(&sb_lock);
  538. }
  539. spin_unlock(&sb_lock);
  540. kfree(work);
  541. printk("Emergency Remount complete\n");
  542. }
  543. void emergency_remount(void)
  544. {
  545. struct work_struct *work;
  546. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  547. if (work) {
  548. INIT_WORK(work, do_emergency_remount);
  549. schedule_work(work);
  550. }
  551. }
  552. /*
  553. * Unnamed block devices are dummy devices used by virtual
  554. * filesystems which don't use real block-devices. -- jrs
  555. */
  556. static DEFINE_IDA(unnamed_dev_ida);
  557. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  558. int set_anon_super(struct super_block *s, void *data)
  559. {
  560. int dev;
  561. int error;
  562. retry:
  563. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  564. return -ENOMEM;
  565. spin_lock(&unnamed_dev_lock);
  566. error = ida_get_new(&unnamed_dev_ida, &dev);
  567. spin_unlock(&unnamed_dev_lock);
  568. if (error == -EAGAIN)
  569. /* We raced and lost with another CPU. */
  570. goto retry;
  571. else if (error)
  572. return -EAGAIN;
  573. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  574. spin_lock(&unnamed_dev_lock);
  575. ida_remove(&unnamed_dev_ida, 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. spin_unlock(&unnamed_dev_lock);
  590. }
  591. EXPORT_SYMBOL(kill_anon_super);
  592. void kill_litter_super(struct super_block *sb)
  593. {
  594. if (sb->s_root)
  595. d_genocide(sb->s_root);
  596. kill_anon_super(sb);
  597. }
  598. EXPORT_SYMBOL(kill_litter_super);
  599. static int ns_test_super(struct super_block *sb, void *data)
  600. {
  601. return sb->s_fs_info == data;
  602. }
  603. static int ns_set_super(struct super_block *sb, void *data)
  604. {
  605. sb->s_fs_info = data;
  606. return set_anon_super(sb, NULL);
  607. }
  608. int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
  609. int (*fill_super)(struct super_block *, void *, int),
  610. struct vfsmount *mnt)
  611. {
  612. struct super_block *sb;
  613. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  614. if (IS_ERR(sb))
  615. return PTR_ERR(sb);
  616. if (!sb->s_root) {
  617. int err;
  618. sb->s_flags = flags;
  619. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  620. if (err) {
  621. deactivate_locked_super(sb);
  622. return err;
  623. }
  624. sb->s_flags |= MS_ACTIVE;
  625. }
  626. simple_set_mnt(mnt, sb);
  627. return 0;
  628. }
  629. EXPORT_SYMBOL(get_sb_ns);
  630. #ifdef CONFIG_BLOCK
  631. static int set_bdev_super(struct super_block *s, void *data)
  632. {
  633. s->s_bdev = data;
  634. s->s_dev = s->s_bdev->bd_dev;
  635. return 0;
  636. }
  637. static int test_bdev_super(struct super_block *s, void *data)
  638. {
  639. return (void *)s->s_bdev == data;
  640. }
  641. int get_sb_bdev(struct file_system_type *fs_type,
  642. int flags, const char *dev_name, void *data,
  643. int (*fill_super)(struct super_block *, void *, int),
  644. struct vfsmount *mnt)
  645. {
  646. struct block_device *bdev;
  647. struct super_block *s;
  648. fmode_t mode = FMODE_READ;
  649. int error = 0;
  650. if (!(flags & MS_RDONLY))
  651. mode |= FMODE_WRITE;
  652. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  653. if (IS_ERR(bdev))
  654. return PTR_ERR(bdev);
  655. /*
  656. * once the super is inserted into the list by sget, s_umount
  657. * will protect the lockfs code from trying to start a snapshot
  658. * while we are mounting
  659. */
  660. down(&bdev->bd_mount_sem);
  661. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  662. up(&bdev->bd_mount_sem);
  663. if (IS_ERR(s))
  664. goto error_s;
  665. if (s->s_root) {
  666. if ((flags ^ s->s_flags) & MS_RDONLY) {
  667. deactivate_locked_super(s);
  668. error = -EBUSY;
  669. goto error_bdev;
  670. }
  671. close_bdev_exclusive(bdev, mode);
  672. } else {
  673. char b[BDEVNAME_SIZE];
  674. s->s_flags = flags;
  675. s->s_mode = mode;
  676. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  677. sb_set_blocksize(s, block_size(bdev));
  678. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  679. if (error) {
  680. deactivate_locked_super(s);
  681. goto error;
  682. }
  683. s->s_flags |= MS_ACTIVE;
  684. bdev->bd_super = s;
  685. }
  686. simple_set_mnt(mnt, s);
  687. return 0;
  688. error_s:
  689. error = PTR_ERR(s);
  690. error_bdev:
  691. close_bdev_exclusive(bdev, mode);
  692. error:
  693. return error;
  694. }
  695. EXPORT_SYMBOL(get_sb_bdev);
  696. void kill_block_super(struct super_block *sb)
  697. {
  698. struct block_device *bdev = sb->s_bdev;
  699. fmode_t mode = sb->s_mode;
  700. bdev->bd_super = NULL;
  701. generic_shutdown_super(sb);
  702. sync_blockdev(bdev);
  703. close_bdev_exclusive(bdev, mode);
  704. }
  705. EXPORT_SYMBOL(kill_block_super);
  706. #endif
  707. int get_sb_nodev(struct file_system_type *fs_type,
  708. int flags, void *data,
  709. int (*fill_super)(struct super_block *, void *, int),
  710. struct vfsmount *mnt)
  711. {
  712. int error;
  713. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  714. if (IS_ERR(s))
  715. return PTR_ERR(s);
  716. s->s_flags = flags;
  717. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  718. if (error) {
  719. deactivate_locked_super(s);
  720. return error;
  721. }
  722. s->s_flags |= MS_ACTIVE;
  723. simple_set_mnt(mnt, s);
  724. return 0;
  725. }
  726. EXPORT_SYMBOL(get_sb_nodev);
  727. static int compare_single(struct super_block *s, void *p)
  728. {
  729. return 1;
  730. }
  731. int get_sb_single(struct file_system_type *fs_type,
  732. int flags, void *data,
  733. int (*fill_super)(struct super_block *, void *, int),
  734. struct vfsmount *mnt)
  735. {
  736. struct super_block *s;
  737. int error;
  738. s = sget(fs_type, compare_single, set_anon_super, NULL);
  739. if (IS_ERR(s))
  740. return PTR_ERR(s);
  741. if (!s->s_root) {
  742. s->s_flags = flags;
  743. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  744. if (error) {
  745. deactivate_locked_super(s);
  746. return error;
  747. }
  748. s->s_flags |= MS_ACTIVE;
  749. }
  750. do_remount_sb(s, flags, data, 0);
  751. simple_set_mnt(mnt, s);
  752. return 0;
  753. }
  754. EXPORT_SYMBOL(get_sb_single);
  755. struct vfsmount *
  756. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  757. {
  758. struct vfsmount *mnt;
  759. char *secdata = NULL;
  760. int error;
  761. if (!type)
  762. return ERR_PTR(-ENODEV);
  763. error = -ENOMEM;
  764. mnt = alloc_vfsmnt(name);
  765. if (!mnt)
  766. goto out;
  767. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  768. secdata = alloc_secdata();
  769. if (!secdata)
  770. goto out_mnt;
  771. error = security_sb_copy_data(data, secdata);
  772. if (error)
  773. goto out_free_secdata;
  774. }
  775. error = type->get_sb(type, flags, name, data, mnt);
  776. if (error < 0)
  777. goto out_free_secdata;
  778. BUG_ON(!mnt->mnt_sb);
  779. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  780. if (error)
  781. goto out_sb;
  782. mnt->mnt_mountpoint = mnt->mnt_root;
  783. mnt->mnt_parent = mnt;
  784. up_write(&mnt->mnt_sb->s_umount);
  785. free_secdata(secdata);
  786. return mnt;
  787. out_sb:
  788. dput(mnt->mnt_root);
  789. deactivate_locked_super(mnt->mnt_sb);
  790. out_free_secdata:
  791. free_secdata(secdata);
  792. out_mnt:
  793. free_vfsmnt(mnt);
  794. out:
  795. return ERR_PTR(error);
  796. }
  797. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  798. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  799. {
  800. int err;
  801. const char *subtype = strchr(fstype, '.');
  802. if (subtype) {
  803. subtype++;
  804. err = -EINVAL;
  805. if (!subtype[0])
  806. goto err;
  807. } else
  808. subtype = "";
  809. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  810. err = -ENOMEM;
  811. if (!mnt->mnt_sb->s_subtype)
  812. goto err;
  813. return mnt;
  814. err:
  815. mntput(mnt);
  816. return ERR_PTR(err);
  817. }
  818. struct vfsmount *
  819. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  820. {
  821. struct file_system_type *type = get_fs_type(fstype);
  822. struct vfsmount *mnt;
  823. if (!type)
  824. return ERR_PTR(-ENODEV);
  825. mnt = vfs_kern_mount(type, flags, name, data);
  826. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  827. !mnt->mnt_sb->s_subtype)
  828. mnt = fs_set_subtype(mnt, fstype);
  829. put_filesystem(type);
  830. return mnt;
  831. }
  832. EXPORT_SYMBOL_GPL(do_kern_mount);
  833. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  834. {
  835. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  836. }
  837. EXPORT_SYMBOL_GPL(kern_mount_data);