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