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