super.c 22 KB

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