super.c 22 KB

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