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