super.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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/config.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/acct.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/quotaops.h>
  30. #include <linux/namei.h>
  31. #include <linux/buffer_head.h> /* for fsync_super() */
  32. #include <linux/mount.h>
  33. #include <linux/security.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/vfs.h>
  36. #include <linux/writeback.h> /* for the emergency remount stuff */
  37. #include <linux/idr.h>
  38. #include <linux/kobject.h>
  39. #include <asm/uaccess.h>
  40. void get_filesystem(struct file_system_type *fs);
  41. void put_filesystem(struct file_system_type *fs);
  42. struct file_system_type *get_fs_type(const char *name);
  43. LIST_HEAD(super_blocks);
  44. DEFINE_SPINLOCK(sb_lock);
  45. /**
  46. * alloc_super - create new superblock
  47. *
  48. * Allocates and initializes a new &struct super_block. alloc_super()
  49. * returns a pointer new superblock or %NULL if allocation had failed.
  50. */
  51. static struct super_block *alloc_super(void)
  52. {
  53. struct super_block *s = kmalloc(sizeof(struct super_block), GFP_USER);
  54. static struct super_operations default_op;
  55. if (s) {
  56. memset(s, 0, sizeof(struct super_block));
  57. if (security_sb_alloc(s)) {
  58. kfree(s);
  59. s = NULL;
  60. goto out;
  61. }
  62. INIT_LIST_HEAD(&s->s_dirty);
  63. INIT_LIST_HEAD(&s->s_io);
  64. INIT_LIST_HEAD(&s->s_files);
  65. INIT_LIST_HEAD(&s->s_instances);
  66. INIT_HLIST_HEAD(&s->s_anon);
  67. INIT_LIST_HEAD(&s->s_inodes);
  68. init_rwsem(&s->s_umount);
  69. sema_init(&s->s_lock, 1);
  70. down_write(&s->s_umount);
  71. s->s_count = S_BIAS;
  72. atomic_set(&s->s_active, 1);
  73. sema_init(&s->s_vfs_rename_sem,1);
  74. sema_init(&s->s_dquot.dqio_sem, 1);
  75. sema_init(&s->s_dquot.dqonoff_sem, 1);
  76. init_rwsem(&s->s_dquot.dqptr_sem);
  77. init_waitqueue_head(&s->s_wait_unfrozen);
  78. s->s_maxbytes = MAX_NON_LFS;
  79. s->dq_op = sb_dquot_ops;
  80. s->s_qcop = sb_quotactl_ops;
  81. s->s_op = &default_op;
  82. s->s_time_gran = 1000000000;
  83. }
  84. out:
  85. return s;
  86. }
  87. /**
  88. * destroy_super - frees a superblock
  89. * @s: superblock to free
  90. *
  91. * Frees a superblock.
  92. */
  93. static inline void destroy_super(struct super_block *s)
  94. {
  95. security_sb_free(s);
  96. kfree(s);
  97. }
  98. /* Superblock refcounting */
  99. /*
  100. * Drop a superblock's refcount. Returns non-zero if the superblock was
  101. * destroyed. The caller must hold sb_lock.
  102. */
  103. int __put_super(struct super_block *sb)
  104. {
  105. int ret = 0;
  106. if (!--sb->s_count) {
  107. destroy_super(sb);
  108. ret = 1;
  109. }
  110. return ret;
  111. }
  112. /*
  113. * Drop a superblock's refcount.
  114. * Returns non-zero if the superblock is about to be destroyed and
  115. * at least is already removed from super_blocks list, so if we are
  116. * making a loop through super blocks then we need to restart.
  117. * The caller must hold sb_lock.
  118. */
  119. int __put_super_and_need_restart(struct super_block *sb)
  120. {
  121. /* check for race with generic_shutdown_super() */
  122. if (list_empty(&sb->s_list)) {
  123. /* super block is removed, need to restart... */
  124. __put_super(sb);
  125. return 1;
  126. }
  127. /* can't be the last, since s_list is still in use */
  128. sb->s_count--;
  129. BUG_ON(sb->s_count == 0);
  130. return 0;
  131. }
  132. /**
  133. * put_super - drop a temporary reference to superblock
  134. * @sb: superblock in question
  135. *
  136. * Drops a temporary reference, frees superblock if there's no
  137. * references left.
  138. */
  139. static void put_super(struct super_block *sb)
  140. {
  141. spin_lock(&sb_lock);
  142. __put_super(sb);
  143. spin_unlock(&sb_lock);
  144. }
  145. /**
  146. * deactivate_super - drop an active reference to superblock
  147. * @s: superblock to deactivate
  148. *
  149. * Drops an active reference to superblock, acquiring a temprory one if
  150. * there is no active references left. In that case we lock superblock,
  151. * tell fs driver to shut it down and drop the temporary reference we
  152. * had just acquired.
  153. */
  154. void deactivate_super(struct super_block *s)
  155. {
  156. struct file_system_type *fs = s->s_type;
  157. if (atomic_dec_and_lock(&s->s_active, &sb_lock)) {
  158. s->s_count -= S_BIAS-1;
  159. spin_unlock(&sb_lock);
  160. down_write(&s->s_umount);
  161. fs->kill_sb(s);
  162. put_filesystem(fs);
  163. put_super(s);
  164. }
  165. }
  166. EXPORT_SYMBOL(deactivate_super);
  167. /**
  168. * grab_super - acquire an active reference
  169. * @s: reference we are trying to make active
  170. *
  171. * Tries to acquire an active reference. grab_super() is used when we
  172. * had just found a superblock in super_blocks or fs_type->fs_supers
  173. * and want to turn it into a full-blown active reference. grab_super()
  174. * is called with sb_lock held and drops it. Returns 1 in case of
  175. * success, 0 if we had failed (superblock contents was already dead or
  176. * dying when grab_super() had been called).
  177. */
  178. static int grab_super(struct super_block *s)
  179. {
  180. s->s_count++;
  181. spin_unlock(&sb_lock);
  182. down_write(&s->s_umount);
  183. if (s->s_root) {
  184. spin_lock(&sb_lock);
  185. if (s->s_count > S_BIAS) {
  186. atomic_inc(&s->s_active);
  187. s->s_count--;
  188. spin_unlock(&sb_lock);
  189. return 1;
  190. }
  191. spin_unlock(&sb_lock);
  192. }
  193. up_write(&s->s_umount);
  194. put_super(s);
  195. yield();
  196. return 0;
  197. }
  198. /**
  199. * generic_shutdown_super - common helper for ->kill_sb()
  200. * @sb: superblock to kill
  201. *
  202. * generic_shutdown_super() does all fs-independent work on superblock
  203. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  204. * that need destruction out of superblock, call generic_shutdown_super()
  205. * and release aforementioned objects. Note: dentries and inodes _are_
  206. * taken care of and do not need specific handling.
  207. */
  208. void generic_shutdown_super(struct super_block *sb)
  209. {
  210. struct dentry *root = sb->s_root;
  211. struct super_operations *sop = sb->s_op;
  212. if (root) {
  213. sb->s_root = NULL;
  214. shrink_dcache_parent(root);
  215. shrink_dcache_anon(&sb->s_anon);
  216. dput(root);
  217. fsync_super(sb);
  218. lock_super(sb);
  219. sb->s_flags &= ~MS_ACTIVE;
  220. /* bad name - it should be evict_inodes() */
  221. invalidate_inodes(sb);
  222. lock_kernel();
  223. if (sop->write_super && sb->s_dirt)
  224. sop->write_super(sb);
  225. if (sop->put_super)
  226. sop->put_super(sb);
  227. /* Forget any remaining inodes */
  228. if (invalidate_inodes(sb)) {
  229. printk("VFS: Busy inodes after unmount. "
  230. "Self-destruct in 5 seconds. Have a nice day...\n");
  231. }
  232. unlock_kernel();
  233. unlock_super(sb);
  234. }
  235. spin_lock(&sb_lock);
  236. /* should be initialized for __put_super_and_need_restart() */
  237. list_del_init(&sb->s_list);
  238. list_del(&sb->s_instances);
  239. spin_unlock(&sb_lock);
  240. up_write(&sb->s_umount);
  241. }
  242. EXPORT_SYMBOL(generic_shutdown_super);
  243. /**
  244. * sget - find or create a superblock
  245. * @type: filesystem type superblock should belong to
  246. * @test: comparison callback
  247. * @set: setup callback
  248. * @data: argument to each of them
  249. */
  250. struct super_block *sget(struct file_system_type *type,
  251. int (*test)(struct super_block *,void *),
  252. int (*set)(struct super_block *,void *),
  253. void *data)
  254. {
  255. struct super_block *s = NULL;
  256. struct list_head *p;
  257. int err;
  258. retry:
  259. spin_lock(&sb_lock);
  260. if (test) list_for_each(p, &type->fs_supers) {
  261. struct super_block *old;
  262. old = list_entry(p, struct super_block, s_instances);
  263. if (!test(old, data))
  264. continue;
  265. if (!grab_super(old))
  266. goto retry;
  267. if (s)
  268. destroy_super(s);
  269. return old;
  270. }
  271. if (!s) {
  272. spin_unlock(&sb_lock);
  273. s = alloc_super();
  274. if (!s)
  275. return ERR_PTR(-ENOMEM);
  276. goto retry;
  277. }
  278. err = set(s, data);
  279. if (err) {
  280. spin_unlock(&sb_lock);
  281. destroy_super(s);
  282. return ERR_PTR(err);
  283. }
  284. s->s_type = type;
  285. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  286. list_add_tail(&s->s_list, &super_blocks);
  287. list_add(&s->s_instances, &type->fs_supers);
  288. spin_unlock(&sb_lock);
  289. get_filesystem(type);
  290. return s;
  291. }
  292. EXPORT_SYMBOL(sget);
  293. void drop_super(struct super_block *sb)
  294. {
  295. up_read(&sb->s_umount);
  296. put_super(sb);
  297. }
  298. EXPORT_SYMBOL(drop_super);
  299. static inline void write_super(struct super_block *sb)
  300. {
  301. lock_super(sb);
  302. if (sb->s_root && sb->s_dirt)
  303. if (sb->s_op->write_super)
  304. sb->s_op->write_super(sb);
  305. unlock_super(sb);
  306. }
  307. /*
  308. * Note: check the dirty flag before waiting, so we don't
  309. * hold up the sync while mounting a device. (The newly
  310. * mounted device won't need syncing.)
  311. */
  312. void sync_supers(void)
  313. {
  314. struct super_block * sb;
  315. restart:
  316. spin_lock(&sb_lock);
  317. sb = sb_entry(super_blocks.next);
  318. while (sb != sb_entry(&super_blocks))
  319. if (sb->s_dirt) {
  320. sb->s_count++;
  321. spin_unlock(&sb_lock);
  322. down_read(&sb->s_umount);
  323. write_super(sb);
  324. drop_super(sb);
  325. goto restart;
  326. } else
  327. sb = sb_entry(sb->s_list.next);
  328. spin_unlock(&sb_lock);
  329. }
  330. /*
  331. * Call the ->sync_fs super_op against all filesytems which are r/w and
  332. * which implement it.
  333. *
  334. * This operation is careful to avoid the livelock which could easily happen
  335. * if two or more filesystems are being continuously dirtied. s_need_sync_fs
  336. * is used only here. We set it against all filesystems and then clear it as
  337. * we sync them. So redirtied filesystems are skipped.
  338. *
  339. * But if process A is currently running sync_filesytems and then process B
  340. * calls sync_filesystems as well, process B will set all the s_need_sync_fs
  341. * flags again, which will cause process A to resync everything. Fix that with
  342. * a local mutex.
  343. *
  344. * (Fabian) Avoid sync_fs with clean fs & wait mode 0
  345. */
  346. void sync_filesystems(int wait)
  347. {
  348. struct super_block *sb;
  349. static DECLARE_MUTEX(mutex);
  350. down(&mutex); /* Could be down_interruptible */
  351. spin_lock(&sb_lock);
  352. for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
  353. sb = sb_entry(sb->s_list.next)) {
  354. if (!sb->s_op->sync_fs)
  355. continue;
  356. if (sb->s_flags & MS_RDONLY)
  357. continue;
  358. sb->s_need_sync_fs = 1;
  359. }
  360. spin_unlock(&sb_lock);
  361. restart:
  362. spin_lock(&sb_lock);
  363. for (sb = sb_entry(super_blocks.next); sb != sb_entry(&super_blocks);
  364. sb = sb_entry(sb->s_list.next)) {
  365. if (!sb->s_need_sync_fs)
  366. continue;
  367. sb->s_need_sync_fs = 0;
  368. if (sb->s_flags & MS_RDONLY)
  369. continue; /* hm. Was remounted r/o meanwhile */
  370. sb->s_count++;
  371. spin_unlock(&sb_lock);
  372. down_read(&sb->s_umount);
  373. if (sb->s_root && (wait || sb->s_dirt))
  374. sb->s_op->sync_fs(sb, wait);
  375. drop_super(sb);
  376. goto restart;
  377. }
  378. spin_unlock(&sb_lock);
  379. up(&mutex);
  380. }
  381. /**
  382. * get_super - get the superblock of a device
  383. * @bdev: device to get the superblock for
  384. *
  385. * Scans the superblock list and finds the superblock of the file system
  386. * mounted on the device given. %NULL is returned if no match is found.
  387. */
  388. struct super_block * get_super(struct block_device *bdev)
  389. {
  390. struct list_head *p;
  391. if (!bdev)
  392. return NULL;
  393. rescan:
  394. spin_lock(&sb_lock);
  395. list_for_each(p, &super_blocks) {
  396. struct super_block *s = sb_entry(p);
  397. if (s->s_bdev == bdev) {
  398. s->s_count++;
  399. spin_unlock(&sb_lock);
  400. down_read(&s->s_umount);
  401. if (s->s_root)
  402. return s;
  403. drop_super(s);
  404. goto rescan;
  405. }
  406. }
  407. spin_unlock(&sb_lock);
  408. return NULL;
  409. }
  410. EXPORT_SYMBOL(get_super);
  411. struct super_block * user_get_super(dev_t dev)
  412. {
  413. struct list_head *p;
  414. rescan:
  415. spin_lock(&sb_lock);
  416. list_for_each(p, &super_blocks) {
  417. struct super_block *s = sb_entry(p);
  418. if (s->s_dev == dev) {
  419. s->s_count++;
  420. spin_unlock(&sb_lock);
  421. down_read(&s->s_umount);
  422. if (s->s_root)
  423. return s;
  424. drop_super(s);
  425. goto rescan;
  426. }
  427. }
  428. spin_unlock(&sb_lock);
  429. return NULL;
  430. }
  431. EXPORT_SYMBOL(user_get_super);
  432. asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
  433. {
  434. struct super_block *s;
  435. struct ustat tmp;
  436. struct kstatfs sbuf;
  437. int err = -EINVAL;
  438. s = user_get_super(new_decode_dev(dev));
  439. if (s == NULL)
  440. goto out;
  441. err = vfs_statfs(s, &sbuf);
  442. drop_super(s);
  443. if (err)
  444. goto out;
  445. memset(&tmp,0,sizeof(struct ustat));
  446. tmp.f_tfree = sbuf.f_bfree;
  447. tmp.f_tinode = sbuf.f_ffree;
  448. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  449. out:
  450. return err;
  451. }
  452. /**
  453. * mark_files_ro
  454. * @sb: superblock in question
  455. *
  456. * All files are marked read/only. We don't care about pending
  457. * delete files so this should be used in 'force' mode only
  458. */
  459. static void mark_files_ro(struct super_block *sb)
  460. {
  461. struct file *f;
  462. file_list_lock();
  463. list_for_each_entry(f, &sb->s_files, f_list) {
  464. if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
  465. f->f_mode &= ~FMODE_WRITE;
  466. }
  467. file_list_unlock();
  468. }
  469. /**
  470. * do_remount_sb - asks filesystem to change mount options.
  471. * @sb: superblock in question
  472. * @flags: numeric part of options
  473. * @data: the rest of options
  474. * @force: whether or not to force the change
  475. *
  476. * Alters the mount options of a mounted file system.
  477. */
  478. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  479. {
  480. int retval;
  481. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  482. return -EACCES;
  483. if (flags & MS_RDONLY)
  484. acct_auto_close(sb);
  485. shrink_dcache_sb(sb);
  486. fsync_super(sb);
  487. /* If we are remounting RDONLY and current sb is read/write,
  488. make sure there are no rw files opened */
  489. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  490. if (force)
  491. mark_files_ro(sb);
  492. else if (!fs_may_remount_ro(sb))
  493. return -EBUSY;
  494. }
  495. if (sb->s_op->remount_fs) {
  496. lock_super(sb);
  497. retval = sb->s_op->remount_fs(sb, &flags, data);
  498. unlock_super(sb);
  499. if (retval)
  500. return retval;
  501. }
  502. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  503. return 0;
  504. }
  505. static void do_emergency_remount(unsigned long foo)
  506. {
  507. struct super_block *sb;
  508. spin_lock(&sb_lock);
  509. list_for_each_entry(sb, &super_blocks, s_list) {
  510. sb->s_count++;
  511. spin_unlock(&sb_lock);
  512. down_read(&sb->s_umount);
  513. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  514. /*
  515. * ->remount_fs needs lock_kernel().
  516. *
  517. * What lock protects sb->s_flags??
  518. */
  519. lock_kernel();
  520. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  521. unlock_kernel();
  522. }
  523. drop_super(sb);
  524. spin_lock(&sb_lock);
  525. }
  526. spin_unlock(&sb_lock);
  527. printk("Emergency Remount complete\n");
  528. }
  529. void emergency_remount(void)
  530. {
  531. pdflush_operation(do_emergency_remount, 0);
  532. }
  533. /*
  534. * Unnamed block devices are dummy devices used by virtual
  535. * filesystems which don't use real block-devices. -- jrs
  536. */
  537. static struct idr unnamed_dev_idr;
  538. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  539. int set_anon_super(struct super_block *s, void *data)
  540. {
  541. int dev;
  542. int error;
  543. retry:
  544. if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
  545. return -ENOMEM;
  546. spin_lock(&unnamed_dev_lock);
  547. error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
  548. spin_unlock(&unnamed_dev_lock);
  549. if (error == -EAGAIN)
  550. /* We raced and lost with another CPU. */
  551. goto retry;
  552. else if (error)
  553. return -EAGAIN;
  554. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  555. spin_lock(&unnamed_dev_lock);
  556. idr_remove(&unnamed_dev_idr, dev);
  557. spin_unlock(&unnamed_dev_lock);
  558. return -EMFILE;
  559. }
  560. s->s_dev = MKDEV(0, dev & MINORMASK);
  561. return 0;
  562. }
  563. EXPORT_SYMBOL(set_anon_super);
  564. void kill_anon_super(struct super_block *sb)
  565. {
  566. int slot = MINOR(sb->s_dev);
  567. generic_shutdown_super(sb);
  568. spin_lock(&unnamed_dev_lock);
  569. idr_remove(&unnamed_dev_idr, slot);
  570. spin_unlock(&unnamed_dev_lock);
  571. }
  572. EXPORT_SYMBOL(kill_anon_super);
  573. void __init unnamed_dev_init(void)
  574. {
  575. idr_init(&unnamed_dev_idr);
  576. }
  577. void kill_litter_super(struct super_block *sb)
  578. {
  579. if (sb->s_root)
  580. d_genocide(sb->s_root);
  581. kill_anon_super(sb);
  582. }
  583. EXPORT_SYMBOL(kill_litter_super);
  584. static int set_bdev_super(struct super_block *s, void *data)
  585. {
  586. s->s_bdev = data;
  587. s->s_dev = s->s_bdev->bd_dev;
  588. return 0;
  589. }
  590. static int test_bdev_super(struct super_block *s, void *data)
  591. {
  592. return (void *)s->s_bdev == data;
  593. }
  594. static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
  595. {
  596. if (bdev->bd_disk) {
  597. if (bdev->bd_part)
  598. kobject_uevent(&bdev->bd_part->kobj, action, NULL);
  599. else
  600. kobject_uevent(&bdev->bd_disk->kobj, action, NULL);
  601. }
  602. }
  603. struct super_block *get_sb_bdev(struct file_system_type *fs_type,
  604. int flags, const char *dev_name, void *data,
  605. int (*fill_super)(struct super_block *, void *, int))
  606. {
  607. struct block_device *bdev;
  608. struct super_block *s;
  609. int error = 0;
  610. bdev = open_bdev_excl(dev_name, flags, fs_type);
  611. if (IS_ERR(bdev))
  612. return (struct super_block *)bdev;
  613. /*
  614. * once the super is inserted into the list by sget, s_umount
  615. * will protect the lockfs code from trying to start a snapshot
  616. * while we are mounting
  617. */
  618. down(&bdev->bd_mount_sem);
  619. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  620. up(&bdev->bd_mount_sem);
  621. if (IS_ERR(s))
  622. goto out;
  623. if (s->s_root) {
  624. if ((flags ^ s->s_flags) & MS_RDONLY) {
  625. up_write(&s->s_umount);
  626. deactivate_super(s);
  627. s = ERR_PTR(-EBUSY);
  628. }
  629. goto out;
  630. } else {
  631. char b[BDEVNAME_SIZE];
  632. s->s_flags = flags;
  633. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  634. s->s_old_blocksize = block_size(bdev);
  635. sb_set_blocksize(s, s->s_old_blocksize);
  636. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  637. if (error) {
  638. up_write(&s->s_umount);
  639. deactivate_super(s);
  640. s = ERR_PTR(error);
  641. } else {
  642. s->s_flags |= MS_ACTIVE;
  643. bdev_uevent(bdev, KOBJ_MOUNT);
  644. }
  645. }
  646. return s;
  647. out:
  648. close_bdev_excl(bdev);
  649. return s;
  650. }
  651. EXPORT_SYMBOL(get_sb_bdev);
  652. void kill_block_super(struct super_block *sb)
  653. {
  654. struct block_device *bdev = sb->s_bdev;
  655. bdev_uevent(bdev, KOBJ_UMOUNT);
  656. generic_shutdown_super(sb);
  657. sync_blockdev(bdev);
  658. close_bdev_excl(bdev);
  659. }
  660. EXPORT_SYMBOL(kill_block_super);
  661. struct super_block *get_sb_nodev(struct file_system_type *fs_type,
  662. int flags, void *data,
  663. int (*fill_super)(struct super_block *, void *, int))
  664. {
  665. int error;
  666. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  667. if (IS_ERR(s))
  668. return s;
  669. s->s_flags = flags;
  670. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  671. if (error) {
  672. up_write(&s->s_umount);
  673. deactivate_super(s);
  674. return ERR_PTR(error);
  675. }
  676. s->s_flags |= MS_ACTIVE;
  677. return s;
  678. }
  679. EXPORT_SYMBOL(get_sb_nodev);
  680. static int compare_single(struct super_block *s, void *p)
  681. {
  682. return 1;
  683. }
  684. struct super_block *get_sb_single(struct file_system_type *fs_type,
  685. int flags, void *data,
  686. int (*fill_super)(struct super_block *, void *, int))
  687. {
  688. struct super_block *s;
  689. int error;
  690. s = sget(fs_type, compare_single, set_anon_super, NULL);
  691. if (IS_ERR(s))
  692. return s;
  693. if (!s->s_root) {
  694. s->s_flags = flags;
  695. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  696. if (error) {
  697. up_write(&s->s_umount);
  698. deactivate_super(s);
  699. return ERR_PTR(error);
  700. }
  701. s->s_flags |= MS_ACTIVE;
  702. }
  703. do_remount_sb(s, flags, data, 0);
  704. return s;
  705. }
  706. EXPORT_SYMBOL(get_sb_single);
  707. struct vfsmount *
  708. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  709. {
  710. struct file_system_type *type = get_fs_type(fstype);
  711. struct super_block *sb = ERR_PTR(-ENOMEM);
  712. struct vfsmount *mnt;
  713. int error;
  714. char *secdata = NULL;
  715. if (!type)
  716. return ERR_PTR(-ENODEV);
  717. mnt = alloc_vfsmnt(name);
  718. if (!mnt)
  719. goto out;
  720. if (data) {
  721. secdata = alloc_secdata();
  722. if (!secdata) {
  723. sb = ERR_PTR(-ENOMEM);
  724. goto out_mnt;
  725. }
  726. error = security_sb_copy_data(type, data, secdata);
  727. if (error) {
  728. sb = ERR_PTR(error);
  729. goto out_free_secdata;
  730. }
  731. }
  732. sb = type->get_sb(type, flags, name, data);
  733. if (IS_ERR(sb))
  734. goto out_free_secdata;
  735. error = security_sb_kern_mount(sb, secdata);
  736. if (error)
  737. goto out_sb;
  738. mnt->mnt_sb = sb;
  739. mnt->mnt_root = dget(sb->s_root);
  740. mnt->mnt_mountpoint = sb->s_root;
  741. mnt->mnt_parent = mnt;
  742. mnt->mnt_namespace = current->namespace;
  743. up_write(&sb->s_umount);
  744. free_secdata(secdata);
  745. put_filesystem(type);
  746. return mnt;
  747. out_sb:
  748. up_write(&sb->s_umount);
  749. deactivate_super(sb);
  750. sb = ERR_PTR(error);
  751. out_free_secdata:
  752. free_secdata(secdata);
  753. out_mnt:
  754. free_vfsmnt(mnt);
  755. out:
  756. put_filesystem(type);
  757. return (struct vfsmount *)sb;
  758. }
  759. EXPORT_SYMBOL_GPL(do_kern_mount);
  760. struct vfsmount *kern_mount(struct file_system_type *type)
  761. {
  762. return do_kern_mount(type->name, 0, type->name, NULL);
  763. }
  764. EXPORT_SYMBOL(kern_mount);