super.c 23 KB

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