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