super.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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/blkdev.h>
  25. #include <linux/mount.h>
  26. #include <linux/security.h>
  27. #include <linux/writeback.h> /* for the emergency remount stuff */
  28. #include <linux/idr.h>
  29. #include <linux/mutex.h>
  30. #include <linux/backing-dev.h>
  31. #include <linux/rculist_bl.h>
  32. #include <linux/cleancache.h>
  33. #include <linux/fsnotify.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/user_namespace.h>
  36. #include "internal.h"
  37. static LIST_HEAD(super_blocks);
  38. static DEFINE_SPINLOCK(sb_lock);
  39. static char *sb_writers_name[SB_FREEZE_LEVELS] = {
  40. "sb_writers",
  41. "sb_pagefaults",
  42. "sb_internal",
  43. };
  44. /*
  45. * One thing we have to be careful of with a per-sb shrinker is that we don't
  46. * drop the last active reference to the superblock from within the shrinker.
  47. * If that happens we could trigger unregistering the shrinker from within the
  48. * shrinker path and that leads to deadlock on the shrinker_rwsem. Hence we
  49. * take a passive reference to the superblock to avoid this from occurring.
  50. */
  51. static unsigned long super_cache_scan(struct shrinker *shrink,
  52. struct shrink_control *sc)
  53. {
  54. struct super_block *sb;
  55. long fs_objects = 0;
  56. long total_objects;
  57. long freed = 0;
  58. long dentries;
  59. long inodes;
  60. sb = container_of(shrink, struct super_block, s_shrink);
  61. /*
  62. * Deadlock avoidance. We may hold various FS locks, and we don't want
  63. * to recurse into the FS that called us in clear_inode() and friends..
  64. */
  65. if (!(sc->gfp_mask & __GFP_FS))
  66. return SHRINK_STOP;
  67. if (!trylock_super(sb))
  68. return SHRINK_STOP;
  69. if (sb->s_op->nr_cached_objects)
  70. fs_objects = sb->s_op->nr_cached_objects(sb, sc);
  71. inodes = list_lru_shrink_count(&sb->s_inode_lru, sc);
  72. dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc);
  73. total_objects = dentries + inodes + fs_objects + 1;
  74. if (!total_objects)
  75. total_objects = 1;
  76. /* proportion the scan between the caches */
  77. dentries = mult_frac(sc->nr_to_scan, dentries, total_objects);
  78. inodes = mult_frac(sc->nr_to_scan, inodes, total_objects);
  79. fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects);
  80. /*
  81. * prune the dcache first as the icache is pinned by it, then
  82. * prune the icache, followed by the filesystem specific caches
  83. *
  84. * Ensure that we always scan at least one object - memcg kmem
  85. * accounting uses this to fully empty the caches.
  86. */
  87. sc->nr_to_scan = dentries + 1;
  88. freed = prune_dcache_sb(sb, sc);
  89. sc->nr_to_scan = inodes + 1;
  90. freed += prune_icache_sb(sb, sc);
  91. if (fs_objects) {
  92. sc->nr_to_scan = fs_objects + 1;
  93. freed += sb->s_op->free_cached_objects(sb, sc);
  94. }
  95. up_read(&sb->s_umount);
  96. return freed;
  97. }
  98. static unsigned long super_cache_count(struct shrinker *shrink,
  99. struct shrink_control *sc)
  100. {
  101. struct super_block *sb;
  102. long total_objects = 0;
  103. sb = container_of(shrink, struct super_block, s_shrink);
  104. /*
  105. * Don't call trylock_super as it is a potential
  106. * scalability bottleneck. The counts could get updated
  107. * between super_cache_count and super_cache_scan anyway.
  108. * Call to super_cache_count with shrinker_rwsem held
  109. * ensures the safety of call to list_lru_shrink_count() and
  110. * s_op->nr_cached_objects().
  111. */
  112. if (sb->s_op && sb->s_op->nr_cached_objects)
  113. total_objects = sb->s_op->nr_cached_objects(sb, sc);
  114. total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc);
  115. total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc);
  116. total_objects = vfs_pressure_ratio(total_objects);
  117. return total_objects;
  118. }
  119. static void destroy_super_work(struct work_struct *work)
  120. {
  121. struct super_block *s = container_of(work, struct super_block,
  122. destroy_work);
  123. int i;
  124. for (i = 0; i < SB_FREEZE_LEVELS; i++)
  125. percpu_free_rwsem(&s->s_writers.rw_sem[i]);
  126. kfree(s);
  127. }
  128. static void destroy_super_rcu(struct rcu_head *head)
  129. {
  130. struct super_block *s = container_of(head, struct super_block, rcu);
  131. INIT_WORK(&s->destroy_work, destroy_super_work);
  132. schedule_work(&s->destroy_work);
  133. }
  134. /**
  135. * destroy_super - frees a superblock
  136. * @s: superblock to free
  137. *
  138. * Frees a superblock.
  139. */
  140. static void destroy_super(struct super_block *s)
  141. {
  142. list_lru_destroy(&s->s_dentry_lru);
  143. list_lru_destroy(&s->s_inode_lru);
  144. security_sb_free(s);
  145. WARN_ON(!list_empty(&s->s_mounts));
  146. put_user_ns(s->s_user_ns);
  147. kfree(s->s_subtype);
  148. call_rcu(&s->rcu, destroy_super_rcu);
  149. }
  150. /**
  151. * alloc_super - create new superblock
  152. * @type: filesystem type superblock should belong to
  153. * @flags: the mount flags
  154. * @user_ns: User namespace for the super_block
  155. *
  156. * Allocates and initializes a new &struct super_block. alloc_super()
  157. * returns a pointer new superblock or %NULL if allocation had failed.
  158. */
  159. static struct super_block *alloc_super(struct file_system_type *type, int flags,
  160. struct user_namespace *user_ns)
  161. {
  162. struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER);
  163. static const struct super_operations default_op;
  164. int i;
  165. if (!s)
  166. return NULL;
  167. INIT_LIST_HEAD(&s->s_mounts);
  168. s->s_user_ns = get_user_ns(user_ns);
  169. if (security_sb_alloc(s))
  170. goto fail;
  171. for (i = 0; i < SB_FREEZE_LEVELS; i++) {
  172. if (__percpu_init_rwsem(&s->s_writers.rw_sem[i],
  173. sb_writers_name[i],
  174. &type->s_writers_key[i]))
  175. goto fail;
  176. }
  177. init_waitqueue_head(&s->s_writers.wait_unfrozen);
  178. s->s_bdi = &noop_backing_dev_info;
  179. s->s_flags = flags;
  180. if (s->s_user_ns != &init_user_ns)
  181. s->s_iflags |= SB_I_NODEV;
  182. INIT_HLIST_NODE(&s->s_instances);
  183. INIT_HLIST_BL_HEAD(&s->s_anon);
  184. mutex_init(&s->s_sync_lock);
  185. INIT_LIST_HEAD(&s->s_inodes);
  186. spin_lock_init(&s->s_inode_list_lock);
  187. INIT_LIST_HEAD(&s->s_inodes_wb);
  188. spin_lock_init(&s->s_inode_wblist_lock);
  189. if (list_lru_init_memcg(&s->s_dentry_lru))
  190. goto fail;
  191. if (list_lru_init_memcg(&s->s_inode_lru))
  192. goto fail;
  193. init_rwsem(&s->s_umount);
  194. lockdep_set_class(&s->s_umount, &type->s_umount_key);
  195. /*
  196. * sget() can have s_umount recursion.
  197. *
  198. * When it cannot find a suitable sb, it allocates a new
  199. * one (this one), and tries again to find a suitable old
  200. * one.
  201. *
  202. * In case that succeeds, it will acquire the s_umount
  203. * lock of the old one. Since these are clearly distrinct
  204. * locks, and this object isn't exposed yet, there's no
  205. * risk of deadlocks.
  206. *
  207. * Annotate this by putting this lock in a different
  208. * subclass.
  209. */
  210. down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING);
  211. s->s_count = 1;
  212. atomic_set(&s->s_active, 1);
  213. mutex_init(&s->s_vfs_rename_mutex);
  214. lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key);
  215. mutex_init(&s->s_dquot.dqio_mutex);
  216. s->s_maxbytes = MAX_NON_LFS;
  217. s->s_op = &default_op;
  218. s->s_time_gran = 1000000000;
  219. s->cleancache_poolid = CLEANCACHE_NO_POOL;
  220. s->s_shrink.seeks = DEFAULT_SEEKS;
  221. s->s_shrink.scan_objects = super_cache_scan;
  222. s->s_shrink.count_objects = super_cache_count;
  223. s->s_shrink.batch = 1024;
  224. s->s_shrink.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE;
  225. return s;
  226. fail:
  227. destroy_super(s);
  228. return NULL;
  229. }
  230. /* Superblock refcounting */
  231. /*
  232. * Drop a superblock's refcount. The caller must hold sb_lock.
  233. */
  234. static void __put_super(struct super_block *sb)
  235. {
  236. if (!--sb->s_count) {
  237. list_del_init(&sb->s_list);
  238. destroy_super(sb);
  239. }
  240. }
  241. /**
  242. * put_super - drop a temporary reference to superblock
  243. * @sb: superblock in question
  244. *
  245. * Drops a temporary reference, frees superblock if there's no
  246. * references left.
  247. */
  248. static void put_super(struct super_block *sb)
  249. {
  250. spin_lock(&sb_lock);
  251. __put_super(sb);
  252. spin_unlock(&sb_lock);
  253. }
  254. /**
  255. * deactivate_locked_super - drop an active reference to superblock
  256. * @s: superblock to deactivate
  257. *
  258. * Drops an active reference to superblock, converting it into a temporary
  259. * one if there is no other active references left. In that case we
  260. * tell fs driver to shut it down and drop the temporary reference we
  261. * had just acquired.
  262. *
  263. * Caller holds exclusive lock on superblock; that lock is released.
  264. */
  265. void deactivate_locked_super(struct super_block *s)
  266. {
  267. struct file_system_type *fs = s->s_type;
  268. if (atomic_dec_and_test(&s->s_active)) {
  269. cleancache_invalidate_fs(s);
  270. unregister_shrinker(&s->s_shrink);
  271. fs->kill_sb(s);
  272. /*
  273. * Since list_lru_destroy() may sleep, we cannot call it from
  274. * put_super(), where we hold the sb_lock. Therefore we destroy
  275. * the lru lists right now.
  276. */
  277. list_lru_destroy(&s->s_dentry_lru);
  278. list_lru_destroy(&s->s_inode_lru);
  279. put_filesystem(fs);
  280. put_super(s);
  281. } else {
  282. up_write(&s->s_umount);
  283. }
  284. }
  285. EXPORT_SYMBOL(deactivate_locked_super);
  286. /**
  287. * deactivate_super - drop an active reference to superblock
  288. * @s: superblock to deactivate
  289. *
  290. * Variant of deactivate_locked_super(), except that superblock is *not*
  291. * locked by caller. If we are going to drop the final active reference,
  292. * lock will be acquired prior to that.
  293. */
  294. void deactivate_super(struct super_block *s)
  295. {
  296. if (!atomic_add_unless(&s->s_active, -1, 1)) {
  297. down_write(&s->s_umount);
  298. deactivate_locked_super(s);
  299. }
  300. }
  301. EXPORT_SYMBOL(deactivate_super);
  302. /**
  303. * grab_super - acquire an active reference
  304. * @s: reference we are trying to make active
  305. *
  306. * Tries to acquire an active reference. grab_super() is used when we
  307. * had just found a superblock in super_blocks or fs_type->fs_supers
  308. * and want to turn it into a full-blown active reference. grab_super()
  309. * is called with sb_lock held and drops it. Returns 1 in case of
  310. * success, 0 if we had failed (superblock contents was already dead or
  311. * dying when grab_super() had been called). Note that this is only
  312. * called for superblocks not in rundown mode (== ones still on ->fs_supers
  313. * of their type), so increment of ->s_count is OK here.
  314. */
  315. static int grab_super(struct super_block *s) __releases(sb_lock)
  316. {
  317. s->s_count++;
  318. spin_unlock(&sb_lock);
  319. down_write(&s->s_umount);
  320. if ((s->s_flags & MS_BORN) && atomic_inc_not_zero(&s->s_active)) {
  321. put_super(s);
  322. return 1;
  323. }
  324. up_write(&s->s_umount);
  325. put_super(s);
  326. return 0;
  327. }
  328. /*
  329. * trylock_super - try to grab ->s_umount shared
  330. * @sb: reference we are trying to grab
  331. *
  332. * Try to prevent fs shutdown. This is used in places where we
  333. * cannot take an active reference but we need to ensure that the
  334. * filesystem is not shut down while we are working on it. It returns
  335. * false if we cannot acquire s_umount or if we lose the race and
  336. * filesystem already got into shutdown, and returns true with the s_umount
  337. * lock held in read mode in case of success. On successful return,
  338. * the caller must drop the s_umount lock when done.
  339. *
  340. * Note that unlike get_super() et.al. this one does *not* bump ->s_count.
  341. * The reason why it's safe is that we are OK with doing trylock instead
  342. * of down_read(). There's a couple of places that are OK with that, but
  343. * it's very much not a general-purpose interface.
  344. */
  345. bool trylock_super(struct super_block *sb)
  346. {
  347. if (down_read_trylock(&sb->s_umount)) {
  348. if (!hlist_unhashed(&sb->s_instances) &&
  349. sb->s_root && (sb->s_flags & MS_BORN))
  350. return true;
  351. up_read(&sb->s_umount);
  352. }
  353. return false;
  354. }
  355. /**
  356. * generic_shutdown_super - common helper for ->kill_sb()
  357. * @sb: superblock to kill
  358. *
  359. * generic_shutdown_super() does all fs-independent work on superblock
  360. * shutdown. Typical ->kill_sb() should pick all fs-specific objects
  361. * that need destruction out of superblock, call generic_shutdown_super()
  362. * and release aforementioned objects. Note: dentries and inodes _are_
  363. * taken care of and do not need specific handling.
  364. *
  365. * Upon calling this function, the filesystem may no longer alter or
  366. * rearrange the set of dentries belonging to this super_block, nor may it
  367. * change the attachments of dentries to inodes.
  368. */
  369. void generic_shutdown_super(struct super_block *sb)
  370. {
  371. const struct super_operations *sop = sb->s_op;
  372. if (sb->s_root) {
  373. shrink_dcache_for_umount(sb);
  374. sync_filesystem(sb);
  375. sb->s_flags &= ~MS_ACTIVE;
  376. fsnotify_unmount_inodes(sb);
  377. cgroup_writeback_umount();
  378. evict_inodes(sb);
  379. if (sb->s_dio_done_wq) {
  380. destroy_workqueue(sb->s_dio_done_wq);
  381. sb->s_dio_done_wq = NULL;
  382. }
  383. if (sop->put_super)
  384. sop->put_super(sb);
  385. if (!list_empty(&sb->s_inodes)) {
  386. printk("VFS: Busy inodes after unmount of %s. "
  387. "Self-destruct in 5 seconds. Have a nice day...\n",
  388. sb->s_id);
  389. }
  390. }
  391. spin_lock(&sb_lock);
  392. /* should be initialized for __put_super_and_need_restart() */
  393. hlist_del_init(&sb->s_instances);
  394. spin_unlock(&sb_lock);
  395. up_write(&sb->s_umount);
  396. if (sb->s_bdi != &noop_backing_dev_info) {
  397. bdi_put(sb->s_bdi);
  398. sb->s_bdi = &noop_backing_dev_info;
  399. }
  400. }
  401. EXPORT_SYMBOL(generic_shutdown_super);
  402. /**
  403. * sget_userns - find or create a superblock
  404. * @type: filesystem type superblock should belong to
  405. * @test: comparison callback
  406. * @set: setup callback
  407. * @flags: mount flags
  408. * @user_ns: User namespace for the super_block
  409. * @data: argument to each of them
  410. */
  411. struct super_block *sget_userns(struct file_system_type *type,
  412. int (*test)(struct super_block *,void *),
  413. int (*set)(struct super_block *,void *),
  414. int flags, struct user_namespace *user_ns,
  415. void *data)
  416. {
  417. struct super_block *s = NULL;
  418. struct super_block *old;
  419. int err;
  420. if (!(flags & (MS_KERNMOUNT|MS_SUBMOUNT)) &&
  421. !(type->fs_flags & FS_USERNS_MOUNT) &&
  422. !capable(CAP_SYS_ADMIN))
  423. return ERR_PTR(-EPERM);
  424. retry:
  425. spin_lock(&sb_lock);
  426. if (test) {
  427. hlist_for_each_entry(old, &type->fs_supers, s_instances) {
  428. if (!test(old, data))
  429. continue;
  430. if (user_ns != old->s_user_ns) {
  431. spin_unlock(&sb_lock);
  432. if (s) {
  433. up_write(&s->s_umount);
  434. destroy_super(s);
  435. }
  436. return ERR_PTR(-EBUSY);
  437. }
  438. if (!grab_super(old))
  439. goto retry;
  440. if (s) {
  441. up_write(&s->s_umount);
  442. destroy_super(s);
  443. s = NULL;
  444. }
  445. return old;
  446. }
  447. }
  448. if (!s) {
  449. spin_unlock(&sb_lock);
  450. s = alloc_super(type, (flags & ~MS_SUBMOUNT), user_ns);
  451. if (!s)
  452. return ERR_PTR(-ENOMEM);
  453. goto retry;
  454. }
  455. err = set(s, data);
  456. if (err) {
  457. spin_unlock(&sb_lock);
  458. up_write(&s->s_umount);
  459. destroy_super(s);
  460. return ERR_PTR(err);
  461. }
  462. s->s_type = type;
  463. strlcpy(s->s_id, type->name, sizeof(s->s_id));
  464. list_add_tail(&s->s_list, &super_blocks);
  465. hlist_add_head(&s->s_instances, &type->fs_supers);
  466. spin_unlock(&sb_lock);
  467. get_filesystem(type);
  468. register_shrinker(&s->s_shrink);
  469. return s;
  470. }
  471. EXPORT_SYMBOL(sget_userns);
  472. /**
  473. * sget - find or create a superblock
  474. * @type: filesystem type superblock should belong to
  475. * @test: comparison callback
  476. * @set: setup callback
  477. * @flags: mount flags
  478. * @data: argument to each of them
  479. */
  480. struct super_block *sget(struct file_system_type *type,
  481. int (*test)(struct super_block *,void *),
  482. int (*set)(struct super_block *,void *),
  483. int flags,
  484. void *data)
  485. {
  486. struct user_namespace *user_ns = current_user_ns();
  487. /* We don't yet pass the user namespace of the parent
  488. * mount through to here so always use &init_user_ns
  489. * until that changes.
  490. */
  491. if (flags & MS_SUBMOUNT)
  492. user_ns = &init_user_ns;
  493. /* Ensure the requestor has permissions over the target filesystem */
  494. if (!(flags & (MS_KERNMOUNT|MS_SUBMOUNT)) && !ns_capable(user_ns, CAP_SYS_ADMIN))
  495. return ERR_PTR(-EPERM);
  496. return sget_userns(type, test, set, flags, user_ns, data);
  497. }
  498. EXPORT_SYMBOL(sget);
  499. void drop_super(struct super_block *sb)
  500. {
  501. up_read(&sb->s_umount);
  502. put_super(sb);
  503. }
  504. EXPORT_SYMBOL(drop_super);
  505. void drop_super_exclusive(struct super_block *sb)
  506. {
  507. up_write(&sb->s_umount);
  508. put_super(sb);
  509. }
  510. EXPORT_SYMBOL(drop_super_exclusive);
  511. /**
  512. * iterate_supers - call function for all active superblocks
  513. * @f: function to call
  514. * @arg: argument to pass to it
  515. *
  516. * Scans the superblock list and calls given function, passing it
  517. * locked superblock and given argument.
  518. */
  519. void iterate_supers(void (*f)(struct super_block *, void *), void *arg)
  520. {
  521. struct super_block *sb, *p = NULL;
  522. spin_lock(&sb_lock);
  523. list_for_each_entry(sb, &super_blocks, s_list) {
  524. if (hlist_unhashed(&sb->s_instances))
  525. continue;
  526. sb->s_count++;
  527. spin_unlock(&sb_lock);
  528. down_read(&sb->s_umount);
  529. if (sb->s_root && (sb->s_flags & MS_BORN))
  530. f(sb, arg);
  531. up_read(&sb->s_umount);
  532. spin_lock(&sb_lock);
  533. if (p)
  534. __put_super(p);
  535. p = sb;
  536. }
  537. if (p)
  538. __put_super(p);
  539. spin_unlock(&sb_lock);
  540. }
  541. /**
  542. * iterate_supers_type - call function for superblocks of given type
  543. * @type: fs type
  544. * @f: function to call
  545. * @arg: argument to pass to it
  546. *
  547. * Scans the superblock list and calls given function, passing it
  548. * locked superblock and given argument.
  549. */
  550. void iterate_supers_type(struct file_system_type *type,
  551. void (*f)(struct super_block *, void *), void *arg)
  552. {
  553. struct super_block *sb, *p = NULL;
  554. spin_lock(&sb_lock);
  555. hlist_for_each_entry(sb, &type->fs_supers, s_instances) {
  556. sb->s_count++;
  557. spin_unlock(&sb_lock);
  558. down_read(&sb->s_umount);
  559. if (sb->s_root && (sb->s_flags & MS_BORN))
  560. f(sb, arg);
  561. up_read(&sb->s_umount);
  562. spin_lock(&sb_lock);
  563. if (p)
  564. __put_super(p);
  565. p = sb;
  566. }
  567. if (p)
  568. __put_super(p);
  569. spin_unlock(&sb_lock);
  570. }
  571. EXPORT_SYMBOL(iterate_supers_type);
  572. static struct super_block *__get_super(struct block_device *bdev, bool excl)
  573. {
  574. struct super_block *sb;
  575. if (!bdev)
  576. return NULL;
  577. spin_lock(&sb_lock);
  578. rescan:
  579. list_for_each_entry(sb, &super_blocks, s_list) {
  580. if (hlist_unhashed(&sb->s_instances))
  581. continue;
  582. if (sb->s_bdev == bdev) {
  583. sb->s_count++;
  584. spin_unlock(&sb_lock);
  585. if (!excl)
  586. down_read(&sb->s_umount);
  587. else
  588. down_write(&sb->s_umount);
  589. /* still alive? */
  590. if (sb->s_root && (sb->s_flags & MS_BORN))
  591. return sb;
  592. if (!excl)
  593. up_read(&sb->s_umount);
  594. else
  595. up_write(&sb->s_umount);
  596. /* nope, got unmounted */
  597. spin_lock(&sb_lock);
  598. __put_super(sb);
  599. goto rescan;
  600. }
  601. }
  602. spin_unlock(&sb_lock);
  603. return NULL;
  604. }
  605. /**
  606. * get_super - get the superblock of a device
  607. * @bdev: device to get the superblock for
  608. *
  609. * Scans the superblock list and finds the superblock of the file system
  610. * mounted on the device given. %NULL is returned if no match is found.
  611. */
  612. struct super_block *get_super(struct block_device *bdev)
  613. {
  614. return __get_super(bdev, false);
  615. }
  616. EXPORT_SYMBOL(get_super);
  617. static struct super_block *__get_super_thawed(struct block_device *bdev,
  618. bool excl)
  619. {
  620. while (1) {
  621. struct super_block *s = __get_super(bdev, excl);
  622. if (!s || s->s_writers.frozen == SB_UNFROZEN)
  623. return s;
  624. if (!excl)
  625. up_read(&s->s_umount);
  626. else
  627. up_write(&s->s_umount);
  628. wait_event(s->s_writers.wait_unfrozen,
  629. s->s_writers.frozen == SB_UNFROZEN);
  630. put_super(s);
  631. }
  632. }
  633. /**
  634. * get_super_thawed - get thawed superblock of a device
  635. * @bdev: device to get the superblock for
  636. *
  637. * Scans the superblock list and finds the superblock of the file system
  638. * mounted on the device. The superblock is returned once it is thawed
  639. * (or immediately if it was not frozen). %NULL is returned if no match
  640. * is found.
  641. */
  642. struct super_block *get_super_thawed(struct block_device *bdev)
  643. {
  644. return __get_super_thawed(bdev, false);
  645. }
  646. EXPORT_SYMBOL(get_super_thawed);
  647. /**
  648. * get_super_exclusive_thawed - get thawed superblock of a device
  649. * @bdev: device to get the superblock for
  650. *
  651. * Scans the superblock list and finds the superblock of the file system
  652. * mounted on the device. The superblock is returned once it is thawed
  653. * (or immediately if it was not frozen) and s_umount semaphore is held
  654. * in exclusive mode. %NULL is returned if no match is found.
  655. */
  656. struct super_block *get_super_exclusive_thawed(struct block_device *bdev)
  657. {
  658. return __get_super_thawed(bdev, true);
  659. }
  660. EXPORT_SYMBOL(get_super_exclusive_thawed);
  661. /**
  662. * get_active_super - get an active reference to the superblock of a device
  663. * @bdev: device to get the superblock for
  664. *
  665. * Scans the superblock list and finds the superblock of the file system
  666. * mounted on the device given. Returns the superblock with an active
  667. * reference or %NULL if none was found.
  668. */
  669. struct super_block *get_active_super(struct block_device *bdev)
  670. {
  671. struct super_block *sb;
  672. if (!bdev)
  673. return NULL;
  674. restart:
  675. spin_lock(&sb_lock);
  676. list_for_each_entry(sb, &super_blocks, s_list) {
  677. if (hlist_unhashed(&sb->s_instances))
  678. continue;
  679. if (sb->s_bdev == bdev) {
  680. if (!grab_super(sb))
  681. goto restart;
  682. up_write(&sb->s_umount);
  683. return sb;
  684. }
  685. }
  686. spin_unlock(&sb_lock);
  687. return NULL;
  688. }
  689. struct super_block *user_get_super(dev_t dev)
  690. {
  691. struct super_block *sb;
  692. spin_lock(&sb_lock);
  693. rescan:
  694. list_for_each_entry(sb, &super_blocks, s_list) {
  695. if (hlist_unhashed(&sb->s_instances))
  696. continue;
  697. if (sb->s_dev == dev) {
  698. sb->s_count++;
  699. spin_unlock(&sb_lock);
  700. down_read(&sb->s_umount);
  701. /* still alive? */
  702. if (sb->s_root && (sb->s_flags & MS_BORN))
  703. return sb;
  704. up_read(&sb->s_umount);
  705. /* nope, got unmounted */
  706. spin_lock(&sb_lock);
  707. __put_super(sb);
  708. goto rescan;
  709. }
  710. }
  711. spin_unlock(&sb_lock);
  712. return NULL;
  713. }
  714. /**
  715. * do_remount_sb - asks filesystem to change mount options.
  716. * @sb: superblock in question
  717. * @flags: numeric part of options
  718. * @data: the rest of options
  719. * @force: whether or not to force the change
  720. *
  721. * Alters the mount options of a mounted file system.
  722. */
  723. int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
  724. {
  725. int retval;
  726. int remount_ro;
  727. if (sb->s_writers.frozen != SB_UNFROZEN)
  728. return -EBUSY;
  729. #ifdef CONFIG_BLOCK
  730. if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev))
  731. return -EACCES;
  732. #endif
  733. remount_ro = (flags & MS_RDONLY) && !sb_rdonly(sb);
  734. if (remount_ro) {
  735. if (!hlist_empty(&sb->s_pins)) {
  736. up_write(&sb->s_umount);
  737. group_pin_kill(&sb->s_pins);
  738. down_write(&sb->s_umount);
  739. if (!sb->s_root)
  740. return 0;
  741. if (sb->s_writers.frozen != SB_UNFROZEN)
  742. return -EBUSY;
  743. remount_ro = (flags & MS_RDONLY) && !sb_rdonly(sb);
  744. }
  745. }
  746. shrink_dcache_sb(sb);
  747. /* If we are remounting RDONLY and current sb is read/write,
  748. make sure there are no rw files opened */
  749. if (remount_ro) {
  750. if (force) {
  751. sb->s_readonly_remount = 1;
  752. smp_wmb();
  753. } else {
  754. retval = sb_prepare_remount_readonly(sb);
  755. if (retval)
  756. return retval;
  757. }
  758. }
  759. if (sb->s_op->remount_fs) {
  760. retval = sb->s_op->remount_fs(sb, &flags, data);
  761. if (retval) {
  762. if (!force)
  763. goto cancel_readonly;
  764. /* If forced remount, go ahead despite any errors */
  765. WARN(1, "forced remount of a %s fs returned %i\n",
  766. sb->s_type->name, retval);
  767. }
  768. }
  769. sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
  770. /* Needs to be ordered wrt mnt_is_readonly() */
  771. smp_wmb();
  772. sb->s_readonly_remount = 0;
  773. /*
  774. * Some filesystems modify their metadata via some other path than the
  775. * bdev buffer cache (eg. use a private mapping, or directories in
  776. * pagecache, etc). Also file data modifications go via their own
  777. * mappings. So If we try to mount readonly then copy the filesystem
  778. * from bdev, we could get stale data, so invalidate it to give a best
  779. * effort at coherency.
  780. */
  781. if (remount_ro && sb->s_bdev)
  782. invalidate_bdev(sb->s_bdev);
  783. return 0;
  784. cancel_readonly:
  785. sb->s_readonly_remount = 0;
  786. return retval;
  787. }
  788. static void do_emergency_remount(struct work_struct *work)
  789. {
  790. struct super_block *sb, *p = NULL;
  791. spin_lock(&sb_lock);
  792. list_for_each_entry(sb, &super_blocks, s_list) {
  793. if (hlist_unhashed(&sb->s_instances))
  794. continue;
  795. sb->s_count++;
  796. spin_unlock(&sb_lock);
  797. down_write(&sb->s_umount);
  798. if (sb->s_root && sb->s_bdev && (sb->s_flags & MS_BORN) &&
  799. !sb_rdonly(sb)) {
  800. /*
  801. * What lock protects sb->s_flags??
  802. */
  803. do_remount_sb(sb, MS_RDONLY, NULL, 1);
  804. }
  805. up_write(&sb->s_umount);
  806. spin_lock(&sb_lock);
  807. if (p)
  808. __put_super(p);
  809. p = sb;
  810. }
  811. if (p)
  812. __put_super(p);
  813. spin_unlock(&sb_lock);
  814. kfree(work);
  815. printk("Emergency Remount complete\n");
  816. }
  817. void emergency_remount(void)
  818. {
  819. struct work_struct *work;
  820. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  821. if (work) {
  822. INIT_WORK(work, do_emergency_remount);
  823. schedule_work(work);
  824. }
  825. }
  826. /*
  827. * Unnamed block devices are dummy devices used by virtual
  828. * filesystems which don't use real block-devices. -- jrs
  829. */
  830. static DEFINE_IDA(unnamed_dev_ida);
  831. static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */
  832. /* Many userspace utilities consider an FSID of 0 invalid.
  833. * Always return at least 1 from get_anon_bdev.
  834. */
  835. static int unnamed_dev_start = 1;
  836. int get_anon_bdev(dev_t *p)
  837. {
  838. int dev;
  839. int error;
  840. retry:
  841. if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0)
  842. return -ENOMEM;
  843. spin_lock(&unnamed_dev_lock);
  844. error = ida_get_new_above(&unnamed_dev_ida, unnamed_dev_start, &dev);
  845. if (!error)
  846. unnamed_dev_start = dev + 1;
  847. spin_unlock(&unnamed_dev_lock);
  848. if (error == -EAGAIN)
  849. /* We raced and lost with another CPU. */
  850. goto retry;
  851. else if (error)
  852. return -EAGAIN;
  853. if (dev >= (1 << MINORBITS)) {
  854. spin_lock(&unnamed_dev_lock);
  855. ida_remove(&unnamed_dev_ida, dev);
  856. if (unnamed_dev_start > dev)
  857. unnamed_dev_start = dev;
  858. spin_unlock(&unnamed_dev_lock);
  859. return -EMFILE;
  860. }
  861. *p = MKDEV(0, dev & MINORMASK);
  862. return 0;
  863. }
  864. EXPORT_SYMBOL(get_anon_bdev);
  865. void free_anon_bdev(dev_t dev)
  866. {
  867. int slot = MINOR(dev);
  868. spin_lock(&unnamed_dev_lock);
  869. ida_remove(&unnamed_dev_ida, slot);
  870. if (slot < unnamed_dev_start)
  871. unnamed_dev_start = slot;
  872. spin_unlock(&unnamed_dev_lock);
  873. }
  874. EXPORT_SYMBOL(free_anon_bdev);
  875. int set_anon_super(struct super_block *s, void *data)
  876. {
  877. return get_anon_bdev(&s->s_dev);
  878. }
  879. EXPORT_SYMBOL(set_anon_super);
  880. void kill_anon_super(struct super_block *sb)
  881. {
  882. dev_t dev = sb->s_dev;
  883. generic_shutdown_super(sb);
  884. free_anon_bdev(dev);
  885. }
  886. EXPORT_SYMBOL(kill_anon_super);
  887. void kill_litter_super(struct super_block *sb)
  888. {
  889. if (sb->s_root)
  890. d_genocide(sb->s_root);
  891. kill_anon_super(sb);
  892. }
  893. EXPORT_SYMBOL(kill_litter_super);
  894. static int ns_test_super(struct super_block *sb, void *data)
  895. {
  896. return sb->s_fs_info == data;
  897. }
  898. static int ns_set_super(struct super_block *sb, void *data)
  899. {
  900. sb->s_fs_info = data;
  901. return set_anon_super(sb, NULL);
  902. }
  903. struct dentry *mount_ns(struct file_system_type *fs_type,
  904. int flags, void *data, void *ns, struct user_namespace *user_ns,
  905. int (*fill_super)(struct super_block *, void *, int))
  906. {
  907. struct super_block *sb;
  908. /* Don't allow mounting unless the caller has CAP_SYS_ADMIN
  909. * over the namespace.
  910. */
  911. if (!(flags & MS_KERNMOUNT) && !ns_capable(user_ns, CAP_SYS_ADMIN))
  912. return ERR_PTR(-EPERM);
  913. sb = sget_userns(fs_type, ns_test_super, ns_set_super, flags,
  914. user_ns, ns);
  915. if (IS_ERR(sb))
  916. return ERR_CAST(sb);
  917. if (!sb->s_root) {
  918. int err;
  919. err = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  920. if (err) {
  921. deactivate_locked_super(sb);
  922. return ERR_PTR(err);
  923. }
  924. sb->s_flags |= MS_ACTIVE;
  925. }
  926. return dget(sb->s_root);
  927. }
  928. EXPORT_SYMBOL(mount_ns);
  929. #ifdef CONFIG_BLOCK
  930. static int set_bdev_super(struct super_block *s, void *data)
  931. {
  932. s->s_bdev = data;
  933. s->s_dev = s->s_bdev->bd_dev;
  934. s->s_bdi = bdi_get(s->s_bdev->bd_bdi);
  935. return 0;
  936. }
  937. static int test_bdev_super(struct super_block *s, void *data)
  938. {
  939. return (void *)s->s_bdev == data;
  940. }
  941. struct dentry *mount_bdev(struct file_system_type *fs_type,
  942. int flags, const char *dev_name, void *data,
  943. int (*fill_super)(struct super_block *, void *, int))
  944. {
  945. struct block_device *bdev;
  946. struct super_block *s;
  947. fmode_t mode = FMODE_READ | FMODE_EXCL;
  948. int error = 0;
  949. if (!(flags & MS_RDONLY))
  950. mode |= FMODE_WRITE;
  951. bdev = blkdev_get_by_path(dev_name, mode, fs_type);
  952. if (IS_ERR(bdev))
  953. return ERR_CAST(bdev);
  954. /*
  955. * once the super is inserted into the list by sget, s_umount
  956. * will protect the lockfs code from trying to start a snapshot
  957. * while we are mounting
  958. */
  959. mutex_lock(&bdev->bd_fsfreeze_mutex);
  960. if (bdev->bd_fsfreeze_count > 0) {
  961. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  962. error = -EBUSY;
  963. goto error_bdev;
  964. }
  965. s = sget(fs_type, test_bdev_super, set_bdev_super, flags | MS_NOSEC,
  966. bdev);
  967. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  968. if (IS_ERR(s))
  969. goto error_s;
  970. if (s->s_root) {
  971. if ((flags ^ s->s_flags) & MS_RDONLY) {
  972. deactivate_locked_super(s);
  973. error = -EBUSY;
  974. goto error_bdev;
  975. }
  976. /*
  977. * s_umount nests inside bd_mutex during
  978. * __invalidate_device(). blkdev_put() acquires
  979. * bd_mutex and can't be called under s_umount. Drop
  980. * s_umount temporarily. This is safe as we're
  981. * holding an active reference.
  982. */
  983. up_write(&s->s_umount);
  984. blkdev_put(bdev, mode);
  985. down_write(&s->s_umount);
  986. } else {
  987. s->s_mode = mode;
  988. snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
  989. sb_set_blocksize(s, block_size(bdev));
  990. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  991. if (error) {
  992. deactivate_locked_super(s);
  993. goto error;
  994. }
  995. s->s_flags |= MS_ACTIVE;
  996. bdev->bd_super = s;
  997. }
  998. return dget(s->s_root);
  999. error_s:
  1000. error = PTR_ERR(s);
  1001. error_bdev:
  1002. blkdev_put(bdev, mode);
  1003. error:
  1004. return ERR_PTR(error);
  1005. }
  1006. EXPORT_SYMBOL(mount_bdev);
  1007. void kill_block_super(struct super_block *sb)
  1008. {
  1009. struct block_device *bdev = sb->s_bdev;
  1010. fmode_t mode = sb->s_mode;
  1011. bdev->bd_super = NULL;
  1012. generic_shutdown_super(sb);
  1013. sync_blockdev(bdev);
  1014. WARN_ON_ONCE(!(mode & FMODE_EXCL));
  1015. blkdev_put(bdev, mode | FMODE_EXCL);
  1016. }
  1017. EXPORT_SYMBOL(kill_block_super);
  1018. #endif
  1019. struct dentry *mount_nodev(struct file_system_type *fs_type,
  1020. int flags, void *data,
  1021. int (*fill_super)(struct super_block *, void *, int))
  1022. {
  1023. int error;
  1024. struct super_block *s = sget(fs_type, NULL, set_anon_super, flags, NULL);
  1025. if (IS_ERR(s))
  1026. return ERR_CAST(s);
  1027. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  1028. if (error) {
  1029. deactivate_locked_super(s);
  1030. return ERR_PTR(error);
  1031. }
  1032. s->s_flags |= MS_ACTIVE;
  1033. return dget(s->s_root);
  1034. }
  1035. EXPORT_SYMBOL(mount_nodev);
  1036. static int compare_single(struct super_block *s, void *p)
  1037. {
  1038. return 1;
  1039. }
  1040. struct dentry *mount_single(struct file_system_type *fs_type,
  1041. int flags, void *data,
  1042. int (*fill_super)(struct super_block *, void *, int))
  1043. {
  1044. struct super_block *s;
  1045. int error;
  1046. s = sget(fs_type, compare_single, set_anon_super, flags, NULL);
  1047. if (IS_ERR(s))
  1048. return ERR_CAST(s);
  1049. if (!s->s_root) {
  1050. error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
  1051. if (error) {
  1052. deactivate_locked_super(s);
  1053. return ERR_PTR(error);
  1054. }
  1055. s->s_flags |= MS_ACTIVE;
  1056. } else {
  1057. do_remount_sb(s, flags, data, 0);
  1058. }
  1059. return dget(s->s_root);
  1060. }
  1061. EXPORT_SYMBOL(mount_single);
  1062. struct dentry *
  1063. mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
  1064. {
  1065. struct dentry *root;
  1066. struct super_block *sb;
  1067. char *secdata = NULL;
  1068. int error = -ENOMEM;
  1069. if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
  1070. secdata = alloc_secdata();
  1071. if (!secdata)
  1072. goto out;
  1073. error = security_sb_copy_data(data, secdata);
  1074. if (error)
  1075. goto out_free_secdata;
  1076. }
  1077. root = type->mount(type, flags, name, data);
  1078. if (IS_ERR(root)) {
  1079. error = PTR_ERR(root);
  1080. goto out_free_secdata;
  1081. }
  1082. sb = root->d_sb;
  1083. BUG_ON(!sb);
  1084. WARN_ON(!sb->s_bdi);
  1085. sb->s_flags |= MS_BORN;
  1086. error = security_sb_kern_mount(sb, flags, secdata);
  1087. if (error)
  1088. goto out_sb;
  1089. /*
  1090. * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE
  1091. * but s_maxbytes was an unsigned long long for many releases. Throw
  1092. * this warning for a little while to try and catch filesystems that
  1093. * violate this rule.
  1094. */
  1095. WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
  1096. "negative value (%lld)\n", type->name, sb->s_maxbytes);
  1097. up_write(&sb->s_umount);
  1098. free_secdata(secdata);
  1099. return root;
  1100. out_sb:
  1101. dput(root);
  1102. deactivate_locked_super(sb);
  1103. out_free_secdata:
  1104. free_secdata(secdata);
  1105. out:
  1106. return ERR_PTR(error);
  1107. }
  1108. /*
  1109. * Setup private BDI for given superblock. It gets automatically cleaned up
  1110. * in generic_shutdown_super().
  1111. */
  1112. int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
  1113. {
  1114. struct backing_dev_info *bdi;
  1115. int err;
  1116. va_list args;
  1117. bdi = bdi_alloc(GFP_KERNEL);
  1118. if (!bdi)
  1119. return -ENOMEM;
  1120. bdi->name = sb->s_type->name;
  1121. va_start(args, fmt);
  1122. err = bdi_register_va(bdi, fmt, args);
  1123. va_end(args);
  1124. if (err) {
  1125. bdi_put(bdi);
  1126. return err;
  1127. }
  1128. WARN_ON(sb->s_bdi != &noop_backing_dev_info);
  1129. sb->s_bdi = bdi;
  1130. return 0;
  1131. }
  1132. EXPORT_SYMBOL(super_setup_bdi_name);
  1133. /*
  1134. * Setup private BDI for given superblock. I gets automatically cleaned up
  1135. * in generic_shutdown_super().
  1136. */
  1137. int super_setup_bdi(struct super_block *sb)
  1138. {
  1139. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  1140. return super_setup_bdi_name(sb, "%.28s-%ld", sb->s_type->name,
  1141. atomic_long_inc_return(&bdi_seq));
  1142. }
  1143. EXPORT_SYMBOL(super_setup_bdi);
  1144. /*
  1145. * This is an internal function, please use sb_end_{write,pagefault,intwrite}
  1146. * instead.
  1147. */
  1148. void __sb_end_write(struct super_block *sb, int level)
  1149. {
  1150. percpu_up_read(sb->s_writers.rw_sem + level-1);
  1151. }
  1152. EXPORT_SYMBOL(__sb_end_write);
  1153. /*
  1154. * This is an internal function, please use sb_start_{write,pagefault,intwrite}
  1155. * instead.
  1156. */
  1157. int __sb_start_write(struct super_block *sb, int level, bool wait)
  1158. {
  1159. bool force_trylock = false;
  1160. int ret = 1;
  1161. #ifdef CONFIG_LOCKDEP
  1162. /*
  1163. * We want lockdep to tell us about possible deadlocks with freezing
  1164. * but it's it bit tricky to properly instrument it. Getting a freeze
  1165. * protection works as getting a read lock but there are subtle
  1166. * problems. XFS for example gets freeze protection on internal level
  1167. * twice in some cases, which is OK only because we already hold a
  1168. * freeze protection also on higher level. Due to these cases we have
  1169. * to use wait == F (trylock mode) which must not fail.
  1170. */
  1171. if (wait) {
  1172. int i;
  1173. for (i = 0; i < level - 1; i++)
  1174. if (percpu_rwsem_is_held(sb->s_writers.rw_sem + i)) {
  1175. force_trylock = true;
  1176. break;
  1177. }
  1178. }
  1179. #endif
  1180. if (wait && !force_trylock)
  1181. percpu_down_read(sb->s_writers.rw_sem + level-1);
  1182. else
  1183. ret = percpu_down_read_trylock(sb->s_writers.rw_sem + level-1);
  1184. WARN_ON(force_trylock && !ret);
  1185. return ret;
  1186. }
  1187. EXPORT_SYMBOL(__sb_start_write);
  1188. /**
  1189. * sb_wait_write - wait until all writers to given file system finish
  1190. * @sb: the super for which we wait
  1191. * @level: type of writers we wait for (normal vs page fault)
  1192. *
  1193. * This function waits until there are no writers of given type to given file
  1194. * system.
  1195. */
  1196. static void sb_wait_write(struct super_block *sb, int level)
  1197. {
  1198. percpu_down_write(sb->s_writers.rw_sem + level-1);
  1199. }
  1200. /*
  1201. * We are going to return to userspace and forget about these locks, the
  1202. * ownership goes to the caller of thaw_super() which does unlock().
  1203. */
  1204. static void lockdep_sb_freeze_release(struct super_block *sb)
  1205. {
  1206. int level;
  1207. for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--)
  1208. percpu_rwsem_release(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
  1209. }
  1210. /*
  1211. * Tell lockdep we are holding these locks before we call ->unfreeze_fs(sb).
  1212. */
  1213. static void lockdep_sb_freeze_acquire(struct super_block *sb)
  1214. {
  1215. int level;
  1216. for (level = 0; level < SB_FREEZE_LEVELS; ++level)
  1217. percpu_rwsem_acquire(sb->s_writers.rw_sem + level, 0, _THIS_IP_);
  1218. }
  1219. static void sb_freeze_unlock(struct super_block *sb)
  1220. {
  1221. int level;
  1222. for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--)
  1223. percpu_up_write(sb->s_writers.rw_sem + level);
  1224. }
  1225. /**
  1226. * freeze_super - lock the filesystem and force it into a consistent state
  1227. * @sb: the super to lock
  1228. *
  1229. * Syncs the super to make sure the filesystem is consistent and calls the fs's
  1230. * freeze_fs. Subsequent calls to this without first thawing the fs will return
  1231. * -EBUSY.
  1232. *
  1233. * During this function, sb->s_writers.frozen goes through these values:
  1234. *
  1235. * SB_UNFROZEN: File system is normal, all writes progress as usual.
  1236. *
  1237. * SB_FREEZE_WRITE: The file system is in the process of being frozen. New
  1238. * writes should be blocked, though page faults are still allowed. We wait for
  1239. * all writes to complete and then proceed to the next stage.
  1240. *
  1241. * SB_FREEZE_PAGEFAULT: Freezing continues. Now also page faults are blocked
  1242. * but internal fs threads can still modify the filesystem (although they
  1243. * should not dirty new pages or inodes), writeback can run etc. After waiting
  1244. * for all running page faults we sync the filesystem which will clean all
  1245. * dirty pages and inodes (no new dirty pages or inodes can be created when
  1246. * sync is running).
  1247. *
  1248. * SB_FREEZE_FS: The file system is frozen. Now all internal sources of fs
  1249. * modification are blocked (e.g. XFS preallocation truncation on inode
  1250. * reclaim). This is usually implemented by blocking new transactions for
  1251. * filesystems that have them and need this additional guard. After all
  1252. * internal writers are finished we call ->freeze_fs() to finish filesystem
  1253. * freezing. Then we transition to SB_FREEZE_COMPLETE state. This state is
  1254. * mostly auxiliary for filesystems to verify they do not modify frozen fs.
  1255. *
  1256. * sb->s_writers.frozen is protected by sb->s_umount.
  1257. */
  1258. int freeze_super(struct super_block *sb)
  1259. {
  1260. int ret;
  1261. atomic_inc(&sb->s_active);
  1262. down_write(&sb->s_umount);
  1263. if (sb->s_writers.frozen != SB_UNFROZEN) {
  1264. deactivate_locked_super(sb);
  1265. return -EBUSY;
  1266. }
  1267. if (!(sb->s_flags & MS_BORN)) {
  1268. up_write(&sb->s_umount);
  1269. return 0; /* sic - it's "nothing to do" */
  1270. }
  1271. if (sb_rdonly(sb)) {
  1272. /* Nothing to do really... */
  1273. sb->s_writers.frozen = SB_FREEZE_COMPLETE;
  1274. up_write(&sb->s_umount);
  1275. return 0;
  1276. }
  1277. sb->s_writers.frozen = SB_FREEZE_WRITE;
  1278. /* Release s_umount to preserve sb_start_write -> s_umount ordering */
  1279. up_write(&sb->s_umount);
  1280. sb_wait_write(sb, SB_FREEZE_WRITE);
  1281. down_write(&sb->s_umount);
  1282. /* Now we go and block page faults... */
  1283. sb->s_writers.frozen = SB_FREEZE_PAGEFAULT;
  1284. sb_wait_write(sb, SB_FREEZE_PAGEFAULT);
  1285. /* All writers are done so after syncing there won't be dirty data */
  1286. sync_filesystem(sb);
  1287. /* Now wait for internal filesystem counter */
  1288. sb->s_writers.frozen = SB_FREEZE_FS;
  1289. sb_wait_write(sb, SB_FREEZE_FS);
  1290. if (sb->s_op->freeze_fs) {
  1291. ret = sb->s_op->freeze_fs(sb);
  1292. if (ret) {
  1293. printk(KERN_ERR
  1294. "VFS:Filesystem freeze failed\n");
  1295. sb->s_writers.frozen = SB_UNFROZEN;
  1296. sb_freeze_unlock(sb);
  1297. wake_up(&sb->s_writers.wait_unfrozen);
  1298. deactivate_locked_super(sb);
  1299. return ret;
  1300. }
  1301. }
  1302. /*
  1303. * For debugging purposes so that fs can warn if it sees write activity
  1304. * when frozen is set to SB_FREEZE_COMPLETE, and for thaw_super().
  1305. */
  1306. sb->s_writers.frozen = SB_FREEZE_COMPLETE;
  1307. lockdep_sb_freeze_release(sb);
  1308. up_write(&sb->s_umount);
  1309. return 0;
  1310. }
  1311. EXPORT_SYMBOL(freeze_super);
  1312. /**
  1313. * thaw_super -- unlock filesystem
  1314. * @sb: the super to thaw
  1315. *
  1316. * Unlocks the filesystem and marks it writeable again after freeze_super().
  1317. */
  1318. int thaw_super(struct super_block *sb)
  1319. {
  1320. int error;
  1321. down_write(&sb->s_umount);
  1322. if (sb->s_writers.frozen != SB_FREEZE_COMPLETE) {
  1323. up_write(&sb->s_umount);
  1324. return -EINVAL;
  1325. }
  1326. if (sb_rdonly(sb)) {
  1327. sb->s_writers.frozen = SB_UNFROZEN;
  1328. goto out;
  1329. }
  1330. lockdep_sb_freeze_acquire(sb);
  1331. if (sb->s_op->unfreeze_fs) {
  1332. error = sb->s_op->unfreeze_fs(sb);
  1333. if (error) {
  1334. printk(KERN_ERR
  1335. "VFS:Filesystem thaw failed\n");
  1336. lockdep_sb_freeze_release(sb);
  1337. up_write(&sb->s_umount);
  1338. return error;
  1339. }
  1340. }
  1341. sb->s_writers.frozen = SB_UNFROZEN;
  1342. sb_freeze_unlock(sb);
  1343. out:
  1344. wake_up(&sb->s_writers.wait_unfrozen);
  1345. deactivate_locked_super(sb);
  1346. return 0;
  1347. }
  1348. EXPORT_SYMBOL(thaw_super);