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