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