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;
  365. spin_lock(&sb_lock);
  366. restart:
  367. list_for_each_entry(sb, &super_blocks, s_list) {
  368. if (list_empty(&sb->s_instances))
  369. continue;
  370. if (sb->s_op->write_super && sb->s_dirt) {
  371. sb->s_count++;
  372. spin_unlock(&sb_lock);
  373. down_read(&sb->s_umount);
  374. if (sb->s_root && sb->s_dirt)
  375. sb->s_op->write_super(sb);
  376. up_read(&sb->s_umount);
  377. spin_lock(&sb_lock);
  378. if (__put_super_and_need_restart(sb))
  379. goto restart;
  380. }
  381. }
  382. spin_unlock(&sb_lock);
  383. }
  384. /**
  385. * get_super - get the superblock of a device
  386. * @bdev: device to get the superblock for
  387. *
  388. * Scans the superblock list and finds the superblock of the file system
  389. * mounted on the device given. %NULL is returned if no match is found.
  390. */
  391. struct super_block * get_super(struct block_device *bdev)
  392. {
  393. struct super_block *sb;
  394. if (!bdev)
  395. return NULL;
  396. spin_lock(&sb_lock);
  397. rescan:
  398. list_for_each_entry(sb, &super_blocks, s_list) {
  399. if (list_empty(&sb->s_instances))
  400. continue;
  401. if (sb->s_bdev == bdev) {
  402. sb->s_count++;
  403. spin_unlock(&sb_lock);
  404. down_read(&sb->s_umount);
  405. if (sb->s_root)
  406. return sb;
  407. up_read(&sb->s_umount);
  408. /* restart only when sb is no longer on the list */
  409. spin_lock(&sb_lock);
  410. if (__put_super_and_need_restart(sb))
  411. goto rescan;
  412. }
  413. }
  414. spin_unlock(&sb_lock);
  415. return NULL;
  416. }
  417. EXPORT_SYMBOL(get_super);
  418. /**
  419. * get_active_super - get an active reference to the superblock of a device
  420. * @bdev: device to get the superblock for
  421. *
  422. * Scans the superblock list and finds the superblock of the file system
  423. * mounted on the device given. Returns the superblock with an active
  424. * reference and s_umount held exclusively or %NULL if none was found.
  425. */
  426. struct super_block *get_active_super(struct block_device *bdev)
  427. {
  428. struct super_block *sb;
  429. if (!bdev)
  430. return NULL;
  431. 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. continue;
  437. if (grab_super(sb)) /* drops sb_lock */
  438. return sb;
  439. spin_lock(&sb_lock);
  440. }
  441. spin_unlock(&sb_lock);
  442. return NULL;
  443. }
  444. struct super_block * user_get_super(dev_t dev)
  445. {
  446. struct super_block *sb;
  447. spin_lock(&sb_lock);
  448. rescan:
  449. list_for_each_entry(sb, &super_blocks, s_list) {
  450. if (list_empty(&sb->s_instances))
  451. continue;
  452. if (sb->s_dev == dev) {
  453. sb->s_count++;
  454. spin_unlock(&sb_lock);
  455. down_read(&sb->s_umount);
  456. if (sb->s_root)
  457. return sb;
  458. up_read(&sb->s_umount);
  459. /* restart only when sb is no longer on the list */
  460. spin_lock(&sb_lock);
  461. if (__put_super_and_need_restart(sb))
  462. goto rescan;
  463. }
  464. }
  465. spin_unlock(&sb_lock);
  466. return NULL;
  467. }
  468. SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
  469. {
  470. struct super_block *s;
  471. struct ustat tmp;
  472. struct kstatfs sbuf;
  473. int err = -EINVAL;
  474. s = user_get_super(new_decode_dev(dev));
  475. if (s == NULL)
  476. goto out;
  477. err = vfs_statfs(s->s_root, &sbuf);
  478. drop_super(s);
  479. if (err)
  480. goto out;
  481. memset(&tmp,0,sizeof(struct ustat));
  482. tmp.f_tfree = sbuf.f_bfree;
  483. tmp.f_tinode = sbuf.f_ffree;
  484. err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0;
  485. out:
  486. return err;
  487. }
  488. /**
  489. * do_remount_sb - asks filesystem to change mount options.
  490. * @sb: superblock in question
  491. * @flags: numeric part of options
  492. * @data: the rest of options
  493. * @force: whether or not to force the change
  494. *
  495. * Alters the mount options of a mounted file system.
  496. */
  497. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  498. {
  499. int retval;
  500. int remount_rw, remount_ro;
  501. if (sb->s_frozen != SB_UNFROZEN)
  502. return -EBUSY;
  503. #ifdef CONFIG_BLOCK
  504. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  505. return -EACCES;
  506. #endif
  507. if (flags & MS_RDONLY)
  508. acct_auto_close(sb);
  509. shrink_dcache_sb(sb);
  510. sync_filesystem(sb);
  511. remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
  512. remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY);
  513. /* If we are remounting RDONLY and current sb is read/write,
  514. make sure there are no rw files opened */
  515. if (remount_ro) {
  516. if (force)
  517. mark_files_ro(sb);
  518. else if (!fs_may_remount_ro(sb))
  519. return -EBUSY;
  520. retval = vfs_dq_off(sb, 1);
  521. if (retval < 0 && retval != -ENOSYS)
  522. return -EBUSY;
  523. }
  524. if (sb->s_op->remount_fs) {
  525. retval = sb->s_op->remount_fs(sb, &flags, data);
  526. if (retval)
  527. return retval;
  528. }
  529. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  530. if (remount_rw)
  531. vfs_dq_quota_on_remount(sb);
  532. /*
  533. * Some filesystems modify their metadata via some other path than the
  534. * bdev buffer cache (eg. use a private mapping, or directories in
  535. * pagecache, etc). Also file data modifications go via their own
  536. * mappings. So If we try to mount readonly then copy the filesystem
  537. * from bdev, we could get stale data, so invalidate it to give a best
  538. * effort at coherency.
  539. */
  540. if (remount_ro && sb->s_bdev)
  541. invalidate_bdev(sb->s_bdev);
  542. return 0;
  543. }
  544. static void do_emergency_remount(struct work_struct *work)
  545. {
  546. struct super_block *sb;
  547. spin_lock(&sb_lock);
  548. list_for_each_entry(sb, &super_blocks, s_list) {
  549. if (list_empty(&sb->s_instances))
  550. continue;
  551. sb->s_count++;
  552. spin_unlock(&sb_lock);
  553. down_write(&sb->s_umount);
  554. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  555. /*
  556. * What lock protects sb->s_flags??
  557. */
  558. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  559. }
  560. up_write(&sb->s_umount);
  561. put_super(sb);
  562. spin_lock(&sb_lock);
  563. }
  564. spin_unlock(&sb_lock);
  565. kfree(work);
  566. printk("Emergency Remount complete\n");
  567. }
  568. void emergency_remount(void)
  569. {
  570. struct work_struct *work;
  571. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  572. if (work) {
  573. INIT_WORK(work, do_emergency_remount);
  574. schedule_work(work);
  575. }
  576. }
  577. /*
  578. * Unnamed block devices are dummy devices used by virtual
  579. * filesystems which don't use real block-devices. -- jrs
  580. */
  581. static DEFINE_IDA(unnamed_dev_ida);
  582. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  583. static int unnamed_dev_start = 0; /* don't bother trying below it */
  584. int set_anon_super(struct super_block *s, void *data)
  585. {
  586. int dev;
  587. int error;
  588. retry:
  589. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  590. return -ENOMEM;
  591. spin_lock(&unnamed_dev_lock);
  592. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  593. if (!error)
  594. unnamed_dev_start = dev + 1;
  595. spin_unlock(&unnamed_dev_lock);
  596. if (error == -EAGAIN)
  597. /* We raced and lost with another CPU. */
  598. goto retry;
  599. else if (error)
  600. return -EAGAIN;
  601. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  602. spin_lock(&unnamed_dev_lock);
  603. ida_remove(&unnamed_dev_ida, dev);
  604. if (unnamed_dev_start > dev)
  605. unnamed_dev_start = dev;
  606. spin_unlock(&unnamed_dev_lock);
  607. return -EMFILE;
  608. }
  609. s->s_dev = MKDEV(0, dev & MINORMASK);
  610. s->s_bdi = &noop_backing_dev_info;
  611. return 0;
  612. }
  613. EXPORT_SYMBOL(set_anon_super);
  614. void kill_anon_super(struct super_block *sb)
  615. {
  616. int slot = MINOR(sb->s_dev);
  617. generic_shutdown_super(sb);
  618. spin_lock(&unnamed_dev_lock);
  619. ida_remove(&unnamed_dev_ida, slot);
  620. if (slot < unnamed_dev_start)
  621. unnamed_dev_start = slot;
  622. spin_unlock(&unnamed_dev_lock);
  623. }
  624. EXPORT_SYMBOL(kill_anon_super);
  625. void kill_litter_super(struct super_block *sb)
  626. {
  627. if (sb->s_root)
  628. d_genocide(sb->s_root);
  629. kill_anon_super(sb);
  630. }
  631. EXPORT_SYMBOL(kill_litter_super);
  632. static int ns_test_super(struct super_block *sb, void *data)
  633. {
  634. return sb->s_fs_info == data;
  635. }
  636. static int ns_set_super(struct super_block *sb, void *data)
  637. {
  638. sb->s_fs_info = data;
  639. return set_anon_super(sb, NULL);
  640. }
  641. int get_sb_ns(struct file_system_type *fs_type, int flags, void *data,
  642. int (*fill_super)(struct super_block *, void *, int),
  643. struct vfsmount *mnt)
  644. {
  645. struct super_block *sb;
  646. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  647. if (IS_ERR(sb))
  648. return PTR_ERR(sb);
  649. if (!sb->s_root) {
  650. int err;
  651. sb->s_flags = flags;
  652. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  653. if (err) {
  654. deactivate_locked_super(sb);
  655. return err;
  656. }
  657. sb->s_flags |= MS_ACTIVE;
  658. }
  659. simple_set_mnt(mnt, sb);
  660. return 0;
  661. }
  662. EXPORT_SYMBOL(get_sb_ns);
  663. #ifdef CONFIG_BLOCK
  664. static int set_bdev_super(struct super_block *s, void *data)
  665. {
  666. s->s_bdev = data;
  667. s->s_dev = s->s_bdev->bd_dev;
  668. /*
  669. * We set the bdi here to the queue backing, file systems can
  670. * overwrite this in ->fill_super()
  671. */
  672. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  673. return 0;
  674. }
  675. static int test_bdev_super(struct super_block *s, void *data)
  676. {
  677. return (void *)s->s_bdev == data;
  678. }
  679. int get_sb_bdev(struct file_system_type *fs_type,
  680. int flags, const char *dev_name, void *data,
  681. int (*fill_super)(struct super_block *, void *, int),
  682. struct vfsmount *mnt)
  683. {
  684. struct block_device *bdev;
  685. struct super_block *s;
  686. fmode_t mode = FMODE_READ;
  687. int error = 0;
  688. if (!(flags & MS_RDONLY))
  689. mode |= FMODE_WRITE;
  690. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  691. if (IS_ERR(bdev))
  692. return PTR_ERR(bdev);
  693. /*
  694. * once the super is inserted into the list by sget, s_umount
  695. * will protect the lockfs code from trying to start a snapshot
  696. * while we are mounting
  697. */
  698. mutex_lock(&bdev->bd_fsfreeze_mutex);
  699. if (bdev->bd_fsfreeze_count > 0) {
  700. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  701. error = -EBUSY;
  702. goto error_bdev;
  703. }
  704. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  705. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  706. if (IS_ERR(s))
  707. goto error_s;
  708. if (s->s_root) {
  709. if ((flags ^ s->s_flags) & MS_RDONLY) {
  710. deactivate_locked_super(s);
  711. error = -EBUSY;
  712. goto error_bdev;
  713. }
  714. close_bdev_exclusive(bdev, mode);
  715. } else {
  716. char b[BDEVNAME_SIZE];
  717. s->s_flags = flags;
  718. s->s_mode = mode;
  719. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  720. sb_set_blocksize(s, block_size(bdev));
  721. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  722. if (error) {
  723. deactivate_locked_super(s);
  724. goto error;
  725. }
  726. s->s_flags |= MS_ACTIVE;
  727. bdev->bd_super = s;
  728. }
  729. simple_set_mnt(mnt, s);
  730. return 0;
  731. error_s:
  732. error = PTR_ERR(s);
  733. error_bdev:
  734. close_bdev_exclusive(bdev, mode);
  735. error:
  736. return error;
  737. }
  738. EXPORT_SYMBOL(get_sb_bdev);
  739. void kill_block_super(struct super_block *sb)
  740. {
  741. struct block_device *bdev = sb->s_bdev;
  742. fmode_t mode = sb->s_mode;
  743. bdev->bd_super = NULL;
  744. generic_shutdown_super(sb);
  745. sync_blockdev(bdev);
  746. close_bdev_exclusive(bdev, mode);
  747. }
  748. EXPORT_SYMBOL(kill_block_super);
  749. #endif
  750. int get_sb_nodev(struct file_system_type *fs_type,
  751. int flags, void *data,
  752. int (*fill_super)(struct super_block *, void *, int),
  753. struct vfsmount *mnt)
  754. {
  755. int error;
  756. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  757. if (IS_ERR(s))
  758. return PTR_ERR(s);
  759. s->s_flags = flags;
  760. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  761. if (error) {
  762. deactivate_locked_super(s);
  763. return error;
  764. }
  765. s->s_flags |= MS_ACTIVE;
  766. simple_set_mnt(mnt, s);
  767. return 0;
  768. }
  769. EXPORT_SYMBOL(get_sb_nodev);
  770. static int compare_single(struct super_block *s, void *p)
  771. {
  772. return 1;
  773. }
  774. int get_sb_single(struct file_system_type *fs_type,
  775. int flags, void *data,
  776. int (*fill_super)(struct super_block *, void *, int),
  777. struct vfsmount *mnt)
  778. {
  779. struct super_block *s;
  780. int error;
  781. s = sget(fs_type, compare_single, set_anon_super, NULL);
  782. if (IS_ERR(s))
  783. return PTR_ERR(s);
  784. if (!s->s_root) {
  785. s->s_flags = flags;
  786. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  787. if (error) {
  788. deactivate_locked_super(s);
  789. return error;
  790. }
  791. s->s_flags |= MS_ACTIVE;
  792. } else {
  793. do_remount_sb(s, flags, data, 0);
  794. }
  795. simple_set_mnt(mnt, s);
  796. return 0;
  797. }
  798. EXPORT_SYMBOL(get_sb_single);
  799. struct vfsmount *
  800. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  801. {
  802. struct vfsmount *mnt;
  803. char *secdata = NULL;
  804. int error;
  805. if (!type)
  806. return ERR_PTR(-ENODEV);
  807. error = -ENOMEM;
  808. mnt = alloc_vfsmnt(name);
  809. if (!mnt)
  810. goto out;
  811. if (flags & MS_KERNMOUNT)
  812. mnt->mnt_flags = MNT_INTERNAL;
  813. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  814. secdata = alloc_secdata();
  815. if (!secdata)
  816. goto out_mnt;
  817. error = security_sb_copy_data(data, secdata);
  818. if (error)
  819. goto out_free_secdata;
  820. }
  821. error = type->get_sb(type, flags, name, data, mnt);
  822. if (error < 0)
  823. goto out_free_secdata;
  824. BUG_ON(!mnt->mnt_sb);
  825. WARN_ON(!mnt->mnt_sb->s_bdi);
  826. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  827. if (error)
  828. goto out_sb;
  829. /*
  830. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  831. * but s_maxbytes was an unsigned long long for many releases. Throw
  832. * this warning for a little while to try and catch filesystems that
  833. * violate this rule. This warning should be either removed or
  834. * converted to a BUG() in 2.6.34.
  835. */
  836. WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  837. "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
  838. mnt->mnt_mountpoint = mnt->mnt_root;
  839. mnt->mnt_parent = mnt;
  840. up_write(&mnt->mnt_sb->s_umount);
  841. free_secdata(secdata);
  842. return mnt;
  843. out_sb:
  844. dput(mnt->mnt_root);
  845. deactivate_locked_super(mnt->mnt_sb);
  846. out_free_secdata:
  847. free_secdata(secdata);
  848. out_mnt:
  849. free_vfsmnt(mnt);
  850. out:
  851. return ERR_PTR(error);
  852. }
  853. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  854. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  855. {
  856. int err;
  857. const char *subtype = strchr(fstype, '.');
  858. if (subtype) {
  859. subtype++;
  860. err = -EINVAL;
  861. if (!subtype[0])
  862. goto err;
  863. } else
  864. subtype = "";
  865. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  866. err = -ENOMEM;
  867. if (!mnt->mnt_sb->s_subtype)
  868. goto err;
  869. return mnt;
  870. err:
  871. mntput(mnt);
  872. return ERR_PTR(err);
  873. }
  874. struct vfsmount *
  875. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  876. {
  877. struct file_system_type *type = get_fs_type(fstype);
  878. struct vfsmount *mnt;
  879. if (!type)
  880. return ERR_PTR(-ENODEV);
  881. mnt = vfs_kern_mount(type, flags, name, data);
  882. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  883. !mnt->mnt_sb->s_subtype)
  884. mnt = fs_set_subtype(mnt, fstype);
  885. put_filesystem(type);
  886. return mnt;
  887. }
  888. EXPORT_SYMBOL_GPL(do_kern_mount);
  889. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  890. {
  891. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  892. }
  893. EXPORT_SYMBOL_GPL(kern_mount_data);