vfs_trans.c revision 1.8 1 /* $NetBSD: vfs_trans.c,v 1.8 2007/05/16 16:11:56 hannken 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.8 2007/05/16 16:11:56 hannken 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/mount.h>
56 #include <sys/rwlock.h>
57 #include <sys/vnode.h>
58 #define _FSTRANS_API_PRIVATE
59 #include <sys/fstrans.h>
60
61 #include <miscfs/syncfs/syncfs.h>
62
63 struct fstrans_lwp_info {
64 struct fstrans_lwp_info *fli_succ;
65 struct mount *fli_mount;
66 int fli_count;
67 enum fstrans_lock_type fli_lock_type;
68 };
69 struct fstrans_mount_info {
70 enum fstrans_state fmi_state;
71 krwlock_t fmi_shared_lock;
72 krwlock_t fmi_lazy_lock;
73 };
74
75 static specificdata_key_t lwp_data_key;
76 static specificdata_key_t mount_data_key;
77 static kmutex_t vfs_suspend_lock; /* Serialize suspensions. */
78 static kmutex_t fstrans_init_lock;
79
80 POOL_INIT(fstrans_pl, sizeof(struct fstrans_lwp_info), 0, 0, 0,
81 "fstrans", NULL, IPL_NONE);
82
83 static void fstrans_lwp_dtor(void *);
84 static void fstrans_mount_dtor(void *);
85 static struct fstrans_mount_info *fstrans_mount_init(struct mount *);
86
87 /*
88 * Initialize
89 */
90 void
91 fstrans_init(void)
92 {
93 int error;
94
95 error = lwp_specific_key_create(&lwp_data_key, fstrans_lwp_dtor);
96 KASSERT(error == 0);
97 error = mount_specific_key_create(&mount_data_key, fstrans_mount_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 static void
124 fstrans_mount_dtor(void *arg)
125 {
126 struct fstrans_mount_info *fmi = arg;
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 = mount_getspecific(mp, mount_data_key)) != 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 mount_setspecific(mp, mount_data_key, 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 = mount_getspecific(mp, mount_data_key)) == 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 = mount_getspecific(mp, mount_data_key);
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 int error;
291 struct fstrans_mount_info *fmi;
292
293 if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
294 fmi = fstrans_mount_init(mp);
295
296 switch (new_state) {
297 case FSTRANS_SUSPENDING:
298 KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
299 if ((error = fstrans_start(mp, FSTRANS_EXCL)) != 0)
300 return error;
301 fmi->fmi_state = FSTRANS_SUSPENDING;
302 break;
303
304 case FSTRANS_SUSPENDED:
305 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
306 fmi->fmi_state == FSTRANS_SUSPENDING);
307 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
308 fstrans_is_owner(mp));
309 if (fmi->fmi_state == FSTRANS_NORMAL &&
310 (error = fstrans_start(mp, FSTRANS_EXCL)) != 0)
311 return error;
312 rw_enter(&fmi->fmi_lazy_lock, RW_WRITER);
313 fmi->fmi_state = FSTRANS_SUSPENDED;
314 break;
315
316 case FSTRANS_NORMAL:
317 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
318 fstrans_is_owner(mp));
319 if (fmi->fmi_state == FSTRANS_SUSPENDED)
320 rw_exit(&fmi->fmi_lazy_lock);
321 if (fmi->fmi_state == FSTRANS_SUSPENDING ||
322 fmi->fmi_state == FSTRANS_SUSPENDED) {
323 fmi->fmi_state = FSTRANS_NORMAL;
324 fstrans_done(mp);
325 }
326 break;
327
328 default:
329 panic("%s: illegal state %d", __func__, new_state);
330 }
331
332 return 0;
333 }
334
335 /*
336 * Get current file system state
337 */
338 enum fstrans_state
339 fstrans_getstate(struct mount *mp)
340 {
341 struct fstrans_mount_info *fmi;
342
343 if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
344 return FSTRANS_NORMAL;
345
346 return fmi->fmi_state;
347 }
348
349 /*
350 * Request a filesystem to suspend all operations.
351 */
352 int
353 vfs_suspend(struct mount *mp, int nowait)
354 {
355 int error;
356
357 if (nowait) {
358 if (!mutex_tryenter(&vfs_suspend_lock))
359 return EWOULDBLOCK;
360 } else
361 mutex_enter(&vfs_suspend_lock);
362
363 mutex_enter(&syncer_mutex);
364
365 if ((error = VFS_SUSPENDCTL(mp, SUSPEND_SUSPEND)) != 0) {
366 mutex_exit(&syncer_mutex);
367 mutex_exit(&vfs_suspend_lock);
368 }
369
370 return error;
371 }
372
373 /*
374 * Request a filesystem to resume all operations.
375 */
376 void
377 vfs_resume(struct mount *mp)
378 {
379
380 VFS_SUSPENDCTL(mp, SUSPEND_RESUME);
381 mutex_exit(&syncer_mutex);
382 mutex_exit(&vfs_suspend_lock);
383 }
384
385 /*
386 * Default vfs_suspendctl routine for file systems that do not support it.
387 */
388 /*ARGSUSED*/
389 int
390 vfs_stdsuspendctl(struct mount *mp __unused, int mode __unused)
391 {
392 return EOPNOTSUPP;
393 }
394
395 #if defined(DDB)
396 void fstrans_dump(int);
397
398 static void
399 fstrans_print_lwp(struct proc *p, struct lwp *l, int verbose)
400 {
401 char prefix[9];
402 struct fstrans_lwp_info *fli;
403
404 snprintf(prefix, sizeof(prefix), "%d.%d", p->p_pid, l->l_lid);
405 for (fli = _lwp_getspecific_by_lwp(l, lwp_data_key);
406 fli;
407 fli = fli->fli_succ) {
408 if (!verbose && fli->fli_count == 0)
409 continue;
410 printf("%-8s", prefix);
411 if (verbose)
412 printf(" @%p", fli);
413 if (fli->fli_mount != NULL)
414 printf(" (%s)", fli->fli_mount->mnt_stat.f_mntonname);
415 else
416 printf(" NULL");
417 switch (fli->fli_lock_type) {
418 case FSTRANS_LAZY:
419 printf(" lazy");
420 break;
421 case FSTRANS_SHARED:
422 printf(" shared");
423 break;
424 case FSTRANS_EXCL:
425 printf(" excl");
426 break;
427 default:
428 printf(" %#x", fli->fli_lock_type);
429 break;
430 }
431 printf(" %d\n", fli->fli_count);
432 prefix[0] = '\0';
433 }
434 }
435
436 static void
437 fstrans_print_mount(struct mount *mp, int verbose)
438 {
439 struct fstrans_mount_info *fmi;
440
441 fmi = mount_getspecific(mp, mount_data_key);
442 if (!verbose && (fmi == NULL || fmi->fmi_state == FSTRANS_NORMAL))
443 return;
444
445 printf("%-16s ", mp->mnt_stat.f_mntonname);
446 if (fmi == NULL) {
447 printf("(null)\n");
448 return;
449 }
450 switch (fmi->fmi_state) {
451 case FSTRANS_NORMAL:
452 printf("state normal\n");
453 break;
454 case FSTRANS_SUSPENDING:
455 printf("state suspending\n");
456 break;
457 case FSTRANS_SUSPENDED:
458 printf("state suspended\n");
459 break;
460 default:
461 printf("state %#x\n", fmi->fmi_state);
462 break;
463 }
464 printf("%16s r=%d w=%d\n", "lock_lazy:",
465 rw_read_held(&fmi->fmi_lazy_lock),
466 rw_write_held(&fmi->fmi_lazy_lock));
467 printf("%16s r=%d w=%d\n", "lock_shared:",
468 rw_read_held(&fmi->fmi_shared_lock),
469 rw_write_held(&fmi->fmi_shared_lock));
470 }
471
472 void
473 fstrans_dump(int full)
474 {
475 const struct proclist_desc *pd;
476 struct proc *p;
477 struct lwp *l;
478 struct mount *mp;
479
480 printf("Fstrans locks by lwp:\n");
481 for (pd = proclists; pd->pd_list != NULL; pd++)
482 LIST_FOREACH(p, pd->pd_list, p_list)
483 LIST_FOREACH(l, &p->p_lwps, l_sibling)
484 fstrans_print_lwp(p, l, full == 1);
485
486 printf("Fstrans state by mount:\n");
487 CIRCLEQ_FOREACH(mp, &mountlist, mnt_list)
488 fstrans_print_mount(mp, full == 1);
489 }
490 #endif /* defined(DDB) */
491