super.c 23 KB

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