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