super.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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_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 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, &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. struct super_block *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. {
  612. struct block_device *bdev;
  613. struct super_block *s;
  614. int error = 0;
  615. bdev = open_bdev_excl(dev_name, flags, fs_type);
  616. if (IS_ERR(bdev))
  617. return (struct super_block *)bdev;
  618. /*
  619. * once the super is inserted into the list by sget, s_umount
  620. * will protect the lockfs code from trying to start a snapshot
  621. * while we are mounting
  622. */
  623. mutex_lock(&bdev->bd_mount_mutex);
  624. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  625. mutex_unlock(&bdev->bd_mount_mutex);
  626. if (IS_ERR(s))
  627. goto out;
  628. if (s->s_root) {
  629. if ((flags ^ s->s_flags) & MS_RDONLY) {
  630. up_write(&s->s_umount);
  631. deactivate_super(s);
  632. s = ERR_PTR(-EBUSY);
  633. }
  634. goto out;
  635. } else {
  636. char b[BDEVNAME_SIZE];
  637. s->s_flags = flags;
  638. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  639. sb_set_blocksize(s, block_size(bdev));
  640. error = fill_super(s, data, flags & MS_SILENT ? 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_SILENT ? 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_SILENT ? 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);