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