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