super.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. static 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, 0);
  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. int remount_rw;
  559. #ifdef CONFIG_BLOCK
  560. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  561. return -EACCES;
  562. #endif
  563. if (flags & MS_RDONLY)
  564. acct_auto_close(sb);
  565. shrink_dcache_sb(sb);
  566. fsync_super(sb);
  567. /* If we are remounting RDONLY and current sb is read/write,
  568. make sure there are no rw files opened */
  569. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  570. if (force)
  571. mark_files_ro(sb);
  572. else if (!fs_may_remount_ro(sb))
  573. return -EBUSY;
  574. retval = DQUOT_OFF(sb, 1);
  575. if (retval < 0 && retval != -ENOSYS)
  576. return -EBUSY;
  577. }
  578. remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
  579. if (sb->s_op->remount_fs) {
  580. lock_super(sb);
  581. retval = sb->s_op->remount_fs(sb, &flags, data);
  582. unlock_super(sb);
  583. if (retval)
  584. return retval;
  585. }
  586. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  587. if (remount_rw)
  588. DQUOT_ON_REMOUNT(sb);
  589. return 0;
  590. }
  591. static void do_emergency_remount(unsigned long foo)
  592. {
  593. struct super_block *sb;
  594. spin_lock(&sb_lock);
  595. list_for_each_entry(sb, &super_blocks, s_list) {
  596. sb->s_count++;
  597. spin_unlock(&sb_lock);
  598. down_read(&sb->s_umount);
  599. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  600. /*
  601. * ->remount_fs needs lock_kernel().
  602. *
  603. * What lock protects sb->s_flags??
  604. */
  605. lock_kernel();
  606. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  607. unlock_kernel();
  608. }
  609. drop_super(sb);
  610. spin_lock(&sb_lock);
  611. }
  612. spin_unlock(&sb_lock);
  613. printk("Emergency Remount complete\n");
  614. }
  615. void emergency_remount(void)
  616. {
  617. pdflush_operation(do_emergency_remount, 0);
  618. }
  619. /*
  620. * Unnamed block devices are dummy devices used by virtual
  621. * filesystems which don't use real block-devices. -- jrs
  622. */
  623. static struct idr unnamed_dev_idr;
  624. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  625. int set_anon_super(struct super_block *s, void *data)
  626. {
  627. int dev;
  628. int error;
  629. retry:
  630. if (idr_pre_get(&unnamed_dev_idr, GFP_ATOMIC) == 0)
  631. return -ENOMEM;
  632. spin_lock(&unnamed_dev_lock);
  633. error = idr_get_new(&unnamed_dev_idr, NULL, &dev);
  634. spin_unlock(&unnamed_dev_lock);
  635. if (error == -EAGAIN)
  636. /* We raced and lost with another CPU. */
  637. goto retry;
  638. else if (error)
  639. return -EAGAIN;
  640. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  641. spin_lock(&unnamed_dev_lock);
  642. idr_remove(&unnamed_dev_idr, dev);
  643. spin_unlock(&unnamed_dev_lock);
  644. return -EMFILE;
  645. }
  646. s->s_dev = MKDEV(0, dev & MINORMASK);
  647. return 0;
  648. }
  649. EXPORT_SYMBOL(set_anon_super);
  650. void kill_anon_super(struct super_block *sb)
  651. {
  652. int slot = MINOR(sb->s_dev);
  653. generic_shutdown_super(sb);
  654. spin_lock(&unnamed_dev_lock);
  655. idr_remove(&unnamed_dev_idr, slot);
  656. spin_unlock(&unnamed_dev_lock);
  657. }
  658. EXPORT_SYMBOL(kill_anon_super);
  659. void __init unnamed_dev_init(void)
  660. {
  661. idr_init(&unnamed_dev_idr);
  662. }
  663. void kill_litter_super(struct super_block *sb)
  664. {
  665. if (sb->s_root)
  666. d_genocide(sb->s_root);
  667. kill_anon_super(sb);
  668. }
  669. EXPORT_SYMBOL(kill_litter_super);
  670. #ifdef CONFIG_BLOCK
  671. static int set_bdev_super(struct super_block *s, void *data)
  672. {
  673. s->s_bdev = data;
  674. s->s_dev = s->s_bdev->bd_dev;
  675. return 0;
  676. }
  677. static int test_bdev_super(struct super_block *s, void *data)
  678. {
  679. return (void *)s->s_bdev == data;
  680. }
  681. int get_sb_bdev(struct file_system_type *fs_type,
  682. int flags, const char *dev_name, void *data,
  683. int (*fill_super)(struct super_block *, void *, int),
  684. struct vfsmount *mnt)
  685. {
  686. struct block_device *bdev;
  687. struct super_block *s;
  688. int error = 0;
  689. bdev = open_bdev_excl(dev_name, flags, 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_excl(bdev);
  710. } else {
  711. char b[BDEVNAME_SIZE];
  712. s->s_flags = flags;
  713. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  714. sb_set_blocksize(s, block_size(bdev));
  715. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  716. if (error) {
  717. up_write(&s->s_umount);
  718. deactivate_super(s);
  719. goto error;
  720. }
  721. s->s_flags |= MS_ACTIVE;
  722. }
  723. return simple_set_mnt(mnt, s);
  724. error_s:
  725. error = PTR_ERR(s);
  726. error_bdev:
  727. close_bdev_excl(bdev);
  728. error:
  729. return error;
  730. }
  731. EXPORT_SYMBOL(get_sb_bdev);
  732. void kill_block_super(struct super_block *sb)
  733. {
  734. struct block_device *bdev = sb->s_bdev;
  735. generic_shutdown_super(sb);
  736. sync_blockdev(bdev);
  737. close_bdev_excl(bdev);
  738. }
  739. EXPORT_SYMBOL(kill_block_super);
  740. #endif
  741. int get_sb_nodev(struct file_system_type *fs_type,
  742. int flags, void *data,
  743. int (*fill_super)(struct super_block *, void *, int),
  744. struct vfsmount *mnt)
  745. {
  746. int error;
  747. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  748. if (IS_ERR(s))
  749. return PTR_ERR(s);
  750. s->s_flags = flags;
  751. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  752. if (error) {
  753. up_write(&s->s_umount);
  754. deactivate_super(s);
  755. return error;
  756. }
  757. s->s_flags |= MS_ACTIVE;
  758. return simple_set_mnt(mnt, s);
  759. }
  760. EXPORT_SYMBOL(get_sb_nodev);
  761. static int compare_single(struct super_block *s, void *p)
  762. {
  763. return 1;
  764. }
  765. int get_sb_single(struct file_system_type *fs_type,
  766. int flags, void *data,
  767. int (*fill_super)(struct super_block *, void *, int),
  768. struct vfsmount *mnt)
  769. {
  770. struct super_block *s;
  771. int error;
  772. s = sget(fs_type, compare_single, set_anon_super, NULL);
  773. if (IS_ERR(s))
  774. return PTR_ERR(s);
  775. if (!s->s_root) {
  776. s->s_flags = flags;
  777. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  778. if (error) {
  779. up_write(&s->s_umount);
  780. deactivate_super(s);
  781. return error;
  782. }
  783. s->s_flags |= MS_ACTIVE;
  784. }
  785. do_remount_sb(s, flags, data, 0);
  786. return simple_set_mnt(mnt, s);
  787. }
  788. EXPORT_SYMBOL(get_sb_single);
  789. struct vfsmount *
  790. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  791. {
  792. struct vfsmount *mnt;
  793. char *secdata = NULL;
  794. int error;
  795. if (!type)
  796. return ERR_PTR(-ENODEV);
  797. error = -ENOMEM;
  798. mnt = alloc_vfsmnt(name);
  799. if (!mnt)
  800. goto out;
  801. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  802. secdata = alloc_secdata();
  803. if (!secdata)
  804. goto out_mnt;
  805. error = security_sb_copy_data(data, secdata);
  806. if (error)
  807. goto out_free_secdata;
  808. }
  809. error = type->get_sb(type, flags, name, data, mnt);
  810. if (error < 0)
  811. goto out_free_secdata;
  812. BUG_ON(!mnt->mnt_sb);
  813. error = security_sb_kern_mount(mnt->mnt_sb, secdata);
  814. if (error)
  815. goto out_sb;
  816. mnt->mnt_mountpoint = mnt->mnt_root;
  817. mnt->mnt_parent = mnt;
  818. up_write(&mnt->mnt_sb->s_umount);
  819. free_secdata(secdata);
  820. return mnt;
  821. out_sb:
  822. dput(mnt->mnt_root);
  823. up_write(&mnt->mnt_sb->s_umount);
  824. deactivate_super(mnt->mnt_sb);
  825. out_free_secdata:
  826. free_secdata(secdata);
  827. out_mnt:
  828. free_vfsmnt(mnt);
  829. out:
  830. return ERR_PTR(error);
  831. }
  832. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  833. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  834. {
  835. int err;
  836. const char *subtype = strchr(fstype, '.');
  837. if (subtype) {
  838. subtype++;
  839. err = -EINVAL;
  840. if (!subtype[0])
  841. goto err;
  842. } else
  843. subtype = "";
  844. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  845. err = -ENOMEM;
  846. if (!mnt->mnt_sb->s_subtype)
  847. goto err;
  848. return mnt;
  849. err:
  850. mntput(mnt);
  851. return ERR_PTR(err);
  852. }
  853. struct vfsmount *
  854. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  855. {
  856. struct file_system_type *type = get_fs_type(fstype);
  857. struct vfsmount *mnt;
  858. if (!type)
  859. return ERR_PTR(-ENODEV);
  860. mnt = vfs_kern_mount(type, flags, name, data);
  861. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  862. !mnt->mnt_sb->s_subtype)
  863. mnt = fs_set_subtype(mnt, fstype);
  864. put_filesystem(type);
  865. return mnt;
  866. }
  867. EXPORT_SYMBOL_GPL(do_kern_mount);
  868. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  869. {
  870. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  871. }
  872. EXPORT_SYMBOL_GPL(kern_mount_data);