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