super.c 23 KB

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