super.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 <linux/mutex.h>
  40. #include <asm/uaccess.h>
  41. void get_filesystem(struct file_system_type *fs);
  42. void put_filesystem(struct file_system_type *fs);
  43. struct file_system_type *get_fs_type(const char *name);
  44. LIST_HEAD(super_blocks);
  45. DEFINE_SPINLOCK(sb_lock);
  46. /**
  47. * alloc_super - create new superblock
  48. *
  49. * Allocates and initializes a new &struct super_block. alloc_super()
  50. * returns a pointer new superblock or %NULL if allocation had failed.
  51. */
  52. static struct super_block *alloc_super(void)
  53. {
  54. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
  55. static struct super_operations default_op;
  56. if (s) {
  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. mutex_init(&s->s_vfs_rename_mutex);
  74. mutex_init(&s->s_dquot.dqio_mutex);
  75. mutex_init(&s->s_dquot.dqonoff_mutex);
  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_sb(sb);
  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 of %s. "
  231. "Self-destruct in 5 seconds. Have a nice day...\n",
  232. sb->s_id);
  233. }
  234. unlock_kernel();
  235. unlock_super(sb);
  236. }
  237. spin_lock(&sb_lock);
  238. /* should be initialized for __put_super_and_need_restart() */
  239. list_del_init(&sb->s_list);
  240. list_del(&sb->s_instances);
  241. spin_unlock(&sb_lock);
  242. up_write(&sb->s_umount);
  243. }
  244. EXPORT_SYMBOL(generic_shutdown_super);
  245. /**
  246. * sget - find or create a superblock
  247. * @type: filesystem type superblock should belong to
  248. * @test: comparison callback
  249. * @set: setup callback
  250. * @data: argument to each of them
  251. */
  252. struct super_block *sget(struct file_system_type *type,
  253. int (*test)(struct super_block *,void *),
  254. int (*set)(struct super_block *,void *),
  255. void *data)
  256. {
  257. struct super_block *s = NULL;
  258. struct list_head *p;
  259. int err;
  260. retry:
  261. spin_lock(&sb_lock);
  262. if (test) list_for_each(p, &type->fs_supers) {
  263. struct super_block *old;
  264. old = list_entry(p, struct super_block, s_instances);
  265. if (!test(old, data))
  266. continue;
  267. if (!grab_super(old))
  268. goto retry;
  269. if (s)
  270. destroy_super(s);
  271. return old;
  272. }
  273. if (!s) {
  274. spin_unlock(&sb_lock);
  275. s = alloc_super();
  276. if (!s)
  277. return ERR_PTR(-ENOMEM);
  278. goto retry;
  279. }
  280. err = set(s, data);
  281. if (err) {
  282. spin_unlock(&sb_lock);
  283. destroy_super(s);
  284. return ERR_PTR(err);
  285. }
  286. s->s_type = type;
  287. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  288. list_add_tail(&s->s_list, &super_blocks);
  289. list_add(&s->s_instances, &type->fs_supers);
  290. spin_unlock(&sb_lock);
  291. get_filesystem(type);
  292. return s;
  293. }
  294. EXPORT_SYMBOL(sget);
  295. void drop_super(struct super_block *sb)
  296. {
  297. up_read(&sb->s_umount);
  298. put_super(sb);
  299. }
  300. EXPORT_SYMBOL(drop_super);
  301. static inline void write_super(struct super_block *sb)
  302. {
  303. lock_super(sb);
  304. if (sb->s_root && sb->s_dirt)
  305. if (sb->s_op->write_super)
  306. sb->s_op->write_super(sb);
  307. unlock_super(sb);
  308. }
  309. /*
  310. * Note: check the dirty flag before waiting, so we don't
  311. * hold up the sync while mounting a device. (The newly
  312. * mounted device won't need syncing.)
  313. */
  314. void sync_supers(void)
  315. {
  316. struct super_block *sb;
  317. spin_lock(&sb_lock);
  318. restart:
  319. list_for_each_entry(sb, &super_blocks, s_list) {
  320. if (sb->s_dirt) {
  321. sb->s_count++;
  322. spin_unlock(&sb_lock);
  323. down_read(&sb->s_umount);
  324. write_super(sb);
  325. up_read(&sb->s_umount);
  326. spin_lock(&sb_lock);
  327. if (__put_super_and_need_restart(sb))
  328. goto restart;
  329. }
  330. }
  331. spin_unlock(&sb_lock);
  332. }
  333. /*
  334. * Call the ->sync_fs super_op against all filesytems which are r/w and
  335. * which implement it.
  336. *
  337. * This operation is careful to avoid the livelock which could easily happen
  338. * if two or more filesystems are being continuously dirtied. s_need_sync_fs
  339. * is used only here. We set it against all filesystems and then clear it as
  340. * we sync them. So redirtied filesystems are skipped.
  341. *
  342. * But if process A is currently running sync_filesytems and then process B
  343. * calls sync_filesystems as well, process B will set all the s_need_sync_fs
  344. * flags again, which will cause process A to resync everything. Fix that with
  345. * a local mutex.
  346. *
  347. * (Fabian) Avoid sync_fs with clean fs & wait mode 0
  348. */
  349. void sync_filesystems(int wait)
  350. {
  351. struct super_block *sb;
  352. static DEFINE_MUTEX(mutex);
  353. mutex_lock(&mutex); /* Could be down_interruptible */
  354. spin_lock(&sb_lock);
  355. list_for_each_entry(sb, &super_blocks, s_list) {
  356. if (!sb->s_op->sync_fs)
  357. continue;
  358. if (sb->s_flags & MS_RDONLY)
  359. continue;
  360. sb->s_need_sync_fs = 1;
  361. }
  362. restart:
  363. list_for_each_entry(sb, &super_blocks, s_list) {
  364. if (!sb->s_need_sync_fs)
  365. continue;
  366. sb->s_need_sync_fs = 0;
  367. if (sb->s_flags & MS_RDONLY)
  368. continue; /* hm. Was remounted r/o meanwhile */
  369. sb->s_count++;
  370. spin_unlock(&sb_lock);
  371. down_read(&sb->s_umount);
  372. if (sb->s_root && (wait || sb->s_dirt))
  373. sb->s_op->sync_fs(sb, wait);
  374. up_read(&sb->s_umount);
  375. /* restart only when sb is no longer on the list */
  376. spin_lock(&sb_lock);
  377. if (__put_super_and_need_restart(sb))
  378. goto restart;
  379. }
  380. spin_unlock(&sb_lock);
  381. mutex_unlock(&mutex);
  382. }
  383. /**
  384. * get_super - get the superblock of a device
  385. * @bdev: device to get the superblock for
  386. *
  387. * Scans the superblock list and finds the superblock of the file system
  388. * mounted on the device given. %NULL is returned if no match is found.
  389. */
  390. struct super_block * get_super(struct block_device *bdev)
  391. {
  392. struct super_block *sb;
  393. if (!bdev)
  394. return NULL;
  395. spin_lock(&sb_lock);
  396. rescan:
  397. list_for_each_entry(sb, &super_blocks, s_list) {
  398. if (sb->s_bdev == bdev) {
  399. sb->s_count++;
  400. spin_unlock(&sb_lock);
  401. down_read(&sb->s_umount);
  402. if (sb->s_root)
  403. return sb;
  404. up_read(&sb->s_umount);
  405. /* restart only when sb is no longer on the list */
  406. spin_lock(&sb_lock);
  407. if (__put_super_and_need_restart(sb))
  408. goto rescan;
  409. }
  410. }
  411. spin_unlock(&sb_lock);
  412. return NULL;
  413. }
  414. EXPORT_SYMBOL(get_super);
  415. struct super_block * user_get_super(dev_t dev)
  416. {
  417. struct super_block *sb;
  418. spin_lock(&sb_lock);
  419. rescan:
  420. list_for_each_entry(sb, &super_blocks, s_list) {
  421. if (sb->s_dev == dev) {
  422. sb->s_count++;
  423. spin_unlock(&sb_lock);
  424. down_read(&sb->s_umount);
  425. if (sb->s_root)
  426. return sb;
  427. up_read(&sb->s_umount);
  428. /* restart only when sb is no longer on the list */
  429. spin_lock(&sb_lock);
  430. if (__put_super_and_need_restart(sb))
  431. goto rescan;
  432. }
  433. }
  434. spin_unlock(&sb_lock);
  435. return NULL;
  436. }
  437. asmlinkage long sys_ustat(unsigned dev, struct ustat __user * ubuf)
  438. {
  439. struct super_block *s;
  440. struct ustat tmp;
  441. struct kstatfs sbuf;
  442. int err = -EINVAL;
  443. s = user_get_super(new_decode_dev(dev));
  444. if (s == NULL)
  445. goto out;
  446. err = vfs_statfs(s->s_root, &sbuf);
  447. drop_super(s);
  448. if (err)
  449. goto out;
  450. memset(&tmp,0,sizeof(struct ustat));
  451. tmp.f_tfree = sbuf.f_bfree;
  452. tmp.f_tinode = sbuf.f_ffree;
  453. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  454. out:
  455. return err;
  456. }
  457. /**
  458. * mark_files_ro
  459. * @sb: superblock in question
  460. *
  461. * All files are marked read/only. We don't care about pending
  462. * delete files so this should be used in 'force' mode only
  463. */
  464. static void mark_files_ro(struct super_block *sb)
  465. {
  466. struct file *f;
  467. file_list_lock();
  468. list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
  469. if (S_ISREG(f->f_dentry->d_inode->i_mode) && file_count(f))
  470. f->f_mode &= ~FMODE_WRITE;
  471. }
  472. file_list_unlock();
  473. }
  474. /**
  475. * do_remount_sb - asks filesystem to change mount options.
  476. * @sb: superblock in question
  477. * @flags: numeric part of options
  478. * @data: the rest of options
  479. * @force: whether or not to force the change
  480. *
  481. * Alters the mount options of a mounted file system.
  482. */
  483. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  484. {
  485. int retval;
  486. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  487. return -EACCES;
  488. if (flags & MS_RDONLY)
  489. acct_auto_close(sb);
  490. shrink_dcache_sb(sb);
  491. fsync_super(sb);
  492. /* If we are remounting RDONLY and current sb is read/write,
  493. make sure there are no rw files opened */
  494. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  495. if (force)
  496. mark_files_ro(sb);
  497. else if (!fs_may_remount_ro(sb))
  498. return -EBUSY;
  499. }
  500. if (sb->s_op->remount_fs) {
  501. lock_super(sb);
  502. retval = sb->s_op->remount_fs(sb, &flags, data);
  503. unlock_super(sb);
  504. if (retval)
  505. return retval;
  506. }
  507. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  508. return 0;
  509. }
  510. static void do_emergency_remount(unsigned long foo)
  511. {
  512. struct super_block *sb;
  513. spin_lock(&sb_lock);
  514. list_for_each_entry(sb, &super_blocks, s_list) {
  515. sb->s_count++;
  516. spin_unlock(&sb_lock);
  517. down_read(&sb->s_umount);
  518. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  519. /*
  520. * ->remount_fs needs lock_kernel().
  521. *
  522. * What lock protects sb->s_flags??
  523. */
  524. lock_kernel();
  525. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  526. unlock_kernel();
  527. }
  528. drop_super(sb);
  529. spin_lock(&sb_lock);
  530. }
  531. spin_unlock(&sb_lock);
  532. printk("Emergency Remount complete\n");
  533. }
  534. void emergency_remount(void)
  535. {
  536. pdflush_operation(do_emergency_remount, 0);
  537. }
  538. /*
  539. * Unnamed block devices are dummy devices used by virtual
  540. * filesystems which don't use real block-devices. -- jrs
  541. */
  542. static struct idr unnamed_dev_idr;
  543. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  544. int set_anon_super(struct super_block *s, void *data)
  545. {
  546. int dev;
  547. int error;
  548. retry:
  549. if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
  550. return -ENOMEM;
  551. spin_lock(&unnamed_dev_lock);
  552. error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
  553. spin_unlock(&unnamed_dev_lock);
  554. if (error == -EAGAIN)
  555. /* We raced and lost with another CPU. */
  556. goto retry;
  557. else if (error)
  558. return -EAGAIN;
  559. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  560. spin_lock(&unnamed_dev_lock);
  561. idr_remove(&unnamed_dev_idr, dev);
  562. spin_unlock(&unnamed_dev_lock);
  563. return -EMFILE;
  564. }
  565. s->s_dev = MKDEV(0, dev & MINORMASK);
  566. return 0;
  567. }
  568. EXPORT_SYMBOL(set_anon_super);
  569. void kill_anon_super(struct super_block *sb)
  570. {
  571. int slot = MINOR(sb->s_dev);
  572. generic_shutdown_super(sb);
  573. spin_lock(&unnamed_dev_lock);
  574. idr_remove(&unnamed_dev_idr, slot);
  575. spin_unlock(&unnamed_dev_lock);
  576. }
  577. EXPORT_SYMBOL(kill_anon_super);
  578. void __init unnamed_dev_init(void)
  579. {
  580. idr_init(&unnamed_dev_idr);
  581. }
  582. void kill_litter_super(struct super_block *sb)
  583. {
  584. if (sb->s_root)
  585. d_genocide(sb->s_root);
  586. kill_anon_super(sb);
  587. }
  588. EXPORT_SYMBOL(kill_litter_super);
  589. static int set_bdev_super(struct super_block *s, void *data)
  590. {
  591. s->s_bdev = data;
  592. s->s_dev = s->s_bdev->bd_dev;
  593. return 0;
  594. }
  595. static int test_bdev_super(struct super_block *s, void *data)
  596. {
  597. return (void *)s->s_bdev == data;
  598. }
  599. static void bdev_uevent(struct block_device *bdev, enum kobject_action action)
  600. {
  601. if (bdev->bd_disk) {
  602. if (bdev->bd_part)
  603. kobject_uevent(&bdev->bd_part->kobj, action);
  604. else
  605. kobject_uevent(&bdev->bd_disk->kobj, action);
  606. }
  607. }
  608. int get_sb_bdev(struct file_system_type *fs_type,
  609. int flags, const char *dev_name, void *data,
  610. int (*fill_super)(struct super_block *, void *, int),
  611. struct vfsmount *mnt)
  612. {
  613. struct block_device *bdev;
  614. struct super_block *s;
  615. int error = 0;
  616. bdev = open_bdev_excl(dev_name, flags, fs_type);
  617. if (IS_ERR(bdev))
  618. return PTR_ERR(bdev);
  619. /*
  620. * once the super is inserted into the list by sget, s_umount
  621. * will protect the lockfs code from trying to start a snapshot
  622. * while we are mounting
  623. */
  624. mutex_lock(&bdev->bd_mount_mutex);
  625. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  626. mutex_unlock(&bdev->bd_mount_mutex);
  627. if (IS_ERR(s))
  628. goto error_s;
  629. if (s->s_root) {
  630. if ((flags ^ s->s_flags) & MS_RDONLY) {
  631. up_write(&s->s_umount);
  632. deactivate_super(s);
  633. error = -EBUSY;
  634. goto error_bdev;
  635. }
  636. close_bdev_excl(bdev);
  637. } else {
  638. char b[BDEVNAME_SIZE];
  639. s->s_flags = flags;
  640. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  641. sb_set_blocksize(s, block_size(bdev));
  642. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  643. if (error) {
  644. up_write(&s->s_umount);
  645. deactivate_super(s);
  646. goto error;
  647. }
  648. s->s_flags |= MS_ACTIVE;
  649. bdev_uevent(bdev, KOBJ_MOUNT);
  650. }
  651. return simple_set_mnt(mnt, s);
  652. error_s:
  653. error = PTR_ERR(s);
  654. error_bdev:
  655. close_bdev_excl(bdev);
  656. error:
  657. return error;
  658. }
  659. EXPORT_SYMBOL(get_sb_bdev);
  660. void kill_block_super(struct super_block *sb)
  661. {
  662. struct block_device *bdev = sb->s_bdev;
  663. bdev_uevent(bdev, KOBJ_UMOUNT);
  664. generic_shutdown_super(sb);
  665. sync_blockdev(bdev);
  666. close_bdev_excl(bdev);
  667. }
  668. EXPORT_SYMBOL(kill_block_super);
  669. int get_sb_nodev(struct file_system_type *fs_type,
  670. int flags, void *data,
  671. int (*fill_super)(struct super_block *, void *, int),
  672. struct vfsmount *mnt)
  673. {
  674. int error;
  675. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  676. if (IS_ERR(s))
  677. return PTR_ERR(s);
  678. s->s_flags = flags;
  679. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  680. if (error) {
  681. up_write(&s->s_umount);
  682. deactivate_super(s);
  683. return error;
  684. }
  685. s->s_flags |= MS_ACTIVE;
  686. return simple_set_mnt(mnt, s);
  687. }
  688. EXPORT_SYMBOL(get_sb_nodev);
  689. static int compare_single(struct super_block *s, void *p)
  690. {
  691. return 1;
  692. }
  693. int get_sb_single(struct file_system_type *fs_type,
  694. int flags, void *data,
  695. int (*fill_super)(struct super_block *, void *, int),
  696. struct vfsmount *mnt)
  697. {
  698. struct super_block *s;
  699. int error;
  700. s = sget(fs_type, compare_single, set_anon_super, NULL);
  701. if (IS_ERR(s))
  702. return PTR_ERR(s);
  703. if (!s->s_root) {
  704. s->s_flags = flags;
  705. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  706. if (error) {
  707. up_write(&s->s_umount);
  708. deactivate_super(s);
  709. return error;
  710. }
  711. s->s_flags |= MS_ACTIVE;
  712. }
  713. do_remount_sb(s, flags, data, 0);
  714. return simple_set_mnt(mnt, s);
  715. }
  716. EXPORT_SYMBOL(get_sb_single);
  717. struct vfsmount *
  718. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  719. {
  720. struct vfsmount *mnt;
  721. char *secdata = NULL;
  722. int error;
  723. if (!type)
  724. return ERR_PTR(-ENODEV);
  725. error = -ENOMEM;
  726. mnt = alloc_vfsmnt(name);
  727. if (!mnt)
  728. goto out;
  729. if (data) {
  730. secdata = alloc_secdata();
  731. if (!secdata)
  732. goto out_mnt;
  733. error = security_sb_copy_data(type, data, secdata);
  734. if (error)
  735. goto out_free_secdata;
  736. }
  737. error = type->get_sb(type, flags, name, data, mnt);
  738. if (error < 0)
  739. goto out_free_secdata;
  740. error = security_sb_kern_mount(mnt->mnt_sb, secdata);
  741. if (error)
  742. goto out_sb;
  743. mnt->mnt_mountpoint = mnt->mnt_root;
  744. mnt->mnt_parent = mnt;
  745. up_write(&mnt->mnt_sb->s_umount);
  746. free_secdata(secdata);
  747. return mnt;
  748. out_sb:
  749. dput(mnt->mnt_root);
  750. up_write(&mnt->mnt_sb->s_umount);
  751. deactivate_super(mnt->mnt_sb);
  752. out_free_secdata:
  753. free_secdata(secdata);
  754. out_mnt:
  755. free_vfsmnt(mnt);
  756. out:
  757. return ERR_PTR(error);
  758. }
  759. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  760. struct vfsmount *
  761. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  762. {
  763. struct file_system_type *type = get_fs_type(fstype);
  764. struct vfsmount *mnt;
  765. if (!type)
  766. return ERR_PTR(-ENODEV);
  767. mnt = vfs_kern_mount(type, flags, name, data);
  768. put_filesystem(type);
  769. return mnt;
  770. }
  771. struct vfsmount *kern_mount(struct file_system_type *type)
  772. {
  773. return vfs_kern_mount(type, 0, type->name, NULL);
  774. }
  775. EXPORT_SYMBOL(kern_mount);