super.c 25 KB

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