lfs_vnops.c revision 1.182 1 /* $NetBSD: lfs_vnops.c,v 1.182 2006/07/13 22:05:52 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
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 * Copyright (c) 1986, 1989, 1991, 1993, 1995
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.182 2006/07/13 22:05:52 martin Exp $");
71
72 #include "opt_compat_netbsd.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/namei.h>
77 #include <sys/resourcevar.h>
78 #include <sys/kernel.h>
79 #include <sys/file.h>
80 #include <sys/stat.h>
81 #include <sys/buf.h>
82 #include <sys/proc.h>
83 #include <sys/mount.h>
84 #include <sys/vnode.h>
85 #include <sys/pool.h>
86 #include <sys/signalvar.h>
87 #include <sys/kauth.h>
88 #include <sys/syslog.h>
89
90 #include <miscfs/fifofs/fifo.h>
91 #include <miscfs/genfs/genfs.h>
92 #include <miscfs/specfs/specdev.h>
93
94 #include <ufs/ufs/inode.h>
95 #include <ufs/ufs/dir.h>
96 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ufs/ufs_extern.h>
98
99 #include <uvm/uvm.h>
100 #include <uvm/uvm_pmap.h>
101 #include <uvm/uvm_stat.h>
102 #include <uvm/uvm_pager.h>
103
104 #include <ufs/lfs/lfs.h>
105 #include <ufs/lfs/lfs_extern.h>
106
107 extern pid_t lfs_writer_daemon;
108
109 /* Global vfs data structures for lfs. */
110 int (**lfs_vnodeop_p)(void *);
111 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
112 { &vop_default_desc, vn_default_error },
113 { &vop_lookup_desc, ufs_lookup }, /* lookup */
114 { &vop_create_desc, lfs_create }, /* create */
115 { &vop_whiteout_desc, ufs_whiteout }, /* whiteout */
116 { &vop_mknod_desc, lfs_mknod }, /* mknod */
117 { &vop_open_desc, ufs_open }, /* open */
118 { &vop_close_desc, lfs_close }, /* close */
119 { &vop_access_desc, ufs_access }, /* access */
120 { &vop_getattr_desc, lfs_getattr }, /* getattr */
121 { &vop_setattr_desc, lfs_setattr }, /* setattr */
122 { &vop_read_desc, lfs_read }, /* read */
123 { &vop_write_desc, lfs_write }, /* write */
124 { &vop_lease_desc, ufs_lease_check }, /* lease */
125 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
126 { &vop_fcntl_desc, lfs_fcntl }, /* fcntl */
127 { &vop_poll_desc, ufs_poll }, /* poll */
128 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
129 { &vop_revoke_desc, ufs_revoke }, /* revoke */
130 { &vop_mmap_desc, lfs_mmap }, /* mmap */
131 { &vop_fsync_desc, lfs_fsync }, /* fsync */
132 { &vop_seek_desc, ufs_seek }, /* seek */
133 { &vop_remove_desc, lfs_remove }, /* remove */
134 { &vop_link_desc, lfs_link }, /* link */
135 { &vop_rename_desc, lfs_rename }, /* rename */
136 { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
137 { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
138 { &vop_symlink_desc, lfs_symlink }, /* symlink */
139 { &vop_readdir_desc, ufs_readdir }, /* readdir */
140 { &vop_readlink_desc, ufs_readlink }, /* readlink */
141 { &vop_abortop_desc, ufs_abortop }, /* abortop */
142 { &vop_inactive_desc, lfs_inactive }, /* inactive */
143 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
144 { &vop_lock_desc, ufs_lock }, /* lock */
145 { &vop_unlock_desc, ufs_unlock }, /* unlock */
146 { &vop_bmap_desc, ufs_bmap }, /* bmap */
147 { &vop_strategy_desc, lfs_strategy }, /* strategy */
148 { &vop_print_desc, ufs_print }, /* print */
149 { &vop_islocked_desc, ufs_islocked }, /* islocked */
150 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
151 { &vop_advlock_desc, ufs_advlock }, /* advlock */
152 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
153 { &vop_getpages_desc, lfs_getpages }, /* getpages */
154 { &vop_putpages_desc, lfs_putpages }, /* putpages */
155 { NULL, NULL }
156 };
157 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
158 { &lfs_vnodeop_p, lfs_vnodeop_entries };
159
160 int (**lfs_specop_p)(void *);
161 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
162 { &vop_default_desc, vn_default_error },
163 { &vop_lookup_desc, spec_lookup }, /* lookup */
164 { &vop_create_desc, spec_create }, /* create */
165 { &vop_mknod_desc, spec_mknod }, /* mknod */
166 { &vop_open_desc, spec_open }, /* open */
167 { &vop_close_desc, lfsspec_close }, /* close */
168 { &vop_access_desc, ufs_access }, /* access */
169 { &vop_getattr_desc, lfs_getattr }, /* getattr */
170 { &vop_setattr_desc, lfs_setattr }, /* setattr */
171 { &vop_read_desc, ufsspec_read }, /* read */
172 { &vop_write_desc, ufsspec_write }, /* write */
173 { &vop_lease_desc, spec_lease_check }, /* lease */
174 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
175 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
176 { &vop_poll_desc, spec_poll }, /* poll */
177 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
178 { &vop_revoke_desc, spec_revoke }, /* revoke */
179 { &vop_mmap_desc, spec_mmap }, /* mmap */
180 { &vop_fsync_desc, spec_fsync }, /* fsync */
181 { &vop_seek_desc, spec_seek }, /* seek */
182 { &vop_remove_desc, spec_remove }, /* remove */
183 { &vop_link_desc, spec_link }, /* link */
184 { &vop_rename_desc, spec_rename }, /* rename */
185 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
186 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
187 { &vop_symlink_desc, spec_symlink }, /* symlink */
188 { &vop_readdir_desc, spec_readdir }, /* readdir */
189 { &vop_readlink_desc, spec_readlink }, /* readlink */
190 { &vop_abortop_desc, spec_abortop }, /* abortop */
191 { &vop_inactive_desc, lfs_inactive }, /* inactive */
192 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
193 { &vop_lock_desc, ufs_lock }, /* lock */
194 { &vop_unlock_desc, ufs_unlock }, /* unlock */
195 { &vop_bmap_desc, spec_bmap }, /* bmap */
196 { &vop_strategy_desc, spec_strategy }, /* strategy */
197 { &vop_print_desc, ufs_print }, /* print */
198 { &vop_islocked_desc, ufs_islocked }, /* islocked */
199 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
200 { &vop_advlock_desc, spec_advlock }, /* advlock */
201 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
202 { &vop_getpages_desc, spec_getpages }, /* getpages */
203 { &vop_putpages_desc, spec_putpages }, /* putpages */
204 { NULL, NULL }
205 };
206 const struct vnodeopv_desc lfs_specop_opv_desc =
207 { &lfs_specop_p, lfs_specop_entries };
208
209 int (**lfs_fifoop_p)(void *);
210 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
211 { &vop_default_desc, vn_default_error },
212 { &vop_lookup_desc, fifo_lookup }, /* lookup */
213 { &vop_create_desc, fifo_create }, /* create */
214 { &vop_mknod_desc, fifo_mknod }, /* mknod */
215 { &vop_open_desc, fifo_open }, /* open */
216 { &vop_close_desc, lfsfifo_close }, /* close */
217 { &vop_access_desc, ufs_access }, /* access */
218 { &vop_getattr_desc, lfs_getattr }, /* getattr */
219 { &vop_setattr_desc, lfs_setattr }, /* setattr */
220 { &vop_read_desc, ufsfifo_read }, /* read */
221 { &vop_write_desc, ufsfifo_write }, /* write */
222 { &vop_lease_desc, fifo_lease_check }, /* lease */
223 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
224 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
225 { &vop_poll_desc, fifo_poll }, /* poll */
226 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
227 { &vop_revoke_desc, fifo_revoke }, /* revoke */
228 { &vop_mmap_desc, fifo_mmap }, /* mmap */
229 { &vop_fsync_desc, fifo_fsync }, /* fsync */
230 { &vop_seek_desc, fifo_seek }, /* seek */
231 { &vop_remove_desc, fifo_remove }, /* remove */
232 { &vop_link_desc, fifo_link }, /* link */
233 { &vop_rename_desc, fifo_rename }, /* rename */
234 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
235 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
236 { &vop_symlink_desc, fifo_symlink }, /* symlink */
237 { &vop_readdir_desc, fifo_readdir }, /* readdir */
238 { &vop_readlink_desc, fifo_readlink }, /* readlink */
239 { &vop_abortop_desc, fifo_abortop }, /* abortop */
240 { &vop_inactive_desc, lfs_inactive }, /* inactive */
241 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
242 { &vop_lock_desc, ufs_lock }, /* lock */
243 { &vop_unlock_desc, ufs_unlock }, /* unlock */
244 { &vop_bmap_desc, fifo_bmap }, /* bmap */
245 { &vop_strategy_desc, fifo_strategy }, /* strategy */
246 { &vop_print_desc, ufs_print }, /* print */
247 { &vop_islocked_desc, ufs_islocked }, /* islocked */
248 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
249 { &vop_advlock_desc, fifo_advlock }, /* advlock */
250 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
251 { &vop_putpages_desc, fifo_putpages }, /* putpages */
252 { NULL, NULL }
253 };
254 const struct vnodeopv_desc lfs_fifoop_opv_desc =
255 { &lfs_fifoop_p, lfs_fifoop_entries };
256
257 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int);
258
259 #define LFS_READWRITE
260 #include <ufs/ufs/ufs_readwrite.c>
261 #undef LFS_READWRITE
262
263 /*
264 * Synch an open file.
265 */
266 /* ARGSUSED */
267 int
268 lfs_fsync(void *v)
269 {
270 struct vop_fsync_args /* {
271 struct vnode *a_vp;
272 kauth_cred_t a_cred;
273 int a_flags;
274 off_t offlo;
275 off_t offhi;
276 struct lwp *a_l;
277 } */ *ap = v;
278 struct vnode *vp = ap->a_vp;
279 int error, wait;
280
281 /* If we're mounted read-only, don't try to sync. */
282 if (VTOI(vp)->i_lfs->lfs_ronly)
283 return 0;
284
285 /*
286 * Trickle sync checks for need to do a checkpoint after possible
287 * activity from the pagedaemon.
288 */
289 if (ap->a_flags & FSYNC_LAZY) {
290 simple_lock(&lfs_subsys_lock);
291 wakeup(&lfs_writer_daemon);
292 simple_unlock(&lfs_subsys_lock);
293 return 0;
294 }
295
296 /*
297 * Don't reclaim any vnodes that are being cleaned.
298 * This prevents the cleaner from writing files twice
299 * in the same partial segment, causing an accounting
300 * underflow.
301 */
302 if (ap->a_flags & FSYNC_RECLAIM) {
303 if (VTOI(vp)->i_flags & IN_CLEANING)
304 return EAGAIN;
305 }
306
307 wait = (ap->a_flags & FSYNC_WAIT);
308 simple_lock(&vp->v_interlock);
309 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
310 round_page(ap->a_offhi),
311 PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
312 if (error)
313 return error;
314 error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
315 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
316 int l = 0;
317 error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
318 ap->a_l->l_proc->p_cred, ap->a_l);
319 }
320 if (wait && !VPISEMPTY(vp))
321 LFS_SET_UINO(VTOI(vp), IN_MODIFIED);
322
323 return error;
324 }
325
326 /*
327 * Take IN_ADIROP off, then call ufs_inactive.
328 */
329 int
330 lfs_inactive(void *v)
331 {
332 struct vop_inactive_args /* {
333 struct vnode *a_vp;
334 struct lwp *a_l;
335 } */ *ap = v;
336
337 KASSERT(VTOI(ap->a_vp)->i_nlink == VTOI(ap->a_vp)->i_ffs_effnlink);
338
339 lfs_unmark_vnode(ap->a_vp);
340
341 /*
342 * The Ifile is only ever inactivated on unmount.
343 * Streamline this process by not giving it more dirty blocks.
344 */
345 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
346 LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
347 VOP_UNLOCK(ap->a_vp, 0);
348 return 0;
349 }
350
351 return ufs_inactive(v);
352 }
353
354 /*
355 * These macros are used to bracket UFS directory ops, so that we can
356 * identify all the pages touched during directory ops which need to
357 * be ordered and flushed atomically, so that they may be recovered.
358 *
359 * Because we have to mark nodes VDIROP in order to prevent
360 * the cache from reclaiming them while a dirop is in progress, we must
361 * also manage the number of nodes so marked (otherwise we can run out).
362 * We do this by setting lfs_dirvcount to the number of marked vnodes; it
363 * is decremented during segment write, when VDIROP is taken off.
364 */
365 #define MARK_VNODE(vp) lfs_mark_vnode(vp)
366 #define UNMARK_VNODE(vp) lfs_unmark_vnode(vp)
367 #define SET_DIROP_CREATE(dvp, vpp) lfs_set_dirop_create((dvp), (vpp))
368 #define SET_DIROP_REMOVE(dvp, vp) lfs_set_dirop((dvp), (vp))
369 static int lfs_set_dirop_create(struct vnode *, struct vnode **);
370 static int lfs_set_dirop(struct vnode *, struct vnode *);
371
372 static int
373 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
374 {
375 struct lfs *fs;
376 int error;
377
378 KASSERT(VOP_ISLOCKED(dvp));
379 KASSERT(vp == NULL || VOP_ISLOCKED(vp));
380
381 fs = VTOI(dvp)->i_lfs;
382
383 ASSERT_NO_SEGLOCK(fs);
384 /*
385 * LFS_NRESERVE calculates direct and indirect blocks as well
386 * as an inode block; an overestimate in most cases.
387 */
388 if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
389 return (error);
390
391 restart:
392 simple_lock(&fs->lfs_interlock);
393 if (fs->lfs_dirops == 0) {
394 simple_unlock(&fs->lfs_interlock);
395 lfs_check(dvp, LFS_UNUSED_LBN, 0);
396 simple_lock(&fs->lfs_interlock);
397 }
398 while (fs->lfs_writer)
399 ltsleep(&fs->lfs_dirops, (PRIBIO + 1), "lfs_sdirop", 0,
400 &fs->lfs_interlock);
401 simple_lock(&lfs_subsys_lock);
402 if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
403 wakeup(&lfs_writer_daemon);
404 simple_unlock(&lfs_subsys_lock);
405 simple_unlock(&fs->lfs_interlock);
406 preempt(1);
407 goto restart;
408 }
409
410 if (lfs_dirvcount > LFS_MAX_DIROP) {
411 simple_unlock(&fs->lfs_interlock);
412 DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
413 "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
414 if ((error = ltsleep(&lfs_dirvcount,
415 PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
416 &lfs_subsys_lock)) != 0) {
417 goto unreserve;
418 }
419 goto restart;
420 }
421 simple_unlock(&lfs_subsys_lock);
422
423 ++fs->lfs_dirops;
424 fs->lfs_doifile = 1;
425 simple_unlock(&fs->lfs_interlock);
426
427 /* Hold a reference so SET_ENDOP will be happy */
428 vref(dvp);
429 if (vp) {
430 vref(vp);
431 MARK_VNODE(vp);
432 }
433
434 MARK_VNODE(dvp);
435 return 0;
436
437 unreserve:
438 lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
439 return error;
440 }
441
442 /*
443 * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
444 * in getnewvnode(), if we have a stacked filesystem mounted on top
445 * of us.
446 *
447 * NB: this means we have to clear the new vnodes on error. Fortunately
448 * SET_ENDOP is there to do that for us.
449 */
450 static int
451 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
452 {
453 int error;
454 struct lfs *fs;
455
456 fs = VFSTOUFS(dvp->v_mount)->um_lfs;
457 ASSERT_NO_SEGLOCK(fs);
458 if (fs->lfs_ronly)
459 return EROFS;
460 if (vpp && (error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, vpp))) {
461 DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
462 dvp, error));
463 return error;
464 }
465 if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
466 if (vpp) {
467 ungetnewvnode(*vpp);
468 *vpp = NULL;
469 }
470 return error;
471 }
472 return 0;
473 }
474
475 #define SET_ENDOP_BASE(fs, dvp, str) \
476 do { \
477 simple_lock(&(fs)->lfs_interlock); \
478 --(fs)->lfs_dirops; \
479 if (!(fs)->lfs_dirops) { \
480 if ((fs)->lfs_nadirop) { \
481 panic("SET_ENDOP: %s: no dirops but " \
482 " nadirop=%d", (str), \
483 (fs)->lfs_nadirop); \
484 } \
485 wakeup(&(fs)->lfs_writer); \
486 simple_unlock(&(fs)->lfs_interlock); \
487 lfs_check((dvp), LFS_UNUSED_LBN, 0); \
488 } else \
489 simple_unlock(&(fs)->lfs_interlock); \
490 } while(0)
491 #define SET_ENDOP_CREATE(fs, dvp, nvpp, str) \
492 do { \
493 UNMARK_VNODE(dvp); \
494 if (nvpp && *nvpp) \
495 UNMARK_VNODE(*nvpp); \
496 /* Check for error return to stem vnode leakage */ \
497 if (nvpp && *nvpp && !((*nvpp)->v_flag & VDIROP)) \
498 ungetnewvnode(*(nvpp)); \
499 SET_ENDOP_BASE((fs), (dvp), (str)); \
500 lfs_reserve((fs), (dvp), NULL, -LFS_NRESERVE(fs)); \
501 vrele(dvp); \
502 } while(0)
503 #define SET_ENDOP_CREATE_AP(ap, str) \
504 SET_ENDOP_CREATE(VTOI((ap)->a_dvp)->i_lfs, (ap)->a_dvp, \
505 (ap)->a_vpp, (str))
506 #define SET_ENDOP_REMOVE(fs, dvp, ovp, str) \
507 do { \
508 UNMARK_VNODE(dvp); \
509 if (ovp) \
510 UNMARK_VNODE(ovp); \
511 SET_ENDOP_BASE((fs), (dvp), (str)); \
512 lfs_reserve((fs), (dvp), (ovp), -LFS_NRESERVE(fs)); \
513 vrele(dvp); \
514 if (ovp) \
515 vrele(ovp); \
516 } while(0)
517
518 void
519 lfs_mark_vnode(struct vnode *vp)
520 {
521 struct inode *ip = VTOI(vp);
522 struct lfs *fs = ip->i_lfs;
523
524 simple_lock(&fs->lfs_interlock);
525 if (!(ip->i_flag & IN_ADIROP)) {
526 if (!(vp->v_flag & VDIROP)) {
527 (void)lfs_vref(vp);
528 simple_lock(&lfs_subsys_lock);
529 ++lfs_dirvcount;
530 ++fs->lfs_dirvcount;
531 simple_unlock(&lfs_subsys_lock);
532 TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
533 vp->v_flag |= VDIROP;
534 }
535 ++fs->lfs_nadirop;
536 ip->i_flag |= IN_ADIROP;
537 } else
538 KASSERT(vp->v_flag & VDIROP);
539 simple_unlock(&fs->lfs_interlock);
540 }
541
542 void
543 lfs_unmark_vnode(struct vnode *vp)
544 {
545 struct inode *ip = VTOI(vp);
546
547 if (ip && (ip->i_flag & IN_ADIROP)) {
548 KASSERT(vp->v_flag & VDIROP);
549 simple_lock(&ip->i_lfs->lfs_interlock);
550 --ip->i_lfs->lfs_nadirop;
551 simple_unlock(&ip->i_lfs->lfs_interlock);
552 ip->i_flag &= ~IN_ADIROP;
553 }
554 }
555
556 int
557 lfs_symlink(void *v)
558 {
559 struct vop_symlink_args /* {
560 struct vnode *a_dvp;
561 struct vnode **a_vpp;
562 struct componentname *a_cnp;
563 struct vattr *a_vap;
564 char *a_target;
565 } */ *ap = v;
566 int error;
567
568 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
569 vput(ap->a_dvp);
570 return error;
571 }
572 error = ufs_symlink(ap);
573 SET_ENDOP_CREATE_AP(ap, "symlink");
574 return (error);
575 }
576
577 int
578 lfs_mknod(void *v)
579 {
580 struct vop_mknod_args /* {
581 struct vnode *a_dvp;
582 struct vnode **a_vpp;
583 struct componentname *a_cnp;
584 struct vattr *a_vap;
585 } */ *ap = v;
586 struct vattr *vap = ap->a_vap;
587 struct vnode **vpp = ap->a_vpp;
588 struct inode *ip;
589 int error;
590 struct mount *mp;
591 ino_t ino;
592
593 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
594 vput(ap->a_dvp);
595 return error;
596 }
597 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
598 ap->a_dvp, vpp, ap->a_cnp);
599
600 /* Either way we're done with the dirop at this point */
601 SET_ENDOP_CREATE_AP(ap, "mknod");
602
603 if (error)
604 return (error);
605
606 ip = VTOI(*vpp);
607 mp = (*vpp)->v_mount;
608 ino = ip->i_number;
609 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
610 if (vap->va_rdev != VNOVAL) {
611 /*
612 * Want to be able to use this to make badblock
613 * inodes, so don't truncate the dev number.
614 */
615 #if 0
616 ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
617 UFS_MPNEEDSWAP((*vpp)->v_mount));
618 #else
619 ip->i_ffs1_rdev = vap->va_rdev;
620 #endif
621 }
622
623 /*
624 * Call fsync to write the vnode so that we don't have to deal with
625 * flushing it when it's marked VDIROP|VXLOCK.
626 *
627 * XXX KS - If we can't flush we also can't call vgone(), so must
628 * return. But, that leaves this vnode in limbo, also not good.
629 * Can this ever happen (barring hardware failure)?
630 */
631 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0,
632 curlwp)) != 0) {
633 panic("lfs_mknod: couldn't fsync (ino %llu)",
634 (unsigned long long)ino);
635 /* return (error); */
636 }
637 /*
638 * Remove vnode so that it will be reloaded by VFS_VGET and
639 * checked to see if it is an alias of an existing entry in
640 * the inode cache.
641 */
642 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
643
644 VOP_UNLOCK(*vpp, 0);
645 lfs_vunref(*vpp);
646 (*vpp)->v_type = VNON;
647 vgone(*vpp);
648 error = VFS_VGET(mp, ino, vpp);
649
650 if (error != 0) {
651 *vpp = NULL;
652 return (error);
653 }
654 return (0);
655 }
656
657 int
658 lfs_create(void *v)
659 {
660 struct vop_create_args /* {
661 struct vnode *a_dvp;
662 struct vnode **a_vpp;
663 struct componentname *a_cnp;
664 struct vattr *a_vap;
665 } */ *ap = v;
666 int error;
667
668 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
669 vput(ap->a_dvp);
670 return error;
671 }
672 error = ufs_create(ap);
673 SET_ENDOP_CREATE_AP(ap, "create");
674 return (error);
675 }
676
677 int
678 lfs_mkdir(void *v)
679 {
680 struct vop_mkdir_args /* {
681 struct vnode *a_dvp;
682 struct vnode **a_vpp;
683 struct componentname *a_cnp;
684 struct vattr *a_vap;
685 } */ *ap = v;
686 int error;
687
688 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
689 vput(ap->a_dvp);
690 return error;
691 }
692 error = ufs_mkdir(ap);
693 SET_ENDOP_CREATE_AP(ap, "mkdir");
694 return (error);
695 }
696
697 int
698 lfs_remove(void *v)
699 {
700 struct vop_remove_args /* {
701 struct vnode *a_dvp;
702 struct vnode *a_vp;
703 struct componentname *a_cnp;
704 } */ *ap = v;
705 struct vnode *dvp, *vp;
706 int error;
707
708 dvp = ap->a_dvp;
709 vp = ap->a_vp;
710 if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
711 if (dvp == vp)
712 vrele(vp);
713 else
714 vput(vp);
715 vput(dvp);
716 return error;
717 }
718 error = ufs_remove(ap);
719 SET_ENDOP_REMOVE(VTOI(dvp)->i_lfs, dvp, ap->a_vp, "remove");
720 return (error);
721 }
722
723 int
724 lfs_rmdir(void *v)
725 {
726 struct vop_rmdir_args /* {
727 struct vnodeop_desc *a_desc;
728 struct vnode *a_dvp;
729 struct vnode *a_vp;
730 struct componentname *a_cnp;
731 } */ *ap = v;
732 struct vnode *vp;
733 int error;
734
735 vp = ap->a_vp;
736 if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
737 vrele(ap->a_dvp);
738 if (ap->a_vp != ap->a_dvp)
739 VOP_UNLOCK(ap->a_dvp, 0);
740 vput(vp);
741 return error;
742 }
743 error = ufs_rmdir(ap);
744 SET_ENDOP_REMOVE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
745 return (error);
746 }
747
748 int
749 lfs_link(void *v)
750 {
751 struct vop_link_args /* {
752 struct vnode *a_dvp;
753 struct vnode *a_vp;
754 struct componentname *a_cnp;
755 } */ *ap = v;
756 int error;
757 struct vnode **vpp = NULL;
758
759 if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
760 vput(ap->a_dvp);
761 return error;
762 }
763 error = ufs_link(ap);
764 SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
765 return (error);
766 }
767
768 int
769 lfs_rename(void *v)
770 {
771 struct vop_rename_args /* {
772 struct vnode *a_fdvp;
773 struct vnode *a_fvp;
774 struct componentname *a_fcnp;
775 struct vnode *a_tdvp;
776 struct vnode *a_tvp;
777 struct componentname *a_tcnp;
778 } */ *ap = v;
779 struct vnode *tvp, *fvp, *tdvp, *fdvp;
780 struct componentname *tcnp, *fcnp;
781 int error;
782 struct lfs *fs;
783
784 fs = VTOI(ap->a_fdvp)->i_lfs;
785 tvp = ap->a_tvp;
786 tdvp = ap->a_tdvp;
787 tcnp = ap->a_tcnp;
788 fvp = ap->a_fvp;
789 fdvp = ap->a_fdvp;
790 fcnp = ap->a_fcnp;
791
792 /*
793 * Check for cross-device rename.
794 * If it is, we don't want to set dirops, just error out.
795 * (In particular note that MARK_VNODE(tdvp) will DTWT on
796 * a cross-device rename.)
797 *
798 * Copied from ufs_rename.
799 */
800 if ((fvp->v_mount != tdvp->v_mount) ||
801 (tvp && (fvp->v_mount != tvp->v_mount))) {
802 error = EXDEV;
803 goto errout;
804 }
805
806 /*
807 * Check to make sure we're not renaming a vnode onto itself
808 * (deleting a hard link by renaming one name onto another);
809 * if we are we can't recursively call VOP_REMOVE since that
810 * would leave us with an unaccounted-for number of live dirops.
811 *
812 * Inline the relevant section of ufs_rename here, *before*
813 * calling SET_DIROP_REMOVE.
814 */
815 if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
816 (VTOI(tdvp)->i_flags & APPEND))) {
817 error = EPERM;
818 goto errout;
819 }
820 if (fvp == tvp) {
821 if (fvp->v_type == VDIR) {
822 error = EINVAL;
823 goto errout;
824 }
825
826 /* Release destination completely. */
827 VOP_ABORTOP(tdvp, tcnp);
828 vput(tdvp);
829 vput(tvp);
830
831 /* Delete source. */
832 vrele(fvp);
833 fcnp->cn_flags &= ~(MODMASK | SAVESTART);
834 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
835 fcnp->cn_nameiop = DELETE;
836 if ((error = relookup(fdvp, &fvp, fcnp))){
837 /* relookup blew away fdvp */
838 return (error);
839 }
840 return (VOP_REMOVE(fdvp, fvp, fcnp));
841 }
842
843 if ((error = SET_DIROP_REMOVE(tdvp, tvp)) != 0)
844 goto errout;
845 MARK_VNODE(fdvp);
846 MARK_VNODE(fvp);
847
848 error = ufs_rename(ap);
849 UNMARK_VNODE(fdvp);
850 UNMARK_VNODE(fvp);
851 SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
852 return (error);
853
854 errout:
855 VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
856 if (tdvp == tvp)
857 vrele(tdvp);
858 else
859 vput(tdvp);
860 if (tvp)
861 vput(tvp);
862 VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
863 vrele(fdvp);
864 vrele(fvp);
865 return (error);
866 }
867
868 /* XXX hack to avoid calling ITIMES in getattr */
869 int
870 lfs_getattr(void *v)
871 {
872 struct vop_getattr_args /* {
873 struct vnode *a_vp;
874 struct vattr *a_vap;
875 kauth_cred_t a_cred;
876 struct lwp *a_l;
877 } */ *ap = v;
878 struct vnode *vp = ap->a_vp;
879 struct inode *ip = VTOI(vp);
880 struct vattr *vap = ap->a_vap;
881 struct lfs *fs = ip->i_lfs;
882 /*
883 * Copy from inode table
884 */
885 vap->va_fsid = ip->i_dev;
886 vap->va_fileid = ip->i_number;
887 vap->va_mode = ip->i_mode & ~IFMT;
888 vap->va_nlink = ip->i_nlink;
889 vap->va_uid = ip->i_uid;
890 vap->va_gid = ip->i_gid;
891 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
892 vap->va_size = vp->v_size;
893 vap->va_atime.tv_sec = ip->i_ffs1_atime;
894 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
895 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
896 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
897 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
898 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
899 vap->va_flags = ip->i_flags;
900 vap->va_gen = ip->i_gen;
901 /* this doesn't belong here */
902 if (vp->v_type == VBLK)
903 vap->va_blocksize = BLKDEV_IOSIZE;
904 else if (vp->v_type == VCHR)
905 vap->va_blocksize = MAXBSIZE;
906 else
907 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
908 vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
909 vap->va_type = vp->v_type;
910 vap->va_filerev = ip->i_modrev;
911 return (0);
912 }
913
914 /*
915 * Check to make sure the inode blocks won't choke the buffer
916 * cache, then call ufs_setattr as usual.
917 */
918 int
919 lfs_setattr(void *v)
920 {
921 struct vop_setattr_args /* {
922 struct vnode *a_vp;
923 struct vattr *a_vap;
924 kauth_cred_t a_cred;
925 struct lwp *a_l;
926 } */ *ap = v;
927 struct vnode *vp = ap->a_vp;
928
929 lfs_check(vp, LFS_UNUSED_LBN, 0);
930 return ufs_setattr(v);
931 }
932
933 /*
934 * Release the block we hold on lfs_newseg wrapping. Called on file close,
935 * or explicitly from LFCNWRAPGO.
936 */
937 static int
938 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
939 {
940 if ((ip->i_lfs_iflags & LFSI_WRAPBLOCK) == 0)
941 return EBUSY;
942
943 ip->i_lfs_iflags &= ~LFSI_WRAPBLOCK;
944
945 KASSERT(fs->lfs_nowrap > 0);
946 if (fs->lfs_nowrap <= 0) {
947 simple_unlock(&fs->lfs_interlock);
948 return 0;
949 }
950
951 if (--fs->lfs_nowrap == 0) {
952 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
953 wakeup(&fs->lfs_nowrap);
954 lfs_wakeup_cleaner(fs);
955 }
956 if (waitfor) {
957 ltsleep(&fs->lfs_nextseg, PCATCH | PUSER,
958 "segment", 0, &fs->lfs_interlock);
959 }
960
961 return 0;
962 }
963
964 /*
965 * Close called
966 */
967 /* ARGSUSED */
968 int
969 lfs_close(void *v)
970 {
971 struct vop_close_args /* {
972 struct vnode *a_vp;
973 int a_fflag;
974 kauth_cred_t a_cred;
975 struct lwp *a_l;
976 } */ *ap = v;
977 struct vnode *vp = ap->a_vp;
978 struct inode *ip = VTOI(vp);
979 struct lfs *fs = ip->i_lfs;
980
981 if (ip->i_lfs_iflags & LFSI_WRAPBLOCK) {
982 simple_lock(&fs->lfs_interlock);
983 lfs_wrapgo(fs, ip, 0);
984 simple_unlock(&fs->lfs_interlock);
985 }
986
987 if (vp == ip->i_lfs->lfs_ivnode &&
988 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
989 return 0;
990
991 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
992 LFS_ITIMES(ip, NULL, NULL, NULL);
993 }
994 return (0);
995 }
996
997 /*
998 * Close wrapper for special devices.
999 *
1000 * Update the times on the inode then do device close.
1001 */
1002 int
1003 lfsspec_close(void *v)
1004 {
1005 struct vop_close_args /* {
1006 struct vnode *a_vp;
1007 int a_fflag;
1008 kauth_cred_t a_cred;
1009 struct lwp *a_l;
1010 } */ *ap = v;
1011 struct vnode *vp;
1012 struct inode *ip;
1013
1014 vp = ap->a_vp;
1015 ip = VTOI(vp);
1016 if (vp->v_usecount > 1) {
1017 LFS_ITIMES(ip, NULL, NULL, NULL);
1018 }
1019 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1020 }
1021
1022 /*
1023 * Close wrapper for fifo's.
1024 *
1025 * Update the times on the inode then do device close.
1026 */
1027 int
1028 lfsfifo_close(void *v)
1029 {
1030 struct vop_close_args /* {
1031 struct vnode *a_vp;
1032 int a_fflag;
1033 kauth_cred_ a_cred;
1034 struct lwp *a_l;
1035 } */ *ap = v;
1036 struct vnode *vp;
1037 struct inode *ip;
1038
1039 vp = ap->a_vp;
1040 ip = VTOI(vp);
1041 if (ap->a_vp->v_usecount > 1) {
1042 LFS_ITIMES(ip, NULL, NULL, NULL);
1043 }
1044 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1045 }
1046
1047 /*
1048 * Reclaim an inode so that it can be used for other purposes.
1049 */
1050
1051 int
1052 lfs_reclaim(void *v)
1053 {
1054 struct vop_reclaim_args /* {
1055 struct vnode *a_vp;
1056 struct lwp *a_l;
1057 } */ *ap = v;
1058 struct vnode *vp = ap->a_vp;
1059 struct inode *ip = VTOI(vp);
1060 int error;
1061
1062 KASSERT(ip->i_nlink == ip->i_ffs_effnlink);
1063
1064 LFS_CLR_UINO(ip, IN_ALLMOD);
1065 if ((error = ufs_reclaim(vp, ap->a_l)))
1066 return (error);
1067 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1068 lfs_deregister_all(vp);
1069 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1070 ip->inode_ext.lfs = NULL;
1071 pool_put(&lfs_inode_pool, vp->v_data);
1072 vp->v_data = NULL;
1073 return (0);
1074 }
1075
1076 /*
1077 * Read a block from a storage device.
1078 * In order to avoid reading blocks that are in the process of being
1079 * written by the cleaner---and hence are not mutexed by the normal
1080 * buffer cache / page cache mechanisms---check for collisions before
1081 * reading.
1082 *
1083 * We inline ufs_strategy to make sure that the VOP_BMAP occurs *before*
1084 * the active cleaner test.
1085 *
1086 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1087 */
1088 int
1089 lfs_strategy(void *v)
1090 {
1091 struct vop_strategy_args /* {
1092 struct vnode *a_vp;
1093 struct buf *a_bp;
1094 } */ *ap = v;
1095 struct buf *bp;
1096 struct lfs *fs;
1097 struct vnode *vp;
1098 struct inode *ip;
1099 daddr_t tbn;
1100 int i, sn, error, slept;
1101
1102 bp = ap->a_bp;
1103 vp = ap->a_vp;
1104 ip = VTOI(vp);
1105 fs = ip->i_lfs;
1106
1107 /* lfs uses its strategy routine only for read */
1108 KASSERT(bp->b_flags & B_READ);
1109
1110 if (vp->v_type == VBLK || vp->v_type == VCHR)
1111 panic("lfs_strategy: spec");
1112 KASSERT(bp->b_bcount != 0);
1113 if (bp->b_blkno == bp->b_lblkno) {
1114 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1115 NULL);
1116 if (error) {
1117 bp->b_error = error;
1118 bp->b_flags |= B_ERROR;
1119 biodone(bp);
1120 return (error);
1121 }
1122 if ((long)bp->b_blkno == -1) /* no valid data */
1123 clrbuf(bp);
1124 }
1125 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1126 biodone(bp);
1127 return (0);
1128 }
1129
1130 slept = 1;
1131 simple_lock(&fs->lfs_interlock);
1132 while (slept && fs->lfs_seglock) {
1133 simple_unlock(&fs->lfs_interlock);
1134 /*
1135 * Look through list of intervals.
1136 * There will only be intervals to look through
1137 * if the cleaner holds the seglock.
1138 * Since the cleaner is synchronous, we can trust
1139 * the list of intervals to be current.
1140 */
1141 tbn = dbtofsb(fs, bp->b_blkno);
1142 sn = dtosn(fs, tbn);
1143 slept = 0;
1144 for (i = 0; i < fs->lfs_cleanind; i++) {
1145 if (sn == dtosn(fs, fs->lfs_cleanint[i]) &&
1146 tbn >= fs->lfs_cleanint[i]) {
1147 DLOG((DLOG_CLEAN,
1148 "lfs_strategy: ino %d lbn %" PRId64
1149 " ind %d sn %d fsb %" PRIx32
1150 " given sn %d fsb %" PRIx64 "\n",
1151 ip->i_number, bp->b_lblkno, i,
1152 dtosn(fs, fs->lfs_cleanint[i]),
1153 fs->lfs_cleanint[i], sn, tbn));
1154 DLOG((DLOG_CLEAN,
1155 "lfs_strategy: sleeping on ino %d lbn %"
1156 PRId64 "\n", ip->i_number, bp->b_lblkno));
1157 simple_lock(&fs->lfs_interlock);
1158 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
1159 /* Cleaner can't wait for itself */
1160 ltsleep(&fs->lfs_iocount,
1161 (PRIBIO + 1) | PNORELOCK,
1162 "clean2", 0,
1163 &fs->lfs_interlock);
1164 slept = 1;
1165 break;
1166 } else if (fs->lfs_seglock) {
1167 ltsleep(&fs->lfs_seglock,
1168 (PRIBIO + 1) | PNORELOCK,
1169 "clean1", 0,
1170 &fs->lfs_interlock);
1171 slept = 1;
1172 break;
1173 }
1174 simple_unlock(&fs->lfs_interlock);
1175 }
1176 }
1177 simple_lock(&fs->lfs_interlock);
1178 }
1179 simple_unlock(&fs->lfs_interlock);
1180
1181 vp = ip->i_devvp;
1182 VOP_STRATEGY(vp, bp);
1183 return (0);
1184 }
1185
1186 void
1187 lfs_flush_dirops(struct lfs *fs)
1188 {
1189 struct inode *ip, *nip;
1190 struct vnode *vp;
1191 extern int lfs_dostats;
1192 struct segment *sp;
1193 int waslocked;
1194
1195 ASSERT_MAYBE_SEGLOCK(fs);
1196 KASSERT(fs->lfs_nadirop == 0);
1197
1198 if (fs->lfs_ronly)
1199 return;
1200
1201 simple_lock(&fs->lfs_interlock);
1202 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
1203 simple_unlock(&fs->lfs_interlock);
1204 return;
1205 } else
1206 simple_unlock(&fs->lfs_interlock);
1207
1208 if (lfs_dostats)
1209 ++lfs_stats.flush_invoked;
1210
1211 /*
1212 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1213 * Technically this is a checkpoint (the on-disk state is valid)
1214 * even though we are leaving out all the file data.
1215 */
1216 lfs_imtime(fs);
1217 lfs_seglock(fs, SEGM_CKP);
1218 sp = fs->lfs_sp;
1219
1220 /*
1221 * lfs_writevnodes, optimized to get dirops out of the way.
1222 * Only write dirops, and don't flush files' pages, only
1223 * blocks from the directories.
1224 *
1225 * We don't need to vref these files because they are
1226 * dirops and so hold an extra reference until the
1227 * segunlock clears them of that status.
1228 *
1229 * We don't need to check for IN_ADIROP because we know that
1230 * no dirops are active.
1231 *
1232 */
1233 simple_lock(&fs->lfs_interlock);
1234 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1235 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1236 simple_unlock(&fs->lfs_interlock);
1237 vp = ITOV(ip);
1238
1239 KASSERT((ip->i_flag & IN_ADIROP) == 0);
1240
1241 /*
1242 * All writes to directories come from dirops; all
1243 * writes to files' direct blocks go through the page
1244 * cache, which we're not touching. Reads to files
1245 * and/or directories will not be affected by writing
1246 * directory blocks inodes and file inodes. So we don't
1247 * really need to lock. If we don't lock, though,
1248 * make sure that we don't clear IN_MODIFIED
1249 * unnecessarily.
1250 */
1251 if (vp->v_flag & (VXLOCK | VFREEING)) {
1252 simple_lock(&fs->lfs_interlock);
1253 continue;
1254 }
1255 waslocked = VOP_ISLOCKED(vp);
1256 if (vp->v_type != VREG &&
1257 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1258 lfs_writefile(fs, sp, vp);
1259 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1260 !(ip->i_flag & IN_ALLMOD)) {
1261 LFS_SET_UINO(ip, IN_MODIFIED);
1262 }
1263 }
1264 (void) lfs_writeinode(fs, sp, ip);
1265 if (waslocked)
1266 LFS_SET_UINO(ip, IN_MODIFIED);
1267 simple_lock(&fs->lfs_interlock);
1268 }
1269 simple_unlock(&fs->lfs_interlock);
1270 /* We've written all the dirops there are */
1271 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
1272 lfs_finalize_fs_seguse(fs);
1273 (void) lfs_writeseg(fs, sp);
1274 lfs_segunlock(fs);
1275 }
1276
1277 /*
1278 * Flush all vnodes for which the pagedaemon has requested pageouts.
1279 * Skip over any files that are marked VDIROP (since lfs_flush_dirop()
1280 * has just run, this would be an error). If we have to skip a vnode
1281 * for any reason, just skip it; if we have to wait for the cleaner,
1282 * abort. The writer daemon will call us again later.
1283 */
1284 void
1285 lfs_flush_pchain(struct lfs *fs)
1286 {
1287 struct inode *ip, *nip;
1288 struct vnode *vp;
1289 extern int lfs_dostats;
1290 struct segment *sp;
1291 int error;
1292
1293 ASSERT_NO_SEGLOCK(fs);
1294
1295 if (fs->lfs_ronly)
1296 return;
1297
1298 simple_lock(&fs->lfs_interlock);
1299 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1300 simple_unlock(&fs->lfs_interlock);
1301 return;
1302 } else
1303 simple_unlock(&fs->lfs_interlock);
1304
1305 /* Get dirops out of the way */
1306 lfs_flush_dirops(fs);
1307
1308 if (lfs_dostats)
1309 ++lfs_stats.flush_invoked;
1310
1311 /*
1312 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1313 */
1314 lfs_imtime(fs);
1315 lfs_seglock(fs, 0);
1316 sp = fs->lfs_sp;
1317
1318 /*
1319 * lfs_writevnodes, optimized to clear pageout requests.
1320 * Only write non-dirop files that are in the pageout queue.
1321 * We're very conservative about what we write; we want to be
1322 * fast and async.
1323 */
1324 simple_lock(&fs->lfs_interlock);
1325 top:
1326 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1327 nip = TAILQ_NEXT(ip, i_lfs_pchain);
1328 vp = ITOV(ip);
1329
1330 if (!(ip->i_flags & IN_PAGING))
1331 goto top;
1332
1333 if (vp->v_flag & (VXLOCK|VDIROP))
1334 continue;
1335 if (vp->v_type != VREG)
1336 continue;
1337 if (lfs_vref(vp))
1338 continue;
1339 simple_unlock(&fs->lfs_interlock);
1340
1341 if (VOP_ISLOCKED(vp)) {
1342 lfs_vunref(vp);
1343 simple_lock(&fs->lfs_interlock);
1344 continue;
1345 }
1346
1347 error = lfs_writefile(fs, sp, vp);
1348 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1349 !(ip->i_flag & IN_ALLMOD)) {
1350 LFS_SET_UINO(ip, IN_MODIFIED);
1351 }
1352 (void) lfs_writeinode(fs, sp, ip);
1353
1354 lfs_vunref(vp);
1355
1356 if (error == EAGAIN) {
1357 lfs_writeseg(fs, sp);
1358 simple_lock(&fs->lfs_interlock);
1359 break;
1360 }
1361 simple_lock(&fs->lfs_interlock);
1362 }
1363 simple_unlock(&fs->lfs_interlock);
1364 (void) lfs_writeseg(fs, sp);
1365 lfs_segunlock(fs);
1366 }
1367
1368 /*
1369 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1370 */
1371 int
1372 lfs_fcntl(void *v)
1373 {
1374 struct vop_fcntl_args /* {
1375 struct vnode *a_vp;
1376 u_long a_command;
1377 caddr_t a_data;
1378 int a_fflag;
1379 kauth_cred_t a_cred;
1380 struct lwp *a_l;
1381 } */ *ap = v;
1382 struct timeval *tvp;
1383 BLOCK_INFO *blkiov;
1384 CLEANERINFO *cip;
1385 SEGUSE *sup;
1386 int blkcnt, error, oclean;
1387 size_t fh_size;
1388 struct lfs_fcntl_markv blkvp;
1389 struct proc *p;
1390 fsid_t *fsidp;
1391 struct lfs *fs;
1392 struct buf *bp;
1393 fhandle_t *fhp;
1394 daddr_t off;
1395
1396 /* Only respect LFS fcntls on fs root or Ifile */
1397 if (VTOI(ap->a_vp)->i_number != ROOTINO &&
1398 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1399 return ufs_fcntl(v);
1400 }
1401
1402 /* Avoid locking a draining lock */
1403 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1404 return ESHUTDOWN;
1405 }
1406
1407 p = ap->a_l->l_proc;
1408 fs = VTOI(ap->a_vp)->i_lfs;
1409 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1410
1411 switch (ap->a_command) {
1412 case LFCNSEGWAITALL:
1413 case LFCNSEGWAITALL_COMPAT:
1414 fsidp = NULL;
1415 /* FALLSTHROUGH */
1416 case LFCNSEGWAIT:
1417 case LFCNSEGWAIT_COMPAT:
1418 tvp = (struct timeval *)ap->a_data;
1419 simple_lock(&fs->lfs_interlock);
1420 ++fs->lfs_sleepers;
1421 simple_unlock(&fs->lfs_interlock);
1422
1423 error = lfs_segwait(fsidp, tvp);
1424
1425 simple_lock(&fs->lfs_interlock);
1426 if (--fs->lfs_sleepers == 0)
1427 wakeup(&fs->lfs_sleepers);
1428 simple_unlock(&fs->lfs_interlock);
1429 return error;
1430
1431 case LFCNBMAPV:
1432 case LFCNMARKV:
1433 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
1434 &p->p_acflag)) != 0)
1435 return (error);
1436 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1437
1438 blkcnt = blkvp.blkcnt;
1439 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1440 return (EINVAL);
1441 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1442 if ((error = copyin(blkvp.blkiov, blkiov,
1443 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1444 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1445 return error;
1446 }
1447
1448 simple_lock(&fs->lfs_interlock);
1449 ++fs->lfs_sleepers;
1450 simple_unlock(&fs->lfs_interlock);
1451 if (ap->a_command == LFCNBMAPV)
1452 error = lfs_bmapv(p, fsidp, blkiov, blkcnt);
1453 else /* LFCNMARKV */
1454 error = lfs_markv(p, fsidp, blkiov, blkcnt);
1455 if (error == 0)
1456 error = copyout(blkiov, blkvp.blkiov,
1457 blkcnt * sizeof(BLOCK_INFO));
1458 simple_lock(&fs->lfs_interlock);
1459 if (--fs->lfs_sleepers == 0)
1460 wakeup(&fs->lfs_sleepers);
1461 simple_unlock(&fs->lfs_interlock);
1462 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1463 return error;
1464
1465 case LFCNRECLAIM:
1466 /*
1467 * Flush dirops and write Ifile, allowing empty segments
1468 * to be immediately reclaimed.
1469 */
1470 lfs_writer_enter(fs, "pndirop");
1471 off = fs->lfs_offset;
1472 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1473 lfs_flush_dirops(fs);
1474 LFS_CLEANERINFO(cip, fs, bp);
1475 oclean = cip->clean;
1476 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1477 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1478 fs->lfs_sp->seg_flags |= SEGM_PROT;
1479 lfs_segunlock(fs);
1480 lfs_writer_leave(fs);
1481
1482 #ifdef DEBUG
1483 LFS_CLEANERINFO(cip, fs, bp);
1484 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
1485 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
1486 fs->lfs_offset - off, cip->clean - oclean,
1487 fs->lfs_activesb));
1488 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
1489 #endif
1490
1491 return 0;
1492
1493 #ifdef COMPAT_30
1494 case LFCNIFILEFH_COMPAT:
1495 /* Return the filehandle of the Ifile */
1496 if ((error = kauth_authorize_generic(ap->a_l->l_proc->p_cred,
1497 KAUTH_GENERIC_ISSUSER,
1498 &ap->a_l->l_proc->p_acflag)) != 0)
1499 return (error);
1500 fhp = (struct fhandle *)ap->a_data;
1501 fhp->fh_fsid = *fsidp;
1502 fh_size = 16; /* former VFS_MAXFIDSIZ */
1503 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1504 #endif
1505
1506 case LFCNIFILEFH:
1507 /* Return the filehandle of the Ifile */
1508 if ((error = kauth_authorize_generic(ap->a_l->l_proc->p_cred,
1509 KAUTH_GENERIC_ISSUSER,
1510 &ap->a_l->l_proc->p_acflag)) != 0)
1511 return (error);
1512 fhp = (struct fhandle *)ap->a_data;
1513 fhp->fh_fsid = *fsidp;
1514 fh_size = sizeof(union lfs_fhandle) -
1515 offsetof(fhandle_t, fh_fid);
1516 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1517
1518 case LFCNREWIND:
1519 /* Move lfs_offset to the lowest-numbered segment */
1520 return lfs_rewind(fs, *(int *)ap->a_data);
1521
1522 case LFCNINVAL:
1523 /* Mark a segment SEGUSE_INVAL */
1524 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
1525 if (sup->su_nbytes > 0) {
1526 brelse(bp);
1527 lfs_unset_inval_all(fs);
1528 return EBUSY;
1529 }
1530 sup->su_flags |= SEGUSE_INVAL;
1531 VOP_BWRITE(bp);
1532 return 0;
1533
1534 case LFCNRESIZE:
1535 /* Resize the filesystem */
1536 return lfs_resize_fs(fs, *(int *)ap->a_data);
1537
1538 case LFCNWRAPSTOP:
1539 case LFCNWRAPSTOP_COMPAT:
1540 /*
1541 * Hold lfs_newseg at segment 0; if requested, sleep until
1542 * the filesystem wraps around. To support external agents
1543 * (dump, fsck-based regression test) that need to look at
1544 * a snapshot of the filesystem, without necessarily
1545 * requiring that all fs activity stops.
1546 */
1547 if (VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPBLOCK)
1548 return EALREADY;
1549
1550 simple_lock(&fs->lfs_interlock);
1551 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPBLOCK;
1552 if (fs->lfs_nowrap == 0)
1553 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
1554 ++fs->lfs_nowrap;
1555 if (*(int *)ap->a_data == 1 ||
1556 ap->a_command == LFCNWRAPSTOP_COMPAT) {
1557 error = ltsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1558 "segwrap", 0, &fs->lfs_interlock);
1559 if (error) {
1560 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
1561 }
1562 }
1563 simple_unlock(&fs->lfs_interlock);
1564 return 0;
1565
1566 case LFCNWRAPGO:
1567 case LFCNWRAPGO_COMPAT:
1568 /*
1569 * Having done its work, the agent wakes up the writer.
1570 * If the argument is 1, it sleeps until a new segment
1571 * is selected.
1572 */
1573 simple_lock(&fs->lfs_interlock);
1574 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
1575 (ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
1576 *((int *)ap->a_data)));
1577 simple_unlock(&fs->lfs_interlock);
1578 return error;
1579
1580 default:
1581 return ufs_fcntl(v);
1582 }
1583 return 0;
1584 }
1585
1586 int
1587 lfs_getpages(void *v)
1588 {
1589 struct vop_getpages_args /* {
1590 struct vnode *a_vp;
1591 voff_t a_offset;
1592 struct vm_page **a_m;
1593 int *a_count;
1594 int a_centeridx;
1595 vm_prot_t a_access_type;
1596 int a_advice;
1597 int a_flags;
1598 } */ *ap = v;
1599
1600 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
1601 (ap->a_access_type & VM_PROT_WRITE) != 0) {
1602 return EPERM;
1603 }
1604 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
1605 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
1606 }
1607
1608 /*
1609 * we're relying on the fact that genfs_getpages() always read in
1610 * entire filesystem blocks.
1611 */
1612 return genfs_getpages(v);
1613 }
1614
1615 /*
1616 * Make sure that for all pages in every block in the given range,
1617 * either all are dirty or all are clean. If any of the pages
1618 * we've seen so far are dirty, put the vnode on the paging chain,
1619 * and mark it IN_PAGING.
1620 *
1621 * If checkfirst != 0, don't check all the pages but return at the
1622 * first dirty page.
1623 */
1624 static int
1625 check_dirty(struct lfs *fs, struct vnode *vp,
1626 off_t startoffset, off_t endoffset, off_t blkeof,
1627 int flags, int checkfirst)
1628 {
1629 int by_list;
1630 struct vm_page *curpg = NULL; /* XXX: gcc */
1631 struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
1632 off_t soff = 0; /* XXX: gcc */
1633 voff_t off;
1634 int i;
1635 int nonexistent;
1636 int any_dirty; /* number of dirty pages */
1637 int dirty; /* number of dirty pages in a block */
1638 int tdirty;
1639 int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
1640 int pagedaemon = (curproc == uvm.pagedaemon_proc);
1641
1642 ASSERT_MAYBE_SEGLOCK(fs);
1643 top:
1644 by_list = (vp->v_uobj.uo_npages <=
1645 ((endoffset - startoffset) >> PAGE_SHIFT) *
1646 UVM_PAGE_HASH_PENALTY);
1647 any_dirty = 0;
1648
1649 if (by_list) {
1650 curpg = TAILQ_FIRST(&vp->v_uobj.memq);
1651 } else {
1652 soff = startoffset;
1653 }
1654 while (by_list || soff < MIN(blkeof, endoffset)) {
1655 if (by_list) {
1656 /*
1657 * Find the first page in a block. Skip
1658 * blocks outside our area of interest or beyond
1659 * the end of file.
1660 */
1661 if (pages_per_block > 1) {
1662 while (curpg &&
1663 ((curpg->offset & fs->lfs_bmask) ||
1664 curpg->offset >= vp->v_size ||
1665 curpg->offset >= endoffset))
1666 curpg = TAILQ_NEXT(curpg, listq);
1667 }
1668 if (curpg == NULL)
1669 break;
1670 soff = curpg->offset;
1671 }
1672
1673 /*
1674 * Mark all pages in extended range busy; find out if any
1675 * of them are dirty.
1676 */
1677 nonexistent = dirty = 0;
1678 for (i = 0; i == 0 || i < pages_per_block; i++) {
1679 if (by_list && pages_per_block <= 1) {
1680 pgs[i] = pg = curpg;
1681 } else {
1682 off = soff + (i << PAGE_SHIFT);
1683 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
1684 if (pg == NULL) {
1685 ++nonexistent;
1686 continue;
1687 }
1688 }
1689 KASSERT(pg != NULL);
1690
1691 /*
1692 * If we're holding the segment lock, we can deadlock
1693 * against a process that has our page and is waiting
1694 * for the cleaner, while the cleaner waits for the
1695 * segment lock. Just bail in that case.
1696 */
1697 if ((pg->flags & PG_BUSY) &&
1698 (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
1699 if (by_list && i > 0)
1700 uvm_page_unbusy(pgs, i);
1701 DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
1702 return -1;
1703 }
1704
1705 while (pg->flags & PG_BUSY) {
1706 pg->flags |= PG_WANTED;
1707 UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
1708 "lfsput", 0);
1709 simple_lock(&vp->v_interlock);
1710 if (by_list) {
1711 if (i > 0)
1712 uvm_page_unbusy(pgs, i);
1713 goto top;
1714 }
1715 }
1716 pg->flags |= PG_BUSY;
1717 UVM_PAGE_OWN(pg, "lfs_putpages");
1718
1719 pmap_page_protect(pg, VM_PROT_NONE);
1720 tdirty = (pmap_clear_modify(pg) ||
1721 (pg->flags & PG_CLEAN) == 0);
1722 dirty += tdirty;
1723 }
1724 if (pages_per_block > 0 && nonexistent >= pages_per_block) {
1725 if (by_list) {
1726 curpg = TAILQ_NEXT(curpg, listq);
1727 } else {
1728 soff += fs->lfs_bsize;
1729 }
1730 continue;
1731 }
1732
1733 any_dirty += dirty;
1734 KASSERT(nonexistent == 0);
1735
1736 /*
1737 * If any are dirty make all dirty; unbusy them,
1738 * but if we were asked to clean, wire them so that
1739 * the pagedaemon doesn't bother us about them while
1740 * they're on their way to disk.
1741 */
1742 for (i = 0; i == 0 || i < pages_per_block; i++) {
1743 pg = pgs[i];
1744 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
1745 if (dirty) {
1746 pg->flags &= ~PG_CLEAN;
1747 if (flags & PGO_FREE) {
1748 /*
1749 * Wire the page so that
1750 * pdaemon doesn't see it again.
1751 */
1752 uvm_lock_pageq();
1753 uvm_pagewire(pg);
1754 uvm_unlock_pageq();
1755
1756 /* Suspended write flag */
1757 pg->flags |= PG_DELWRI;
1758 }
1759 }
1760 if (pg->flags & PG_WANTED)
1761 wakeup(pg);
1762 pg->flags &= ~(PG_WANTED|PG_BUSY);
1763 UVM_PAGE_OWN(pg, NULL);
1764 }
1765
1766 if (checkfirst && any_dirty)
1767 break;
1768
1769 if (by_list) {
1770 curpg = TAILQ_NEXT(curpg, listq);
1771 } else {
1772 soff += MAX(PAGE_SIZE, fs->lfs_bsize);
1773 }
1774 }
1775
1776 return any_dirty;
1777 }
1778
1779 /*
1780 * lfs_putpages functions like genfs_putpages except that
1781 *
1782 * (1) It needs to bounds-check the incoming requests to ensure that
1783 * they are block-aligned; if they are not, expand the range and
1784 * do the right thing in case, e.g., the requested range is clean
1785 * but the expanded range is dirty.
1786 *
1787 * (2) It needs to explicitly send blocks to be written when it is done.
1788 * VOP_PUTPAGES is not ever called with the seglock held, so
1789 * we simply take the seglock and let lfs_segunlock wait for us.
1790 * XXX Actually we can be called with the seglock held, if we have
1791 * XXX to flush a vnode while lfs_markv is in operation. As of this
1792 * XXX writing we panic in this case.
1793 *
1794 * Assumptions:
1795 *
1796 * (1) The caller does not hold any pages in this vnode busy. If it does,
1797 * there is a danger that when we expand the page range and busy the
1798 * pages we will deadlock.
1799 *
1800 * (2) We are called with vp->v_interlock held; we must return with it
1801 * released.
1802 *
1803 * (3) We don't absolutely have to free pages right away, provided that
1804 * the request does not have PGO_SYNCIO. When the pagedaemon gives
1805 * us a request with PGO_FREE, we take the pages out of the paging
1806 * queue and wake up the writer, which will handle freeing them for us.
1807 *
1808 * We ensure that for any filesystem block, all pages for that
1809 * block are either resident or not, even if those pages are higher
1810 * than EOF; that means that we will be getting requests to free
1811 * "unused" pages above EOF all the time, and should ignore them.
1812 *
1813 * (4) If we are called with PGO_LOCKED, the finfo array we are to write
1814 * into has been set up for us by lfs_writefile. If not, we will
1815 * have to handle allocating and/or freeing an finfo entry.
1816 *
1817 * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
1818 */
1819
1820 int
1821 lfs_putpages(void *v)
1822 {
1823 int error;
1824 struct vop_putpages_args /* {
1825 struct vnode *a_vp;
1826 voff_t a_offlo;
1827 voff_t a_offhi;
1828 int a_flags;
1829 } */ *ap = v;
1830 struct vnode *vp;
1831 struct inode *ip;
1832 struct lfs *fs;
1833 struct segment *sp;
1834 off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
1835 off_t off, max_endoffset;
1836 int s;
1837 boolean_t seglocked, sync, pagedaemon;
1838 struct vm_page *pg;
1839 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
1840
1841 vp = ap->a_vp;
1842 ip = VTOI(vp);
1843 fs = ip->i_lfs;
1844 sync = (ap->a_flags & PGO_SYNCIO) != 0;
1845 pagedaemon = (curproc == uvm.pagedaemon_proc);
1846
1847 /* Putpages does nothing for metadata. */
1848 if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
1849 simple_unlock(&vp->v_interlock);
1850 return 0;
1851 }
1852
1853 /*
1854 * If there are no pages, don't do anything.
1855 */
1856 if (vp->v_uobj.uo_npages == 0) {
1857 s = splbio();
1858 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
1859 (vp->v_flag & VONWORKLST)) {
1860 vp->v_flag &= ~VONWORKLST;
1861 LIST_REMOVE(vp, v_synclist);
1862 }
1863 splx(s);
1864 simple_unlock(&vp->v_interlock);
1865
1866 /* Remove us from paging queue, if we were on it */
1867 simple_lock(&fs->lfs_interlock);
1868 if (ip->i_flags & IN_PAGING) {
1869 ip->i_flags &= ~IN_PAGING;
1870 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1871 }
1872 simple_unlock(&fs->lfs_interlock);
1873 return 0;
1874 }
1875
1876 blkeof = blkroundup(fs, ip->i_size);
1877
1878 /*
1879 * Ignore requests to free pages past EOF but in the same block
1880 * as EOF, unless the request is synchronous. (If the request is
1881 * sync, it comes from lfs_truncate.)
1882 * XXXUBC Make these pages look "active" so the pagedaemon won't
1883 * XXXUBC bother us with them again.
1884 */
1885 if (!sync && ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
1886 origoffset = ap->a_offlo;
1887 for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
1888 pg = uvm_pagelookup(&vp->v_uobj, off);
1889 KASSERT(pg != NULL);
1890 while (pg->flags & PG_BUSY) {
1891 pg->flags |= PG_WANTED;
1892 UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
1893 "lfsput2", 0);
1894 simple_lock(&vp->v_interlock);
1895 }
1896 uvm_lock_pageq();
1897 uvm_pageactivate(pg);
1898 uvm_unlock_pageq();
1899 }
1900 ap->a_offlo = blkeof;
1901 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
1902 simple_unlock(&vp->v_interlock);
1903 return 0;
1904 }
1905 }
1906
1907 /*
1908 * Extend page range to start and end at block boundaries.
1909 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
1910 */
1911 origoffset = ap->a_offlo;
1912 origendoffset = ap->a_offhi;
1913 startoffset = origoffset & ~(fs->lfs_bmask);
1914 max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
1915 << fs->lfs_bshift;
1916
1917 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
1918 endoffset = max_endoffset;
1919 origendoffset = endoffset;
1920 } else {
1921 origendoffset = round_page(ap->a_offhi);
1922 endoffset = round_page(blkroundup(fs, origendoffset));
1923 }
1924
1925 KASSERT(startoffset > 0 || endoffset >= startoffset);
1926 if (startoffset == endoffset) {
1927 /* Nothing to do, why were we called? */
1928 simple_unlock(&vp->v_interlock);
1929 DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
1930 PRId64 "\n", startoffset));
1931 return 0;
1932 }
1933
1934 ap->a_offlo = startoffset;
1935 ap->a_offhi = endoffset;
1936
1937 if (!(ap->a_flags & PGO_CLEANIT))
1938 return genfs_putpages(v);
1939
1940 /*
1941 * If there are more than one page per block, we don't want
1942 * to get caught locking them backwards; so set PGO_BUSYFAIL
1943 * to avoid deadlocks.
1944 */
1945 ap->a_flags |= PGO_BUSYFAIL;
1946
1947 do {
1948 int r;
1949
1950 /* If no pages are dirty, we can just use genfs_putpages. */
1951 r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
1952 ap->a_flags, 1);
1953 if (r < 0) {
1954 simple_unlock(&vp->v_interlock);
1955 return EDEADLK;
1956 }
1957 if (r > 0)
1958 break;
1959
1960 /*
1961 * Sometimes pages are dirtied between the time that
1962 * we check and the time we try to clean them.
1963 * Instruct lfs_gop_write to return EDEADLK in this case
1964 * so we can write them properly.
1965 */
1966 ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
1967 r = genfs_putpages(v);
1968 ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
1969 if (r != EDEADLK)
1970 return r;
1971
1972 /* Start over. */
1973 preempt(1);
1974 simple_lock(&vp->v_interlock);
1975 } while(1);
1976
1977 /*
1978 * Dirty and asked to clean.
1979 *
1980 * Pagedaemon can't actually write LFS pages; wake up
1981 * the writer to take care of that. The writer will
1982 * notice the pager inode queue and act on that.
1983 */
1984 if (pagedaemon) {
1985 simple_lock(&fs->lfs_interlock);
1986 if (!(ip->i_flags & IN_PAGING)) {
1987 ip->i_flags |= IN_PAGING;
1988 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1989 }
1990 simple_lock(&lfs_subsys_lock);
1991 wakeup(&lfs_writer_daemon);
1992 simple_unlock(&lfs_subsys_lock);
1993 simple_unlock(&fs->lfs_interlock);
1994 simple_unlock(&vp->v_interlock);
1995 preempt(1);
1996 return EWOULDBLOCK;
1997 }
1998
1999 /*
2000 * If this is a file created in a recent dirop, we can't flush its
2001 * inode until the dirop is complete. Drain dirops, then flush the
2002 * filesystem (taking care of any other pending dirops while we're
2003 * at it).
2004 */
2005 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
2006 (vp->v_flag & VDIROP)) {
2007 int locked;
2008
2009 DLOG((DLOG_PAGE, "lfs_putpages: flushing VDIROP\n"));
2010 locked = VOP_ISLOCKED(vp) && /* XXX */
2011 vp->v_lock.lk_lockholder == curproc->p_pid;
2012 simple_unlock(&vp->v_interlock);
2013 lfs_writer_enter(fs, "ppdirop");
2014 if (locked)
2015 VOP_UNLOCK(vp, 0);
2016
2017 simple_lock(&fs->lfs_interlock);
2018 lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
2019 simple_unlock(&fs->lfs_interlock);
2020
2021 simple_lock(&vp->v_interlock);
2022 if (locked) {
2023 VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2024 simple_lock(&vp->v_interlock);
2025 }
2026 lfs_writer_leave(fs);
2027
2028 /* XXX the flush should have taken care of this one too! */
2029 }
2030
2031 /*
2032 * This is it. We are going to write some pages. From here on
2033 * down it's all just mechanics.
2034 *
2035 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
2036 */
2037 ap->a_flags &= ~PGO_SYNCIO;
2038
2039 /*
2040 * If we've already got the seglock, flush the node and return.
2041 * The FIP has already been set up for us by lfs_writefile,
2042 * and FIP cleanup and lfs_updatemeta will also be done there,
2043 * unless genfs_putpages returns EDEADLK; then we must flush
2044 * what we have, and correct FIP and segment header accounting.
2045 */
2046 get_seglock:
2047 seglocked = (ap->a_flags & PGO_LOCKED) != 0;
2048 if (!seglocked) {
2049 simple_unlock(&vp->v_interlock);
2050 /*
2051 * Take the seglock, because we are going to be writing pages.
2052 */
2053 error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
2054 if (error != 0)
2055 return error;
2056 simple_lock(&vp->v_interlock);
2057 }
2058
2059 /*
2060 * VOP_PUTPAGES should not be called while holding the seglock.
2061 * XXXUBC fix lfs_markv, or do this properly.
2062 */
2063 #ifdef notyet
2064 KASSERT(fs->lfs_seglock == 1);
2065 #endif /* notyet */
2066
2067 /*
2068 * We assume we're being called with sp->fip pointing at blank space.
2069 * Account for a new FIP in the segment header, and set sp->vp.
2070 * (This should duplicate the setup at the top of lfs_writefile().)
2071 */
2072 sp = fs->lfs_sp;
2073 if (!seglocked)
2074 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2075 KASSERT(sp->vp == NULL);
2076 sp->vp = vp;
2077
2078 if (!seglocked) {
2079 if (vp->v_flag & VDIROP)
2080 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
2081 }
2082
2083 /*
2084 * Loop through genfs_putpages until all pages are gathered.
2085 * genfs_putpages() drops the interlock, so reacquire it if necessary.
2086 * Whenever we lose the interlock we have to rerun check_dirty, as
2087 * well.
2088 */
2089 again:
2090 if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
2091 ap->a_flags, 0) < 0) {
2092 simple_unlock(&vp->v_interlock);
2093 sp->vp = NULL;
2094 if (!seglocked) {
2095 lfs_release_finfo(fs);
2096 lfs_segunlock(fs);
2097 }
2098 if (pagedaemon)
2099 return EDEADLK;
2100 /* else seglocked == 0 */
2101 preempt(1);
2102 simple_lock(&vp->v_interlock);
2103 goto get_seglock;
2104 }
2105
2106 error = genfs_putpages(v);
2107 if (error == EDEADLK || error == EAGAIN) {
2108 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
2109 " EDEADLK [2] ino %d off %x (seg %d)\n",
2110 ip->i_number, fs->lfs_offset,
2111 dtosn(fs, fs->lfs_offset)));
2112 /* If nothing to write, short-circuit */
2113 if (sp->cbpp - sp->bpp > 1) {
2114 /* Write gathered pages */
2115 lfs_updatemeta(sp);
2116 lfs_release_finfo(fs);
2117 (void) lfs_writeseg(fs, sp);
2118
2119 /*
2120 * Reinitialize brand new FIP and add us to it.
2121 */
2122 KASSERT(sp->vp == vp);
2123 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2124 }
2125
2126 /* Give the write a chance to complete */
2127 preempt(1);
2128
2129 /* We've lost the interlock. Start over. */
2130 if (error == EDEADLK) {
2131 simple_lock(&vp->v_interlock);
2132 goto again;
2133 }
2134 }
2135
2136 KASSERT(sp->vp == vp);
2137 if (!seglocked) {
2138 sp->vp = NULL;
2139
2140 /* Write indirect blocks as well */
2141 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
2142 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
2143 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
2144
2145 KASSERT(sp->vp == NULL);
2146 sp->vp = vp;
2147 }
2148
2149 /*
2150 * Blocks are now gathered into a segment waiting to be written.
2151 * All that's left to do is update metadata, and write them.
2152 */
2153 lfs_updatemeta(sp);
2154 KASSERT(sp->vp == vp);
2155 sp->vp = NULL;
2156
2157 if (seglocked) {
2158 /* we're called by lfs_writefile. */
2159 return error;
2160 }
2161
2162 /* Clean up FIP and send it to disk. */
2163 lfs_release_finfo(fs);
2164 lfs_writeseg(fs, fs->lfs_sp);
2165
2166 /*
2167 * Remove us from paging queue, since we've now written all our
2168 * pages.
2169 */
2170 simple_lock(&fs->lfs_interlock);
2171 if (ip->i_flags & IN_PAGING) {
2172 ip->i_flags &= ~IN_PAGING;
2173 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2174 }
2175 simple_unlock(&fs->lfs_interlock);
2176
2177 /*
2178 * XXX - with the malloc/copy writeseg, the pages are freed by now
2179 * even if we don't wait (e.g. if we hold a nested lock). This
2180 * will not be true if we stop using malloc/copy.
2181 */
2182 KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
2183 lfs_segunlock(fs);
2184
2185 /*
2186 * Wait for v_numoutput to drop to zero. The seglock should
2187 * take care of this, but there is a slight possibility that
2188 * aiodoned might not have got around to our buffers yet.
2189 */
2190 if (sync) {
2191 s = splbio();
2192 simple_lock(&global_v_numoutput_slock);
2193 while (vp->v_numoutput > 0) {
2194 DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
2195 " num %d\n", ip->i_number, vp->v_numoutput));
2196 vp->v_flag |= VBWAIT;
2197 ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vn", 0,
2198 &global_v_numoutput_slock);
2199 }
2200 simple_unlock(&global_v_numoutput_slock);
2201 splx(s);
2202 }
2203 return error;
2204 }
2205
2206 /*
2207 * Return the last logical file offset that should be written for this file
2208 * if we're doing a write that ends at "size". If writing, we need to know
2209 * about sizes on disk, i.e. fragments if there are any; if reading, we need
2210 * to know about entire blocks.
2211 */
2212 void
2213 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
2214 {
2215 struct inode *ip = VTOI(vp);
2216 struct lfs *fs = ip->i_lfs;
2217 daddr_t olbn, nlbn;
2218
2219 olbn = lblkno(fs, ip->i_size);
2220 nlbn = lblkno(fs, size);
2221 if (!(flags & GOP_SIZE_MEM) && nlbn < NDADDR && olbn <= nlbn) {
2222 *eobp = fragroundup(fs, size);
2223 } else {
2224 *eobp = blkroundup(fs, size);
2225 }
2226 }
2227
2228 #ifdef DEBUG
2229 void lfs_dump_vop(void *);
2230
2231 void
2232 lfs_dump_vop(void *v)
2233 {
2234 struct vop_putpages_args /* {
2235 struct vnode *a_vp;
2236 voff_t a_offlo;
2237 voff_t a_offhi;
2238 int a_flags;
2239 } */ *ap = v;
2240
2241 #ifdef DDB
2242 vfs_vnode_print(ap->a_vp, 0, printf);
2243 #endif
2244 lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
2245 }
2246 #endif
2247
2248 int
2249 lfs_mmap(void *v)
2250 {
2251 struct vop_mmap_args /* {
2252 const struct vnodeop_desc *a_desc;
2253 struct vnode *a_vp;
2254 int a_fflags;
2255 kauth_cred_t a_cred;
2256 struct lwp *a_l;
2257 } */ *ap = v;
2258
2259 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
2260 return EOPNOTSUPP;
2261 return ufs_mmap(v);
2262 }
2263