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