super.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. * iterate_supers_type - call function for superblocks of given type
  414. * @type: fs type
  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_type(struct file_system_type *type,
  422. void (*f)(struct super_block *, void *), void *arg)
  423. {
  424. struct super_block *sb, *p = NULL;
  425. spin_lock(&sb_lock);
  426. list_for_each_entry(sb, &type->fs_supers, s_instances) {
  427. sb->s_count++;
  428. spin_unlock(&sb_lock);
  429. down_read(&sb->s_umount);
  430. if (sb->s_root)
  431. f(sb, arg);
  432. up_read(&sb->s_umount);
  433. spin_lock(&sb_lock);
  434. if (p)
  435. __put_super(p);
  436. p = sb;
  437. }
  438. if (p)
  439. __put_super(p);
  440. spin_unlock(&sb_lock);
  441. }
  442. EXPORT_SYMBOL(iterate_supers_type);
  443. /**
  444. * get_super - get the superblock of a device
  445. * @bdev: device to get the superblock for
  446. *
  447. * Scans the superblock list and finds the superblock of the file system
  448. * mounted on the device given. %NULL is returned if no match is found.
  449. */
  450. struct super_block *get_super(struct block_device *bdev)
  451. {
  452. struct super_block *sb;
  453. if (!bdev)
  454. return NULL;
  455. spin_lock(&sb_lock);
  456. rescan:
  457. list_for_each_entry(sb, &super_blocks, s_list) {
  458. if (list_empty(&sb->s_instances))
  459. continue;
  460. if (sb->s_bdev == bdev) {
  461. sb->s_count++;
  462. spin_unlock(&sb_lock);
  463. down_read(&sb->s_umount);
  464. /* still alive? */
  465. if (sb->s_root)
  466. return sb;
  467. up_read(&sb->s_umount);
  468. /* nope, got unmounted */
  469. spin_lock(&sb_lock);
  470. __put_super(sb);
  471. goto rescan;
  472. }
  473. }
  474. spin_unlock(&sb_lock);
  475. return NULL;
  476. }
  477. EXPORT_SYMBOL(get_super);
  478. /**
  479. * get_active_super - get an active reference to the superblock of a device
  480. * @bdev: device to get the superblock for
  481. *
  482. * Scans the superblock list and finds the superblock of the file system
  483. * mounted on the device given. Returns the superblock with an active
  484. * reference or %NULL if none was found.
  485. */
  486. struct super_block *get_active_super(struct block_device *bdev)
  487. {
  488. struct super_block *sb;
  489. if (!bdev)
  490. return NULL;
  491. restart:
  492. spin_lock(&sb_lock);
  493. list_for_each_entry(sb, &super_blocks, s_list) {
  494. if (list_empty(&sb->s_instances))
  495. continue;
  496. if (sb->s_bdev == bdev) {
  497. if (grab_super(sb)) /* drops sb_lock */
  498. return sb;
  499. else
  500. goto restart;
  501. }
  502. }
  503. spin_unlock(&sb_lock);
  504. return NULL;
  505. }
  506. struct super_block *user_get_super(dev_t dev)
  507. {
  508. struct super_block *sb;
  509. spin_lock(&sb_lock);
  510. rescan:
  511. list_for_each_entry(sb, &super_blocks, s_list) {
  512. if (list_empty(&sb->s_instances))
  513. continue;
  514. if (sb->s_dev == dev) {
  515. sb->s_count++;
  516. spin_unlock(&sb_lock);
  517. down_read(&sb->s_umount);
  518. /* still alive? */
  519. if (sb->s_root)
  520. return sb;
  521. up_read(&sb->s_umount);
  522. /* nope, got unmounted */
  523. spin_lock(&sb_lock);
  524. __put_super(sb);
  525. goto rescan;
  526. }
  527. }
  528. spin_unlock(&sb_lock);
  529. return NULL;
  530. }
  531. /**
  532. * do_remount_sb - asks filesystem to change mount options.
  533. * @sb: superblock in question
  534. * @flags: numeric part of options
  535. * @data: the rest of options
  536. * @force: whether or not to force the change
  537. *
  538. * Alters the mount options of a mounted file system.
  539. */
  540. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  541. {
  542. int retval;
  543. int remount_ro;
  544. if (sb->s_frozen != SB_UNFROZEN)
  545. return -EBUSY;
  546. #ifdef CONFIG_BLOCK
  547. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  548. return -EACCES;
  549. #endif
  550. if (flags & MS_RDONLY)
  551. acct_auto_close(sb);
  552. shrink_dcache_sb(sb);
  553. sync_filesystem(sb);
  554. remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
  555. /* If we are remounting RDONLY and current sb is read/write,
  556. make sure there are no rw files opened */
  557. if (remount_ro) {
  558. if (force)
  559. mark_files_ro(sb);
  560. else if (!fs_may_remount_ro(sb))
  561. return -EBUSY;
  562. }
  563. if (sb->s_op->remount_fs) {
  564. retval = sb->s_op->remount_fs(sb, &flags, data);
  565. if (retval)
  566. return retval;
  567. }
  568. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  569. /*
  570. * Some filesystems modify their metadata via some other path than the
  571. * bdev buffer cache (eg. use a private mapping, or directories in
  572. * pagecache, etc). Also file data modifications go via their own
  573. * mappings. So If we try to mount readonly then copy the filesystem
  574. * from bdev, we could get stale data, so invalidate it to give a best
  575. * effort at coherency.
  576. */
  577. if (remount_ro && sb->s_bdev)
  578. invalidate_bdev(sb->s_bdev);
  579. return 0;
  580. }
  581. static void do_emergency_remount(struct work_struct *work)
  582. {
  583. struct super_block *sb, *p = NULL;
  584. spin_lock(&sb_lock);
  585. list_for_each_entry(sb, &super_blocks, s_list) {
  586. if (list_empty(&sb->s_instances))
  587. continue;
  588. sb->s_count++;
  589. spin_unlock(&sb_lock);
  590. down_write(&sb->s_umount);
  591. if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) {
  592. /*
  593. * What lock protects sb->s_flags??
  594. */
  595. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  596. }
  597. up_write(&sb->s_umount);
  598. spin_lock(&sb_lock);
  599. if (p)
  600. __put_super(p);
  601. p = sb;
  602. }
  603. if (p)
  604. __put_super(p);
  605. spin_unlock(&sb_lock);
  606. kfree(work);
  607. printk("Emergency Remount complete\n");
  608. }
  609. void emergency_remount(void)
  610. {
  611. struct work_struct *work;
  612. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  613. if (work) {
  614. INIT_WORK(work, do_emergency_remount);
  615. schedule_work(work);
  616. }
  617. }
  618. /*
  619. * Unnamed block devices are dummy devices used by virtual
  620. * filesystems which don't use real block-devices. -- jrs
  621. */
  622. static DEFINE_IDA(unnamed_dev_ida);
  623. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  624. static int unnamed_dev_start = 0; /* don't bother trying below it */
  625. int set_anon_super(struct super_block *s, void *data)
  626. {
  627. int dev;
  628. int error;
  629. retry:
  630. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  631. return -ENOMEM;
  632. spin_lock(&unnamed_dev_lock);
  633. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  634. if (!error)
  635. unnamed_dev_start = dev + 1;
  636. spin_unlock(&unnamed_dev_lock);
  637. if (error == -EAGAIN)
  638. /* We raced and lost with another CPU. */
  639. goto retry;
  640. else if (error)
  641. return -EAGAIN;
  642. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  643. spin_lock(&unnamed_dev_lock);
  644. ida_remove(&unnamed_dev_ida, dev);
  645. if (unnamed_dev_start > dev)
  646. unnamed_dev_start = dev;
  647. spin_unlock(&unnamed_dev_lock);
  648. return -EMFILE;
  649. }
  650. s->s_dev = MKDEV(0, dev & MINORMASK);
  651. s->s_bdi = &noop_backing_dev_info;
  652. return 0;
  653. }
  654. EXPORT_SYMBOL(set_anon_super);
  655. void kill_anon_super(struct super_block *sb)
  656. {
  657. int slot = MINOR(sb->s_dev);
  658. generic_shutdown_super(sb);
  659. spin_lock(&unnamed_dev_lock);
  660. ida_remove(&unnamed_dev_ida, slot);
  661. if (slot < unnamed_dev_start)
  662. unnamed_dev_start = slot;
  663. spin_unlock(&unnamed_dev_lock);
  664. }
  665. EXPORT_SYMBOL(kill_anon_super);
  666. void kill_litter_super(struct super_block *sb)
  667. {
  668. if (sb->s_root)
  669. d_genocide(sb->s_root);
  670. kill_anon_super(sb);
  671. }
  672. EXPORT_SYMBOL(kill_litter_super);
  673. static int ns_test_super(struct super_block *sb, void *data)
  674. {
  675. return sb->s_fs_info == data;
  676. }
  677. static int ns_set_super(struct super_block *sb, void *data)
  678. {
  679. sb->s_fs_info = data;
  680. return set_anon_super(sb, NULL);
  681. }
  682. struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
  683. void *data, int (*fill_super)(struct super_block *, void *, int))
  684. {
  685. struct super_block *sb;
  686. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  687. if (IS_ERR(sb))
  688. return ERR_CAST(sb);
  689. if (!sb->s_root) {
  690. int err;
  691. sb->s_flags = flags;
  692. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  693. if (err) {
  694. deactivate_locked_super(sb);
  695. return ERR_PTR(err);
  696. }
  697. sb->s_flags |= MS_ACTIVE;
  698. }
  699. return dget(sb->s_root);
  700. }
  701. EXPORT_SYMBOL(mount_ns);
  702. #ifdef CONFIG_BLOCK
  703. static int set_bdev_super(struct super_block *s, void *data)
  704. {
  705. s->s_bdev = data;
  706. s->s_dev = s->s_bdev->bd_dev;
  707. /*
  708. * We set the bdi here to the queue backing, file systems can
  709. * overwrite this in ->fill_super()
  710. */
  711. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  712. return 0;
  713. }
  714. static int test_bdev_super(struct super_block *s, void *data)
  715. {
  716. return (void *)s->s_bdev == data;
  717. }
  718. struct dentry *mount_bdev(struct file_system_type *fs_type,
  719. int flags, const char *dev_name, void *data,
  720. int (*fill_super)(struct super_block *, void *, int))
  721. {
  722. struct block_device *bdev;
  723. struct super_block *s;
  724. fmode_t mode = FMODE_READ | FMODE_EXCL;
  725. int error = 0;
  726. if (!(flags & MS_RDONLY))
  727. mode |= FMODE_WRITE;
  728. bdev = blkdev_get_by_path(dev_name, mode, fs_type);
  729. if (IS_ERR(bdev))
  730. return ERR_CAST(bdev);
  731. /*
  732. * once the super is inserted into the list by sget, s_umount
  733. * will protect the lockfs code from trying to start a snapshot
  734. * while we are mounting
  735. */
  736. mutex_lock(&bdev->bd_fsfreeze_mutex);
  737. if (bdev->bd_fsfreeze_count > 0) {
  738. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  739. error = -EBUSY;
  740. goto error_bdev;
  741. }
  742. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  743. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  744. if (IS_ERR(s))
  745. goto error_s;
  746. if (s->s_root) {
  747. if ((flags ^ s->s_flags) & MS_RDONLY) {
  748. deactivate_locked_super(s);
  749. error = -EBUSY;
  750. goto error_bdev;
  751. }
  752. /*
  753. * s_umount nests inside bd_mutex during
  754. * __invalidate_device(). blkdev_put() acquires
  755. * bd_mutex and can't be called under s_umount. Drop
  756. * s_umount temporarily. This is safe as we're
  757. * holding an active reference.
  758. */
  759. up_write(&s->s_umount);
  760. blkdev_put(bdev, mode);
  761. down_write(&s->s_umount);
  762. } else {
  763. char b[BDEVNAME_SIZE];
  764. s->s_flags = flags | MS_NOSEC;
  765. s->s_mode = mode;
  766. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  767. sb_set_blocksize(s, block_size(bdev));
  768. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  769. if (error) {
  770. deactivate_locked_super(s);
  771. goto error;
  772. }
  773. s->s_flags |= MS_ACTIVE;
  774. bdev->bd_super = s;
  775. }
  776. return dget(s->s_root);
  777. error_s:
  778. error = PTR_ERR(s);
  779. error_bdev:
  780. blkdev_put(bdev, mode);
  781. error:
  782. return ERR_PTR(error);
  783. }
  784. EXPORT_SYMBOL(mount_bdev);
  785. void kill_block_super(struct super_block *sb)
  786. {
  787. struct block_device *bdev = sb->s_bdev;
  788. fmode_t mode = sb->s_mode;
  789. bdev->bd_super = NULL;
  790. generic_shutdown_super(sb);
  791. sync_blockdev(bdev);
  792. WARN_ON_ONCE(!(mode & FMODE_EXCL));
  793. blkdev_put(bdev, mode | FMODE_EXCL);
  794. }
  795. EXPORT_SYMBOL(kill_block_super);
  796. #endif
  797. struct dentry *mount_nodev(struct file_system_type *fs_type,
  798. int flags, void *data,
  799. int (*fill_super)(struct super_block *, void *, int))
  800. {
  801. int error;
  802. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  803. if (IS_ERR(s))
  804. return ERR_CAST(s);
  805. s->s_flags = flags;
  806. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  807. if (error) {
  808. deactivate_locked_super(s);
  809. return ERR_PTR(error);
  810. }
  811. s->s_flags |= MS_ACTIVE;
  812. return dget(s->s_root);
  813. }
  814. EXPORT_SYMBOL(mount_nodev);
  815. static int compare_single(struct super_block *s, void *p)
  816. {
  817. return 1;
  818. }
  819. struct dentry *mount_single(struct file_system_type *fs_type,
  820. int flags, void *data,
  821. int (*fill_super)(struct super_block *, void *, int))
  822. {
  823. struct super_block *s;
  824. int error;
  825. s = sget(fs_type, compare_single, set_anon_super, NULL);
  826. if (IS_ERR(s))
  827. return ERR_CAST(s);
  828. if (!s->s_root) {
  829. s->s_flags = flags;
  830. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  831. if (error) {
  832. deactivate_locked_super(s);
  833. return ERR_PTR(error);
  834. }
  835. s->s_flags |= MS_ACTIVE;
  836. } else {
  837. do_remount_sb(s, flags, data, 0);
  838. }
  839. return dget(s->s_root);
  840. }
  841. EXPORT_SYMBOL(mount_single);
  842. struct dentry *
  843. mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
  844. {
  845. struct dentry *root;
  846. struct super_block *sb;
  847. char *secdata = NULL;
  848. int error = -ENOMEM;
  849. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  850. secdata = alloc_secdata();
  851. if (!secdata)
  852. goto out;
  853. error = security_sb_copy_data(data, secdata);
  854. if (error)
  855. goto out_free_secdata;
  856. }
  857. root = type->mount(type, flags, name, data);
  858. if (IS_ERR(root)) {
  859. error = PTR_ERR(root);
  860. goto out_free_secdata;
  861. }
  862. sb = root->d_sb;
  863. BUG_ON(!sb);
  864. WARN_ON(!sb->s_bdi);
  865. WARN_ON(sb->s_bdi == &default_backing_dev_info);
  866. sb->s_flags |= MS_BORN;
  867. error = security_sb_kern_mount(sb, flags, secdata);
  868. if (error)
  869. goto out_sb;
  870. /*
  871. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  872. * but s_maxbytes was an unsigned long long for many releases. Throw
  873. * this warning for a little while to try and catch filesystems that
  874. * violate this rule.
  875. */
  876. WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  877. "negative value (%lld)\n", type->name, sb->s_maxbytes);
  878. up_write(&sb->s_umount);
  879. free_secdata(secdata);
  880. return root;
  881. out_sb:
  882. dput(root);
  883. deactivate_locked_super(sb);
  884. out_free_secdata:
  885. free_secdata(secdata);
  886. out:
  887. return ERR_PTR(error);
  888. }
  889. /**
  890. * freeze_super - lock the filesystem and force it into a consistent state
  891. * @sb: the super to lock
  892. *
  893. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  894. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  895. * -EBUSY.
  896. */
  897. int freeze_super(struct super_block *sb)
  898. {
  899. int ret;
  900. atomic_inc(&sb->s_active);
  901. down_write(&sb->s_umount);
  902. if (sb->s_frozen) {
  903. deactivate_locked_super(sb);
  904. return -EBUSY;
  905. }
  906. if (sb->s_flags & MS_RDONLY) {
  907. sb->s_frozen = SB_FREEZE_TRANS;
  908. smp_wmb();
  909. up_write(&sb->s_umount);
  910. return 0;
  911. }
  912. sb->s_frozen = SB_FREEZE_WRITE;
  913. smp_wmb();
  914. sync_filesystem(sb);
  915. sb->s_frozen = SB_FREEZE_TRANS;
  916. smp_wmb();
  917. sync_blockdev(sb->s_bdev);
  918. if (sb->s_op->freeze_fs) {
  919. ret = sb->s_op->freeze_fs(sb);
  920. if (ret) {
  921. printk(KERN_ERR
  922. "VFS:Filesystem freeze failed\n");
  923. sb->s_frozen = SB_UNFROZEN;
  924. deactivate_locked_super(sb);
  925. return ret;
  926. }
  927. }
  928. up_write(&sb->s_umount);
  929. return 0;
  930. }
  931. EXPORT_SYMBOL(freeze_super);
  932. /**
  933. * thaw_super -- unlock filesystem
  934. * @sb: the super to thaw
  935. *
  936. * Unlocks the filesystem and marks it writeable again after freeze_super().
  937. */
  938. int thaw_super(struct super_block *sb)
  939. {
  940. int error;
  941. down_write(&sb->s_umount);
  942. if (sb->s_frozen == SB_UNFROZEN) {
  943. up_write(&sb->s_umount);
  944. return -EINVAL;
  945. }
  946. if (sb->s_flags & MS_RDONLY)
  947. goto out;
  948. if (sb->s_op->unfreeze_fs) {
  949. error = sb->s_op->unfreeze_fs(sb);
  950. if (error) {
  951. printk(KERN_ERR
  952. "VFS:Filesystem thaw failed\n");
  953. sb->s_frozen = SB_FREEZE_TRANS;
  954. up_write(&sb->s_umount);
  955. return error;
  956. }
  957. }
  958. out:
  959. sb->s_frozen = SB_UNFROZEN;
  960. smp_wmb();
  961. wake_up(&sb->s_wait_unfrozen);
  962. deactivate_locked_super(sb);
  963. return 0;
  964. }
  965. EXPORT_SYMBOL(thaw_super);