super.c 24 KB

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