super.c 21 KB

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