ulfs_lookup.c revision 1.19.4.2 1 1.19.4.2 yamt /* $NetBSD: ulfs_lookup.c,v 1.19.4.2 2014/05/22 11:41:19 yamt Exp $ */
2 1.19.4.2 yamt /* from NetBSD: ufs_lookup.c,v 1.122 2013/01/22 09:39:18 dholland Exp */
3 1.19.4.2 yamt
4 1.19.4.2 yamt /*
5 1.19.4.2 yamt * Copyright (c) 1989, 1993
6 1.19.4.2 yamt * The Regents of the University of California. All rights reserved.
7 1.19.4.2 yamt * (c) UNIX System Laboratories, Inc.
8 1.19.4.2 yamt * All or some portions of this file are derived from material licensed
9 1.19.4.2 yamt * to the University of California by American Telephone and Telegraph
10 1.19.4.2 yamt * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 1.19.4.2 yamt * the permission of UNIX System Laboratories, Inc.
12 1.19.4.2 yamt *
13 1.19.4.2 yamt * Redistribution and use in source and binary forms, with or without
14 1.19.4.2 yamt * modification, are permitted provided that the following conditions
15 1.19.4.2 yamt * are met:
16 1.19.4.2 yamt * 1. Redistributions of source code must retain the above copyright
17 1.19.4.2 yamt * notice, this list of conditions and the following disclaimer.
18 1.19.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
19 1.19.4.2 yamt * notice, this list of conditions and the following disclaimer in the
20 1.19.4.2 yamt * documentation and/or other materials provided with the distribution.
21 1.19.4.2 yamt * 3. Neither the name of the University nor the names of its contributors
22 1.19.4.2 yamt * may be used to endorse or promote products derived from this software
23 1.19.4.2 yamt * without specific prior written permission.
24 1.19.4.2 yamt *
25 1.19.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.19.4.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.19.4.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.19.4.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.19.4.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.19.4.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.19.4.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.19.4.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.19.4.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.19.4.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.19.4.2 yamt * SUCH DAMAGE.
36 1.19.4.2 yamt *
37 1.19.4.2 yamt * @(#)ufs_lookup.c 8.9 (Berkeley) 8/11/94
38 1.19.4.2 yamt */
39 1.19.4.2 yamt
40 1.19.4.2 yamt #include <sys/cdefs.h>
41 1.19.4.2 yamt __KERNEL_RCSID(0, "$NetBSD: ulfs_lookup.c,v 1.19.4.2 2014/05/22 11:41:19 yamt Exp $");
42 1.19.4.2 yamt
43 1.19.4.2 yamt #ifdef _KERNEL_OPT
44 1.19.4.2 yamt #include "opt_lfs.h"
45 1.19.4.2 yamt #endif
46 1.19.4.2 yamt
47 1.19.4.2 yamt #include <sys/param.h>
48 1.19.4.2 yamt #include <sys/systm.h>
49 1.19.4.2 yamt #include <sys/namei.h>
50 1.19.4.2 yamt #include <sys/buf.h>
51 1.19.4.2 yamt #include <sys/file.h>
52 1.19.4.2 yamt #include <sys/stat.h>
53 1.19.4.2 yamt #include <sys/mount.h>
54 1.19.4.2 yamt #include <sys/vnode.h>
55 1.19.4.2 yamt #include <sys/kernel.h>
56 1.19.4.2 yamt #include <sys/kauth.h>
57 1.19.4.2 yamt #include <sys/wapbl.h>
58 1.19.4.2 yamt #include <sys/fstrans.h>
59 1.19.4.2 yamt #include <sys/proc.h>
60 1.19.4.2 yamt #include <sys/kmem.h>
61 1.19.4.2 yamt
62 1.19.4.2 yamt #include <ufs/lfs/lfs_extern.h>
63 1.19.4.2 yamt
64 1.19.4.2 yamt #include <ufs/lfs/ulfs_inode.h>
65 1.19.4.2 yamt #ifdef LFS_DIRHASH
66 1.19.4.2 yamt #include <ufs/lfs/ulfs_dirhash.h>
67 1.19.4.2 yamt #endif
68 1.19.4.2 yamt #include <ufs/lfs/ulfsmount.h>
69 1.19.4.2 yamt #include <ufs/lfs/ulfs_extern.h>
70 1.19.4.2 yamt #include <ufs/lfs/ulfs_bswap.h>
71 1.19.4.2 yamt
72 1.19.4.2 yamt #include <miscfs/genfs/genfs.h>
73 1.19.4.2 yamt
74 1.19.4.2 yamt #ifdef DIAGNOSTIC
75 1.19.4.2 yamt int lfs_dirchk = 1;
76 1.19.4.2 yamt #else
77 1.19.4.2 yamt int lfs_dirchk = 0;
78 1.19.4.2 yamt #endif
79 1.19.4.2 yamt
80 1.19.4.2 yamt /*
81 1.19.4.2 yamt * Convert a component of a pathname into a pointer to a locked inode.
82 1.19.4.2 yamt * This is a very central and rather complicated routine.
83 1.19.4.2 yamt * If the file system is not maintained in a strict tree hierarchy,
84 1.19.4.2 yamt * this can result in a deadlock situation (see comments in code below).
85 1.19.4.2 yamt *
86 1.19.4.2 yamt * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
87 1.19.4.2 yamt * on whether the name is to be looked up, created, renamed, or deleted.
88 1.19.4.2 yamt * When CREATE, RENAME, or DELETE is specified, information usable in
89 1.19.4.2 yamt * creating, renaming, or deleting a directory entry may be calculated.
90 1.19.4.2 yamt * If flag has LOCKPARENT or'ed into it and the target of the pathname
91 1.19.4.2 yamt * exists, lookup returns both the target and its parent directory locked.
92 1.19.4.2 yamt * When creating or renaming and LOCKPARENT is specified, the target may
93 1.19.4.2 yamt * not be ".". When deleting and LOCKPARENT is specified, the target may
94 1.19.4.2 yamt * be "."., but the caller must check to ensure it does an vrele and vput
95 1.19.4.2 yamt * instead of two vputs.
96 1.19.4.2 yamt *
97 1.19.4.2 yamt * Overall outline of ulfs_lookup:
98 1.19.4.2 yamt *
99 1.19.4.2 yamt * check accessibility of directory
100 1.19.4.2 yamt * look for name in cache, if found, then if at end of path
101 1.19.4.2 yamt * and deleting or creating, drop it, else return name
102 1.19.4.2 yamt * search for name in directory, to found or notfound
103 1.19.4.2 yamt * notfound:
104 1.19.4.2 yamt * if creating, return locked directory, leaving info on available slots
105 1.19.4.2 yamt * else return error
106 1.19.4.2 yamt * found:
107 1.19.4.2 yamt * if at end of path and deleting, return information to allow delete
108 1.19.4.2 yamt * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
109 1.19.4.2 yamt * inode and return info to allow rewrite
110 1.19.4.2 yamt * if not at end, add name to cache; if at end and neither creating
111 1.19.4.2 yamt * nor deleting, add name to cache
112 1.19.4.2 yamt */
113 1.19.4.2 yamt int
114 1.19.4.2 yamt ulfs_lookup(void *v)
115 1.19.4.2 yamt {
116 1.19.4.2 yamt struct vop_lookup_v2_args /* {
117 1.19.4.2 yamt struct vnode *a_dvp;
118 1.19.4.2 yamt struct vnode **a_vpp;
119 1.19.4.2 yamt struct componentname *a_cnp;
120 1.19.4.2 yamt } */ *ap = v;
121 1.19.4.2 yamt struct vnode *vdp = ap->a_dvp; /* vnode for directory being searched */
122 1.19.4.2 yamt struct inode *dp = VTOI(vdp); /* inode for directory being searched */
123 1.19.4.2 yamt struct buf *bp; /* a buffer of directory entries */
124 1.19.4.2 yamt struct lfs_direct *ep; /* the current directory entry */
125 1.19.4.2 yamt int entryoffsetinblock; /* offset of ep in bp's buffer */
126 1.19.4.2 yamt enum {
127 1.19.4.2 yamt NONE, /* need to search a slot for our new entry */
128 1.19.4.2 yamt COMPACT, /* a compaction can make a slot in the current
129 1.19.4.2 yamt DIRBLKSIZ block */
130 1.19.4.2 yamt FOUND, /* found a slot (or no need to search) */
131 1.19.4.2 yamt } slotstatus;
132 1.19.4.2 yamt doff_t slotoffset; /* offset of area with free space.
133 1.19.4.2 yamt a special value -1 for invalid */
134 1.19.4.2 yamt int slotsize; /* size of area at slotoffset */
135 1.19.4.2 yamt int slotfreespace; /* accumulated amount of space free in
136 1.19.4.2 yamt the current DIRBLKSIZ block */
137 1.19.4.2 yamt int slotneeded; /* size of the entry we're seeking */
138 1.19.4.2 yamt int numdirpasses; /* strategy for directory search */
139 1.19.4.2 yamt doff_t endsearch; /* offset to end directory search */
140 1.19.4.2 yamt doff_t prevoff; /* previous value of ulr_offset */
141 1.19.4.2 yamt struct vnode *pdp; /* saved dp during symlink work */
142 1.19.4.2 yamt struct vnode *tdp; /* returned by VFS_VGET */
143 1.19.4.2 yamt doff_t enduseful; /* pointer past last used dir slot.
144 1.19.4.2 yamt used for directory truncation. */
145 1.19.4.2 yamt u_long bmask; /* block offset mask */
146 1.19.4.2 yamt int error;
147 1.19.4.2 yamt struct vnode **vpp = ap->a_vpp;
148 1.19.4.2 yamt struct componentname *cnp = ap->a_cnp;
149 1.19.4.2 yamt kauth_cred_t cred = cnp->cn_cred;
150 1.19.4.2 yamt int flags;
151 1.19.4.2 yamt int nameiop = cnp->cn_nameiop;
152 1.19.4.2 yamt struct lfs *fs = dp->i_lfs;
153 1.19.4.2 yamt const int needswap = ULFS_MPNEEDSWAP(fs);
154 1.19.4.2 yamt int dirblksiz = fs->um_dirblksiz;
155 1.19.4.2 yamt ino_t foundino;
156 1.19.4.2 yamt struct ulfs_lookup_results *results;
157 1.19.4.2 yamt int iswhiteout; /* temp result from cache_lookup() */
158 1.19.4.2 yamt
159 1.19.4.2 yamt flags = cnp->cn_flags;
160 1.19.4.2 yamt
161 1.19.4.2 yamt bp = NULL;
162 1.19.4.2 yamt slotoffset = -1;
163 1.19.4.2 yamt *vpp = NULL;
164 1.19.4.2 yamt endsearch = 0; /* silence compiler warning */
165 1.19.4.2 yamt
166 1.19.4.2 yamt /*
167 1.19.4.2 yamt * Produce the auxiliary lookup results into i_crap. Increment
168 1.19.4.2 yamt * its serial number so elsewhere we can tell if we're using
169 1.19.4.2 yamt * stale results. This should not be done this way. XXX.
170 1.19.4.2 yamt */
171 1.19.4.2 yamt results = &dp->i_crap;
172 1.19.4.2 yamt dp->i_crapcounter++;
173 1.19.4.2 yamt
174 1.19.4.2 yamt /*
175 1.19.4.2 yamt * Check accessiblity of directory.
176 1.19.4.2 yamt */
177 1.19.4.2 yamt if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
178 1.19.4.2 yamt return (error);
179 1.19.4.2 yamt
180 1.19.4.2 yamt if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
181 1.19.4.2 yamt (nameiop == DELETE || nameiop == RENAME))
182 1.19.4.2 yamt return (EROFS);
183 1.19.4.2 yamt
184 1.19.4.2 yamt /*
185 1.19.4.2 yamt * We now have a segment name to search for, and a directory to search.
186 1.19.4.2 yamt *
187 1.19.4.2 yamt * Before tediously performing a linear scan of the directory,
188 1.19.4.2 yamt * check the name cache to see if the directory/name pair
189 1.19.4.2 yamt * we are looking for is known already.
190 1.19.4.2 yamt */
191 1.19.4.2 yamt if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
192 1.19.4.2 yamt cnp->cn_nameiop, cnp->cn_flags, &iswhiteout, vpp)) {
193 1.19.4.2 yamt if (iswhiteout) {
194 1.19.4.2 yamt cnp->cn_flags |= ISWHITEOUT;
195 1.19.4.2 yamt }
196 1.19.4.2 yamt return *vpp == NULLVP ? ENOENT : 0;
197 1.19.4.2 yamt }
198 1.19.4.2 yamt if (iswhiteout) {
199 1.19.4.2 yamt /*
200 1.19.4.2 yamt * The namecache set iswhiteout without finding a
201 1.19.4.2 yamt * cache entry. As of this writing (20121014), this
202 1.19.4.2 yamt * can happen if there was a whiteout entry that has
203 1.19.4.2 yamt * been invalidated by the lookup. It is not clear if
204 1.19.4.2 yamt * it is correct to set ISWHITEOUT in this case or
205 1.19.4.2 yamt * not; however, doing so retains the prior behavior,
206 1.19.4.2 yamt * so we'll go with that until some clearer answer
207 1.19.4.2 yamt * appears. XXX
208 1.19.4.2 yamt */
209 1.19.4.2 yamt cnp->cn_flags |= ISWHITEOUT;
210 1.19.4.2 yamt }
211 1.19.4.2 yamt
212 1.19.4.2 yamt fstrans_start(vdp->v_mount, FSTRANS_SHARED);
213 1.19.4.2 yamt
214 1.19.4.2 yamt /*
215 1.19.4.2 yamt * Suppress search for slots unless creating
216 1.19.4.2 yamt * file and at end of pathname, in which case
217 1.19.4.2 yamt * we watch for a place to put the new file in
218 1.19.4.2 yamt * case it doesn't already exist.
219 1.19.4.2 yamt */
220 1.19.4.2 yamt slotstatus = FOUND;
221 1.19.4.2 yamt slotfreespace = slotsize = slotneeded = 0;
222 1.19.4.2 yamt if ((nameiop == CREATE || nameiop == RENAME) && (flags & ISLASTCN)) {
223 1.19.4.2 yamt slotstatus = NONE;
224 1.19.4.2 yamt slotneeded = LFS_DIRECTSIZ(cnp->cn_namelen);
225 1.19.4.2 yamt }
226 1.19.4.2 yamt
227 1.19.4.2 yamt /*
228 1.19.4.2 yamt * If there is cached information on a previous search of
229 1.19.4.2 yamt * this directory, pick up where we last left off.
230 1.19.4.2 yamt * We cache only lookups as these are the most common
231 1.19.4.2 yamt * and have the greatest payoff. Caching CREATE has little
232 1.19.4.2 yamt * benefit as it usually must search the entire directory
233 1.19.4.2 yamt * to determine that the entry does not exist. Caching the
234 1.19.4.2 yamt * location of the last DELETE or RENAME has not reduced
235 1.19.4.2 yamt * profiling time and hence has been removed in the interest
236 1.19.4.2 yamt * of simplicity.
237 1.19.4.2 yamt */
238 1.19.4.2 yamt bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
239 1.19.4.2 yamt
240 1.19.4.2 yamt #ifdef LFS_DIRHASH
241 1.19.4.2 yamt /*
242 1.19.4.2 yamt * Use dirhash for fast operations on large directories. The logic
243 1.19.4.2 yamt * to determine whether to hash the directory is contained within
244 1.19.4.2 yamt * ulfsdirhash_build(); a zero return means that it decided to hash
245 1.19.4.2 yamt * this directory and it successfully built up the hash table.
246 1.19.4.2 yamt */
247 1.19.4.2 yamt if (ulfsdirhash_build(dp) == 0) {
248 1.19.4.2 yamt /* Look for a free slot if needed. */
249 1.19.4.2 yamt enduseful = dp->i_size;
250 1.19.4.2 yamt if (slotstatus != FOUND) {
251 1.19.4.2 yamt slotoffset = ulfsdirhash_findfree(dp, slotneeded,
252 1.19.4.2 yamt &slotsize);
253 1.19.4.2 yamt if (slotoffset >= 0) {
254 1.19.4.2 yamt slotstatus = COMPACT;
255 1.19.4.2 yamt enduseful = ulfsdirhash_enduseful(dp);
256 1.19.4.2 yamt if (enduseful < 0)
257 1.19.4.2 yamt enduseful = dp->i_size;
258 1.19.4.2 yamt }
259 1.19.4.2 yamt }
260 1.19.4.2 yamt /* Look up the component. */
261 1.19.4.2 yamt numdirpasses = 1;
262 1.19.4.2 yamt entryoffsetinblock = 0; /* silence compiler warning */
263 1.19.4.2 yamt switch (ulfsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
264 1.19.4.2 yamt &results->ulr_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
265 1.19.4.2 yamt case 0:
266 1.19.4.2 yamt ep = (struct lfs_direct *)((char *)bp->b_data +
267 1.19.4.2 yamt (results->ulr_offset & bmask));
268 1.19.4.2 yamt goto foundentry;
269 1.19.4.2 yamt case ENOENT:
270 1.19.4.2 yamt results->ulr_offset = roundup(dp->i_size, dirblksiz);
271 1.19.4.2 yamt goto notfound;
272 1.19.4.2 yamt default:
273 1.19.4.2 yamt /* Something failed; just do a linear search. */
274 1.19.4.2 yamt break;
275 1.19.4.2 yamt }
276 1.19.4.2 yamt }
277 1.19.4.2 yamt #endif /* LFS_DIRHASH */
278 1.19.4.2 yamt
279 1.19.4.2 yamt if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
280 1.19.4.2 yamt results->ulr_diroff >= dp->i_size) {
281 1.19.4.2 yamt entryoffsetinblock = 0;
282 1.19.4.2 yamt results->ulr_offset = 0;
283 1.19.4.2 yamt numdirpasses = 1;
284 1.19.4.2 yamt } else {
285 1.19.4.2 yamt results->ulr_offset = results->ulr_diroff;
286 1.19.4.2 yamt if ((entryoffsetinblock = results->ulr_offset & bmask) &&
287 1.19.4.2 yamt (error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
288 1.19.4.2 yamt NULL, &bp, false)))
289 1.19.4.2 yamt goto out;
290 1.19.4.2 yamt numdirpasses = 2;
291 1.19.4.2 yamt nchstats.ncs_2passes++;
292 1.19.4.2 yamt }
293 1.19.4.2 yamt prevoff = results->ulr_offset;
294 1.19.4.2 yamt endsearch = roundup(dp->i_size, dirblksiz);
295 1.19.4.2 yamt enduseful = 0;
296 1.19.4.2 yamt
297 1.19.4.2 yamt searchloop:
298 1.19.4.2 yamt while (results->ulr_offset < endsearch) {
299 1.19.4.2 yamt if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
300 1.19.4.2 yamt preempt();
301 1.19.4.2 yamt /*
302 1.19.4.2 yamt * If necessary, get the next directory block.
303 1.19.4.2 yamt */
304 1.19.4.2 yamt if ((results->ulr_offset & bmask) == 0) {
305 1.19.4.2 yamt if (bp != NULL)
306 1.19.4.2 yamt brelse(bp, 0);
307 1.19.4.2 yamt error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
308 1.19.4.2 yamt NULL, &bp, false);
309 1.19.4.2 yamt if (error)
310 1.19.4.2 yamt goto out;
311 1.19.4.2 yamt entryoffsetinblock = 0;
312 1.19.4.2 yamt }
313 1.19.4.2 yamt /*
314 1.19.4.2 yamt * If still looking for a slot, and at a DIRBLKSIZ
315 1.19.4.2 yamt * boundary, have to start looking for free space again.
316 1.19.4.2 yamt */
317 1.19.4.2 yamt if (slotstatus == NONE &&
318 1.19.4.2 yamt (entryoffsetinblock & (dirblksiz - 1)) == 0) {
319 1.19.4.2 yamt slotoffset = -1;
320 1.19.4.2 yamt slotfreespace = 0;
321 1.19.4.2 yamt }
322 1.19.4.2 yamt /*
323 1.19.4.2 yamt * Get pointer to next entry.
324 1.19.4.2 yamt * Full validation checks are slow, so we only check
325 1.19.4.2 yamt * enough to insure forward progress through the
326 1.19.4.2 yamt * directory. Complete checks can be run by patching
327 1.19.4.2 yamt * "lfs_dirchk" to be true.
328 1.19.4.2 yamt */
329 1.19.4.2 yamt KASSERT(bp != NULL);
330 1.19.4.2 yamt ep = (struct lfs_direct *)((char *)bp->b_data + entryoffsetinblock);
331 1.19.4.2 yamt if (ep->d_reclen == 0 ||
332 1.19.4.2 yamt (lfs_dirchk && ulfs_dirbadentry(vdp, ep, entryoffsetinblock))) {
333 1.19.4.2 yamt int i;
334 1.19.4.2 yamt
335 1.19.4.2 yamt ulfs_dirbad(dp, results->ulr_offset, "mangled entry");
336 1.19.4.2 yamt i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
337 1.19.4.2 yamt results->ulr_offset += i;
338 1.19.4.2 yamt entryoffsetinblock += i;
339 1.19.4.2 yamt continue;
340 1.19.4.2 yamt }
341 1.19.4.2 yamt
342 1.19.4.2 yamt /*
343 1.19.4.2 yamt * If an appropriate sized slot has not yet been found,
344 1.19.4.2 yamt * check to see if one is available. Also accumulate space
345 1.19.4.2 yamt * in the current block so that we can determine if
346 1.19.4.2 yamt * compaction is viable.
347 1.19.4.2 yamt */
348 1.19.4.2 yamt if (slotstatus != FOUND) {
349 1.19.4.2 yamt int size = ulfs_rw16(ep->d_reclen, needswap);
350 1.19.4.2 yamt
351 1.19.4.2 yamt if (ep->d_ino != 0)
352 1.19.4.2 yamt size -= LFS_DIRSIZ(FSFMT(vdp), ep, needswap);
353 1.19.4.2 yamt if (size > 0) {
354 1.19.4.2 yamt if (size >= slotneeded) {
355 1.19.4.2 yamt slotstatus = FOUND;
356 1.19.4.2 yamt slotoffset = results->ulr_offset;
357 1.19.4.2 yamt slotsize = ulfs_rw16(ep->d_reclen,
358 1.19.4.2 yamt needswap);
359 1.19.4.2 yamt } else if (slotstatus == NONE) {
360 1.19.4.2 yamt slotfreespace += size;
361 1.19.4.2 yamt if (slotoffset == -1)
362 1.19.4.2 yamt slotoffset = results->ulr_offset;
363 1.19.4.2 yamt if (slotfreespace >= slotneeded) {
364 1.19.4.2 yamt slotstatus = COMPACT;
365 1.19.4.2 yamt slotsize = results->ulr_offset +
366 1.19.4.2 yamt ulfs_rw16(ep->d_reclen,
367 1.19.4.2 yamt needswap) -
368 1.19.4.2 yamt slotoffset;
369 1.19.4.2 yamt }
370 1.19.4.2 yamt }
371 1.19.4.2 yamt }
372 1.19.4.2 yamt }
373 1.19.4.2 yamt
374 1.19.4.2 yamt /*
375 1.19.4.2 yamt * Check for a name match.
376 1.19.4.2 yamt */
377 1.19.4.2 yamt if (ep->d_ino) {
378 1.19.4.2 yamt int namlen;
379 1.19.4.2 yamt
380 1.19.4.2 yamt #if (BYTE_ORDER == LITTLE_ENDIAN)
381 1.19.4.2 yamt if (FSFMT(vdp) && needswap == 0)
382 1.19.4.2 yamt namlen = ep->d_type;
383 1.19.4.2 yamt else
384 1.19.4.2 yamt namlen = ep->d_namlen;
385 1.19.4.2 yamt #else
386 1.19.4.2 yamt if (FSFMT(vdp) && needswap != 0)
387 1.19.4.2 yamt namlen = ep->d_type;
388 1.19.4.2 yamt else
389 1.19.4.2 yamt namlen = ep->d_namlen;
390 1.19.4.2 yamt #endif
391 1.19.4.2 yamt if (namlen == cnp->cn_namelen &&
392 1.19.4.2 yamt !memcmp(cnp->cn_nameptr, ep->d_name,
393 1.19.4.2 yamt (unsigned)namlen)) {
394 1.19.4.2 yamt #ifdef LFS_DIRHASH
395 1.19.4.2 yamt foundentry:
396 1.19.4.2 yamt #endif
397 1.19.4.2 yamt /*
398 1.19.4.2 yamt * Save directory entry's inode number and
399 1.19.4.2 yamt * reclen, and release directory buffer.
400 1.19.4.2 yamt */
401 1.19.4.2 yamt if (!FSFMT(vdp) && ep->d_type == LFS_DT_WHT) {
402 1.19.4.2 yamt slotstatus = FOUND;
403 1.19.4.2 yamt slotoffset = results->ulr_offset;
404 1.19.4.2 yamt slotsize = ulfs_rw16(ep->d_reclen,
405 1.19.4.2 yamt needswap);
406 1.19.4.2 yamt results->ulr_reclen = slotsize;
407 1.19.4.2 yamt /*
408 1.19.4.2 yamt * This is used to set
409 1.19.4.2 yamt * results->ulr_endoff,
410 1.19.4.2 yamt * which may be used by ulfs_direnter()
411 1.19.4.2 yamt * as a length to truncate the
412 1.19.4.2 yamt * directory to. Therefore, it must
413 1.19.4.2 yamt * point past the end of the last
414 1.19.4.2 yamt * non-empty directory entry. We don't
415 1.19.4.2 yamt * know where that is in this case, so
416 1.19.4.2 yamt * we effectively disable shrinking by
417 1.19.4.2 yamt * using the existing size of the
418 1.19.4.2 yamt * directory.
419 1.19.4.2 yamt *
420 1.19.4.2 yamt * Note that we wouldn't expect to
421 1.19.4.2 yamt * shrink the directory while rewriting
422 1.19.4.2 yamt * an existing entry anyway.
423 1.19.4.2 yamt */
424 1.19.4.2 yamt enduseful = endsearch;
425 1.19.4.2 yamt cnp->cn_flags |= ISWHITEOUT;
426 1.19.4.2 yamt numdirpasses--;
427 1.19.4.2 yamt goto notfound;
428 1.19.4.2 yamt }
429 1.19.4.2 yamt foundino = ulfs_rw32(ep->d_ino, needswap);
430 1.19.4.2 yamt results->ulr_reclen =
431 1.19.4.2 yamt ulfs_rw16(ep->d_reclen, needswap);
432 1.19.4.2 yamt goto found;
433 1.19.4.2 yamt }
434 1.19.4.2 yamt }
435 1.19.4.2 yamt prevoff = results->ulr_offset;
436 1.19.4.2 yamt results->ulr_offset += ulfs_rw16(ep->d_reclen, needswap);
437 1.19.4.2 yamt entryoffsetinblock += ulfs_rw16(ep->d_reclen, needswap);
438 1.19.4.2 yamt if (ep->d_ino)
439 1.19.4.2 yamt enduseful = results->ulr_offset;
440 1.19.4.2 yamt }
441 1.19.4.2 yamt notfound:
442 1.19.4.2 yamt /*
443 1.19.4.2 yamt * If we started in the middle of the directory and failed
444 1.19.4.2 yamt * to find our target, we must check the beginning as well.
445 1.19.4.2 yamt */
446 1.19.4.2 yamt if (numdirpasses == 2) {
447 1.19.4.2 yamt numdirpasses--;
448 1.19.4.2 yamt results->ulr_offset = 0;
449 1.19.4.2 yamt endsearch = results->ulr_diroff;
450 1.19.4.2 yamt goto searchloop;
451 1.19.4.2 yamt }
452 1.19.4.2 yamt if (bp != NULL)
453 1.19.4.2 yamt brelse(bp, 0);
454 1.19.4.2 yamt /*
455 1.19.4.2 yamt * If creating, and at end of pathname and current
456 1.19.4.2 yamt * directory has not been removed, then can consider
457 1.19.4.2 yamt * allowing file to be created.
458 1.19.4.2 yamt */
459 1.19.4.2 yamt if ((nameiop == CREATE || nameiop == RENAME ||
460 1.19.4.2 yamt (nameiop == DELETE &&
461 1.19.4.2 yamt (cnp->cn_flags & DOWHITEOUT) &&
462 1.19.4.2 yamt (cnp->cn_flags & ISWHITEOUT))) &&
463 1.19.4.2 yamt (flags & ISLASTCN) && dp->i_nlink != 0) {
464 1.19.4.2 yamt /*
465 1.19.4.2 yamt * Access for write is interpreted as allowing
466 1.19.4.2 yamt * creation of files in the directory.
467 1.19.4.2 yamt */
468 1.19.4.2 yamt error = VOP_ACCESS(vdp, VWRITE, cred);
469 1.19.4.2 yamt if (error)
470 1.19.4.2 yamt goto out;
471 1.19.4.2 yamt /*
472 1.19.4.2 yamt * Return an indication of where the new directory
473 1.19.4.2 yamt * entry should be put. If we didn't find a slot,
474 1.19.4.2 yamt * then set results->ulr_count to 0 indicating
475 1.19.4.2 yamt * that the new slot belongs at the end of the
476 1.19.4.2 yamt * directory. If we found a slot, then the new entry
477 1.19.4.2 yamt * can be put in the range from results->ulr_offset to
478 1.19.4.2 yamt * results->ulr_offset + results->ulr_count.
479 1.19.4.2 yamt */
480 1.19.4.2 yamt if (slotstatus == NONE) {
481 1.19.4.2 yamt results->ulr_offset = roundup(dp->i_size, dirblksiz);
482 1.19.4.2 yamt results->ulr_count = 0;
483 1.19.4.2 yamt enduseful = results->ulr_offset;
484 1.19.4.2 yamt } else if (nameiop == DELETE) {
485 1.19.4.2 yamt results->ulr_offset = slotoffset;
486 1.19.4.2 yamt if ((results->ulr_offset & (dirblksiz - 1)) == 0)
487 1.19.4.2 yamt results->ulr_count = 0;
488 1.19.4.2 yamt else
489 1.19.4.2 yamt results->ulr_count =
490 1.19.4.2 yamt results->ulr_offset - prevoff;
491 1.19.4.2 yamt } else {
492 1.19.4.2 yamt results->ulr_offset = slotoffset;
493 1.19.4.2 yamt results->ulr_count = slotsize;
494 1.19.4.2 yamt if (enduseful < slotoffset + slotsize)
495 1.19.4.2 yamt enduseful = slotoffset + slotsize;
496 1.19.4.2 yamt }
497 1.19.4.2 yamt results->ulr_endoff = roundup(enduseful, dirblksiz);
498 1.19.4.2 yamt #if 0 /* commented out by dbj. none of the on disk fields changed */
499 1.19.4.2 yamt dp->i_flag |= IN_CHANGE | IN_UPDATE;
500 1.19.4.2 yamt #endif
501 1.19.4.2 yamt /*
502 1.19.4.2 yamt * We return with the directory locked, so that
503 1.19.4.2 yamt * the parameters we set up above will still be
504 1.19.4.2 yamt * valid if we actually decide to do a direnter().
505 1.19.4.2 yamt * We return ni_vp == NULL to indicate that the entry
506 1.19.4.2 yamt * does not currently exist; we leave a pointer to
507 1.19.4.2 yamt * the (locked) directory inode in ndp->ni_dvp.
508 1.19.4.2 yamt *
509 1.19.4.2 yamt * NB - if the directory is unlocked, then this
510 1.19.4.2 yamt * information cannot be used.
511 1.19.4.2 yamt */
512 1.19.4.2 yamt error = EJUSTRETURN;
513 1.19.4.2 yamt goto out;
514 1.19.4.2 yamt }
515 1.19.4.2 yamt /*
516 1.19.4.2 yamt * Insert name into cache (as non-existent) if appropriate.
517 1.19.4.2 yamt */
518 1.19.4.2 yamt if (nameiop != CREATE) {
519 1.19.4.2 yamt cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
520 1.19.4.2 yamt cnp->cn_flags);
521 1.19.4.2 yamt }
522 1.19.4.2 yamt error = ENOENT;
523 1.19.4.2 yamt goto out;
524 1.19.4.2 yamt
525 1.19.4.2 yamt found:
526 1.19.4.2 yamt if (numdirpasses == 2)
527 1.19.4.2 yamt nchstats.ncs_pass2++;
528 1.19.4.2 yamt /*
529 1.19.4.2 yamt * Check that directory length properly reflects presence
530 1.19.4.2 yamt * of this entry.
531 1.19.4.2 yamt */
532 1.19.4.2 yamt if (results->ulr_offset + LFS_DIRSIZ(FSFMT(vdp), ep, needswap) > dp->i_size) {
533 1.19.4.2 yamt ulfs_dirbad(dp, results->ulr_offset, "i_size too small");
534 1.19.4.2 yamt dp->i_size =
535 1.19.4.2 yamt results->ulr_offset + LFS_DIRSIZ(FSFMT(vdp), ep, needswap);
536 1.19.4.2 yamt DIP_ASSIGN(dp, size, dp->i_size);
537 1.19.4.2 yamt dp->i_flag |= IN_CHANGE | IN_UPDATE;
538 1.19.4.2 yamt }
539 1.19.4.2 yamt brelse(bp, 0);
540 1.19.4.2 yamt
541 1.19.4.2 yamt /*
542 1.19.4.2 yamt * Found component in pathname.
543 1.19.4.2 yamt * If the final component of path name, save information
544 1.19.4.2 yamt * in the cache as to where the entry was found.
545 1.19.4.2 yamt */
546 1.19.4.2 yamt if ((flags & ISLASTCN) && nameiop == LOOKUP)
547 1.19.4.2 yamt results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
548 1.19.4.2 yamt
549 1.19.4.2 yamt /*
550 1.19.4.2 yamt * If deleting, and at end of pathname, return
551 1.19.4.2 yamt * parameters which can be used to remove file.
552 1.19.4.2 yamt * Lock the inode, being careful with ".".
553 1.19.4.2 yamt */
554 1.19.4.2 yamt if (nameiop == DELETE && (flags & ISLASTCN)) {
555 1.19.4.2 yamt /*
556 1.19.4.2 yamt * Return pointer to current entry in results->ulr_offset,
557 1.19.4.2 yamt * and distance past previous entry (if there
558 1.19.4.2 yamt * is a previous entry in this block) in results->ulr_count.
559 1.19.4.2 yamt * Save directory inode pointer in ndp->ni_dvp for dirremove().
560 1.19.4.2 yamt */
561 1.19.4.2 yamt if ((results->ulr_offset & (dirblksiz - 1)) == 0)
562 1.19.4.2 yamt results->ulr_count = 0;
563 1.19.4.2 yamt else
564 1.19.4.2 yamt results->ulr_count = results->ulr_offset - prevoff;
565 1.19.4.2 yamt if (dp->i_number == foundino) {
566 1.19.4.2 yamt vref(vdp);
567 1.19.4.2 yamt tdp = vdp;
568 1.19.4.2 yamt } else {
569 1.19.4.2 yamt if (flags & ISDOTDOT)
570 1.19.4.2 yamt VOP_UNLOCK(vdp); /* race to get the inode */
571 1.19.4.2 yamt error = VFS_VGET(vdp->v_mount, foundino, &tdp);
572 1.19.4.2 yamt if (flags & ISDOTDOT)
573 1.19.4.2 yamt vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
574 1.19.4.2 yamt if (error)
575 1.19.4.2 yamt goto out;
576 1.19.4.2 yamt }
577 1.19.4.2 yamt /*
578 1.19.4.2 yamt * Write access to directory required to delete files.
579 1.19.4.2 yamt */
580 1.19.4.2 yamt error = VOP_ACCESS(vdp, VWRITE, cred);
581 1.19.4.2 yamt if (error) {
582 1.19.4.2 yamt if (dp->i_number == foundino)
583 1.19.4.2 yamt vrele(tdp);
584 1.19.4.2 yamt else
585 1.19.4.2 yamt vput(tdp);
586 1.19.4.2 yamt goto out;
587 1.19.4.2 yamt }
588 1.19.4.2 yamt /*
589 1.19.4.2 yamt * If directory is "sticky", then user must own
590 1.19.4.2 yamt * the directory, or the file in it, else she
591 1.19.4.2 yamt * may not delete it (unless she's root). This
592 1.19.4.2 yamt * implements append-only directories.
593 1.19.4.2 yamt */
594 1.19.4.2 yamt if (dp->i_mode & ISVTX) {
595 1.19.4.2 yamt error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
596 1.19.4.2 yamt tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
597 1.19.4.2 yamt VTOI(tdp)->i_uid));
598 1.19.4.2 yamt if (error) {
599 1.19.4.2 yamt if (dp->i_number == foundino)
600 1.19.4.2 yamt vrele(tdp);
601 1.19.4.2 yamt else
602 1.19.4.2 yamt vput(tdp);
603 1.19.4.2 yamt error = EPERM;
604 1.19.4.2 yamt goto out;
605 1.19.4.2 yamt }
606 1.19.4.2 yamt }
607 1.19.4.2 yamt *vpp = tdp;
608 1.19.4.2 yamt error = 0;
609 1.19.4.2 yamt goto out;
610 1.19.4.2 yamt }
611 1.19.4.2 yamt
612 1.19.4.2 yamt /*
613 1.19.4.2 yamt * If rewriting (RENAME), return the inode and the
614 1.19.4.2 yamt * information required to rewrite the present directory
615 1.19.4.2 yamt * Must get inode of directory entry to verify it's a
616 1.19.4.2 yamt * regular file, or empty directory.
617 1.19.4.2 yamt */
618 1.19.4.2 yamt if (nameiop == RENAME && (flags & ISLASTCN)) {
619 1.19.4.2 yamt error = VOP_ACCESS(vdp, VWRITE, cred);
620 1.19.4.2 yamt if (error)
621 1.19.4.2 yamt goto out;
622 1.19.4.2 yamt /*
623 1.19.4.2 yamt * Careful about locking second inode.
624 1.19.4.2 yamt * This can only occur if the target is ".".
625 1.19.4.2 yamt */
626 1.19.4.2 yamt if (dp->i_number == foundino) {
627 1.19.4.2 yamt error = EISDIR;
628 1.19.4.2 yamt goto out;
629 1.19.4.2 yamt }
630 1.19.4.2 yamt if (flags & ISDOTDOT)
631 1.19.4.2 yamt VOP_UNLOCK(vdp); /* race to get the inode */
632 1.19.4.2 yamt error = VFS_VGET(vdp->v_mount, foundino, &tdp);
633 1.19.4.2 yamt if (flags & ISDOTDOT)
634 1.19.4.2 yamt vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
635 1.19.4.2 yamt if (error)
636 1.19.4.2 yamt goto out;
637 1.19.4.2 yamt *vpp = tdp;
638 1.19.4.2 yamt error = 0;
639 1.19.4.2 yamt goto out;
640 1.19.4.2 yamt }
641 1.19.4.2 yamt
642 1.19.4.2 yamt /*
643 1.19.4.2 yamt * Step through the translation in the name. We do not `vput' the
644 1.19.4.2 yamt * directory because we may need it again if a symbolic link
645 1.19.4.2 yamt * is relative to the current directory. Instead we save it
646 1.19.4.2 yamt * unlocked as "pdp". We must get the target inode before unlocking
647 1.19.4.2 yamt * the directory to insure that the inode will not be removed
648 1.19.4.2 yamt * before we get it. We prevent deadlock by always fetching
649 1.19.4.2 yamt * inodes from the root, moving down the directory tree. Thus
650 1.19.4.2 yamt * when following backward pointers ".." we must unlock the
651 1.19.4.2 yamt * parent directory before getting the requested directory.
652 1.19.4.2 yamt * There is a potential race condition here if both the current
653 1.19.4.2 yamt * and parent directories are removed before the VFS_VGET for the
654 1.19.4.2 yamt * inode associated with ".." returns. We hope that this occurs
655 1.19.4.2 yamt * infrequently since we cannot avoid this race condition without
656 1.19.4.2 yamt * implementing a sophisticated deadlock detection algorithm.
657 1.19.4.2 yamt * Note also that this simple deadlock detection scheme will not
658 1.19.4.2 yamt * work if the file system has any hard links other than ".."
659 1.19.4.2 yamt * that point backwards in the directory structure.
660 1.19.4.2 yamt */
661 1.19.4.2 yamt pdp = vdp;
662 1.19.4.2 yamt if (flags & ISDOTDOT) {
663 1.19.4.2 yamt VOP_UNLOCK(pdp); /* race to get the inode */
664 1.19.4.2 yamt error = VFS_VGET(vdp->v_mount, foundino, &tdp);
665 1.19.4.2 yamt vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
666 1.19.4.2 yamt if (error) {
667 1.19.4.2 yamt goto out;
668 1.19.4.2 yamt }
669 1.19.4.2 yamt *vpp = tdp;
670 1.19.4.2 yamt } else if (dp->i_number == foundino) {
671 1.19.4.2 yamt vref(vdp); /* we want ourself, ie "." */
672 1.19.4.2 yamt *vpp = vdp;
673 1.19.4.2 yamt } else {
674 1.19.4.2 yamt error = VFS_VGET(vdp->v_mount, foundino, &tdp);
675 1.19.4.2 yamt if (error)
676 1.19.4.2 yamt goto out;
677 1.19.4.2 yamt *vpp = tdp;
678 1.19.4.2 yamt }
679 1.19.4.2 yamt
680 1.19.4.2 yamt /*
681 1.19.4.2 yamt * Insert name into cache if appropriate.
682 1.19.4.2 yamt */
683 1.19.4.2 yamt cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
684 1.19.4.2 yamt error = 0;
685 1.19.4.2 yamt
686 1.19.4.2 yamt out:
687 1.19.4.2 yamt if (error == 0 && *vpp != vdp)
688 1.19.4.2 yamt VOP_UNLOCK(*vpp);
689 1.19.4.2 yamt fstrans_done(vdp->v_mount);
690 1.19.4.2 yamt return error;
691 1.19.4.2 yamt }
692 1.19.4.2 yamt
693 1.19.4.2 yamt void
694 1.19.4.2 yamt ulfs_dirbad(struct inode *ip, doff_t offset, const char *how)
695 1.19.4.2 yamt {
696 1.19.4.2 yamt struct mount *mp;
697 1.19.4.2 yamt
698 1.19.4.2 yamt mp = ITOV(ip)->v_mount;
699 1.19.4.2 yamt printf("%s: bad dir ino %llu at offset %d: %s\n",
700 1.19.4.2 yamt mp->mnt_stat.f_mntonname, (unsigned long long)ip->i_number,
701 1.19.4.2 yamt offset, how);
702 1.19.4.2 yamt if ((mp->mnt_stat.f_flag & MNT_RDONLY) == 0)
703 1.19.4.2 yamt panic("bad dir");
704 1.19.4.2 yamt }
705 1.19.4.2 yamt
706 1.19.4.2 yamt /*
707 1.19.4.2 yamt * Do consistency checking on a directory entry:
708 1.19.4.2 yamt * record length must be multiple of 4
709 1.19.4.2 yamt * entry must fit in rest of its DIRBLKSIZ block
710 1.19.4.2 yamt * record must be large enough to contain entry
711 1.19.4.2 yamt * name is not longer than LFS_MAXNAMLEN
712 1.19.4.2 yamt * name must be as long as advertised, and null terminated
713 1.19.4.2 yamt */
714 1.19.4.2 yamt int
715 1.19.4.2 yamt ulfs_dirbadentry(struct vnode *dp, struct lfs_direct *ep, int entryoffsetinblock)
716 1.19.4.2 yamt {
717 1.19.4.2 yamt int i;
718 1.19.4.2 yamt int namlen;
719 1.19.4.2 yamt struct ulfsmount *ump = VFSTOULFS(dp->v_mount);
720 1.19.4.2 yamt struct lfs *fs = ump->um_lfs;
721 1.19.4.2 yamt const int needswap = ULFS_MPNEEDSWAP(fs);
722 1.19.4.2 yamt int dirblksiz = fs->um_dirblksiz;
723 1.19.4.2 yamt
724 1.19.4.2 yamt #if (BYTE_ORDER == LITTLE_ENDIAN)
725 1.19.4.2 yamt if (FSFMT(dp) && needswap == 0)
726 1.19.4.2 yamt namlen = ep->d_type;
727 1.19.4.2 yamt else
728 1.19.4.2 yamt namlen = ep->d_namlen;
729 1.19.4.2 yamt #else
730 1.19.4.2 yamt if (FSFMT(dp) && needswap != 0)
731 1.19.4.2 yamt namlen = ep->d_type;
732 1.19.4.2 yamt else
733 1.19.4.2 yamt namlen = ep->d_namlen;
734 1.19.4.2 yamt #endif
735 1.19.4.2 yamt if ((ulfs_rw16(ep->d_reclen, needswap) & 0x3) != 0 ||
736 1.19.4.2 yamt ulfs_rw16(ep->d_reclen, needswap) >
737 1.19.4.2 yamt dirblksiz - (entryoffsetinblock & (dirblksiz - 1)) ||
738 1.19.4.2 yamt ulfs_rw16(ep->d_reclen, needswap) <
739 1.19.4.2 yamt LFS_DIRSIZ(FSFMT(dp), ep, needswap) ||
740 1.19.4.2 yamt namlen > LFS_MAXNAMLEN) {
741 1.19.4.2 yamt /*return (1); */
742 1.19.4.2 yamt printf("First bad, reclen=%#x, DIRSIZ=%lu, namlen=%d, "
743 1.19.4.2 yamt "flags=%#x, entryoffsetinblock=%d, dirblksiz = %d\n",
744 1.19.4.2 yamt ulfs_rw16(ep->d_reclen, needswap),
745 1.19.4.2 yamt (u_long)LFS_DIRSIZ(FSFMT(dp), ep, needswap),
746 1.19.4.2 yamt namlen, dp->v_mount->mnt_flag, entryoffsetinblock,
747 1.19.4.2 yamt dirblksiz);
748 1.19.4.2 yamt goto bad;
749 1.19.4.2 yamt }
750 1.19.4.2 yamt if (ep->d_ino == 0)
751 1.19.4.2 yamt return (0);
752 1.19.4.2 yamt for (i = 0; i < namlen; i++)
753 1.19.4.2 yamt if (ep->d_name[i] == '\0') {
754 1.19.4.2 yamt /*return (1); */
755 1.19.4.2 yamt printf("Second bad\n");
756 1.19.4.2 yamt goto bad;
757 1.19.4.2 yamt }
758 1.19.4.2 yamt if (ep->d_name[i])
759 1.19.4.2 yamt goto bad;
760 1.19.4.2 yamt return (0);
761 1.19.4.2 yamt bad:
762 1.19.4.2 yamt return (1);
763 1.19.4.2 yamt }
764 1.19.4.2 yamt
765 1.19.4.2 yamt /*
766 1.19.4.2 yamt * Construct a new directory entry after a call to namei, using the
767 1.19.4.2 yamt * name in the componentname argument cnp. The argument ip is the
768 1.19.4.2 yamt * inode to which the new directory entry will refer.
769 1.19.4.2 yamt */
770 1.19.4.2 yamt void
771 1.19.4.2 yamt ulfs_makedirentry(struct inode *ip, struct componentname *cnp,
772 1.19.4.2 yamt struct lfs_direct *newdirp)
773 1.19.4.2 yamt {
774 1.19.4.2 yamt newdirp->d_ino = ip->i_number;
775 1.19.4.2 yamt newdirp->d_namlen = cnp->cn_namelen;
776 1.19.4.2 yamt memcpy(newdirp->d_name, cnp->cn_nameptr, (size_t)cnp->cn_namelen);
777 1.19.4.2 yamt newdirp->d_name[cnp->cn_namelen] = '\0';
778 1.19.4.2 yamt if (FSFMT(ITOV(ip)))
779 1.19.4.2 yamt newdirp->d_type = 0;
780 1.19.4.2 yamt else
781 1.19.4.2 yamt newdirp->d_type = LFS_IFTODT(ip->i_mode);
782 1.19.4.2 yamt }
783 1.19.4.2 yamt
784 1.19.4.2 yamt /*
785 1.19.4.2 yamt * Write a directory entry after a call to namei, using the parameters
786 1.19.4.2 yamt * that ulfs_lookup left in nameidata and in the ulfs_lookup_results.
787 1.19.4.2 yamt *
788 1.19.4.2 yamt * DVP is the directory to be updated. It must be locked.
789 1.19.4.2 yamt * ULR is the ulfs_lookup_results structure from the final lookup step.
790 1.19.4.2 yamt * TVP is not used. (XXX: why is it here? remove it)
791 1.19.4.2 yamt * DIRP is the new directory entry contents.
792 1.19.4.2 yamt * CNP is the componentname from the final lookup step.
793 1.19.4.2 yamt * NEWDIRBP is not used and (XXX) should be removed. The previous
794 1.19.4.2 yamt * comment here said it was used by the now-removed softupdates code.
795 1.19.4.2 yamt *
796 1.19.4.2 yamt * The link count of the target inode is *not* incremented; the
797 1.19.4.2 yamt * caller does that.
798 1.19.4.2 yamt *
799 1.19.4.2 yamt * If ulr->ulr_count is 0, ulfs_lookup did not find space to insert the
800 1.19.4.2 yamt * directory entry. ulr_offset, which is the place to put the entry,
801 1.19.4.2 yamt * should be on a block boundary (and should be at the end of the
802 1.19.4.2 yamt * directory AFAIK) and a fresh block is allocated to put the new
803 1.19.4.2 yamt * directory entry in.
804 1.19.4.2 yamt *
805 1.19.4.2 yamt * If ulr->ulr_count is not zero, ulfs_lookup found a slot to insert
806 1.19.4.2 yamt * the entry into. This slot ranges from ulr_offset to ulr_offset +
807 1.19.4.2 yamt * ulr_count. However, this slot may already be partially populated
808 1.19.4.2 yamt * requiring compaction. See notes below.
809 1.19.4.2 yamt *
810 1.19.4.2 yamt * Furthermore, if ulr_count is not zero and ulr_endoff is not the
811 1.19.4.2 yamt * same as i_size, the directory is truncated to size ulr_endoff.
812 1.19.4.2 yamt */
813 1.19.4.2 yamt int
814 1.19.4.2 yamt ulfs_direnter(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
815 1.19.4.2 yamt struct vnode *tvp, struct lfs_direct *dirp,
816 1.19.4.2 yamt struct componentname *cnp, struct buf *newdirbp)
817 1.19.4.2 yamt {
818 1.19.4.2 yamt kauth_cred_t cr;
819 1.19.4.2 yamt int newentrysize;
820 1.19.4.2 yamt struct inode *dp;
821 1.19.4.2 yamt struct buf *bp;
822 1.19.4.2 yamt u_int dsize;
823 1.19.4.2 yamt struct lfs_direct *ep, *nep;
824 1.19.4.2 yamt int error, ret, lfs_blkoff, loc, spacefree;
825 1.19.4.2 yamt char *dirbuf;
826 1.19.4.2 yamt struct timespec ts;
827 1.19.4.2 yamt struct ulfsmount *ump = VFSTOULFS(dvp->v_mount);
828 1.19.4.2 yamt struct lfs *fs = ump->um_lfs;
829 1.19.4.2 yamt const int needswap = ULFS_MPNEEDSWAP(fs);
830 1.19.4.2 yamt int dirblksiz = fs->um_dirblksiz;
831 1.19.4.2 yamt
832 1.19.4.2 yamt error = 0;
833 1.19.4.2 yamt cr = cnp->cn_cred;
834 1.19.4.2 yamt
835 1.19.4.2 yamt dp = VTOI(dvp);
836 1.19.4.2 yamt newentrysize = LFS_DIRSIZ(0, dirp, 0);
837 1.19.4.2 yamt
838 1.19.4.2 yamt if (ulr->ulr_count == 0) {
839 1.19.4.2 yamt /*
840 1.19.4.2 yamt * If ulr_count is 0, then namei could find no
841 1.19.4.2 yamt * space in the directory. Here, ulr_offset will
842 1.19.4.2 yamt * be on a directory block boundary and we will write the
843 1.19.4.2 yamt * new entry into a fresh block.
844 1.19.4.2 yamt */
845 1.19.4.2 yamt if (ulr->ulr_offset & (dirblksiz - 1))
846 1.19.4.2 yamt panic("ulfs_direnter: newblk");
847 1.19.4.2 yamt if ((error = lfs_balloc(dvp, (off_t)ulr->ulr_offset, dirblksiz,
848 1.19.4.2 yamt cr, B_CLRBUF | B_SYNC, &bp)) != 0) {
849 1.19.4.2 yamt return (error);
850 1.19.4.2 yamt }
851 1.19.4.2 yamt dp->i_size = ulr->ulr_offset + dirblksiz;
852 1.19.4.2 yamt DIP_ASSIGN(dp, size, dp->i_size);
853 1.19.4.2 yamt dp->i_flag |= IN_CHANGE | IN_UPDATE;
854 1.19.4.2 yamt uvm_vnp_setsize(dvp, dp->i_size);
855 1.19.4.2 yamt dirp->d_reclen = ulfs_rw16(dirblksiz, needswap);
856 1.19.4.2 yamt dirp->d_ino = ulfs_rw32(dirp->d_ino, needswap);
857 1.19.4.2 yamt if (FSFMT(dvp)) {
858 1.19.4.2 yamt #if (BYTE_ORDER == LITTLE_ENDIAN)
859 1.19.4.2 yamt if (needswap == 0) {
860 1.19.4.2 yamt #else
861 1.19.4.2 yamt if (needswap != 0) {
862 1.19.4.2 yamt #endif
863 1.19.4.2 yamt u_char tmp = dirp->d_namlen;
864 1.19.4.2 yamt dirp->d_namlen = dirp->d_type;
865 1.19.4.2 yamt dirp->d_type = tmp;
866 1.19.4.2 yamt }
867 1.19.4.2 yamt }
868 1.19.4.2 yamt lfs_blkoff = ulr->ulr_offset & (ump->um_mountp->mnt_stat.f_iosize - 1);
869 1.19.4.2 yamt memcpy((char *)bp->b_data + lfs_blkoff, dirp, newentrysize);
870 1.19.4.2 yamt #ifdef LFS_DIRHASH
871 1.19.4.2 yamt if (dp->i_dirhash != NULL) {
872 1.19.4.2 yamt ulfsdirhash_newblk(dp, ulr->ulr_offset);
873 1.19.4.2 yamt ulfsdirhash_add(dp, dirp, ulr->ulr_offset);
874 1.19.4.2 yamt ulfsdirhash_checkblock(dp, (char *)bp->b_data + lfs_blkoff,
875 1.19.4.2 yamt ulr->ulr_offset);
876 1.19.4.2 yamt }
877 1.19.4.2 yamt #endif
878 1.19.4.2 yamt error = VOP_BWRITE(bp->b_vp, bp);
879 1.19.4.2 yamt vfs_timestamp(&ts);
880 1.19.4.2 yamt ret = lfs_update(dvp, &ts, &ts, UPDATE_DIROP);
881 1.19.4.2 yamt if (error == 0)
882 1.19.4.2 yamt return (ret);
883 1.19.4.2 yamt return (error);
884 1.19.4.2 yamt }
885 1.19.4.2 yamt
886 1.19.4.2 yamt /*
887 1.19.4.2 yamt * If ulr_count is non-zero, then namei found space for the new
888 1.19.4.2 yamt * entry in the range ulr_offset to ulr_offset + ulr_count
889 1.19.4.2 yamt * in the directory. To use this space, we may have to compact
890 1.19.4.2 yamt * the entries located there, by copying them together towards the
891 1.19.4.2 yamt * beginning of the block, leaving the free space in one usable
892 1.19.4.2 yamt * chunk at the end.
893 1.19.4.2 yamt */
894 1.19.4.2 yamt
895 1.19.4.2 yamt /*
896 1.19.4.2 yamt * Increase size of directory if entry eats into new space.
897 1.19.4.2 yamt * This should never push the size past a new multiple of
898 1.19.4.2 yamt * DIRBLKSIZ.
899 1.19.4.2 yamt *
900 1.19.4.2 yamt * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
901 1.19.4.2 yamt */
902 1.19.4.2 yamt if (ulr->ulr_offset + ulr->ulr_count > dp->i_size) {
903 1.19.4.2 yamt #ifdef DIAGNOSTIC
904 1.19.4.2 yamt printf("ulfs_direnter: reached 4.2-only block, "
905 1.19.4.2 yamt "not supposed to happen\n");
906 1.19.4.2 yamt #endif
907 1.19.4.2 yamt dp->i_size = ulr->ulr_offset + ulr->ulr_count;
908 1.19.4.2 yamt DIP_ASSIGN(dp, size, dp->i_size);
909 1.19.4.2 yamt dp->i_flag |= IN_CHANGE | IN_UPDATE;
910 1.19.4.2 yamt }
911 1.19.4.2 yamt /*
912 1.19.4.2 yamt * Get the block containing the space for the new directory entry.
913 1.19.4.2 yamt */
914 1.19.4.2 yamt error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp, true);
915 1.19.4.2 yamt if (error) {
916 1.19.4.2 yamt return (error);
917 1.19.4.2 yamt }
918 1.19.4.2 yamt /*
919 1.19.4.2 yamt * Find space for the new entry. In the simple case, the entry at
920 1.19.4.2 yamt * offset base will have the space. If it does not, then namei
921 1.19.4.2 yamt * arranged that compacting the region ulr_offset to
922 1.19.4.2 yamt * ulr_offset + ulr_count would yield the space.
923 1.19.4.2 yamt */
924 1.19.4.2 yamt ep = (struct lfs_direct *)dirbuf;
925 1.19.4.2 yamt dsize = (ep->d_ino != 0) ? LFS_DIRSIZ(FSFMT(dvp), ep, needswap) : 0;
926 1.19.4.2 yamt spacefree = ulfs_rw16(ep->d_reclen, needswap) - dsize;
927 1.19.4.2 yamt for (loc = ulfs_rw16(ep->d_reclen, needswap); loc < ulr->ulr_count; ) {
928 1.19.4.2 yamt uint16_t reclen;
929 1.19.4.2 yamt
930 1.19.4.2 yamt nep = (struct lfs_direct *)(dirbuf + loc);
931 1.19.4.2 yamt
932 1.19.4.2 yamt /* Trim the existing slot (NB: dsize may be zero). */
933 1.19.4.2 yamt ep->d_reclen = ulfs_rw16(dsize, needswap);
934 1.19.4.2 yamt ep = (struct lfs_direct *)((char *)ep + dsize);
935 1.19.4.2 yamt
936 1.19.4.2 yamt reclen = ulfs_rw16(nep->d_reclen, needswap);
937 1.19.4.2 yamt loc += reclen;
938 1.19.4.2 yamt if (nep->d_ino == 0) {
939 1.19.4.2 yamt /*
940 1.19.4.2 yamt * A mid-block unused entry. Such entries are
941 1.19.4.2 yamt * never created by the kernel, but fsck_ffs
942 1.19.4.2 yamt * can create them (and it doesn't fix them).
943 1.19.4.2 yamt *
944 1.19.4.2 yamt * Add up the free space, and initialise the
945 1.19.4.2 yamt * relocated entry since we don't memcpy it.
946 1.19.4.2 yamt */
947 1.19.4.2 yamt spacefree += reclen;
948 1.19.4.2 yamt ep->d_ino = 0;
949 1.19.4.2 yamt dsize = 0;
950 1.19.4.2 yamt continue;
951 1.19.4.2 yamt }
952 1.19.4.2 yamt dsize = LFS_DIRSIZ(FSFMT(dvp), nep, needswap);
953 1.19.4.2 yamt spacefree += reclen - dsize;
954 1.19.4.2 yamt #ifdef LFS_DIRHASH
955 1.19.4.2 yamt if (dp->i_dirhash != NULL)
956 1.19.4.2 yamt ulfsdirhash_move(dp, nep,
957 1.19.4.2 yamt ulr->ulr_offset + ((char *)nep - dirbuf),
958 1.19.4.2 yamt ulr->ulr_offset + ((char *)ep - dirbuf));
959 1.19.4.2 yamt #endif
960 1.19.4.2 yamt memcpy((void *)ep, (void *)nep, dsize);
961 1.19.4.2 yamt }
962 1.19.4.2 yamt /*
963 1.19.4.2 yamt * Here, `ep' points to a directory entry containing `dsize' in-use
964 1.19.4.2 yamt * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
965 1.19.4.2 yamt * then the entry is completely unused (dsize == 0). The value
966 1.19.4.2 yamt * of ep->d_reclen is always indeterminate.
967 1.19.4.2 yamt *
968 1.19.4.2 yamt * Update the pointer fields in the previous entry (if any),
969 1.19.4.2 yamt * copy in the new entry, and write out the block.
970 1.19.4.2 yamt */
971 1.19.4.2 yamt if (ep->d_ino == 0 ||
972 1.19.4.2 yamt (ulfs_rw32(ep->d_ino, needswap) == ULFS_WINO &&
973 1.19.4.2 yamt memcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
974 1.19.4.2 yamt if (spacefree + dsize < newentrysize)
975 1.19.4.2 yamt panic("ulfs_direnter: compact1");
976 1.19.4.2 yamt dirp->d_reclen = spacefree + dsize;
977 1.19.4.2 yamt } else {
978 1.19.4.2 yamt if (spacefree < newentrysize)
979 1.19.4.2 yamt panic("ulfs_direnter: compact2");
980 1.19.4.2 yamt dirp->d_reclen = spacefree;
981 1.19.4.2 yamt ep->d_reclen = ulfs_rw16(dsize, needswap);
982 1.19.4.2 yamt ep = (struct lfs_direct *)((char *)ep + dsize);
983 1.19.4.2 yamt }
984 1.19.4.2 yamt dirp->d_reclen = ulfs_rw16(dirp->d_reclen, needswap);
985 1.19.4.2 yamt dirp->d_ino = ulfs_rw32(dirp->d_ino, needswap);
986 1.19.4.2 yamt if (FSFMT(dvp)) {
987 1.19.4.2 yamt #if (BYTE_ORDER == LITTLE_ENDIAN)
988 1.19.4.2 yamt if (needswap == 0) {
989 1.19.4.2 yamt #else
990 1.19.4.2 yamt if (needswap != 0) {
991 1.19.4.2 yamt #endif
992 1.19.4.2 yamt u_char tmp = dirp->d_namlen;
993 1.19.4.2 yamt dirp->d_namlen = dirp->d_type;
994 1.19.4.2 yamt dirp->d_type = tmp;
995 1.19.4.2 yamt }
996 1.19.4.2 yamt }
997 1.19.4.2 yamt #ifdef LFS_DIRHASH
998 1.19.4.2 yamt if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
999 1.19.4.2 yamt dirp->d_reclen == spacefree))
1000 1.19.4.2 yamt ulfsdirhash_add(dp, dirp, ulr->ulr_offset + ((char *)ep - dirbuf));
1001 1.19.4.2 yamt #endif
1002 1.19.4.2 yamt memcpy((void *)ep, (void *)dirp, (u_int)newentrysize);
1003 1.19.4.2 yamt #ifdef LFS_DIRHASH
1004 1.19.4.2 yamt if (dp->i_dirhash != NULL)
1005 1.19.4.2 yamt ulfsdirhash_checkblock(dp, dirbuf -
1006 1.19.4.2 yamt (ulr->ulr_offset & (dirblksiz - 1)),
1007 1.19.4.2 yamt ulr->ulr_offset & ~(dirblksiz - 1));
1008 1.19.4.2 yamt #endif
1009 1.19.4.2 yamt error = VOP_BWRITE(bp->b_vp, bp);
1010 1.19.4.2 yamt dp->i_flag |= IN_CHANGE | IN_UPDATE;
1011 1.19.4.2 yamt /*
1012 1.19.4.2 yamt * If all went well, and the directory can be shortened, proceed
1013 1.19.4.2 yamt * with the truncation. Note that we have to unlock the inode for
1014 1.19.4.2 yamt * the entry that we just entered, as the truncation may need to
1015 1.19.4.2 yamt * lock other inodes which can lead to deadlock if we also hold a
1016 1.19.4.2 yamt * lock on the newly entered node.
1017 1.19.4.2 yamt */
1018 1.19.4.2 yamt if (error == 0 && ulr->ulr_endoff && ulr->ulr_endoff < dp->i_size) {
1019 1.19.4.2 yamt #ifdef LFS_DIRHASH
1020 1.19.4.2 yamt if (dp->i_dirhash != NULL)
1021 1.19.4.2 yamt ulfsdirhash_dirtrunc(dp, ulr->ulr_endoff);
1022 1.19.4.2 yamt #endif
1023 1.19.4.2 yamt (void) lfs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC, cr);
1024 1.19.4.2 yamt }
1025 1.19.4.2 yamt return (error);
1026 1.19.4.2 yamt }
1027 1.19.4.2 yamt
1028 1.19.4.2 yamt /*
1029 1.19.4.2 yamt * Remove a directory entry after a call to namei, using the
1030 1.19.4.2 yamt * parameters that ulfs_lookup left in nameidata and in the
1031 1.19.4.2 yamt * ulfs_lookup_results.
1032 1.19.4.2 yamt *
1033 1.19.4.2 yamt * DVP is the directory to be updated. It must be locked.
1034 1.19.4.2 yamt * ULR is the ulfs_lookup_results structure from the final lookup step.
1035 1.19.4.2 yamt * IP, if not null, is the inode being unlinked.
1036 1.19.4.2 yamt * FLAGS may contain DOWHITEOUT.
1037 1.19.4.2 yamt * ISRMDIR is not used and (XXX) should be removed.
1038 1.19.4.2 yamt *
1039 1.19.4.2 yamt * If FLAGS contains DOWHITEOUT the entry is replaced with a whiteout
1040 1.19.4.2 yamt * instead of being cleared.
1041 1.19.4.2 yamt *
1042 1.19.4.2 yamt * ulr->ulr_offset contains the position of the directory entry
1043 1.19.4.2 yamt * to be removed.
1044 1.19.4.2 yamt *
1045 1.19.4.2 yamt * ulr->ulr_reclen contains the size of the directory entry to be
1046 1.19.4.2 yamt * removed.
1047 1.19.4.2 yamt *
1048 1.19.4.2 yamt * ulr->ulr_count contains the size of the *previous* directory
1049 1.19.4.2 yamt * entry. This allows finding it, for free space management. If
1050 1.19.4.2 yamt * ulr_count is 0, the target entry is at the beginning of the
1051 1.19.4.2 yamt * directory. (Does this ever happen? The first entry should be ".",
1052 1.19.4.2 yamt * which should only be removed at rmdir time. Does rmdir come here
1053 1.19.4.2 yamt * to clear out the "." and ".." entries? Perhaps, but I doubt it.)
1054 1.19.4.2 yamt *
1055 1.19.4.2 yamt * The space is marked free by adding it to the record length (not
1056 1.19.4.2 yamt * name length) of the preceding entry. If the first entry becomes
1057 1.19.4.2 yamt * free, it is marked free by setting the inode number to 0.
1058 1.19.4.2 yamt *
1059 1.19.4.2 yamt * The link count of IP is decremented. Note that this is not the
1060 1.19.4.2 yamt * inverse behavior of ulfs_direnter, which does not adjust link
1061 1.19.4.2 yamt * counts. Sigh.
1062 1.19.4.2 yamt */
1063 1.19.4.2 yamt int
1064 1.19.4.2 yamt ulfs_dirremove(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
1065 1.19.4.2 yamt struct inode *ip, int flags, int isrmdir)
1066 1.19.4.2 yamt {
1067 1.19.4.2 yamt struct inode *dp = VTOI(dvp);
1068 1.19.4.2 yamt struct lfs_direct *ep;
1069 1.19.4.2 yamt struct buf *bp;
1070 1.19.4.2 yamt int error;
1071 1.19.4.2 yamt const int needswap = ULFS_MPNEEDSWAP(dp->i_lfs);
1072 1.19.4.2 yamt
1073 1.19.4.2 yamt if (flags & DOWHITEOUT) {
1074 1.19.4.2 yamt /*
1075 1.19.4.2 yamt * Whiteout entry: set d_ino to ULFS_WINO.
1076 1.19.4.2 yamt */
1077 1.19.4.2 yamt error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, (void *)&ep,
1078 1.19.4.2 yamt &bp, true);
1079 1.19.4.2 yamt if (error)
1080 1.19.4.2 yamt return (error);
1081 1.19.4.2 yamt ep->d_ino = ulfs_rw32(ULFS_WINO, needswap);
1082 1.19.4.2 yamt ep->d_type = LFS_DT_WHT;
1083 1.19.4.2 yamt goto out;
1084 1.19.4.2 yamt }
1085 1.19.4.2 yamt
1086 1.19.4.2 yamt if ((error = ulfs_blkatoff(dvp,
1087 1.19.4.2 yamt (off_t)(ulr->ulr_offset - ulr->ulr_count), (void *)&ep, &bp, true)) != 0)
1088 1.19.4.2 yamt return (error);
1089 1.19.4.2 yamt
1090 1.19.4.2 yamt #ifdef LFS_DIRHASH
1091 1.19.4.2 yamt /*
1092 1.19.4.2 yamt * Remove the dirhash entry. This is complicated by the fact
1093 1.19.4.2 yamt * that `ep' is the previous entry when ulr_count != 0.
1094 1.19.4.2 yamt */
1095 1.19.4.2 yamt if (dp->i_dirhash != NULL)
1096 1.19.4.2 yamt ulfsdirhash_remove(dp, (ulr->ulr_count == 0) ? ep :
1097 1.19.4.2 yamt (struct lfs_direct *)((char *)ep +
1098 1.19.4.2 yamt ulfs_rw16(ep->d_reclen, needswap)), ulr->ulr_offset);
1099 1.19.4.2 yamt #endif
1100 1.19.4.2 yamt
1101 1.19.4.2 yamt if (ulr->ulr_count == 0) {
1102 1.19.4.2 yamt /*
1103 1.19.4.2 yamt * First entry in block: set d_ino to zero.
1104 1.19.4.2 yamt */
1105 1.19.4.2 yamt ep->d_ino = 0;
1106 1.19.4.2 yamt } else {
1107 1.19.4.2 yamt /*
1108 1.19.4.2 yamt * Collapse new free space into previous entry.
1109 1.19.4.2 yamt */
1110 1.19.4.2 yamt ep->d_reclen =
1111 1.19.4.2 yamt ulfs_rw16(ulfs_rw16(ep->d_reclen, needswap) + ulr->ulr_reclen,
1112 1.19.4.2 yamt needswap);
1113 1.19.4.2 yamt }
1114 1.19.4.2 yamt
1115 1.19.4.2 yamt #ifdef LFS_DIRHASH
1116 1.19.4.2 yamt if (dp->i_dirhash != NULL) {
1117 1.19.4.2 yamt int dirblksiz = ip->i_lfs->um_dirblksiz;
1118 1.19.4.2 yamt ulfsdirhash_checkblock(dp, (char *)ep -
1119 1.19.4.2 yamt ((ulr->ulr_offset - ulr->ulr_count) & (dirblksiz - 1)),
1120 1.19.4.2 yamt ulr->ulr_offset & ~(dirblksiz - 1));
1121 1.19.4.2 yamt }
1122 1.19.4.2 yamt #endif
1123 1.19.4.2 yamt
1124 1.19.4.2 yamt out:
1125 1.19.4.2 yamt if (ip) {
1126 1.19.4.2 yamt ip->i_nlink--;
1127 1.19.4.2 yamt DIP_ASSIGN(ip, nlink, ip->i_nlink);
1128 1.19.4.2 yamt ip->i_flag |= IN_CHANGE;
1129 1.19.4.2 yamt }
1130 1.19.4.2 yamt /*
1131 1.19.4.2 yamt * XXX did it ever occur to anyone that it might be a good
1132 1.19.4.2 yamt * idea to restore ip->i_nlink if this fails? Or something?
1133 1.19.4.2 yamt * Currently on error return from this function the state of
1134 1.19.4.2 yamt * ip->i_nlink depends on what happened, and callers
1135 1.19.4.2 yamt * definitely do not take this into account.
1136 1.19.4.2 yamt */
1137 1.19.4.2 yamt error = VOP_BWRITE(bp->b_vp, bp);
1138 1.19.4.2 yamt dp->i_flag |= IN_CHANGE | IN_UPDATE;
1139 1.19.4.2 yamt /*
1140 1.19.4.2 yamt * If the last named reference to a snapshot goes away,
1141 1.19.4.2 yamt * drop its snapshot reference so that it will be reclaimed
1142 1.19.4.2 yamt * when last open reference goes away.
1143 1.19.4.2 yamt */
1144 1.19.4.2 yamt if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 &&
1145 1.19.4.2 yamt ip->i_nlink == 0)
1146 1.19.4.2 yamt ulfs_snapgone(ip);
1147 1.19.4.2 yamt return (error);
1148 1.19.4.2 yamt }
1149 1.19.4.2 yamt
1150 1.19.4.2 yamt /*
1151 1.19.4.2 yamt * Rewrite an existing directory entry to point at the inode supplied.
1152 1.19.4.2 yamt *
1153 1.19.4.2 yamt * DP is the directory to update.
1154 1.19.4.2 yamt * OFFSET is the position of the entry in question. It may come
1155 1.19.4.2 yamt * from ulr_offset of a ulfs_lookup_results.
1156 1.19.4.2 yamt * OIP is the old inode the directory previously pointed to.
1157 1.19.4.2 yamt * NEWINUM is the number of the new inode.
1158 1.19.4.2 yamt * NEWTYPE is the new value for the type field of the directory entry.
1159 1.19.4.2 yamt * (This is ignored if the fs doesn't support that.)
1160 1.19.4.2 yamt * ISRMDIR is not used and (XXX) should be removed.
1161 1.19.4.2 yamt * IFLAGS are added to DP's inode flags.
1162 1.19.4.2 yamt *
1163 1.19.4.2 yamt * The link count of OIP is decremented. Note that the link count of
1164 1.19.4.2 yamt * the new inode is *not* incremented. Yay for symmetry.
1165 1.19.4.2 yamt */
1166 1.19.4.2 yamt int
1167 1.19.4.2 yamt ulfs_dirrewrite(struct inode *dp, off_t offset,
1168 1.19.4.2 yamt struct inode *oip, ino_t newinum, int newtype,
1169 1.19.4.2 yamt int isrmdir, int iflags)
1170 1.19.4.2 yamt {
1171 1.19.4.2 yamt struct buf *bp;
1172 1.19.4.2 yamt struct lfs_direct *ep;
1173 1.19.4.2 yamt struct vnode *vdp = ITOV(dp);
1174 1.19.4.2 yamt int error;
1175 1.19.4.2 yamt
1176 1.19.4.2 yamt error = ulfs_blkatoff(vdp, offset, (void *)&ep, &bp, true);
1177 1.19.4.2 yamt if (error)
1178 1.19.4.2 yamt return (error);
1179 1.19.4.2 yamt ep->d_ino = ulfs_rw32(newinum, ULFS_IPNEEDSWAP(dp));
1180 1.19.4.2 yamt if (!FSFMT(vdp))
1181 1.19.4.2 yamt ep->d_type = newtype;
1182 1.19.4.2 yamt oip->i_nlink--;
1183 1.19.4.2 yamt DIP_ASSIGN(oip, nlink, oip->i_nlink);
1184 1.19.4.2 yamt oip->i_flag |= IN_CHANGE;
1185 1.19.4.2 yamt error = VOP_BWRITE(bp->b_vp, bp);
1186 1.19.4.2 yamt dp->i_flag |= iflags;
1187 1.19.4.2 yamt /*
1188 1.19.4.2 yamt * If the last named reference to a snapshot goes away,
1189 1.19.4.2 yamt * drop its snapshot reference so that it will be reclaimed
1190 1.19.4.2 yamt * when last open reference goes away.
1191 1.19.4.2 yamt */
1192 1.19.4.2 yamt if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_nlink == 0)
1193 1.19.4.2 yamt ulfs_snapgone(oip);
1194 1.19.4.2 yamt return (error);
1195 1.19.4.2 yamt }
1196 1.19.4.2 yamt
1197 1.19.4.2 yamt /*
1198 1.19.4.2 yamt * Check if a directory is empty or not.
1199 1.19.4.2 yamt * Inode supplied must be locked.
1200 1.19.4.2 yamt *
1201 1.19.4.2 yamt * Using a struct lfs_dirtemplate here is not precisely
1202 1.19.4.2 yamt * what we want, but better than using a struct lfs_direct.
1203 1.19.4.2 yamt *
1204 1.19.4.2 yamt * NB: does not handle corrupted directories.
1205 1.19.4.2 yamt */
1206 1.19.4.2 yamt int
1207 1.19.4.2 yamt ulfs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
1208 1.19.4.2 yamt {
1209 1.19.4.2 yamt doff_t off;
1210 1.19.4.2 yamt struct lfs_dirtemplate dbuf;
1211 1.19.4.2 yamt struct lfs_direct *dp = (struct lfs_direct *)&dbuf;
1212 1.19.4.2 yamt int error, namlen;
1213 1.19.4.2 yamt size_t count;
1214 1.19.4.2 yamt const int needswap = ULFS_IPNEEDSWAP(ip);
1215 1.19.4.2 yamt #define MINDIRSIZ (sizeof (struct lfs_dirtemplate) / 2)
1216 1.19.4.2 yamt
1217 1.19.4.2 yamt for (off = 0; off < ip->i_size;
1218 1.19.4.2 yamt off += ulfs_rw16(dp->d_reclen, needswap)) {
1219 1.19.4.2 yamt error = vn_rdwr(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ, off,
1220 1.19.4.2 yamt UIO_SYSSPACE, IO_NODELOCKED, cred, &count, NULL);
1221 1.19.4.2 yamt /*
1222 1.19.4.2 yamt * Since we read MINDIRSIZ, residual must
1223 1.19.4.2 yamt * be 0 unless we're at end of file.
1224 1.19.4.2 yamt */
1225 1.19.4.2 yamt if (error || count != 0)
1226 1.19.4.2 yamt return (0);
1227 1.19.4.2 yamt /* avoid infinite loops */
1228 1.19.4.2 yamt if (dp->d_reclen == 0)
1229 1.19.4.2 yamt return (0);
1230 1.19.4.2 yamt /* skip empty entries */
1231 1.19.4.2 yamt if (dp->d_ino == 0 || ulfs_rw32(dp->d_ino, needswap) == ULFS_WINO)
1232 1.19.4.2 yamt continue;
1233 1.19.4.2 yamt /* accept only "." and ".." */
1234 1.19.4.2 yamt #if (BYTE_ORDER == LITTLE_ENDIAN)
1235 1.19.4.2 yamt if (FSFMT(ITOV(ip)) && needswap == 0)
1236 1.19.4.2 yamt namlen = dp->d_type;
1237 1.19.4.2 yamt else
1238 1.19.4.2 yamt namlen = dp->d_namlen;
1239 1.19.4.2 yamt #else
1240 1.19.4.2 yamt if (FSFMT(ITOV(ip)) && needswap != 0)
1241 1.19.4.2 yamt namlen = dp->d_type;
1242 1.19.4.2 yamt else
1243 1.19.4.2 yamt namlen = dp->d_namlen;
1244 1.19.4.2 yamt #endif
1245 1.19.4.2 yamt if (namlen > 2)
1246 1.19.4.2 yamt return (0);
1247 1.19.4.2 yamt if (dp->d_name[0] != '.')
1248 1.19.4.2 yamt return (0);
1249 1.19.4.2 yamt /*
1250 1.19.4.2 yamt * At this point namlen must be 1 or 2.
1251 1.19.4.2 yamt * 1 implies ".", 2 implies ".." if second
1252 1.19.4.2 yamt * char is also "."
1253 1.19.4.2 yamt */
1254 1.19.4.2 yamt if (namlen == 1 &&
1255 1.19.4.2 yamt ulfs_rw32(dp->d_ino, needswap) == ip->i_number)
1256 1.19.4.2 yamt continue;
1257 1.19.4.2 yamt if (dp->d_name[1] == '.' &&
1258 1.19.4.2 yamt ulfs_rw32(dp->d_ino, needswap) == parentino)
1259 1.19.4.2 yamt continue;
1260 1.19.4.2 yamt return (0);
1261 1.19.4.2 yamt }
1262 1.19.4.2 yamt return (1);
1263 1.19.4.2 yamt }
1264 1.19.4.2 yamt
1265 1.19.4.2 yamt /*
1266 1.19.4.2 yamt * Check if source directory is in the path of the target directory.
1267 1.19.4.2 yamt * Target is supplied locked, source is unlocked.
1268 1.19.4.2 yamt * The target is always vput before returning.
1269 1.19.4.2 yamt */
1270 1.19.4.2 yamt int
1271 1.19.4.2 yamt ulfs_checkpath(struct inode *source, struct inode *target, kauth_cred_t cred)
1272 1.19.4.2 yamt {
1273 1.19.4.2 yamt struct vnode *nextvp, *vp;
1274 1.19.4.2 yamt int error, rootino, namlen;
1275 1.19.4.2 yamt struct lfs_dirtemplate dirbuf;
1276 1.19.4.2 yamt const int needswap = ULFS_IPNEEDSWAP(target);
1277 1.19.4.2 yamt
1278 1.19.4.2 yamt vp = ITOV(target);
1279 1.19.4.2 yamt if (target->i_number == source->i_number) {
1280 1.19.4.2 yamt error = EEXIST;
1281 1.19.4.2 yamt goto out;
1282 1.19.4.2 yamt }
1283 1.19.4.2 yamt rootino = ULFS_ROOTINO;
1284 1.19.4.2 yamt error = 0;
1285 1.19.4.2 yamt if (target->i_number == rootino)
1286 1.19.4.2 yamt goto out;
1287 1.19.4.2 yamt
1288 1.19.4.2 yamt for (;;) {
1289 1.19.4.2 yamt if (vp->v_type != VDIR) {
1290 1.19.4.2 yamt error = ENOTDIR;
1291 1.19.4.2 yamt break;
1292 1.19.4.2 yamt }
1293 1.19.4.2 yamt error = vn_rdwr(UIO_READ, vp, (void *)&dirbuf,
1294 1.19.4.2 yamt sizeof (struct lfs_dirtemplate), (off_t)0, UIO_SYSSPACE,
1295 1.19.4.2 yamt IO_NODELOCKED, cred, NULL, NULL);
1296 1.19.4.2 yamt if (error != 0)
1297 1.19.4.2 yamt break;
1298 1.19.4.2 yamt #if (BYTE_ORDER == LITTLE_ENDIAN)
1299 1.19.4.2 yamt if (FSFMT(vp) && needswap == 0)
1300 1.19.4.2 yamt namlen = dirbuf.dotdot_type;
1301 1.19.4.2 yamt else
1302 1.19.4.2 yamt namlen = dirbuf.dotdot_namlen;
1303 1.19.4.2 yamt #else
1304 1.19.4.2 yamt if (FSFMT(vp) && needswap != 0)
1305 1.19.4.2 yamt namlen = dirbuf.dotdot_type;
1306 1.19.4.2 yamt else
1307 1.19.4.2 yamt namlen = dirbuf.dotdot_namlen;
1308 1.19.4.2 yamt #endif
1309 1.19.4.2 yamt if (namlen != 2 ||
1310 1.19.4.2 yamt dirbuf.dotdot_name[0] != '.' ||
1311 1.19.4.2 yamt dirbuf.dotdot_name[1] != '.') {
1312 1.19.4.2 yamt error = ENOTDIR;
1313 1.19.4.2 yamt break;
1314 1.19.4.2 yamt }
1315 1.19.4.2 yamt if (ulfs_rw32(dirbuf.dotdot_ino, needswap) == source->i_number) {
1316 1.19.4.2 yamt error = EINVAL;
1317 1.19.4.2 yamt break;
1318 1.19.4.2 yamt }
1319 1.19.4.2 yamt if (ulfs_rw32(dirbuf.dotdot_ino, needswap) == rootino)
1320 1.19.4.2 yamt break;
1321 1.19.4.2 yamt VOP_UNLOCK(vp);
1322 1.19.4.2 yamt error = VFS_VGET(vp->v_mount,
1323 1.19.4.2 yamt ulfs_rw32(dirbuf.dotdot_ino, needswap), &nextvp);
1324 1.19.4.2 yamt vrele(vp);
1325 1.19.4.2 yamt if (error) {
1326 1.19.4.2 yamt vp = NULL;
1327 1.19.4.2 yamt break;
1328 1.19.4.2 yamt }
1329 1.19.4.2 yamt vp = nextvp;
1330 1.19.4.2 yamt }
1331 1.19.4.2 yamt
1332 1.19.4.2 yamt out:
1333 1.19.4.2 yamt if (error == ENOTDIR)
1334 1.19.4.2 yamt printf("checkpath: .. not a directory\n");
1335 1.19.4.2 yamt if (vp != NULL)
1336 1.19.4.2 yamt vput(vp);
1337 1.19.4.2 yamt return (error);
1338 1.19.4.2 yamt }
1339 1.19.4.2 yamt
1340 1.19.4.2 yamt /*
1341 1.19.4.2 yamt * Extract the inode number of ".." from a directory.
1342 1.19.4.2 yamt * Helper for ulfs_parentcheck.
1343 1.19.4.2 yamt */
1344 1.19.4.2 yamt static int
1345 1.19.4.2 yamt ulfs_readdotdot(struct vnode *vp, int needswap, kauth_cred_t cred, ino_t *result)
1346 1.19.4.2 yamt {
1347 1.19.4.2 yamt struct lfs_dirtemplate dirbuf;
1348 1.19.4.2 yamt int namlen, error;
1349 1.19.4.2 yamt
1350 1.19.4.2 yamt error = vn_rdwr(UIO_READ, vp, &dirbuf,
1351 1.19.4.2 yamt sizeof (struct lfs_dirtemplate), (off_t)0, UIO_SYSSPACE,
1352 1.19.4.2 yamt IO_NODELOCKED, cred, NULL, NULL);
1353 1.19.4.2 yamt if (error) {
1354 1.19.4.2 yamt return error;
1355 1.19.4.2 yamt }
1356 1.19.4.2 yamt
1357 1.19.4.2 yamt #if (BYTE_ORDER == LITTLE_ENDIAN)
1358 1.19.4.2 yamt if (FSFMT(vp) && needswap == 0)
1359 1.19.4.2 yamt namlen = dirbuf.dotdot_type;
1360 1.19.4.2 yamt else
1361 1.19.4.2 yamt namlen = dirbuf.dotdot_namlen;
1362 1.19.4.2 yamt #else
1363 1.19.4.2 yamt if (FSFMT(vp) && needswap != 0)
1364 1.19.4.2 yamt namlen = dirbuf.dotdot_type;
1365 1.19.4.2 yamt else
1366 1.19.4.2 yamt namlen = dirbuf.dotdot_namlen;
1367 1.19.4.2 yamt #endif
1368 1.19.4.2 yamt if (namlen != 2 ||
1369 1.19.4.2 yamt dirbuf.dotdot_name[0] != '.' ||
1370 1.19.4.2 yamt dirbuf.dotdot_name[1] != '.') {
1371 1.19.4.2 yamt printf("ulfs_readdotdot: directory %llu contains "
1372 1.19.4.2 yamt "garbage instead of ..\n",
1373 1.19.4.2 yamt (unsigned long long) VTOI(vp)->i_number);
1374 1.19.4.2 yamt return ENOTDIR;
1375 1.19.4.2 yamt }
1376 1.19.4.2 yamt *result = ulfs_rw32(dirbuf.dotdot_ino, needswap);
1377 1.19.4.2 yamt return 0;
1378 1.19.4.2 yamt }
1379 1.19.4.2 yamt
1380 1.19.4.2 yamt /*
1381 1.19.4.2 yamt * Check if LOWER is a descendent of UPPER. If we find UPPER, return
1382 1.19.4.2 yamt * nonzero in FOUND and return a reference to the immediate descendent
1383 1.19.4.2 yamt * of UPPER in UPPERCHILD. If we don't find UPPER (that is, if we
1384 1.19.4.2 yamt * reach the volume root and that isn't UPPER), return zero in FOUND
1385 1.19.4.2 yamt * and null in UPPERCHILD.
1386 1.19.4.2 yamt *
1387 1.19.4.2 yamt * Neither UPPER nor LOWER should be locked.
1388 1.19.4.2 yamt *
1389 1.19.4.2 yamt * On error (such as a permissions error checking up the directory
1390 1.19.4.2 yamt * tree) fail entirely.
1391 1.19.4.2 yamt *
1392 1.19.4.2 yamt * Note that UPPER and LOWER must be on the same volume, and because
1393 1.19.4.2 yamt * we inspect only that volume NEEDSWAP can be constant.
1394 1.19.4.2 yamt */
1395 1.19.4.2 yamt int
1396 1.19.4.2 yamt ulfs_parentcheck(struct vnode *upper, struct vnode *lower, kauth_cred_t cred,
1397 1.19.4.2 yamt int *found_ret, struct vnode **upperchild_ret)
1398 1.19.4.2 yamt {
1399 1.19.4.2 yamt const int needswap = ULFS_IPNEEDSWAP(VTOI(lower));
1400 1.19.4.2 yamt ino_t upper_ino, found_ino = -1; /* XXX gcc 4.8 */
1401 1.19.4.2 yamt struct vnode *current, *next;
1402 1.19.4.2 yamt int error;
1403 1.19.4.2 yamt
1404 1.19.4.2 yamt if (upper == lower) {
1405 1.19.4.2 yamt vref(upper);
1406 1.19.4.2 yamt *found_ret = 1;
1407 1.19.4.2 yamt *upperchild_ret = upper;
1408 1.19.4.2 yamt return 0;
1409 1.19.4.2 yamt }
1410 1.19.4.2 yamt if (VTOI(lower)->i_number == ULFS_ROOTINO) {
1411 1.19.4.2 yamt *found_ret = 0;
1412 1.19.4.2 yamt *upperchild_ret = NULL;
1413 1.19.4.2 yamt return 0;
1414 1.19.4.2 yamt }
1415 1.19.4.2 yamt
1416 1.19.4.2 yamt upper_ino = VTOI(upper)->i_number;
1417 1.19.4.2 yamt
1418 1.19.4.2 yamt current = lower;
1419 1.19.4.2 yamt vref(current);
1420 1.19.4.2 yamt vn_lock(current, LK_EXCLUSIVE | LK_RETRY);
1421 1.19.4.2 yamt
1422 1.19.4.2 yamt for (;;) {
1423 1.19.4.2 yamt error = ulfs_readdotdot(current, needswap, cred, &found_ino);
1424 1.19.4.2 yamt if (error) {
1425 1.19.4.2 yamt vput(current);
1426 1.19.4.2 yamt return error;
1427 1.19.4.2 yamt }
1428 1.19.4.2 yamt if (found_ino == upper_ino) {
1429 1.19.4.2 yamt VOP_UNLOCK(current);
1430 1.19.4.2 yamt *found_ret = 1;
1431 1.19.4.2 yamt *upperchild_ret = current;
1432 1.19.4.2 yamt return 0;
1433 1.19.4.2 yamt }
1434 1.19.4.2 yamt if (found_ino == ULFS_ROOTINO) {
1435 1.19.4.2 yamt vput(current);
1436 1.19.4.2 yamt *found_ret = 0;
1437 1.19.4.2 yamt *upperchild_ret = NULL;
1438 1.19.4.2 yamt return 0;
1439 1.19.4.2 yamt }
1440 1.19.4.2 yamt VOP_UNLOCK(current);
1441 1.19.4.2 yamt error = VFS_VGET(current->v_mount, found_ino, &next);
1442 1.19.4.2 yamt if (error) {
1443 1.19.4.2 yamt vrele(current);
1444 1.19.4.2 yamt return error;
1445 1.19.4.2 yamt }
1446 1.19.4.2 yamt KASSERT(VOP_ISLOCKED(next));
1447 1.19.4.2 yamt if (next->v_type != VDIR) {
1448 1.19.4.2 yamt printf("ulfs_parentcheck: inode %llu reached via .. of "
1449 1.19.4.2 yamt "inode %llu is not a directory\n",
1450 1.19.4.2 yamt (unsigned long long)VTOI(next)->i_number,
1451 1.19.4.2 yamt (unsigned long long)VTOI(current)->i_number);
1452 1.19.4.2 yamt vput(next);
1453 1.19.4.2 yamt vrele(current);
1454 1.19.4.2 yamt return ENOTDIR;
1455 1.19.4.2 yamt }
1456 1.19.4.2 yamt vrele(current);
1457 1.19.4.2 yamt current = next;
1458 1.19.4.2 yamt }
1459 1.19.4.2 yamt
1460 1.19.4.2 yamt return 0;
1461 1.19.4.2 yamt }
1462 1.19.4.2 yamt
1463 1.19.4.2 yamt #define ULFS_DIRRABLKS 0
1464 1.19.4.2 yamt int ulfs_dirrablks = ULFS_DIRRABLKS;
1465 1.19.4.2 yamt
1466 1.19.4.2 yamt /*
1467 1.19.4.2 yamt * ulfs_blkatoff: Return buffer with the contents of block "offset" from
1468 1.19.4.2 yamt * the beginning of directory "vp". If "res" is non-NULL, fill it in with
1469 1.19.4.2 yamt * a pointer to the remaining space in the directory. If the caller intends
1470 1.19.4.2 yamt * to modify the buffer returned, "modify" must be true.
1471 1.19.4.2 yamt */
1472 1.19.4.2 yamt
1473 1.19.4.2 yamt int
1474 1.19.4.2 yamt ulfs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp,
1475 1.19.4.2 yamt bool modify)
1476 1.19.4.2 yamt {
1477 1.19.4.2 yamt struct inode *ip __diagused;
1478 1.19.4.2 yamt struct buf *bp;
1479 1.19.4.2 yamt daddr_t lbn;
1480 1.19.4.2 yamt const int dirrablks = ulfs_dirrablks;
1481 1.19.4.2 yamt daddr_t *blks;
1482 1.19.4.2 yamt int *blksizes;
1483 1.19.4.2 yamt int run, error;
1484 1.19.4.2 yamt struct mount *mp = vp->v_mount;
1485 1.19.4.2 yamt const int bshift = mp->mnt_fs_bshift;
1486 1.19.4.2 yamt const int bsize = 1 << bshift;
1487 1.19.4.2 yamt off_t eof;
1488 1.19.4.2 yamt
1489 1.19.4.2 yamt blks = kmem_alloc((1 + dirrablks) * sizeof(daddr_t), KM_SLEEP);
1490 1.19.4.2 yamt blksizes = kmem_alloc((1 + dirrablks) * sizeof(int), KM_SLEEP);
1491 1.19.4.2 yamt ip = VTOI(vp);
1492 1.19.4.2 yamt KASSERT(vp->v_size == ip->i_size);
1493 1.19.4.2 yamt GOP_SIZE(vp, vp->v_size, &eof, 0);
1494 1.19.4.2 yamt lbn = offset >> bshift;
1495 1.19.4.2 yamt
1496 1.19.4.2 yamt for (run = 0; run <= dirrablks;) {
1497 1.19.4.2 yamt const off_t curoff = lbn << bshift;
1498 1.19.4.2 yamt const int size = MIN(eof - curoff, bsize);
1499 1.19.4.2 yamt
1500 1.19.4.2 yamt if (size == 0) {
1501 1.19.4.2 yamt break;
1502 1.19.4.2 yamt }
1503 1.19.4.2 yamt KASSERT(curoff < eof);
1504 1.19.4.2 yamt blks[run] = lbn;
1505 1.19.4.2 yamt blksizes[run] = size;
1506 1.19.4.2 yamt lbn++;
1507 1.19.4.2 yamt run++;
1508 1.19.4.2 yamt if (size != bsize) {
1509 1.19.4.2 yamt break;
1510 1.19.4.2 yamt }
1511 1.19.4.2 yamt }
1512 1.19.4.2 yamt KASSERT(run >= 1);
1513 1.19.4.2 yamt error = breadn(vp, blks[0], blksizes[0], &blks[1], &blksizes[1],
1514 1.19.4.2 yamt run - 1, NOCRED, (modify ? B_MODIFY : 0), &bp);
1515 1.19.4.2 yamt if (error != 0) {
1516 1.19.4.2 yamt *bpp = NULL;
1517 1.19.4.2 yamt goto out;
1518 1.19.4.2 yamt }
1519 1.19.4.2 yamt if (res) {
1520 1.19.4.2 yamt *res = (char *)bp->b_data + (offset & (bsize - 1));
1521 1.19.4.2 yamt }
1522 1.19.4.2 yamt *bpp = bp;
1523 1.19.4.2 yamt
1524 1.19.4.2 yamt out:
1525 1.19.4.2 yamt kmem_free(blks, (1 + dirrablks) * sizeof(daddr_t));
1526 1.19.4.2 yamt kmem_free(blksizes, (1 + dirrablks) * sizeof(int));
1527 1.19.4.2 yamt return error;
1528 1.19.4.2 yamt }
1529