super.c 25 KB

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