]>
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> | |
25 | #include <linux/init.h> | |
26 | #include <linux/smp_lock.h> | |
27 | #include <linux/acct.h> | |
28 | #include <linux/blkdev.h> | |
29 | #include <linux/quotaops.h> | |
30 | #include <linux/namei.h> | |
31 | #include <linux/buffer_head.h> /* for fsync_super() */ | |
32 | #include <linux/mount.h> | |
33 | #include <linux/security.h> | |
34 | #include <linux/syscalls.h> | |
35 | #include <linux/vfs.h> | |
36 | #include <linux/writeback.h> /* for the emergency remount stuff */ | |
37 | #include <linux/idr.h> | |
38 | #include <linux/kobject.h> | |
353ab6e9 | 39 | #include <linux/mutex.h> |
49e0d02c | 40 | #include <linux/file.h> |
efaee192 | 41 | #include <linux/async.h> |
1da177e4 | 42 | #include <asm/uaccess.h> |
6d59e7f5 | 43 | #include "internal.h" |
1da177e4 LT |
44 | |
45 | ||
1da177e4 LT |
46 | LIST_HEAD(super_blocks); |
47 | DEFINE_SPINLOCK(sb_lock); | |
48 | ||
49 | /** | |
50 | * alloc_super - create new superblock | |
fe2bbc48 | 51 | * @type: filesystem type superblock should belong to |
1da177e4 LT |
52 | * |
53 | * Allocates and initializes a new &struct super_block. alloc_super() | |
54 | * returns a pointer new superblock or %NULL if allocation had failed. | |
55 | */ | |
cf516249 | 56 | static struct super_block *alloc_super(struct file_system_type *type) |
1da177e4 | 57 | { |
11b0b5ab | 58 | struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER); |
1da177e4 LT |
59 | static struct super_operations default_op; |
60 | ||
61 | if (s) { | |
1da177e4 LT |
62 | if (security_sb_alloc(s)) { |
63 | kfree(s); | |
64 | s = NULL; | |
65 | goto out; | |
66 | } | |
67 | INIT_LIST_HEAD(&s->s_dirty); | |
68 | INIT_LIST_HEAD(&s->s_io); | |
0e0f4fc2 | 69 | INIT_LIST_HEAD(&s->s_more_io); |
1da177e4 LT |
70 | INIT_LIST_HEAD(&s->s_files); |
71 | INIT_LIST_HEAD(&s->s_instances); | |
72 | INIT_HLIST_HEAD(&s->s_anon); | |
73 | INIT_LIST_HEAD(&s->s_inodes); | |
da3bbdd4 | 74 | INIT_LIST_HEAD(&s->s_dentry_lru); |
efaee192 | 75 | INIT_LIST_HEAD(&s->s_async_list); |
1da177e4 | 76 | init_rwsem(&s->s_umount); |
7892f2f4 | 77 | mutex_init(&s->s_lock); |
897c6ff9 | 78 | lockdep_set_class(&s->s_umount, &type->s_umount_key); |
cf516249 IM |
79 | /* |
80 | * The locking rules for s_lock are up to the | |
81 | * filesystem. For example ext3fs has different | |
82 | * lock ordering than usbfs: | |
83 | */ | |
84 | lockdep_set_class(&s->s_lock, &type->s_lock_key); | |
ada723dc PZ |
85 | /* |
86 | * sget() can have s_umount recursion. | |
87 | * | |
88 | * When it cannot find a suitable sb, it allocates a new | |
89 | * one (this one), and tries again to find a suitable old | |
90 | * one. | |
91 | * | |
92 | * In case that succeeds, it will acquire the s_umount | |
93 | * lock of the old one. Since these are clearly distrinct | |
94 | * locks, and this object isn't exposed yet, there's no | |
95 | * risk of deadlocks. | |
96 | * | |
97 | * Annotate this by putting this lock in a different | |
98 | * subclass. | |
99 | */ | |
100 | down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING); | |
1da177e4 LT |
101 | s->s_count = S_BIAS; |
102 | atomic_set(&s->s_active, 1); | |
a11f3a05 | 103 | mutex_init(&s->s_vfs_rename_mutex); |
d3be915f IM |
104 | mutex_init(&s->s_dquot.dqio_mutex); |
105 | mutex_init(&s->s_dquot.dqonoff_mutex); | |
1da177e4 LT |
106 | init_rwsem(&s->s_dquot.dqptr_sem); |
107 | init_waitqueue_head(&s->s_wait_unfrozen); | |
108 | s->s_maxbytes = MAX_NON_LFS; | |
109 | s->dq_op = sb_dquot_ops; | |
110 | s->s_qcop = sb_quotactl_ops; | |
111 | s->s_op = &default_op; | |
112 | s->s_time_gran = 1000000000; | |
113 | } | |
114 | out: | |
115 | return s; | |
116 | } | |
117 | ||
118 | /** | |
119 | * destroy_super - frees a superblock | |
120 | * @s: superblock to free | |
121 | * | |
122 | * Frees a superblock. | |
123 | */ | |
124 | static inline void destroy_super(struct super_block *s) | |
125 | { | |
126 | security_sb_free(s); | |
79c0b2df | 127 | kfree(s->s_subtype); |
b3b304a2 | 128 | kfree(s->s_options); |
1da177e4 LT |
129 | kfree(s); |
130 | } | |
131 | ||
132 | /* Superblock refcounting */ | |
133 | ||
134 | /* | |
135 | * Drop a superblock's refcount. Returns non-zero if the superblock was | |
136 | * destroyed. The caller must hold sb_lock. | |
137 | */ | |
6b09ae66 | 138 | static int __put_super(struct super_block *sb) |
1da177e4 LT |
139 | { |
140 | int ret = 0; | |
141 | ||
142 | if (!--sb->s_count) { | |
143 | destroy_super(sb); | |
144 | ret = 1; | |
145 | } | |
146 | return ret; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Drop a superblock's refcount. | |
151 | * Returns non-zero if the superblock is about to be destroyed and | |
152 | * at least is already removed from super_blocks list, so if we are | |
153 | * making a loop through super blocks then we need to restart. | |
154 | * The caller must hold sb_lock. | |
155 | */ | |
156 | int __put_super_and_need_restart(struct super_block *sb) | |
157 | { | |
158 | /* check for race with generic_shutdown_super() */ | |
159 | if (list_empty(&sb->s_list)) { | |
160 | /* super block is removed, need to restart... */ | |
161 | __put_super(sb); | |
162 | return 1; | |
163 | } | |
164 | /* can't be the last, since s_list is still in use */ | |
165 | sb->s_count--; | |
166 | BUG_ON(sb->s_count == 0); | |
167 | return 0; | |
168 | } | |
169 | ||
170 | /** | |
171 | * put_super - drop a temporary reference to superblock | |
172 | * @sb: superblock in question | |
173 | * | |
174 | * Drops a temporary reference, frees superblock if there's no | |
175 | * references left. | |
176 | */ | |
177 | static void put_super(struct super_block *sb) | |
178 | { | |
179 | spin_lock(&sb_lock); | |
180 | __put_super(sb); | |
181 | spin_unlock(&sb_lock); | |
182 | } | |
183 | ||
184 | ||
185 | /** | |
186 | * deactivate_super - drop an active reference to superblock | |
187 | * @s: superblock to deactivate | |
188 | * | |
189 | * Drops an active reference to superblock, acquiring a temprory one if | |
190 | * there is no active references left. In that case we lock superblock, | |
191 | * tell fs driver to shut it down and drop the temporary reference we | |
192 | * had just acquired. | |
193 | */ | |
194 | void deactivate_super(struct super_block *s) | |
195 | { | |
196 | struct file_system_type *fs = s->s_type; | |
197 | if (atomic_dec_and_lock(&s->s_active, &sb_lock)) { | |
198 | s->s_count -= S_BIAS-1; | |
199 | spin_unlock(&sb_lock); | |
9e3509e2 | 200 | vfs_dq_off(s, 0); |
1da177e4 LT |
201 | down_write(&s->s_umount); |
202 | fs->kill_sb(s); | |
203 | put_filesystem(fs); | |
204 | put_super(s); | |
205 | } | |
206 | } | |
207 | ||
208 | EXPORT_SYMBOL(deactivate_super); | |
209 | ||
210 | /** | |
211 | * grab_super - acquire an active reference | |
212 | * @s: reference we are trying to make active | |
213 | * | |
214 | * Tries to acquire an active reference. grab_super() is used when we | |
215 | * had just found a superblock in super_blocks or fs_type->fs_supers | |
216 | * and want to turn it into a full-blown active reference. grab_super() | |
217 | * is called with sb_lock held and drops it. Returns 1 in case of | |
218 | * success, 0 if we had failed (superblock contents was already dead or | |
219 | * dying when grab_super() had been called). | |
220 | */ | |
9c4dbee7 | 221 | static int grab_super(struct super_block *s) __releases(sb_lock) |
1da177e4 LT |
222 | { |
223 | s->s_count++; | |
224 | spin_unlock(&sb_lock); | |
225 | down_write(&s->s_umount); | |
226 | if (s->s_root) { | |
227 | spin_lock(&sb_lock); | |
228 | if (s->s_count > S_BIAS) { | |
229 | atomic_inc(&s->s_active); | |
230 | s->s_count--; | |
231 | spin_unlock(&sb_lock); | |
232 | return 1; | |
233 | } | |
234 | spin_unlock(&sb_lock); | |
235 | } | |
236 | up_write(&s->s_umount); | |
237 | put_super(s); | |
238 | yield(); | |
239 | return 0; | |
240 | } | |
241 | ||
914e2637 AV |
242 | /* |
243 | * Superblock locking. We really ought to get rid of these two. | |
244 | */ | |
245 | void lock_super(struct super_block * sb) | |
246 | { | |
247 | get_fs_excl(); | |
248 | mutex_lock(&sb->s_lock); | |
249 | } | |
250 | ||
251 | void unlock_super(struct super_block * sb) | |
252 | { | |
253 | put_fs_excl(); | |
254 | mutex_unlock(&sb->s_lock); | |
255 | } | |
256 | ||
257 | EXPORT_SYMBOL(lock_super); | |
258 | EXPORT_SYMBOL(unlock_super); | |
259 | ||
cf9a2ae8 DH |
260 | /* |
261 | * Write out and wait upon all dirty data associated with this | |
262 | * superblock. Filesystem data as well as the underlying block | |
263 | * device. Takes the superblock lock. Requires a second blkdev | |
264 | * flush by the caller to complete the operation. | |
265 | */ | |
266 | void __fsync_super(struct super_block *sb) | |
267 | { | |
268 | sync_inodes_sb(sb, 0); | |
9e3509e2 | 269 | vfs_dq_sync(sb); |
cf9a2ae8 DH |
270 | lock_super(sb); |
271 | if (sb->s_dirt && sb->s_op->write_super) | |
272 | sb->s_op->write_super(sb); | |
273 | unlock_super(sb); | |
274 | if (sb->s_op->sync_fs) | |
275 | sb->s_op->sync_fs(sb, 1); | |
276 | sync_blockdev(sb->s_bdev); | |
277 | sync_inodes_sb(sb, 1); | |
278 | } | |
279 | ||
280 | /* | |
281 | * Write out and wait upon all dirty data associated with this | |
282 | * superblock. Filesystem data as well as the underlying block | |
283 | * device. Takes the superblock lock. | |
284 | */ | |
285 | int fsync_super(struct super_block *sb) | |
286 | { | |
287 | __fsync_super(sb); | |
288 | return sync_blockdev(sb->s_bdev); | |
289 | } | |
290 | ||
1da177e4 LT |
291 | /** |
292 | * generic_shutdown_super - common helper for ->kill_sb() | |
293 | * @sb: superblock to kill | |
294 | * | |
295 | * generic_shutdown_super() does all fs-independent work on superblock | |
296 | * shutdown. Typical ->kill_sb() should pick all fs-specific objects | |
297 | * that need destruction out of superblock, call generic_shutdown_super() | |
298 | * and release aforementioned objects. Note: dentries and inodes _are_ | |
299 | * taken care of and do not need specific handling. | |
c636ebdb DH |
300 | * |
301 | * Upon calling this function, the filesystem may no longer alter or | |
302 | * rearrange the set of dentries belonging to this super_block, nor may it | |
303 | * change the attachments of dentries to inodes. | |
1da177e4 LT |
304 | */ |
305 | void generic_shutdown_super(struct super_block *sb) | |
306 | { | |
ee9b6d61 | 307 | const struct super_operations *sop = sb->s_op; |
1da177e4 | 308 | |
efaee192 | 309 | |
c636ebdb DH |
310 | if (sb->s_root) { |
311 | shrink_dcache_for_umount(sb); | |
1da177e4 LT |
312 | fsync_super(sb); |
313 | lock_super(sb); | |
314 | sb->s_flags &= ~MS_ACTIVE; | |
efaee192 AV |
315 | |
316 | /* | |
317 | * wait for asynchronous fs operations to finish before going further | |
318 | */ | |
766ccb9e | 319 | async_synchronize_full_domain(&sb->s_async_list); |
efaee192 | 320 | |
1da177e4 LT |
321 | /* bad name - it should be evict_inodes() */ |
322 | invalidate_inodes(sb); | |
323 | lock_kernel(); | |
324 | ||
325 | if (sop->write_super && sb->s_dirt) | |
326 | sop->write_super(sb); | |
327 | if (sop->put_super) | |
328 | sop->put_super(sb); | |
329 | ||
330 | /* Forget any remaining inodes */ | |
331 | if (invalidate_inodes(sb)) { | |
7b4fe29e DJ |
332 | printk("VFS: Busy inodes after unmount of %s. " |
333 | "Self-destruct in 5 seconds. Have a nice day...\n", | |
334 | sb->s_id); | |
1da177e4 LT |
335 | } |
336 | ||
337 | unlock_kernel(); | |
338 | unlock_super(sb); | |
339 | } | |
340 | spin_lock(&sb_lock); | |
341 | /* should be initialized for __put_super_and_need_restart() */ | |
342 | list_del_init(&sb->s_list); | |
343 | list_del(&sb->s_instances); | |
344 | spin_unlock(&sb_lock); | |
345 | up_write(&sb->s_umount); | |
346 | } | |
347 | ||
348 | EXPORT_SYMBOL(generic_shutdown_super); | |
349 | ||
350 | /** | |
351 | * sget - find or create a superblock | |
352 | * @type: filesystem type superblock should belong to | |
353 | * @test: comparison callback | |
354 | * @set: setup callback | |
355 | * @data: argument to each of them | |
356 | */ | |
357 | struct super_block *sget(struct file_system_type *type, | |
358 | int (*test)(struct super_block *,void *), | |
359 | int (*set)(struct super_block *,void *), | |
360 | void *data) | |
361 | { | |
362 | struct super_block *s = NULL; | |
d4730127 | 363 | struct super_block *old; |
1da177e4 LT |
364 | int err; |
365 | ||
366 | retry: | |
367 | spin_lock(&sb_lock); | |
d4730127 MK |
368 | if (test) { |
369 | list_for_each_entry(old, &type->fs_supers, s_instances) { | |
370 | if (!test(old, data)) | |
371 | continue; | |
372 | if (!grab_super(old)) | |
373 | goto retry; | |
a3cfbb53 LZ |
374 | if (s) { |
375 | up_write(&s->s_umount); | |
d4730127 | 376 | destroy_super(s); |
a3cfbb53 | 377 | } |
d4730127 MK |
378 | return old; |
379 | } | |
1da177e4 LT |
380 | } |
381 | if (!s) { | |
382 | spin_unlock(&sb_lock); | |
cf516249 | 383 | s = alloc_super(type); |
1da177e4 LT |
384 | if (!s) |
385 | return ERR_PTR(-ENOMEM); | |
386 | goto retry; | |
387 | } | |
388 | ||
389 | err = set(s, data); | |
390 | if (err) { | |
391 | spin_unlock(&sb_lock); | |
a3cfbb53 | 392 | up_write(&s->s_umount); |
1da177e4 LT |
393 | destroy_super(s); |
394 | return ERR_PTR(err); | |
395 | } | |
396 | s->s_type = type; | |
397 | strlcpy(s->s_id, type->name, sizeof(s->s_id)); | |
398 | list_add_tail(&s->s_list, &super_blocks); | |
399 | list_add(&s->s_instances, &type->fs_supers); | |
400 | spin_unlock(&sb_lock); | |
401 | get_filesystem(type); | |
402 | return s; | |
403 | } | |
404 | ||
405 | EXPORT_SYMBOL(sget); | |
406 | ||
407 | void drop_super(struct super_block *sb) | |
408 | { | |
409 | up_read(&sb->s_umount); | |
410 | put_super(sb); | |
411 | } | |
412 | ||
413 | EXPORT_SYMBOL(drop_super); | |
414 | ||
415 | static inline void write_super(struct super_block *sb) | |
416 | { | |
417 | lock_super(sb); | |
418 | if (sb->s_root && sb->s_dirt) | |
419 | if (sb->s_op->write_super) | |
420 | sb->s_op->write_super(sb); | |
421 | unlock_super(sb); | |
422 | } | |
423 | ||
424 | /* | |
425 | * Note: check the dirty flag before waiting, so we don't | |
426 | * hold up the sync while mounting a device. (The newly | |
427 | * mounted device won't need syncing.) | |
428 | */ | |
429 | void sync_supers(void) | |
430 | { | |
618f0636 KK |
431 | struct super_block *sb; |
432 | ||
1da177e4 | 433 | spin_lock(&sb_lock); |
618f0636 KK |
434 | restart: |
435 | list_for_each_entry(sb, &super_blocks, s_list) { | |
1da177e4 LT |
436 | if (sb->s_dirt) { |
437 | sb->s_count++; | |
438 | spin_unlock(&sb_lock); | |
439 | down_read(&sb->s_umount); | |
440 | write_super(sb); | |
618f0636 KK |
441 | up_read(&sb->s_umount); |
442 | spin_lock(&sb_lock); | |
443 | if (__put_super_and_need_restart(sb)) | |
444 | goto restart; | |
445 | } | |
446 | } | |
1da177e4 LT |
447 | spin_unlock(&sb_lock); |
448 | } | |
449 | ||
450 | /* | |
3a4fa0a2 | 451 | * Call the ->sync_fs super_op against all filesystems which are r/w and |
1da177e4 LT |
452 | * which implement it. |
453 | * | |
454 | * This operation is careful to avoid the livelock which could easily happen | |
455 | * if two or more filesystems are being continuously dirtied. s_need_sync_fs | |
456 | * is used only here. We set it against all filesystems and then clear it as | |
457 | * we sync them. So redirtied filesystems are skipped. | |
458 | * | |
3a4fa0a2 | 459 | * But if process A is currently running sync_filesystems and then process B |
1da177e4 LT |
460 | * calls sync_filesystems as well, process B will set all the s_need_sync_fs |
461 | * flags again, which will cause process A to resync everything. Fix that with | |
462 | * a local mutex. | |
463 | * | |
464 | * (Fabian) Avoid sync_fs with clean fs & wait mode 0 | |
465 | */ | |
466 | void sync_filesystems(int wait) | |
467 | { | |
468 | struct super_block *sb; | |
353ab6e9 | 469 | static DEFINE_MUTEX(mutex); |
1da177e4 | 470 | |
353ab6e9 | 471 | mutex_lock(&mutex); /* Could be down_interruptible */ |
1da177e4 | 472 | spin_lock(&sb_lock); |
618f0636 | 473 | list_for_each_entry(sb, &super_blocks, s_list) { |
1da177e4 LT |
474 | if (!sb->s_op->sync_fs) |
475 | continue; | |
476 | if (sb->s_flags & MS_RDONLY) | |
477 | continue; | |
478 | sb->s_need_sync_fs = 1; | |
479 | } | |
1da177e4 LT |
480 | |
481 | restart: | |
618f0636 | 482 | list_for_each_entry(sb, &super_blocks, s_list) { |
1da177e4 LT |
483 | if (!sb->s_need_sync_fs) |
484 | continue; | |
485 | sb->s_need_sync_fs = 0; | |
486 | if (sb->s_flags & MS_RDONLY) | |
487 | continue; /* hm. Was remounted r/o meanwhile */ | |
488 | sb->s_count++; | |
489 | spin_unlock(&sb_lock); | |
490 | down_read(&sb->s_umount); | |
766ccb9e | 491 | async_synchronize_full_domain(&sb->s_async_list); |
1da177e4 LT |
492 | if (sb->s_root && (wait || sb->s_dirt)) |
493 | sb->s_op->sync_fs(sb, wait); | |
618f0636 KK |
494 | up_read(&sb->s_umount); |
495 | /* restart only when sb is no longer on the list */ | |
496 | spin_lock(&sb_lock); | |
497 | if (__put_super_and_need_restart(sb)) | |
498 | goto restart; | |
1da177e4 LT |
499 | } |
500 | spin_unlock(&sb_lock); | |
353ab6e9 | 501 | mutex_unlock(&mutex); |
1da177e4 LT |
502 | } |
503 | ||
504 | /** | |
505 | * get_super - get the superblock of a device | |
506 | * @bdev: device to get the superblock for | |
507 | * | |
508 | * Scans the superblock list and finds the superblock of the file system | |
509 | * mounted on the device given. %NULL is returned if no match is found. | |
510 | */ | |
511 | ||
512 | struct super_block * get_super(struct block_device *bdev) | |
513 | { | |
618f0636 KK |
514 | struct super_block *sb; |
515 | ||
1da177e4 LT |
516 | if (!bdev) |
517 | return NULL; | |
618f0636 | 518 | |
1da177e4 | 519 | spin_lock(&sb_lock); |
618f0636 KK |
520 | rescan: |
521 | list_for_each_entry(sb, &super_blocks, s_list) { | |
522 | if (sb->s_bdev == bdev) { | |
523 | sb->s_count++; | |
1da177e4 | 524 | spin_unlock(&sb_lock); |
618f0636 KK |
525 | down_read(&sb->s_umount); |
526 | if (sb->s_root) | |
527 | return sb; | |
528 | up_read(&sb->s_umount); | |
529 | /* restart only when sb is no longer on the list */ | |
530 | spin_lock(&sb_lock); | |
531 | if (__put_super_and_need_restart(sb)) | |
532 | goto rescan; | |
1da177e4 LT |
533 | } |
534 | } | |
535 | spin_unlock(&sb_lock); | |
536 | return NULL; | |
537 | } | |
538 | ||
539 | EXPORT_SYMBOL(get_super); | |
540 | ||
541 | struct super_block * user_get_super(dev_t dev) | |
542 | { | |
618f0636 | 543 | struct super_block *sb; |
1da177e4 | 544 | |
1da177e4 | 545 | spin_lock(&sb_lock); |
618f0636 KK |
546 | rescan: |
547 | list_for_each_entry(sb, &super_blocks, s_list) { | |
548 | if (sb->s_dev == dev) { | |
549 | sb->s_count++; | |
1da177e4 | 550 | spin_unlock(&sb_lock); |
618f0636 KK |
551 | down_read(&sb->s_umount); |
552 | if (sb->s_root) | |
553 | return sb; | |
554 | up_read(&sb->s_umount); | |
555 | /* restart only when sb is no longer on the list */ | |
556 | spin_lock(&sb_lock); | |
557 | if (__put_super_and_need_restart(sb)) | |
558 | goto rescan; | |
1da177e4 LT |
559 | } |
560 | } | |
561 | spin_unlock(&sb_lock); | |
562 | return NULL; | |
563 | } | |
564 | ||
257ac264 | 565 | SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf) |
1da177e4 LT |
566 | { |
567 | struct super_block *s; | |
568 | struct ustat tmp; | |
569 | struct kstatfs sbuf; | |
570 | int err = -EINVAL; | |
571 | ||
572 | s = user_get_super(new_decode_dev(dev)); | |
573 | if (s == NULL) | |
574 | goto out; | |
726c3342 | 575 | err = vfs_statfs(s->s_root, &sbuf); |
1da177e4 LT |
576 | drop_super(s); |
577 | if (err) | |
578 | goto out; | |
579 | ||
580 | memset(&tmp,0,sizeof(struct ustat)); | |
581 | tmp.f_tfree = sbuf.f_bfree; | |
582 | tmp.f_tinode = sbuf.f_ffree; | |
583 | ||
584 | err = copy_to_user(ubuf,&tmp,sizeof(struct ustat)) ? -EFAULT : 0; | |
585 | out: | |
586 | return err; | |
587 | } | |
588 | ||
589 | /** | |
a6b91919 | 590 | * mark_files_ro - mark all files read-only |
1da177e4 LT |
591 | * @sb: superblock in question |
592 | * | |
a6b91919 RD |
593 | * All files are marked read-only. We don't care about pending |
594 | * delete files so this should be used in 'force' mode only. | |
1da177e4 LT |
595 | */ |
596 | ||
597 | static void mark_files_ro(struct super_block *sb) | |
598 | { | |
599 | struct file *f; | |
600 | ||
49e0d02c | 601 | retry: |
1da177e4 | 602 | file_list_lock(); |
2f512016 | 603 | list_for_each_entry(f, &sb->s_files, f_u.fu_list) { |
49e0d02c DH |
604 | struct vfsmount *mnt; |
605 | if (!S_ISREG(f->f_path.dentry->d_inode->i_mode)) | |
606 | continue; | |
607 | if (!file_count(f)) | |
608 | continue; | |
609 | if (!(f->f_mode & FMODE_WRITE)) | |
610 | continue; | |
611 | f->f_mode &= ~FMODE_WRITE; | |
ad775f5a DH |
612 | if (file_check_writeable(f) != 0) |
613 | continue; | |
614 | file_release_write(f); | |
49e0d02c DH |
615 | mnt = mntget(f->f_path.mnt); |
616 | file_list_unlock(); | |
617 | /* | |
618 | * This can sleep, so we can't hold | |
619 | * the file_list_lock() spinlock. | |
620 | */ | |
621 | mnt_drop_write(mnt); | |
622 | mntput(mnt); | |
623 | goto retry; | |
1da177e4 LT |
624 | } |
625 | file_list_unlock(); | |
626 | } | |
627 | ||
628 | /** | |
629 | * do_remount_sb - asks filesystem to change mount options. | |
630 | * @sb: superblock in question | |
631 | * @flags: numeric part of options | |
632 | * @data: the rest of options | |
633 | * @force: whether or not to force the change | |
634 | * | |
635 | * Alters the mount options of a mounted file system. | |
636 | */ | |
637 | int do_remount_sb(struct super_block *sb, int flags, void *data, int force) | |
638 | { | |
639 | int retval; | |
0ff5af83 | 640 | int remount_rw; |
1da177e4 | 641 | |
9361401e | 642 | #ifdef CONFIG_BLOCK |
1da177e4 LT |
643 | if (!(flags & MS_RDONLY) && bdev_read_only(sb->s_bdev)) |
644 | return -EACCES; | |
9361401e | 645 | #endif |
1da177e4 LT |
646 | if (flags & MS_RDONLY) |
647 | acct_auto_close(sb); | |
648 | shrink_dcache_sb(sb); | |
649 | fsync_super(sb); | |
650 | ||
651 | /* If we are remounting RDONLY and current sb is read/write, | |
652 | make sure there are no rw files opened */ | |
653 | if ((flags & MS_RDONLY) && !(sb->s_flags & MS_RDONLY)) { | |
654 | if (force) | |
655 | mark_files_ro(sb); | |
656 | else if (!fs_may_remount_ro(sb)) | |
657 | return -EBUSY; | |
9e3509e2 | 658 | retval = vfs_dq_off(sb, 1); |
0ff5af83 JK |
659 | if (retval < 0 && retval != -ENOSYS) |
660 | return -EBUSY; | |
1da177e4 | 661 | } |
0ff5af83 | 662 | remount_rw = !(flags & MS_RDONLY) && (sb->s_flags & MS_RDONLY); |
1da177e4 LT |
663 | |
664 | if (sb->s_op->remount_fs) { | |
665 | lock_super(sb); | |
666 | retval = sb->s_op->remount_fs(sb, &flags, data); | |
667 | unlock_super(sb); | |
668 | if (retval) | |
669 | return retval; | |
670 | } | |
671 | sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK); | |
0ff5af83 | 672 | if (remount_rw) |
9e3509e2 | 673 | vfs_dq_quota_on_remount(sb); |
1da177e4 LT |
674 | return 0; |
675 | } | |
676 | ||
a2a9537a | 677 | static void do_emergency_remount(struct work_struct *work) |
1da177e4 LT |
678 | { |
679 | struct super_block *sb; | |
680 | ||
681 | spin_lock(&sb_lock); | |
682 | list_for_each_entry(sb, &super_blocks, s_list) { | |
683 | sb->s_count++; | |
684 | spin_unlock(&sb_lock); | |
685 | down_read(&sb->s_umount); | |
686 | if (sb->s_root && sb->s_bdev && !(sb->s_flags & MS_RDONLY)) { | |
687 | /* | |
688 | * ->remount_fs needs lock_kernel(). | |
689 | * | |
690 | * What lock protects sb->s_flags?? | |
691 | */ | |
692 | lock_kernel(); | |
693 | do_remount_sb(sb, MS_RDONLY, NULL, 1); | |
694 | unlock_kernel(); | |
695 | } | |
696 | drop_super(sb); | |
697 | spin_lock(&sb_lock); | |
698 | } | |
699 | spin_unlock(&sb_lock); | |
a2a9537a | 700 | kfree(work); |
1da177e4 LT |
701 | printk("Emergency Remount complete\n"); |
702 | } | |
703 | ||
704 | void emergency_remount(void) | |
705 | { | |
a2a9537a JA |
706 | struct work_struct *work; |
707 | ||
708 | work = kmalloc(sizeof(*work), GFP_ATOMIC); | |
709 | if (work) { | |
710 | INIT_WORK(work, do_emergency_remount); | |
711 | schedule_work(work); | |
712 | } | |
1da177e4 LT |
713 | } |
714 | ||
715 | /* | |
716 | * Unnamed block devices are dummy devices used by virtual | |
717 | * filesystems which don't use real block-devices. -- jrs | |
718 | */ | |
719 | ||
ad76cbc6 | 720 | static DEFINE_IDA(unnamed_dev_ida); |
1da177e4 LT |
721 | static DEFINE_SPINLOCK(unnamed_dev_lock);/* protects the above */ |
722 | ||
723 | int set_anon_super(struct super_block *s, void *data) | |
724 | { | |
725 | int dev; | |
726 | int error; | |
727 | ||
728 | retry: | |
ad76cbc6 | 729 | if (ida_pre_get(&unnamed_dev_ida, GFP_ATOMIC) == 0) |
1da177e4 LT |
730 | return -ENOMEM; |
731 | spin_lock(&unnamed_dev_lock); | |
ad76cbc6 | 732 | error = ida_get_new(&unnamed_dev_ida, &dev); |
1da177e4 LT |
733 | spin_unlock(&unnamed_dev_lock); |
734 | if (error == -EAGAIN) | |
735 | /* We raced and lost with another CPU. */ | |
736 | goto retry; | |
737 | else if (error) | |
738 | return -EAGAIN; | |
739 | ||
740 | if ((dev & MAX_ID_MASK) == (1 << MINORBITS)) { | |
741 | spin_lock(&unnamed_dev_lock); | |
ad76cbc6 | 742 | ida_remove(&unnamed_dev_ida, dev); |
1da177e4 LT |
743 | spin_unlock(&unnamed_dev_lock); |
744 | return -EMFILE; | |
745 | } | |
746 | s->s_dev = MKDEV(0, dev & MINORMASK); | |
747 | return 0; | |
748 | } | |
749 | ||
750 | EXPORT_SYMBOL(set_anon_super); | |
751 | ||
752 | void kill_anon_super(struct super_block *sb) | |
753 | { | |
754 | int slot = MINOR(sb->s_dev); | |
755 | ||
756 | generic_shutdown_super(sb); | |
757 | spin_lock(&unnamed_dev_lock); | |
ad76cbc6 | 758 | ida_remove(&unnamed_dev_ida, slot); |
1da177e4 LT |
759 | spin_unlock(&unnamed_dev_lock); |
760 | } | |
761 | ||
762 | EXPORT_SYMBOL(kill_anon_super); | |
763 | ||
1da177e4 LT |
764 | void kill_litter_super(struct super_block *sb) |
765 | { | |
766 | if (sb->s_root) | |
767 | d_genocide(sb->s_root); | |
768 | kill_anon_super(sb); | |
769 | } | |
770 | ||
771 | EXPORT_SYMBOL(kill_litter_super); | |
772 | ||
9361401e | 773 | #ifdef CONFIG_BLOCK |
1da177e4 LT |
774 | static int set_bdev_super(struct super_block *s, void *data) |
775 | { | |
776 | s->s_bdev = data; | |
777 | s->s_dev = s->s_bdev->bd_dev; | |
778 | return 0; | |
779 | } | |
780 | ||
781 | static int test_bdev_super(struct super_block *s, void *data) | |
782 | { | |
783 | return (void *)s->s_bdev == data; | |
784 | } | |
785 | ||
454e2398 | 786 | int get_sb_bdev(struct file_system_type *fs_type, |
1da177e4 | 787 | int flags, const char *dev_name, void *data, |
454e2398 DH |
788 | int (*fill_super)(struct super_block *, void *, int), |
789 | struct vfsmount *mnt) | |
1da177e4 LT |
790 | { |
791 | struct block_device *bdev; | |
792 | struct super_block *s; | |
30c40d2c | 793 | fmode_t mode = FMODE_READ; |
1da177e4 LT |
794 | int error = 0; |
795 | ||
30c40d2c AV |
796 | if (!(flags & MS_RDONLY)) |
797 | mode |= FMODE_WRITE; | |
798 | ||
799 | bdev = open_bdev_exclusive(dev_name, mode, fs_type); | |
1da177e4 | 800 | if (IS_ERR(bdev)) |
454e2398 | 801 | return PTR_ERR(bdev); |
1da177e4 LT |
802 | |
803 | /* | |
804 | * once the super is inserted into the list by sget, s_umount | |
805 | * will protect the lockfs code from trying to start a snapshot | |
806 | * while we are mounting | |
807 | */ | |
f73ca1b7 | 808 | down(&bdev->bd_mount_sem); |
1da177e4 | 809 | s = sget(fs_type, test_bdev_super, set_bdev_super, bdev); |
f73ca1b7 | 810 | up(&bdev->bd_mount_sem); |
1da177e4 | 811 | if (IS_ERR(s)) |
454e2398 | 812 | goto error_s; |
1da177e4 LT |
813 | |
814 | if (s->s_root) { | |
815 | if ((flags ^ s->s_flags) & MS_RDONLY) { | |
816 | up_write(&s->s_umount); | |
817 | deactivate_super(s); | |
454e2398 DH |
818 | error = -EBUSY; |
819 | goto error_bdev; | |
1da177e4 | 820 | } |
454e2398 | 821 | |
30c40d2c | 822 | close_bdev_exclusive(bdev, mode); |
1da177e4 LT |
823 | } else { |
824 | char b[BDEVNAME_SIZE]; | |
825 | ||
826 | s->s_flags = flags; | |
30c40d2c | 827 | s->s_mode = mode; |
1da177e4 | 828 | strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); |
e78c9a00 | 829 | sb_set_blocksize(s, block_size(bdev)); |
9b04c997 | 830 | error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); |
1da177e4 LT |
831 | if (error) { |
832 | up_write(&s->s_umount); | |
833 | deactivate_super(s); | |
454e2398 | 834 | goto error; |
fa675765 | 835 | } |
454e2398 DH |
836 | |
837 | s->s_flags |= MS_ACTIVE; | |
87d8fe1e | 838 | bdev->bd_super = s; |
1da177e4 LT |
839 | } |
840 | ||
a3ec947c SB |
841 | simple_set_mnt(mnt, s); |
842 | return 0; | |
1da177e4 | 843 | |
454e2398 DH |
844 | error_s: |
845 | error = PTR_ERR(s); | |
846 | error_bdev: | |
30c40d2c | 847 | close_bdev_exclusive(bdev, mode); |
454e2398 DH |
848 | error: |
849 | return error; | |
1da177e4 LT |
850 | } |
851 | ||
852 | EXPORT_SYMBOL(get_sb_bdev); | |
853 | ||
854 | void kill_block_super(struct super_block *sb) | |
855 | { | |
856 | struct block_device *bdev = sb->s_bdev; | |
30c40d2c | 857 | fmode_t mode = sb->s_mode; |
1da177e4 | 858 | |
87d8fe1e | 859 | bdev->bd_super = 0; |
1da177e4 LT |
860 | generic_shutdown_super(sb); |
861 | sync_blockdev(bdev); | |
30c40d2c | 862 | close_bdev_exclusive(bdev, mode); |
1da177e4 LT |
863 | } |
864 | ||
865 | EXPORT_SYMBOL(kill_block_super); | |
9361401e | 866 | #endif |
1da177e4 | 867 | |
454e2398 | 868 | int get_sb_nodev(struct file_system_type *fs_type, |
1da177e4 | 869 | int flags, void *data, |
454e2398 DH |
870 | int (*fill_super)(struct super_block *, void *, int), |
871 | struct vfsmount *mnt) | |
1da177e4 LT |
872 | { |
873 | int error; | |
874 | struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL); | |
875 | ||
876 | if (IS_ERR(s)) | |
454e2398 | 877 | return PTR_ERR(s); |
1da177e4 LT |
878 | |
879 | s->s_flags = flags; | |
880 | ||
9b04c997 | 881 | error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); |
1da177e4 LT |
882 | if (error) { |
883 | up_write(&s->s_umount); | |
884 | deactivate_super(s); | |
454e2398 | 885 | return error; |
1da177e4 LT |
886 | } |
887 | s->s_flags |= MS_ACTIVE; | |
a3ec947c SB |
888 | simple_set_mnt(mnt, s); |
889 | return 0; | |
1da177e4 LT |
890 | } |
891 | ||
892 | EXPORT_SYMBOL(get_sb_nodev); | |
893 | ||
894 | static int compare_single(struct super_block *s, void *p) | |
895 | { | |
896 | return 1; | |
897 | } | |
898 | ||
454e2398 | 899 | int get_sb_single(struct file_system_type *fs_type, |
1da177e4 | 900 | int flags, void *data, |
454e2398 DH |
901 | int (*fill_super)(struct super_block *, void *, int), |
902 | struct vfsmount *mnt) | |
1da177e4 LT |
903 | { |
904 | struct super_block *s; | |
905 | int error; | |
906 | ||
907 | s = sget(fs_type, compare_single, set_anon_super, NULL); | |
908 | if (IS_ERR(s)) | |
454e2398 | 909 | return PTR_ERR(s); |
1da177e4 LT |
910 | if (!s->s_root) { |
911 | s->s_flags = flags; | |
9b04c997 | 912 | error = fill_super(s, data, flags & MS_SILENT ? 1 : 0); |
1da177e4 LT |
913 | if (error) { |
914 | up_write(&s->s_umount); | |
915 | deactivate_super(s); | |
454e2398 | 916 | return error; |
1da177e4 LT |
917 | } |
918 | s->s_flags |= MS_ACTIVE; | |
919 | } | |
920 | do_remount_sb(s, flags, data, 0); | |
a3ec947c SB |
921 | simple_set_mnt(mnt, s); |
922 | return 0; | |
1da177e4 LT |
923 | } |
924 | ||
925 | EXPORT_SYMBOL(get_sb_single); | |
926 | ||
927 | struct vfsmount * | |
bb4a58bf | 928 | vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) |
1da177e4 | 929 | { |
1da177e4 | 930 | struct vfsmount *mnt; |
1da177e4 | 931 | char *secdata = NULL; |
454e2398 | 932 | int error; |
1da177e4 LT |
933 | |
934 | if (!type) | |
935 | return ERR_PTR(-ENODEV); | |
1da177e4 | 936 | |
454e2398 | 937 | error = -ENOMEM; |
1da177e4 LT |
938 | mnt = alloc_vfsmnt(name); |
939 | if (!mnt) | |
940 | goto out; | |
941 | ||
e0007529 | 942 | if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) { |
1da177e4 | 943 | secdata = alloc_secdata(); |
454e2398 | 944 | if (!secdata) |
1da177e4 | 945 | goto out_mnt; |
1da177e4 | 946 | |
e0007529 | 947 | error = security_sb_copy_data(data, secdata); |
454e2398 | 948 | if (error) |
1da177e4 | 949 | goto out_free_secdata; |
1da177e4 LT |
950 | } |
951 | ||
454e2398 DH |
952 | error = type->get_sb(type, flags, name, data, mnt); |
953 | if (error < 0) | |
1da177e4 | 954 | goto out_free_secdata; |
b4c07bce | 955 | BUG_ON(!mnt->mnt_sb); |
454e2398 | 956 | |
12204e24 | 957 | error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata); |
1da177e4 LT |
958 | if (error) |
959 | goto out_sb; | |
454e2398 DH |
960 | |
961 | mnt->mnt_mountpoint = mnt->mnt_root; | |
1da177e4 | 962 | mnt->mnt_parent = mnt; |
454e2398 | 963 | up_write(&mnt->mnt_sb->s_umount); |
8680e22f | 964 | free_secdata(secdata); |
1da177e4 LT |
965 | return mnt; |
966 | out_sb: | |
454e2398 DH |
967 | dput(mnt->mnt_root); |
968 | up_write(&mnt->mnt_sb->s_umount); | |
969 | deactivate_super(mnt->mnt_sb); | |
1da177e4 LT |
970 | out_free_secdata: |
971 | free_secdata(secdata); | |
972 | out_mnt: | |
973 | free_vfsmnt(mnt); | |
974 | out: | |
454e2398 | 975 | return ERR_PTR(error); |
1da177e4 LT |
976 | } |
977 | ||
bb4a58bf TM |
978 | EXPORT_SYMBOL_GPL(vfs_kern_mount); |
979 | ||
79c0b2df MS |
980 | static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) |
981 | { | |
982 | int err; | |
983 | const char *subtype = strchr(fstype, '.'); | |
984 | if (subtype) { | |
985 | subtype++; | |
986 | err = -EINVAL; | |
987 | if (!subtype[0]) | |
988 | goto err; | |
989 | } else | |
990 | subtype = ""; | |
991 | ||
992 | mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); | |
993 | err = -ENOMEM; | |
994 | if (!mnt->mnt_sb->s_subtype) | |
995 | goto err; | |
996 | return mnt; | |
997 | ||
998 | err: | |
999 | mntput(mnt); | |
1000 | return ERR_PTR(err); | |
1001 | } | |
1002 | ||
bb4a58bf TM |
1003 | struct vfsmount * |
1004 | do_kern_mount(const char *fstype, int flags, const char *name, void *data) | |
1005 | { | |
1006 | struct file_system_type *type = get_fs_type(fstype); | |
1007 | struct vfsmount *mnt; | |
1008 | if (!type) | |
1009 | return ERR_PTR(-ENODEV); | |
1010 | mnt = vfs_kern_mount(type, flags, name, data); | |
79c0b2df MS |
1011 | if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && |
1012 | !mnt->mnt_sb->s_subtype) | |
1013 | mnt = fs_set_subtype(mnt, fstype); | |
bb4a58bf TM |
1014 | put_filesystem(type); |
1015 | return mnt; | |
1016 | } | |
8a4e98d9 | 1017 | EXPORT_SYMBOL_GPL(do_kern_mount); |
bb4a58bf | 1018 | |
8bf9725c | 1019 | struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) |
1da177e4 | 1020 | { |
8bf9725c | 1021 | return vfs_kern_mount(type, MS_KERNMOUNT, type->name, data); |
1da177e4 LT |
1022 | } |
1023 | ||
8bf9725c | 1024 | EXPORT_SYMBOL_GPL(kern_mount_data); |