super.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. mutex_init(&s->s_lock);
  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. DQUOT_OFF(s);
  161. down_write(&s->s_umount);
  162. fs->kill_sb(s);
  163. put_filesystem(fs);
  164. put_super(s);
  165. }
  166. }
  167. EXPORT_SYMBOL(deactivate_super);
  168. /**
  169. * grab_super - acquire an active reference
  170. * @s: reference we are trying to make active
  171. *
  172. * Tries to acquire an active reference. grab_super() is used when we
  173. * had just found a superblock in super_blocks or fs_type->fs_supers
  174. * and want to turn it into a full-blown active reference. grab_super()
  175. * is called with sb_lock held and drops it. Returns 1 in case of
  176. * success, 0 if we had failed (superblock contents was already dead or
  177. * dying when grab_super() had been called).
  178. */
  179. static int grab_super(struct super_block *s)
  180. {
  181. s->s_count++;
  182. spin_unlock(&sb_lock);
  183. down_write(&s->s_umount);
  184. if (s->s_root) {
  185. spin_lock(&sb_lock);
  186. if (s->s_count > S_BIAS) {
  187. atomic_inc(&s->s_active);
  188. s->s_count--;
  189. spin_unlock(&sb_lock);
  190. return 1;
  191. }
  192. spin_unlock(&sb_lock);
  193. }
  194. up_write(&s->s_umount);
  195. put_super(s);
  196. yield();
  197. return 0;
  198. }
  199. /**
  200. * generic_shutdown_super - common helper for ->kill_sb()
  201. * @sb: superblock to kill
  202. *
  203. * generic_shutdown_super() does all fs-independent work on superblock
  204. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  205. * that need destruction out of superblock, call generic_shutdown_super()
  206. * and release aforementioned objects. Note: dentries and inodes _are_
  207. * taken care of and do not need specific handling.
  208. */
  209. void generic_shutdown_super(struct super_block *sb)
  210. {
  211. struct dentry *root = sb->s_root;
  212. struct super_operations *sop = sb->s_op;
  213. if (root) {
  214. sb->s_root = NULL;
  215. shrink_dcache_parent(root);
  216. shrink_dcache_anon(&sb->s_anon);
  217. dput(root);
  218. fsync_super(sb);
  219. lock_super(sb);
  220. sb->s_flags &= ~MS_ACTIVE;
  221. /* bad name - it should be evict_inodes() */
  222. invalidate_inodes(sb);
  223. lock_kernel();
  224. if (sop->write_super && sb->s_dirt)
  225. sop->write_super(sb);
  226. if (sop->put_super)
  227. sop->put_super(sb);
  228. /* Forget any remaining inodes */
  229. if (invalidate_inodes(sb)) {
  230. printk("VFS: Busy inodes after unmount. "
  231. "Self-destruct in 5 seconds. Have a nice day...\n");
  232. }
  233. unlock_kernel();
  234. unlock_super(sb);
  235. }
  236. spin_lock(&sb_lock);
  237. /* should be initialized for __put_super_and_need_restart() */
  238. list_del_init(&sb->s_list);
  239. list_del(&sb->s_instances);
  240. spin_unlock(&sb_lock);
  241. up_write(&sb->s_umount);
  242. }
  243. EXPORT_SYMBOL(generic_shutdown_super);
  244. /**
  245. * sget - find or create a superblock
  246. * @type: filesystem type superblock should belong to
  247. * @test: comparison callback
  248. * @set: setup callback
  249. * @data: argument to each of them
  250. */
  251. struct super_block *sget(struct file_system_type *type,
  252. int (*test)(struct super_block *,void *),
  253. int (*set)(struct super_block *,void *),
  254. void *data)
  255. {
  256. struct super_block *s = NULL;
  257. struct list_head *p;
  258. int err;
  259. retry:
  260. spin_lock(&sb_lock);
  261. if (test) list_for_each(p, &type->fs_supers) {
  262. struct super_block *old;
  263. old = list_entry(p, struct super_block, s_instances);
  264. if (!test(old, data))
  265. continue;
  266. if (!grab_super(old))
  267. goto retry;
  268. if (s)
  269. destroy_super(s);
  270. return old;
  271. }
  272. if (!s) {
  273. spin_unlock(&sb_lock);
  274. s = alloc_super();
  275. if (!s)
  276. return ERR_PTR(-ENOMEM);
  277. goto retry;
  278. }
  279. err = set(s, data);
  280. if (err) {
  281. spin_unlock(&sb_lock);
  282. destroy_super(s);
  283. return ERR_PTR(err);
  284. }
  285. s->s_type = type;
  286. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  287. list_add_tail(&s->s_list, &super_blocks);
  288. list_add(&s->s_instances, &type->fs_supers);
  289. spin_unlock(&sb_lock);
  290. get_filesystem(type);
  291. return s;
  292. }
  293. EXPORT_SYMBOL(sget);
  294. void drop_super(struct super_block *sb)
  295. {
  296. up_read(&sb->s_umount);
  297. put_super(sb);
  298. }
  299. EXPORT_SYMBOL(drop_super);
  300. static inline void write_super(struct super_block *sb)
  301. {
  302. lock_super(sb);
  303. if (sb->s_root && sb->s_dirt)
  304. if (sb->s_op->write_super)
  305. sb->s_op->write_super(sb);
  306. unlock_super(sb);
  307. }
  308. /*
  309. * Note: check the dirty flag before waiting, so we don't
  310. * hold up the sync while mounting a device. (The newly
  311. * mounted device won't need syncing.)
  312. */
  313. void sync_supers(void)
  314. {
  315. struct super_block *sb;
  316. spin_lock(&sb_lock);
  317. restart:
  318. list_for_each_entry(sb, &super_blocks, s_list) {
  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. up_read(&sb->s_umount);
  325. spin_lock(&sb_lock);
  326. if (__put_super_and_need_restart(sb))
  327. goto restart;
  328. }
  329. }
  330. spin_unlock(&sb_lock);
  331. }
  332. /*
  333. * Call the ->sync_fs super_op against all filesytems which are r/w and
  334. * which implement it.
  335. *
  336. * This operation is careful to avoid the livelock which could easily happen
  337. * if two or more filesystems are being continuously dirtied. s_need_sync_fs
  338. * is used only here. We set it against all filesystems and then clear it as
  339. * we sync them. So redirtied filesystems are skipped.
  340. *
  341. * But if process A is currently running sync_filesytems and then process B
  342. * calls sync_filesystems as well, process B will set all the s_need_sync_fs
  343. * flags again, which will cause process A to resync everything. Fix that with
  344. * a local mutex.
  345. *
  346. * (Fabian) Avoid sync_fs with clean fs & wait mode 0
  347. */
  348. void sync_filesystems(int wait)
  349. {
  350. struct super_block *sb;
  351. static DECLARE_MUTEX(mutex);
  352. down(&mutex); /* Could be down_interruptible */
  353. spin_lock(&sb_lock);
  354. list_for_each_entry(sb, &super_blocks, s_list) {
  355. if (!sb->s_op->sync_fs)
  356. continue;
  357. if (sb->s_flags & MS_RDONLY)
  358. continue;
  359. sb->s_need_sync_fs = 1;
  360. }
  361. restart:
  362. list_for_each_entry(sb, &super_blocks, s_list) {
  363. if (!sb->s_need_sync_fs)
  364. continue;
  365. sb->s_need_sync_fs = 0;
  366. if (sb->s_flags & MS_RDONLY)
  367. continue; /* hm. Was remounted r/o meanwhile */
  368. sb->s_count++;
  369. spin_unlock(&sb_lock);
  370. down_read(&sb->s_umount);
  371. if (sb->s_root && (wait || sb->s_dirt))
  372. sb->s_op->sync_fs(sb, wait);
  373. up_read(&sb->s_umount);
  374. /* restart only when sb is no longer on the list */
  375. spin_lock(&sb_lock);
  376. if (__put_super_and_need_restart(sb))
  377. goto restart;
  378. }
  379. spin_unlock(&sb_lock);
  380. up(&mutex);
  381. }
  382. /**
  383. * get_super - get the superblock of a device
  384. * @bdev: device to get the superblock for
  385. *
  386. * Scans the superblock list and finds the superblock of the file system
  387. * mounted on the device given. %NULL is returned if no match is found.
  388. */
  389. struct super_block * get_super(struct block_device *bdev)
  390. {
  391. struct super_block *sb;
  392. if (!bdev)
  393. return NULL;
  394. spin_lock(&sb_lock);
  395. rescan:
  396. list_for_each_entry(sb, &super_blocks, s_list) {
  397. if (sb->s_bdev == bdev) {
  398. sb->s_count++;
  399. spin_unlock(&sb_lock);
  400. down_read(&sb->s_umount);
  401. if (sb->s_root)
  402. return sb;
  403. up_read(&sb->s_umount);
  404. /* restart only when sb is no longer on the list */
  405. spin_lock(&sb_lock);
  406. if (__put_super_and_need_restart(sb))
  407. goto rescan;
  408. }
  409. }
  410. spin_unlock(&sb_lock);
  411. return NULL;
  412. }
  413. EXPORT_SYMBOL(get_super);
  414. struct super_block * user_get_super(dev_t dev)
  415. {
  416. struct super_block *sb;
  417. spin_lock(&sb_lock);
  418. rescan:
  419. list_for_each_entry(sb, &super_blocks, s_list) {
  420. if (sb->s_dev == dev) {
  421. sb->s_count++;
  422. spin_unlock(&sb_lock);
  423. down_read(&sb->s_umount);
  424. if (sb->s_root)
  425. return sb;
  426. up_read(&sb->s_umount);
  427. /* restart only when sb is no longer on the list */
  428. spin_lock(&sb_lock);
  429. if (__put_super_and_need_restart(sb))
  430. goto rescan;
  431. }
  432. }
  433. spin_unlock(&sb_lock);
  434. return NULL;
  435. }
  436. asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
  437. {
  438. struct super_block *s;
  439. struct ustat tmp;
  440. struct kstatfs sbuf;
  441. int err = -EINVAL;
  442. s = user_get_super(new_decode_dev(dev));
  443. if (s == NULL)
  444. goto out;
  445. err = vfs_statfs(s, &sbuf);
  446. drop_super(s);
  447. if (err)
  448. goto out;
  449. memset(&tmp,0,sizeof(struct ustat));
  450. tmp.f_tfree = sbuf.f_bfree;
  451. tmp.f_tinode = sbuf.f_ffree;
  452. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  453. out:
  454. return err;
  455. }
  456. /**
  457. * mark_files_ro
  458. * @sb: superblock in question
  459. *
  460. * All files are marked read/only. We don't care about pending
  461. * delete files so this should be used in 'force' mode only
  462. */
  463. static void mark_files_ro(struct super_block *sb)
  464. {
  465. struct file *f;
  466. file_list_lock();
  467. list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
  468. if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
  469. f->f_mode &= ~FMODE_WRITE;
  470. }
  471. file_list_unlock();
  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. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  486. return -EACCES;
  487. if (flags & MS_RDONLY)
  488. acct_auto_close(sb);
  489. shrink_dcache_sb(sb);
  490. fsync_super(sb);
  491. /* If we are remounting RDONLY and current sb is read/write,
  492. make sure there are no rw files opened */
  493. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  494. if (force)
  495. mark_files_ro(sb);
  496. else if (!fs_may_remount_ro(sb))
  497. return -EBUSY;
  498. }
  499. if (sb->s_op->remount_fs) {
  500. lock_super(sb);
  501. retval = sb->s_op->remount_fs(sb, &flags, data);
  502. unlock_super(sb);
  503. if (retval)
  504. return retval;
  505. }
  506. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  507. return 0;
  508. }
  509. static void do_emergency_remount(unsigned long foo)
  510. {
  511. struct super_block *sb;
  512. spin_lock(&sb_lock);
  513. list_for_each_entry(sb, &super_blocks, s_list) {
  514. sb->s_count++;
  515. spin_unlock(&sb_lock);
  516. down_read(&sb->s_umount);
  517. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  518. /*
  519. * ->remount_fs needs lock_kernel().
  520. *
  521. * What lock protects sb->s_flags??
  522. */
  523. lock_kernel();
  524. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  525. unlock_kernel();
  526. }
  527. drop_super(sb);
  528. spin_lock(&sb_lock);
  529. }
  530. spin_unlock(&sb_lock);
  531. printk("Emergency Remount complete\n");
  532. }
  533. void emergency_remount(void)
  534. {
  535. pdflush_operation(do_emergency_remount, 0);
  536. }
  537. /*
  538. * Unnamed block devices are dummy devices used by virtual
  539. * filesystems which don't use real block-devices. -- jrs
  540. */
  541. static struct idr unnamed_dev_idr;
  542. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  543. int set_anon_super(struct super_block *s, void *data)
  544. {
  545. int dev;
  546. int error;
  547. retry:
  548. if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
  549. return -ENOMEM;
  550. spin_lock(&unnamed_dev_lock);
  551. error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
  552. spin_unlock(&unnamed_dev_lock);
  553. if (error == -EAGAIN)
  554. /* We raced and lost with another CPU. */
  555. goto retry;
  556. else if (error)
  557. return -EAGAIN;
  558. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  559. spin_lock(&unnamed_dev_lock);
  560. idr_remove(&unnamed_dev_idr, dev);
  561. spin_unlock(&unnamed_dev_lock);
  562. return -EMFILE;
  563. }
  564. s->s_dev = MKDEV(0, dev & MINORMASK);
  565. return 0;
  566. }
  567. EXPORT_SYMBOL(set_anon_super);
  568. void kill_anon_super(struct super_block *sb)
  569. {
  570. int slot = MINOR(sb->s_dev);
  571. generic_shutdown_super(sb);
  572. spin_lock(&unnamed_dev_lock);
  573. idr_remove(&unnamed_dev_idr, slot);
  574. spin_unlock(&unnamed_dev_lock);
  575. }
  576. EXPORT_SYMBOL(kill_anon_super);
  577. void __init unnamed_dev_init(void)
  578. {
  579. idr_init(&unnamed_dev_idr);
  580. }
  581. void kill_litter_super(struct super_block *sb)
  582. {
  583. if (sb->s_root)
  584. d_genocide(sb->s_root);
  585. kill_anon_super(sb);
  586. }
  587. EXPORT_SYMBOL(kill_litter_super);
  588. static int set_bdev_super(struct super_block *s, void *data)
  589. {
  590. s->s_bdev = data;
  591. s->s_dev = s->s_bdev->bd_dev;
  592. return 0;
  593. }
  594. static int test_bdev_super(struct super_block *s, void *data)
  595. {
  596. return (void *)s->s_bdev == data;
  597. }
  598. struct super_block *get_sb_bdev(struct file_system_type *fs_type,
  599. int flags, const char *dev_name, void *data,
  600. int (*fill_super)(struct super_block *, void *, int))
  601. {
  602. struct block_device *bdev;
  603. struct super_block *s;
  604. int error = 0;
  605. bdev = open_bdev_excl(dev_name, flags, fs_type);
  606. if (IS_ERR(bdev))
  607. return (struct super_block *)bdev;
  608. /*
  609. * once the super is inserted into the list by sget, s_umount
  610. * will protect the lockfs code from trying to start a snapshot
  611. * while we are mounting
  612. */
  613. down(&bdev->bd_mount_sem);
  614. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  615. up(&bdev->bd_mount_sem);
  616. if (IS_ERR(s))
  617. goto out;
  618. if (s->s_root) {
  619. if ((flags ^ s->s_flags) & MS_RDONLY) {
  620. up_write(&s->s_umount);
  621. deactivate_super(s);
  622. s = ERR_PTR(-EBUSY);
  623. }
  624. goto out;
  625. } else {
  626. char b[BDEVNAME_SIZE];
  627. s->s_flags = flags;
  628. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  629. sb_set_blocksize(s, block_size(bdev));
  630. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  631. if (error) {
  632. up_write(&s->s_umount);
  633. deactivate_super(s);
  634. s = ERR_PTR(error);
  635. } else
  636. s->s_flags |= MS_ACTIVE;
  637. }
  638. return s;
  639. out:
  640. close_bdev_excl(bdev);
  641. return s;
  642. }
  643. EXPORT_SYMBOL(get_sb_bdev);
  644. void kill_block_super(struct super_block *sb)
  645. {
  646. struct block_device *bdev = sb->s_bdev;
  647. generic_shutdown_super(sb);
  648. sync_blockdev(bdev);
  649. close_bdev_excl(bdev);
  650. }
  651. EXPORT_SYMBOL(kill_block_super);
  652. struct super_block *get_sb_nodev(struct file_system_type *fs_type,
  653. int flags, void *data,
  654. int (*fill_super)(struct super_block *, void *, int))
  655. {
  656. int error;
  657. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  658. if (IS_ERR(s))
  659. return s;
  660. s->s_flags = flags;
  661. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  662. if (error) {
  663. up_write(&s->s_umount);
  664. deactivate_super(s);
  665. return ERR_PTR(error);
  666. }
  667. s->s_flags |= MS_ACTIVE;
  668. return s;
  669. }
  670. EXPORT_SYMBOL(get_sb_nodev);
  671. static int compare_single(struct super_block *s, void *p)
  672. {
  673. return 1;
  674. }
  675. struct super_block *get_sb_single(struct file_system_type *fs_type,
  676. int flags, void *data,
  677. int (*fill_super)(struct super_block *, void *, int))
  678. {
  679. struct super_block *s;
  680. int error;
  681. s = sget(fs_type, compare_single, set_anon_super, NULL);
  682. if (IS_ERR(s))
  683. return s;
  684. if (!s->s_root) {
  685. s->s_flags = flags;
  686. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  687. if (error) {
  688. up_write(&s->s_umount);
  689. deactivate_super(s);
  690. return ERR_PTR(error);
  691. }
  692. s->s_flags |= MS_ACTIVE;
  693. }
  694. do_remount_sb(s, flags, data, 0);
  695. return s;
  696. }
  697. EXPORT_SYMBOL(get_sb_single);
  698. struct vfsmount *
  699. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  700. {
  701. struct file_system_type *type = get_fs_type(fstype);
  702. struct super_block *sb = ERR_PTR(-ENOMEM);
  703. struct vfsmount *mnt;
  704. int error;
  705. char *secdata = NULL;
  706. if (!type)
  707. return ERR_PTR(-ENODEV);
  708. mnt = alloc_vfsmnt(name);
  709. if (!mnt)
  710. goto out;
  711. if (data) {
  712. secdata = alloc_secdata();
  713. if (!secdata) {
  714. sb = ERR_PTR(-ENOMEM);
  715. goto out_mnt;
  716. }
  717. error = security_sb_copy_data(type, data, secdata);
  718. if (error) {
  719. sb = ERR_PTR(error);
  720. goto out_free_secdata;
  721. }
  722. }
  723. sb = type->get_sb(type, flags, name, data);
  724. if (IS_ERR(sb))
  725. goto out_free_secdata;
  726. error = security_sb_kern_mount(sb, secdata);
  727. if (error)
  728. goto out_sb;
  729. mnt->mnt_sb = sb;
  730. mnt->mnt_root = dget(sb->s_root);
  731. mnt->mnt_mountpoint = sb->s_root;
  732. mnt->mnt_parent = mnt;
  733. up_write(&sb->s_umount);
  734. free_secdata(secdata);
  735. put_filesystem(type);
  736. return mnt;
  737. out_sb:
  738. up_write(&sb->s_umount);
  739. deactivate_super(sb);
  740. sb = ERR_PTR(error);
  741. out_free_secdata:
  742. free_secdata(secdata);
  743. out_mnt:
  744. free_vfsmnt(mnt);
  745. out:
  746. put_filesystem(type);
  747. return (struct vfsmount *)sb;
  748. }
  749. EXPORT_SYMBOL_GPL(do_kern_mount);
  750. struct vfsmount *kern_mount(struct file_system_type *type)
  751. {
  752. return do_kern_mount(type->name, 0, type->name, NULL);
  753. }
  754. EXPORT_SYMBOL(kern_mount);