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