vfs_trans.c revision 1.5.8.2 1 /* $NetBSD: vfs_trans.c,v 1.5.8.2 2007/04/05 21:57:50 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.2 2007/04/05 21:57:50 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/mount.h>
56 #include <sys/vnode.h>
57 #define _FSTRANS_API_PRIVATE
58 #include <sys/fstrans.h>
59 #include <sys/proc.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 struct lock fmi_shared_lock;
72 struct lock 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 lockmgr(&fmi->fmi_lazy_lock, LK_DRAIN, NULL);
130 lockmgr(&fmi->fmi_shared_lock, LK_DRAIN, NULL);
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 lockinit(&new->fmi_lazy_lock, PVFS, "suspfs", 0, 0);
152 lockinit(&new->fmi_shared_lock, PVFS, "suspfs", 0, 0);
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 int error, lkflags;
171 struct fstrans_lwp_info *fli, *new_fli;
172 struct fstrans_mount_info *fmi;
173
174 ASSERT_SLEEPABLE(NULL, __func__);
175
176 lkflags = (lock_type == FSTRANS_EXCL ? LK_EXCLUSIVE : LK_SHARED);
177 if (!wait)
178 lkflags |= LK_NOWAIT;
179
180 if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
181 return 0;
182
183 new_fli = NULL;
184 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
185 if (fli->fli_mount == NULL && new_fli == NULL)
186 new_fli = fli;
187 if (fli->fli_mount == mp) {
188 KASSERT(fli->fli_count > 0);
189 if (fli->fli_lock_type != FSTRANS_EXCL &&
190 lock_type == FSTRANS_EXCL)
191 panic("fstrans_start: cannot upgrade lock");
192 fli->fli_count += 1;
193 return 0;
194 }
195 }
196
197 if (new_fli == NULL) {
198 new_fli = pool_get(&fstrans_pl, PR_WAITOK);
199 new_fli->fli_mount = NULL;
200 new_fli->fli_count = 0;
201 new_fli->fli_succ = lwp_getspecific(lwp_data_key);
202 lwp_setspecific(lwp_data_key, new_fli);
203 }
204
205 KASSERT(new_fli->fli_mount == NULL);
206 KASSERT(new_fli->fli_count == 0);
207
208 if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
209 fmi = fstrans_mount_init(mp);
210
211 if (lock_type == FSTRANS_LAZY)
212 error = lockmgr(&fmi->fmi_lazy_lock, lkflags, NULL);
213 else
214 error = lockmgr(&fmi->fmi_shared_lock, lkflags, NULL);
215 if (error)
216 return error;
217
218 new_fli->fli_mount = mp;
219 new_fli->fli_count = 1;
220 new_fli->fli_lock_type = lock_type;
221
222 return 0;
223 }
224
225 /*
226 * Finish a transaction.
227 */
228 void
229 fstrans_done(struct mount *mp)
230 {
231 struct fstrans_lwp_info *fli;
232 struct fstrans_mount_info *fmi;
233
234 if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
235 return;
236
237 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
238 if (fli->fli_mount == mp) {
239 fli->fli_count -= 1;
240 if (fli->fli_count > 0)
241 return;
242 break;
243 }
244 }
245
246 KASSERT(fli != NULL);
247 KASSERT(fli->fli_mount == mp);
248 KASSERT(fli->fli_count == 0);
249 fli->fli_mount = NULL;
250 fmi = mount_getspecific(mp, mount_data_key);
251 KASSERT(fmi != NULL);
252 if (fli->fli_lock_type == FSTRANS_LAZY)
253 lockmgr(&fmi->fmi_lazy_lock, LK_RELEASE, NULL);
254 else
255 lockmgr(&fmi->fmi_shared_lock, LK_RELEASE, NULL);
256 }
257
258 /*
259 * Check if this thread has an exclusive lock.
260 */
261 int
262 fstrans_is_owner(struct mount *mp)
263 {
264 struct fstrans_lwp_info *fli;
265
266 if (mp == NULL)
267 return 0;
268 if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
269 return 0;
270
271 for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ)
272 if (fli->fli_mount == mp)
273 break;
274
275 if (fli == NULL)
276 return 0;
277
278 KASSERT(fli->fli_mount == mp);
279 KASSERT(fli->fli_count > 0);
280 return (fli->fli_lock_type == FSTRANS_EXCL);
281 }
282
283 /*
284 * Set new file system state.
285 */
286 int
287 fstrans_setstate(struct mount *mp, enum fstrans_state new_state)
288 {
289 int error;
290 struct fstrans_mount_info *fmi;
291
292 if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
293 fmi = fstrans_mount_init(mp);
294
295 switch (new_state) {
296 case FSTRANS_SUSPENDING:
297 KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
298 if ((error = fstrans_start(mp, FSTRANS_EXCL)) != 0)
299 return error;
300 fmi->fmi_state = FSTRANS_SUSPENDING;
301 break;
302
303 case FSTRANS_SUSPENDED:
304 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
305 fmi->fmi_state == FSTRANS_SUSPENDING);
306 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
307 fstrans_is_owner(mp));
308 if (fmi->fmi_state == FSTRANS_NORMAL &&
309 (error = fstrans_start(mp, FSTRANS_EXCL)) != 0)
310 return error;
311 if ((error = lockmgr(&fmi->fmi_lazy_lock, LK_EXCLUSIVE, NULL))
312 != 0)
313 return error;
314 fmi->fmi_state = FSTRANS_SUSPENDED;
315 break;
316
317 case FSTRANS_NORMAL:
318 KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
319 fstrans_is_owner(mp));
320 if (fmi->fmi_state == FSTRANS_SUSPENDED)
321 lockmgr(&fmi->fmi_lazy_lock, LK_RELEASE, NULL);
322 if (fmi->fmi_state == FSTRANS_SUSPENDING ||
323 fmi->fmi_state == FSTRANS_SUSPENDED) {
324 fmi->fmi_state = FSTRANS_NORMAL;
325 fstrans_done(mp);
326 }
327 break;
328
329 default:
330 panic("%s: illegal state %d", __func__, new_state);
331 }
332
333 return 0;
334 }
335
336 /*
337 * Get current file system state
338 */
339 enum fstrans_state
340 fstrans_getstate(struct mount *mp)
341 {
342 struct fstrans_mount_info *fmi;
343
344 if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
345 return FSTRANS_NORMAL;
346
347 return fmi->fmi_state;
348 }
349
350 /*
351 * Request a filesystem to suspend all operations.
352 */
353 int
354 vfs_suspend(struct mount *mp, int nowait)
355 {
356 int error;
357
358 if (nowait) {
359 if (!mutex_tryenter(&vfs_suspend_lock))
360 return EWOULDBLOCK;
361 } else
362 mutex_enter(&vfs_suspend_lock);
363
364 mutex_enter(&syncer_mutex);
365
366 if ((error = VFS_SUSPENDCTL(mp, SUSPEND_SUSPEND)) != 0) {
367 mutex_exit(&syncer_mutex);
368 mutex_exit(&vfs_suspend_lock);
369 }
370
371 return error;
372 }
373
374 /*
375 * Request a filesystem to resume all operations.
376 */
377 void
378 vfs_resume(struct mount *mp)
379 {
380
381 VFS_SUSPENDCTL(mp, SUSPEND_RESUME);
382 mutex_exit(&syncer_mutex);
383 mutex_exit(&vfs_suspend_lock);
384 }
385
386 /*
387 * Default vfs_suspendctl routine for file systems that do not support it.
388 */
389 /*ARGSUSED*/
390 int
391 vfs_stdsuspendctl(struct mount *mp __unused, int mode __unused)
392 {
393 return EOPNOTSUPP;
394 }
395
396 #if defined(DDB)
397 void fstrans_dump(int);
398
399 static void
400 fstrans_print_lwp(struct proc *p, struct lwp *l, int verbose)
401 {
402 char prefix[9];
403 struct fstrans_lwp_info *fli;
404
405 snprintf(prefix, sizeof(prefix), "%d.%d", p->p_pid, l->l_lid);
406 for (fli = _lwp_getspecific_by_lwp(l, lwp_data_key);
407 fli;
408 fli = fli->fli_succ) {
409 if (!verbose && fli->fli_count == 0)
410 continue;
411 printf("%-8s", prefix);
412 if (verbose)
413 printf(" @%p", fli);
414 if (fli->fli_mount != NULL)
415 printf(" (%s)", fli->fli_mount->mnt_stat.f_mntonname);
416 else
417 printf(" NULL");
418 switch (fli->fli_lock_type) {
419 case FSTRANS_LAZY:
420 printf(" lazy");
421 break;
422 case FSTRANS_SHARED:
423 printf(" shared");
424 break;
425 case FSTRANS_EXCL:
426 printf(" excl");
427 break;
428 default:
429 printf(" %#x", fli->fli_lock_type);
430 break;
431 }
432 printf(" %d\n", fli->fli_count);
433 prefix[0] = '\0';
434 }
435 }
436
437 static void
438 fstrans_print_mount(struct mount *mp, int verbose)
439 {
440 struct fstrans_mount_info *fmi;
441
442 fmi = mount_getspecific(mp, mount_data_key);
443 if (!verbose && (fmi == NULL || fmi->fmi_state == FSTRANS_NORMAL))
444 return;
445
446 printf("%-16s ", mp->mnt_stat.f_mntonname);
447 if (fmi == NULL) {
448 printf("(null)\n");
449 return;
450 }
451 switch (fmi->fmi_state) {
452 case FSTRANS_NORMAL:
453 printf("state normal\n");
454 break;
455 case FSTRANS_SUSPENDING:
456 printf("state suspending\n");
457 break;
458 case FSTRANS_SUSPENDED:
459 printf("state suspended\n");
460 break;
461 default:
462 printf("state %#x\n", fmi->fmi_state);
463 break;
464 }
465 printf("%16s", "lock_lazy:");
466 lockmgr_printinfo(&fmi->fmi_lazy_lock);
467 printf("\n");
468 printf("%16s", "lock_shared:");
469 lockmgr_printinfo(&fmi->fmi_shared_lock);
470 printf("\n");
471 }
472
473 void
474 fstrans_dump(int full)
475 {
476 const struct proclist_desc *pd;
477 struct proc *p;
478 struct lwp *l;
479 struct mount *mp;
480
481 printf("Fstrans locks by lwp:\n");
482 for (pd = proclists; pd->pd_list != NULL; pd++)
483 LIST_FOREACH(p, pd->pd_list, p_list)
484 LIST_FOREACH(l, &p->p_lwps, l_sibling)
485 fstrans_print_lwp(p, l, full == 1);
486
487 printf("Fstrans state by mount:\n");
488 CIRCLEQ_FOREACH(mp, &mountlist, mnt_list)
489 fstrans_print_mount(mp, full == 1);
490 }
491 #endif /* defined(DDB) */
492