super.c 30 KB

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