super.c 26 KB

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