super.c 30 KB

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