lfs_syscalls.c revision 1.99 1 1.99 yamt /* $NetBSD: lfs_syscalls.c,v 1.99 2003/11/07 14:52:27 yamt Exp $ */
2 1.3 cgd
3 1.1 mycroft /*-
4 1.80 perseant * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 1.22 perseant * All rights reserved.
6 1.22 perseant *
7 1.22 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.22 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.22 perseant *
10 1.22 perseant * Redistribution and use in source and binary forms, with or without
11 1.22 perseant * modification, are permitted provided that the following conditions
12 1.22 perseant * are met:
13 1.22 perseant * 1. Redistributions of source code must retain the above copyright
14 1.22 perseant * notice, this list of conditions and the following disclaimer.
15 1.22 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.22 perseant * notice, this list of conditions and the following disclaimer in the
17 1.22 perseant * documentation and/or other materials provided with the distribution.
18 1.22 perseant * 3. All advertising materials mentioning features or use of this software
19 1.22 perseant * must display the following acknowledgement:
20 1.82 perseant * This product includes software developed by the NetBSD
21 1.82 perseant * Foundation, Inc. and its contributors.
22 1.22 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.22 perseant * contributors may be used to endorse or promote products derived
24 1.22 perseant * from this software without specific prior written permission.
25 1.22 perseant *
26 1.22 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.22 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.22 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.22 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.22 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.22 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.22 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.22 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.22 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.22 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.22 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.22 perseant */
38 1.22 perseant /*-
39 1.1 mycroft * Copyright (c) 1991, 1993, 1994
40 1.1 mycroft * The Regents of the University of California. All rights reserved.
41 1.1 mycroft *
42 1.1 mycroft * Redistribution and use in source and binary forms, with or without
43 1.1 mycroft * modification, are permitted provided that the following conditions
44 1.1 mycroft * are met:
45 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
46 1.1 mycroft * notice, this list of conditions and the following disclaimer.
47 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
49 1.1 mycroft * documentation and/or other materials provided with the distribution.
50 1.97 agc * 3. Neither the name of the University nor the names of its contributors
51 1.1 mycroft * may be used to endorse or promote products derived from this software
52 1.1 mycroft * without specific prior written permission.
53 1.1 mycroft *
54 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 mycroft * SUCH DAMAGE.
65 1.1 mycroft *
66 1.16 fvdl * @(#)lfs_syscalls.c 8.10 (Berkeley) 5/14/95
67 1.1 mycroft */
68 1.61 lukem
69 1.61 lukem #include <sys/cdefs.h>
70 1.99 yamt __KERNEL_RCSID(0, "$NetBSD: lfs_syscalls.c,v 1.99 2003/11/07 14:52:27 yamt Exp $");
71 1.15 thorpej
72 1.86 perseant #ifndef LFS
73 1.86 perseant # define LFS /* for prototypes in syscallargs.h */
74 1.86 perseant #endif
75 1.1 mycroft
76 1.1 mycroft #include <sys/param.h>
77 1.5 cgd #include <sys/systm.h>
78 1.1 mycroft #include <sys/proc.h>
79 1.1 mycroft #include <sys/buf.h>
80 1.1 mycroft #include <sys/mount.h>
81 1.1 mycroft #include <sys/vnode.h>
82 1.1 mycroft #include <sys/malloc.h>
83 1.1 mycroft #include <sys/kernel.h>
84 1.1 mycroft
85 1.78 thorpej #include <sys/sa.h>
86 1.5 cgd #include <sys/syscallargs.h>
87 1.5 cgd
88 1.1 mycroft #include <ufs/ufs/inode.h>
89 1.1 mycroft #include <ufs/ufs/ufsmount.h>
90 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
91 1.1 mycroft
92 1.1 mycroft #include <ufs/lfs/lfs.h>
93 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
94 1.10 christos
95 1.65 perseant struct buf *lfs_fakebuf(struct lfs *, struct vnode *, int, size_t, caddr_t);
96 1.74 yamt int lfs_fasthashget(dev_t, ino_t, struct vnode **);
97 1.1 mycroft
98 1.16 fvdl int debug_cleaner = 0;
99 1.16 fvdl int clean_vnlocked = 0;
100 1.16 fvdl int clean_inlocked = 0;
101 1.22 perseant int verbose_debug = 0;
102 1.22 perseant
103 1.22 perseant pid_t lfs_cleaner_pid = 0;
104 1.80 perseant
105 1.22 perseant #define LFS_FORCE_WRITE UNASSIGNED
106 1.16 fvdl
107 1.1 mycroft /*
108 1.31 christos * sys_lfs_markv:
109 1.1 mycroft *
110 1.1 mycroft * This will mark inodes and blocks dirty, so they are written into the log.
111 1.1 mycroft * It will block until all the blocks have been written. The segment create
112 1.1 mycroft * time passed in the block_info and inode_info structures is used to decide
113 1.1 mycroft * if the data is valid for each block (in case some process dirtied a block
114 1.1 mycroft * or inode that is being cleaned between the determination that a block is
115 1.1 mycroft * live and the lfs_markv call).
116 1.1 mycroft *
117 1.1 mycroft * 0 on success
118 1.1 mycroft * -1/errno is return on error.
119 1.1 mycroft */
120 1.57 perseant #ifdef USE_64BIT_SYSCALLS
121 1.1 mycroft int
122 1.93 fvdl sys_lfs_markv(struct proc *p, void *v, register_t *retval)
123 1.9 thorpej {
124 1.32 drochner struct sys_lfs_markv_args /* {
125 1.5 cgd syscallarg(fsid_t *) fsidp;
126 1.5 cgd syscallarg(struct block_info *) blkiov;
127 1.5 cgd syscallarg(int) blkcnt;
128 1.32 drochner } */ *uap = v;
129 1.57 perseant BLOCK_INFO *blkiov;
130 1.57 perseant int blkcnt, error;
131 1.57 perseant fsid_t fsid;
132 1.57 perseant
133 1.57 perseant if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
134 1.57 perseant return (error);
135 1.57 perseant
136 1.57 perseant if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
137 1.57 perseant return (error);
138 1.57 perseant
139 1.57 perseant blkcnt = SCARG(uap, blkcnt);
140 1.84 perseant if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
141 1.58 jdolecek return (EINVAL);
142 1.58 jdolecek
143 1.57 perseant blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
144 1.57 perseant if ((error = copyin(SCARG(uap, blkiov), blkiov,
145 1.57 perseant blkcnt * sizeof(BLOCK_INFO))) != 0)
146 1.57 perseant goto out;
147 1.57 perseant
148 1.57 perseant if ((error = lfs_markv(p, &fsid, blkiov, blkcnt)) == 0)
149 1.57 perseant copyout(blkiov, SCARG(uap, blkiov),
150 1.57 perseant blkcnt * sizeof(BLOCK_INFO));
151 1.57 perseant out:
152 1.57 perseant free(blkiov, M_SEGMENT);
153 1.57 perseant return error;
154 1.57 perseant }
155 1.57 perseant #else
156 1.57 perseant int
157 1.78 thorpej sys_lfs_markv(struct lwp *l, void *v, register_t *retval)
158 1.57 perseant {
159 1.57 perseant struct sys_lfs_markv_args /* {
160 1.57 perseant syscallarg(fsid_t *) fsidp;
161 1.57 perseant syscallarg(struct block_info *) blkiov;
162 1.57 perseant syscallarg(int) blkcnt;
163 1.57 perseant } */ *uap = v;
164 1.57 perseant BLOCK_INFO *blkiov;
165 1.57 perseant BLOCK_INFO_15 *blkiov15;
166 1.57 perseant int i, blkcnt, error;
167 1.57 perseant fsid_t fsid;
168 1.57 perseant
169 1.93 fvdl if ((error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag)) != 0)
170 1.57 perseant return (error);
171 1.57 perseant
172 1.57 perseant if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
173 1.57 perseant return (error);
174 1.57 perseant
175 1.57 perseant blkcnt = SCARG(uap, blkcnt);
176 1.84 perseant if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
177 1.58 jdolecek return (EINVAL);
178 1.58 jdolecek
179 1.57 perseant blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
180 1.57 perseant blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
181 1.57 perseant if ((error = copyin(SCARG(uap, blkiov), blkiov15,
182 1.57 perseant blkcnt * sizeof(BLOCK_INFO_15))) != 0)
183 1.57 perseant goto out;
184 1.57 perseant
185 1.57 perseant for (i = 0; i < blkcnt; i++) {
186 1.57 perseant blkiov[i].bi_inode = blkiov15[i].bi_inode;
187 1.57 perseant blkiov[i].bi_lbn = blkiov15[i].bi_lbn;
188 1.57 perseant blkiov[i].bi_daddr = blkiov15[i].bi_daddr;
189 1.57 perseant blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
190 1.57 perseant blkiov[i].bi_version = blkiov15[i].bi_version;
191 1.82 perseant blkiov[i].bi_bp = blkiov15[i].bi_bp;
192 1.57 perseant blkiov[i].bi_size = blkiov15[i].bi_size;
193 1.57 perseant }
194 1.57 perseant
195 1.93 fvdl if ((error = lfs_markv(l->l_proc, &fsid, blkiov, blkcnt)) == 0) {
196 1.57 perseant for (i = 0; i < blkcnt; i++) {
197 1.82 perseant blkiov15[i].bi_inode = blkiov[i].bi_inode;
198 1.82 perseant blkiov15[i].bi_lbn = blkiov[i].bi_lbn;
199 1.82 perseant blkiov15[i].bi_daddr = blkiov[i].bi_daddr;
200 1.57 perseant blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
201 1.82 perseant blkiov15[i].bi_version = blkiov[i].bi_version;
202 1.82 perseant blkiov15[i].bi_bp = blkiov[i].bi_bp;
203 1.82 perseant blkiov15[i].bi_size = blkiov[i].bi_size;
204 1.57 perseant }
205 1.57 perseant copyout(blkiov15, SCARG(uap, blkiov),
206 1.57 perseant blkcnt * sizeof(BLOCK_INFO_15));
207 1.57 perseant }
208 1.57 perseant out:
209 1.57 perseant free(blkiov, M_SEGMENT);
210 1.57 perseant free(blkiov15, M_SEGMENT);
211 1.57 perseant return error;
212 1.57 perseant }
213 1.57 perseant #endif
214 1.57 perseant
215 1.77 yamt #define LFS_MARKV_MAX_BLOCKS (LFS_MAX_BUFS)
216 1.77 yamt
217 1.84 perseant int
218 1.57 perseant lfs_markv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
219 1.57 perseant {
220 1.1 mycroft BLOCK_INFO *blkp;
221 1.1 mycroft IFILE *ifp;
222 1.96 yamt struct buf *bp;
223 1.10 christos struct inode *ip = NULL;
224 1.1 mycroft struct lfs *fs;
225 1.1 mycroft struct mount *mntp;
226 1.1 mycroft struct vnode *vp;
227 1.22 perseant #ifdef DEBUG_LFS
228 1.62 chs int vputc = 0, iwritten = 0;
229 1.22 perseant #endif
230 1.1 mycroft ino_t lastino;
231 1.79 fvdl daddr_t b_daddr, v_daddr;
232 1.74 yamt int cnt, error;
233 1.62 chs int do_again = 0;
234 1.74 yamt int numrefed = 0;
235 1.49 perseant ino_t maxino;
236 1.69 perseant size_t obsize;
237 1.1 mycroft
238 1.77 yamt /* number of blocks/inodes that we have already bwrite'ed */
239 1.77 yamt int nblkwritten, ninowritten;
240 1.77 yamt
241 1.57 perseant if ((mntp = vfs_getvfs(fsidp)) == NULL)
242 1.53 perseant return (ENOENT);
243 1.1 mycroft
244 1.22 perseant fs = VFSTOUFS(mntp)->um_lfs;
245 1.96 yamt
246 1.96 yamt if (fs->lfs_ronly)
247 1.96 yamt return EROFS;
248 1.96 yamt
249 1.89 fvdl maxino = (fragstoblks(fs, fsbtofrags(fs, VTOI(fs->lfs_ivnode)->i_ffs1_blocks)) -
250 1.49 perseant fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
251 1.49 perseant
252 1.57 perseant cnt = blkcnt;
253 1.22 perseant
254 1.53 perseant if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
255 1.53 perseant return (error);
256 1.53 perseant
257 1.22 perseant /*
258 1.22 perseant * This seglock is just to prevent the fact that we might have to sleep
259 1.22 perseant * from allowing the possibility that our blocks might become
260 1.22 perseant * invalid.
261 1.22 perseant *
262 1.22 perseant * It is also important to note here that unless we specify SEGM_CKP,
263 1.22 perseant * any Ifile blocks that we might be asked to clean will never get
264 1.22 perseant * to the disk.
265 1.22 perseant */
266 1.67 perseant lfs_seglock(fs, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
267 1.22 perseant
268 1.1 mycroft /* Mark blocks/inodes dirty. */
269 1.1 mycroft error = 0;
270 1.1 mycroft
271 1.22 perseant #ifdef DEBUG_LFS
272 1.22 perseant /* Run through and count the inodes */
273 1.22 perseant lastino = LFS_UNUSED_INUM;
274 1.62 chs for (blkp = blkiov; cnt--; ++blkp) {
275 1.62 chs if (lastino != blkp->bi_inode) {
276 1.22 perseant lastino = blkp->bi_inode;
277 1.22 perseant vputc++;
278 1.22 perseant }
279 1.22 perseant }
280 1.57 perseant cnt = blkcnt;
281 1.22 perseant printf("[%d/",vputc);
282 1.62 chs iwritten = 0;
283 1.22 perseant #endif /* DEBUG_LFS */
284 1.22 perseant /* these were inside the initialization for the for loop */
285 1.22 perseant v_daddr = LFS_UNUSED_DADDR;
286 1.22 perseant lastino = LFS_UNUSED_INUM;
287 1.77 yamt nblkwritten = ninowritten = 0;
288 1.57 perseant for (blkp = blkiov; cnt--; ++blkp)
289 1.22 perseant {
290 1.62 chs if (blkp->bi_daddr == LFS_FORCE_WRITE)
291 1.79 fvdl printf("lfs_markv: warning: force-writing ino %d "
292 1.79 fvdl "lbn %lld\n",
293 1.79 fvdl blkp->bi_inode, (long long)blkp->bi_lbn);
294 1.49 perseant /* Bounds-check incoming data, avoid panic for failed VGET */
295 1.49 perseant if (blkp->bi_inode <= 0 || blkp->bi_inode >= maxino) {
296 1.49 perseant error = EINVAL;
297 1.96 yamt goto err3;
298 1.49 perseant }
299 1.1 mycroft /*
300 1.1 mycroft * Get the IFILE entry (only once) and see if the file still
301 1.1 mycroft * exists.
302 1.1 mycroft */
303 1.1 mycroft if (lastino != blkp->bi_inode) {
304 1.22 perseant /*
305 1.22 perseant * Finish the old file, if there was one. The presence
306 1.22 perseant * of a usable vnode in vp is signaled by a valid v_daddr.
307 1.22 perseant */
308 1.62 chs if (v_daddr != LFS_UNUSED_DADDR) {
309 1.30 perseant #ifdef DEBUG_LFS
310 1.62 chs if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
311 1.22 perseant iwritten++;
312 1.22 perseant #endif
313 1.1 mycroft lfs_vunref(vp);
314 1.22 perseant numrefed--;
315 1.1 mycroft }
316 1.1 mycroft
317 1.22 perseant /*
318 1.22 perseant * Start a new file
319 1.22 perseant */
320 1.1 mycroft lastino = blkp->bi_inode;
321 1.1 mycroft if (blkp->bi_inode == LFS_IFILE_INUM)
322 1.1 mycroft v_daddr = fs->lfs_idaddr;
323 1.1 mycroft else {
324 1.1 mycroft LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
325 1.22 perseant /* XXX fix for force write */
326 1.1 mycroft v_daddr = ifp->if_daddr;
327 1.1 mycroft brelse(bp);
328 1.1 mycroft }
329 1.22 perseant /* Don't force-write the ifile */
330 1.22 perseant if (blkp->bi_inode == LFS_IFILE_INUM
331 1.22 perseant && blkp->bi_daddr == LFS_FORCE_WRITE)
332 1.22 perseant {
333 1.22 perseant continue;
334 1.22 perseant }
335 1.35 perseant if (v_daddr == LFS_UNUSED_DADDR
336 1.35 perseant && blkp->bi_daddr != LFS_FORCE_WRITE)
337 1.22 perseant {
338 1.1 mycroft continue;
339 1.22 perseant }
340 1.1 mycroft
341 1.1 mycroft /* Get the vnode/inode. */
342 1.62 chs error = lfs_fastvget(mntp, blkp->bi_inode, v_daddr,
343 1.22 perseant &vp,
344 1.62 chs (blkp->bi_lbn == LFS_UNUSED_LBN
345 1.22 perseant ? blkp->bi_bp
346 1.74 yamt : NULL));
347 1.22 perseant
348 1.62 chs if (!error) {
349 1.22 perseant numrefed++;
350 1.22 perseant }
351 1.62 chs if (error) {
352 1.43 perseant #ifdef DEBUG_LFS
353 1.29 perseant printf("lfs_markv: lfs_fastvget failed with %d (ino %d, segment %d)\n",
354 1.22 perseant error, blkp->bi_inode,
355 1.57 perseant dtosn(fs, blkp->bi_daddr));
356 1.43 perseant #endif /* DEBUG_LFS */
357 1.22 perseant /*
358 1.22 perseant * If we got EAGAIN, that means that the
359 1.22 perseant * Inode was locked. This is
360 1.22 perseant * recoverable: just clean the rest of
361 1.22 perseant * this segment, and let the cleaner try
362 1.82 perseant * again with another. (When the
363 1.22 perseant * cleaner runs again, this segment will
364 1.22 perseant * sort high on the list, since it is
365 1.22 perseant * now almost entirely empty.) But, we
366 1.22 perseant * still set v_daddr = LFS_UNUSED_ADDR
367 1.22 perseant * so as not to test this over and over
368 1.22 perseant * again.
369 1.22 perseant */
370 1.62 chs if (error == EAGAIN) {
371 1.22 perseant error = 0;
372 1.22 perseant do_again++;
373 1.22 perseant }
374 1.22 perseant #ifdef DIAGNOSTIC
375 1.62 chs else if (error != ENOENT)
376 1.22 perseant panic("lfs_markv VFS_VGET FAILED");
377 1.1 mycroft #endif
378 1.22 perseant /* lastino = LFS_UNUSED_INUM; */
379 1.1 mycroft v_daddr = LFS_UNUSED_DADDR;
380 1.22 perseant vp = NULL;
381 1.22 perseant ip = NULL;
382 1.1 mycroft continue;
383 1.19 pk }
384 1.1 mycroft ip = VTOI(vp);
385 1.77 yamt ninowritten++;
386 1.22 perseant } else if (v_daddr == LFS_UNUSED_DADDR) {
387 1.22 perseant /*
388 1.22 perseant * This can only happen if the vnode is dead (or
389 1.22 perseant * in any case we can't get it...e.g., it is
390 1.22 perseant * inlocked). Keep going.
391 1.22 perseant */
392 1.1 mycroft continue;
393 1.22 perseant }
394 1.22 perseant
395 1.22 perseant /* Past this point we are guaranteed that vp, ip are valid. */
396 1.1 mycroft
397 1.1 mycroft /* If this BLOCK_INFO didn't contain a block, keep going. */
398 1.22 perseant if (blkp->bi_lbn == LFS_UNUSED_LBN) {
399 1.22 perseant /* XXX need to make sure that the inode gets written in this case */
400 1.22 perseant /* XXX but only write the inode if it's the right one */
401 1.53 perseant if (blkp->bi_inode != LFS_IFILE_INUM) {
402 1.53 perseant LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
403 1.62 chs if (ifp->if_daddr == blkp->bi_daddr
404 1.22 perseant || blkp->bi_daddr == LFS_FORCE_WRITE)
405 1.22 perseant {
406 1.47 perseant LFS_SET_UINO(ip, IN_CLEANING);
407 1.22 perseant }
408 1.53 perseant brelse(bp);
409 1.53 perseant }
410 1.1 mycroft continue;
411 1.22 perseant }
412 1.22 perseant
413 1.22 perseant b_daddr = 0;
414 1.62 chs if (blkp->bi_daddr != LFS_FORCE_WRITE) {
415 1.22 perseant if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
416 1.57 perseant dbtofsb(fs, b_daddr) != blkp->bi_daddr)
417 1.22 perseant {
418 1.62 chs if (dtosn(fs,dbtofsb(fs, b_daddr))
419 1.57 perseant == dtosn(fs,blkp->bi_daddr))
420 1.22 perseant {
421 1.79 fvdl printf("lfs_markv: wrong da same seg: %llx vs %llx\n",
422 1.79 fvdl (long long)blkp->bi_daddr, (long long)dbtofsb(fs, b_daddr));
423 1.22 perseant }
424 1.67 perseant do_again++;
425 1.22 perseant continue;
426 1.22 perseant }
427 1.22 perseant }
428 1.69 perseant
429 1.69 perseant /*
430 1.69 perseant * Check block sizes. The blocks being cleaned come from
431 1.69 perseant * disk, so they should have the same size as their on-disk
432 1.69 perseant * counterparts.
433 1.69 perseant */
434 1.72 yamt if (blkp->bi_lbn >= 0)
435 1.72 yamt obsize = blksize(fs, ip, blkp->bi_lbn);
436 1.72 yamt else
437 1.72 yamt obsize = fs->lfs_bsize;
438 1.69 perseant /* Check for fragment size change */
439 1.69 perseant if (blkp->bi_lbn >= 0 && blkp->bi_lbn < NDADDR) {
440 1.69 perseant obsize = ip->i_lfs_fragsize[blkp->bi_lbn];
441 1.69 perseant }
442 1.69 perseant if (obsize != blkp->bi_size) {
443 1.79 fvdl printf("lfs_markv: ino %d lbn %lld wrong size (%ld != %d), try again\n",
444 1.79 fvdl blkp->bi_inode, (long long)blkp->bi_lbn,
445 1.70 briggs (long) obsize, blkp->bi_size);
446 1.69 perseant do_again++;
447 1.69 perseant continue;
448 1.69 perseant }
449 1.69 perseant
450 1.22 perseant /*
451 1.69 perseant * If we get to here, then we are keeping the block. If
452 1.22 perseant * it is an indirect block, we want to actually put it
453 1.22 perseant * in the buffer cache so that it can be updated in the
454 1.82 perseant * finish_meta section. If it's not, we need to
455 1.22 perseant * allocate a fake buffer so that writeseg can perform
456 1.22 perseant * the copyin and write the buffer.
457 1.22 perseant */
458 1.38 perseant if (ip->i_number != LFS_IFILE_INUM && blkp->bi_lbn >= 0) {
459 1.38 perseant /* Data Block */
460 1.65 perseant bp = lfs_fakebuf(fs, vp, blkp->bi_lbn,
461 1.23 perseant blkp->bi_size, blkp->bi_bp);
462 1.23 perseant /* Pretend we used bread() to get it */
463 1.57 perseant bp->b_blkno = fsbtodb(fs, blkp->bi_daddr);
464 1.38 perseant } else {
465 1.75 yamt /* Indirect block or ifile */
466 1.75 yamt if (blkp->bi_size != fs->lfs_bsize &&
467 1.75 yamt ip->i_number != LFS_IFILE_INUM)
468 1.72 yamt panic("lfs_markv: partial indirect block?"
469 1.72 yamt " size=%d\n", blkp->bi_size);
470 1.22 perseant bp = getblk(vp, blkp->bi_lbn, blkp->bi_size, 0, 0);
471 1.22 perseant if (!(bp->b_flags & (B_DONE|B_DELWRI))) { /* B_CACHE */
472 1.22 perseant /*
473 1.22 perseant * The block in question was not found
474 1.22 perseant * in the cache; i.e., the block that
475 1.82 perseant * getblk() returned is empty. So, we
476 1.22 perseant * can (and should) copy in the
477 1.22 perseant * contents, because we've already
478 1.22 perseant * determined that this was the right
479 1.22 perseant * version of this block on disk.
480 1.22 perseant *
481 1.22 perseant * And, it can't have changed underneath
482 1.22 perseant * us, because we have the segment lock.
483 1.22 perseant */
484 1.22 perseant error = copyin(blkp->bi_bp, bp->b_data, blkp->bi_size);
485 1.62 chs if (error)
486 1.22 perseant goto err2;
487 1.22 perseant }
488 1.22 perseant }
489 1.96 yamt if ((error = lfs_bwrite_ext(bp, BW_CLEAN)) != 0)
490 1.22 perseant goto err2;
491 1.77 yamt
492 1.77 yamt nblkwritten++;
493 1.77 yamt /*
494 1.77 yamt * XXX should account indirect blocks and ifile pages as well
495 1.77 yamt */
496 1.89 fvdl if (nblkwritten + lblkno(fs, ninowritten * sizeof (struct ufs1_dinode))
497 1.77 yamt > LFS_MARKV_MAX_BLOCKS) {
498 1.77 yamt #ifdef DEBUG_LFS
499 1.77 yamt printf("lfs_markv: writing %d blks %d inos\n",
500 1.77 yamt nblkwritten, ninowritten);
501 1.77 yamt #endif
502 1.77 yamt lfs_segwrite(mntp, SEGM_CLEAN);
503 1.77 yamt nblkwritten = ninowritten = 0;
504 1.77 yamt }
505 1.22 perseant }
506 1.22 perseant
507 1.22 perseant /*
508 1.22 perseant * Finish the old file, if there was one
509 1.22 perseant */
510 1.62 chs if (v_daddr != LFS_UNUSED_DADDR) {
511 1.22 perseant #ifdef DEBUG_LFS
512 1.62 chs if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
513 1.22 perseant iwritten++;
514 1.22 perseant #endif
515 1.22 perseant lfs_vunref(vp);
516 1.22 perseant numrefed--;
517 1.22 perseant }
518 1.22 perseant
519 1.74 yamt #ifdef DEBUG_LFS
520 1.74 yamt printf("%d]",iwritten);
521 1.74 yamt if (numrefed != 0) {
522 1.74 yamt panic("lfs_markv: numrefed=%d", numrefed);
523 1.74 yamt }
524 1.74 yamt #endif
525 1.74 yamt
526 1.77 yamt #ifdef DEBUG_LFS
527 1.77 yamt printf("lfs_markv: writing %d blks %d inos (check point)\n",
528 1.77 yamt nblkwritten, ninowritten);
529 1.77 yamt #endif
530 1.22 perseant /*
531 1.22 perseant * The last write has to be SEGM_SYNC, because of calling semantics.
532 1.22 perseant * It also has to be SEGM_CKP, because otherwise we could write
533 1.22 perseant * over the newly cleaned data contained in a checkpoint, and then
534 1.22 perseant * we'd be unhappy at recovery time.
535 1.22 perseant */
536 1.67 perseant lfs_segwrite(mntp, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
537 1.22 perseant
538 1.1 mycroft lfs_segunlock(fs);
539 1.1 mycroft
540 1.53 perseant vfs_unbusy(mntp);
541 1.62 chs if (error)
542 1.22 perseant return (error);
543 1.62 chs else if (do_again)
544 1.22 perseant return EAGAIN;
545 1.1 mycroft
546 1.22 perseant return 0;
547 1.22 perseant
548 1.96 yamt err2:
549 1.29 perseant printf("lfs_markv err2\n");
550 1.53 perseant
551 1.96 yamt /*
552 1.96 yamt * XXX we're here because copyin() failed.
553 1.96 yamt * XXX it means that we can't trust the cleanerd. too bad.
554 1.96 yamt * XXX how can we recover from this?
555 1.96 yamt */
556 1.96 yamt
557 1.96 yamt err3:
558 1.96 yamt /*
559 1.96 yamt * XXX should do segwrite here anyway?
560 1.96 yamt */
561 1.96 yamt
562 1.96 yamt if (v_daddr != LFS_UNUSED_DADDR) {
563 1.96 yamt lfs_vunref(vp);
564 1.96 yamt --numrefed;
565 1.22 perseant }
566 1.96 yamt
567 1.1 mycroft lfs_segunlock(fs);
568 1.53 perseant vfs_unbusy(mntp);
569 1.53 perseant #ifdef DEBUG_LFS
570 1.74 yamt if (numrefed != 0) {
571 1.74 yamt panic("lfs_markv: numrefed=%d", numrefed);
572 1.53 perseant }
573 1.53 perseant #endif
574 1.53 perseant
575 1.22 perseant return (error);
576 1.1 mycroft }
577 1.1 mycroft
578 1.1 mycroft /*
579 1.31 christos * sys_lfs_bmapv:
580 1.1 mycroft *
581 1.1 mycroft * This will fill in the current disk address for arrays of blocks.
582 1.1 mycroft *
583 1.1 mycroft * 0 on success
584 1.1 mycroft * -1/errno is return on error.
585 1.1 mycroft */
586 1.57 perseant #ifdef USE_64BIT_SYSCALLS
587 1.57 perseant int
588 1.93 fvdl sys_lfs_bmapv(struct proc *p, void *v, register_t *retval)
589 1.57 perseant {
590 1.57 perseant struct sys_lfs_bmapv_args /* {
591 1.57 perseant syscallarg(fsid_t *) fsidp;
592 1.57 perseant syscallarg(struct block_info *) blkiov;
593 1.57 perseant syscallarg(int) blkcnt;
594 1.57 perseant } */ *uap = v;
595 1.57 perseant BLOCK_INFO *blkiov;
596 1.57 perseant int blkcnt, error;
597 1.57 perseant fsid_t fsid;
598 1.22 perseant
599 1.57 perseant if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
600 1.57 perseant return (error);
601 1.57 perseant
602 1.57 perseant if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
603 1.57 perseant return (error);
604 1.57 perseant
605 1.57 perseant blkcnt = SCARG(uap, blkcnt);
606 1.71 itojun if ((u_int) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
607 1.71 itojun return (EINVAL);
608 1.57 perseant blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
609 1.57 perseant if ((error = copyin(SCARG(uap, blkiov), blkiov,
610 1.57 perseant blkcnt * sizeof(BLOCK_INFO))) != 0)
611 1.57 perseant goto out;
612 1.57 perseant
613 1.57 perseant if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0)
614 1.57 perseant copyout(blkiov, SCARG(uap, blkiov),
615 1.57 perseant blkcnt * sizeof(BLOCK_INFO));
616 1.57 perseant out:
617 1.57 perseant free(blkiov, M_SEGMENT);
618 1.57 perseant return error;
619 1.57 perseant }
620 1.57 perseant #else
621 1.1 mycroft int
622 1.78 thorpej sys_lfs_bmapv(struct lwp *l, void *v, register_t *retval)
623 1.9 thorpej {
624 1.32 drochner struct sys_lfs_bmapv_args /* {
625 1.32 drochner syscallarg(fsid_t *) fsidp;
626 1.32 drochner syscallarg(struct block_info *) blkiov;
627 1.32 drochner syscallarg(int) blkcnt;
628 1.32 drochner } */ *uap = v;
629 1.78 thorpej struct proc *p = l->l_proc;
630 1.57 perseant BLOCK_INFO *blkiov;
631 1.57 perseant BLOCK_INFO_15 *blkiov15;
632 1.57 perseant int i, blkcnt, error;
633 1.57 perseant fsid_t fsid;
634 1.57 perseant
635 1.57 perseant if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
636 1.57 perseant return (error);
637 1.57 perseant
638 1.57 perseant if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
639 1.57 perseant return (error);
640 1.57 perseant
641 1.57 perseant blkcnt = SCARG(uap, blkcnt);
642 1.90 nakayama if ((size_t) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
643 1.71 itojun return (EINVAL);
644 1.57 perseant blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
645 1.57 perseant blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
646 1.57 perseant if ((error = copyin(SCARG(uap, blkiov), blkiov15,
647 1.57 perseant blkcnt * sizeof(BLOCK_INFO_15))) != 0)
648 1.57 perseant goto out;
649 1.57 perseant
650 1.57 perseant for (i = 0; i < blkcnt; i++) {
651 1.57 perseant blkiov[i].bi_inode = blkiov15[i].bi_inode;
652 1.57 perseant blkiov[i].bi_lbn = blkiov15[i].bi_lbn;
653 1.57 perseant blkiov[i].bi_daddr = blkiov15[i].bi_daddr;
654 1.57 perseant blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
655 1.57 perseant blkiov[i].bi_version = blkiov15[i].bi_version;
656 1.82 perseant blkiov[i].bi_bp = blkiov15[i].bi_bp;
657 1.57 perseant blkiov[i].bi_size = blkiov15[i].bi_size;
658 1.57 perseant }
659 1.57 perseant
660 1.93 fvdl if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0) {
661 1.57 perseant for (i = 0; i < blkcnt; i++) {
662 1.82 perseant blkiov15[i].bi_inode = blkiov[i].bi_inode;
663 1.82 perseant blkiov15[i].bi_lbn = blkiov[i].bi_lbn;
664 1.82 perseant blkiov15[i].bi_daddr = blkiov[i].bi_daddr;
665 1.57 perseant blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
666 1.82 perseant blkiov15[i].bi_version = blkiov[i].bi_version;
667 1.82 perseant blkiov15[i].bi_bp = blkiov[i].bi_bp;
668 1.82 perseant blkiov15[i].bi_size = blkiov[i].bi_size;
669 1.57 perseant }
670 1.57 perseant copyout(blkiov15, SCARG(uap, blkiov),
671 1.57 perseant blkcnt * sizeof(BLOCK_INFO_15));
672 1.57 perseant }
673 1.57 perseant out:
674 1.57 perseant free(blkiov, M_SEGMENT);
675 1.57 perseant free(blkiov15, M_SEGMENT);
676 1.57 perseant return error;
677 1.57 perseant }
678 1.57 perseant #endif
679 1.57 perseant
680 1.84 perseant int
681 1.93 fvdl lfs_bmapv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
682 1.57 perseant {
683 1.1 mycroft BLOCK_INFO *blkp;
684 1.22 perseant IFILE *ifp;
685 1.22 perseant struct buf *bp;
686 1.22 perseant struct inode *ip = NULL;
687 1.22 perseant struct lfs *fs;
688 1.1 mycroft struct mount *mntp;
689 1.16 fvdl struct ufsmount *ump;
690 1.1 mycroft struct vnode *vp;
691 1.22 perseant ino_t lastino;
692 1.79 fvdl daddr_t v_daddr;
693 1.74 yamt int cnt, error;
694 1.74 yamt int numrefed = 0;
695 1.1 mycroft
696 1.93 fvdl lfs_cleaner_pid = p->p_pid;
697 1.22 perseant
698 1.57 perseant if ((mntp = vfs_getvfs(fsidp)) == NULL)
699 1.53 perseant return (ENOENT);
700 1.22 perseant
701 1.22 perseant ump = VFSTOUFS(mntp);
702 1.53 perseant if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
703 1.53 perseant return (error);
704 1.22 perseant
705 1.57 perseant cnt = blkcnt;
706 1.22 perseant
707 1.22 perseant fs = VFSTOUFS(mntp)->um_lfs;
708 1.22 perseant
709 1.22 perseant error = 0;
710 1.22 perseant
711 1.22 perseant /* these were inside the initialization for the for loop */
712 1.22 perseant v_daddr = LFS_UNUSED_DADDR;
713 1.22 perseant lastino = LFS_UNUSED_INUM;
714 1.57 perseant for (blkp = blkiov; cnt--; ++blkp)
715 1.22 perseant {
716 1.16 fvdl /*
717 1.22 perseant * Get the IFILE entry (only once) and see if the file still
718 1.22 perseant * exists.
719 1.16 fvdl */
720 1.22 perseant if (lastino != blkp->bi_inode) {
721 1.22 perseant /*
722 1.22 perseant * Finish the old file, if there was one. The presence
723 1.22 perseant * of a usable vnode in vp is signaled by a valid
724 1.22 perseant * v_daddr.
725 1.22 perseant */
726 1.62 chs if (v_daddr != LFS_UNUSED_DADDR) {
727 1.22 perseant lfs_vunref(vp);
728 1.22 perseant numrefed--;
729 1.22 perseant }
730 1.22 perseant
731 1.22 perseant /*
732 1.22 perseant * Start a new file
733 1.22 perseant */
734 1.22 perseant lastino = blkp->bi_inode;
735 1.22 perseant if (blkp->bi_inode == LFS_IFILE_INUM)
736 1.22 perseant v_daddr = fs->lfs_idaddr;
737 1.22 perseant else {
738 1.22 perseant LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
739 1.22 perseant v_daddr = ifp->if_daddr;
740 1.22 perseant brelse(bp);
741 1.22 perseant }
742 1.22 perseant if (v_daddr == LFS_UNUSED_DADDR) {
743 1.22 perseant blkp->bi_daddr = LFS_UNUSED_DADDR;
744 1.22 perseant continue;
745 1.22 perseant }
746 1.22 perseant /*
747 1.22 perseant * A regular call to VFS_VGET could deadlock
748 1.22 perseant * here. Instead, we try an unlocked access.
749 1.22 perseant */
750 1.22 perseant vp = ufs_ihashlookup(ump->um_dev, blkp->bi_inode);
751 1.24 perseant if (vp != NULL && !(vp->v_flag & VXLOCK)) {
752 1.22 perseant ip = VTOI(vp);
753 1.42 perseant if (lfs_vref(vp)) {
754 1.42 perseant v_daddr = LFS_UNUSED_DADDR;
755 1.42 perseant continue;
756 1.42 perseant }
757 1.43 perseant numrefed++;
758 1.22 perseant } else {
759 1.92 thorpej error = VFS_VGET(mntp, blkp->bi_inode, &vp);
760 1.62 chs if (error) {
761 1.24 perseant #ifdef DEBUG_LFS
762 1.25 perseant printf("lfs_bmapv: vget of ino %d failed with %d",blkp->bi_inode,error);
763 1.24 perseant #endif
764 1.43 perseant v_daddr = LFS_UNUSED_DADDR;
765 1.22 perseant continue;
766 1.22 perseant } else {
767 1.74 yamt KASSERT(VOP_ISLOCKED(vp));
768 1.74 yamt VOP_UNLOCK(vp, 0);
769 1.22 perseant numrefed++;
770 1.22 perseant }
771 1.22 perseant }
772 1.22 perseant ip = VTOI(vp);
773 1.22 perseant } else if (v_daddr == LFS_UNUSED_DADDR) {
774 1.22 perseant /*
775 1.22 perseant * This can only happen if the vnode is dead.
776 1.82 perseant * Keep going. Note that we DO NOT set the
777 1.22 perseant * bi_addr to anything -- if we failed to get
778 1.22 perseant * the vnode, for example, we want to assume
779 1.22 perseant * conservatively that all of its blocks *are*
780 1.22 perseant * located in the segment in question.
781 1.22 perseant * lfs_markv will throw them out if we are
782 1.22 perseant * wrong.
783 1.22 perseant */
784 1.22 perseant /* blkp->bi_daddr = LFS_UNUSED_DADDR; */
785 1.22 perseant continue;
786 1.22 perseant }
787 1.22 perseant
788 1.22 perseant /* Past this point we are guaranteed that vp, ip are valid. */
789 1.22 perseant
790 1.62 chs if (blkp->bi_lbn == LFS_UNUSED_LBN) {
791 1.22 perseant /*
792 1.22 perseant * We just want the inode address, which is
793 1.22 perseant * conveniently in v_daddr.
794 1.22 perseant */
795 1.22 perseant blkp->bi_daddr = v_daddr;
796 1.22 perseant } else {
797 1.79 fvdl daddr_t bi_daddr;
798 1.79 fvdl
799 1.79 fvdl /* XXX ondisk32 */
800 1.22 perseant error = VOP_BMAP(vp, blkp->bi_lbn, NULL,
801 1.79 fvdl &bi_daddr, NULL);
802 1.62 chs if (error)
803 1.22 perseant {
804 1.22 perseant blkp->bi_daddr = LFS_UNUSED_DADDR;
805 1.22 perseant continue;
806 1.22 perseant }
807 1.79 fvdl blkp->bi_daddr = dbtofsb(fs, bi_daddr);
808 1.66 perseant /* Fill in the block size, too */
809 1.72 yamt if (blkp->bi_lbn >= 0)
810 1.72 yamt blkp->bi_size = blksize(fs, ip, blkp->bi_lbn);
811 1.72 yamt else
812 1.72 yamt blkp->bi_size = fs->lfs_bsize;
813 1.22 perseant }
814 1.22 perseant }
815 1.22 perseant
816 1.22 perseant /*
817 1.22 perseant * Finish the old file, if there was one. The presence
818 1.22 perseant * of a usable vnode in vp is signaled by a valid v_daddr.
819 1.22 perseant */
820 1.62 chs if (v_daddr != LFS_UNUSED_DADDR) {
821 1.22 perseant lfs_vunref(vp);
822 1.22 perseant numrefed--;
823 1.22 perseant }
824 1.22 perseant
825 1.74 yamt #ifdef DEBUG_LFS
826 1.74 yamt if (numrefed != 0) {
827 1.74 yamt panic("lfs_bmapv: numrefed=%d", numrefed);
828 1.22 perseant }
829 1.74 yamt #endif
830 1.22 perseant
831 1.53 perseant vfs_unbusy(mntp);
832 1.22 perseant
833 1.22 perseant return 0;
834 1.1 mycroft }
835 1.1 mycroft
836 1.1 mycroft /*
837 1.31 christos * sys_lfs_segclean:
838 1.1 mycroft *
839 1.1 mycroft * Mark the segment clean.
840 1.1 mycroft *
841 1.1 mycroft * 0 on success
842 1.1 mycroft * -1/errno is return on error.
843 1.1 mycroft */
844 1.1 mycroft int
845 1.78 thorpej sys_lfs_segclean(struct lwp *l, void *v, register_t *retval)
846 1.9 thorpej {
847 1.32 drochner struct sys_lfs_segclean_args /* {
848 1.32 drochner syscallarg(fsid_t *) fsidp;
849 1.32 drochner syscallarg(u_long) segment;
850 1.32 drochner } */ *uap = v;
851 1.80 perseant struct lfs *fs;
852 1.1 mycroft struct mount *mntp;
853 1.1 mycroft fsid_t fsid;
854 1.1 mycroft int error;
855 1.67 perseant unsigned long segnum;
856 1.80 perseant struct proc *p = l->l_proc;
857 1.22 perseant
858 1.10 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
859 1.1 mycroft return (error);
860 1.22 perseant
861 1.10 christos if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
862 1.1 mycroft return (error);
863 1.16 fvdl if ((mntp = vfs_getvfs(&fsid)) == NULL)
864 1.53 perseant return (ENOENT);
865 1.22 perseant
866 1.1 mycroft fs = VFSTOUFS(mntp)->um_lfs;
867 1.67 perseant segnum = SCARG(uap, segment);
868 1.22 perseant
869 1.53 perseant if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
870 1.53 perseant return (error);
871 1.80 perseant
872 1.65 perseant lfs_seglock(fs, SEGM_PROT);
873 1.80 perseant error = lfs_do_segclean(fs, segnum);
874 1.80 perseant lfs_segunlock(fs);
875 1.80 perseant vfs_unbusy(mntp);
876 1.80 perseant return error;
877 1.80 perseant }
878 1.80 perseant
879 1.80 perseant /*
880 1.80 perseant * Actually mark the segment clean.
881 1.80 perseant * Must be called with the segment lock held.
882 1.80 perseant */
883 1.80 perseant int
884 1.80 perseant lfs_do_segclean(struct lfs *fs, unsigned long segnum)
885 1.80 perseant {
886 1.80 perseant struct buf *bp;
887 1.80 perseant CLEANERINFO *cip;
888 1.80 perseant SEGUSE *sup;
889 1.80 perseant
890 1.80 perseant if (dtosn(fs, fs->lfs_curseg) == segnum) {
891 1.80 perseant return (EBUSY);
892 1.80 perseant }
893 1.80 perseant
894 1.67 perseant LFS_SEGENTRY(sup, fs, segnum, bp);
895 1.67 perseant if (sup->su_nbytes) {
896 1.67 perseant printf("lfs_segclean: not cleaning segment %lu: %d live bytes\n",
897 1.67 perseant segnum, sup->su_nbytes);
898 1.67 perseant brelse(bp);
899 1.67 perseant return (EBUSY);
900 1.67 perseant }
901 1.1 mycroft if (sup->su_flags & SEGUSE_ACTIVE) {
902 1.1 mycroft brelse(bp);
903 1.1 mycroft return (EBUSY);
904 1.50 perseant }
905 1.50 perseant if (!(sup->su_flags & SEGUSE_DIRTY)) {
906 1.50 perseant brelse(bp);
907 1.50 perseant return (EALREADY);
908 1.1 mycroft }
909 1.22 perseant
910 1.57 perseant fs->lfs_avail += segtod(fs, 1);
911 1.46 perseant if (sup->su_flags & SEGUSE_SUPERBLOCK)
912 1.57 perseant fs->lfs_avail -= btofsb(fs, LFS_SBPAD);
913 1.67 perseant if (fs->lfs_version > 1 && segnum == 0 &&
914 1.57 perseant fs->lfs_start < btofsb(fs, LFS_LABELPAD))
915 1.57 perseant fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
916 1.57 perseant fs->lfs_bfree += sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
917 1.57 perseant btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
918 1.57 perseant fs->lfs_dmeta -= sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
919 1.57 perseant btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
920 1.43 perseant if (fs->lfs_dmeta < 0)
921 1.43 perseant fs->lfs_dmeta = 0;
922 1.1 mycroft sup->su_flags &= ~SEGUSE_DIRTY;
923 1.80 perseant LFS_WRITESEGENTRY(sup, fs, segnum, bp);
924 1.22 perseant
925 1.1 mycroft LFS_CLEANERINFO(cip, fs, bp);
926 1.1 mycroft ++cip->clean;
927 1.1 mycroft --cip->dirty;
928 1.22 perseant fs->lfs_nclean = cip->clean;
929 1.49 perseant cip->bfree = fs->lfs_bfree;
930 1.49 perseant cip->avail = fs->lfs_avail - fs->lfs_ravail;
931 1.65 perseant (void) LFS_BWRITE_LOG(bp);
932 1.1 mycroft wakeup(&fs->lfs_avail);
933 1.22 perseant
934 1.1 mycroft return (0);
935 1.1 mycroft }
936 1.1 mycroft
937 1.1 mycroft /*
938 1.1 mycroft * This will block until a segment in file system fsid is written. A timeout
939 1.1 mycroft * in milliseconds may be specified which will awake the cleaner automatically.
940 1.1 mycroft * An fsid of -1 means any file system, and a timeout of 0 means forever.
941 1.84 perseant */
942 1.84 perseant int
943 1.84 perseant lfs_segwait(fsid_t *fsidp, struct timeval *tv)
944 1.84 perseant {
945 1.84 perseant struct mount *mntp;
946 1.84 perseant void *addr;
947 1.84 perseant u_long timeout;
948 1.84 perseant int error, s;
949 1.84 perseant
950 1.84 perseant if ((mntp = vfs_getvfs(fsidp)) == NULL)
951 1.84 perseant addr = &lfs_allclean_wakeup;
952 1.84 perseant else
953 1.84 perseant addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
954 1.84 perseant /*
955 1.84 perseant * XXX THIS COULD SLEEP FOREVER IF TIMEOUT IS {0,0}!
956 1.84 perseant * XXX IS THAT WHAT IS INTENDED?
957 1.84 perseant */
958 1.84 perseant s = splclock();
959 1.84 perseant timeradd(tv, &time, tv);
960 1.84 perseant timeout = hzto(tv);
961 1.84 perseant splx(s);
962 1.84 perseant error = tsleep(addr, PCATCH | PUSER, "segment", timeout);
963 1.84 perseant return (error == ERESTART ? EINTR : 0);
964 1.84 perseant }
965 1.84 perseant
966 1.84 perseant /*
967 1.84 perseant * sys_lfs_segwait:
968 1.84 perseant *
969 1.84 perseant * System call wrapper around lfs_segwait().
970 1.1 mycroft *
971 1.1 mycroft * 0 on success
972 1.1 mycroft * 1 on timeout
973 1.1 mycroft * -1/errno is return on error.
974 1.1 mycroft */
975 1.1 mycroft int
976 1.78 thorpej sys_lfs_segwait(struct lwp *l, void *v, register_t *retval)
977 1.9 thorpej {
978 1.32 drochner struct sys_lfs_segwait_args /* {
979 1.32 drochner syscallarg(fsid_t *) fsidp;
980 1.32 drochner syscallarg(struct timeval *) tv;
981 1.32 drochner } */ *uap = v;
982 1.78 thorpej struct proc *p = l->l_proc;
983 1.1 mycroft struct timeval atv;
984 1.1 mycroft fsid_t fsid;
985 1.84 perseant int error;
986 1.22 perseant
987 1.84 perseant /* XXX need we be su to segwait? */
988 1.10 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
989 1.1 mycroft return (error);
990 1.1 mycroft }
991 1.10 christos if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
992 1.1 mycroft return (error);
993 1.22 perseant
994 1.5 cgd if (SCARG(uap, tv)) {
995 1.10 christos error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
996 1.10 christos if (error)
997 1.1 mycroft return (error);
998 1.1 mycroft if (itimerfix(&atv))
999 1.1 mycroft return (EINVAL);
1000 1.84 perseant } else /* NULL or invalid */
1001 1.84 perseant atv.tv_sec = atv.tv_usec = 0;
1002 1.84 perseant return lfs_segwait(&fsid, &atv);
1003 1.1 mycroft }
1004 1.1 mycroft
1005 1.1 mycroft /*
1006 1.1 mycroft * VFS_VGET call specialized for the cleaner. The cleaner already knows the
1007 1.1 mycroft * daddr from the ifile, so don't look it up again. If the cleaner is
1008 1.1 mycroft * processing IINFO structures, it may have the ondisk inode already, so
1009 1.1 mycroft * don't go retrieving it again.
1010 1.22 perseant *
1011 1.74 yamt * we lfs_vref, and it is the caller's responsibility to lfs_vunref
1012 1.74 yamt * when finished.
1013 1.1 mycroft */
1014 1.22 perseant extern struct lock ufs_hashlock;
1015 1.22 perseant
1016 1.1 mycroft int
1017 1.74 yamt lfs_fasthashget(dev_t dev, ino_t ino, struct vnode **vpp)
1018 1.44 fvdl {
1019 1.44 fvdl
1020 1.44 fvdl /*
1021 1.44 fvdl * This is playing fast and loose. Someone may have the inode
1022 1.44 fvdl * locked, in which case they are going to be distinctly unhappy
1023 1.44 fvdl * if we trash something.
1024 1.44 fvdl */
1025 1.44 fvdl if ((*vpp = ufs_ihashlookup(dev, ino)) != NULL) {
1026 1.44 fvdl if ((*vpp)->v_flag & VXLOCK) {
1027 1.46 perseant printf("lfs_fastvget: vnode VXLOCKed for ino %d\n",
1028 1.46 perseant ino);
1029 1.44 fvdl clean_vnlocked++;
1030 1.44 fvdl #ifdef LFS_EAGAIN_FAIL
1031 1.44 fvdl return EAGAIN;
1032 1.44 fvdl #endif
1033 1.44 fvdl }
1034 1.44 fvdl if (lfs_vref(*vpp)) {
1035 1.44 fvdl clean_inlocked++;
1036 1.44 fvdl return EAGAIN;
1037 1.44 fvdl }
1038 1.44 fvdl } else
1039 1.44 fvdl *vpp = NULL;
1040 1.44 fvdl
1041 1.44 fvdl return (0);
1042 1.44 fvdl }
1043 1.44 fvdl
1044 1.44 fvdl int
1045 1.89 fvdl lfs_fastvget(struct mount *mp, ino_t ino, daddr_t daddr, struct vnode **vpp, struct ufs1_dinode *dinp)
1046 1.1 mycroft {
1047 1.41 augustss struct inode *ip;
1048 1.89 fvdl struct ufs1_dinode *dip;
1049 1.1 mycroft struct vnode *vp;
1050 1.1 mycroft struct ufsmount *ump;
1051 1.1 mycroft dev_t dev;
1052 1.88 yamt int error, retries;
1053 1.22 perseant struct buf *bp;
1054 1.57 perseant struct lfs *fs;
1055 1.22 perseant
1056 1.1 mycroft ump = VFSTOUFS(mp);
1057 1.1 mycroft dev = ump->um_dev;
1058 1.57 perseant fs = ump->um_lfs;
1059 1.54 perseant
1060 1.54 perseant /*
1061 1.54 perseant * Wait until the filesystem is fully mounted before allowing vget
1062 1.82 perseant * to complete. This prevents possible problems with roll-forward.
1063 1.54 perseant */
1064 1.62 chs while (fs->lfs_flags & LFS_NOTYET) {
1065 1.57 perseant tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_fnotyet", 0);
1066 1.54 perseant }
1067 1.54 perseant /*
1068 1.54 perseant * This is playing fast and loose. Someone may have the inode
1069 1.54 perseant * locked, in which case they are going to be distinctly unhappy
1070 1.54 perseant * if we trash something.
1071 1.54 perseant */
1072 1.44 fvdl
1073 1.74 yamt error = lfs_fasthashget(dev, ino, vpp);
1074 1.44 fvdl if (error != 0 || *vpp != NULL)
1075 1.44 fvdl return (error);
1076 1.44 fvdl
1077 1.45 fvdl if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
1078 1.44 fvdl *vpp = NULL;
1079 1.44 fvdl return (error);
1080 1.44 fvdl }
1081 1.44 fvdl
1082 1.22 perseant do {
1083 1.74 yamt error = lfs_fasthashget(dev, ino, vpp);
1084 1.44 fvdl if (error != 0 || *vpp != NULL) {
1085 1.44 fvdl ungetnewvnode(vp);
1086 1.44 fvdl return (error);
1087 1.22 perseant }
1088 1.22 perseant } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1089 1.1 mycroft
1090 1.1 mycroft /* Allocate new vnode/inode. */
1091 1.44 fvdl lfs_vcreate(mp, ino, vp);
1092 1.44 fvdl
1093 1.1 mycroft /*
1094 1.1 mycroft * Put it onto its hash chain and lock it so that other requests for
1095 1.1 mycroft * this inode will block if they arrive while we are sleeping waiting
1096 1.1 mycroft * for old data structures to be purged or for the contents of the
1097 1.1 mycroft * disk portion of this inode to be read.
1098 1.1 mycroft */
1099 1.1 mycroft ip = VTOI(vp);
1100 1.1 mycroft ufs_ihashins(ip);
1101 1.22 perseant lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1102 1.22 perseant
1103 1.1 mycroft /*
1104 1.1 mycroft * XXX
1105 1.1 mycroft * This may not need to be here, logically it should go down with
1106 1.1 mycroft * the i_devvp initialization.
1107 1.1 mycroft * Ask Kirk.
1108 1.1 mycroft */
1109 1.57 perseant ip->i_lfs = fs;
1110 1.1 mycroft
1111 1.1 mycroft /* Read in the disk contents for the inode, copy into the inode. */
1112 1.10 christos if (dinp) {
1113 1.89 fvdl error = copyin(dinp, ip->i_din.ffs1_din, sizeof (struct ufs1_dinode));
1114 1.22 perseant if (error) {
1115 1.24 perseant printf("lfs_fastvget: dinode copyin failed for ino %d\n", ino);
1116 1.22 perseant ufs_ihashrem(ip);
1117 1.22 perseant
1118 1.22 perseant /* Unlock and discard unneeded inode. */
1119 1.33 wrstuden lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
1120 1.22 perseant lfs_vunref(vp);
1121 1.22 perseant *vpp = NULL;
1122 1.1 mycroft return (error);
1123 1.22 perseant }
1124 1.62 chs if (ip->i_number != ino)
1125 1.22 perseant panic("lfs_fastvget: I was fed the wrong inode!");
1126 1.22 perseant } else {
1127 1.65 perseant retries = 0;
1128 1.65 perseant again:
1129 1.57 perseant error = bread(ump->um_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
1130 1.57 perseant NOCRED, &bp);
1131 1.10 christos if (error) {
1132 1.29 perseant printf("lfs_fastvget: bread failed with %d\n",error);
1133 1.1 mycroft /*
1134 1.1 mycroft * The inode does not contain anything useful, so it
1135 1.1 mycroft * would be misleading to leave it on its hash chain.
1136 1.1 mycroft * Iput() will return it to the free list.
1137 1.1 mycroft */
1138 1.1 mycroft ufs_ihashrem(ip);
1139 1.22 perseant
1140 1.1 mycroft /* Unlock and discard unneeded inode. */
1141 1.33 wrstuden lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
1142 1.1 mycroft lfs_vunref(vp);
1143 1.1 mycroft brelse(bp);
1144 1.1 mycroft *vpp = NULL;
1145 1.1 mycroft return (error);
1146 1.1 mycroft }
1147 1.65 perseant dip = lfs_ifind(ump->um_lfs, ino, bp);
1148 1.65 perseant if (dip == NULL) {
1149 1.65 perseant /* Assume write has not completed yet; try again */
1150 1.65 perseant bp->b_flags |= B_INVAL;
1151 1.65 perseant brelse(bp);
1152 1.65 perseant ++retries;
1153 1.65 perseant if (retries > LFS_IFIND_RETRIES)
1154 1.65 perseant panic("lfs_fastvget: dinode not found");
1155 1.65 perseant printf("lfs_fastvget: dinode not found, retrying...\n");
1156 1.65 perseant goto again;
1157 1.65 perseant }
1158 1.89 fvdl *ip->i_din.ffs1_din = *dip;
1159 1.1 mycroft brelse(bp);
1160 1.1 mycroft }
1161 1.99 yamt lfs_vinit(mp, &vp);
1162 1.63 chs
1163 1.1 mycroft *vpp = vp;
1164 1.88 yamt
1165 1.74 yamt KASSERT(VOP_ISLOCKED(vp));
1166 1.74 yamt VOP_UNLOCK(vp, 0);
1167 1.22 perseant
1168 1.1 mycroft return (0);
1169 1.1 mycroft }
1170 1.22 perseant
1171 1.85 perseant /*
1172 1.85 perseant * Make up a "fake" cleaner buffer, copy the data from userland into it.
1173 1.85 perseant */
1174 1.1 mycroft struct buf *
1175 1.65 perseant lfs_fakebuf(struct lfs *fs, struct vnode *vp, int lbn, size_t size, caddr_t uaddr)
1176 1.1 mycroft {
1177 1.1 mycroft struct buf *bp;
1178 1.25 perseant int error;
1179 1.75 yamt
1180 1.75 yamt KASSERT(VTOI(vp)->i_number != LFS_IFILE_INUM);
1181 1.73 yamt
1182 1.80 perseant bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size, LFS_NB_CLEAN);
1183 1.25 perseant error = copyin(uaddr, bp->b_data, size);
1184 1.62 chs if (error) {
1185 1.80 perseant lfs_freebuf(fs, bp);
1186 1.25 perseant return NULL;
1187 1.22 perseant }
1188 1.73 yamt KDASSERT(bp->b_iodone == lfs_callback);
1189 1.73 yamt
1190 1.65 perseant #if 0
1191 1.65 perseant bp->b_saveaddr = (caddr_t)fs;
1192 1.65 perseant ++fs->lfs_iocount;
1193 1.65 perseant #endif
1194 1.1 mycroft bp->b_bufsize = size;
1195 1.1 mycroft bp->b_bcount = size;
1196 1.1 mycroft return (bp);
1197 1.1 mycroft }
1198