super.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. spin_lock(&sb_lock);
  316. restart:
  317. list_for_each_entry(sb, &super_blocks, s_list) {
  318. if (sb->s_dirt) {
  319. sb->s_count++;
  320. spin_unlock(&sb_lock);
  321. down_read(&sb->s_umount);
  322. write_super(sb);
  323. up_read(&sb->s_umount);
  324. spin_lock(&sb_lock);
  325. if (__put_super_and_need_restart(sb))
  326. goto restart;
  327. }
  328. }
  329. spin_unlock(&sb_lock);
  330. }
  331. /*
  332. * Call the ->sync_fs super_op against all filesytems which are r/w and
  333. * which implement it.
  334. *
  335. * This operation is careful to avoid the livelock which could easily happen
  336. * if two or more filesystems are being continuously dirtied. s_need_sync_fs
  337. * is used only here. We set it against all filesystems and then clear it as
  338. * we sync them. So redirtied filesystems are skipped.
  339. *
  340. * But if process A is currently running sync_filesytems and then process B
  341. * calls sync_filesystems as well, process B will set all the s_need_sync_fs
  342. * flags again, which will cause process A to resync everything. Fix that with
  343. * a local mutex.
  344. *
  345. * (Fabian) Avoid sync_fs with clean fs & wait mode 0
  346. */
  347. void sync_filesystems(int wait)
  348. {
  349. struct super_block *sb;
  350. static DECLARE_MUTEX(mutex);
  351. down(&mutex); /* Could be down_interruptible */
  352. spin_lock(&sb_lock);
  353. list_for_each_entry(sb, &super_blocks, s_list) {
  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. restart:
  361. list_for_each_entry(sb, &super_blocks, s_list) {
  362. if (!sb->s_need_sync_fs)
  363. continue;
  364. sb->s_need_sync_fs = 0;
  365. if (sb->s_flags & MS_RDONLY)
  366. continue; /* hm. Was remounted r/o meanwhile */
  367. sb->s_count++;
  368. spin_unlock(&sb_lock);
  369. down_read(&sb->s_umount);
  370. if (sb->s_root && (wait || sb->s_dirt))
  371. sb->s_op->sync_fs(sb, wait);
  372. up_read(&sb->s_umount);
  373. /* restart only when sb is no longer on the list */
  374. spin_lock(&sb_lock);
  375. if (__put_super_and_need_restart(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 super_block *sb;
  391. if (!bdev)
  392. return NULL;
  393. spin_lock(&sb_lock);
  394. rescan:
  395. list_for_each_entry(sb, &super_blocks, s_list) {
  396. if (sb->s_bdev == bdev) {
  397. sb->s_count++;
  398. spin_unlock(&sb_lock);
  399. down_read(&sb->s_umount);
  400. if (sb->s_root)
  401. return sb;
  402. up_read(&sb->s_umount);
  403. /* restart only when sb is no longer on the list */
  404. spin_lock(&sb_lock);
  405. if (__put_super_and_need_restart(sb))
  406. goto rescan;
  407. }
  408. }
  409. spin_unlock(&sb_lock);
  410. return NULL;
  411. }
  412. EXPORT_SYMBOL(get_super);
  413. struct super_block * user_get_super(dev_t dev)
  414. {
  415. struct super_block *sb;
  416. spin_lock(&sb_lock);
  417. rescan:
  418. list_for_each_entry(sb, &super_blocks, s_list) {
  419. if (sb->s_dev == dev) {
  420. sb->s_count++;
  421. spin_unlock(&sb_lock);
  422. down_read(&sb->s_umount);
  423. if (sb->s_root)
  424. return sb;
  425. up_read(&sb->s_umount);
  426. /* restart only when sb is no longer on the list */
  427. spin_lock(&sb_lock);
  428. if (__put_super_and_need_restart(sb))
  429. goto rescan;
  430. }
  431. }
  432. spin_unlock(&sb_lock);
  433. return NULL;
  434. }
  435. EXPORT_SYMBOL(user_get_super);
  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_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. static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
  599. {
  600. if (bdev->bd_disk) {
  601. if (bdev->bd_part)
  602. kobject_uevent(&bdev->bd_part->kobj, action, NULL);
  603. else
  604. kobject_uevent(&bdev->bd_disk->kobj, action, NULL);
  605. }
  606. }
  607. struct super_block *get_sb_bdev(struct file_system_type *fs_type,
  608. int flags, const char *dev_name, void *data,
  609. int (*fill_super)(struct super_block *, void *, int))
  610. {
  611. struct block_device *bdev;
  612. struct super_block *s;
  613. int error = 0;
  614. bdev = open_bdev_excl(dev_name, flags, fs_type);
  615. if (IS_ERR(bdev))
  616. return (struct super_block *)bdev;
  617. /*
  618. * once the super is inserted into the list by sget, s_umount
  619. * will protect the lockfs code from trying to start a snapshot
  620. * while we are mounting
  621. */
  622. down(&bdev->bd_mount_sem);
  623. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  624. up(&bdev->bd_mount_sem);
  625. if (IS_ERR(s))
  626. goto out;
  627. if (s->s_root) {
  628. if ((flags ^ s->s_flags) & MS_RDONLY) {
  629. up_write(&s->s_umount);
  630. deactivate_super(s);
  631. s = ERR_PTR(-EBUSY);
  632. }
  633. goto out;
  634. } else {
  635. char b[BDEVNAME_SIZE];
  636. s->s_flags = flags;
  637. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  638. s->s_old_blocksize = block_size(bdev);
  639. sb_set_blocksize(s, s->s_old_blocksize);
  640. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  641. if (error) {
  642. up_write(&s->s_umount);
  643. deactivate_super(s);
  644. s = ERR_PTR(error);
  645. } else {
  646. s->s_flags |= MS_ACTIVE;
  647. bdev_uevent(bdev, KOBJ_MOUNT);
  648. }
  649. }
  650. return s;
  651. out:
  652. close_bdev_excl(bdev);
  653. return s;
  654. }
  655. EXPORT_SYMBOL(get_sb_bdev);
  656. void kill_block_super(struct super_block *sb)
  657. {
  658. struct block_device *bdev = sb->s_bdev;
  659. bdev_uevent(bdev, KOBJ_UMOUNT);
  660. generic_shutdown_super(sb);
  661. sync_blockdev(bdev);
  662. close_bdev_excl(bdev);
  663. }
  664. EXPORT_SYMBOL(kill_block_super);
  665. struct super_block *get_sb_nodev(struct file_system_type *fs_type,
  666. int flags, void *data,
  667. int (*fill_super)(struct super_block *, void *, int))
  668. {
  669. int error;
  670. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  671. if (IS_ERR(s))
  672. return s;
  673. s->s_flags = flags;
  674. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  675. if (error) {
  676. up_write(&s->s_umount);
  677. deactivate_super(s);
  678. return ERR_PTR(error);
  679. }
  680. s->s_flags |= MS_ACTIVE;
  681. return s;
  682. }
  683. EXPORT_SYMBOL(get_sb_nodev);
  684. static int compare_single(struct super_block *s, void *p)
  685. {
  686. return 1;
  687. }
  688. struct super_block *get_sb_single(struct file_system_type *fs_type,
  689. int flags, void *data,
  690. int (*fill_super)(struct super_block *, void *, int))
  691. {
  692. struct super_block *s;
  693. int error;
  694. s = sget(fs_type, compare_single, set_anon_super, NULL);
  695. if (IS_ERR(s))
  696. return s;
  697. if (!s->s_root) {
  698. s->s_flags = flags;
  699. error = fill_super(s, data, flags & MS_VERBOSE ? 1 : 0);
  700. if (error) {
  701. up_write(&s->s_umount);
  702. deactivate_super(s);
  703. return ERR_PTR(error);
  704. }
  705. s->s_flags |= MS_ACTIVE;
  706. }
  707. do_remount_sb(s, flags, data, 0);
  708. return s;
  709. }
  710. EXPORT_SYMBOL(get_sb_single);
  711. struct vfsmount *
  712. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  713. {
  714. struct file_system_type *type = get_fs_type(fstype);
  715. struct super_block *sb = ERR_PTR(-ENOMEM);
  716. struct vfsmount *mnt;
  717. int error;
  718. char *secdata = NULL;
  719. if (!type)
  720. return ERR_PTR(-ENODEV);
  721. mnt = alloc_vfsmnt(name);
  722. if (!mnt)
  723. goto out;
  724. if (data) {
  725. secdata = alloc_secdata();
  726. if (!secdata) {
  727. sb = ERR_PTR(-ENOMEM);
  728. goto out_mnt;
  729. }
  730. error = security_sb_copy_data(type, data, secdata);
  731. if (error) {
  732. sb = ERR_PTR(error);
  733. goto out_free_secdata;
  734. }
  735. }
  736. sb = type->get_sb(type, flags, name, data);
  737. if (IS_ERR(sb))
  738. goto out_free_secdata;
  739. error = security_sb_kern_mount(sb, secdata);
  740. if (error)
  741. goto out_sb;
  742. mnt->mnt_sb = sb;
  743. mnt->mnt_root = dget(sb->s_root);
  744. mnt->mnt_mountpoint = sb->s_root;
  745. mnt->mnt_parent = mnt;
  746. up_write(&sb->s_umount);
  747. free_secdata(secdata);
  748. put_filesystem(type);
  749. return mnt;
  750. out_sb:
  751. up_write(&sb->s_umount);
  752. deactivate_super(sb);
  753. sb = ERR_PTR(error);
  754. out_free_secdata:
  755. free_secdata(secdata);
  756. out_mnt:
  757. free_vfsmnt(mnt);
  758. out:
  759. put_filesystem(type);
  760. return (struct vfsmount *)sb;
  761. }
  762. EXPORT_SYMBOL_GPL(do_kern_mount);
  763. struct vfsmount *kern_mount(struct file_system_type *type)
  764. {
  765. return do_kern_mount(type->name, 0, type->name, NULL);
  766. }
  767. EXPORT_SYMBOL(kern_mount);