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