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