super.c 24 KB

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