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