super.c 23 KB

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