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