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