super.c 30 KB

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