super.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. up_write(&s->s_umount);
  345. destroy_super(s);
  346. }
  347. return old;
  348. }
  349. }
  350. if (!s) {
  351. spin_unlock(&sb_lock);
  352. s = alloc_super(type);
  353. if (!s)
  354. return ERR_PTR(-ENOMEM);
  355. goto retry;
  356. }
  357. err = set(s, data);
  358. if (err) {
  359. spin_unlock(&sb_lock);
  360. up_write(&s->s_umount);
  361. destroy_super(s);
  362. return ERR_PTR(err);
  363. }
  364. s->s_type = type;
  365. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  366. list_add_tail(&s->s_list, &super_blocks);
  367. list_add(&s->s_instances, &type->fs_supers);
  368. spin_unlock(&sb_lock);
  369. get_filesystem(type);
  370. return s;
  371. }
  372. EXPORT_SYMBOL(sget);
  373. void drop_super(struct super_block *sb)
  374. {
  375. up_read(&sb->s_umount);
  376. put_super(sb);
  377. }
  378. EXPORT_SYMBOL(drop_super);
  379. static inline void write_super(struct super_block *sb)
  380. {
  381. lock_super(sb);
  382. if (sb->s_root && sb->s_dirt)
  383. if (sb->s_op->write_super)
  384. sb->s_op->write_super(sb);
  385. unlock_super(sb);
  386. }
  387. /*
  388. * Note: check the dirty flag before waiting, so we don't
  389. * hold up the sync while mounting a device. (The newly
  390. * mounted device won't need syncing.)
  391. */
  392. void sync_supers(void)
  393. {
  394. struct super_block *sb;
  395. spin_lock(&sb_lock);
  396. restart:
  397. list_for_each_entry(sb, &super_blocks, s_list) {
  398. if (sb->s_dirt) {
  399. sb->s_count++;
  400. spin_unlock(&sb_lock);
  401. down_read(&sb->s_umount);
  402. write_super(sb);
  403. up_read(&sb->s_umount);
  404. spin_lock(&sb_lock);
  405. if (__put_super_and_need_restart(sb))
  406. goto restart;
  407. }
  408. }
  409. spin_unlock(&sb_lock);
  410. }
  411. /*
  412. * Call the ->sync_fs super_op against all filesystems which are r/w and
  413. * which implement it.
  414. *
  415. * This operation is careful to avoid the livelock which could easily happen
  416. * if two or more filesystems are being continuously dirtied. s_need_sync_fs
  417. * is used only here. We set it against all filesystems and then clear it as
  418. * we sync them. So redirtied filesystems are skipped.
  419. *
  420. * But if process A is currently running sync_filesystems and then process B
  421. * calls sync_filesystems as well, process B will set all the s_need_sync_fs
  422. * flags again, which will cause process A to resync everything. Fix that with
  423. * a local mutex.
  424. *
  425. * (Fabian) Avoid sync_fs with clean fs & wait mode 0
  426. */
  427. void sync_filesystems(int wait)
  428. {
  429. struct super_block *sb;
  430. static DEFINE_MUTEX(mutex);
  431. mutex_lock(&mutex); /* Could be down_interruptible */
  432. spin_lock(&sb_lock);
  433. list_for_each_entry(sb, &super_blocks, s_list) {
  434. if (!sb->s_op->sync_fs)
  435. continue;
  436. if (sb->s_flags & MS_RDONLY)
  437. continue;
  438. sb->s_need_sync_fs = 1;
  439. }
  440. restart:
  441. list_for_each_entry(sb, &super_blocks, s_list) {
  442. if (!sb->s_need_sync_fs)
  443. continue;
  444. sb->s_need_sync_fs = 0;
  445. if (sb->s_flags & MS_RDONLY)
  446. continue; /* hm. Was remounted r/o meanwhile */
  447. sb->s_count++;
  448. spin_unlock(&sb_lock);
  449. down_read(&sb->s_umount);
  450. async_synchronize_full_domain(&sb->s_async_list);
  451. if (sb->s_root && (wait || sb->s_dirt))
  452. sb->s_op->sync_fs(sb, wait);
  453. up_read(&sb->s_umount);
  454. /* restart only when sb is no longer on the list */
  455. spin_lock(&sb_lock);
  456. if (__put_super_and_need_restart(sb))
  457. goto restart;
  458. }
  459. spin_unlock(&sb_lock);
  460. mutex_unlock(&mutex);
  461. }
  462. /**
  463. * get_super - get the superblock of a device
  464. * @bdev: device to get the superblock for
  465. *
  466. * Scans the superblock list and finds the superblock of the file system
  467. * mounted on the device given. %NULL is returned if no match is found.
  468. */
  469. struct super_block * get_super(struct block_device *bdev)
  470. {
  471. struct super_block *sb;
  472. if (!bdev)
  473. return NULL;
  474. spin_lock(&sb_lock);
  475. rescan:
  476. list_for_each_entry(sb, &super_blocks, s_list) {
  477. if (sb->s_bdev == bdev) {
  478. sb->s_count++;
  479. spin_unlock(&sb_lock);
  480. down_read(&sb->s_umount);
  481. if (sb->s_root)
  482. return sb;
  483. up_read(&sb->s_umount);
  484. /* restart only when sb is no longer on the list */
  485. spin_lock(&sb_lock);
  486. if (__put_super_and_need_restart(sb))
  487. goto rescan;
  488. }
  489. }
  490. spin_unlock(&sb_lock);
  491. return NULL;
  492. }
  493. EXPORT_SYMBOL(get_super);
  494. struct super_block * user_get_super(dev_t dev)
  495. {
  496. struct super_block *sb;
  497. spin_lock(&sb_lock);
  498. rescan:
  499. list_for_each_entry(sb, &super_blocks, s_list) {
  500. if (sb->s_dev == dev) {
  501. sb->s_count++;
  502. spin_unlock(&sb_lock);
  503. down_read(&sb->s_umount);
  504. if (sb->s_root)
  505. return sb;
  506. up_read(&sb->s_umount);
  507. /* restart only when sb is no longer on the list */
  508. spin_lock(&sb_lock);
  509. if (__put_super_and_need_restart(sb))
  510. goto rescan;
  511. }
  512. }
  513. spin_unlock(&sb_lock);
  514. return NULL;
  515. }
  516. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  517. {
  518. struct super_block *s;
  519. struct ustat tmp;
  520. struct kstatfs sbuf;
  521. int err = -EINVAL;
  522. s = user_get_super(new_decode_dev(dev));
  523. if (s == NULL)
  524. goto out;
  525. err = vfs_statfs(s->s_root, &sbuf);
  526. drop_super(s);
  527. if (err)
  528. goto out;
  529. memset(&tmp,0,sizeof(struct ustat));
  530. tmp.f_tfree = sbuf.f_bfree;
  531. tmp.f_tinode = sbuf.f_ffree;
  532. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  533. out:
  534. return err;
  535. }
  536. /**
  537. * mark_files_ro - mark all files read-only
  538. * @sb: superblock in question
  539. *
  540. * All files are marked read-only. We don't care about pending
  541. * delete files so this should be used in 'force' mode only.
  542. */
  543. static void mark_files_ro(struct super_block *sb)
  544. {
  545. struct file *f;
  546. retry:
  547. file_list_lock();
  548. list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
  549. struct vfsmount *mnt;
  550. if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
  551. continue;
  552. if (!file_count(f))
  553. continue;
  554. if (!(f->f_mode & FMODE_WRITE))
  555. continue;
  556. f->f_mode &= ~FMODE_WRITE;
  557. if (file_check_writeable(f) != 0)
  558. continue;
  559. file_release_write(f);
  560. mnt = mntget(f->f_path.mnt);
  561. file_list_unlock();
  562. /*
  563. * This can sleep, so we can't hold
  564. * the file_list_lock() spinlock.
  565. */
  566. mnt_drop_write(mnt);
  567. mntput(mnt);
  568. goto retry;
  569. }
  570. file_list_unlock();
  571. }
  572. /**
  573. * do_remount_sb - asks filesystem to change mount options.
  574. * @sb: superblock in question
  575. * @flags: numeric part of options
  576. * @data: the rest of options
  577. * @force: whether or not to force the change
  578. *
  579. * Alters the mount options of a mounted file system.
  580. */
  581. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  582. {
  583. int retval;
  584. int remount_rw;
  585. #ifdef CONFIG_BLOCK
  586. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  587. return -EACCES;
  588. #endif
  589. if (flags & MS_RDONLY)
  590. acct_auto_close(sb);
  591. shrink_dcache_sb(sb);
  592. fsync_super(sb);
  593. /* If we are remounting RDONLY and current sb is read/write,
  594. make sure there are no rw files opened */
  595. if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) {
  596. if (force)
  597. mark_files_ro(sb);
  598. else if (!fs_may_remount_ro(sb))
  599. return -EBUSY;
  600. retval = DQUOT_OFF(sb, 1);
  601. if (retval < 0 && retval != -ENOSYS)
  602. return -EBUSY;
  603. }
  604. remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
  605. if (sb->s_op->remount_fs) {
  606. lock_super(sb);
  607. retval = sb->s_op->remount_fs(sb, &flags, data);
  608. unlock_super(sb);
  609. if (retval)
  610. return retval;
  611. }
  612. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  613. if (remount_rw)
  614. DQUOT_ON_REMOUNT(sb);
  615. return 0;
  616. }
  617. static void do_emergency_remount(unsigned long foo)
  618. {
  619. struct super_block *sb;
  620. spin_lock(&sb_lock);
  621. list_for_each_entry(sb, &super_blocks, s_list) {
  622. sb->s_count++;
  623. spin_unlock(&sb_lock);
  624. down_read(&sb->s_umount);
  625. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  626. /*
  627. * ->remount_fs needs lock_kernel().
  628. *
  629. * What lock protects sb->s_flags??
  630. */
  631. lock_kernel();
  632. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  633. unlock_kernel();
  634. }
  635. drop_super(sb);
  636. spin_lock(&sb_lock);
  637. }
  638. spin_unlock(&sb_lock);
  639. printk("Emergency Remount complete\n");
  640. }
  641. void emergency_remount(void)
  642. {
  643. pdflush_operation(do_emergency_remount, 0);
  644. }
  645. /*
  646. * Unnamed block devices are dummy devices used by virtual
  647. * filesystems which don't use real block-devices. -- jrs
  648. */
  649. static DEFINE_IDA(unnamed_dev_ida);
  650. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  651. int set_anon_super(struct super_block *s, void *data)
  652. {
  653. int dev;
  654. int error;
  655. retry:
  656. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  657. return -ENOMEM;
  658. spin_lock(&unnamed_dev_lock);
  659. error = ida_get_new(&unnamed_dev_ida, &dev);
  660. spin_unlock(&unnamed_dev_lock);
  661. if (error == -EAGAIN)
  662. /* We raced and lost with another CPU. */
  663. goto retry;
  664. else if (error)
  665. return -EAGAIN;
  666. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  667. spin_lock(&unnamed_dev_lock);
  668. ida_remove(&unnamed_dev_ida, dev);
  669. spin_unlock(&unnamed_dev_lock);
  670. return -EMFILE;
  671. }
  672. s->s_dev = MKDEV(0, dev & MINORMASK);
  673. return 0;
  674. }
  675. EXPORT_SYMBOL(set_anon_super);
  676. void kill_anon_super(struct super_block *sb)
  677. {
  678. int slot = MINOR(sb->s_dev);
  679. generic_shutdown_super(sb);
  680. spin_lock(&unnamed_dev_lock);
  681. ida_remove(&unnamed_dev_ida, slot);
  682. spin_unlock(&unnamed_dev_lock);
  683. }
  684. EXPORT_SYMBOL(kill_anon_super);
  685. void kill_litter_super(struct super_block *sb)
  686. {
  687. if (sb->s_root)
  688. d_genocide(sb->s_root);
  689. kill_anon_super(sb);
  690. }
  691. EXPORT_SYMBOL(kill_litter_super);
  692. #ifdef CONFIG_BLOCK
  693. static int set_bdev_super(struct super_block *s, void *data)
  694. {
  695. s->s_bdev = data;
  696. s->s_dev = s->s_bdev->bd_dev;
  697. return 0;
  698. }
  699. static int test_bdev_super(struct super_block *s, void *data)
  700. {
  701. return (void *)s->s_bdev == data;
  702. }
  703. int get_sb_bdev(struct file_system_type *fs_type,
  704. int flags, const char *dev_name, void *data,
  705. int (*fill_super)(struct super_block *, void *, int),
  706. struct vfsmount *mnt)
  707. {
  708. struct block_device *bdev;
  709. struct super_block *s;
  710. fmode_t mode = FMODE_READ;
  711. int error = 0;
  712. if (!(flags & MS_RDONLY))
  713. mode |= FMODE_WRITE;
  714. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  715. if (IS_ERR(bdev))
  716. return PTR_ERR(bdev);
  717. /*
  718. * once the super is inserted into the list by sget, s_umount
  719. * will protect the lockfs code from trying to start a snapshot
  720. * while we are mounting
  721. */
  722. down(&bdev->bd_mount_sem);
  723. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  724. up(&bdev->bd_mount_sem);
  725. if (IS_ERR(s))
  726. goto error_s;
  727. if (s->s_root) {
  728. if ((flags ^ s->s_flags) & MS_RDONLY) {
  729. up_write(&s->s_umount);
  730. deactivate_super(s);
  731. error = -EBUSY;
  732. goto error_bdev;
  733. }
  734. close_bdev_exclusive(bdev, mode);
  735. } else {
  736. char b[BDEVNAME_SIZE];
  737. s->s_flags = flags;
  738. s->s_mode = mode;
  739. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  740. sb_set_blocksize(s, block_size(bdev));
  741. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  742. if (error) {
  743. up_write(&s->s_umount);
  744. deactivate_super(s);
  745. goto error;
  746. }
  747. s->s_flags |= MS_ACTIVE;
  748. bdev->bd_super = s;
  749. }
  750. return simple_set_mnt(mnt, s);
  751. error_s:
  752. error = PTR_ERR(s);
  753. error_bdev:
  754. close_bdev_exclusive(bdev, mode);
  755. error:
  756. return error;
  757. }
  758. EXPORT_SYMBOL(get_sb_bdev);
  759. void kill_block_super(struct super_block *sb)
  760. {
  761. struct block_device *bdev = sb->s_bdev;
  762. fmode_t mode = sb->s_mode;
  763. bdev->bd_super = 0;
  764. generic_shutdown_super(sb);
  765. sync_blockdev(bdev);
  766. close_bdev_exclusive(bdev, mode);
  767. }
  768. EXPORT_SYMBOL(kill_block_super);
  769. #endif
  770. int get_sb_nodev(struct file_system_type *fs_type,
  771. int flags, void *data,
  772. int (*fill_super)(struct super_block *, void *, int),
  773. struct vfsmount *mnt)
  774. {
  775. int error;
  776. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  777. if (IS_ERR(s))
  778. return PTR_ERR(s);
  779. s->s_flags = flags;
  780. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  781. if (error) {
  782. up_write(&s->s_umount);
  783. deactivate_super(s);
  784. return error;
  785. }
  786. s->s_flags |= MS_ACTIVE;
  787. return simple_set_mnt(mnt, s);
  788. }
  789. EXPORT_SYMBOL(get_sb_nodev);
  790. static int compare_single(struct super_block *s, void *p)
  791. {
  792. return 1;
  793. }
  794. int get_sb_single(struct file_system_type *fs_type,
  795. int flags, void *data,
  796. int (*fill_super)(struct super_block *, void *, int),
  797. struct vfsmount *mnt)
  798. {
  799. struct super_block *s;
  800. int error;
  801. s = sget(fs_type, compare_single, set_anon_super, NULL);
  802. if (IS_ERR(s))
  803. return PTR_ERR(s);
  804. if (!s->s_root) {
  805. s->s_flags = flags;
  806. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  807. if (error) {
  808. up_write(&s->s_umount);
  809. deactivate_super(s);
  810. return error;
  811. }
  812. s->s_flags |= MS_ACTIVE;
  813. }
  814. do_remount_sb(s, flags, data, 0);
  815. return simple_set_mnt(mnt, s);
  816. }
  817. EXPORT_SYMBOL(get_sb_single);
  818. struct vfsmount *
  819. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  820. {
  821. struct vfsmount *mnt;
  822. char *secdata = NULL;
  823. int error;
  824. if (!type)
  825. return ERR_PTR(-ENODEV);
  826. error = -ENOMEM;
  827. mnt = alloc_vfsmnt(name);
  828. if (!mnt)
  829. goto out;
  830. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  831. secdata = alloc_secdata();
  832. if (!secdata)
  833. goto out_mnt;
  834. error = security_sb_copy_data(data, secdata);
  835. if (error)
  836. goto out_free_secdata;
  837. }
  838. error = type->get_sb(type, flags, name, data, mnt);
  839. if (error < 0)
  840. goto out_free_secdata;
  841. BUG_ON(!mnt->mnt_sb);
  842. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  843. if (error)
  844. goto out_sb;
  845. mnt->mnt_mountpoint = mnt->mnt_root;
  846. mnt->mnt_parent = mnt;
  847. up_write(&mnt->mnt_sb->s_umount);
  848. free_secdata(secdata);
  849. return mnt;
  850. out_sb:
  851. dput(mnt->mnt_root);
  852. up_write(&mnt->mnt_sb->s_umount);
  853. deactivate_super(mnt->mnt_sb);
  854. out_free_secdata:
  855. free_secdata(secdata);
  856. out_mnt:
  857. free_vfsmnt(mnt);
  858. out:
  859. return ERR_PTR(error);
  860. }
  861. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  862. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  863. {
  864. int err;
  865. const char *subtype = strchr(fstype, '.');
  866. if (subtype) {
  867. subtype++;
  868. err = -EINVAL;
  869. if (!subtype[0])
  870. goto err;
  871. } else
  872. subtype = "";
  873. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  874. err = -ENOMEM;
  875. if (!mnt->mnt_sb->s_subtype)
  876. goto err;
  877. return mnt;
  878. err:
  879. mntput(mnt);
  880. return ERR_PTR(err);
  881. }
  882. struct vfsmount *
  883. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  884. {
  885. struct file_system_type *type = get_fs_type(fstype);
  886. struct vfsmount *mnt;
  887. if (!type)
  888. return ERR_PTR(-ENODEV);
  889. mnt = vfs_kern_mount(type, flags, name, data);
  890. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  891. !mnt->mnt_sb->s_subtype)
  892. mnt = fs_set_subtype(mnt, fstype);
  893. put_filesystem(type);
  894. return mnt;
  895. }
  896. EXPORT_SYMBOL_GPL(do_kern_mount);
  897. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  898. {
  899. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  900. }
  901. EXPORT_SYMBOL_GPL(kern_mount_data);