super.c 20 KB

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