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