super.c 30 KB

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