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