super.c 24 KB

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