super.c 24 KB

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