super.c 24 KB

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