super.c 21 KB

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