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