super.c 28 KB

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