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