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