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