vfs_trans.c revision 1.5.8.7 1 /* $NetBSD: vfs_trans.c,v 1.5.8.7 2007/10/09 15:22:23 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juergen Hannken-Illjes.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.5.8.7 2007/10/09 15:22:23 ad Exp $");
41
42 /*
43 * File system transaction operations.
44 */
45
46 #include "opt_ddb.h"
47
48 #if defined(DDB)
49 #define _LWP_API_PRIVATE /* Need _lwp_getspecific_by_lwp() */
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/kmem.h>
56 #include <sys/mount.h>
57 #include <sys/rwlock.h>
58 #include <sys/vnode.h>
59 #define _FSTRANS_API_PRIVATE
60 #include <sys/fstrans.h>
61 #include <sys/proc.h>
62
63 #include <miscfs/specfs/specdev.h>
64 #include <miscfs/syncfs/syncfs.h>
65
66 struct fstrans_lwp_info {
67 struct fstrans_lwp_info *fli_succ;
68 struct mount *fli_mount;
69 int fli_count;
70 enum fstrans_lock_type fli_lock_type;
71 };
72 struct fstrans_mount_info {
73 enum fstrans_state fmi_state;
74 krwlock_t fmi_shared_lock;
75 krwlock_t fmi_lazy_lock;
76 };
77
78 static specificdata_key_t lwp_data_key;
79 static specificdata_key_t mount_cow_key;
80 static kmutex_t vfs_suspend_lock; /* Serialize suspensions. */
81 static kmutex_t fstrans_init_lock;
82
83 POOL_INIT(fstrans_pl, sizeof(struct fstrans_lwp_info), 0, 0, 0,
84 "fstrans", NULL, IPL_NONE);
85
86 static void fstrans_lwp_dtor(void *);
87 static struct fstrans_mount_info *fstrans_mount_init(struct mount *);
88
89 /*
90 * Initialize
91 */
92 void
93 fstrans_init(void)
94 {
95 int error;
96
97 error = lwp_specific_key_create(&lwp_data_key, fstrans_lwp_dtor);
98 KASSERT(error == 0);
99
100 mutex_init(&vfs_suspend_lock, MUTEX_DEFAULT, IPL_NONE);
101 mutex_init(&fstrans_init_lock, MUTEX_DEFAULT, IPL_NONE);
102 }
103
104 /*
105 * Deallocate lwp state
106 */
107 static void
108 fstrans_lwp_dtor(void *arg)
109 {
110 struct fstrans_lwp_info *fli, *fli_next;
111
112 for (fli = arg; fli; fli = fli_next) {
113 KASSERT(fli->fli_mount == NULL);
114 KASSERT(fli->fli_count == 0);
115 fli_next = fli->fli_succ;
116 pool_put(&fstrans_pl, fli);
117 }
118 }
119
120 /*
121 * Deallocate mount state
122 */
123 void
124 fstrans_unmount(struct mount *mp)
125 {
126 struct fstrans_mount_info *fmi = mp->mnt_transinfo;
127
128 KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
129 rw_destroy(&fmi->fmi_lazy_lock);
130 rw_destroy(&fmi->fmi_shared_lock);
131 free(fmi, M_MOUNT);
132 }
133
134 /*
135 * Create mount info for this mount
136 */
137 static struct fstrans_mount_info *
138 fstrans_mount_init(struct mount *mp)
139 {
140 struct fstrans_mount_info *new;
141
142 mutex_enter(&fstrans_init_lock);
143
144 if ((new = mp->mnt_transinfo) != NULL) {
145 mutex_exit(&fstrans_init_lock);
146 return new;
147 }
148
149 new = malloc(sizeof(*new), M_MOUNT, M_WAITOK);
150 new->fmi_state = FSTRANS_NORMAL;
151 rw_init(&new->fmi_lazy_lock);
152 rw_init(&new->fmi_shared_lock);
153
154 mp->mnt_transinfo = new;
155 mutex_exit(&fstrans_init_lock);
156
157 return new;
158 }
159
160 /*
161 * Start a transaction. If this thread already has a transaction on this
162 * file system increment the reference counter.
163 * A thread with an exclusive transaction lock may get a shared or lazy one.
164 * A thread with a shared or lazy transaction lock cannot upgrade to an
165 * exclusive one yet.
166 */
167 int
168 _fstrans_start(struct mount *mp, enum fstrans_lock_type lock_type, int wait)
169 {
170 krwlock_t *lock_p;
171 krw_t lock_op;
172 struct fstrans_lwp_info *fli, *new_fli;
173 struct fstrans_mount_info *fmi;
174
175 ASSERT_SLEEPABLE(NULL, __func__);
176
177 if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
178 return 0;
179
180 new_fli = NULL;
181 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
182 if (fli->fli_mount == NULL && new_fli == NULL)
183 new_fli = fli;
184 if (fli->fli_mount == mp) {
185 KASSERT(fli->fli_count > 0);
186 if (fli->fli_lock_type != FSTRANS_EXCL &&
187 lock_type == FSTRANS_EXCL)
188 panic("fstrans_start: cannot upgrade lock");
189 fli->fli_count += 1;
190 return 0;
191 }
192 }
193
194 if (new_fli == NULL) {
195 new_fli = pool_get(&fstrans_pl, PR_WAITOK);
196 new_fli->fli_mount = NULL;
197 new_fli->fli_count = 0;
198 new_fli->fli_succ = lwp_getspecific(lwp_data_key);
199 lwp_setspecific(lwp_data_key, new_fli);
200 }
201
202 KASSERT(new_fli->fli_mount == NULL);
203 KASSERT(new_fli->fli_count == 0);
204
205 if ((fmi = mp->mnt_transinfo) == NULL)
206 fmi = fstrans_mount_init(mp);
207
208 if (lock_type == FSTRANS_LAZY)
209 lock_p = &fmi->fmi_lazy_lock;
210 else
211 lock_p = &fmi->fmi_shared_lock;
212 lock_op = (lock_type == FSTRANS_EXCL ? RW_WRITER : RW_READER);
213
214 if (wait)
215 rw_enter(lock_p, lock_op);
216 else if (rw_tryenter(lock_p, lock_op) == 0)
217 return EBUSY;
218
219 new_fli->fli_mount = mp;
220 new_fli->fli_count = 1;
221 new_fli->fli_lock_type = lock_type;
222
223 return 0;
224 }
225
226 /*
227 * Finish a transaction.
228 */
229 void
230 fstrans_done(struct mount *mp)
231 {
232 struct fstrans_lwp_info *fli;
233 struct fstrans_mount_info *fmi;
234
235 if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
236 return;
237
238 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
239 if (fli->fli_mount == mp) {
240 fli->fli_count -= 1;
241 if (fli->fli_count > 0)
242 return;
243 break;
244 }
245 }
246
247 KASSERT(fli != NULL);
248 KASSERT(fli->fli_mount == mp);
249 KASSERT(fli->fli_count == 0);
250 fli->fli_mount = NULL;
251 fmi = mp->mnt_transinfo;
252 KASSERT(fmi != NULL);
253 if (fli->fli_lock_type == FSTRANS_LAZY)
254 rw_exit(&fmi->fmi_lazy_lock);
255 else
256 rw_exit(&fmi->fmi_shared_lock);
257 }
258
259 /*
260 * Check if this thread has an exclusive lock.
261 */
262 int
263 fstrans_is_owner(struct mount *mp)
264 {
265 struct fstrans_lwp_info *fli;
266
267 if (mp == NULL)
268 return 0;
269 if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
270 return 0;
271
272 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ)
273 if (fli->fli_mount == mp)
274 break;
275
276 if (fli == NULL)
277 return 0;
278
279 KASSERT(fli->fli_mount == mp);
280 KASSERT(fli->fli_count > 0);
281 return (fli->fli_lock_type == FSTRANS_EXCL);
282 }
283
284 /*
285 * Set new file system state.
286 */
287 int
288 fstrans_setstate(struct mount *mp, enum fstrans_state new_state)
289 {
290 struct fstrans_mount_info *fmi;
291
292 if ((fmi = mp->mnt_transinfo) == NULL)
293 fmi = fstrans_mount_init(mp);
294
295 switch (new_state) {
296 case FSTRANS_SUSPENDING:
297 KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
298 fstrans_start(mp, FSTRANS_EXCL);
299 fmi->fmi_state = FSTRANS_SUSPENDING;
300 break;
301
302 case FSTRANS_SUSPENDED:
303 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
304 fmi->fmi_state == FSTRANS_SUSPENDING);
305 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
306 fstrans_is_owner(mp));
307 if (fmi->fmi_state == FSTRANS_NORMAL)
308 fstrans_start(mp, FSTRANS_EXCL);
309 rw_enter(&fmi->fmi_lazy_lock, RW_WRITER);
310 fmi->fmi_state = FSTRANS_SUSPENDED;
311 break;
312
313 case FSTRANS_NORMAL:
314 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
315 fstrans_is_owner(mp));
316 if (fmi->fmi_state == FSTRANS_SUSPENDED)
317 rw_exit(&fmi->fmi_lazy_lock);
318 if (fmi->fmi_state == FSTRANS_SUSPENDING ||
319 fmi->fmi_state == FSTRANS_SUSPENDED) {
320 fmi->fmi_state = FSTRANS_NORMAL;
321 fstrans_done(mp);
322 }
323 break;
324
325 default:
326 panic("%s: illegal state %d", __func__, new_state);
327 }
328
329 return 0;
330 }
331
332 /*
333 * Get current file system state
334 */
335 enum fstrans_state
336 fstrans_getstate(struct mount *mp)
337 {
338 struct fstrans_mount_info *fmi;
339
340 if ((fmi = mp->mnt_transinfo) == NULL)
341 return FSTRANS_NORMAL;
342
343 return fmi->fmi_state;
344 }
345
346 /*
347 * Request a filesystem to suspend all operations.
348 */
349 int
350 vfs_suspend(struct mount *mp, int nowait)
351 {
352 int error;
353
354 if (nowait) {
355 if (!mutex_tryenter(&vfs_suspend_lock))
356 return EWOULDBLOCK;
357 } else
358 mutex_enter(&vfs_suspend_lock);
359
360 mutex_enter(&syncer_mutex);
361
362 if ((error = VFS_SUSPENDCTL(mp, SUSPEND_SUSPEND)) != 0) {
363 mutex_exit(&syncer_mutex);
364 mutex_exit(&vfs_suspend_lock);
365 }
366
367 return error;
368 }
369
370 /*
371 * Request a filesystem to resume all operations.
372 */
373 void
374 vfs_resume(struct mount *mp)
375 {
376
377 VFS_SUSPENDCTL(mp, SUSPEND_RESUME);
378 mutex_exit(&syncer_mutex);
379 mutex_exit(&vfs_suspend_lock);
380 }
381
382 #if defined(DDB)
383 void fstrans_dump(int);
384
385 static void
386 fstrans_print_lwp(struct proc *p, struct lwp *l, int verbose)
387 {
388 char prefix[9];
389 struct fstrans_lwp_info *fli;
390
391 snprintf(prefix, sizeof(prefix), "%d.%d", p->p_pid, l->l_lid);
392 for (fli = _lwp_getspecific_by_lwp(l, lwp_data_key);
393 fli;
394 fli = fli->fli_succ) {
395 if (!verbose && fli->fli_count == 0)
396 continue;
397 printf("%-8s", prefix);
398 if (verbose)
399 printf(" @%p", fli);
400 if (fli->fli_mount != NULL)
401 printf(" (%s)", fli->fli_mount->mnt_stat.f_mntonname);
402 else
403 printf(" NULL");
404 switch (fli->fli_lock_type) {
405 case FSTRANS_LAZY:
406 printf(" lazy");
407 break;
408 case FSTRANS_SHARED:
409 printf(" shared");
410 break;
411 case FSTRANS_EXCL:
412 printf(" excl");
413 break;
414 default:
415 printf(" %#x", fli->fli_lock_type);
416 break;
417 }
418 printf(" %d\n", fli->fli_count);
419 prefix[0] = '\0';
420 }
421 }
422
423 static void
424 fstrans_print_mount(struct mount *mp, int verbose)
425 {
426 struct fstrans_mount_info *fmi;
427
428 fmi = mp->mnt_transinfo;
429 if (!verbose && (fmi == NULL || fmi->fmi_state == FSTRANS_NORMAL))
430 return;
431
432 printf("%-16s ", mp->mnt_stat.f_mntonname);
433 if (fmi == NULL) {
434 printf("(null)\n");
435 return;
436 }
437 switch (fmi->fmi_state) {
438 case FSTRANS_NORMAL:
439 printf("state normal\n");
440 break;
441 case FSTRANS_SUSPENDING:
442 printf("state suspending\n");
443 break;
444 case FSTRANS_SUSPENDED:
445 printf("state suspended\n");
446 break;
447 default:
448 printf("state %#x\n", fmi->fmi_state);
449 break;
450 }
451 printf("%16s r=%d w=%d\n", "lock_lazy:",
452 rw_read_held(&fmi->fmi_lazy_lock),
453 rw_write_held(&fmi->fmi_lazy_lock));
454 printf("%16s r=%d w=%d\n", "lock_shared:",
455 rw_read_held(&fmi->fmi_shared_lock),
456 rw_write_held(&fmi->fmi_shared_lock));
457 }
458
459 void
460 fstrans_dump(int full)
461 {
462 const struct proclist_desc *pd;
463 struct proc *p;
464 struct lwp *l;
465 struct mount *mp;
466
467 printf("Fstrans locks by lwp:\n");
468 for (pd = proclists; pd->pd_list != NULL; pd++)
469 LIST_FOREACH(p, pd->pd_list, p_list)
470 LIST_FOREACH(l, &p->p_lwps, l_sibling)
471 fstrans_print_lwp(p, l, full == 1);
472
473 printf("Fstrans state by mount:\n");
474 CIRCLEQ_FOREACH(mp, &mountlist, mnt_list)
475 fstrans_print_mount(mp, full == 1);
476 }
477 #endif /* defined(DDB) */
478
479
480 struct fscow_handler {
481 SLIST_ENTRY(fscow_handler) ch_list;
482 int (*ch_func)(void *, struct buf *);
483 void *ch_arg;
484 };
485
486 struct fscow_mount_info {
487 krwlock_t cmi_lock;
488 SLIST_HEAD(, fscow_handler) cmi_handler;
489 };
490
491 /*
492 * Create mount info for this mount
493 */
494 static struct fscow_mount_info *
495 fscow_mount_init(struct mount *mp)
496 {
497 struct fscow_mount_info *new;
498
499 mutex_enter(&fstrans_init_lock);
500
501 if ((new = mount_getspecific(mp, mount_cow_key)) != NULL) {
502 mutex_exit(&fstrans_init_lock);
503 return new;
504 }
505
506 if ((new = kmem_alloc(sizeof(*new), KM_SLEEP)) != NULL) {
507 SLIST_INIT(&new->cmi_handler);
508 rw_init(&new->cmi_lock);
509 mount_setspecific(mp, mount_cow_key, new);
510 }
511
512 mutex_exit(&fstrans_init_lock);
513
514 return new;
515 }
516
517 int
518 fscow_establish(struct mount *mp, int (*func)(void *, struct buf *), void *arg)
519 {
520 struct fscow_mount_info *cmi;
521 struct fscow_handler *new;
522
523 if ((cmi = mount_getspecific(mp, mount_cow_key)) == NULL)
524 cmi = fscow_mount_init(mp);
525 if (cmi == NULL)
526 return ENOMEM;
527
528 if ((new = kmem_alloc(sizeof(*new), KM_SLEEP)) == NULL)
529 return ENOMEM;
530 new->ch_func = func;
531 new->ch_arg = arg;
532 rw_enter(&cmi->cmi_lock, RW_WRITER);
533 SLIST_INSERT_HEAD(&cmi->cmi_handler, new, ch_list);
534 rw_exit(&cmi->cmi_lock);
535
536 return 0;
537 }
538
539 int
540 fscow_disestablish(struct mount *mp, int (*func)(void *, struct buf *),
541 void *arg)
542 {
543 struct fscow_mount_info *cmi;
544 struct fscow_handler *hp = NULL;
545
546 if ((cmi = mount_getspecific(mp, mount_cow_key)) == NULL)
547 return EINVAL;
548
549 rw_enter(&cmi->cmi_lock, RW_WRITER);
550 SLIST_FOREACH(hp, &cmi->cmi_handler, ch_list)
551 if (hp->ch_func == func && hp->ch_arg == arg)
552 break;
553 if (hp != NULL) {
554 SLIST_REMOVE(&cmi->cmi_handler, hp, fscow_handler, ch_list);
555 kmem_free(hp, sizeof(*hp));
556 }
557 rw_exit(&cmi->cmi_lock);
558
559 return hp ? 0 : EINVAL;
560 }
561
562 int
563 fscow_run(struct buf *bp)
564 {
565 int error = 0;
566 struct mount *mp;
567 struct fscow_mount_info *cmi;
568 struct fscow_handler *hp;
569
570 if (bp->b_vp == NULL)
571 return 0;
572 if (bp->b_vp->v_type == VBLK)
573 mp = bp->b_vp->v_specmountpoint;
574 else
575 mp = bp->b_vp->v_mount;
576 if (mp == NULL)
577 return 0;
578
579 if ((cmi = mount_getspecific(mp, mount_cow_key)) == NULL)
580 return 0;
581
582 rw_enter(&cmi->cmi_lock, RW_READER);
583 SLIST_FOREACH(hp, &cmi->cmi_handler, ch_list)
584 if ((error = (*hp->ch_func)(hp->ch_arg, bp)) != 0)
585 break;
586 rw_exit(&cmi->cmi_lock);
587
588 return error;
589 }
590