vfs_trans.c revision 1.5.2.1 1 /* $NetBSD: vfs_trans.c,v 1.5.2.1 2007/03/24 14:56:07 yamt 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.2.1 2007/03/24 14:56:07 yamt 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/vnode.h>
57 #define _FSTRANS_API_PRIVATE
58 #include <sys/fstrans.h>
59
60 #include <miscfs/syncfs/syncfs.h>
61
62 struct fstrans_lwp_info {
63 struct fstrans_lwp_info *fli_succ;
64 struct mount *fli_mount;
65 int fli_count;
66 enum fstrans_lock_type fli_lock_type;
67 };
68 struct fstrans_mount_info {
69 enum fstrans_state fmi_state;
70 struct lock fmi_shared_lock;
71 struct lock fmi_lazy_lock;
72 };
73
74 static specificdata_key_t lwp_data_key;
75 static specificdata_key_t mount_data_key;
76 static kmutex_t vfs_suspend_lock; /* Serialize suspensions. */
77 static kmutex_t fstrans_init_lock;
78
79 POOL_INIT(fstrans_pl, sizeof(struct fstrans_lwp_info), 0, 0, 0,
80 "fstrans", NULL, IPL_NONE);
81
82 static void fstrans_lwp_dtor(void *);
83 static void fstrans_mount_dtor(void *);
84 static struct fstrans_mount_info *fstrans_mount_init(struct mount *);
85
86 /*
87 * Initialize
88 */
89 void
90 fstrans_init(void)
91 {
92 int error;
93
94 error = lwp_specific_key_create(&lwp_data_key, fstrans_lwp_dtor);
95 KASSERT(error == 0);
96 error = mount_specific_key_create(&mount_data_key, fstrans_mount_dtor);
97 KASSERT(error == 0);
98
99 mutex_init(&vfs_suspend_lock, MUTEX_DEFAULT, IPL_NONE);
100 mutex_init(&fstrans_init_lock, MUTEX_DEFAULT, IPL_NONE);
101 }
102
103 /*
104 * Deallocate lwp state
105 */
106 static void
107 fstrans_lwp_dtor(void *arg)
108 {
109 struct fstrans_lwp_info *fli, *fli_next;
110
111 for (fli = arg; fli; fli = fli_next) {
112 KASSERT(fli->fli_mount == NULL);
113 KASSERT(fli->fli_count == 0);
114 fli_next = fli->fli_succ;
115 pool_put(&fstrans_pl, fli);
116 }
117 }
118
119 /*
120 * Deallocate mount state
121 */
122 static void
123 fstrans_mount_dtor(void *arg)
124 {
125 struct fstrans_mount_info *fmi = arg;
126
127 KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
128 lockmgr(&fmi->fmi_lazy_lock, LK_DRAIN, NULL);
129 lockmgr(&fmi->fmi_shared_lock, LK_DRAIN, NULL);
130 free(fmi, M_MOUNT);
131 }
132
133 /*
134 * Create mount info for this mount
135 */
136 static struct fstrans_mount_info *
137 fstrans_mount_init(struct mount *mp)
138 {
139 struct fstrans_mount_info *new;
140
141 mutex_enter(&fstrans_init_lock);
142
143 if ((new = mount_getspecific(mp, mount_data_key)) != NULL) {
144 mutex_exit(&fstrans_init_lock);
145 return new;
146 }
147
148 new = malloc(sizeof(*new), M_MOUNT, M_WAITOK);
149 new->fmi_state = FSTRANS_NORMAL;
150 lockinit(&new->fmi_lazy_lock, PVFS, "suspfs", 0, 0);
151 lockinit(&new->fmi_shared_lock, PVFS, "suspfs", 0, 0);
152
153 mount_setspecific(mp, mount_data_key, new);
154 mutex_exit(&fstrans_init_lock);
155
156 return new;
157 }
158
159 /*
160 * Start a transaction. If this thread already has a transaction on this
161 * file system increment the reference counter.
162 * A thread with an exclusive transaction lock may get a shared or lazy one.
163 * A thread with a shared or lazy transaction lock cannot upgrade to an
164 * exclusive one yet.
165 */
166 int
167 _fstrans_start(struct mount *mp, enum fstrans_lock_type lock_type, int wait)
168 {
169 int error, lkflags;
170 struct fstrans_lwp_info *fli, *new_fli;
171 struct fstrans_mount_info *fmi;
172
173 ASSERT_SLEEPABLE(NULL, __func__);
174
175 lkflags = (lock_type == FSTRANS_EXCL ? LK_EXCLUSIVE : LK_SHARED);
176 if (!wait)
177 lkflags |= LK_NOWAIT;
178
179 if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
180 return 0;
181
182 new_fli = NULL;
183 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
184 if (fli->fli_mount == NULL && new_fli == NULL)
185 new_fli = fli;
186 if (fli->fli_mount == mp) {
187 KASSERT(fli->fli_count > 0);
188 if (fli->fli_lock_type != FSTRANS_EXCL &&
189 lock_type == FSTRANS_EXCL)
190 panic("fstrans_start: cannot upgrade lock");
191 fli->fli_count += 1;
192 return 0;
193 }
194 }
195
196 if (new_fli == NULL) {
197 new_fli = pool_get(&fstrans_pl, PR_WAITOK);
198 new_fli->fli_mount = NULL;
199 new_fli->fli_count = 0;
200 new_fli->fli_succ = lwp_getspecific(lwp_data_key);
201 lwp_setspecific(lwp_data_key, new_fli);
202 }
203
204 KASSERT(new_fli->fli_mount == NULL);
205 KASSERT(new_fli->fli_count == 0);
206
207 if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
208 fmi = fstrans_mount_init(mp);
209
210 if (lock_type == FSTRANS_LAZY)
211 error = lockmgr(&fmi->fmi_lazy_lock, lkflags, NULL);
212 else
213 error = lockmgr(&fmi->fmi_shared_lock, lkflags, NULL);
214 if (error)
215 return error;
216
217 new_fli->fli_mount = mp;
218 new_fli->fli_count = 1;
219 new_fli->fli_lock_type = lock_type;
220
221 return 0;
222 }
223
224 /*
225 * Finish a transaction.
226 */
227 void
228 fstrans_done(struct mount *mp)
229 {
230 struct fstrans_lwp_info *fli;
231 struct fstrans_mount_info *fmi;
232
233 if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
234 return;
235
236 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
237 if (fli->fli_mount == mp) {
238 fli->fli_count -= 1;
239 if (fli->fli_count > 0)
240 return;
241 break;
242 }
243 }
244
245 KASSERT(fli != NULL);
246 KASSERT(fli->fli_mount == mp);
247 KASSERT(fli->fli_count == 0);
248 fli->fli_mount = NULL;
249 fmi = mount_getspecific(mp, mount_data_key);
250 KASSERT(fmi != NULL);
251 if (fli->fli_lock_type == FSTRANS_LAZY)
252 lockmgr(&fmi->fmi_lazy_lock, LK_RELEASE, NULL);
253 else
254 lockmgr(&fmi->fmi_shared_lock, LK_RELEASE, NULL);
255 }
256
257 /*
258 * Check if this thread has an exclusive lock.
259 */
260 int
261 fstrans_is_owner(struct mount *mp)
262 {
263 struct fstrans_lwp_info *fli;
264
265 if (mp == NULL)
266 return 0;
267 if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
268 return 0;
269
270 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ)
271 if (fli->fli_mount == mp)
272 break;
273
274 if (fli == NULL)
275 return 0;
276
277 KASSERT(fli->fli_mount == mp);
278 KASSERT(fli->fli_count > 0);
279 return (fli->fli_lock_type == FSTRANS_EXCL);
280 }
281
282 /*
283 * Set new file system state.
284 */
285 int
286 fstrans_setstate(struct mount *mp, enum fstrans_state new_state)
287 {
288 int error;
289 struct fstrans_mount_info *fmi;
290
291 if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
292 fmi = fstrans_mount_init(mp);
293
294 switch (new_state) {
295 case FSTRANS_SUSPENDING:
296 KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
297 if ((error = fstrans_start(mp, FSTRANS_EXCL)) != 0)
298 return error;
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 (error = fstrans_start(mp, FSTRANS_EXCL)) != 0)
309 return error;
310 if ((error = lockmgr(&fmi->fmi_lazy_lock, LK_EXCLUSIVE, NULL))
311 != 0)
312 return error;
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 lockmgr(&fmi->fmi_lazy_lock, LK_RELEASE, NULL);
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", "lock_lazy:");
465 lockmgr_printinfo(&fmi->fmi_lazy_lock);
466 printf("\n");
467 printf("%16s", "lock_shared:");
468 lockmgr_printinfo(&fmi->fmi_shared_lock);
469 printf("\n");
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