super.c 24 KB

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