super.c 24 KB

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