lfs_vnops.c revision 1.255 1 /* $NetBSD: lfs_vnops.c,v 1.255 2013/07/28 04:24:44 pgoyette 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1986, 1989, 1991, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.255 2013/07/28 04:24:44 pgoyette Exp $");
64
65 #ifdef _KERNEL_OPT
66 #include "opt_compat_netbsd.h"
67 #include "opt_uvm_page_trkown.h"
68 #endif
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/namei.h>
73 #include <sys/resourcevar.h>
74 #include <sys/kernel.h>
75 #include <sys/file.h>
76 #include <sys/stat.h>
77 #include <sys/buf.h>
78 #include <sys/proc.h>
79 #include <sys/mount.h>
80 #include <sys/vnode.h>
81 #include <sys/pool.h>
82 #include <sys/signalvar.h>
83 #include <sys/kauth.h>
84 #include <sys/syslog.h>
85 #include <sys/fstrans.h>
86
87 #include <miscfs/fifofs/fifo.h>
88 #include <miscfs/genfs/genfs.h>
89 #include <miscfs/specfs/specdev.h>
90
91 #include <ufs/lfs/ulfs_inode.h>
92 #include <ufs/lfs/ulfsmount.h>
93 #include <ufs/lfs/ulfs_bswap.h>
94 #include <ufs/lfs/ulfs_extern.h>
95
96 #include <uvm/uvm.h>
97 #include <uvm/uvm_pmap.h>
98 #include <uvm/uvm_stat.h>
99 #include <uvm/uvm_pager.h>
100
101 #include <ufs/lfs/lfs.h>
102 #include <ufs/lfs/lfs_kernel.h>
103 #include <ufs/lfs/lfs_extern.h>
104
105 extern pid_t lfs_writer_daemon;
106 int lfs_ignore_lazy_sync = 1;
107
108 static int lfs_openextattr(void *v);
109 static int lfs_closeextattr(void *v);
110 static int lfs_getextattr(void *v);
111 static int lfs_setextattr(void *v);
112 static int lfs_listextattr(void *v);
113 static int lfs_deleteextattr(void *v);
114
115 /* Global vfs data structures for lfs. */
116 int (**lfs_vnodeop_p)(void *);
117 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
118 { &vop_default_desc, vn_default_error },
119 { &vop_lookup_desc, ulfs_lookup }, /* lookup */
120 { &vop_create_desc, lfs_create }, /* create */
121 { &vop_whiteout_desc, ulfs_whiteout }, /* whiteout */
122 { &vop_mknod_desc, lfs_mknod }, /* mknod */
123 { &vop_open_desc, ulfs_open }, /* open */
124 { &vop_close_desc, lfs_close }, /* close */
125 { &vop_access_desc, ulfs_access }, /* access */
126 { &vop_getattr_desc, lfs_getattr }, /* getattr */
127 { &vop_setattr_desc, lfs_setattr }, /* setattr */
128 { &vop_read_desc, lfs_read }, /* read */
129 { &vop_write_desc, lfs_write }, /* write */
130 { &vop_ioctl_desc, ulfs_ioctl }, /* ioctl */
131 { &vop_fcntl_desc, lfs_fcntl }, /* fcntl */
132 { &vop_poll_desc, ulfs_poll }, /* poll */
133 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
134 { &vop_revoke_desc, ulfs_revoke }, /* revoke */
135 { &vop_mmap_desc, lfs_mmap }, /* mmap */
136 { &vop_fsync_desc, lfs_fsync }, /* fsync */
137 { &vop_seek_desc, ulfs_seek }, /* seek */
138 { &vop_remove_desc, lfs_remove }, /* remove */
139 { &vop_link_desc, lfs_link }, /* link */
140 { &vop_rename_desc, lfs_rename }, /* rename */
141 { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
142 { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
143 { &vop_symlink_desc, lfs_symlink }, /* symlink */
144 { &vop_readdir_desc, ulfs_readdir }, /* readdir */
145 { &vop_readlink_desc, ulfs_readlink }, /* readlink */
146 { &vop_abortop_desc, ulfs_abortop }, /* abortop */
147 { &vop_inactive_desc, lfs_inactive }, /* inactive */
148 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
149 { &vop_lock_desc, ulfs_lock }, /* lock */
150 { &vop_unlock_desc, ulfs_unlock }, /* unlock */
151 { &vop_bmap_desc, ulfs_bmap }, /* bmap */
152 { &vop_strategy_desc, lfs_strategy }, /* strategy */
153 { &vop_print_desc, ulfs_print }, /* print */
154 { &vop_islocked_desc, ulfs_islocked }, /* islocked */
155 { &vop_pathconf_desc, ulfs_pathconf }, /* pathconf */
156 { &vop_advlock_desc, ulfs_advlock }, /* advlock */
157 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
158 { &vop_getpages_desc, lfs_getpages }, /* getpages */
159 { &vop_putpages_desc, lfs_putpages }, /* putpages */
160 { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
161 { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
162 { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
163 { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
164 { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
165 { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
166 { NULL, NULL }
167 };
168 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
169 { &lfs_vnodeop_p, lfs_vnodeop_entries };
170
171 int (**lfs_specop_p)(void *);
172 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
173 { &vop_default_desc, vn_default_error },
174 { &vop_lookup_desc, spec_lookup }, /* lookup */
175 { &vop_create_desc, spec_create }, /* create */
176 { &vop_mknod_desc, spec_mknod }, /* mknod */
177 { &vop_open_desc, spec_open }, /* open */
178 { &vop_close_desc, lfsspec_close }, /* close */
179 { &vop_access_desc, ulfs_access }, /* access */
180 { &vop_getattr_desc, lfs_getattr }, /* getattr */
181 { &vop_setattr_desc, lfs_setattr }, /* setattr */
182 { &vop_read_desc, ulfsspec_read }, /* read */
183 { &vop_write_desc, ulfsspec_write }, /* write */
184 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
185 { &vop_fcntl_desc, ulfs_fcntl }, /* fcntl */
186 { &vop_poll_desc, spec_poll }, /* poll */
187 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
188 { &vop_revoke_desc, spec_revoke }, /* revoke */
189 { &vop_mmap_desc, spec_mmap }, /* mmap */
190 { &vop_fsync_desc, spec_fsync }, /* fsync */
191 { &vop_seek_desc, spec_seek }, /* seek */
192 { &vop_remove_desc, spec_remove }, /* remove */
193 { &vop_link_desc, spec_link }, /* link */
194 { &vop_rename_desc, spec_rename }, /* rename */
195 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
196 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
197 { &vop_symlink_desc, spec_symlink }, /* symlink */
198 { &vop_readdir_desc, spec_readdir }, /* readdir */
199 { &vop_readlink_desc, spec_readlink }, /* readlink */
200 { &vop_abortop_desc, spec_abortop }, /* abortop */
201 { &vop_inactive_desc, lfs_inactive }, /* inactive */
202 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
203 { &vop_lock_desc, ulfs_lock }, /* lock */
204 { &vop_unlock_desc, ulfs_unlock }, /* unlock */
205 { &vop_bmap_desc, spec_bmap }, /* bmap */
206 { &vop_strategy_desc, spec_strategy }, /* strategy */
207 { &vop_print_desc, ulfs_print }, /* print */
208 { &vop_islocked_desc, ulfs_islocked }, /* islocked */
209 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
210 { &vop_advlock_desc, spec_advlock }, /* advlock */
211 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
212 { &vop_getpages_desc, spec_getpages }, /* getpages */
213 { &vop_putpages_desc, spec_putpages }, /* putpages */
214 { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
215 { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
216 { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
217 { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
218 { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
219 { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
220 { NULL, NULL }
221 };
222 const struct vnodeopv_desc lfs_specop_opv_desc =
223 { &lfs_specop_p, lfs_specop_entries };
224
225 int (**lfs_fifoop_p)(void *);
226 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
227 { &vop_default_desc, vn_default_error },
228 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
229 { &vop_create_desc, vn_fifo_bypass }, /* create */
230 { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
231 { &vop_open_desc, vn_fifo_bypass }, /* open */
232 { &vop_close_desc, lfsfifo_close }, /* close */
233 { &vop_access_desc, ulfs_access }, /* access */
234 { &vop_getattr_desc, lfs_getattr }, /* getattr */
235 { &vop_setattr_desc, lfs_setattr }, /* setattr */
236 { &vop_read_desc, ulfsfifo_read }, /* read */
237 { &vop_write_desc, ulfsfifo_write }, /* write */
238 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
239 { &vop_fcntl_desc, ulfs_fcntl }, /* fcntl */
240 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
241 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
242 { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
243 { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
244 { &vop_fsync_desc, vn_fifo_bypass }, /* fsync */
245 { &vop_seek_desc, vn_fifo_bypass }, /* seek */
246 { &vop_remove_desc, vn_fifo_bypass }, /* remove */
247 { &vop_link_desc, vn_fifo_bypass }, /* link */
248 { &vop_rename_desc, vn_fifo_bypass }, /* rename */
249 { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
250 { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
251 { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
252 { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
253 { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
254 { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
255 { &vop_inactive_desc, lfs_inactive }, /* inactive */
256 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
257 { &vop_lock_desc, ulfs_lock }, /* lock */
258 { &vop_unlock_desc, ulfs_unlock }, /* unlock */
259 { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
260 { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
261 { &vop_print_desc, ulfs_print }, /* print */
262 { &vop_islocked_desc, ulfs_islocked }, /* islocked */
263 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
264 { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
265 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
266 { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
267 { &vop_openextattr_desc, lfs_openextattr }, /* openextattr */
268 { &vop_closeextattr_desc, lfs_closeextattr }, /* closeextattr */
269 { &vop_getextattr_desc, lfs_getextattr }, /* getextattr */
270 { &vop_setextattr_desc, lfs_setextattr }, /* setextattr */
271 { &vop_listextattr_desc, lfs_listextattr }, /* listextattr */
272 { &vop_deleteextattr_desc, lfs_deleteextattr }, /* deleteextattr */
273 { NULL, NULL }
274 };
275 const struct vnodeopv_desc lfs_fifoop_opv_desc =
276 { &lfs_fifoop_p, lfs_fifoop_entries };
277
278 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int, struct vm_page **);
279
280 #define LFS_READWRITE
281 #include <ufs/lfs/ulfs_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 kauth_cred_t a_cred;
294 int a_flags;
295 off_t offlo;
296 off_t offhi;
297 } */ *ap = v;
298 struct vnode *vp = ap->a_vp;
299 int error, wait;
300 struct inode *ip = VTOI(vp);
301 struct lfs *fs = ip->i_lfs;
302
303 /* If we're mounted read-only, don't try to sync. */
304 if (fs->lfs_ronly)
305 return 0;
306
307 /* If a removed vnode is being cleaned, no need to sync here. */
308 if ((ap->a_flags & FSYNC_RECLAIM) != 0 && ip->i_mode == 0)
309 return 0;
310
311 /*
312 * Trickle sync simply adds this vnode to the pager list, as if
313 * the pagedaemon had requested a pageout.
314 */
315 if (ap->a_flags & FSYNC_LAZY) {
316 if (lfs_ignore_lazy_sync == 0) {
317 mutex_enter(&lfs_lock);
318 if (!(ip->i_flags & IN_PAGING)) {
319 ip->i_flags |= IN_PAGING;
320 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
321 i_lfs_pchain);
322 }
323 wakeup(&lfs_writer_daemon);
324 mutex_exit(&lfs_lock);
325 }
326 return 0;
327 }
328
329 /*
330 * If a vnode is bring cleaned, flush it out before we try to
331 * reuse it. This prevents the cleaner from writing files twice
332 * in the same partial segment, causing an accounting underflow.
333 */
334 if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
335 lfs_vflush(vp);
336 }
337
338 wait = (ap->a_flags & FSYNC_WAIT);
339 do {
340 mutex_enter(vp->v_interlock);
341 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
342 round_page(ap->a_offhi),
343 PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
344 if (error == EAGAIN) {
345 mutex_enter(&lfs_lock);
346 mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_fsync",
347 hz / 100 + 1, &lfs_lock);
348 mutex_exit(&lfs_lock);
349 }
350 } while (error == EAGAIN);
351 if (error)
352 return error;
353
354 if ((ap->a_flags & FSYNC_DATAONLY) == 0)
355 error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
356
357 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
358 int l = 0;
359 error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
360 curlwp->l_cred);
361 }
362 if (wait && !VPISEMPTY(vp))
363 LFS_SET_UINO(ip, IN_MODIFIED);
364
365 return error;
366 }
367
368 /*
369 * Take IN_ADIROP off, then call ulfs_inactive.
370 */
371 int
372 lfs_inactive(void *v)
373 {
374 struct vop_inactive_args /* {
375 struct vnode *a_vp;
376 } */ *ap = v;
377
378 lfs_unmark_vnode(ap->a_vp);
379
380 /*
381 * The Ifile is only ever inactivated on unmount.
382 * Streamline this process by not giving it more dirty blocks.
383 */
384 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
385 mutex_enter(&lfs_lock);
386 LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
387 mutex_exit(&lfs_lock);
388 VOP_UNLOCK(ap->a_vp);
389 return 0;
390 }
391
392 #ifdef DEBUG
393 /*
394 * This might happen on unmount.
395 * XXX If it happens at any other time, it should be a panic.
396 */
397 if (ap->a_vp->v_uflag & VU_DIROP) {
398 struct inode *ip = VTOI(ap->a_vp);
399 printf("lfs_inactive: inactivating VU_DIROP? ino = %d\n", (int)ip->i_number);
400 }
401 #endif /* DIAGNOSTIC */
402
403 return ulfs_inactive(v);
404 }
405
406 int
407 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
408 {
409 struct lfs *fs;
410 int error;
411
412 KASSERT(VOP_ISLOCKED(dvp));
413 KASSERT(vp == NULL || VOP_ISLOCKED(vp));
414
415 fs = VTOI(dvp)->i_lfs;
416
417 ASSERT_NO_SEGLOCK(fs);
418 /*
419 * LFS_NRESERVE calculates direct and indirect blocks as well
420 * as an inode block; an overestimate in most cases.
421 */
422 if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
423 return (error);
424
425 restart:
426 mutex_enter(&lfs_lock);
427 if (fs->lfs_dirops == 0) {
428 mutex_exit(&lfs_lock);
429 lfs_check(dvp, LFS_UNUSED_LBN, 0);
430 mutex_enter(&lfs_lock);
431 }
432 while (fs->lfs_writer) {
433 error = mtsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
434 "lfs_sdirop", 0, &lfs_lock);
435 if (error == EINTR) {
436 mutex_exit(&lfs_lock);
437 goto unreserve;
438 }
439 }
440 if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
441 wakeup(&lfs_writer_daemon);
442 mutex_exit(&lfs_lock);
443 preempt();
444 goto restart;
445 }
446
447 if (lfs_dirvcount > LFS_MAX_DIROP) {
448 DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
449 "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
450 if ((error = mtsleep(&lfs_dirvcount,
451 PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
452 &lfs_lock)) != 0) {
453 goto unreserve;
454 }
455 goto restart;
456 }
457
458 ++fs->lfs_dirops;
459 /* fs->lfs_doifile = 1; */ /* XXX why? --ks */
460 mutex_exit(&lfs_lock);
461
462 /* Hold a reference so SET_ENDOP will be happy */
463 vref(dvp);
464 if (vp) {
465 vref(vp);
466 MARK_VNODE(vp);
467 }
468
469 MARK_VNODE(dvp);
470 return 0;
471
472 unreserve:
473 lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
474 return error;
475 }
476
477 /*
478 * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
479 * in getnewvnode(), if we have a stacked filesystem mounted on top
480 * of us.
481 *
482 * NB: this means we have to clear the new vnodes on error. Fortunately
483 * SET_ENDOP is there to do that for us.
484 */
485 int
486 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
487 {
488 int error;
489 struct lfs *fs;
490
491 fs = VFSTOULFS(dvp->v_mount)->um_lfs;
492 ASSERT_NO_SEGLOCK(fs);
493 if (fs->lfs_ronly)
494 return EROFS;
495 if (vpp == NULL) {
496 return lfs_set_dirop(dvp, NULL);
497 }
498 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
499 if (error) {
500 DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
501 dvp, error));
502 return error;
503 }
504 if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
505 ungetnewvnode(*vpp);
506 *vpp = NULL;
507 return error;
508 }
509 return 0;
510 }
511
512 void
513 lfs_mark_vnode(struct vnode *vp)
514 {
515 struct inode *ip = VTOI(vp);
516 struct lfs *fs = ip->i_lfs;
517
518 mutex_enter(&lfs_lock);
519 if (!(ip->i_flag & IN_ADIROP)) {
520 if (!(vp->v_uflag & VU_DIROP)) {
521 mutex_exit(&lfs_lock);
522 mutex_enter(vp->v_interlock);
523 if (lfs_vref(vp) != 0)
524 panic("lfs_mark_vnode: could not vref");
525 mutex_enter(&lfs_lock);
526 ++lfs_dirvcount;
527 ++fs->lfs_dirvcount;
528 TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
529 vp->v_uflag |= VU_DIROP;
530 }
531 ++fs->lfs_nadirop;
532 ip->i_flag &= ~IN_CDIROP;
533 ip->i_flag |= IN_ADIROP;
534 } else
535 KASSERT(vp->v_uflag & VU_DIROP);
536 mutex_exit(&lfs_lock);
537 }
538
539 void
540 lfs_unmark_vnode(struct vnode *vp)
541 {
542 struct inode *ip = VTOI(vp);
543
544 mutex_enter(&lfs_lock);
545 if (ip && (ip->i_flag & IN_ADIROP)) {
546 KASSERT(vp->v_uflag & VU_DIROP);
547 --ip->i_lfs->lfs_nadirop;
548 ip->i_flag &= ~IN_ADIROP;
549 }
550 mutex_exit(&lfs_lock);
551 }
552
553 int
554 lfs_symlink(void *v)
555 {
556 struct vop_symlink_args /* {
557 struct vnode *a_dvp;
558 struct vnode **a_vpp;
559 struct componentname *a_cnp;
560 struct vattr *a_vap;
561 char *a_target;
562 } */ *ap = v;
563 int error;
564
565 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
566 vput(ap->a_dvp);
567 return error;
568 }
569 error = ulfs_symlink(ap);
570 SET_ENDOP_CREATE_AP(ap, "symlink");
571 return (error);
572 }
573
574 int
575 lfs_mknod(void *v)
576 {
577 struct vop_mknod_args /* {
578 struct vnode *a_dvp;
579 struct vnode **a_vpp;
580 struct componentname *a_cnp;
581 struct vattr *a_vap;
582 } */ *ap = v;
583 struct vattr *vap;
584 struct vnode **vpp;
585 struct inode *ip;
586 int error;
587 struct mount *mp;
588 ino_t ino;
589 struct ulfs_lookup_results *ulr;
590
591 vap = ap->a_vap;
592 vpp = ap->a_vpp;
593
594 /* XXX should handle this material another way */
595 ulr = &VTOI(ap->a_dvp)->i_crap;
596 ULFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
597
598 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
599 vput(ap->a_dvp);
600 return error;
601 }
602
603 fstrans_start(ap->a_dvp->v_mount, FSTRANS_SHARED);
604 error = ulfs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
605 ap->a_dvp, ulr, vpp, ap->a_cnp);
606
607 /* Either way we're done with the dirop at this point */
608 SET_ENDOP_CREATE_AP(ap, "mknod");
609
610 if (error) {
611 fstrans_done(ap->a_dvp->v_mount);
612 *vpp = NULL;
613 return (error);
614 }
615
616 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
617 ip = VTOI(*vpp);
618 mp = (*vpp)->v_mount;
619 ino = ip->i_number;
620 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
621 if (vap->va_rdev != VNOVAL) {
622 struct ulfsmount *ump = ip->i_ump;
623 /*
624 * Want to be able to use this to make badblock
625 * inodes, so don't truncate the dev number.
626 */
627 if (ump->um_fstype == ULFS1)
628 ip->i_ffs1_rdev = ulfs_rw32(vap->va_rdev,
629 ULFS_MPNEEDSWAP(fs));
630 else
631 ip->i_ffs2_rdev = ulfs_rw64(vap->va_rdev,
632 ULFS_MPNEEDSWAP(fs));
633 }
634
635 /*
636 * Call fsync to write the vnode so that we don't have to deal with
637 * flushing it when it's marked VU_DIROP|VI_XLOCK.
638 *
639 * XXX KS - If we can't flush we also can't call vgone(), so must
640 * return. But, that leaves this vnode in limbo, also not good.
641 * Can this ever happen (barring hardware failure)?
642 */
643 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
644 panic("lfs_mknod: couldn't fsync (ino %llu)",
645 (unsigned long long)ino);
646 /* return (error); */
647 }
648 /*
649 * Remove vnode so that it will be reloaded by VFS_VGET and
650 * checked to see if it is an alias of an existing entry in
651 * the inode cache.
652 */
653 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
654
655 (*vpp)->v_type = VNON;
656 VOP_UNLOCK(*vpp);
657 vgone(*vpp);
658 error = VFS_VGET(mp, ino, vpp);
659
660 fstrans_done(ap->a_dvp->v_mount);
661 if (error != 0) {
662 *vpp = NULL;
663 return (error);
664 }
665 return (0);
666 }
667
668 int
669 lfs_create(void *v)
670 {
671 struct vop_create_args /* {
672 struct vnode *a_dvp;
673 struct vnode **a_vpp;
674 struct componentname *a_cnp;
675 struct vattr *a_vap;
676 } */ *ap = v;
677 int error;
678
679 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
680 vput(ap->a_dvp);
681 return error;
682 }
683 error = ulfs_create(ap);
684 SET_ENDOP_CREATE_AP(ap, "create");
685 return (error);
686 }
687
688 int
689 lfs_mkdir(void *v)
690 {
691 struct vop_mkdir_args /* {
692 struct vnode *a_dvp;
693 struct vnode **a_vpp;
694 struct componentname *a_cnp;
695 struct vattr *a_vap;
696 } */ *ap = v;
697 int error;
698
699 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
700 vput(ap->a_dvp);
701 return error;
702 }
703 error = ulfs_mkdir(ap);
704 SET_ENDOP_CREATE_AP(ap, "mkdir");
705 return (error);
706 }
707
708 int
709 lfs_remove(void *v)
710 {
711 struct vop_remove_args /* {
712 struct vnode *a_dvp;
713 struct vnode *a_vp;
714 struct componentname *a_cnp;
715 } */ *ap = v;
716 struct vnode *dvp, *vp;
717 struct inode *ip;
718 int error;
719
720 dvp = ap->a_dvp;
721 vp = ap->a_vp;
722 ip = VTOI(vp);
723 if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
724 if (dvp == vp)
725 vrele(vp);
726 else
727 vput(vp);
728 vput(dvp);
729 return error;
730 }
731 error = ulfs_remove(ap);
732 if (ip->i_nlink == 0)
733 lfs_orphan(ip->i_lfs, ip->i_number);
734 SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
735 return (error);
736 }
737
738 int
739 lfs_rmdir(void *v)
740 {
741 struct vop_rmdir_args /* {
742 struct vnodeop_desc *a_desc;
743 struct vnode *a_dvp;
744 struct vnode *a_vp;
745 struct componentname *a_cnp;
746 } */ *ap = v;
747 struct vnode *vp;
748 struct inode *ip;
749 int error;
750
751 vp = ap->a_vp;
752 ip = VTOI(vp);
753 if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
754 if (ap->a_dvp == vp)
755 vrele(ap->a_dvp);
756 else
757 vput(ap->a_dvp);
758 vput(vp);
759 return error;
760 }
761 error = ulfs_rmdir(ap);
762 if (ip->i_nlink == 0)
763 lfs_orphan(ip->i_lfs, ip->i_number);
764 SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
765 return (error);
766 }
767
768 int
769 lfs_link(void *v)
770 {
771 struct vop_link_args /* {
772 struct vnode *a_dvp;
773 struct vnode *a_vp;
774 struct componentname *a_cnp;
775 } */ *ap = v;
776 int error;
777 struct vnode **vpp = NULL;
778
779 if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
780 vput(ap->a_dvp);
781 return error;
782 }
783 error = ulfs_link(ap);
784 SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
785 return (error);
786 }
787
788 /* XXX hack to avoid calling ITIMES in getattr */
789 int
790 lfs_getattr(void *v)
791 {
792 struct vop_getattr_args /* {
793 struct vnode *a_vp;
794 struct vattr *a_vap;
795 kauth_cred_t a_cred;
796 } */ *ap = v;
797 struct vnode *vp = ap->a_vp;
798 struct inode *ip = VTOI(vp);
799 struct vattr *vap = ap->a_vap;
800 struct lfs *fs = ip->i_lfs;
801
802 fstrans_start(vp->v_mount, FSTRANS_SHARED);
803 /*
804 * Copy from inode table
805 */
806 vap->va_fsid = ip->i_dev;
807 vap->va_fileid = ip->i_number;
808 vap->va_mode = ip->i_mode & ~LFS_IFMT;
809 vap->va_nlink = ip->i_nlink;
810 vap->va_uid = ip->i_uid;
811 vap->va_gid = ip->i_gid;
812 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
813 vap->va_size = vp->v_size;
814 vap->va_atime.tv_sec = ip->i_ffs1_atime;
815 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
816 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
817 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
818 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
819 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
820 vap->va_flags = ip->i_flags;
821 vap->va_gen = ip->i_gen;
822 /* this doesn't belong here */
823 if (vp->v_type == VBLK)
824 vap->va_blocksize = BLKDEV_IOSIZE;
825 else if (vp->v_type == VCHR)
826 vap->va_blocksize = MAXBSIZE;
827 else
828 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
829 vap->va_bytes = lfs_fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
830 vap->va_type = vp->v_type;
831 vap->va_filerev = ip->i_modrev;
832 fstrans_done(vp->v_mount);
833 return (0);
834 }
835
836 /*
837 * Check to make sure the inode blocks won't choke the buffer
838 * cache, then call ulfs_setattr as usual.
839 */
840 int
841 lfs_setattr(void *v)
842 {
843 struct vop_setattr_args /* {
844 struct vnode *a_vp;
845 struct vattr *a_vap;
846 kauth_cred_t a_cred;
847 } */ *ap = v;
848 struct vnode *vp = ap->a_vp;
849
850 lfs_check(vp, LFS_UNUSED_LBN, 0);
851 return ulfs_setattr(v);
852 }
853
854 /*
855 * Release the block we hold on lfs_newseg wrapping. Called on file close,
856 * or explicitly from LFCNWRAPGO. Called with the interlock held.
857 */
858 static int
859 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
860 {
861 if (fs->lfs_stoplwp != curlwp)
862 return EBUSY;
863
864 fs->lfs_stoplwp = NULL;
865 cv_signal(&fs->lfs_stopcv);
866
867 KASSERT(fs->lfs_nowrap > 0);
868 if (fs->lfs_nowrap <= 0) {
869 return 0;
870 }
871
872 if (--fs->lfs_nowrap == 0) {
873 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
874 wakeup(&fs->lfs_wrappass);
875 lfs_wakeup_cleaner(fs);
876 }
877 if (waitfor) {
878 mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
879 0, &lfs_lock);
880 }
881
882 return 0;
883 }
884
885 /*
886 * Close called.
887 *
888 * Update the times on the inode.
889 */
890 /* ARGSUSED */
891 int
892 lfs_close(void *v)
893 {
894 struct vop_close_args /* {
895 struct vnode *a_vp;
896 int a_fflag;
897 kauth_cred_t a_cred;
898 } */ *ap = v;
899 struct vnode *vp = ap->a_vp;
900 struct inode *ip = VTOI(vp);
901 struct lfs *fs = ip->i_lfs;
902
903 if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
904 fs->lfs_stoplwp == curlwp) {
905 mutex_enter(&lfs_lock);
906 log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
907 lfs_wrapgo(fs, ip, 0);
908 mutex_exit(&lfs_lock);
909 }
910
911 if (vp == ip->i_lfs->lfs_ivnode &&
912 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
913 return 0;
914
915 fstrans_start(vp->v_mount, FSTRANS_SHARED);
916 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
917 LFS_ITIMES(ip, NULL, NULL, NULL);
918 }
919 fstrans_done(vp->v_mount);
920 return (0);
921 }
922
923 /*
924 * Close wrapper for special devices.
925 *
926 * Update the times on the inode then do device close.
927 */
928 int
929 lfsspec_close(void *v)
930 {
931 struct vop_close_args /* {
932 struct vnode *a_vp;
933 int a_fflag;
934 kauth_cred_t a_cred;
935 } */ *ap = v;
936 struct vnode *vp;
937 struct inode *ip;
938
939 vp = ap->a_vp;
940 ip = VTOI(vp);
941 if (vp->v_usecount > 1) {
942 LFS_ITIMES(ip, NULL, NULL, NULL);
943 }
944 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
945 }
946
947 /*
948 * Close wrapper for fifo's.
949 *
950 * Update the times on the inode then do device close.
951 */
952 int
953 lfsfifo_close(void *v)
954 {
955 struct vop_close_args /* {
956 struct vnode *a_vp;
957 int a_fflag;
958 kauth_cred_ a_cred;
959 } */ *ap = v;
960 struct vnode *vp;
961 struct inode *ip;
962
963 vp = ap->a_vp;
964 ip = VTOI(vp);
965 if (ap->a_vp->v_usecount > 1) {
966 LFS_ITIMES(ip, NULL, NULL, NULL);
967 }
968 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
969 }
970
971 /*
972 * Reclaim an inode so that it can be used for other purposes.
973 */
974
975 int
976 lfs_reclaim(void *v)
977 {
978 struct vop_reclaim_args /* {
979 struct vnode *a_vp;
980 } */ *ap = v;
981 struct vnode *vp = ap->a_vp;
982 struct inode *ip = VTOI(vp);
983 struct lfs *fs = ip->i_lfs;
984 int error;
985
986 /*
987 * The inode must be freed and updated before being removed
988 * from its hash chain. Other threads trying to gain a hold
989 * on the inode will be stalled because it is locked (VI_XLOCK).
990 */
991 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
992 lfs_vfree(vp, ip->i_number, ip->i_omode);
993
994 mutex_enter(&lfs_lock);
995 LFS_CLR_UINO(ip, IN_ALLMOD);
996 mutex_exit(&lfs_lock);
997 if ((error = ulfs_reclaim(vp)))
998 return (error);
999
1000 /*
1001 * Take us off the paging and/or dirop queues if we were on them.
1002 * We shouldn't be on them.
1003 */
1004 mutex_enter(&lfs_lock);
1005 if (ip->i_flags & IN_PAGING) {
1006 log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
1007 fs->lfs_fsmnt);
1008 ip->i_flags &= ~IN_PAGING;
1009 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1010 }
1011 if (vp->v_uflag & VU_DIROP) {
1012 panic("reclaimed vnode is VU_DIROP");
1013 vp->v_uflag &= ~VU_DIROP;
1014 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
1015 }
1016 mutex_exit(&lfs_lock);
1017
1018 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1019 lfs_deregister_all(vp);
1020 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1021 ip->inode_ext.lfs = NULL;
1022 genfs_node_destroy(vp);
1023 pool_put(&lfs_inode_pool, vp->v_data);
1024 vp->v_data = NULL;
1025 return (0);
1026 }
1027
1028 /*
1029 * Read a block from a storage device.
1030 *
1031 * Calculate the logical to physical mapping if not done already,
1032 * then call the device strategy routine.
1033 *
1034 * In order to avoid reading blocks that are in the process of being
1035 * written by the cleaner---and hence are not mutexed by the normal
1036 * buffer cache / page cache mechanisms---check for collisions before
1037 * reading.
1038 *
1039 * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
1040 * the active cleaner test.
1041 *
1042 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1043 */
1044 int
1045 lfs_strategy(void *v)
1046 {
1047 struct vop_strategy_args /* {
1048 struct vnode *a_vp;
1049 struct buf *a_bp;
1050 } */ *ap = v;
1051 struct buf *bp;
1052 struct lfs *fs;
1053 struct vnode *vp;
1054 struct inode *ip;
1055 daddr_t tbn;
1056 #define MAXLOOP 25
1057 int i, sn, error, slept, loopcount;
1058
1059 bp = ap->a_bp;
1060 vp = ap->a_vp;
1061 ip = VTOI(vp);
1062 fs = ip->i_lfs;
1063
1064 /* lfs uses its strategy routine only for read */
1065 KASSERT(bp->b_flags & B_READ);
1066
1067 if (vp->v_type == VBLK || vp->v_type == VCHR)
1068 panic("lfs_strategy: spec");
1069 KASSERT(bp->b_bcount != 0);
1070 if (bp->b_blkno == bp->b_lblkno) {
1071 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1072 NULL);
1073 if (error) {
1074 bp->b_error = error;
1075 bp->b_resid = bp->b_bcount;
1076 biodone(bp);
1077 return (error);
1078 }
1079 if ((long)bp->b_blkno == -1) /* no valid data */
1080 clrbuf(bp);
1081 }
1082 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1083 bp->b_resid = bp->b_bcount;
1084 biodone(bp);
1085 return (0);
1086 }
1087
1088 slept = 1;
1089 loopcount = 0;
1090 mutex_enter(&lfs_lock);
1091 while (slept && fs->lfs_seglock) {
1092 mutex_exit(&lfs_lock);
1093 /*
1094 * Look through list of intervals.
1095 * There will only be intervals to look through
1096 * if the cleaner holds the seglock.
1097 * Since the cleaner is synchronous, we can trust
1098 * the list of intervals to be current.
1099 */
1100 tbn = LFS_DBTOFSB(fs, bp->b_blkno);
1101 sn = lfs_dtosn(fs, tbn);
1102 slept = 0;
1103 for (i = 0; i < fs->lfs_cleanind; i++) {
1104 if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
1105 tbn >= fs->lfs_cleanint[i]) {
1106 DLOG((DLOG_CLEAN,
1107 "lfs_strategy: ino %d lbn %" PRId64
1108 " ind %d sn %d fsb %" PRIx32
1109 " given sn %d fsb %" PRIx64 "\n",
1110 ip->i_number, bp->b_lblkno, i,
1111 lfs_dtosn(fs, fs->lfs_cleanint[i]),
1112 fs->lfs_cleanint[i], sn, tbn));
1113 DLOG((DLOG_CLEAN,
1114 "lfs_strategy: sleeping on ino %d lbn %"
1115 PRId64 "\n", ip->i_number, bp->b_lblkno));
1116 mutex_enter(&lfs_lock);
1117 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
1118 /*
1119 * Cleaner can't wait for itself.
1120 * Instead, wait for the blocks
1121 * to be written to disk.
1122 * XXX we need pribio in the test
1123 * XXX here.
1124 */
1125 mtsleep(&fs->lfs_iocount,
1126 (PRIBIO + 1) | PNORELOCK,
1127 "clean2", hz/10 + 1,
1128 &lfs_lock);
1129 slept = 1;
1130 ++loopcount;
1131 break;
1132 } else if (fs->lfs_seglock) {
1133 mtsleep(&fs->lfs_seglock,
1134 (PRIBIO + 1) | PNORELOCK,
1135 "clean1", 0,
1136 &lfs_lock);
1137 slept = 1;
1138 break;
1139 }
1140 mutex_exit(&lfs_lock);
1141 }
1142 }
1143 mutex_enter(&lfs_lock);
1144 if (loopcount > MAXLOOP) {
1145 printf("lfs_strategy: breaking out of clean2 loop\n");
1146 break;
1147 }
1148 }
1149 mutex_exit(&lfs_lock);
1150
1151 vp = ip->i_devvp;
1152 return VOP_STRATEGY(vp, bp);
1153 }
1154
1155 /*
1156 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1157 * Technically this is a checkpoint (the on-disk state is valid)
1158 * even though we are leaving out all the file data.
1159 */
1160 int
1161 lfs_flush_dirops(struct lfs *fs)
1162 {
1163 struct inode *ip, *nip;
1164 struct vnode *vp;
1165 extern int lfs_dostats;
1166 struct segment *sp;
1167 int flags = 0;
1168 int error = 0;
1169
1170 ASSERT_MAYBE_SEGLOCK(fs);
1171 KASSERT(fs->lfs_nadirop == 0);
1172
1173 if (fs->lfs_ronly)
1174 return EROFS;
1175
1176 mutex_enter(&lfs_lock);
1177 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
1178 mutex_exit(&lfs_lock);
1179 return 0;
1180 } else
1181 mutex_exit(&lfs_lock);
1182
1183 if (lfs_dostats)
1184 ++lfs_stats.flush_invoked;
1185
1186 lfs_imtime(fs);
1187 lfs_seglock(fs, flags);
1188 sp = fs->lfs_sp;
1189
1190 /*
1191 * lfs_writevnodes, optimized to get dirops out of the way.
1192 * Only write dirops, and don't flush files' pages, only
1193 * blocks from the directories.
1194 *
1195 * We don't need to vref these files because they are
1196 * dirops and so hold an extra reference until the
1197 * segunlock clears them of that status.
1198 *
1199 * We don't need to check for IN_ADIROP because we know that
1200 * no dirops are active.
1201 *
1202 */
1203 mutex_enter(&lfs_lock);
1204 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1205 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1206 mutex_exit(&lfs_lock);
1207 vp = ITOV(ip);
1208
1209 KASSERT((ip->i_flag & IN_ADIROP) == 0);
1210 KASSERT(vp->v_uflag & VU_DIROP);
1211 KASSERT(!(vp->v_iflag & VI_XLOCK));
1212
1213 /*
1214 * All writes to directories come from dirops; all
1215 * writes to files' direct blocks go through the page
1216 * cache, which we're not touching. Reads to files
1217 * and/or directories will not be affected by writing
1218 * directory blocks inodes and file inodes. So we don't
1219 * really need to lock.
1220 */
1221 if (vp->v_iflag & VI_XLOCK) {
1222 mutex_enter(&lfs_lock);
1223 continue;
1224 }
1225 /* XXX see below
1226 * waslocked = VOP_ISLOCKED(vp);
1227 */
1228 if (vp->v_type != VREG &&
1229 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1230 error = lfs_writefile(fs, sp, vp);
1231 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1232 !(ip->i_flag & IN_ALLMOD)) {
1233 mutex_enter(&lfs_lock);
1234 LFS_SET_UINO(ip, IN_MODIFIED);
1235 mutex_exit(&lfs_lock);
1236 }
1237 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1238 mutex_enter(&lfs_lock);
1239 error = EAGAIN;
1240 break;
1241 }
1242 }
1243 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1244 error = lfs_writeinode(fs, sp, ip);
1245 mutex_enter(&lfs_lock);
1246 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1247 error = EAGAIN;
1248 break;
1249 }
1250
1251 /*
1252 * We might need to update these inodes again,
1253 * for example, if they have data blocks to write.
1254 * Make sure that after this flush, they are still
1255 * marked IN_MODIFIED so that we don't forget to
1256 * write them.
1257 */
1258 /* XXX only for non-directories? --KS */
1259 LFS_SET_UINO(ip, IN_MODIFIED);
1260 }
1261 mutex_exit(&lfs_lock);
1262 /* We've written all the dirops there are */
1263 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
1264 lfs_finalize_fs_seguse(fs);
1265 (void) lfs_writeseg(fs, sp);
1266 lfs_segunlock(fs);
1267
1268 return error;
1269 }
1270
1271 /*
1272 * Flush all vnodes for which the pagedaemon has requested pageouts.
1273 * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
1274 * has just run, this would be an error). If we have to skip a vnode
1275 * for any reason, just skip it; if we have to wait for the cleaner,
1276 * abort. The writer daemon will call us again later.
1277 */
1278 int
1279 lfs_flush_pchain(struct lfs *fs)
1280 {
1281 struct inode *ip, *nip;
1282 struct vnode *vp;
1283 extern int lfs_dostats;
1284 struct segment *sp;
1285 int error, error2;
1286
1287 ASSERT_NO_SEGLOCK(fs);
1288
1289 if (fs->lfs_ronly)
1290 return EROFS;
1291
1292 mutex_enter(&lfs_lock);
1293 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1294 mutex_exit(&lfs_lock);
1295 return 0;
1296 } else
1297 mutex_exit(&lfs_lock);
1298
1299 /* Get dirops out of the way */
1300 if ((error = lfs_flush_dirops(fs)) != 0)
1301 return error;
1302
1303 if (lfs_dostats)
1304 ++lfs_stats.flush_invoked;
1305
1306 /*
1307 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1308 */
1309 lfs_imtime(fs);
1310 lfs_seglock(fs, 0);
1311 sp = fs->lfs_sp;
1312
1313 /*
1314 * lfs_writevnodes, optimized to clear pageout requests.
1315 * Only write non-dirop files that are in the pageout queue.
1316 * We're very conservative about what we write; we want to be
1317 * fast and async.
1318 */
1319 mutex_enter(&lfs_lock);
1320 top:
1321 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1322 nip = TAILQ_NEXT(ip, i_lfs_pchain);
1323 vp = ITOV(ip);
1324
1325 if (!(ip->i_flags & IN_PAGING))
1326 goto top;
1327
1328 mutex_enter(vp->v_interlock);
1329 if ((vp->v_iflag & VI_XLOCK) || (vp->v_uflag & VU_DIROP) != 0) {
1330 mutex_exit(vp->v_interlock);
1331 continue;
1332 }
1333 if (vp->v_type != VREG) {
1334 mutex_exit(vp->v_interlock);
1335 continue;
1336 }
1337 if (lfs_vref(vp))
1338 continue;
1339 mutex_exit(&lfs_lock);
1340
1341 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
1342 lfs_vunref(vp);
1343 mutex_enter(&lfs_lock);
1344 continue;
1345 }
1346
1347 error = lfs_writefile(fs, sp, vp);
1348 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1349 !(ip->i_flag & IN_ALLMOD)) {
1350 mutex_enter(&lfs_lock);
1351 LFS_SET_UINO(ip, IN_MODIFIED);
1352 mutex_exit(&lfs_lock);
1353 }
1354 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1355 error2 = lfs_writeinode(fs, sp, ip);
1356
1357 VOP_UNLOCK(vp);
1358 lfs_vunref(vp);
1359
1360 if (error == EAGAIN || error2 == EAGAIN) {
1361 lfs_writeseg(fs, sp);
1362 mutex_enter(&lfs_lock);
1363 break;
1364 }
1365 mutex_enter(&lfs_lock);
1366 }
1367 mutex_exit(&lfs_lock);
1368 (void) lfs_writeseg(fs, sp);
1369 lfs_segunlock(fs);
1370
1371 return 0;
1372 }
1373
1374 /*
1375 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1376 */
1377 int
1378 lfs_fcntl(void *v)
1379 {
1380 struct vop_fcntl_args /* {
1381 struct vnode *a_vp;
1382 u_int a_command;
1383 void * a_data;
1384 int a_fflag;
1385 kauth_cred_t a_cred;
1386 } */ *ap = v;
1387 struct timeval tv;
1388 struct timeval *tvp;
1389 BLOCK_INFO *blkiov;
1390 CLEANERINFO *cip;
1391 SEGUSE *sup;
1392 int blkcnt, error, oclean;
1393 size_t fh_size;
1394 struct lfs_fcntl_markv blkvp;
1395 struct lwp *l;
1396 fsid_t *fsidp;
1397 struct lfs *fs;
1398 struct buf *bp;
1399 fhandle_t *fhp;
1400 daddr_t off;
1401
1402 /* Only respect LFS fcntls on fs root or Ifile */
1403 if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
1404 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1405 return ulfs_fcntl(v);
1406 }
1407
1408 /* Avoid locking a draining lock */
1409 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1410 return ESHUTDOWN;
1411 }
1412
1413 /* LFS control and monitoring fcntls are available only to root */
1414 l = curlwp;
1415 if (((ap->a_command & 0xff00) >> 8) == 'L' &&
1416 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
1417 KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
1418 return (error);
1419
1420 fs = VTOI(ap->a_vp)->i_lfs;
1421 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1422
1423 error = 0;
1424 switch ((int)ap->a_command) {
1425 case LFCNSEGWAITALL_COMPAT_50:
1426 case LFCNSEGWAITALL_COMPAT:
1427 fsidp = NULL;
1428 /* FALLSTHROUGH */
1429 case LFCNSEGWAIT_COMPAT_50:
1430 case LFCNSEGWAIT_COMPAT:
1431 {
1432 struct timeval50 *tvp50
1433 = (struct timeval50 *)ap->a_data;
1434 timeval50_to_timeval(tvp50, &tv);
1435 tvp = &tv;
1436 }
1437 goto segwait_common;
1438 case LFCNSEGWAITALL:
1439 fsidp = NULL;
1440 /* FALLSTHROUGH */
1441 case LFCNSEGWAIT:
1442 tvp = (struct timeval *)ap->a_data;
1443 segwait_common:
1444 mutex_enter(&lfs_lock);
1445 ++fs->lfs_sleepers;
1446 mutex_exit(&lfs_lock);
1447
1448 error = lfs_segwait(fsidp, tvp);
1449
1450 mutex_enter(&lfs_lock);
1451 if (--fs->lfs_sleepers == 0)
1452 wakeup(&fs->lfs_sleepers);
1453 mutex_exit(&lfs_lock);
1454 return error;
1455
1456 case LFCNBMAPV:
1457 case LFCNMARKV:
1458 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1459
1460 blkcnt = blkvp.blkcnt;
1461 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1462 return (EINVAL);
1463 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1464 if ((error = copyin(blkvp.blkiov, blkiov,
1465 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1466 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1467 return error;
1468 }
1469
1470 mutex_enter(&lfs_lock);
1471 ++fs->lfs_sleepers;
1472 mutex_exit(&lfs_lock);
1473 if (ap->a_command == LFCNBMAPV)
1474 error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
1475 else /* LFCNMARKV */
1476 error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
1477 if (error == 0)
1478 error = copyout(blkiov, blkvp.blkiov,
1479 blkcnt * sizeof(BLOCK_INFO));
1480 mutex_enter(&lfs_lock);
1481 if (--fs->lfs_sleepers == 0)
1482 wakeup(&fs->lfs_sleepers);
1483 mutex_exit(&lfs_lock);
1484 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1485 return error;
1486
1487 case LFCNRECLAIM:
1488 /*
1489 * Flush dirops and write Ifile, allowing empty segments
1490 * to be immediately reclaimed.
1491 */
1492 lfs_writer_enter(fs, "pndirop");
1493 off = fs->lfs_offset;
1494 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1495 lfs_flush_dirops(fs);
1496 LFS_CLEANERINFO(cip, fs, bp);
1497 oclean = cip->clean;
1498 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1499 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1500 fs->lfs_sp->seg_flags |= SEGM_PROT;
1501 lfs_segunlock(fs);
1502 lfs_writer_leave(fs);
1503
1504 #ifdef DEBUG
1505 LFS_CLEANERINFO(cip, fs, bp);
1506 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
1507 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
1508 fs->lfs_offset - off, cip->clean - oclean,
1509 fs->lfs_activesb));
1510 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
1511 #endif
1512
1513 return 0;
1514
1515 case LFCNIFILEFH_COMPAT:
1516 /* Return the filehandle of the Ifile */
1517 if ((error = kauth_authorize_system(l->l_cred,
1518 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
1519 return (error);
1520 fhp = (struct fhandle *)ap->a_data;
1521 fhp->fh_fsid = *fsidp;
1522 fh_size = 16; /* former VFS_MAXFIDSIZ */
1523 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1524
1525 case LFCNIFILEFH_COMPAT2:
1526 case LFCNIFILEFH:
1527 /* Return the filehandle of the Ifile */
1528 fhp = (struct fhandle *)ap->a_data;
1529 fhp->fh_fsid = *fsidp;
1530 fh_size = sizeof(struct lfs_fhandle) -
1531 offsetof(fhandle_t, fh_fid);
1532 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1533
1534 case LFCNREWIND:
1535 /* Move lfs_offset to the lowest-numbered segment */
1536 return lfs_rewind(fs, *(int *)ap->a_data);
1537
1538 case LFCNINVAL:
1539 /* Mark a segment SEGUSE_INVAL */
1540 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
1541 if (sup->su_nbytes > 0) {
1542 brelse(bp, 0);
1543 lfs_unset_inval_all(fs);
1544 return EBUSY;
1545 }
1546 sup->su_flags |= SEGUSE_INVAL;
1547 VOP_BWRITE(bp->b_vp, bp);
1548 return 0;
1549
1550 case LFCNRESIZE:
1551 /* Resize the filesystem */
1552 return lfs_resize_fs(fs, *(int *)ap->a_data);
1553
1554 case LFCNWRAPSTOP:
1555 case LFCNWRAPSTOP_COMPAT:
1556 /*
1557 * Hold lfs_newseg at segment 0; if requested, sleep until
1558 * the filesystem wraps around. To support external agents
1559 * (dump, fsck-based regression test) that need to look at
1560 * a snapshot of the filesystem, without necessarily
1561 * requiring that all fs activity stops.
1562 */
1563 if (fs->lfs_stoplwp == curlwp)
1564 return EALREADY;
1565
1566 mutex_enter(&lfs_lock);
1567 while (fs->lfs_stoplwp != NULL)
1568 cv_wait(&fs->lfs_stopcv, &lfs_lock);
1569 fs->lfs_stoplwp = curlwp;
1570 if (fs->lfs_nowrap == 0)
1571 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
1572 ++fs->lfs_nowrap;
1573 if (*(int *)ap->a_data == 1
1574 || ap->a_command == LFCNWRAPSTOP_COMPAT) {
1575 log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
1576 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1577 "segwrap", 0, &lfs_lock);
1578 log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
1579 if (error) {
1580 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
1581 }
1582 }
1583 mutex_exit(&lfs_lock);
1584 return 0;
1585
1586 case LFCNWRAPGO:
1587 case LFCNWRAPGO_COMPAT:
1588 /*
1589 * Having done its work, the agent wakes up the writer.
1590 * If the argument is 1, it sleeps until a new segment
1591 * is selected.
1592 */
1593 mutex_enter(&lfs_lock);
1594 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
1595 ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
1596 *((int *)ap->a_data));
1597 mutex_exit(&lfs_lock);
1598 return error;
1599
1600 case LFCNWRAPPASS:
1601 if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
1602 return EALREADY;
1603 mutex_enter(&lfs_lock);
1604 if (fs->lfs_stoplwp != curlwp) {
1605 mutex_exit(&lfs_lock);
1606 return EALREADY;
1607 }
1608 if (fs->lfs_nowrap == 0) {
1609 mutex_exit(&lfs_lock);
1610 return EBUSY;
1611 }
1612 fs->lfs_wrappass = 1;
1613 wakeup(&fs->lfs_wrappass);
1614 /* Wait for the log to wrap, if asked */
1615 if (*(int *)ap->a_data) {
1616 mutex_enter(ap->a_vp->v_interlock);
1617 if (lfs_vref(ap->a_vp) != 0)
1618 panic("LFCNWRAPPASS: lfs_vref failed");
1619 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
1620 log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
1621 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1622 "segwrap", 0, &lfs_lock);
1623 log(LOG_NOTICE, "LFCNPASS done waiting\n");
1624 VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
1625 lfs_vunref(ap->a_vp);
1626 }
1627 mutex_exit(&lfs_lock);
1628 return error;
1629
1630 case LFCNWRAPSTATUS:
1631 mutex_enter(&lfs_lock);
1632 *(int *)ap->a_data = fs->lfs_wrapstatus;
1633 mutex_exit(&lfs_lock);
1634 return 0;
1635
1636 default:
1637 return ulfs_fcntl(v);
1638 }
1639 return 0;
1640 }
1641
1642 int
1643 lfs_getpages(void *v)
1644 {
1645 struct vop_getpages_args /* {
1646 struct vnode *a_vp;
1647 voff_t a_offset;
1648 struct vm_page **a_m;
1649 int *a_count;
1650 int a_centeridx;
1651 vm_prot_t a_access_type;
1652 int a_advice;
1653 int a_flags;
1654 } */ *ap = v;
1655
1656 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
1657 (ap->a_access_type & VM_PROT_WRITE) != 0) {
1658 return EPERM;
1659 }
1660 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
1661 mutex_enter(&lfs_lock);
1662 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
1663 mutex_exit(&lfs_lock);
1664 }
1665
1666 /*
1667 * we're relying on the fact that genfs_getpages() always read in
1668 * entire filesystem blocks.
1669 */
1670 return genfs_getpages(v);
1671 }
1672
1673 /*
1674 * Wait for a page to become unbusy, possibly printing diagnostic messages
1675 * as well.
1676 *
1677 * Called with vp->v_interlock held; return with it held.
1678 */
1679 static void
1680 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label)
1681 {
1682 KASSERT(mutex_owned(vp->v_interlock));
1683 if ((pg->flags & PG_BUSY) == 0)
1684 return; /* Nothing to wait for! */
1685
1686 #if defined(DEBUG) && defined(UVM_PAGE_TRKOWN)
1687 static struct vm_page *lastpg;
1688
1689 if (label != NULL && pg != lastpg) {
1690 if (pg->owner_tag) {
1691 printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n",
1692 curproc->p_pid, curlwp->l_lid, label,
1693 pg, pg->owner, pg->lowner, pg->owner_tag);
1694 } else {
1695 printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n",
1696 curproc->p_pid, curlwp->l_lid, label, pg);
1697 }
1698 }
1699 lastpg = pg;
1700 #endif
1701
1702 pg->flags |= PG_WANTED;
1703 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0, "lfsput", 0);
1704 mutex_enter(vp->v_interlock);
1705 }
1706
1707 /*
1708 * This routine is called by lfs_putpages() when it can't complete the
1709 * write because a page is busy. This means that either (1) someone,
1710 * possibly the pagedaemon, is looking at this page, and will give it up
1711 * presently; or (2) we ourselves are holding the page busy in the
1712 * process of being written (either gathered or actually on its way to
1713 * disk). We don't need to give up the segment lock, but we might need
1714 * to call lfs_writeseg() to expedite the page's journey to disk.
1715 *
1716 * Called with vp->v_interlock held; return with it held.
1717 */
1718 /* #define BUSYWAIT */
1719 static void
1720 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg,
1721 int seglocked, const char *label)
1722 {
1723 KASSERT(mutex_owned(vp->v_interlock));
1724 #ifndef BUSYWAIT
1725 struct inode *ip = VTOI(vp);
1726 struct segment *sp = fs->lfs_sp;
1727 int count = 0;
1728
1729 if (pg == NULL)
1730 return;
1731
1732 while (pg->flags & PG_BUSY &&
1733 pg->uobject == &vp->v_uobj) {
1734 mutex_exit(vp->v_interlock);
1735 if (sp->cbpp - sp->bpp > 1) {
1736 /* Write gathered pages */
1737 lfs_updatemeta(sp);
1738 lfs_release_finfo(fs);
1739 (void) lfs_writeseg(fs, sp);
1740
1741 /*
1742 * Reinitialize FIP
1743 */
1744 KASSERT(sp->vp == vp);
1745 lfs_acquire_finfo(fs, ip->i_number,
1746 ip->i_gen);
1747 }
1748 ++count;
1749 mutex_enter(vp->v_interlock);
1750 wait_for_page(vp, pg, label);
1751 }
1752 if (label != NULL && count > 1) {
1753 DLOG((DLOG_PAGE, "lfs_putpages[%d]: %s: %sn = %d\n",
1754 curproc->p_pid, label, (count > 0 ? "looping, " : ""),
1755 count));
1756 }
1757 #else
1758 preempt(1);
1759 #endif
1760 KASSERT(mutex_owned(vp->v_interlock));
1761 }
1762
1763 /*
1764 * Make sure that for all pages in every block in the given range,
1765 * either all are dirty or all are clean. If any of the pages
1766 * we've seen so far are dirty, put the vnode on the paging chain,
1767 * and mark it IN_PAGING.
1768 *
1769 * If checkfirst != 0, don't check all the pages but return at the
1770 * first dirty page.
1771 */
1772 static int
1773 check_dirty(struct lfs *fs, struct vnode *vp,
1774 off_t startoffset, off_t endoffset, off_t blkeof,
1775 int flags, int checkfirst, struct vm_page **pgp)
1776 {
1777 int by_list;
1778 struct vm_page *curpg = NULL; /* XXX: gcc */
1779 struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
1780 off_t soff = 0; /* XXX: gcc */
1781 voff_t off;
1782 int i;
1783 int nonexistent;
1784 int any_dirty; /* number of dirty pages */
1785 int dirty; /* number of dirty pages in a block */
1786 int tdirty;
1787 int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
1788 int pagedaemon = (curlwp == uvm.pagedaemon_lwp);
1789
1790 KASSERT(mutex_owned(vp->v_interlock));
1791 ASSERT_MAYBE_SEGLOCK(fs);
1792 top:
1793 by_list = (vp->v_uobj.uo_npages <=
1794 ((endoffset - startoffset) >> PAGE_SHIFT) *
1795 UVM_PAGE_TREE_PENALTY);
1796 any_dirty = 0;
1797
1798 if (by_list) {
1799 curpg = TAILQ_FIRST(&vp->v_uobj.memq);
1800 } else {
1801 soff = startoffset;
1802 }
1803 while (by_list || soff < MIN(blkeof, endoffset)) {
1804 if (by_list) {
1805 /*
1806 * Find the first page in a block. Skip
1807 * blocks outside our area of interest or beyond
1808 * the end of file.
1809 */
1810 KASSERT(curpg == NULL
1811 || (curpg->flags & PG_MARKER) == 0);
1812 if (pages_per_block > 1) {
1813 while (curpg &&
1814 ((curpg->offset & fs->lfs_bmask) ||
1815 curpg->offset >= vp->v_size ||
1816 curpg->offset >= endoffset)) {
1817 curpg = TAILQ_NEXT(curpg, listq.queue);
1818 KASSERT(curpg == NULL ||
1819 (curpg->flags & PG_MARKER) == 0);
1820 }
1821 }
1822 if (curpg == NULL)
1823 break;
1824 soff = curpg->offset;
1825 }
1826
1827 /*
1828 * Mark all pages in extended range busy; find out if any
1829 * of them are dirty.
1830 */
1831 nonexistent = dirty = 0;
1832 for (i = 0; i == 0 || i < pages_per_block; i++) {
1833 KASSERT(mutex_owned(vp->v_interlock));
1834 if (by_list && pages_per_block <= 1) {
1835 pgs[i] = pg = curpg;
1836 } else {
1837 off = soff + (i << PAGE_SHIFT);
1838 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
1839 if (pg == NULL) {
1840 ++nonexistent;
1841 continue;
1842 }
1843 }
1844 KASSERT(pg != NULL);
1845
1846 /*
1847 * If we're holding the segment lock, we can deadlock
1848 * against a process that has our page and is waiting
1849 * for the cleaner, while the cleaner waits for the
1850 * segment lock. Just bail in that case.
1851 */
1852 if ((pg->flags & PG_BUSY) &&
1853 (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
1854 if (i > 0)
1855 uvm_page_unbusy(pgs, i);
1856 DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
1857 if (pgp)
1858 *pgp = pg;
1859 KASSERT(mutex_owned(vp->v_interlock));
1860 return -1;
1861 }
1862
1863 while (pg->flags & PG_BUSY) {
1864 wait_for_page(vp, pg, NULL);
1865 KASSERT(mutex_owned(vp->v_interlock));
1866 if (i > 0)
1867 uvm_page_unbusy(pgs, i);
1868 KASSERT(mutex_owned(vp->v_interlock));
1869 goto top;
1870 }
1871 pg->flags |= PG_BUSY;
1872 UVM_PAGE_OWN(pg, "lfs_putpages");
1873
1874 pmap_page_protect(pg, VM_PROT_NONE);
1875 tdirty = (pmap_clear_modify(pg) ||
1876 (pg->flags & PG_CLEAN) == 0);
1877 dirty += tdirty;
1878 }
1879 if (pages_per_block > 0 && nonexistent >= pages_per_block) {
1880 if (by_list) {
1881 curpg = TAILQ_NEXT(curpg, listq.queue);
1882 } else {
1883 soff += fs->lfs_bsize;
1884 }
1885 continue;
1886 }
1887
1888 any_dirty += dirty;
1889 KASSERT(nonexistent == 0);
1890 KASSERT(mutex_owned(vp->v_interlock));
1891
1892 /*
1893 * If any are dirty make all dirty; unbusy them,
1894 * but if we were asked to clean, wire them so that
1895 * the pagedaemon doesn't bother us about them while
1896 * they're on their way to disk.
1897 */
1898 for (i = 0; i == 0 || i < pages_per_block; i++) {
1899 KASSERT(mutex_owned(vp->v_interlock));
1900 pg = pgs[i];
1901 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
1902 KASSERT(pg->flags & PG_BUSY);
1903 if (dirty) {
1904 pg->flags &= ~PG_CLEAN;
1905 if (flags & PGO_FREE) {
1906 /*
1907 * Wire the page so that
1908 * pdaemon doesn't see it again.
1909 */
1910 mutex_enter(&uvm_pageqlock);
1911 uvm_pagewire(pg);
1912 mutex_exit(&uvm_pageqlock);
1913
1914 /* Suspended write flag */
1915 pg->flags |= PG_DELWRI;
1916 }
1917 }
1918 if (pg->flags & PG_WANTED)
1919 wakeup(pg);
1920 pg->flags &= ~(PG_WANTED|PG_BUSY);
1921 UVM_PAGE_OWN(pg, NULL);
1922 }
1923
1924 if (checkfirst && any_dirty)
1925 break;
1926
1927 if (by_list) {
1928 curpg = TAILQ_NEXT(curpg, listq.queue);
1929 } else {
1930 soff += MAX(PAGE_SIZE, fs->lfs_bsize);
1931 }
1932 }
1933
1934 KASSERT(mutex_owned(vp->v_interlock));
1935 return any_dirty;
1936 }
1937
1938 /*
1939 * lfs_putpages functions like genfs_putpages except that
1940 *
1941 * (1) It needs to bounds-check the incoming requests to ensure that
1942 * they are block-aligned; if they are not, expand the range and
1943 * do the right thing in case, e.g., the requested range is clean
1944 * but the expanded range is dirty.
1945 *
1946 * (2) It needs to explicitly send blocks to be written when it is done.
1947 * If VOP_PUTPAGES is called without the seglock held, we simply take
1948 * the seglock and let lfs_segunlock wait for us.
1949 * XXX There might be a bad situation if we have to flush a vnode while
1950 * XXX lfs_markv is in operation. As of this writing we panic in this
1951 * XXX case.
1952 *
1953 * Assumptions:
1954 *
1955 * (1) The caller does not hold any pages in this vnode busy. If it does,
1956 * there is a danger that when we expand the page range and busy the
1957 * pages we will deadlock.
1958 *
1959 * (2) We are called with vp->v_interlock held; we must return with it
1960 * released.
1961 *
1962 * (3) We don't absolutely have to free pages right away, provided that
1963 * the request does not have PGO_SYNCIO. When the pagedaemon gives
1964 * us a request with PGO_FREE, we take the pages out of the paging
1965 * queue and wake up the writer, which will handle freeing them for us.
1966 *
1967 * We ensure that for any filesystem block, all pages for that
1968 * block are either resident or not, even if those pages are higher
1969 * than EOF; that means that we will be getting requests to free
1970 * "unused" pages above EOF all the time, and should ignore them.
1971 *
1972 * (4) If we are called with PGO_LOCKED, the finfo array we are to write
1973 * into has been set up for us by lfs_writefile. If not, we will
1974 * have to handle allocating and/or freeing an finfo entry.
1975 *
1976 * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
1977 */
1978
1979 /* How many times to loop before we should start to worry */
1980 #define TOOMANY 4
1981
1982 int
1983 lfs_putpages(void *v)
1984 {
1985 int error;
1986 struct vop_putpages_args /* {
1987 struct vnode *a_vp;
1988 voff_t a_offlo;
1989 voff_t a_offhi;
1990 int a_flags;
1991 } */ *ap = v;
1992 struct vnode *vp;
1993 struct inode *ip;
1994 struct lfs *fs;
1995 struct segment *sp;
1996 off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
1997 off_t off, max_endoffset;
1998 bool seglocked, sync, pagedaemon, reclaim;
1999 struct vm_page *pg, *busypg;
2000 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
2001 int oreclaim = 0;
2002 int donewriting = 0;
2003 #ifdef DEBUG
2004 int debug_n_again, debug_n_dirtyclean;
2005 #endif
2006
2007 vp = ap->a_vp;
2008 ip = VTOI(vp);
2009 fs = ip->i_lfs;
2010 sync = (ap->a_flags & PGO_SYNCIO) != 0;
2011 reclaim = (ap->a_flags & PGO_RECLAIM) != 0;
2012 pagedaemon = (curlwp == uvm.pagedaemon_lwp);
2013
2014 KASSERT(mutex_owned(vp->v_interlock));
2015
2016 /* Putpages does nothing for metadata. */
2017 if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
2018 mutex_exit(vp->v_interlock);
2019 return 0;
2020 }
2021
2022 /*
2023 * If there are no pages, don't do anything.
2024 */
2025 if (vp->v_uobj.uo_npages == 0) {
2026 if (TAILQ_EMPTY(&vp->v_uobj.memq) &&
2027 (vp->v_iflag & VI_ONWORKLST) &&
2028 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
2029 vp->v_iflag &= ~VI_WRMAPDIRTY;
2030 vn_syncer_remove_from_worklist(vp);
2031 }
2032 mutex_exit(vp->v_interlock);
2033
2034 /* Remove us from paging queue, if we were on it */
2035 mutex_enter(&lfs_lock);
2036 if (ip->i_flags & IN_PAGING) {
2037 ip->i_flags &= ~IN_PAGING;
2038 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2039 }
2040 mutex_exit(&lfs_lock);
2041
2042 KASSERT(!mutex_owned(vp->v_interlock));
2043 return 0;
2044 }
2045
2046 blkeof = lfs_blkroundup(fs, ip->i_size);
2047
2048 /*
2049 * Ignore requests to free pages past EOF but in the same block
2050 * as EOF, unless the vnode is being reclaimed or the request
2051 * is synchronous. (If the request is sync, it comes from
2052 * lfs_truncate.)
2053 *
2054 * To avoid being flooded with this request, make these pages
2055 * look "active".
2056 */
2057 if (!sync && !reclaim &&
2058 ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
2059 origoffset = ap->a_offlo;
2060 for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
2061 pg = uvm_pagelookup(&vp->v_uobj, off);
2062 KASSERT(pg != NULL);
2063 while (pg->flags & PG_BUSY) {
2064 pg->flags |= PG_WANTED;
2065 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0,
2066 "lfsput2", 0);
2067 mutex_enter(vp->v_interlock);
2068 }
2069 mutex_enter(&uvm_pageqlock);
2070 uvm_pageactivate(pg);
2071 mutex_exit(&uvm_pageqlock);
2072 }
2073 ap->a_offlo = blkeof;
2074 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
2075 mutex_exit(vp->v_interlock);
2076 return 0;
2077 }
2078 }
2079
2080 /*
2081 * Extend page range to start and end at block boundaries.
2082 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
2083 */
2084 origoffset = ap->a_offlo;
2085 origendoffset = ap->a_offhi;
2086 startoffset = origoffset & ~(fs->lfs_bmask);
2087 max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
2088 << fs->lfs_bshift;
2089
2090 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
2091 endoffset = max_endoffset;
2092 origendoffset = endoffset;
2093 } else {
2094 origendoffset = round_page(ap->a_offhi);
2095 endoffset = round_page(lfs_blkroundup(fs, origendoffset));
2096 }
2097
2098 KASSERT(startoffset > 0 || endoffset >= startoffset);
2099 if (startoffset == endoffset) {
2100 /* Nothing to do, why were we called? */
2101 mutex_exit(vp->v_interlock);
2102 DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
2103 PRId64 "\n", startoffset));
2104 return 0;
2105 }
2106
2107 ap->a_offlo = startoffset;
2108 ap->a_offhi = endoffset;
2109
2110 /*
2111 * If not cleaning, just send the pages through genfs_putpages
2112 * to be returned to the pool.
2113 */
2114 if (!(ap->a_flags & PGO_CLEANIT)) {
2115 DLOG((DLOG_PAGE, "lfs_putpages: no cleanit vn %p ino %d (flags %x)\n",
2116 vp, (int)ip->i_number, ap->a_flags));
2117 int r = genfs_putpages(v);
2118 KASSERT(!mutex_owned(vp->v_interlock));
2119 return r;
2120 }
2121
2122 /* Set PGO_BUSYFAIL to avoid deadlocks */
2123 ap->a_flags |= PGO_BUSYFAIL;
2124
2125 /*
2126 * Likewise, if we are asked to clean but the pages are not
2127 * dirty, we can just free them using genfs_putpages.
2128 */
2129 #ifdef DEBUG
2130 debug_n_dirtyclean = 0;
2131 #endif
2132 do {
2133 int r;
2134 KASSERT(mutex_owned(vp->v_interlock));
2135
2136 /* Count the number of dirty pages */
2137 r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
2138 ap->a_flags, 1, NULL);
2139 if (r < 0) {
2140 /* Pages are busy with another process */
2141 mutex_exit(vp->v_interlock);
2142 return EDEADLK;
2143 }
2144 if (r > 0) /* Some pages are dirty */
2145 break;
2146
2147 /*
2148 * Sometimes pages are dirtied between the time that
2149 * we check and the time we try to clean them.
2150 * Instruct lfs_gop_write to return EDEADLK in this case
2151 * so we can write them properly.
2152 */
2153 ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
2154 r = genfs_do_putpages(vp, startoffset, endoffset,
2155 ap->a_flags & ~PGO_SYNCIO, &busypg);
2156 ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
2157 if (r != EDEADLK) {
2158 KASSERT(!mutex_owned(vp->v_interlock));
2159 return r;
2160 }
2161
2162 /* One of the pages was busy. Start over. */
2163 mutex_enter(vp->v_interlock);
2164 wait_for_page(vp, busypg, "dirtyclean");
2165 #ifdef DEBUG
2166 ++debug_n_dirtyclean;
2167 #endif
2168 } while(1);
2169
2170 #ifdef DEBUG
2171 if (debug_n_dirtyclean > TOOMANY)
2172 DLOG((DLOG_PAGE, "lfs_putpages: dirtyclean: looping, n = %d\n",
2173 debug_n_dirtyclean));
2174 #endif
2175
2176 /*
2177 * Dirty and asked to clean.
2178 *
2179 * Pagedaemon can't actually write LFS pages; wake up
2180 * the writer to take care of that. The writer will
2181 * notice the pager inode queue and act on that.
2182 *
2183 * XXX We must drop the vp->interlock before taking the lfs_lock or we
2184 * get a nasty deadlock with lfs_flush_pchain().
2185 */
2186 if (pagedaemon) {
2187 mutex_exit(vp->v_interlock);
2188 mutex_enter(&lfs_lock);
2189 if (!(ip->i_flags & IN_PAGING)) {
2190 ip->i_flags |= IN_PAGING;
2191 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2192 }
2193 wakeup(&lfs_writer_daemon);
2194 mutex_exit(&lfs_lock);
2195 preempt();
2196 KASSERT(!mutex_owned(vp->v_interlock));
2197 return EWOULDBLOCK;
2198 }
2199
2200 /*
2201 * If this is a file created in a recent dirop, we can't flush its
2202 * inode until the dirop is complete. Drain dirops, then flush the
2203 * filesystem (taking care of any other pending dirops while we're
2204 * at it).
2205 */
2206 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
2207 (vp->v_uflag & VU_DIROP)) {
2208 DLOG((DLOG_PAGE, "lfs_putpages: flushing VU_DIROP\n"));
2209
2210 lfs_writer_enter(fs, "ppdirop");
2211
2212 /* Note if we hold the vnode locked */
2213 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
2214 {
2215 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode already locked\n"));
2216 } else {
2217 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode not locked\n"));
2218 }
2219 mutex_exit(vp->v_interlock);
2220
2221 mutex_enter(&lfs_lock);
2222 lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
2223 mutex_exit(&lfs_lock);
2224
2225 mutex_enter(vp->v_interlock);
2226 lfs_writer_leave(fs);
2227
2228 /* The flush will have cleaned out this vnode as well,
2229 no need to do more to it. */
2230 }
2231
2232 /*
2233 * This is it. We are going to write some pages. From here on
2234 * down it's all just mechanics.
2235 *
2236 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
2237 */
2238 ap->a_flags &= ~PGO_SYNCIO;
2239
2240 /*
2241 * If we've already got the seglock, flush the node and return.
2242 * The FIP has already been set up for us by lfs_writefile,
2243 * and FIP cleanup and lfs_updatemeta will also be done there,
2244 * unless genfs_putpages returns EDEADLK; then we must flush
2245 * what we have, and correct FIP and segment header accounting.
2246 */
2247 get_seglock:
2248 /*
2249 * If we are not called with the segment locked, lock it.
2250 * Account for a new FIP in the segment header, and set sp->vp.
2251 * (This should duplicate the setup at the top of lfs_writefile().)
2252 */
2253 seglocked = (ap->a_flags & PGO_LOCKED) != 0;
2254 if (!seglocked) {
2255 mutex_exit(vp->v_interlock);
2256 error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
2257 if (error != 0) {
2258 KASSERT(!mutex_owned(vp->v_interlock));
2259 return error;
2260 }
2261 mutex_enter(vp->v_interlock);
2262 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2263 }
2264 sp = fs->lfs_sp;
2265 KASSERT(sp->vp == NULL);
2266 sp->vp = vp;
2267
2268 /* Note segments written by reclaim; only for debugging */
2269 if ((vp->v_iflag & VI_XLOCK) != 0) {
2270 sp->seg_flags |= SEGM_RECLAIM;
2271 fs->lfs_reclino = ip->i_number;
2272 }
2273
2274 /*
2275 * Ensure that the partial segment is marked SS_DIROP if this
2276 * vnode is a DIROP.
2277 */
2278 if (!seglocked && vp->v_uflag & VU_DIROP)
2279 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
2280
2281 /*
2282 * Loop over genfs_putpages until all pages are gathered.
2283 * genfs_putpages() drops the interlock, so reacquire it if necessary.
2284 * Whenever we lose the interlock we have to rerun check_dirty, as
2285 * well, since more pages might have been dirtied in our absence.
2286 */
2287 #ifdef DEBUG
2288 debug_n_again = 0;
2289 #endif
2290 do {
2291 busypg = NULL;
2292 KASSERT(mutex_owned(vp->v_interlock));
2293 if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
2294 ap->a_flags, 0, &busypg) < 0) {
2295 mutex_exit(vp->v_interlock);
2296 /* XXX why? --ks */
2297 mutex_enter(vp->v_interlock);
2298 write_and_wait(fs, vp, busypg, seglocked, NULL);
2299 if (!seglocked) {
2300 mutex_exit(vp->v_interlock);
2301 lfs_release_finfo(fs);
2302 lfs_segunlock(fs);
2303 mutex_enter(vp->v_interlock);
2304 }
2305 sp->vp = NULL;
2306 goto get_seglock;
2307 }
2308
2309 busypg = NULL;
2310 KASSERT(!mutex_owned(&uvm_pageqlock));
2311 oreclaim = (ap->a_flags & PGO_RECLAIM);
2312 ap->a_flags &= ~PGO_RECLAIM;
2313 error = genfs_do_putpages(vp, startoffset, endoffset,
2314 ap->a_flags, &busypg);
2315 ap->a_flags |= oreclaim;
2316
2317 if (error == EDEADLK || error == EAGAIN) {
2318 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
2319 " %d ino %d off %x (seg %d)\n", error,
2320 ip->i_number, fs->lfs_offset,
2321 lfs_dtosn(fs, fs->lfs_offset)));
2322
2323 if (oreclaim) {
2324 mutex_enter(vp->v_interlock);
2325 write_and_wait(fs, vp, busypg, seglocked, "again");
2326 mutex_exit(vp->v_interlock);
2327 } else {
2328 if ((sp->seg_flags & SEGM_SINGLE) &&
2329 fs->lfs_curseg != fs->lfs_startseg)
2330 donewriting = 1;
2331 }
2332 } else if (error) {
2333 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
2334 " %d ino %d off %x (seg %d)\n", error,
2335 (int)ip->i_number, fs->lfs_offset,
2336 lfs_dtosn(fs, fs->lfs_offset)));
2337 }
2338 /* genfs_do_putpages loses the interlock */
2339 #ifdef DEBUG
2340 ++debug_n_again;
2341 #endif
2342 if (oreclaim && error == EAGAIN) {
2343 DLOG((DLOG_PAGE, "vp %p ino %d vi_flags %x a_flags %x avoiding vclean panic\n",
2344 vp, (int)ip->i_number, vp->v_iflag, ap->a_flags));
2345 mutex_enter(vp->v_interlock);
2346 }
2347 if (error == EDEADLK)
2348 mutex_enter(vp->v_interlock);
2349 } while (error == EDEADLK || (oreclaim && error == EAGAIN));
2350 #ifdef DEBUG
2351 if (debug_n_again > TOOMANY)
2352 DLOG((DLOG_PAGE, "lfs_putpages: again: looping, n = %d\n", debug_n_again));
2353 #endif
2354
2355 KASSERT(sp != NULL && sp->vp == vp);
2356 if (!seglocked && !donewriting) {
2357 sp->vp = NULL;
2358
2359 /* Write indirect blocks as well */
2360 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
2361 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
2362 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
2363
2364 KASSERT(sp->vp == NULL);
2365 sp->vp = vp;
2366 }
2367
2368 /*
2369 * Blocks are now gathered into a segment waiting to be written.
2370 * All that's left to do is update metadata, and write them.
2371 */
2372 lfs_updatemeta(sp);
2373 KASSERT(sp->vp == vp);
2374 sp->vp = NULL;
2375
2376 /*
2377 * If we were called from lfs_writefile, we don't need to clean up
2378 * the FIP or unlock the segment lock. We're done.
2379 */
2380 if (seglocked) {
2381 KASSERT(!mutex_owned(vp->v_interlock));
2382 return error;
2383 }
2384
2385 /* Clean up FIP and send it to disk. */
2386 lfs_release_finfo(fs);
2387 lfs_writeseg(fs, fs->lfs_sp);
2388
2389 /*
2390 * Remove us from paging queue if we wrote all our pages.
2391 */
2392 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
2393 mutex_enter(&lfs_lock);
2394 if (ip->i_flags & IN_PAGING) {
2395 ip->i_flags &= ~IN_PAGING;
2396 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2397 }
2398 mutex_exit(&lfs_lock);
2399 }
2400
2401 /*
2402 * XXX - with the malloc/copy writeseg, the pages are freed by now
2403 * even if we don't wait (e.g. if we hold a nested lock). This
2404 * will not be true if we stop using malloc/copy.
2405 */
2406 KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
2407 lfs_segunlock(fs);
2408
2409 /*
2410 * Wait for v_numoutput to drop to zero. The seglock should
2411 * take care of this, but there is a slight possibility that
2412 * aiodoned might not have got around to our buffers yet.
2413 */
2414 if (sync) {
2415 mutex_enter(vp->v_interlock);
2416 while (vp->v_numoutput > 0) {
2417 DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
2418 " num %d\n", ip->i_number, vp->v_numoutput));
2419 cv_wait(&vp->v_cv, vp->v_interlock);
2420 }
2421 mutex_exit(vp->v_interlock);
2422 }
2423 KASSERT(!mutex_owned(vp->v_interlock));
2424 return error;
2425 }
2426
2427 /*
2428 * Return the last logical file offset that should be written for this file
2429 * if we're doing a write that ends at "size". If writing, we need to know
2430 * about sizes on disk, i.e. fragments if there are any; if reading, we need
2431 * to know about entire blocks.
2432 */
2433 void
2434 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
2435 {
2436 struct inode *ip = VTOI(vp);
2437 struct lfs *fs = ip->i_lfs;
2438 daddr_t olbn, nlbn;
2439
2440 olbn = lfs_lblkno(fs, ip->i_size);
2441 nlbn = lfs_lblkno(fs, size);
2442 if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
2443 *eobp = lfs_fragroundup(fs, size);
2444 } else {
2445 *eobp = lfs_blkroundup(fs, size);
2446 }
2447 }
2448
2449 #ifdef DEBUG
2450 void lfs_dump_vop(void *);
2451
2452 void
2453 lfs_dump_vop(void *v)
2454 {
2455 struct vop_putpages_args /* {
2456 struct vnode *a_vp;
2457 voff_t a_offlo;
2458 voff_t a_offhi;
2459 int a_flags;
2460 } */ *ap = v;
2461
2462 #ifdef DDB
2463 vfs_vnode_print(ap->a_vp, 0, printf);
2464 #endif
2465 lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
2466 }
2467 #endif
2468
2469 int
2470 lfs_mmap(void *v)
2471 {
2472 struct vop_mmap_args /* {
2473 const struct vnodeop_desc *a_desc;
2474 struct vnode *a_vp;
2475 vm_prot_t a_prot;
2476 kauth_cred_t a_cred;
2477 } */ *ap = v;
2478
2479 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
2480 return EOPNOTSUPP;
2481 return ulfs_mmap(v);
2482 }
2483
2484 static int
2485 lfs_openextattr(void *v)
2486 {
2487 struct vop_openextattr_args /* {
2488 struct vnode *a_vp;
2489 kauth_cred_t a_cred;
2490 struct proc *a_p;
2491 } */ *ap = v;
2492 struct inode *ip = VTOI(ap->a_vp);
2493 struct ulfsmount *ump = ip->i_ump;
2494 //struct lfs *fs = ip->i_lfs;
2495
2496 /* Not supported for ULFS1 file systems. */
2497 if (ump->um_fstype == ULFS1)
2498 return (EOPNOTSUPP);
2499
2500 /* XXX Not implemented for ULFS2 file systems. */
2501 return (EOPNOTSUPP);
2502 }
2503
2504 static int
2505 lfs_closeextattr(void *v)
2506 {
2507 struct vop_closeextattr_args /* {
2508 struct vnode *a_vp;
2509 int a_commit;
2510 kauth_cred_t a_cred;
2511 struct proc *a_p;
2512 } */ *ap = v;
2513 struct inode *ip = VTOI(ap->a_vp);
2514 struct ulfsmount *ump = ip->i_ump;
2515 //struct lfs *fs = ip->i_lfs;
2516
2517 /* Not supported for ULFS1 file systems. */
2518 if (ump->um_fstype == ULFS1)
2519 return (EOPNOTSUPP);
2520
2521 /* XXX Not implemented for ULFS2 file systems. */
2522 return (EOPNOTSUPP);
2523 }
2524
2525 static int
2526 lfs_getextattr(void *v)
2527 {
2528 struct vop_getextattr_args /* {
2529 struct vnode *a_vp;
2530 int a_attrnamespace;
2531 const char *a_name;
2532 struct uio *a_uio;
2533 size_t *a_size;
2534 kauth_cred_t a_cred;
2535 struct proc *a_p;
2536 } */ *ap = v;
2537 struct vnode *vp = ap->a_vp;
2538 struct inode *ip = VTOI(vp);
2539 struct ulfsmount *ump = ip->i_ump;
2540 //struct lfs *fs = ip->i_lfs;
2541 int error;
2542
2543 if (ump->um_fstype == ULFS1) {
2544 #ifdef LFS_EXTATTR
2545 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2546 error = ulfs_getextattr(ap);
2547 fstrans_done(vp->v_mount);
2548 #else
2549 error = EOPNOTSUPP;
2550 #endif
2551 return error;
2552 }
2553
2554 /* XXX Not implemented for ULFS2 file systems. */
2555 return (EOPNOTSUPP);
2556 }
2557
2558 static int
2559 lfs_setextattr(void *v)
2560 {
2561 struct vop_setextattr_args /* {
2562 struct vnode *a_vp;
2563 int a_attrnamespace;
2564 const char *a_name;
2565 struct uio *a_uio;
2566 kauth_cred_t a_cred;
2567 struct proc *a_p;
2568 } */ *ap = v;
2569 struct vnode *vp = ap->a_vp;
2570 struct inode *ip = VTOI(vp);
2571 struct ulfsmount *ump = ip->i_ump;
2572 //struct lfs *fs = ip->i_lfs;
2573 int error;
2574
2575 if (ump->um_fstype == ULFS1) {
2576 #ifdef LFS_EXTATTR
2577 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2578 error = ulfs_setextattr(ap);
2579 fstrans_done(vp->v_mount);
2580 #else
2581 error = EOPNOTSUPP;
2582 #endif
2583 return error;
2584 }
2585
2586 /* XXX Not implemented for ULFS2 file systems. */
2587 return (EOPNOTSUPP);
2588 }
2589
2590 static int
2591 lfs_listextattr(void *v)
2592 {
2593 struct vop_listextattr_args /* {
2594 struct vnode *a_vp;
2595 int a_attrnamespace;
2596 struct uio *a_uio;
2597 size_t *a_size;
2598 kauth_cred_t a_cred;
2599 struct proc *a_p;
2600 } */ *ap = v;
2601 struct vnode *vp = ap->a_vp;
2602 struct inode *ip = VTOI(vp);
2603 struct ulfsmount *ump = ip->i_ump;
2604 //struct lfs *fs = ip->i_lfs;
2605 int error;
2606
2607 if (ump->um_fstype == ULFS1) {
2608 #ifdef LFS_EXTATTR
2609 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2610 error = ulfs_listextattr(ap);
2611 fstrans_done(vp->v_mount);
2612 #else
2613 error = EOPNOTSUPP;
2614 #endif
2615 return error;
2616 }
2617
2618 /* XXX Not implemented for ULFS2 file systems. */
2619 return (EOPNOTSUPP);
2620 }
2621
2622 static int
2623 lfs_deleteextattr(void *v)
2624 {
2625 struct vop_deleteextattr_args /* {
2626 struct vnode *a_vp;
2627 int a_attrnamespace;
2628 kauth_cred_t a_cred;
2629 struct proc *a_p;
2630 } */ *ap = v;
2631 struct vnode *vp = ap->a_vp;
2632 struct inode *ip = VTOI(vp);
2633 struct ulfsmount *ump = ip->i_ump;
2634 //struct fs *fs = ip->i_lfs;
2635 int error;
2636
2637 if (ump->um_fstype == ULFS1) {
2638 #ifdef LFS_EXTATTR
2639 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2640 error = ulfs_deleteextattr(ap);
2641 fstrans_done(vp->v_mount);
2642 #else
2643 error = EOPNOTSUPP;
2644 #endif
2645 return error;
2646 }
2647
2648 /* XXX Not implemented for ULFS2 file systems. */
2649 return (EOPNOTSUPP);
2650 }
2651