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