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