super.c 24 KB

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