super.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. put_filesystem(fs);
  169. put_super(s);
  170. } else {
  171. up_write(&s->s_umount);
  172. }
  173. }
  174. EXPORT_SYMBOL(deactivate_locked_super);
  175. /**
  176. * deactivate_super - drop an active reference to superblock
  177. * @s: superblock to deactivate
  178. *
  179. * Variant of deactivate_locked_super(), except that superblock is *not*
  180. * locked by caller. If we are going to drop the final active reference,
  181. * lock will be acquired prior to that.
  182. */
  183. void deactivate_super(struct super_block *s)
  184. {
  185. if (!atomic_add_unless(&s->s_active, -1, 1)) {
  186. down_write(&s->s_umount);
  187. deactivate_locked_super(s);
  188. }
  189. }
  190. EXPORT_SYMBOL(deactivate_super);
  191. /**
  192. * grab_super - acquire an active reference
  193. * @s: reference we are trying to make active
  194. *
  195. * Tries to acquire an active reference. grab_super() is used when we
  196. * had just found a superblock in super_blocks or fs_type->fs_supers
  197. * and want to turn it into a full-blown active reference. grab_super()
  198. * is called with sb_lock held and drops it. Returns 1 in case of
  199. * success, 0 if we had failed (superblock contents was already dead or
  200. * dying when grab_super() had been called).
  201. */
  202. static int grab_super(struct super_block *s) __releases(sb_lock)
  203. {
  204. if (atomic_inc_not_zero(&s->s_active)) {
  205. spin_unlock(&sb_lock);
  206. return 1;
  207. }
  208. /* it's going away */
  209. s->s_count++;
  210. spin_unlock(&sb_lock);
  211. /* wait for it to die */
  212. down_write(&s->s_umount);
  213. up_write(&s->s_umount);
  214. put_super(s);
  215. return 0;
  216. }
  217. /*
  218. * Superblock locking. We really ought to get rid of these two.
  219. */
  220. void lock_super(struct super_block * sb)
  221. {
  222. get_fs_excl();
  223. mutex_lock(&sb->s_lock);
  224. }
  225. void unlock_super(struct super_block * sb)
  226. {
  227. put_fs_excl();
  228. mutex_unlock(&sb->s_lock);
  229. }
  230. EXPORT_SYMBOL(lock_super);
  231. EXPORT_SYMBOL(unlock_super);
  232. /**
  233. * generic_shutdown_super - common helper for ->kill_sb()
  234. * @sb: superblock to kill
  235. *
  236. * generic_shutdown_super() does all fs-independent work on superblock
  237. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  238. * that need destruction out of superblock, call generic_shutdown_super()
  239. * and release aforementioned objects. Note: dentries and inodes _are_
  240. * taken care of and do not need specific handling.
  241. *
  242. * Upon calling this function, the filesystem may no longer alter or
  243. * rearrange the set of dentries belonging to this super_block, nor may it
  244. * change the attachments of dentries to inodes.
  245. */
  246. void generic_shutdown_super(struct super_block *sb)
  247. {
  248. const struct super_operations *sop = sb->s_op;
  249. if (sb->s_root) {
  250. shrink_dcache_for_umount(sb);
  251. sync_filesystem(sb);
  252. get_fs_excl();
  253. sb->s_flags &= ~MS_ACTIVE;
  254. fsnotify_unmount_inodes(&sb->s_inodes);
  255. evict_inodes(sb);
  256. if (sop->put_super)
  257. sop->put_super(sb);
  258. if (!list_empty(&sb->s_inodes)) {
  259. printk("VFS: Busy inodes after unmount of %s. "
  260. "Self-destruct in 5 seconds. Have a nice day...\n",
  261. sb->s_id);
  262. }
  263. put_fs_excl();
  264. }
  265. spin_lock(&sb_lock);
  266. /* should be initialized for __put_super_and_need_restart() */
  267. list_del_init(&sb->s_instances);
  268. spin_unlock(&sb_lock);
  269. up_write(&sb->s_umount);
  270. }
  271. EXPORT_SYMBOL(generic_shutdown_super);
  272. /**
  273. * sget - find or create a superblock
  274. * @type: filesystem type superblock should belong to
  275. * @test: comparison callback
  276. * @set: setup callback
  277. * @data: argument to each of them
  278. */
  279. struct super_block *sget(struct file_system_type *type,
  280. int (*test)(struct super_block *,void *),
  281. int (*set)(struct super_block *,void *),
  282. void *data)
  283. {
  284. struct super_block *s = NULL;
  285. struct super_block *old;
  286. int err;
  287. retry:
  288. spin_lock(&sb_lock);
  289. if (test) {
  290. list_for_each_entry(old, &type->fs_supers, s_instances) {
  291. if (!test(old, data))
  292. continue;
  293. if (!grab_super(old))
  294. goto retry;
  295. if (s) {
  296. up_write(&s->s_umount);
  297. destroy_super(s);
  298. s = NULL;
  299. }
  300. down_write(&old->s_umount);
  301. if (unlikely(!(old->s_flags & MS_BORN))) {
  302. deactivate_locked_super(old);
  303. goto retry;
  304. }
  305. return old;
  306. }
  307. }
  308. if (!s) {
  309. spin_unlock(&sb_lock);
  310. s = alloc_super(type);
  311. if (!s)
  312. return ERR_PTR(-ENOMEM);
  313. goto retry;
  314. }
  315. err = set(s, data);
  316. if (err) {
  317. spin_unlock(&sb_lock);
  318. up_write(&s->s_umount);
  319. destroy_super(s);
  320. return ERR_PTR(err);
  321. }
  322. s->s_type = type;
  323. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  324. list_add_tail(&s->s_list, &super_blocks);
  325. list_add(&s->s_instances, &type->fs_supers);
  326. spin_unlock(&sb_lock);
  327. get_filesystem(type);
  328. return s;
  329. }
  330. EXPORT_SYMBOL(sget);
  331. void drop_super(struct super_block *sb)
  332. {
  333. up_read(&sb->s_umount);
  334. put_super(sb);
  335. }
  336. EXPORT_SYMBOL(drop_super);
  337. /**
  338. * sync_supers - helper for periodic superblock writeback
  339. *
  340. * Call the write_super method if present on all dirty superblocks in
  341. * the system. This is for the periodic writeback used by most older
  342. * filesystems. For data integrity superblock writeback use
  343. * sync_filesystems() instead.
  344. *
  345. * Note: check the dirty flag before waiting, so we don't
  346. * hold up the sync while mounting a device. (The newly
  347. * mounted device won't need syncing.)
  348. */
  349. void sync_supers(void)
  350. {
  351. struct super_block *sb, *p = NULL;
  352. spin_lock(&sb_lock);
  353. list_for_each_entry(sb, &super_blocks, s_list) {
  354. if (list_empty(&sb->s_instances))
  355. continue;
  356. if (sb->s_op->write_super && sb->s_dirt) {
  357. sb->s_count++;
  358. spin_unlock(&sb_lock);
  359. down_read(&sb->s_umount);
  360. if (sb->s_root && sb->s_dirt)
  361. sb->s_op->write_super(sb);
  362. up_read(&sb->s_umount);
  363. spin_lock(&sb_lock);
  364. if (p)
  365. __put_super(p);
  366. p = sb;
  367. }
  368. }
  369. if (p)
  370. __put_super(p);
  371. spin_unlock(&sb_lock);
  372. }
  373. /**
  374. * iterate_supers - call function for all active superblocks
  375. * @f: function to call
  376. * @arg: argument to pass to it
  377. *
  378. * Scans the superblock list and calls given function, passing it
  379. * locked superblock and given argument.
  380. */
  381. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  382. {
  383. struct super_block *sb, *p = NULL;
  384. spin_lock(&sb_lock);
  385. list_for_each_entry(sb, &super_blocks, s_list) {
  386. if (list_empty(&sb->s_instances))
  387. continue;
  388. sb->s_count++;
  389. spin_unlock(&sb_lock);
  390. down_read(&sb->s_umount);
  391. if (sb->s_root)
  392. f(sb, arg);
  393. up_read(&sb->s_umount);
  394. spin_lock(&sb_lock);
  395. if (p)
  396. __put_super(p);
  397. p = sb;
  398. }
  399. if (p)
  400. __put_super(p);
  401. spin_unlock(&sb_lock);
  402. }
  403. /**
  404. * get_super - get the superblock of a device
  405. * @bdev: device to get the superblock for
  406. *
  407. * Scans the superblock list and finds the superblock of the file system
  408. * mounted on the device given. %NULL is returned if no match is found.
  409. */
  410. struct super_block *get_super(struct block_device *bdev)
  411. {
  412. struct super_block *sb;
  413. if (!bdev)
  414. return NULL;
  415. spin_lock(&sb_lock);
  416. rescan:
  417. list_for_each_entry(sb, &super_blocks, s_list) {
  418. if (list_empty(&sb->s_instances))
  419. continue;
  420. if (sb->s_bdev == bdev) {
  421. sb->s_count++;
  422. spin_unlock(&sb_lock);
  423. down_read(&sb->s_umount);
  424. /* still alive? */
  425. if (sb->s_root)
  426. return sb;
  427. up_read(&sb->s_umount);
  428. /* nope, got unmounted */
  429. spin_lock(&sb_lock);
  430. __put_super(sb);
  431. goto rescan;
  432. }
  433. }
  434. spin_unlock(&sb_lock);
  435. return NULL;
  436. }
  437. EXPORT_SYMBOL(get_super);
  438. /**
  439. * get_active_super - get an active reference to the superblock of a device
  440. * @bdev: device to get the superblock for
  441. *
  442. * Scans the superblock list and finds the superblock of the file system
  443. * mounted on the device given. Returns the superblock with an active
  444. * reference or %NULL if none was found.
  445. */
  446. struct super_block *get_active_super(struct block_device *bdev)
  447. {
  448. struct super_block *sb;
  449. if (!bdev)
  450. return NULL;
  451. restart:
  452. spin_lock(&sb_lock);
  453. list_for_each_entry(sb, &super_blocks, s_list) {
  454. if (list_empty(&sb->s_instances))
  455. continue;
  456. if (sb->s_bdev == bdev) {
  457. if (grab_super(sb)) /* drops sb_lock */
  458. return sb;
  459. else
  460. goto restart;
  461. }
  462. }
  463. spin_unlock(&sb_lock);
  464. return NULL;
  465. }
  466. struct super_block *user_get_super(dev_t dev)
  467. {
  468. struct super_block *sb;
  469. spin_lock(&sb_lock);
  470. rescan:
  471. list_for_each_entry(sb, &super_blocks, s_list) {
  472. if (list_empty(&sb->s_instances))
  473. continue;
  474. if (sb->s_dev == dev) {
  475. sb->s_count++;
  476. spin_unlock(&sb_lock);
  477. down_read(&sb->s_umount);
  478. /* still alive? */
  479. if (sb->s_root)
  480. return sb;
  481. up_read(&sb->s_umount);
  482. /* nope, got unmounted */
  483. spin_lock(&sb_lock);
  484. __put_super(sb);
  485. goto rescan;
  486. }
  487. }
  488. spin_unlock(&sb_lock);
  489. return NULL;
  490. }
  491. /**
  492. * do_remount_sb - asks filesystem to change mount options.
  493. * @sb: superblock in question
  494. * @flags: numeric part of options
  495. * @data: the rest of options
  496. * @force: whether or not to force the change
  497. *
  498. * Alters the mount options of a mounted file system.
  499. */
  500. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  501. {
  502. int retval;
  503. int remount_ro;
  504. if (sb->s_frozen != SB_UNFROZEN)
  505. return -EBUSY;
  506. #ifdef CONFIG_BLOCK
  507. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  508. return -EACCES;
  509. #endif
  510. if (flags & MS_RDONLY)
  511. acct_auto_close(sb);
  512. shrink_dcache_sb(sb);
  513. sync_filesystem(sb);
  514. remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
  515. /* If we are remounting RDONLY and current sb is read/write,
  516. make sure there are no rw files opened */
  517. if (remount_ro) {
  518. if (force)
  519. mark_files_ro(sb);
  520. else if (!fs_may_remount_ro(sb))
  521. return -EBUSY;
  522. }
  523. if (sb->s_op->remount_fs) {
  524. retval = sb->s_op->remount_fs(sb, &flags, data);
  525. if (retval)
  526. return retval;
  527. }
  528. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  529. /*
  530. * Some filesystems modify their metadata via some other path than the
  531. * bdev buffer cache (eg. use a private mapping, or directories in
  532. * pagecache, etc). Also file data modifications go via their own
  533. * mappings. So If we try to mount readonly then copy the filesystem
  534. * from bdev, we could get stale data, so invalidate it to give a best
  535. * effort at coherency.
  536. */
  537. if (remount_ro && sb->s_bdev)
  538. invalidate_bdev(sb->s_bdev);
  539. return 0;
  540. }
  541. static void do_emergency_remount(struct work_struct *work)
  542. {
  543. struct super_block *sb, *p = NULL;
  544. spin_lock(&sb_lock);
  545. list_for_each_entry(sb, &super_blocks, s_list) {
  546. if (list_empty(&sb->s_instances))
  547. continue;
  548. sb->s_count++;
  549. spin_unlock(&sb_lock);
  550. down_write(&sb->s_umount);
  551. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  552. /*
  553. * What lock protects sb->s_flags??
  554. */
  555. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  556. }
  557. up_write(&sb->s_umount);
  558. spin_lock(&sb_lock);
  559. if (p)
  560. __put_super(p);
  561. p = sb;
  562. }
  563. if (p)
  564. __put_super(p);
  565. spin_unlock(&sb_lock);
  566. kfree(work);
  567. printk("Emergency Remount complete\n");
  568. }
  569. void emergency_remount(void)
  570. {
  571. struct work_struct *work;
  572. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  573. if (work) {
  574. INIT_WORK(work, do_emergency_remount);
  575. schedule_work(work);
  576. }
  577. }
  578. /*
  579. * Unnamed block devices are dummy devices used by virtual
  580. * filesystems which don't use real block-devices. -- jrs
  581. */
  582. static DEFINE_IDA(unnamed_dev_ida);
  583. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  584. static int unnamed_dev_start = 0; /* don't bother trying below it */
  585. int set_anon_super(struct super_block *s, void *data)
  586. {
  587. int dev;
  588. int error;
  589. retry:
  590. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  591. return -ENOMEM;
  592. spin_lock(&unnamed_dev_lock);
  593. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  594. if (!error)
  595. unnamed_dev_start = dev + 1;
  596. spin_unlock(&unnamed_dev_lock);
  597. if (error == -EAGAIN)
  598. /* We raced and lost with another CPU. */
  599. goto retry;
  600. else if (error)
  601. return -EAGAIN;
  602. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  603. spin_lock(&unnamed_dev_lock);
  604. ida_remove(&unnamed_dev_ida, dev);
  605. if (unnamed_dev_start > dev)
  606. unnamed_dev_start = dev;
  607. spin_unlock(&unnamed_dev_lock);
  608. return -EMFILE;
  609. }
  610. s->s_dev = MKDEV(0, dev & MINORMASK);
  611. s->s_bdi = &noop_backing_dev_info;
  612. return 0;
  613. }
  614. EXPORT_SYMBOL(set_anon_super);
  615. void kill_anon_super(struct super_block *sb)
  616. {
  617. int slot = MINOR(sb->s_dev);
  618. generic_shutdown_super(sb);
  619. spin_lock(&unnamed_dev_lock);
  620. ida_remove(&unnamed_dev_ida, slot);
  621. if (slot < unnamed_dev_start)
  622. unnamed_dev_start = slot;
  623. spin_unlock(&unnamed_dev_lock);
  624. }
  625. EXPORT_SYMBOL(kill_anon_super);
  626. void kill_litter_super(struct super_block *sb)
  627. {
  628. if (sb->s_root)
  629. d_genocide(sb->s_root);
  630. kill_anon_super(sb);
  631. }
  632. EXPORT_SYMBOL(kill_litter_super);
  633. static int ns_test_super(struct super_block *sb, void *data)
  634. {
  635. return sb->s_fs_info == data;
  636. }
  637. static int ns_set_super(struct super_block *sb, void *data)
  638. {
  639. sb->s_fs_info = data;
  640. return set_anon_super(sb, NULL);
  641. }
  642. struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
  643. void *data, int (*fill_super)(struct super_block *, void *, int))
  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 ERR_CAST(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_PTR(err);
  656. }
  657. sb->s_flags |= MS_ACTIVE;
  658. }
  659. return dget(sb->s_root);
  660. }
  661. EXPORT_SYMBOL(mount_ns);
  662. #ifdef CONFIG_BLOCK
  663. static int set_bdev_super(struct super_block *s, void *data)
  664. {
  665. s->s_bdev = data;
  666. s->s_dev = s->s_bdev->bd_dev;
  667. /*
  668. * We set the bdi here to the queue backing, file systems can
  669. * overwrite this in ->fill_super()
  670. */
  671. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  672. return 0;
  673. }
  674. static int test_bdev_super(struct super_block *s, void *data)
  675. {
  676. return (void *)s->s_bdev == data;
  677. }
  678. struct dentry *mount_bdev(struct file_system_type *fs_type,
  679. int flags, const char *dev_name, void *data,
  680. int (*fill_super)(struct super_block *, void *, int))
  681. {
  682. struct block_device *bdev;
  683. struct super_block *s;
  684. fmode_t mode = FMODE_READ;
  685. int error = 0;
  686. if (!(flags & MS_RDONLY))
  687. mode |= FMODE_WRITE;
  688. bdev = open_bdev_exclusive(dev_name, mode, fs_type);
  689. if (IS_ERR(bdev))
  690. return ERR_CAST(bdev);
  691. /*
  692. * once the super is inserted into the list by sget, s_umount
  693. * will protect the lockfs code from trying to start a snapshot
  694. * while we are mounting
  695. */
  696. mutex_lock(&bdev->bd_fsfreeze_mutex);
  697. if (bdev->bd_fsfreeze_count > 0) {
  698. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  699. error = -EBUSY;
  700. goto error_bdev;
  701. }
  702. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  703. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  704. if (IS_ERR(s))
  705. goto error_s;
  706. if (s->s_root) {
  707. if ((flags ^ s->s_flags) & MS_RDONLY) {
  708. deactivate_locked_super(s);
  709. error = -EBUSY;
  710. goto error_bdev;
  711. }
  712. /*
  713. * s_umount nests inside bd_mutex during
  714. * __invalidate_device(). close_bdev_exclusive()
  715. * acquires bd_mutex and can't be called under
  716. * s_umount. Drop s_umount temporarily. This is safe
  717. * as we're holding an active reference.
  718. */
  719. up_write(&s->s_umount);
  720. close_bdev_exclusive(bdev, mode);
  721. down_write(&s->s_umount);
  722. } else {
  723. char b[BDEVNAME_SIZE];
  724. s->s_flags = flags;
  725. s->s_mode = mode;
  726. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  727. sb_set_blocksize(s, block_size(bdev));
  728. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  729. if (error) {
  730. deactivate_locked_super(s);
  731. goto error;
  732. }
  733. s->s_flags |= MS_ACTIVE;
  734. bdev->bd_super = s;
  735. }
  736. return dget(s->s_root);
  737. error_s:
  738. error = PTR_ERR(s);
  739. error_bdev:
  740. close_bdev_exclusive(bdev, mode);
  741. error:
  742. return ERR_PTR(error);
  743. }
  744. EXPORT_SYMBOL(mount_bdev);
  745. int get_sb_bdev(struct file_system_type *fs_type,
  746. int flags, const char *dev_name, void *data,
  747. int (*fill_super)(struct super_block *, void *, int),
  748. struct vfsmount *mnt)
  749. {
  750. struct dentry *root;
  751. root = mount_bdev(fs_type, flags, dev_name, data, fill_super);
  752. if (IS_ERR(root))
  753. return PTR_ERR(root);
  754. mnt->mnt_root = root;
  755. mnt->mnt_sb = root->d_sb;
  756. return 0;
  757. }
  758. EXPORT_SYMBOL(get_sb_bdev);
  759. void kill_block_super(struct super_block *sb)
  760. {
  761. struct block_device *bdev = sb->s_bdev;
  762. fmode_t mode = sb->s_mode;
  763. bdev->bd_super = NULL;
  764. generic_shutdown_super(sb);
  765. sync_blockdev(bdev);
  766. close_bdev_exclusive(bdev, mode);
  767. }
  768. EXPORT_SYMBOL(kill_block_super);
  769. #endif
  770. struct dentry *mount_nodev(struct file_system_type *fs_type,
  771. int flags, void *data,
  772. int (*fill_super)(struct super_block *, void *, int))
  773. {
  774. int error;
  775. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  776. if (IS_ERR(s))
  777. return ERR_CAST(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 ERR_PTR(error);
  783. }
  784. s->s_flags |= MS_ACTIVE;
  785. return dget(s->s_root);
  786. }
  787. EXPORT_SYMBOL(mount_nodev);
  788. int get_sb_nodev(struct file_system_type *fs_type,
  789. int flags, void *data,
  790. int (*fill_super)(struct super_block *, void *, int),
  791. struct vfsmount *mnt)
  792. {
  793. struct dentry *root;
  794. root = mount_nodev(fs_type, flags, data, fill_super);
  795. if (IS_ERR(root))
  796. return PTR_ERR(root);
  797. mnt->mnt_root = root;
  798. mnt->mnt_sb = root->d_sb;
  799. return 0;
  800. }
  801. EXPORT_SYMBOL(get_sb_nodev);
  802. static int compare_single(struct super_block *s, void *p)
  803. {
  804. return 1;
  805. }
  806. struct dentry *mount_single(struct file_system_type *fs_type,
  807. int flags, void *data,
  808. int (*fill_super)(struct super_block *, void *, int))
  809. {
  810. struct super_block *s;
  811. int error;
  812. s = sget(fs_type, compare_single, set_anon_super, NULL);
  813. if (IS_ERR(s))
  814. return ERR_CAST(s);
  815. if (!s->s_root) {
  816. s->s_flags = flags;
  817. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  818. if (error) {
  819. deactivate_locked_super(s);
  820. return ERR_PTR(error);
  821. }
  822. s->s_flags |= MS_ACTIVE;
  823. } else {
  824. do_remount_sb(s, flags, data, 0);
  825. }
  826. return dget(s->s_root);
  827. }
  828. EXPORT_SYMBOL(mount_single);
  829. int get_sb_single(struct file_system_type *fs_type,
  830. int flags, void *data,
  831. int (*fill_super)(struct super_block *, void *, int),
  832. struct vfsmount *mnt)
  833. {
  834. struct dentry *root;
  835. root = mount_single(fs_type, flags, data, fill_super);
  836. if (IS_ERR(root))
  837. return PTR_ERR(root);
  838. mnt->mnt_root = root;
  839. mnt->mnt_sb = root->d_sb;
  840. return 0;
  841. }
  842. EXPORT_SYMBOL(get_sb_single);
  843. struct vfsmount *
  844. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  845. {
  846. struct vfsmount *mnt;
  847. struct dentry *root;
  848. char *secdata = NULL;
  849. int error;
  850. if (!type)
  851. return ERR_PTR(-ENODEV);
  852. error = -ENOMEM;
  853. mnt = alloc_vfsmnt(name);
  854. if (!mnt)
  855. goto out;
  856. if (flags & MS_KERNMOUNT)
  857. mnt->mnt_flags = MNT_INTERNAL;
  858. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  859. secdata = alloc_secdata();
  860. if (!secdata)
  861. goto out_mnt;
  862. error = security_sb_copy_data(data, secdata);
  863. if (error)
  864. goto out_free_secdata;
  865. }
  866. if (type->mount) {
  867. root = type->mount(type, flags, name, data);
  868. if (IS_ERR(root)) {
  869. error = PTR_ERR(root);
  870. goto out_free_secdata;
  871. }
  872. mnt->mnt_root = root;
  873. mnt->mnt_sb = root->d_sb;
  874. } else {
  875. error = type->get_sb(type, flags, name, data, mnt);
  876. if (error < 0)
  877. goto out_free_secdata;
  878. }
  879. BUG_ON(!mnt->mnt_sb);
  880. WARN_ON(!mnt->mnt_sb->s_bdi);
  881. mnt->mnt_sb->s_flags |= MS_BORN;
  882. error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
  883. if (error)
  884. goto out_sb;
  885. /*
  886. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  887. * but s_maxbytes was an unsigned long long for many releases. Throw
  888. * this warning for a little while to try and catch filesystems that
  889. * violate this rule. This warning should be either removed or
  890. * converted to a BUG() in 2.6.34.
  891. */
  892. WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  893. "negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
  894. mnt->mnt_mountpoint = mnt->mnt_root;
  895. mnt->mnt_parent = mnt;
  896. up_write(&mnt->mnt_sb->s_umount);
  897. free_secdata(secdata);
  898. return mnt;
  899. out_sb:
  900. dput(mnt->mnt_root);
  901. deactivate_locked_super(mnt->mnt_sb);
  902. out_free_secdata:
  903. free_secdata(secdata);
  904. out_mnt:
  905. free_vfsmnt(mnt);
  906. out:
  907. return ERR_PTR(error);
  908. }
  909. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  910. /**
  911. * freeze_super - lock the filesystem and force it into a consistent state
  912. * @sb: the super to lock
  913. *
  914. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  915. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  916. * -EBUSY.
  917. */
  918. int freeze_super(struct super_block *sb)
  919. {
  920. int ret;
  921. atomic_inc(&sb->s_active);
  922. down_write(&sb->s_umount);
  923. if (sb->s_frozen) {
  924. deactivate_locked_super(sb);
  925. return -EBUSY;
  926. }
  927. if (sb->s_flags & MS_RDONLY) {
  928. sb->s_frozen = SB_FREEZE_TRANS;
  929. smp_wmb();
  930. up_write(&sb->s_umount);
  931. return 0;
  932. }
  933. sb->s_frozen = SB_FREEZE_WRITE;
  934. smp_wmb();
  935. sync_filesystem(sb);
  936. sb->s_frozen = SB_FREEZE_TRANS;
  937. smp_wmb();
  938. sync_blockdev(sb->s_bdev);
  939. if (sb->s_op->freeze_fs) {
  940. ret = sb->s_op->freeze_fs(sb);
  941. if (ret) {
  942. printk(KERN_ERR
  943. "VFS:Filesystem freeze failed\n");
  944. sb->s_frozen = SB_UNFROZEN;
  945. deactivate_locked_super(sb);
  946. return ret;
  947. }
  948. }
  949. up_write(&sb->s_umount);
  950. return 0;
  951. }
  952. EXPORT_SYMBOL(freeze_super);
  953. /**
  954. * thaw_super -- unlock filesystem
  955. * @sb: the super to thaw
  956. *
  957. * Unlocks the filesystem and marks it writeable again after freeze_super().
  958. */
  959. int thaw_super(struct super_block *sb)
  960. {
  961. int error;
  962. down_write(&sb->s_umount);
  963. if (sb->s_frozen == SB_UNFROZEN) {
  964. up_write(&sb->s_umount);
  965. return -EINVAL;
  966. }
  967. if (sb->s_flags & MS_RDONLY)
  968. goto out;
  969. if (sb->s_op->unfreeze_fs) {
  970. error = sb->s_op->unfreeze_fs(sb);
  971. if (error) {
  972. printk(KERN_ERR
  973. "VFS:Filesystem thaw failed\n");
  974. sb->s_frozen = SB_FREEZE_TRANS;
  975. up_write(&sb->s_umount);
  976. return error;
  977. }
  978. }
  979. out:
  980. sb->s_frozen = SB_UNFROZEN;
  981. smp_wmb();
  982. wake_up(&sb->s_wait_unfrozen);
  983. deactivate_locked_super(sb);
  984. return 0;
  985. }
  986. EXPORT_SYMBOL(thaw_super);
  987. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  988. {
  989. int err;
  990. const char *subtype = strchr(fstype, '.');
  991. if (subtype) {
  992. subtype++;
  993. err = -EINVAL;
  994. if (!subtype[0])
  995. goto err;
  996. } else
  997. subtype = "";
  998. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  999. err = -ENOMEM;
  1000. if (!mnt->mnt_sb->s_subtype)
  1001. goto err;
  1002. return mnt;
  1003. err:
  1004. mntput_long(mnt);
  1005. return ERR_PTR(err);
  1006. }
  1007. struct vfsmount *
  1008. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  1009. {
  1010. struct file_system_type *type = get_fs_type(fstype);
  1011. struct vfsmount *mnt;
  1012. if (!type)
  1013. return ERR_PTR(-ENODEV);
  1014. mnt = vfs_kern_mount(type, flags, name, data);
  1015. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1016. !mnt->mnt_sb->s_subtype)
  1017. mnt = fs_set_subtype(mnt, fstype);
  1018. put_filesystem(type);
  1019. return mnt;
  1020. }
  1021. EXPORT_SYMBOL_GPL(do_kern_mount);
  1022. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  1023. {
  1024. return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  1025. }
  1026. EXPORT_SYMBOL_GPL(kern_mount_data);