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