super.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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/acct.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mount.h>
  27. #include <linux/security.h>
  28. #include <linux/writeback.h> /* for the emergency remount stuff */
  29. #include <linux/idr.h>
  30. #include <linux/mutex.h>
  31. #include <linux/backing-dev.h>
  32. #include <linux/rculist_bl.h>
  33. #include "internal.h"
  34. LIST_HEAD(super_blocks);
  35. DEFINE_SPINLOCK(sb_lock);
  36. /**
  37. * alloc_super - create new superblock
  38. * @type: filesystem type superblock should belong to
  39. *
  40. * Allocates and initializes a new &struct super_block. alloc_super()
  41. * returns a pointer new superblock or %NULL if allocation had failed.
  42. */
  43. static struct super_block *alloc_super(struct file_system_type *type)
  44. {
  45. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
  46. static const struct super_operations default_op;
  47. if (s) {
  48. if (security_sb_alloc(s)) {
  49. kfree(s);
  50. s = NULL;
  51. goto out;
  52. }
  53. #ifdef CONFIG_SMP
  54. s->s_files = alloc_percpu(struct list_head);
  55. if (!s->s_files) {
  56. security_sb_free(s);
  57. kfree(s);
  58. s = NULL;
  59. goto out;
  60. } else {
  61. int i;
  62. for_each_possible_cpu(i)
  63. INIT_LIST_HEAD(per_cpu_ptr(s->s_files, i));
  64. }
  65. #else
  66. INIT_LIST_HEAD(&s->s_files);
  67. #endif
  68. INIT_LIST_HEAD(&s->s_instances);
  69. INIT_HLIST_BL_HEAD(&s->s_anon);
  70. INIT_LIST_HEAD(&s->s_inodes);
  71. INIT_LIST_HEAD(&s->s_dentry_lru);
  72. init_rwsem(&s->s_umount);
  73. mutex_init(&s->s_lock);
  74. lockdep_set_class(&s->s_umount, &type->s_umount_key);
  75. /*
  76. * The locking rules for s_lock are up to the
  77. * filesystem. For example ext3fs has different
  78. * lock ordering than usbfs:
  79. */
  80. lockdep_set_class(&s->s_lock, &type->s_lock_key);
  81. /*
  82. * sget() can have s_umount recursion.
  83. *
  84. * When it cannot find a suitable sb, it allocates a new
  85. * one (this one), and tries again to find a suitable old
  86. * one.
  87. *
  88. * In case that succeeds, it will acquire the s_umount
  89. * lock of the old one. Since these are clearly distrinct
  90. * locks, and this object isn't exposed yet, there's no
  91. * risk of deadlocks.
  92. *
  93. * Annotate this by putting this lock in a different
  94. * subclass.
  95. */
  96. down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
  97. s->s_count = 1;
  98. atomic_set(&s->s_active, 1);
  99. mutex_init(&s->s_vfs_rename_mutex);
  100. lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
  101. mutex_init(&s->s_dquot.dqio_mutex);
  102. mutex_init(&s->s_dquot.dqonoff_mutex);
  103. init_rwsem(&s->s_dquot.dqptr_sem);
  104. init_waitqueue_head(&s->s_wait_unfrozen);
  105. s->s_maxbytes = MAX_NON_LFS;
  106. s->s_op = &default_op;
  107. s->s_time_gran = 1000000000;
  108. }
  109. out:
  110. return s;
  111. }
  112. /**
  113. * destroy_super - frees a superblock
  114. * @s: superblock to free
  115. *
  116. * Frees a superblock.
  117. */
  118. static inline void destroy_super(struct super_block *s)
  119. {
  120. #ifdef CONFIG_SMP
  121. free_percpu(s->s_files);
  122. #endif
  123. security_sb_free(s);
  124. kfree(s->s_subtype);
  125. kfree(s->s_options);
  126. kfree(s);
  127. }
  128. /* Superblock refcounting */
  129. /*
  130. * Drop a superblock's refcount. The caller must hold sb_lock.
  131. */
  132. void __put_super(struct super_block *sb)
  133. {
  134. if (!--sb->s_count) {
  135. list_del_init(&sb->s_list);
  136. destroy_super(sb);
  137. }
  138. }
  139. /**
  140. * put_super - drop a temporary reference to superblock
  141. * @sb: superblock in question
  142. *
  143. * Drops a temporary reference, frees superblock if there's no
  144. * references left.
  145. */
  146. void put_super(struct super_block *sb)
  147. {
  148. spin_lock(&sb_lock);
  149. __put_super(sb);
  150. spin_unlock(&sb_lock);
  151. }
  152. /**
  153. * deactivate_locked_super - drop an active reference to superblock
  154. * @s: superblock to deactivate
  155. *
  156. * Drops an active reference to superblock, converting it into a temprory
  157. * one if there is no other active references left. In that case we
  158. * tell fs driver to shut it down and drop the temporary reference we
  159. * had just acquired.
  160. *
  161. * Caller holds exclusive lock on superblock; that lock is released.
  162. */
  163. void deactivate_locked_super(struct super_block *s)
  164. {
  165. struct file_system_type *fs = s->s_type;
  166. if (atomic_dec_and_test(&s->s_active)) {
  167. fs->kill_sb(s);
  168. /*
  169. * We need to call rcu_barrier so all the delayed rcu free
  170. * inodes are flushed before we release the fs module.
  171. */
  172. rcu_barrier();
  173. put_filesystem(fs);
  174. put_super(s);
  175. } else {
  176. up_write(&s->s_umount);
  177. }
  178. }
  179. EXPORT_SYMBOL(deactivate_locked_super);
  180. /**
  181. * deactivate_super - drop an active reference to superblock
  182. * @s: superblock to deactivate
  183. *
  184. * Variant of deactivate_locked_super(), except that superblock is *not*
  185. * locked by caller. If we are going to drop the final active reference,
  186. * lock will be acquired prior to that.
  187. */
  188. void deactivate_super(struct super_block *s)
  189. {
  190. if (!atomic_add_unless(&s->s_active, -1, 1)) {
  191. down_write(&s->s_umount);
  192. deactivate_locked_super(s);
  193. }
  194. }
  195. EXPORT_SYMBOL(deactivate_super);
  196. /**
  197. * grab_super - acquire an active reference
  198. * @s: reference we are trying to make active
  199. *
  200. * Tries to acquire an active reference. grab_super() is used when we
  201. * had just found a superblock in super_blocks or fs_type->fs_supers
  202. * and want to turn it into a full-blown active reference. grab_super()
  203. * is called with sb_lock held and drops it. Returns 1 in case of
  204. * success, 0 if we had failed (superblock contents was already dead or
  205. * dying when grab_super() had been called).
  206. */
  207. static int grab_super(struct super_block *s) __releases(sb_lock)
  208. {
  209. if (atomic_inc_not_zero(&s->s_active)) {
  210. spin_unlock(&sb_lock);
  211. return 1;
  212. }
  213. /* it's going away */
  214. s->s_count++;
  215. spin_unlock(&sb_lock);
  216. /* wait for it to die */
  217. down_write(&s->s_umount);
  218. up_write(&s->s_umount);
  219. put_super(s);
  220. return 0;
  221. }
  222. /*
  223. * Superblock locking. We really ought to get rid of these two.
  224. */
  225. void lock_super(struct super_block * sb)
  226. {
  227. get_fs_excl();
  228. mutex_lock(&sb->s_lock);
  229. }
  230. void unlock_super(struct super_block * sb)
  231. {
  232. put_fs_excl();
  233. mutex_unlock(&sb->s_lock);
  234. }
  235. EXPORT_SYMBOL(lock_super);
  236. EXPORT_SYMBOL(unlock_super);
  237. /**
  238. * generic_shutdown_super - common helper for ->kill_sb()
  239. * @sb: superblock to kill
  240. *
  241. * generic_shutdown_super() does all fs-independent work on superblock
  242. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  243. * that need destruction out of superblock, call generic_shutdown_super()
  244. * and release aforementioned objects. Note: dentries and inodes _are_
  245. * taken care of and do not need specific handling.
  246. *
  247. * Upon calling this function, the filesystem may no longer alter or
  248. * rearrange the set of dentries belonging to this super_block, nor may it
  249. * change the attachments of dentries to inodes.
  250. */
  251. void generic_shutdown_super(struct super_block *sb)
  252. {
  253. const struct super_operations *sop = sb->s_op;
  254. if (sb->s_root) {
  255. shrink_dcache_for_umount(sb);
  256. sync_filesystem(sb);
  257. get_fs_excl();
  258. sb->s_flags &= ~MS_ACTIVE;
  259. fsnotify_unmount_inodes(&sb->s_inodes);
  260. evict_inodes(sb);
  261. if (sop->put_super)
  262. sop->put_super(sb);
  263. if (!list_empty(&sb->s_inodes)) {
  264. printk("VFS: Busy inodes after unmount of %s. "
  265. "Self-destruct in 5 seconds. Have a nice day...\n",
  266. sb->s_id);
  267. }
  268. put_fs_excl();
  269. }
  270. spin_lock(&sb_lock);
  271. /* should be initialized for __put_super_and_need_restart() */
  272. list_del_init(&sb->s_instances);
  273. spin_unlock(&sb_lock);
  274. up_write(&sb->s_umount);
  275. }
  276. EXPORT_SYMBOL(generic_shutdown_super);
  277. /**
  278. * sget - find or create a superblock
  279. * @type: filesystem type superblock should belong to
  280. * @test: comparison callback
  281. * @set: setup callback
  282. * @data: argument to each of them
  283. */
  284. struct super_block *sget(struct file_system_type *type,
  285. int (*test)(struct super_block *,void *),
  286. int (*set)(struct super_block *,void *),
  287. void *data)
  288. {
  289. struct super_block *s = NULL;
  290. struct super_block *old;
  291. int err;
  292. retry:
  293. spin_lock(&sb_lock);
  294. if (test) {
  295. list_for_each_entry(old, &type->fs_supers, s_instances) {
  296. if (!test(old, data))
  297. continue;
  298. if (!grab_super(old))
  299. goto retry;
  300. if (s) {
  301. up_write(&s->s_umount);
  302. destroy_super(s);
  303. s = NULL;
  304. }
  305. down_write(&old->s_umount);
  306. if (unlikely(!(old->s_flags & MS_BORN))) {
  307. deactivate_locked_super(old);
  308. goto retry;
  309. }
  310. return old;
  311. }
  312. }
  313. if (!s) {
  314. spin_unlock(&sb_lock);
  315. s = alloc_super(type);
  316. if (!s)
  317. return ERR_PTR(-ENOMEM);
  318. goto retry;
  319. }
  320. err = set(s, data);
  321. if (err) {
  322. spin_unlock(&sb_lock);
  323. up_write(&s->s_umount);
  324. destroy_super(s);
  325. return ERR_PTR(err);
  326. }
  327. s->s_type = type;
  328. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  329. list_add_tail(&s->s_list, &super_blocks);
  330. list_add(&s->s_instances, &type->fs_supers);
  331. spin_unlock(&sb_lock);
  332. get_filesystem(type);
  333. return s;
  334. }
  335. EXPORT_SYMBOL(sget);
  336. void drop_super(struct super_block *sb)
  337. {
  338. up_read(&sb->s_umount);
  339. put_super(sb);
  340. }
  341. EXPORT_SYMBOL(drop_super);
  342. /**
  343. * sync_supers - helper for periodic superblock writeback
  344. *
  345. * Call the write_super method if present on all dirty superblocks in
  346. * the system. This is for the periodic writeback used by most older
  347. * filesystems. For data integrity superblock writeback use
  348. * sync_filesystems() instead.
  349. *
  350. * Note: check the dirty flag before waiting, so we don't
  351. * hold up the sync while mounting a device. (The newly
  352. * mounted device won't need syncing.)
  353. */
  354. void sync_supers(void)
  355. {
  356. struct super_block *sb, *p = NULL;
  357. spin_lock(&sb_lock);
  358. list_for_each_entry(sb, &super_blocks, s_list) {
  359. if (list_empty(&sb->s_instances))
  360. continue;
  361. if (sb->s_op->write_super && sb->s_dirt) {
  362. sb->s_count++;
  363. spin_unlock(&sb_lock);
  364. down_read(&sb->s_umount);
  365. if (sb->s_root && sb->s_dirt)
  366. sb->s_op->write_super(sb);
  367. up_read(&sb->s_umount);
  368. spin_lock(&sb_lock);
  369. if (p)
  370. __put_super(p);
  371. p = sb;
  372. }
  373. }
  374. if (p)
  375. __put_super(p);
  376. spin_unlock(&sb_lock);
  377. }
  378. /**
  379. * iterate_supers - call function for all active superblocks
  380. * @f: function to call
  381. * @arg: argument to pass to it
  382. *
  383. * Scans the superblock list and calls given function, passing it
  384. * locked superblock and given argument.
  385. */
  386. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  387. {
  388. struct super_block *sb, *p = NULL;
  389. spin_lock(&sb_lock);
  390. list_for_each_entry(sb, &super_blocks, s_list) {
  391. if (list_empty(&sb->s_instances))
  392. continue;
  393. sb->s_count++;
  394. spin_unlock(&sb_lock);
  395. down_read(&sb->s_umount);
  396. if (sb->s_root)
  397. f(sb, arg);
  398. up_read(&sb->s_umount);
  399. spin_lock(&sb_lock);
  400. if (p)
  401. __put_super(p);
  402. p = sb;
  403. }
  404. if (p)
  405. __put_super(p);
  406. spin_unlock(&sb_lock);
  407. }
  408. /**
  409. * get_super - get the superblock of a device
  410. * @bdev: device to get the superblock for
  411. *
  412. * Scans the superblock list and finds the superblock of the file system
  413. * mounted on the device given. %NULL is returned if no match is found.
  414. */
  415. struct super_block *get_super(struct block_device *bdev)
  416. {
  417. struct super_block *sb;
  418. if (!bdev)
  419. return NULL;
  420. spin_lock(&sb_lock);
  421. rescan:
  422. list_for_each_entry(sb, &super_blocks, s_list) {
  423. if (list_empty(&sb->s_instances))
  424. continue;
  425. if (sb->s_bdev == bdev) {
  426. sb->s_count++;
  427. spin_unlock(&sb_lock);
  428. down_read(&sb->s_umount);
  429. /* still alive? */
  430. if (sb->s_root)
  431. return sb;
  432. up_read(&sb->s_umount);
  433. /* nope, got unmounted */
  434. spin_lock(&sb_lock);
  435. __put_super(sb);
  436. goto rescan;
  437. }
  438. }
  439. spin_unlock(&sb_lock);
  440. return NULL;
  441. }
  442. EXPORT_SYMBOL(get_super);
  443. /**
  444. * get_active_super - get an active reference to the superblock of a device
  445. * @bdev: device to get the superblock for
  446. *
  447. * Scans the superblock list and finds the superblock of the file system
  448. * mounted on the device given. Returns the superblock with an active
  449. * reference or %NULL if none was found.
  450. */
  451. struct super_block *get_active_super(struct block_device *bdev)
  452. {
  453. struct super_block *sb;
  454. if (!bdev)
  455. return NULL;
  456. restart:
  457. spin_lock(&sb_lock);
  458. list_for_each_entry(sb, &super_blocks, s_list) {
  459. if (list_empty(&sb->s_instances))
  460. continue;
  461. if (sb->s_bdev == bdev) {
  462. if (grab_super(sb)) /* drops sb_lock */
  463. return sb;
  464. else
  465. goto restart;
  466. }
  467. }
  468. spin_unlock(&sb_lock);
  469. return NULL;
  470. }
  471. struct super_block *user_get_super(dev_t dev)
  472. {
  473. struct super_block *sb;
  474. spin_lock(&sb_lock);
  475. rescan:
  476. list_for_each_entry(sb, &super_blocks, s_list) {
  477. if (list_empty(&sb->s_instances))
  478. continue;
  479. if (sb->s_dev == dev) {
  480. sb->s_count++;
  481. spin_unlock(&sb_lock);
  482. down_read(&sb->s_umount);
  483. /* still alive? */
  484. if (sb->s_root)
  485. return sb;
  486. up_read(&sb->s_umount);
  487. /* nope, got unmounted */
  488. spin_lock(&sb_lock);
  489. __put_super(sb);
  490. goto rescan;
  491. }
  492. }
  493. spin_unlock(&sb_lock);
  494. return NULL;
  495. }
  496. /**
  497. * do_remount_sb - asks filesystem to change mount options.
  498. * @sb: superblock in question
  499. * @flags: numeric part of options
  500. * @data: the rest of options
  501. * @force: whether or not to force the change
  502. *
  503. * Alters the mount options of a mounted file system.
  504. */
  505. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  506. {
  507. int retval;
  508. int remount_ro;
  509. if (sb->s_frozen != SB_UNFROZEN)
  510. return -EBUSY;
  511. #ifdef CONFIG_BLOCK
  512. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  513. return -EACCES;
  514. #endif
  515. if (flags & MS_RDONLY)
  516. acct_auto_close(sb);
  517. shrink_dcache_sb(sb);
  518. sync_filesystem(sb);
  519. remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
  520. /* If we are remounting RDONLY and current sb is read/write,
  521. make sure there are no rw files opened */
  522. if (remount_ro) {
  523. if (force)
  524. mark_files_ro(sb);
  525. else if (!fs_may_remount_ro(sb))
  526. return -EBUSY;
  527. }
  528. if (sb->s_op->remount_fs) {
  529. retval = sb->s_op->remount_fs(sb, &flags, data);
  530. if (retval)
  531. return retval;
  532. }
  533. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  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, *p = NULL;
  549. spin_lock(&sb_lock);
  550. list_for_each_entry(sb, &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. if (p)
  565. __put_super(p);
  566. p = sb;
  567. }
  568. if (p)
  569. __put_super(p);
  570. spin_unlock(&sb_lock);
  571. kfree(work);
  572. printk("Emergency Remount complete\n");
  573. }
  574. void emergency_remount(void)
  575. {
  576. struct work_struct *work;
  577. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  578. if (work) {
  579. INIT_WORK(work, do_emergency_remount);
  580. schedule_work(work);
  581. }
  582. }
  583. /*
  584. * Unnamed block devices are dummy devices used by virtual
  585. * filesystems which don't use real block-devices. -- jrs
  586. */
  587. static DEFINE_IDA(unnamed_dev_ida);
  588. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  589. static int unnamed_dev_start = 0; /* don't bother trying below it */
  590. int set_anon_super(struct super_block *s, void *data)
  591. {
  592. int dev;
  593. int error;
  594. retry:
  595. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  596. return -ENOMEM;
  597. spin_lock(&unnamed_dev_lock);
  598. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  599. if (!error)
  600. unnamed_dev_start = dev + 1;
  601. spin_unlock(&unnamed_dev_lock);
  602. if (error == -EAGAIN)
  603. /* We raced and lost with another CPU. */
  604. goto retry;
  605. else if (error)
  606. return -EAGAIN;
  607. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  608. spin_lock(&unnamed_dev_lock);
  609. ida_remove(&unnamed_dev_ida, dev);
  610. if (unnamed_dev_start > dev)
  611. unnamed_dev_start = dev;
  612. spin_unlock(&unnamed_dev_lock);
  613. return -EMFILE;
  614. }
  615. s->s_dev = MKDEV(0, dev & MINORMASK);
  616. s->s_bdi = &noop_backing_dev_info;
  617. return 0;
  618. }
  619. EXPORT_SYMBOL(set_anon_super);
  620. void kill_anon_super(struct super_block *sb)
  621. {
  622. int slot = MINOR(sb->s_dev);
  623. generic_shutdown_super(sb);
  624. spin_lock(&unnamed_dev_lock);
  625. ida_remove(&unnamed_dev_ida, slot);
  626. if (slot < unnamed_dev_start)
  627. unnamed_dev_start = slot;
  628. spin_unlock(&unnamed_dev_lock);
  629. }
  630. EXPORT_SYMBOL(kill_anon_super);
  631. void kill_litter_super(struct super_block *sb)
  632. {
  633. if (sb->s_root)
  634. d_genocide(sb->s_root);
  635. kill_anon_super(sb);
  636. }
  637. EXPORT_SYMBOL(kill_litter_super);
  638. static int ns_test_super(struct super_block *sb, void *data)
  639. {
  640. return sb->s_fs_info == data;
  641. }
  642. static int ns_set_super(struct super_block *sb, void *data)
  643. {
  644. sb->s_fs_info = data;
  645. return set_anon_super(sb, NULL);
  646. }
  647. struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
  648. void *data, int (*fill_super)(struct super_block *, void *, int))
  649. {
  650. struct super_block *sb;
  651. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  652. if (IS_ERR(sb))
  653. return ERR_CAST(sb);
  654. if (!sb->s_root) {
  655. int err;
  656. sb->s_flags = flags;
  657. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  658. if (err) {
  659. deactivate_locked_super(sb);
  660. return ERR_PTR(err);
  661. }
  662. sb->s_flags |= MS_ACTIVE;
  663. }
  664. return dget(sb->s_root);
  665. }
  666. EXPORT_SYMBOL(mount_ns);
  667. #ifdef CONFIG_BLOCK
  668. static int set_bdev_super(struct super_block *s, void *data)
  669. {
  670. s->s_bdev = data;
  671. s->s_dev = s->s_bdev->bd_dev;
  672. /*
  673. * We set the bdi here to the queue backing, file systems can
  674. * overwrite this in ->fill_super()
  675. */
  676. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  677. return 0;
  678. }
  679. static int test_bdev_super(struct super_block *s, void *data)
  680. {
  681. return (void *)s->s_bdev == data;
  682. }
  683. struct dentry *mount_bdev(struct file_system_type *fs_type,
  684. int flags, const char *dev_name, void *data,
  685. int (*fill_super)(struct super_block *, void *, int))
  686. {
  687. struct block_device *bdev;
  688. struct super_block *s;
  689. fmode_t mode = FMODE_READ | FMODE_EXCL;
  690. int error = 0;
  691. if (!(flags & MS_RDONLY))
  692. mode |= FMODE_WRITE;
  693. bdev = blkdev_get_by_path(dev_name, mode, fs_type);
  694. if (IS_ERR(bdev))
  695. return ERR_CAST(bdev);
  696. /*
  697. * once the super is inserted into the list by sget, s_umount
  698. * will protect the lockfs code from trying to start a snapshot
  699. * while we are mounting
  700. */
  701. mutex_lock(&bdev->bd_fsfreeze_mutex);
  702. if (bdev->bd_fsfreeze_count > 0) {
  703. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  704. error = -EBUSY;
  705. goto error_bdev;
  706. }
  707. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  708. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  709. if (IS_ERR(s))
  710. goto error_s;
  711. if (s->s_root) {
  712. if ((flags ^ s->s_flags) & MS_RDONLY) {
  713. deactivate_locked_super(s);
  714. error = -EBUSY;
  715. goto error_bdev;
  716. }
  717. /*
  718. * s_umount nests inside bd_mutex during
  719. * __invalidate_device(). blkdev_put() acquires
  720. * bd_mutex and can't be called under s_umount. Drop
  721. * s_umount temporarily. This is safe as we're
  722. * holding an active reference.
  723. */
  724. up_write(&s->s_umount);
  725. blkdev_put(bdev, mode);
  726. down_write(&s->s_umount);
  727. } else {
  728. char b[BDEVNAME_SIZE];
  729. s->s_flags = flags;
  730. s->s_mode = mode;
  731. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  732. sb_set_blocksize(s, block_size(bdev));
  733. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  734. if (error) {
  735. deactivate_locked_super(s);
  736. goto error;
  737. }
  738. s->s_flags |= MS_ACTIVE;
  739. bdev->bd_super = s;
  740. }
  741. return dget(s->s_root);
  742. error_s:
  743. error = PTR_ERR(s);
  744. error_bdev:
  745. blkdev_put(bdev, mode);
  746. error:
  747. return ERR_PTR(error);
  748. }
  749. EXPORT_SYMBOL(mount_bdev);
  750. int get_sb_bdev(struct file_system_type *fs_type,
  751. int flags, const char *dev_name, void *data,
  752. int (*fill_super)(struct super_block *, void *, int),
  753. struct vfsmount *mnt)
  754. {
  755. struct dentry *root;
  756. root = mount_bdev(fs_type, flags, dev_name, data, fill_super);
  757. if (IS_ERR(root))
  758. return PTR_ERR(root);
  759. mnt->mnt_root = root;
  760. mnt->mnt_sb = root->d_sb;
  761. return 0;
  762. }
  763. EXPORT_SYMBOL(get_sb_bdev);
  764. void kill_block_super(struct super_block *sb)
  765. {
  766. struct block_device *bdev = sb->s_bdev;
  767. fmode_t mode = sb->s_mode;
  768. bdev->bd_super = NULL;
  769. generic_shutdown_super(sb);
  770. sync_blockdev(bdev);
  771. WARN_ON_ONCE(!(mode & FMODE_EXCL));
  772. blkdev_put(bdev, mode | FMODE_EXCL);
  773. }
  774. EXPORT_SYMBOL(kill_block_super);
  775. #endif
  776. struct dentry *mount_nodev(struct file_system_type *fs_type,
  777. int flags, void *data,
  778. int (*fill_super)(struct super_block *, void *, int))
  779. {
  780. int error;
  781. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  782. if (IS_ERR(s))
  783. return ERR_CAST(s);
  784. s->s_flags = flags;
  785. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  786. if (error) {
  787. deactivate_locked_super(s);
  788. return ERR_PTR(error);
  789. }
  790. s->s_flags |= MS_ACTIVE;
  791. return dget(s->s_root);
  792. }
  793. EXPORT_SYMBOL(mount_nodev);
  794. int get_sb_nodev(struct file_system_type *fs_type,
  795. int flags, void *data,
  796. int (*fill_super)(struct super_block *, void *, int),
  797. struct vfsmount *mnt)
  798. {
  799. struct dentry *root;
  800. root = mount_nodev(fs_type, flags, data, fill_super);
  801. if (IS_ERR(root))
  802. return PTR_ERR(root);
  803. mnt->mnt_root = root;
  804. mnt->mnt_sb = root->d_sb;
  805. return 0;
  806. }
  807. EXPORT_SYMBOL(get_sb_nodev);
  808. static int compare_single(struct super_block *s, void *p)
  809. {
  810. return 1;
  811. }
  812. struct dentry *mount_single(struct file_system_type *fs_type,
  813. int flags, void *data,
  814. int (*fill_super)(struct super_block *, void *, int))
  815. {
  816. struct super_block *s;
  817. int error;
  818. s = sget(fs_type, compare_single, set_anon_super, NULL);
  819. if (IS_ERR(s))
  820. return ERR_CAST(s);
  821. if (!s->s_root) {
  822. s->s_flags = flags;
  823. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  824. if (error) {
  825. deactivate_locked_super(s);
  826. return ERR_PTR(error);
  827. }
  828. s->s_flags |= MS_ACTIVE;
  829. } else {
  830. do_remount_sb(s, flags, data, 0);
  831. }
  832. return dget(s->s_root);
  833. }
  834. EXPORT_SYMBOL(mount_single);
  835. int get_sb_single(struct file_system_type *fs_type,
  836. int flags, void *data,
  837. int (*fill_super)(struct super_block *, void *, int),
  838. struct vfsmount *mnt)
  839. {
  840. struct dentry *root;
  841. root = mount_single(fs_type, flags, data, fill_super);
  842. if (IS_ERR(root))
  843. return PTR_ERR(root);
  844. mnt->mnt_root = root;
  845. mnt->mnt_sb = root->d_sb;
  846. return 0;
  847. }
  848. EXPORT_SYMBOL(get_sb_single);
  849. struct vfsmount *
  850. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  851. {
  852. struct vfsmount *mnt;
  853. struct dentry *root;
  854. char *secdata = NULL;
  855. int error;
  856. if (!type)
  857. return ERR_PTR(-ENODEV);
  858. error = -ENOMEM;
  859. mnt = alloc_vfsmnt(name);
  860. if (!mnt)
  861. goto out;
  862. if (flags & MS_KERNMOUNT)
  863. mnt->mnt_flags = MNT_INTERNAL;
  864. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  865. secdata = alloc_secdata();
  866. if (!secdata)
  867. goto out_mnt;
  868. error = security_sb_copy_data(data, secdata);
  869. if (error)
  870. goto out_free_secdata;
  871. }
  872. if (type->mount) {
  873. root = type->mount(type, flags, name, data);
  874. if (IS_ERR(root)) {
  875. error = PTR_ERR(root);
  876. goto out_free_secdata;
  877. }
  878. mnt->mnt_root = root;
  879. mnt->mnt_sb = root->d_sb;
  880. } else {
  881. error = type->get_sb(type, flags, name, data, mnt);
  882. if (error < 0)
  883. goto out_free_secdata;
  884. }
  885. BUG_ON(!mnt->mnt_sb);
  886. WARN_ON(!mnt->mnt_sb->s_bdi);
  887. mnt->mnt_sb->s_flags |= MS_BORN;
  888. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  889. if (error)
  890. goto out_sb;
  891. /*
  892. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  893. * but s_maxbytes was an unsigned long long for many releases. Throw
  894. * this warning for a little while to try and catch filesystems that
  895. * violate this rule. This warning should be either removed or
  896. * converted to a BUG() in 2.6.34.
  897. */
  898. WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  899. "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
  900. mnt->mnt_mountpoint = mnt->mnt_root;
  901. mnt->mnt_parent = mnt;
  902. up_write(&mnt->mnt_sb->s_umount);
  903. free_secdata(secdata);
  904. return mnt;
  905. out_sb:
  906. dput(mnt->mnt_root);
  907. deactivate_locked_super(mnt->mnt_sb);
  908. out_free_secdata:
  909. free_secdata(secdata);
  910. out_mnt:
  911. free_vfsmnt(mnt);
  912. out:
  913. return ERR_PTR(error);
  914. }
  915. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  916. /**
  917. * freeze_super - lock the filesystem and force it into a consistent state
  918. * @sb: the super to lock
  919. *
  920. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  921. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  922. * -EBUSY.
  923. */
  924. int freeze_super(struct super_block *sb)
  925. {
  926. int ret;
  927. atomic_inc(&sb->s_active);
  928. down_write(&sb->s_umount);
  929. if (sb->s_frozen) {
  930. deactivate_locked_super(sb);
  931. return -EBUSY;
  932. }
  933. if (sb->s_flags & MS_RDONLY) {
  934. sb->s_frozen = SB_FREEZE_TRANS;
  935. smp_wmb();
  936. up_write(&sb->s_umount);
  937. return 0;
  938. }
  939. sb->s_frozen = SB_FREEZE_WRITE;
  940. smp_wmb();
  941. sync_filesystem(sb);
  942. sb->s_frozen = SB_FREEZE_TRANS;
  943. smp_wmb();
  944. sync_blockdev(sb->s_bdev);
  945. if (sb->s_op->freeze_fs) {
  946. ret = sb->s_op->freeze_fs(sb);
  947. if (ret) {
  948. printk(KERN_ERR
  949. "VFS:Filesystem freeze failed\n");
  950. sb->s_frozen = SB_UNFROZEN;
  951. deactivate_locked_super(sb);
  952. return ret;
  953. }
  954. }
  955. up_write(&sb->s_umount);
  956. return 0;
  957. }
  958. EXPORT_SYMBOL(freeze_super);
  959. /**
  960. * thaw_super -- unlock filesystem
  961. * @sb: the super to thaw
  962. *
  963. * Unlocks the filesystem and marks it writeable again after freeze_super().
  964. */
  965. int thaw_super(struct super_block *sb)
  966. {
  967. int error;
  968. down_write(&sb->s_umount);
  969. if (sb->s_frozen == SB_UNFROZEN) {
  970. up_write(&sb->s_umount);
  971. return -EINVAL;
  972. }
  973. if (sb->s_flags & MS_RDONLY)
  974. goto out;
  975. if (sb->s_op->unfreeze_fs) {
  976. error = sb->s_op->unfreeze_fs(sb);
  977. if (error) {
  978. printk(KERN_ERR
  979. "VFS:Filesystem thaw failed\n");
  980. sb->s_frozen = SB_FREEZE_TRANS;
  981. up_write(&sb->s_umount);
  982. return error;
  983. }
  984. }
  985. out:
  986. sb->s_frozen = SB_UNFROZEN;
  987. smp_wmb();
  988. wake_up(&sb->s_wait_unfrozen);
  989. deactivate_locked_super(sb);
  990. return 0;
  991. }
  992. EXPORT_SYMBOL(thaw_super);
  993. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  994. {
  995. int err;
  996. const char *subtype = strchr(fstype, '.');
  997. if (subtype) {
  998. subtype++;
  999. err = -EINVAL;
  1000. if (!subtype[0])
  1001. goto err;
  1002. } else
  1003. subtype = "";
  1004. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  1005. err = -ENOMEM;
  1006. if (!mnt->mnt_sb->s_subtype)
  1007. goto err;
  1008. return mnt;
  1009. err:
  1010. mntput(mnt);
  1011. return ERR_PTR(err);
  1012. }
  1013. struct vfsmount *
  1014. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  1015. {
  1016. struct file_system_type *type = get_fs_type(fstype);
  1017. struct vfsmount *mnt;
  1018. if (!type)
  1019. return ERR_PTR(-ENODEV);
  1020. mnt = vfs_kern_mount(type, flags, name, data);
  1021. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1022. !mnt->mnt_sb->s_subtype)
  1023. mnt = fs_set_subtype(mnt, fstype);
  1024. put_filesystem(type);
  1025. return mnt;
  1026. }
  1027. EXPORT_SYMBOL_GPL(do_kern_mount);
  1028. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  1029. {
  1030. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  1031. }
  1032. EXPORT_SYMBOL_GPL(kern_mount_data);