super.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 !sc->nr_to_scan ? 0 : -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_HLIST_NODE(&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. static 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. static 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 (hlist_unhashed(&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 && (sb->s_flags & MS_BORN))
  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. mutex_lock(&sb->s_lock);
  321. }
  322. void unlock_super(struct super_block * sb)
  323. {
  324. mutex_unlock(&sb->s_lock);
  325. }
  326. EXPORT_SYMBOL(lock_super);
  327. EXPORT_SYMBOL(unlock_super);
  328. /**
  329. * generic_shutdown_super - common helper for ->kill_sb()
  330. * @sb: superblock to kill
  331. *
  332. * generic_shutdown_super() does all fs-independent work on superblock
  333. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  334. * that need destruction out of superblock, call generic_shutdown_super()
  335. * and release aforementioned objects. Note: dentries and inodes _are_
  336. * taken care of and do not need specific handling.
  337. *
  338. * Upon calling this function, the filesystem may no longer alter or
  339. * rearrange the set of dentries belonging to this super_block, nor may it
  340. * change the attachments of dentries to inodes.
  341. */
  342. void generic_shutdown_super(struct super_block *sb)
  343. {
  344. const struct super_operations *sop = sb->s_op;
  345. if (sb->s_root) {
  346. shrink_dcache_for_umount(sb);
  347. sync_filesystem(sb);
  348. sb->s_flags &= ~MS_ACTIVE;
  349. fsnotify_unmount_inodes(&sb->s_inodes);
  350. evict_inodes(sb);
  351. if (sop->put_super)
  352. sop->put_super(sb);
  353. if (!list_empty(&sb->s_inodes)) {
  354. printk("VFS: Busy inodes after unmount of %s. "
  355. "Self-destruct in 5 seconds. Have a nice day...\n",
  356. sb->s_id);
  357. }
  358. }
  359. spin_lock(&sb_lock);
  360. /* should be initialized for __put_super_and_need_restart() */
  361. hlist_del_init(&sb->s_instances);
  362. spin_unlock(&sb_lock);
  363. up_write(&sb->s_umount);
  364. }
  365. EXPORT_SYMBOL(generic_shutdown_super);
  366. /**
  367. * sget - find or create a superblock
  368. * @type: filesystem type superblock should belong to
  369. * @test: comparison callback
  370. * @set: setup callback
  371. * @data: argument to each of them
  372. */
  373. struct super_block *sget(struct file_system_type *type,
  374. int (*test)(struct super_block *,void *),
  375. int (*set)(struct super_block *,void *),
  376. void *data)
  377. {
  378. struct super_block *s = NULL;
  379. struct hlist_node *node;
  380. struct super_block *old;
  381. int err;
  382. retry:
  383. spin_lock(&sb_lock);
  384. if (test) {
  385. hlist_for_each_entry(old, node, &type->fs_supers, s_instances) {
  386. if (!test(old, data))
  387. continue;
  388. if (!grab_super(old))
  389. goto retry;
  390. if (s) {
  391. up_write(&s->s_umount);
  392. destroy_super(s);
  393. s = NULL;
  394. }
  395. down_write(&old->s_umount);
  396. if (unlikely(!(old->s_flags & MS_BORN))) {
  397. deactivate_locked_super(old);
  398. goto retry;
  399. }
  400. return old;
  401. }
  402. }
  403. if (!s) {
  404. spin_unlock(&sb_lock);
  405. s = alloc_super(type);
  406. if (!s)
  407. return ERR_PTR(-ENOMEM);
  408. goto retry;
  409. }
  410. err = set(s, data);
  411. if (err) {
  412. spin_unlock(&sb_lock);
  413. up_write(&s->s_umount);
  414. destroy_super(s);
  415. return ERR_PTR(err);
  416. }
  417. s->s_type = type;
  418. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  419. list_add_tail(&s->s_list, &super_blocks);
  420. hlist_add_head(&s->s_instances, &type->fs_supers);
  421. spin_unlock(&sb_lock);
  422. get_filesystem(type);
  423. register_shrinker(&s->s_shrink);
  424. return s;
  425. }
  426. EXPORT_SYMBOL(sget);
  427. void drop_super(struct super_block *sb)
  428. {
  429. up_read(&sb->s_umount);
  430. put_super(sb);
  431. }
  432. EXPORT_SYMBOL(drop_super);
  433. /**
  434. * sync_supers - helper for periodic superblock writeback
  435. *
  436. * Call the write_super method if present on all dirty superblocks in
  437. * the system. This is for the periodic writeback used by most older
  438. * filesystems. For data integrity superblock writeback use
  439. * sync_filesystems() instead.
  440. *
  441. * Note: check the dirty flag before waiting, so we don't
  442. * hold up the sync while mounting a device. (The newly
  443. * mounted device won't need syncing.)
  444. */
  445. void sync_supers(void)
  446. {
  447. struct super_block *sb, *p = NULL;
  448. spin_lock(&sb_lock);
  449. list_for_each_entry(sb, &super_blocks, s_list) {
  450. if (hlist_unhashed(&sb->s_instances))
  451. continue;
  452. if (sb->s_op->write_super && sb->s_dirt) {
  453. sb->s_count++;
  454. spin_unlock(&sb_lock);
  455. down_read(&sb->s_umount);
  456. if (sb->s_root && sb->s_dirt && (sb->s_flags & MS_BORN))
  457. sb->s_op->write_super(sb);
  458. up_read(&sb->s_umount);
  459. spin_lock(&sb_lock);
  460. if (p)
  461. __put_super(p);
  462. p = sb;
  463. }
  464. }
  465. if (p)
  466. __put_super(p);
  467. spin_unlock(&sb_lock);
  468. }
  469. /**
  470. * iterate_supers - call function for all active superblocks
  471. * @f: function to call
  472. * @arg: argument to pass to it
  473. *
  474. * Scans the superblock list and calls given function, passing it
  475. * locked superblock and given argument.
  476. */
  477. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  478. {
  479. struct super_block *sb, *p = NULL;
  480. spin_lock(&sb_lock);
  481. list_for_each_entry(sb, &super_blocks, s_list) {
  482. if (hlist_unhashed(&sb->s_instances))
  483. continue;
  484. sb->s_count++;
  485. spin_unlock(&sb_lock);
  486. down_read(&sb->s_umount);
  487. if (sb->s_root && (sb->s_flags & MS_BORN))
  488. f(sb, arg);
  489. up_read(&sb->s_umount);
  490. spin_lock(&sb_lock);
  491. if (p)
  492. __put_super(p);
  493. p = sb;
  494. }
  495. if (p)
  496. __put_super(p);
  497. spin_unlock(&sb_lock);
  498. }
  499. /**
  500. * iterate_supers_type - call function for superblocks of given type
  501. * @type: fs type
  502. * @f: function to call
  503. * @arg: argument to pass to it
  504. *
  505. * Scans the superblock list and calls given function, passing it
  506. * locked superblock and given argument.
  507. */
  508. void iterate_supers_type(struct file_system_type *type,
  509. void (*f)(struct super_block *, void *), void *arg)
  510. {
  511. struct super_block *sb, *p = NULL;
  512. struct hlist_node *node;
  513. spin_lock(&sb_lock);
  514. hlist_for_each_entry(sb, node, &type->fs_supers, s_instances) {
  515. sb->s_count++;
  516. spin_unlock(&sb_lock);
  517. down_read(&sb->s_umount);
  518. if (sb->s_root && (sb->s_flags & MS_BORN))
  519. f(sb, arg);
  520. up_read(&sb->s_umount);
  521. spin_lock(&sb_lock);
  522. if (p)
  523. __put_super(p);
  524. p = sb;
  525. }
  526. if (p)
  527. __put_super(p);
  528. spin_unlock(&sb_lock);
  529. }
  530. EXPORT_SYMBOL(iterate_supers_type);
  531. /**
  532. * get_super - get the superblock of a device
  533. * @bdev: device to get the superblock for
  534. *
  535. * Scans the superblock list and finds the superblock of the file system
  536. * mounted on the device given. %NULL is returned if no match is found.
  537. */
  538. struct super_block *get_super(struct block_device *bdev)
  539. {
  540. struct super_block *sb;
  541. if (!bdev)
  542. return NULL;
  543. spin_lock(&sb_lock);
  544. rescan:
  545. list_for_each_entry(sb, &super_blocks, s_list) {
  546. if (hlist_unhashed(&sb->s_instances))
  547. continue;
  548. if (sb->s_bdev == bdev) {
  549. sb->s_count++;
  550. spin_unlock(&sb_lock);
  551. down_read(&sb->s_umount);
  552. /* still alive? */
  553. if (sb->s_root && (sb->s_flags & MS_BORN))
  554. return sb;
  555. up_read(&sb->s_umount);
  556. /* nope, got unmounted */
  557. spin_lock(&sb_lock);
  558. __put_super(sb);
  559. goto rescan;
  560. }
  561. }
  562. spin_unlock(&sb_lock);
  563. return NULL;
  564. }
  565. EXPORT_SYMBOL(get_super);
  566. /**
  567. * get_active_super - get an active reference to the superblock of a device
  568. * @bdev: device to get the superblock for
  569. *
  570. * Scans the superblock list and finds the superblock of the file system
  571. * mounted on the device given. Returns the superblock with an active
  572. * reference or %NULL if none was found.
  573. */
  574. struct super_block *get_active_super(struct block_device *bdev)
  575. {
  576. struct super_block *sb;
  577. if (!bdev)
  578. return NULL;
  579. restart:
  580. spin_lock(&sb_lock);
  581. list_for_each_entry(sb, &super_blocks, s_list) {
  582. if (hlist_unhashed(&sb->s_instances))
  583. continue;
  584. if (sb->s_bdev == bdev) {
  585. if (grab_super(sb)) /* drops sb_lock */
  586. return sb;
  587. else
  588. goto restart;
  589. }
  590. }
  591. spin_unlock(&sb_lock);
  592. return NULL;
  593. }
  594. struct super_block *user_get_super(dev_t dev)
  595. {
  596. struct super_block *sb;
  597. spin_lock(&sb_lock);
  598. rescan:
  599. list_for_each_entry(sb, &super_blocks, s_list) {
  600. if (hlist_unhashed(&sb->s_instances))
  601. continue;
  602. if (sb->s_dev == dev) {
  603. sb->s_count++;
  604. spin_unlock(&sb_lock);
  605. down_read(&sb->s_umount);
  606. /* still alive? */
  607. if (sb->s_root && (sb->s_flags & MS_BORN))
  608. return sb;
  609. up_read(&sb->s_umount);
  610. /* nope, got unmounted */
  611. spin_lock(&sb_lock);
  612. __put_super(sb);
  613. goto rescan;
  614. }
  615. }
  616. spin_unlock(&sb_lock);
  617. return NULL;
  618. }
  619. /**
  620. * do_remount_sb - asks filesystem to change mount options.
  621. * @sb: superblock in question
  622. * @flags: numeric part of options
  623. * @data: the rest of options
  624. * @force: whether or not to force the change
  625. *
  626. * Alters the mount options of a mounted file system.
  627. */
  628. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  629. {
  630. int retval;
  631. int remount_ro;
  632. if (sb->s_frozen != SB_UNFROZEN)
  633. return -EBUSY;
  634. #ifdef CONFIG_BLOCK
  635. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  636. return -EACCES;
  637. #endif
  638. if (flags & MS_RDONLY)
  639. acct_auto_close(sb);
  640. shrink_dcache_sb(sb);
  641. sync_filesystem(sb);
  642. remount_ro = (flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY);
  643. /* If we are remounting RDONLY and current sb is read/write,
  644. make sure there are no rw files opened */
  645. if (remount_ro) {
  646. if (force)
  647. mark_files_ro(sb);
  648. else if (!fs_may_remount_ro(sb))
  649. return -EBUSY;
  650. }
  651. if (sb->s_op->remount_fs) {
  652. retval = sb->s_op->remount_fs(sb, &flags, data);
  653. if (retval) {
  654. if (!force)
  655. return retval;
  656. /* If forced remount, go ahead despite any errors */
  657. WARN(1, "forced remount of a %s fs returned %i\n",
  658. sb->s_type->name, retval);
  659. }
  660. }
  661. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  662. /*
  663. * Some filesystems modify their metadata via some other path than the
  664. * bdev buffer cache (eg. use a private mapping, or directories in
  665. * pagecache, etc). Also file data modifications go via their own
  666. * mappings. So If we try to mount readonly then copy the filesystem
  667. * from bdev, we could get stale data, so invalidate it to give a best
  668. * effort at coherency.
  669. */
  670. if (remount_ro && sb->s_bdev)
  671. invalidate_bdev(sb->s_bdev);
  672. return 0;
  673. }
  674. static void do_emergency_remount(struct work_struct *work)
  675. {
  676. struct super_block *sb, *p = NULL;
  677. spin_lock(&sb_lock);
  678. list_for_each_entry(sb, &super_blocks, s_list) {
  679. if (hlist_unhashed(&sb->s_instances))
  680. continue;
  681. sb->s_count++;
  682. spin_unlock(&sb_lock);
  683. down_write(&sb->s_umount);
  684. if (sb->s_root && sb->s_bdev && (sb->s_flags & MS_BORN) &&
  685. !(sb->s_flags & MS_RDONLY)) {
  686. /*
  687. * What lock protects sb->s_flags??
  688. */
  689. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  690. }
  691. up_write(&sb->s_umount);
  692. spin_lock(&sb_lock);
  693. if (p)
  694. __put_super(p);
  695. p = sb;
  696. }
  697. if (p)
  698. __put_super(p);
  699. spin_unlock(&sb_lock);
  700. kfree(work);
  701. printk("Emergency Remount complete\n");
  702. }
  703. void emergency_remount(void)
  704. {
  705. struct work_struct *work;
  706. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  707. if (work) {
  708. INIT_WORK(work, do_emergency_remount);
  709. schedule_work(work);
  710. }
  711. }
  712. /*
  713. * Unnamed block devices are dummy devices used by virtual
  714. * filesystems which don't use real block-devices. -- jrs
  715. */
  716. static DEFINE_IDA(unnamed_dev_ida);
  717. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  718. static int unnamed_dev_start = 0; /* don't bother trying below it */
  719. int get_anon_bdev(dev_t *p)
  720. {
  721. int dev;
  722. int error;
  723. retry:
  724. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  725. return -ENOMEM;
  726. spin_lock(&unnamed_dev_lock);
  727. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  728. if (!error)
  729. unnamed_dev_start = dev + 1;
  730. spin_unlock(&unnamed_dev_lock);
  731. if (error == -EAGAIN)
  732. /* We raced and lost with another CPU. */
  733. goto retry;
  734. else if (error)
  735. return -EAGAIN;
  736. if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) {
  737. spin_lock(&unnamed_dev_lock);
  738. ida_remove(&unnamed_dev_ida, dev);
  739. if (unnamed_dev_start > dev)
  740. unnamed_dev_start = dev;
  741. spin_unlock(&unnamed_dev_lock);
  742. return -EMFILE;
  743. }
  744. *p = MKDEV(0, dev & MINORMASK);
  745. return 0;
  746. }
  747. EXPORT_SYMBOL(get_anon_bdev);
  748. void free_anon_bdev(dev_t dev)
  749. {
  750. int slot = MINOR(dev);
  751. spin_lock(&unnamed_dev_lock);
  752. ida_remove(&unnamed_dev_ida, slot);
  753. if (slot < unnamed_dev_start)
  754. unnamed_dev_start = slot;
  755. spin_unlock(&unnamed_dev_lock);
  756. }
  757. EXPORT_SYMBOL(free_anon_bdev);
  758. int set_anon_super(struct super_block *s, void *data)
  759. {
  760. int error = get_anon_bdev(&s->s_dev);
  761. if (!error)
  762. s->s_bdi = &noop_backing_dev_info;
  763. return error;
  764. }
  765. EXPORT_SYMBOL(set_anon_super);
  766. void kill_anon_super(struct super_block *sb)
  767. {
  768. dev_t dev = sb->s_dev;
  769. generic_shutdown_super(sb);
  770. free_anon_bdev(dev);
  771. }
  772. EXPORT_SYMBOL(kill_anon_super);
  773. void kill_litter_super(struct super_block *sb)
  774. {
  775. if (sb->s_root)
  776. d_genocide(sb->s_root);
  777. kill_anon_super(sb);
  778. }
  779. EXPORT_SYMBOL(kill_litter_super);
  780. static int ns_test_super(struct super_block *sb, void *data)
  781. {
  782. return sb->s_fs_info == data;
  783. }
  784. static int ns_set_super(struct super_block *sb, void *data)
  785. {
  786. sb->s_fs_info = data;
  787. return set_anon_super(sb, NULL);
  788. }
  789. struct dentry *mount_ns(struct file_system_type *fs_type, int flags,
  790. void *data, int (*fill_super)(struct super_block *, void *, int))
  791. {
  792. struct super_block *sb;
  793. sb = sget(fs_type, ns_test_super, ns_set_super, data);
  794. if (IS_ERR(sb))
  795. return ERR_CAST(sb);
  796. if (!sb->s_root) {
  797. int err;
  798. sb->s_flags = flags;
  799. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  800. if (err) {
  801. deactivate_locked_super(sb);
  802. return ERR_PTR(err);
  803. }
  804. sb->s_flags |= MS_ACTIVE;
  805. }
  806. return dget(sb->s_root);
  807. }
  808. EXPORT_SYMBOL(mount_ns);
  809. #ifdef CONFIG_BLOCK
  810. static int set_bdev_super(struct super_block *s, void *data)
  811. {
  812. s->s_bdev = data;
  813. s->s_dev = s->s_bdev->bd_dev;
  814. /*
  815. * We set the bdi here to the queue backing, file systems can
  816. * overwrite this in ->fill_super()
  817. */
  818. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  819. return 0;
  820. }
  821. static int test_bdev_super(struct super_block *s, void *data)
  822. {
  823. return (void *)s->s_bdev == data;
  824. }
  825. struct dentry *mount_bdev(struct file_system_type *fs_type,
  826. int flags, const char *dev_name, void *data,
  827. int (*fill_super)(struct super_block *, void *, int))
  828. {
  829. struct block_device *bdev;
  830. struct super_block *s;
  831. fmode_t mode = FMODE_READ | FMODE_EXCL;
  832. int error = 0;
  833. if (!(flags & MS_RDONLY))
  834. mode |= FMODE_WRITE;
  835. bdev = blkdev_get_by_path(dev_name, mode, fs_type);
  836. if (IS_ERR(bdev))
  837. return ERR_CAST(bdev);
  838. /*
  839. * once the super is inserted into the list by sget, s_umount
  840. * will protect the lockfs code from trying to start a snapshot
  841. * while we are mounting
  842. */
  843. mutex_lock(&bdev->bd_fsfreeze_mutex);
  844. if (bdev->bd_fsfreeze_count > 0) {
  845. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  846. error = -EBUSY;
  847. goto error_bdev;
  848. }
  849. s = sget(fs_type, test_bdev_super, set_bdev_super, bdev);
  850. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  851. if (IS_ERR(s))
  852. goto error_s;
  853. if (s->s_root) {
  854. if ((flags ^ s->s_flags) & MS_RDONLY) {
  855. deactivate_locked_super(s);
  856. error = -EBUSY;
  857. goto error_bdev;
  858. }
  859. /*
  860. * s_umount nests inside bd_mutex during
  861. * __invalidate_device(). blkdev_put() acquires
  862. * bd_mutex and can't be called under s_umount. Drop
  863. * s_umount temporarily. This is safe as we're
  864. * holding an active reference.
  865. */
  866. up_write(&s->s_umount);
  867. blkdev_put(bdev, mode);
  868. down_write(&s->s_umount);
  869. } else {
  870. char b[BDEVNAME_SIZE];
  871. s->s_flags = flags | MS_NOSEC;
  872. s->s_mode = mode;
  873. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  874. sb_set_blocksize(s, block_size(bdev));
  875. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  876. if (error) {
  877. deactivate_locked_super(s);
  878. goto error;
  879. }
  880. s->s_flags |= MS_ACTIVE;
  881. bdev->bd_super = s;
  882. }
  883. return dget(s->s_root);
  884. error_s:
  885. error = PTR_ERR(s);
  886. error_bdev:
  887. blkdev_put(bdev, mode);
  888. error:
  889. return ERR_PTR(error);
  890. }
  891. EXPORT_SYMBOL(mount_bdev);
  892. void kill_block_super(struct super_block *sb)
  893. {
  894. struct block_device *bdev = sb->s_bdev;
  895. fmode_t mode = sb->s_mode;
  896. bdev->bd_super = NULL;
  897. generic_shutdown_super(sb);
  898. sync_blockdev(bdev);
  899. WARN_ON_ONCE(!(mode & FMODE_EXCL));
  900. blkdev_put(bdev, mode | FMODE_EXCL);
  901. }
  902. EXPORT_SYMBOL(kill_block_super);
  903. #endif
  904. struct dentry *mount_nodev(struct file_system_type *fs_type,
  905. int flags, void *data,
  906. int (*fill_super)(struct super_block *, void *, int))
  907. {
  908. int error;
  909. struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
  910. if (IS_ERR(s))
  911. return ERR_CAST(s);
  912. s->s_flags = flags;
  913. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  914. if (error) {
  915. deactivate_locked_super(s);
  916. return ERR_PTR(error);
  917. }
  918. s->s_flags |= MS_ACTIVE;
  919. return dget(s->s_root);
  920. }
  921. EXPORT_SYMBOL(mount_nodev);
  922. static int compare_single(struct super_block *s, void *p)
  923. {
  924. return 1;
  925. }
  926. struct dentry *mount_single(struct file_system_type *fs_type,
  927. int flags, void *data,
  928. int (*fill_super)(struct super_block *, void *, int))
  929. {
  930. struct super_block *s;
  931. int error;
  932. s = sget(fs_type, compare_single, set_anon_super, NULL);
  933. if (IS_ERR(s))
  934. return ERR_CAST(s);
  935. if (!s->s_root) {
  936. s->s_flags = flags;
  937. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  938. if (error) {
  939. deactivate_locked_super(s);
  940. return ERR_PTR(error);
  941. }
  942. s->s_flags |= MS_ACTIVE;
  943. } else {
  944. do_remount_sb(s, flags, data, 0);
  945. }
  946. return dget(s->s_root);
  947. }
  948. EXPORT_SYMBOL(mount_single);
  949. struct dentry *
  950. mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
  951. {
  952. struct dentry *root;
  953. struct super_block *sb;
  954. char *secdata = NULL;
  955. int error = -ENOMEM;
  956. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  957. secdata = alloc_secdata();
  958. if (!secdata)
  959. goto out;
  960. error = security_sb_copy_data(data, secdata);
  961. if (error)
  962. goto out_free_secdata;
  963. }
  964. root = type->mount(type, flags, name, data);
  965. if (IS_ERR(root)) {
  966. error = PTR_ERR(root);
  967. goto out_free_secdata;
  968. }
  969. sb = root->d_sb;
  970. BUG_ON(!sb);
  971. WARN_ON(!sb->s_bdi);
  972. WARN_ON(sb->s_bdi == &default_backing_dev_info);
  973. sb->s_flags |= MS_BORN;
  974. error = security_sb_kern_mount(sb, flags, secdata);
  975. if (error)
  976. goto out_sb;
  977. /*
  978. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  979. * but s_maxbytes was an unsigned long long for many releases. Throw
  980. * this warning for a little while to try and catch filesystems that
  981. * violate this rule.
  982. */
  983. WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  984. "negative value (%lld)\n", type->name, sb->s_maxbytes);
  985. up_write(&sb->s_umount);
  986. free_secdata(secdata);
  987. return root;
  988. out_sb:
  989. dput(root);
  990. deactivate_locked_super(sb);
  991. out_free_secdata:
  992. free_secdata(secdata);
  993. out:
  994. return ERR_PTR(error);
  995. }
  996. /**
  997. * freeze_super - lock the filesystem and force it into a consistent state
  998. * @sb: the super to lock
  999. *
  1000. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  1001. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  1002. * -EBUSY.
  1003. */
  1004. int freeze_super(struct super_block *sb)
  1005. {
  1006. int ret;
  1007. atomic_inc(&sb->s_active);
  1008. down_write(&sb->s_umount);
  1009. if (sb->s_frozen) {
  1010. deactivate_locked_super(sb);
  1011. return -EBUSY;
  1012. }
  1013. if (!(sb->s_flags & MS_BORN)) {
  1014. up_write(&sb->s_umount);
  1015. return 0; /* sic - it's "nothing to do" */
  1016. }
  1017. if (sb->s_flags & MS_RDONLY) {
  1018. sb->s_frozen = SB_FREEZE_TRANS;
  1019. smp_wmb();
  1020. up_write(&sb->s_umount);
  1021. return 0;
  1022. }
  1023. sb->s_frozen = SB_FREEZE_WRITE;
  1024. smp_wmb();
  1025. sync_filesystem(sb);
  1026. sb->s_frozen = SB_FREEZE_TRANS;
  1027. smp_wmb();
  1028. sync_blockdev(sb->s_bdev);
  1029. if (sb->s_op->freeze_fs) {
  1030. ret = sb->s_op->freeze_fs(sb);
  1031. if (ret) {
  1032. printk(KERN_ERR
  1033. "VFS:Filesystem freeze failed\n");
  1034. sb->s_frozen = SB_UNFROZEN;
  1035. deactivate_locked_super(sb);
  1036. return ret;
  1037. }
  1038. }
  1039. up_write(&sb->s_umount);
  1040. return 0;
  1041. }
  1042. EXPORT_SYMBOL(freeze_super);
  1043. /**
  1044. * thaw_super -- unlock filesystem
  1045. * @sb: the super to thaw
  1046. *
  1047. * Unlocks the filesystem and marks it writeable again after freeze_super().
  1048. */
  1049. int thaw_super(struct super_block *sb)
  1050. {
  1051. int error;
  1052. down_write(&sb->s_umount);
  1053. if (sb->s_frozen == SB_UNFROZEN) {
  1054. up_write(&sb->s_umount);
  1055. return -EINVAL;
  1056. }
  1057. if (sb->s_flags & MS_RDONLY)
  1058. goto out;
  1059. if (sb->s_op->unfreeze_fs) {
  1060. error = sb->s_op->unfreeze_fs(sb);
  1061. if (error) {
  1062. printk(KERN_ERR
  1063. "VFS:Filesystem thaw failed\n");
  1064. sb->s_frozen = SB_FREEZE_TRANS;
  1065. up_write(&sb->s_umount);
  1066. return error;
  1067. }
  1068. }
  1069. out:
  1070. sb->s_frozen = SB_UNFROZEN;
  1071. smp_wmb();
  1072. wake_up(&sb->s_wait_unfrozen);
  1073. deactivate_locked_super(sb);
  1074. return 0;
  1075. }
  1076. EXPORT_SYMBOL(thaw_super);