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