lfs_vnops.c revision 1.259 1 /* $NetBSD: lfs_vnops.c,v 1.259 2013/10/18 15:15:22 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
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.259 2013/10/18 15:15:22 christos 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 struct lfs *fs = ip->i_lfs;
624 /*
625 * Want to be able to use this to make badblock
626 * inodes, so don't truncate the dev number.
627 */
628 if (ump->um_fstype == ULFS1)
629 ip->i_ffs1_rdev = ulfs_rw32(vap->va_rdev,
630 ULFS_MPNEEDSWAP(fs));
631 else
632 ip->i_ffs2_rdev = ulfs_rw64(vap->va_rdev,
633 ULFS_MPNEEDSWAP(fs));
634 }
635
636 /*
637 * Call fsync to write the vnode so that we don't have to deal with
638 * flushing it when it's marked VU_DIROP|VI_XLOCK.
639 *
640 * XXX KS - If we can't flush we also can't call vgone(), so must
641 * return. But, that leaves this vnode in limbo, also not good.
642 * Can this ever happen (barring hardware failure)?
643 */
644 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
645 panic("lfs_mknod: couldn't fsync (ino %llu)",
646 (unsigned long long)ino);
647 /* return (error); */
648 }
649 /*
650 * Remove vnode so that it will be reloaded by VFS_VGET and
651 * checked to see if it is an alias of an existing entry in
652 * the inode cache.
653 */
654 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
655
656 (*vpp)->v_type = VNON;
657 VOP_UNLOCK(*vpp);
658 vgone(*vpp);
659 error = VFS_VGET(mp, ino, vpp);
660
661 fstrans_done(ap->a_dvp->v_mount);
662 if (error != 0) {
663 *vpp = NULL;
664 return (error);
665 }
666 return (0);
667 }
668
669 int
670 lfs_create(void *v)
671 {
672 struct vop_create_args /* {
673 struct vnode *a_dvp;
674 struct vnode **a_vpp;
675 struct componentname *a_cnp;
676 struct vattr *a_vap;
677 } */ *ap = v;
678 int error;
679
680 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
681 vput(ap->a_dvp);
682 return error;
683 }
684 error = ulfs_create(ap);
685 SET_ENDOP_CREATE_AP(ap, "create");
686 return (error);
687 }
688
689 int
690 lfs_mkdir(void *v)
691 {
692 struct vop_mkdir_args /* {
693 struct vnode *a_dvp;
694 struct vnode **a_vpp;
695 struct componentname *a_cnp;
696 struct vattr *a_vap;
697 } */ *ap = v;
698 int error;
699
700 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
701 vput(ap->a_dvp);
702 return error;
703 }
704 error = ulfs_mkdir(ap);
705 SET_ENDOP_CREATE_AP(ap, "mkdir");
706 return (error);
707 }
708
709 int
710 lfs_remove(void *v)
711 {
712 struct vop_remove_args /* {
713 struct vnode *a_dvp;
714 struct vnode *a_vp;
715 struct componentname *a_cnp;
716 } */ *ap = v;
717 struct vnode *dvp, *vp;
718 struct inode *ip;
719 int error;
720
721 dvp = ap->a_dvp;
722 vp = ap->a_vp;
723 ip = VTOI(vp);
724 if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
725 if (dvp == vp)
726 vrele(vp);
727 else
728 vput(vp);
729 vput(dvp);
730 return error;
731 }
732 error = ulfs_remove(ap);
733 if (ip->i_nlink == 0)
734 lfs_orphan(ip->i_lfs, ip->i_number);
735 SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
736 return (error);
737 }
738
739 int
740 lfs_rmdir(void *v)
741 {
742 struct vop_rmdir_args /* {
743 struct vnodeop_desc *a_desc;
744 struct vnode *a_dvp;
745 struct vnode *a_vp;
746 struct componentname *a_cnp;
747 } */ *ap = v;
748 struct vnode *vp;
749 struct inode *ip;
750 int error;
751
752 vp = ap->a_vp;
753 ip = VTOI(vp);
754 if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
755 if (ap->a_dvp == vp)
756 vrele(ap->a_dvp);
757 else
758 vput(ap->a_dvp);
759 vput(vp);
760 return error;
761 }
762 error = ulfs_rmdir(ap);
763 if (ip->i_nlink == 0)
764 lfs_orphan(ip->i_lfs, ip->i_number);
765 SET_ENDOP_REMOVE(ip->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 = ulfs_link(ap);
785 SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
786 return (error);
787 }
788
789 /* XXX hack to avoid calling ITIMES in getattr */
790 int
791 lfs_getattr(void *v)
792 {
793 struct vop_getattr_args /* {
794 struct vnode *a_vp;
795 struct vattr *a_vap;
796 kauth_cred_t a_cred;
797 } */ *ap = v;
798 struct vnode *vp = ap->a_vp;
799 struct inode *ip = VTOI(vp);
800 struct vattr *vap = ap->a_vap;
801 struct lfs *fs = ip->i_lfs;
802
803 fstrans_start(vp->v_mount, FSTRANS_SHARED);
804 /*
805 * Copy from inode table
806 */
807 vap->va_fsid = ip->i_dev;
808 vap->va_fileid = ip->i_number;
809 vap->va_mode = ip->i_mode & ~LFS_IFMT;
810 vap->va_nlink = ip->i_nlink;
811 vap->va_uid = ip->i_uid;
812 vap->va_gid = ip->i_gid;
813 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
814 vap->va_size = vp->v_size;
815 vap->va_atime.tv_sec = ip->i_ffs1_atime;
816 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
817 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
818 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
819 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
820 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
821 vap->va_flags = ip->i_flags;
822 vap->va_gen = ip->i_gen;
823 /* this doesn't belong here */
824 if (vp->v_type == VBLK)
825 vap->va_blocksize = BLKDEV_IOSIZE;
826 else if (vp->v_type == VCHR)
827 vap->va_blocksize = MAXBSIZE;
828 else
829 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
830 vap->va_bytes = lfs_fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
831 vap->va_type = vp->v_type;
832 vap->va_filerev = ip->i_modrev;
833 fstrans_done(vp->v_mount);
834 return (0);
835 }
836
837 /*
838 * Check to make sure the inode blocks won't choke the buffer
839 * cache, then call ulfs_setattr as usual.
840 */
841 int
842 lfs_setattr(void *v)
843 {
844 struct vop_setattr_args /* {
845 struct vnode *a_vp;
846 struct vattr *a_vap;
847 kauth_cred_t a_cred;
848 } */ *ap = v;
849 struct vnode *vp = ap->a_vp;
850
851 lfs_check(vp, LFS_UNUSED_LBN, 0);
852 return ulfs_setattr(v);
853 }
854
855 /*
856 * Release the block we hold on lfs_newseg wrapping. Called on file close,
857 * or explicitly from LFCNWRAPGO. Called with the interlock held.
858 */
859 static int
860 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
861 {
862 if (fs->lfs_stoplwp != curlwp)
863 return EBUSY;
864
865 fs->lfs_stoplwp = NULL;
866 cv_signal(&fs->lfs_stopcv);
867
868 KASSERT(fs->lfs_nowrap > 0);
869 if (fs->lfs_nowrap <= 0) {
870 return 0;
871 }
872
873 if (--fs->lfs_nowrap == 0) {
874 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
875 wakeup(&fs->lfs_wrappass);
876 lfs_wakeup_cleaner(fs);
877 }
878 if (waitfor) {
879 mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
880 0, &lfs_lock);
881 }
882
883 return 0;
884 }
885
886 /*
887 * Close called.
888 *
889 * Update the times on the inode.
890 */
891 /* ARGSUSED */
892 int
893 lfs_close(void *v)
894 {
895 struct vop_close_args /* {
896 struct vnode *a_vp;
897 int a_fflag;
898 kauth_cred_t a_cred;
899 } */ *ap = v;
900 struct vnode *vp = ap->a_vp;
901 struct inode *ip = VTOI(vp);
902 struct lfs *fs = ip->i_lfs;
903
904 if ((ip->i_number == ULFS_ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
905 fs->lfs_stoplwp == curlwp) {
906 mutex_enter(&lfs_lock);
907 log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
908 lfs_wrapgo(fs, ip, 0);
909 mutex_exit(&lfs_lock);
910 }
911
912 if (vp == ip->i_lfs->lfs_ivnode &&
913 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
914 return 0;
915
916 fstrans_start(vp->v_mount, FSTRANS_SHARED);
917 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
918 LFS_ITIMES(ip, NULL, NULL, NULL);
919 }
920 fstrans_done(vp->v_mount);
921 return (0);
922 }
923
924 /*
925 * Close wrapper for special devices.
926 *
927 * Update the times on the inode then do device close.
928 */
929 int
930 lfsspec_close(void *v)
931 {
932 struct vop_close_args /* {
933 struct vnode *a_vp;
934 int a_fflag;
935 kauth_cred_t a_cred;
936 } */ *ap = v;
937 struct vnode *vp;
938 struct inode *ip;
939
940 vp = ap->a_vp;
941 ip = VTOI(vp);
942 if (vp->v_usecount > 1) {
943 LFS_ITIMES(ip, NULL, NULL, NULL);
944 }
945 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
946 }
947
948 /*
949 * Close wrapper for fifo's.
950 *
951 * Update the times on the inode then do device close.
952 */
953 int
954 lfsfifo_close(void *v)
955 {
956 struct vop_close_args /* {
957 struct vnode *a_vp;
958 int a_fflag;
959 kauth_cred_ a_cred;
960 } */ *ap = v;
961 struct vnode *vp;
962 struct inode *ip;
963
964 vp = ap->a_vp;
965 ip = VTOI(vp);
966 if (ap->a_vp->v_usecount > 1) {
967 LFS_ITIMES(ip, NULL, NULL, NULL);
968 }
969 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
970 }
971
972 /*
973 * Reclaim an inode so that it can be used for other purposes.
974 */
975
976 int
977 lfs_reclaim(void *v)
978 {
979 struct vop_reclaim_args /* {
980 struct vnode *a_vp;
981 } */ *ap = v;
982 struct vnode *vp = ap->a_vp;
983 struct inode *ip = VTOI(vp);
984 struct lfs *fs = ip->i_lfs;
985 int error;
986
987 /*
988 * The inode must be freed and updated before being removed
989 * from its hash chain. Other threads trying to gain a hold
990 * on the inode will be stalled because it is locked (VI_XLOCK).
991 */
992 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
993 lfs_vfree(vp, ip->i_number, ip->i_omode);
994
995 mutex_enter(&lfs_lock);
996 LFS_CLR_UINO(ip, IN_ALLMOD);
997 mutex_exit(&lfs_lock);
998 if ((error = ulfs_reclaim(vp)))
999 return (error);
1000
1001 /*
1002 * Take us off the paging and/or dirop queues if we were on them.
1003 * We shouldn't be on them.
1004 */
1005 mutex_enter(&lfs_lock);
1006 if (ip->i_flags & IN_PAGING) {
1007 log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
1008 fs->lfs_fsmnt);
1009 ip->i_flags &= ~IN_PAGING;
1010 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1011 }
1012 if (vp->v_uflag & VU_DIROP) {
1013 panic("reclaimed vnode is VU_DIROP");
1014 vp->v_uflag &= ~VU_DIROP;
1015 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
1016 }
1017 mutex_exit(&lfs_lock);
1018
1019 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1020 lfs_deregister_all(vp);
1021 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1022 ip->inode_ext.lfs = NULL;
1023 genfs_node_destroy(vp);
1024 pool_put(&lfs_inode_pool, vp->v_data);
1025 vp->v_data = NULL;
1026 return (0);
1027 }
1028
1029 /*
1030 * Read a block from a storage device.
1031 *
1032 * Calculate the logical to physical mapping if not done already,
1033 * then call the device strategy routine.
1034 *
1035 * In order to avoid reading blocks that are in the process of being
1036 * written by the cleaner---and hence are not mutexed by the normal
1037 * buffer cache / page cache mechanisms---check for collisions before
1038 * reading.
1039 *
1040 * We inline ulfs_strategy to make sure that the VOP_BMAP occurs *before*
1041 * the active cleaner test.
1042 *
1043 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1044 */
1045 int
1046 lfs_strategy(void *v)
1047 {
1048 struct vop_strategy_args /* {
1049 struct vnode *a_vp;
1050 struct buf *a_bp;
1051 } */ *ap = v;
1052 struct buf *bp;
1053 struct lfs *fs;
1054 struct vnode *vp;
1055 struct inode *ip;
1056 daddr_t tbn;
1057 #define MAXLOOP 25
1058 int i, sn, error, slept, loopcount;
1059
1060 bp = ap->a_bp;
1061 vp = ap->a_vp;
1062 ip = VTOI(vp);
1063 fs = ip->i_lfs;
1064
1065 /* lfs uses its strategy routine only for read */
1066 KASSERT(bp->b_flags & B_READ);
1067
1068 if (vp->v_type == VBLK || vp->v_type == VCHR)
1069 panic("lfs_strategy: spec");
1070 KASSERT(bp->b_bcount != 0);
1071 if (bp->b_blkno == bp->b_lblkno) {
1072 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1073 NULL);
1074 if (error) {
1075 bp->b_error = error;
1076 bp->b_resid = bp->b_bcount;
1077 biodone(bp);
1078 return (error);
1079 }
1080 if ((long)bp->b_blkno == -1) /* no valid data */
1081 clrbuf(bp);
1082 }
1083 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1084 bp->b_resid = bp->b_bcount;
1085 biodone(bp);
1086 return (0);
1087 }
1088
1089 slept = 1;
1090 loopcount = 0;
1091 mutex_enter(&lfs_lock);
1092 while (slept && fs->lfs_seglock) {
1093 mutex_exit(&lfs_lock);
1094 /*
1095 * Look through list of intervals.
1096 * There will only be intervals to look through
1097 * if the cleaner holds the seglock.
1098 * Since the cleaner is synchronous, we can trust
1099 * the list of intervals to be current.
1100 */
1101 tbn = LFS_DBTOFSB(fs, bp->b_blkno);
1102 sn = lfs_dtosn(fs, tbn);
1103 slept = 0;
1104 for (i = 0; i < fs->lfs_cleanind; i++) {
1105 if (sn == lfs_dtosn(fs, fs->lfs_cleanint[i]) &&
1106 tbn >= fs->lfs_cleanint[i]) {
1107 DLOG((DLOG_CLEAN,
1108 "lfs_strategy: ino %d lbn %" PRId64
1109 " ind %d sn %d fsb %" PRIx32
1110 " given sn %d fsb %" PRIx64 "\n",
1111 ip->i_number, bp->b_lblkno, i,
1112 lfs_dtosn(fs, fs->lfs_cleanint[i]),
1113 fs->lfs_cleanint[i], sn, tbn));
1114 DLOG((DLOG_CLEAN,
1115 "lfs_strategy: sleeping on ino %d lbn %"
1116 PRId64 "\n", ip->i_number, bp->b_lblkno));
1117 mutex_enter(&lfs_lock);
1118 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
1119 /*
1120 * Cleaner can't wait for itself.
1121 * Instead, wait for the blocks
1122 * to be written to disk.
1123 * XXX we need pribio in the test
1124 * XXX here.
1125 */
1126 mtsleep(&fs->lfs_iocount,
1127 (PRIBIO + 1) | PNORELOCK,
1128 "clean2", hz/10 + 1,
1129 &lfs_lock);
1130 slept = 1;
1131 ++loopcount;
1132 break;
1133 } else if (fs->lfs_seglock) {
1134 mtsleep(&fs->lfs_seglock,
1135 (PRIBIO + 1) | PNORELOCK,
1136 "clean1", 0,
1137 &lfs_lock);
1138 slept = 1;
1139 break;
1140 }
1141 mutex_exit(&lfs_lock);
1142 }
1143 }
1144 mutex_enter(&lfs_lock);
1145 if (loopcount > MAXLOOP) {
1146 printf("lfs_strategy: breaking out of clean2 loop\n");
1147 break;
1148 }
1149 }
1150 mutex_exit(&lfs_lock);
1151
1152 vp = ip->i_devvp;
1153 return VOP_STRATEGY(vp, bp);
1154 }
1155
1156 /*
1157 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
1158 * Technically this is a checkpoint (the on-disk state is valid)
1159 * even though we are leaving out all the file data.
1160 */
1161 int
1162 lfs_flush_dirops(struct lfs *fs)
1163 {
1164 struct inode *ip, *nip;
1165 struct vnode *vp;
1166 extern int lfs_dostats;
1167 struct segment *sp;
1168 int flags = 0;
1169 int error = 0;
1170
1171 ASSERT_MAYBE_SEGLOCK(fs);
1172 KASSERT(fs->lfs_nadirop == 0);
1173
1174 if (fs->lfs_ronly)
1175 return EROFS;
1176
1177 mutex_enter(&lfs_lock);
1178 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
1179 mutex_exit(&lfs_lock);
1180 return 0;
1181 } else
1182 mutex_exit(&lfs_lock);
1183
1184 if (lfs_dostats)
1185 ++lfs_stats.flush_invoked;
1186
1187 lfs_imtime(fs);
1188 lfs_seglock(fs, flags);
1189 sp = fs->lfs_sp;
1190
1191 /*
1192 * lfs_writevnodes, optimized to get dirops out of the way.
1193 * Only write dirops, and don't flush files' pages, only
1194 * blocks from the directories.
1195 *
1196 * We don't need to vref these files because they are
1197 * dirops and so hold an extra reference until the
1198 * segunlock clears them of that status.
1199 *
1200 * We don't need to check for IN_ADIROP because we know that
1201 * no dirops are active.
1202 *
1203 */
1204 mutex_enter(&lfs_lock);
1205 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
1206 nip = TAILQ_NEXT(ip, i_lfs_dchain);
1207 mutex_exit(&lfs_lock);
1208 vp = ITOV(ip);
1209
1210 KASSERT((ip->i_flag & IN_ADIROP) == 0);
1211 KASSERT(vp->v_uflag & VU_DIROP);
1212 KASSERT(!(vp->v_iflag & VI_XLOCK));
1213
1214 /*
1215 * All writes to directories come from dirops; all
1216 * writes to files' direct blocks go through the page
1217 * cache, which we're not touching. Reads to files
1218 * and/or directories will not be affected by writing
1219 * directory blocks inodes and file inodes. So we don't
1220 * really need to lock.
1221 */
1222 if (vp->v_iflag & VI_XLOCK) {
1223 mutex_enter(&lfs_lock);
1224 continue;
1225 }
1226 /* XXX see below
1227 * waslocked = VOP_ISLOCKED(vp);
1228 */
1229 if (vp->v_type != VREG &&
1230 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
1231 error = lfs_writefile(fs, sp, vp);
1232 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1233 !(ip->i_flag & IN_ALLMOD)) {
1234 mutex_enter(&lfs_lock);
1235 LFS_SET_UINO(ip, IN_MODIFIED);
1236 mutex_exit(&lfs_lock);
1237 }
1238 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1239 mutex_enter(&lfs_lock);
1240 error = EAGAIN;
1241 break;
1242 }
1243 }
1244 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1245 error = lfs_writeinode(fs, sp, ip);
1246 mutex_enter(&lfs_lock);
1247 if (error && (sp->seg_flags & SEGM_SINGLE)) {
1248 error = EAGAIN;
1249 break;
1250 }
1251
1252 /*
1253 * We might need to update these inodes again,
1254 * for example, if they have data blocks to write.
1255 * Make sure that after this flush, they are still
1256 * marked IN_MODIFIED so that we don't forget to
1257 * write them.
1258 */
1259 /* XXX only for non-directories? --KS */
1260 LFS_SET_UINO(ip, IN_MODIFIED);
1261 }
1262 mutex_exit(&lfs_lock);
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 return error;
1270 }
1271
1272 /*
1273 * Flush all vnodes for which the pagedaemon has requested pageouts.
1274 * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
1275 * has just run, this would be an error). If we have to skip a vnode
1276 * for any reason, just skip it; if we have to wait for the cleaner,
1277 * abort. The writer daemon will call us again later.
1278 */
1279 int
1280 lfs_flush_pchain(struct lfs *fs)
1281 {
1282 struct inode *ip, *nip;
1283 struct vnode *vp;
1284 extern int lfs_dostats;
1285 struct segment *sp;
1286 int error, error2;
1287
1288 ASSERT_NO_SEGLOCK(fs);
1289
1290 if (fs->lfs_ronly)
1291 return EROFS;
1292
1293 mutex_enter(&lfs_lock);
1294 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
1295 mutex_exit(&lfs_lock);
1296 return 0;
1297 } else
1298 mutex_exit(&lfs_lock);
1299
1300 /* Get dirops out of the way */
1301 if ((error = lfs_flush_dirops(fs)) != 0)
1302 return error;
1303
1304 if (lfs_dostats)
1305 ++lfs_stats.flush_invoked;
1306
1307 /*
1308 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
1309 */
1310 lfs_imtime(fs);
1311 lfs_seglock(fs, 0);
1312 sp = fs->lfs_sp;
1313
1314 /*
1315 * lfs_writevnodes, optimized to clear pageout requests.
1316 * Only write non-dirop files that are in the pageout queue.
1317 * We're very conservative about what we write; we want to be
1318 * fast and async.
1319 */
1320 mutex_enter(&lfs_lock);
1321 top:
1322 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
1323 nip = TAILQ_NEXT(ip, i_lfs_pchain);
1324 vp = ITOV(ip);
1325
1326 if (!(ip->i_flags & IN_PAGING))
1327 goto top;
1328
1329 mutex_enter(vp->v_interlock);
1330 if ((vp->v_iflag & VI_XLOCK) || (vp->v_uflag & VU_DIROP) != 0) {
1331 mutex_exit(vp->v_interlock);
1332 continue;
1333 }
1334 if (vp->v_type != VREG) {
1335 mutex_exit(vp->v_interlock);
1336 continue;
1337 }
1338 if (lfs_vref(vp))
1339 continue;
1340 mutex_exit(&lfs_lock);
1341
1342 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
1343 lfs_vunref(vp);
1344 mutex_enter(&lfs_lock);
1345 continue;
1346 }
1347
1348 error = lfs_writefile(fs, sp, vp);
1349 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
1350 !(ip->i_flag & IN_ALLMOD)) {
1351 mutex_enter(&lfs_lock);
1352 LFS_SET_UINO(ip, IN_MODIFIED);
1353 mutex_exit(&lfs_lock);
1354 }
1355 KDASSERT(ip->i_number != LFS_IFILE_INUM);
1356 error2 = lfs_writeinode(fs, sp, ip);
1357
1358 VOP_UNLOCK(vp);
1359 lfs_vunref(vp);
1360
1361 if (error == EAGAIN || error2 == EAGAIN) {
1362 lfs_writeseg(fs, sp);
1363 mutex_enter(&lfs_lock);
1364 break;
1365 }
1366 mutex_enter(&lfs_lock);
1367 }
1368 mutex_exit(&lfs_lock);
1369 (void) lfs_writeseg(fs, sp);
1370 lfs_segunlock(fs);
1371
1372 return 0;
1373 }
1374
1375 /*
1376 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
1377 */
1378 int
1379 lfs_fcntl(void *v)
1380 {
1381 struct vop_fcntl_args /* {
1382 struct vnode *a_vp;
1383 u_int a_command;
1384 void * a_data;
1385 int a_fflag;
1386 kauth_cred_t a_cred;
1387 } */ *ap = v;
1388 struct timeval tv;
1389 struct timeval *tvp;
1390 BLOCK_INFO *blkiov;
1391 CLEANERINFO *cip;
1392 SEGUSE *sup;
1393 int blkcnt, error;
1394 size_t fh_size;
1395 struct lfs_fcntl_markv blkvp;
1396 struct lwp *l;
1397 fsid_t *fsidp;
1398 struct lfs *fs;
1399 struct buf *bp;
1400 fhandle_t *fhp;
1401 daddr_t off;
1402 int oclean;
1403
1404 /* Only respect LFS fcntls on fs root or Ifile */
1405 if (VTOI(ap->a_vp)->i_number != ULFS_ROOTINO &&
1406 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
1407 return ulfs_fcntl(v);
1408 }
1409
1410 /* Avoid locking a draining lock */
1411 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
1412 return ESHUTDOWN;
1413 }
1414
1415 /* LFS control and monitoring fcntls are available only to root */
1416 l = curlwp;
1417 if (((ap->a_command & 0xff00) >> 8) == 'L' &&
1418 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_LFS,
1419 KAUTH_REQ_SYSTEM_LFS_FCNTL, NULL, NULL, NULL)) != 0)
1420 return (error);
1421
1422 fs = VTOI(ap->a_vp)->i_lfs;
1423 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
1424
1425 error = 0;
1426 switch ((int)ap->a_command) {
1427 case LFCNSEGWAITALL_COMPAT_50:
1428 case LFCNSEGWAITALL_COMPAT:
1429 fsidp = NULL;
1430 /* FALLSTHROUGH */
1431 case LFCNSEGWAIT_COMPAT_50:
1432 case LFCNSEGWAIT_COMPAT:
1433 {
1434 struct timeval50 *tvp50
1435 = (struct timeval50 *)ap->a_data;
1436 timeval50_to_timeval(tvp50, &tv);
1437 tvp = &tv;
1438 }
1439 goto segwait_common;
1440 case LFCNSEGWAITALL:
1441 fsidp = NULL;
1442 /* FALLSTHROUGH */
1443 case LFCNSEGWAIT:
1444 tvp = (struct timeval *)ap->a_data;
1445 segwait_common:
1446 mutex_enter(&lfs_lock);
1447 ++fs->lfs_sleepers;
1448 mutex_exit(&lfs_lock);
1449
1450 error = lfs_segwait(fsidp, tvp);
1451
1452 mutex_enter(&lfs_lock);
1453 if (--fs->lfs_sleepers == 0)
1454 wakeup(&fs->lfs_sleepers);
1455 mutex_exit(&lfs_lock);
1456 return error;
1457
1458 case LFCNBMAPV:
1459 case LFCNMARKV:
1460 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
1461
1462 blkcnt = blkvp.blkcnt;
1463 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
1464 return (EINVAL);
1465 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
1466 if ((error = copyin(blkvp.blkiov, blkiov,
1467 blkcnt * sizeof(BLOCK_INFO))) != 0) {
1468 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1469 return error;
1470 }
1471
1472 mutex_enter(&lfs_lock);
1473 ++fs->lfs_sleepers;
1474 mutex_exit(&lfs_lock);
1475 if (ap->a_command == LFCNBMAPV)
1476 error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
1477 else /* LFCNMARKV */
1478 error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
1479 if (error == 0)
1480 error = copyout(blkiov, blkvp.blkiov,
1481 blkcnt * sizeof(BLOCK_INFO));
1482 mutex_enter(&lfs_lock);
1483 if (--fs->lfs_sleepers == 0)
1484 wakeup(&fs->lfs_sleepers);
1485 mutex_exit(&lfs_lock);
1486 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
1487 return error;
1488
1489 case LFCNRECLAIM:
1490 /*
1491 * Flush dirops and write Ifile, allowing empty segments
1492 * to be immediately reclaimed.
1493 */
1494 lfs_writer_enter(fs, "pndirop");
1495 off = fs->lfs_offset;
1496 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
1497 lfs_flush_dirops(fs);
1498 LFS_CLEANERINFO(cip, fs, bp);
1499 oclean = cip->clean;
1500 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1501 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
1502 fs->lfs_sp->seg_flags |= SEGM_PROT;
1503 lfs_segunlock(fs);
1504 lfs_writer_leave(fs);
1505
1506 #ifdef DEBUG
1507 LFS_CLEANERINFO(cip, fs, bp);
1508 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
1509 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
1510 fs->lfs_offset - off, cip->clean - oclean,
1511 fs->lfs_activesb));
1512 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
1513 #else
1514 __USE(oclean);
1515 __USE(off);
1516 #endif
1517
1518 return 0;
1519
1520 case LFCNIFILEFH_COMPAT:
1521 /* Return the filehandle of the Ifile */
1522 if ((error = kauth_authorize_system(l->l_cred,
1523 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
1524 return (error);
1525 fhp = (struct fhandle *)ap->a_data;
1526 fhp->fh_fsid = *fsidp;
1527 fh_size = 16; /* former VFS_MAXFIDSIZ */
1528 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1529
1530 case LFCNIFILEFH_COMPAT2:
1531 case LFCNIFILEFH:
1532 /* Return the filehandle of the Ifile */
1533 fhp = (struct fhandle *)ap->a_data;
1534 fhp->fh_fsid = *fsidp;
1535 fh_size = sizeof(struct lfs_fhandle) -
1536 offsetof(fhandle_t, fh_fid);
1537 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
1538
1539 case LFCNREWIND:
1540 /* Move lfs_offset to the lowest-numbered segment */
1541 return lfs_rewind(fs, *(int *)ap->a_data);
1542
1543 case LFCNINVAL:
1544 /* Mark a segment SEGUSE_INVAL */
1545 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
1546 if (sup->su_nbytes > 0) {
1547 brelse(bp, 0);
1548 lfs_unset_inval_all(fs);
1549 return EBUSY;
1550 }
1551 sup->su_flags |= SEGUSE_INVAL;
1552 VOP_BWRITE(bp->b_vp, bp);
1553 return 0;
1554
1555 case LFCNRESIZE:
1556 /* Resize the filesystem */
1557 return lfs_resize_fs(fs, *(int *)ap->a_data);
1558
1559 case LFCNWRAPSTOP:
1560 case LFCNWRAPSTOP_COMPAT:
1561 /*
1562 * Hold lfs_newseg at segment 0; if requested, sleep until
1563 * the filesystem wraps around. To support external agents
1564 * (dump, fsck-based regression test) that need to look at
1565 * a snapshot of the filesystem, without necessarily
1566 * requiring that all fs activity stops.
1567 */
1568 if (fs->lfs_stoplwp == curlwp)
1569 return EALREADY;
1570
1571 mutex_enter(&lfs_lock);
1572 while (fs->lfs_stoplwp != NULL)
1573 cv_wait(&fs->lfs_stopcv, &lfs_lock);
1574 fs->lfs_stoplwp = curlwp;
1575 if (fs->lfs_nowrap == 0)
1576 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
1577 ++fs->lfs_nowrap;
1578 if (*(int *)ap->a_data == 1
1579 || ap->a_command == LFCNWRAPSTOP_COMPAT) {
1580 log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
1581 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1582 "segwrap", 0, &lfs_lock);
1583 log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
1584 if (error) {
1585 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
1586 }
1587 }
1588 mutex_exit(&lfs_lock);
1589 return 0;
1590
1591 case LFCNWRAPGO:
1592 case LFCNWRAPGO_COMPAT:
1593 /*
1594 * Having done its work, the agent wakes up the writer.
1595 * If the argument is 1, it sleeps until a new segment
1596 * is selected.
1597 */
1598 mutex_enter(&lfs_lock);
1599 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
1600 ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
1601 *((int *)ap->a_data));
1602 mutex_exit(&lfs_lock);
1603 return error;
1604
1605 case LFCNWRAPPASS:
1606 if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
1607 return EALREADY;
1608 mutex_enter(&lfs_lock);
1609 if (fs->lfs_stoplwp != curlwp) {
1610 mutex_exit(&lfs_lock);
1611 return EALREADY;
1612 }
1613 if (fs->lfs_nowrap == 0) {
1614 mutex_exit(&lfs_lock);
1615 return EBUSY;
1616 }
1617 fs->lfs_wrappass = 1;
1618 wakeup(&fs->lfs_wrappass);
1619 /* Wait for the log to wrap, if asked */
1620 if (*(int *)ap->a_data) {
1621 mutex_enter(ap->a_vp->v_interlock);
1622 if (lfs_vref(ap->a_vp) != 0)
1623 panic("LFCNWRAPPASS: lfs_vref failed");
1624 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
1625 log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
1626 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
1627 "segwrap", 0, &lfs_lock);
1628 log(LOG_NOTICE, "LFCNPASS done waiting\n");
1629 VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
1630 lfs_vunref(ap->a_vp);
1631 }
1632 mutex_exit(&lfs_lock);
1633 return error;
1634
1635 case LFCNWRAPSTATUS:
1636 mutex_enter(&lfs_lock);
1637 *(int *)ap->a_data = fs->lfs_wrapstatus;
1638 mutex_exit(&lfs_lock);
1639 return 0;
1640
1641 default:
1642 return ulfs_fcntl(v);
1643 }
1644 return 0;
1645 }
1646
1647 int
1648 lfs_getpages(void *v)
1649 {
1650 struct vop_getpages_args /* {
1651 struct vnode *a_vp;
1652 voff_t a_offset;
1653 struct vm_page **a_m;
1654 int *a_count;
1655 int a_centeridx;
1656 vm_prot_t a_access_type;
1657 int a_advice;
1658 int a_flags;
1659 } */ *ap = v;
1660
1661 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
1662 (ap->a_access_type & VM_PROT_WRITE) != 0) {
1663 return EPERM;
1664 }
1665 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
1666 mutex_enter(&lfs_lock);
1667 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
1668 mutex_exit(&lfs_lock);
1669 }
1670
1671 /*
1672 * we're relying on the fact that genfs_getpages() always read in
1673 * entire filesystem blocks.
1674 */
1675 return genfs_getpages(v);
1676 }
1677
1678 /*
1679 * Wait for a page to become unbusy, possibly printing diagnostic messages
1680 * as well.
1681 *
1682 * Called with vp->v_interlock held; return with it held.
1683 */
1684 static void
1685 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label)
1686 {
1687 KASSERT(mutex_owned(vp->v_interlock));
1688 if ((pg->flags & PG_BUSY) == 0)
1689 return; /* Nothing to wait for! */
1690
1691 #if defined(DEBUG) && defined(UVM_PAGE_TRKOWN)
1692 static struct vm_page *lastpg;
1693
1694 if (label != NULL && pg != lastpg) {
1695 if (pg->owner_tag) {
1696 printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n",
1697 curproc->p_pid, curlwp->l_lid, label,
1698 pg, pg->owner, pg->lowner, pg->owner_tag);
1699 } else {
1700 printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n",
1701 curproc->p_pid, curlwp->l_lid, label, pg);
1702 }
1703 }
1704 lastpg = pg;
1705 #endif
1706
1707 pg->flags |= PG_WANTED;
1708 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0, "lfsput", 0);
1709 mutex_enter(vp->v_interlock);
1710 }
1711
1712 /*
1713 * This routine is called by lfs_putpages() when it can't complete the
1714 * write because a page is busy. This means that either (1) someone,
1715 * possibly the pagedaemon, is looking at this page, and will give it up
1716 * presently; or (2) we ourselves are holding the page busy in the
1717 * process of being written (either gathered or actually on its way to
1718 * disk). We don't need to give up the segment lock, but we might need
1719 * to call lfs_writeseg() to expedite the page's journey to disk.
1720 *
1721 * Called with vp->v_interlock held; return with it held.
1722 */
1723 /* #define BUSYWAIT */
1724 static void
1725 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg,
1726 int seglocked, const char *label)
1727 {
1728 KASSERT(mutex_owned(vp->v_interlock));
1729 #ifndef BUSYWAIT
1730 struct inode *ip = VTOI(vp);
1731 struct segment *sp = fs->lfs_sp;
1732 int count = 0;
1733
1734 if (pg == NULL)
1735 return;
1736
1737 while (pg->flags & PG_BUSY &&
1738 pg->uobject == &vp->v_uobj) {
1739 mutex_exit(vp->v_interlock);
1740 if (sp->cbpp - sp->bpp > 1) {
1741 /* Write gathered pages */
1742 lfs_updatemeta(sp);
1743 lfs_release_finfo(fs);
1744 (void) lfs_writeseg(fs, sp);
1745
1746 /*
1747 * Reinitialize FIP
1748 */
1749 KASSERT(sp->vp == vp);
1750 lfs_acquire_finfo(fs, ip->i_number,
1751 ip->i_gen);
1752 }
1753 ++count;
1754 mutex_enter(vp->v_interlock);
1755 wait_for_page(vp, pg, label);
1756 }
1757 if (label != NULL && count > 1) {
1758 DLOG((DLOG_PAGE, "lfs_putpages[%d]: %s: %sn = %d\n",
1759 curproc->p_pid, label, (count > 0 ? "looping, " : ""),
1760 count));
1761 }
1762 #else
1763 preempt(1);
1764 #endif
1765 KASSERT(mutex_owned(vp->v_interlock));
1766 }
1767
1768 /*
1769 * Make sure that for all pages in every block in the given range,
1770 * either all are dirty or all are clean. If any of the pages
1771 * we've seen so far are dirty, put the vnode on the paging chain,
1772 * and mark it IN_PAGING.
1773 *
1774 * If checkfirst != 0, don't check all the pages but return at the
1775 * first dirty page.
1776 */
1777 static int
1778 check_dirty(struct lfs *fs, struct vnode *vp,
1779 off_t startoffset, off_t endoffset, off_t blkeof,
1780 int flags, int checkfirst, struct vm_page **pgp)
1781 {
1782 int by_list;
1783 struct vm_page *curpg = NULL; /* XXX: gcc */
1784 struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
1785 off_t soff = 0; /* XXX: gcc */
1786 voff_t off;
1787 int i;
1788 int nonexistent;
1789 int any_dirty; /* number of dirty pages */
1790 int dirty; /* number of dirty pages in a block */
1791 int tdirty;
1792 int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
1793 int pagedaemon = (curlwp == uvm.pagedaemon_lwp);
1794
1795 KASSERT(mutex_owned(vp->v_interlock));
1796 ASSERT_MAYBE_SEGLOCK(fs);
1797 top:
1798 by_list = (vp->v_uobj.uo_npages <=
1799 ((endoffset - startoffset) >> PAGE_SHIFT) *
1800 UVM_PAGE_TREE_PENALTY);
1801 any_dirty = 0;
1802
1803 if (by_list) {
1804 curpg = TAILQ_FIRST(&vp->v_uobj.memq);
1805 } else {
1806 soff = startoffset;
1807 }
1808 while (by_list || soff < MIN(blkeof, endoffset)) {
1809 if (by_list) {
1810 /*
1811 * Find the first page in a block. Skip
1812 * blocks outside our area of interest or beyond
1813 * the end of file.
1814 */
1815 KASSERT(curpg == NULL
1816 || (curpg->flags & PG_MARKER) == 0);
1817 if (pages_per_block > 1) {
1818 while (curpg &&
1819 ((curpg->offset & fs->lfs_bmask) ||
1820 curpg->offset >= vp->v_size ||
1821 curpg->offset >= endoffset)) {
1822 curpg = TAILQ_NEXT(curpg, listq.queue);
1823 KASSERT(curpg == NULL ||
1824 (curpg->flags & PG_MARKER) == 0);
1825 }
1826 }
1827 if (curpg == NULL)
1828 break;
1829 soff = curpg->offset;
1830 }
1831
1832 /*
1833 * Mark all pages in extended range busy; find out if any
1834 * of them are dirty.
1835 */
1836 nonexistent = dirty = 0;
1837 for (i = 0; i == 0 || i < pages_per_block; i++) {
1838 KASSERT(mutex_owned(vp->v_interlock));
1839 if (by_list && pages_per_block <= 1) {
1840 pgs[i] = pg = curpg;
1841 } else {
1842 off = soff + (i << PAGE_SHIFT);
1843 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
1844 if (pg == NULL) {
1845 ++nonexistent;
1846 continue;
1847 }
1848 }
1849 KASSERT(pg != NULL);
1850
1851 /*
1852 * If we're holding the segment lock, we can deadlock
1853 * against a process that has our page and is waiting
1854 * for the cleaner, while the cleaner waits for the
1855 * segment lock. Just bail in that case.
1856 */
1857 if ((pg->flags & PG_BUSY) &&
1858 (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
1859 if (i > 0)
1860 uvm_page_unbusy(pgs, i);
1861 DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
1862 if (pgp)
1863 *pgp = pg;
1864 KASSERT(mutex_owned(vp->v_interlock));
1865 return -1;
1866 }
1867
1868 while (pg->flags & PG_BUSY) {
1869 wait_for_page(vp, pg, NULL);
1870 KASSERT(mutex_owned(vp->v_interlock));
1871 if (i > 0)
1872 uvm_page_unbusy(pgs, i);
1873 KASSERT(mutex_owned(vp->v_interlock));
1874 goto top;
1875 }
1876 pg->flags |= PG_BUSY;
1877 UVM_PAGE_OWN(pg, "lfs_putpages");
1878
1879 pmap_page_protect(pg, VM_PROT_NONE);
1880 tdirty = (pmap_clear_modify(pg) ||
1881 (pg->flags & PG_CLEAN) == 0);
1882 dirty += tdirty;
1883 }
1884 if (pages_per_block > 0 && nonexistent >= pages_per_block) {
1885 if (by_list) {
1886 curpg = TAILQ_NEXT(curpg, listq.queue);
1887 } else {
1888 soff += fs->lfs_bsize;
1889 }
1890 continue;
1891 }
1892
1893 any_dirty += dirty;
1894 KASSERT(nonexistent == 0);
1895 KASSERT(mutex_owned(vp->v_interlock));
1896
1897 /*
1898 * If any are dirty make all dirty; unbusy them,
1899 * but if we were asked to clean, wire them so that
1900 * the pagedaemon doesn't bother us about them while
1901 * they're on their way to disk.
1902 */
1903 for (i = 0; i == 0 || i < pages_per_block; i++) {
1904 KASSERT(mutex_owned(vp->v_interlock));
1905 pg = pgs[i];
1906 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
1907 KASSERT(pg->flags & PG_BUSY);
1908 if (dirty) {
1909 pg->flags &= ~PG_CLEAN;
1910 if (flags & PGO_FREE) {
1911 /*
1912 * Wire the page so that
1913 * pdaemon doesn't see it again.
1914 */
1915 mutex_enter(&uvm_pageqlock);
1916 uvm_pagewire(pg);
1917 mutex_exit(&uvm_pageqlock);
1918
1919 /* Suspended write flag */
1920 pg->flags |= PG_DELWRI;
1921 }
1922 }
1923 if (pg->flags & PG_WANTED)
1924 wakeup(pg);
1925 pg->flags &= ~(PG_WANTED|PG_BUSY);
1926 UVM_PAGE_OWN(pg, NULL);
1927 }
1928
1929 if (checkfirst && any_dirty)
1930 break;
1931
1932 if (by_list) {
1933 curpg = TAILQ_NEXT(curpg, listq.queue);
1934 } else {
1935 soff += MAX(PAGE_SIZE, fs->lfs_bsize);
1936 }
1937 }
1938
1939 KASSERT(mutex_owned(vp->v_interlock));
1940 return any_dirty;
1941 }
1942
1943 /*
1944 * lfs_putpages functions like genfs_putpages except that
1945 *
1946 * (1) It needs to bounds-check the incoming requests to ensure that
1947 * they are block-aligned; if they are not, expand the range and
1948 * do the right thing in case, e.g., the requested range is clean
1949 * but the expanded range is dirty.
1950 *
1951 * (2) It needs to explicitly send blocks to be written when it is done.
1952 * If VOP_PUTPAGES is called without the seglock held, we simply take
1953 * the seglock and let lfs_segunlock wait for us.
1954 * XXX There might be a bad situation if we have to flush a vnode while
1955 * XXX lfs_markv is in operation. As of this writing we panic in this
1956 * XXX case.
1957 *
1958 * Assumptions:
1959 *
1960 * (1) The caller does not hold any pages in this vnode busy. If it does,
1961 * there is a danger that when we expand the page range and busy the
1962 * pages we will deadlock.
1963 *
1964 * (2) We are called with vp->v_interlock held; we must return with it
1965 * released.
1966 *
1967 * (3) We don't absolutely have to free pages right away, provided that
1968 * the request does not have PGO_SYNCIO. When the pagedaemon gives
1969 * us a request with PGO_FREE, we take the pages out of the paging
1970 * queue and wake up the writer, which will handle freeing them for us.
1971 *
1972 * We ensure that for any filesystem block, all pages for that
1973 * block are either resident or not, even if those pages are higher
1974 * than EOF; that means that we will be getting requests to free
1975 * "unused" pages above EOF all the time, and should ignore them.
1976 *
1977 * (4) If we are called with PGO_LOCKED, the finfo array we are to write
1978 * into has been set up for us by lfs_writefile. If not, we will
1979 * have to handle allocating and/or freeing an finfo entry.
1980 *
1981 * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
1982 */
1983
1984 /* How many times to loop before we should start to worry */
1985 #define TOOMANY 4
1986
1987 int
1988 lfs_putpages(void *v)
1989 {
1990 int error;
1991 struct vop_putpages_args /* {
1992 struct vnode *a_vp;
1993 voff_t a_offlo;
1994 voff_t a_offhi;
1995 int a_flags;
1996 } */ *ap = v;
1997 struct vnode *vp;
1998 struct inode *ip;
1999 struct lfs *fs;
2000 struct segment *sp;
2001 off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
2002 off_t off, max_endoffset;
2003 bool seglocked, sync, pagedaemon, reclaim;
2004 struct vm_page *pg, *busypg;
2005 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
2006 int oreclaim = 0;
2007 int donewriting = 0;
2008 #ifdef DEBUG
2009 int debug_n_again, debug_n_dirtyclean;
2010 #endif
2011
2012 vp = ap->a_vp;
2013 ip = VTOI(vp);
2014 fs = ip->i_lfs;
2015 sync = (ap->a_flags & PGO_SYNCIO) != 0;
2016 reclaim = (ap->a_flags & PGO_RECLAIM) != 0;
2017 pagedaemon = (curlwp == uvm.pagedaemon_lwp);
2018
2019 KASSERT(mutex_owned(vp->v_interlock));
2020
2021 /* Putpages does nothing for metadata. */
2022 if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
2023 mutex_exit(vp->v_interlock);
2024 return 0;
2025 }
2026
2027 /*
2028 * If there are no pages, don't do anything.
2029 */
2030 if (vp->v_uobj.uo_npages == 0) {
2031 if (TAILQ_EMPTY(&vp->v_uobj.memq) &&
2032 (vp->v_iflag & VI_ONWORKLST) &&
2033 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
2034 vp->v_iflag &= ~VI_WRMAPDIRTY;
2035 vn_syncer_remove_from_worklist(vp);
2036 }
2037 mutex_exit(vp->v_interlock);
2038
2039 /* Remove us from paging queue, if we were on it */
2040 mutex_enter(&lfs_lock);
2041 if (ip->i_flags & IN_PAGING) {
2042 ip->i_flags &= ~IN_PAGING;
2043 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2044 }
2045 mutex_exit(&lfs_lock);
2046
2047 KASSERT(!mutex_owned(vp->v_interlock));
2048 return 0;
2049 }
2050
2051 blkeof = lfs_blkroundup(fs, ip->i_size);
2052
2053 /*
2054 * Ignore requests to free pages past EOF but in the same block
2055 * as EOF, unless the vnode is being reclaimed or the request
2056 * is synchronous. (If the request is sync, it comes from
2057 * lfs_truncate.)
2058 *
2059 * To avoid being flooded with this request, make these pages
2060 * look "active".
2061 */
2062 if (!sync && !reclaim &&
2063 ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
2064 origoffset = ap->a_offlo;
2065 for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
2066 pg = uvm_pagelookup(&vp->v_uobj, off);
2067 KASSERT(pg != NULL);
2068 while (pg->flags & PG_BUSY) {
2069 pg->flags |= PG_WANTED;
2070 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0,
2071 "lfsput2", 0);
2072 mutex_enter(vp->v_interlock);
2073 }
2074 mutex_enter(&uvm_pageqlock);
2075 uvm_pageactivate(pg);
2076 mutex_exit(&uvm_pageqlock);
2077 }
2078 ap->a_offlo = blkeof;
2079 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
2080 mutex_exit(vp->v_interlock);
2081 return 0;
2082 }
2083 }
2084
2085 /*
2086 * Extend page range to start and end at block boundaries.
2087 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
2088 */
2089 origoffset = ap->a_offlo;
2090 origendoffset = ap->a_offhi;
2091 startoffset = origoffset & ~(fs->lfs_bmask);
2092 max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
2093 << fs->lfs_bshift;
2094
2095 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
2096 endoffset = max_endoffset;
2097 origendoffset = endoffset;
2098 } else {
2099 origendoffset = round_page(ap->a_offhi);
2100 endoffset = round_page(lfs_blkroundup(fs, origendoffset));
2101 }
2102
2103 KASSERT(startoffset > 0 || endoffset >= startoffset);
2104 if (startoffset == endoffset) {
2105 /* Nothing to do, why were we called? */
2106 mutex_exit(vp->v_interlock);
2107 DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
2108 PRId64 "\n", startoffset));
2109 return 0;
2110 }
2111
2112 ap->a_offlo = startoffset;
2113 ap->a_offhi = endoffset;
2114
2115 /*
2116 * If not cleaning, just send the pages through genfs_putpages
2117 * to be returned to the pool.
2118 */
2119 if (!(ap->a_flags & PGO_CLEANIT)) {
2120 DLOG((DLOG_PAGE, "lfs_putpages: no cleanit vn %p ino %d (flags %x)\n",
2121 vp, (int)ip->i_number, ap->a_flags));
2122 int r = genfs_putpages(v);
2123 KASSERT(!mutex_owned(vp->v_interlock));
2124 return r;
2125 }
2126
2127 /* Set PGO_BUSYFAIL to avoid deadlocks */
2128 ap->a_flags |= PGO_BUSYFAIL;
2129
2130 /*
2131 * Likewise, if we are asked to clean but the pages are not
2132 * dirty, we can just free them using genfs_putpages.
2133 */
2134 #ifdef DEBUG
2135 debug_n_dirtyclean = 0;
2136 #endif
2137 do {
2138 int r;
2139 KASSERT(mutex_owned(vp->v_interlock));
2140
2141 /* Count the number of dirty pages */
2142 r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
2143 ap->a_flags, 1, NULL);
2144 if (r < 0) {
2145 /* Pages are busy with another process */
2146 mutex_exit(vp->v_interlock);
2147 return EDEADLK;
2148 }
2149 if (r > 0) /* Some pages are dirty */
2150 break;
2151
2152 /*
2153 * Sometimes pages are dirtied between the time that
2154 * we check and the time we try to clean them.
2155 * Instruct lfs_gop_write to return EDEADLK in this case
2156 * so we can write them properly.
2157 */
2158 ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
2159 r = genfs_do_putpages(vp, startoffset, endoffset,
2160 ap->a_flags & ~PGO_SYNCIO, &busypg);
2161 ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
2162 if (r != EDEADLK) {
2163 KASSERT(!mutex_owned(vp->v_interlock));
2164 return r;
2165 }
2166
2167 /* One of the pages was busy. Start over. */
2168 mutex_enter(vp->v_interlock);
2169 wait_for_page(vp, busypg, "dirtyclean");
2170 #ifdef DEBUG
2171 ++debug_n_dirtyclean;
2172 #endif
2173 } while(1);
2174
2175 #ifdef DEBUG
2176 if (debug_n_dirtyclean > TOOMANY)
2177 DLOG((DLOG_PAGE, "lfs_putpages: dirtyclean: looping, n = %d\n",
2178 debug_n_dirtyclean));
2179 #endif
2180
2181 /*
2182 * Dirty and asked to clean.
2183 *
2184 * Pagedaemon can't actually write LFS pages; wake up
2185 * the writer to take care of that. The writer will
2186 * notice the pager inode queue and act on that.
2187 *
2188 * XXX We must drop the vp->interlock before taking the lfs_lock or we
2189 * get a nasty deadlock with lfs_flush_pchain().
2190 */
2191 if (pagedaemon) {
2192 mutex_exit(vp->v_interlock);
2193 mutex_enter(&lfs_lock);
2194 if (!(ip->i_flags & IN_PAGING)) {
2195 ip->i_flags |= IN_PAGING;
2196 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2197 }
2198 wakeup(&lfs_writer_daemon);
2199 mutex_exit(&lfs_lock);
2200 preempt();
2201 KASSERT(!mutex_owned(vp->v_interlock));
2202 return EWOULDBLOCK;
2203 }
2204
2205 /*
2206 * If this is a file created in a recent dirop, we can't flush its
2207 * inode until the dirop is complete. Drain dirops, then flush the
2208 * filesystem (taking care of any other pending dirops while we're
2209 * at it).
2210 */
2211 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
2212 (vp->v_uflag & VU_DIROP)) {
2213 DLOG((DLOG_PAGE, "lfs_putpages: flushing VU_DIROP\n"));
2214
2215 lfs_writer_enter(fs, "ppdirop");
2216
2217 /* Note if we hold the vnode locked */
2218 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
2219 {
2220 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode already locked\n"));
2221 } else {
2222 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode not locked\n"));
2223 }
2224 mutex_exit(vp->v_interlock);
2225
2226 mutex_enter(&lfs_lock);
2227 lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
2228 mutex_exit(&lfs_lock);
2229
2230 mutex_enter(vp->v_interlock);
2231 lfs_writer_leave(fs);
2232
2233 /* The flush will have cleaned out this vnode as well,
2234 no need to do more to it. */
2235 }
2236
2237 /*
2238 * This is it. We are going to write some pages. From here on
2239 * down it's all just mechanics.
2240 *
2241 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
2242 */
2243 ap->a_flags &= ~PGO_SYNCIO;
2244
2245 /*
2246 * If we've already got the seglock, flush the node and return.
2247 * The FIP has already been set up for us by lfs_writefile,
2248 * and FIP cleanup and lfs_updatemeta will also be done there,
2249 * unless genfs_putpages returns EDEADLK; then we must flush
2250 * what we have, and correct FIP and segment header accounting.
2251 */
2252 get_seglock:
2253 /*
2254 * If we are not called with the segment locked, lock it.
2255 * Account for a new FIP in the segment header, and set sp->vp.
2256 * (This should duplicate the setup at the top of lfs_writefile().)
2257 */
2258 seglocked = (ap->a_flags & PGO_LOCKED) != 0;
2259 if (!seglocked) {
2260 mutex_exit(vp->v_interlock);
2261 error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
2262 if (error != 0) {
2263 KASSERT(!mutex_owned(vp->v_interlock));
2264 return error;
2265 }
2266 mutex_enter(vp->v_interlock);
2267 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
2268 }
2269 sp = fs->lfs_sp;
2270 KASSERT(sp->vp == NULL);
2271 sp->vp = vp;
2272
2273 /* Note segments written by reclaim; only for debugging */
2274 if ((vp->v_iflag & VI_XLOCK) != 0) {
2275 sp->seg_flags |= SEGM_RECLAIM;
2276 fs->lfs_reclino = ip->i_number;
2277 }
2278
2279 /*
2280 * Ensure that the partial segment is marked SS_DIROP if this
2281 * vnode is a DIROP.
2282 */
2283 if (!seglocked && vp->v_uflag & VU_DIROP)
2284 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
2285
2286 /*
2287 * Loop over genfs_putpages until all pages are gathered.
2288 * genfs_putpages() drops the interlock, so reacquire it if necessary.
2289 * Whenever we lose the interlock we have to rerun check_dirty, as
2290 * well, since more pages might have been dirtied in our absence.
2291 */
2292 #ifdef DEBUG
2293 debug_n_again = 0;
2294 #endif
2295 do {
2296 busypg = NULL;
2297 KASSERT(mutex_owned(vp->v_interlock));
2298 if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
2299 ap->a_flags, 0, &busypg) < 0) {
2300 mutex_exit(vp->v_interlock);
2301 /* XXX why? --ks */
2302 mutex_enter(vp->v_interlock);
2303 write_and_wait(fs, vp, busypg, seglocked, NULL);
2304 if (!seglocked) {
2305 mutex_exit(vp->v_interlock);
2306 lfs_release_finfo(fs);
2307 lfs_segunlock(fs);
2308 mutex_enter(vp->v_interlock);
2309 }
2310 sp->vp = NULL;
2311 goto get_seglock;
2312 }
2313
2314 busypg = NULL;
2315 KASSERT(!mutex_owned(&uvm_pageqlock));
2316 oreclaim = (ap->a_flags & PGO_RECLAIM);
2317 ap->a_flags &= ~PGO_RECLAIM;
2318 error = genfs_do_putpages(vp, startoffset, endoffset,
2319 ap->a_flags, &busypg);
2320 ap->a_flags |= oreclaim;
2321
2322 if (error == EDEADLK || error == EAGAIN) {
2323 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
2324 " %d ino %d off %x (seg %d)\n", error,
2325 ip->i_number, fs->lfs_offset,
2326 lfs_dtosn(fs, fs->lfs_offset)));
2327
2328 if (oreclaim) {
2329 mutex_enter(vp->v_interlock);
2330 write_and_wait(fs, vp, busypg, seglocked, "again");
2331 mutex_exit(vp->v_interlock);
2332 } else {
2333 if ((sp->seg_flags & SEGM_SINGLE) &&
2334 fs->lfs_curseg != fs->lfs_startseg)
2335 donewriting = 1;
2336 }
2337 } else if (error) {
2338 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
2339 " %d ino %d off %x (seg %d)\n", error,
2340 (int)ip->i_number, fs->lfs_offset,
2341 lfs_dtosn(fs, fs->lfs_offset)));
2342 }
2343 /* genfs_do_putpages loses the interlock */
2344 #ifdef DEBUG
2345 ++debug_n_again;
2346 #endif
2347 if (oreclaim && error == EAGAIN) {
2348 DLOG((DLOG_PAGE, "vp %p ino %d vi_flags %x a_flags %x avoiding vclean panic\n",
2349 vp, (int)ip->i_number, vp->v_iflag, ap->a_flags));
2350 mutex_enter(vp->v_interlock);
2351 }
2352 if (error == EDEADLK)
2353 mutex_enter(vp->v_interlock);
2354 } while (error == EDEADLK || (oreclaim && error == EAGAIN));
2355 #ifdef DEBUG
2356 if (debug_n_again > TOOMANY)
2357 DLOG((DLOG_PAGE, "lfs_putpages: again: looping, n = %d\n", debug_n_again));
2358 #endif
2359
2360 KASSERT(sp != NULL && sp->vp == vp);
2361 if (!seglocked && !donewriting) {
2362 sp->vp = NULL;
2363
2364 /* Write indirect blocks as well */
2365 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
2366 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
2367 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
2368
2369 KASSERT(sp->vp == NULL);
2370 sp->vp = vp;
2371 }
2372
2373 /*
2374 * Blocks are now gathered into a segment waiting to be written.
2375 * All that's left to do is update metadata, and write them.
2376 */
2377 lfs_updatemeta(sp);
2378 KASSERT(sp->vp == vp);
2379 sp->vp = NULL;
2380
2381 /*
2382 * If we were called from lfs_writefile, we don't need to clean up
2383 * the FIP or unlock the segment lock. We're done.
2384 */
2385 if (seglocked) {
2386 KASSERT(!mutex_owned(vp->v_interlock));
2387 return error;
2388 }
2389
2390 /* Clean up FIP and send it to disk. */
2391 lfs_release_finfo(fs);
2392 lfs_writeseg(fs, fs->lfs_sp);
2393
2394 /*
2395 * Remove us from paging queue if we wrote all our pages.
2396 */
2397 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
2398 mutex_enter(&lfs_lock);
2399 if (ip->i_flags & IN_PAGING) {
2400 ip->i_flags &= ~IN_PAGING;
2401 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2402 }
2403 mutex_exit(&lfs_lock);
2404 }
2405
2406 /*
2407 * XXX - with the malloc/copy writeseg, the pages are freed by now
2408 * even if we don't wait (e.g. if we hold a nested lock). This
2409 * will not be true if we stop using malloc/copy.
2410 */
2411 KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
2412 lfs_segunlock(fs);
2413
2414 /*
2415 * Wait for v_numoutput to drop to zero. The seglock should
2416 * take care of this, but there is a slight possibility that
2417 * aiodoned might not have got around to our buffers yet.
2418 */
2419 if (sync) {
2420 mutex_enter(vp->v_interlock);
2421 while (vp->v_numoutput > 0) {
2422 DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
2423 " num %d\n", ip->i_number, vp->v_numoutput));
2424 cv_wait(&vp->v_cv, vp->v_interlock);
2425 }
2426 mutex_exit(vp->v_interlock);
2427 }
2428 KASSERT(!mutex_owned(vp->v_interlock));
2429 return error;
2430 }
2431
2432 /*
2433 * Return the last logical file offset that should be written for this file
2434 * if we're doing a write that ends at "size". If writing, we need to know
2435 * about sizes on disk, i.e. fragments if there are any; if reading, we need
2436 * to know about entire blocks.
2437 */
2438 void
2439 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
2440 {
2441 struct inode *ip = VTOI(vp);
2442 struct lfs *fs = ip->i_lfs;
2443 daddr_t olbn, nlbn;
2444
2445 olbn = lfs_lblkno(fs, ip->i_size);
2446 nlbn = lfs_lblkno(fs, size);
2447 if (!(flags & GOP_SIZE_MEM) && nlbn < ULFS_NDADDR && olbn <= nlbn) {
2448 *eobp = lfs_fragroundup(fs, size);
2449 } else {
2450 *eobp = lfs_blkroundup(fs, size);
2451 }
2452 }
2453
2454 #ifdef DEBUG
2455 void lfs_dump_vop(void *);
2456
2457 void
2458 lfs_dump_vop(void *v)
2459 {
2460 struct vop_putpages_args /* {
2461 struct vnode *a_vp;
2462 voff_t a_offlo;
2463 voff_t a_offhi;
2464 int a_flags;
2465 } */ *ap = v;
2466
2467 #ifdef DDB
2468 vfs_vnode_print(ap->a_vp, 0, printf);
2469 #endif
2470 lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
2471 }
2472 #endif
2473
2474 int
2475 lfs_mmap(void *v)
2476 {
2477 struct vop_mmap_args /* {
2478 const struct vnodeop_desc *a_desc;
2479 struct vnode *a_vp;
2480 vm_prot_t a_prot;
2481 kauth_cred_t a_cred;
2482 } */ *ap = v;
2483
2484 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
2485 return EOPNOTSUPP;
2486 return ulfs_mmap(v);
2487 }
2488
2489 static int
2490 lfs_openextattr(void *v)
2491 {
2492 struct vop_openextattr_args /* {
2493 struct vnode *a_vp;
2494 kauth_cred_t a_cred;
2495 struct proc *a_p;
2496 } */ *ap = v;
2497 struct inode *ip = VTOI(ap->a_vp);
2498 struct ulfsmount *ump = ip->i_ump;
2499 //struct lfs *fs = ip->i_lfs;
2500
2501 /* Not supported for ULFS1 file systems. */
2502 if (ump->um_fstype == ULFS1)
2503 return (EOPNOTSUPP);
2504
2505 /* XXX Not implemented for ULFS2 file systems. */
2506 return (EOPNOTSUPP);
2507 }
2508
2509 static int
2510 lfs_closeextattr(void *v)
2511 {
2512 struct vop_closeextattr_args /* {
2513 struct vnode *a_vp;
2514 int a_commit;
2515 kauth_cred_t a_cred;
2516 struct proc *a_p;
2517 } */ *ap = v;
2518 struct inode *ip = VTOI(ap->a_vp);
2519 struct ulfsmount *ump = ip->i_ump;
2520 //struct lfs *fs = ip->i_lfs;
2521
2522 /* Not supported for ULFS1 file systems. */
2523 if (ump->um_fstype == ULFS1)
2524 return (EOPNOTSUPP);
2525
2526 /* XXX Not implemented for ULFS2 file systems. */
2527 return (EOPNOTSUPP);
2528 }
2529
2530 static int
2531 lfs_getextattr(void *v)
2532 {
2533 struct vop_getextattr_args /* {
2534 struct vnode *a_vp;
2535 int a_attrnamespace;
2536 const char *a_name;
2537 struct uio *a_uio;
2538 size_t *a_size;
2539 kauth_cred_t a_cred;
2540 struct proc *a_p;
2541 } */ *ap = v;
2542 struct vnode *vp = ap->a_vp;
2543 struct inode *ip = VTOI(vp);
2544 struct ulfsmount *ump = ip->i_ump;
2545 //struct lfs *fs = ip->i_lfs;
2546 int error;
2547
2548 if (ump->um_fstype == ULFS1) {
2549 #ifdef LFS_EXTATTR
2550 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2551 error = ulfs_getextattr(ap);
2552 fstrans_done(vp->v_mount);
2553 #else
2554 error = EOPNOTSUPP;
2555 #endif
2556 return error;
2557 }
2558
2559 /* XXX Not implemented for ULFS2 file systems. */
2560 return (EOPNOTSUPP);
2561 }
2562
2563 static int
2564 lfs_setextattr(void *v)
2565 {
2566 struct vop_setextattr_args /* {
2567 struct vnode *a_vp;
2568 int a_attrnamespace;
2569 const char *a_name;
2570 struct uio *a_uio;
2571 kauth_cred_t a_cred;
2572 struct proc *a_p;
2573 } */ *ap = v;
2574 struct vnode *vp = ap->a_vp;
2575 struct inode *ip = VTOI(vp);
2576 struct ulfsmount *ump = ip->i_ump;
2577 //struct lfs *fs = ip->i_lfs;
2578 int error;
2579
2580 if (ump->um_fstype == ULFS1) {
2581 #ifdef LFS_EXTATTR
2582 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2583 error = ulfs_setextattr(ap);
2584 fstrans_done(vp->v_mount);
2585 #else
2586 error = EOPNOTSUPP;
2587 #endif
2588 return error;
2589 }
2590
2591 /* XXX Not implemented for ULFS2 file systems. */
2592 return (EOPNOTSUPP);
2593 }
2594
2595 static int
2596 lfs_listextattr(void *v)
2597 {
2598 struct vop_listextattr_args /* {
2599 struct vnode *a_vp;
2600 int a_attrnamespace;
2601 struct uio *a_uio;
2602 size_t *a_size;
2603 kauth_cred_t a_cred;
2604 struct proc *a_p;
2605 } */ *ap = v;
2606 struct vnode *vp = ap->a_vp;
2607 struct inode *ip = VTOI(vp);
2608 struct ulfsmount *ump = ip->i_ump;
2609 //struct lfs *fs = ip->i_lfs;
2610 int error;
2611
2612 if (ump->um_fstype == ULFS1) {
2613 #ifdef LFS_EXTATTR
2614 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2615 error = ulfs_listextattr(ap);
2616 fstrans_done(vp->v_mount);
2617 #else
2618 error = EOPNOTSUPP;
2619 #endif
2620 return error;
2621 }
2622
2623 /* XXX Not implemented for ULFS2 file systems. */
2624 return (EOPNOTSUPP);
2625 }
2626
2627 static int
2628 lfs_deleteextattr(void *v)
2629 {
2630 struct vop_deleteextattr_args /* {
2631 struct vnode *a_vp;
2632 int a_attrnamespace;
2633 kauth_cred_t a_cred;
2634 struct proc *a_p;
2635 } */ *ap = v;
2636 struct vnode *vp = ap->a_vp;
2637 struct inode *ip = VTOI(vp);
2638 struct ulfsmount *ump = ip->i_ump;
2639 //struct fs *fs = ip->i_lfs;
2640 int error;
2641
2642 if (ump->um_fstype == ULFS1) {
2643 #ifdef LFS_EXTATTR
2644 fstrans_start(vp->v_mount, FSTRANS_SHARED);
2645 error = ulfs_deleteextattr(ap);
2646 fstrans_done(vp->v_mount);
2647 #else
2648 error = EOPNOTSUPP;
2649 #endif
2650 return error;
2651 }
2652
2653 /* XXX Not implemented for ULFS2 file systems. */
2654 return (EOPNOTSUPP);
2655 }
2656