ulfs_lookup.c revision 1.29 1 1.29 dholland /* $NetBSD: ulfs_lookup.c,v 1.29 2015/09/15 15:00:49 dholland 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.29 dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_lookup.c,v 1.29 2015/09/15 15:00:49 dholland 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.26 dholland #include <ufs/lfs/lfs.h>
63 1.26 dholland #include <ufs/lfs/lfs_accessors.h>
64 1.13 dholland #include <ufs/lfs/lfs_extern.h>
65 1.13 dholland
66 1.2 dholland #include <ufs/lfs/ulfs_inode.h>
67 1.3 dholland #ifdef LFS_DIRHASH
68 1.2 dholland #include <ufs/lfs/ulfs_dirhash.h>
69 1.1 dholland #endif
70 1.2 dholland #include <ufs/lfs/ulfsmount.h>
71 1.2 dholland #include <ufs/lfs/ulfs_extern.h>
72 1.2 dholland #include <ufs/lfs/ulfs_bswap.h>
73 1.1 dholland
74 1.1 dholland #include <miscfs/genfs/genfs.h>
75 1.1 dholland
76 1.1 dholland #ifdef DIAGNOSTIC
77 1.5 dholland int lfs_dirchk = 1;
78 1.1 dholland #else
79 1.5 dholland int lfs_dirchk = 0;
80 1.1 dholland #endif
81 1.1 dholland
82 1.1 dholland /*
83 1.1 dholland * Convert a component of a pathname into a pointer to a locked inode.
84 1.1 dholland * This is a very central and rather complicated routine.
85 1.1 dholland * If the file system is not maintained in a strict tree hierarchy,
86 1.1 dholland * this can result in a deadlock situation (see comments in code below).
87 1.1 dholland *
88 1.1 dholland * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
89 1.1 dholland * on whether the name is to be looked up, created, renamed, or deleted.
90 1.1 dholland * When CREATE, RENAME, or DELETE is specified, information usable in
91 1.1 dholland * creating, renaming, or deleting a directory entry may be calculated.
92 1.1 dholland * If flag has LOCKPARENT or'ed into it and the target of the pathname
93 1.1 dholland * exists, lookup returns both the target and its parent directory locked.
94 1.1 dholland * When creating or renaming and LOCKPARENT is specified, the target may
95 1.1 dholland * not be ".". When deleting and LOCKPARENT is specified, the target may
96 1.1 dholland * be "."., but the caller must check to ensure it does an vrele and vput
97 1.1 dholland * instead of two vputs.
98 1.1 dholland *
99 1.4 dholland * Overall outline of ulfs_lookup:
100 1.1 dholland *
101 1.1 dholland * check accessibility of directory
102 1.1 dholland * look for name in cache, if found, then if at end of path
103 1.1 dholland * and deleting or creating, drop it, else return name
104 1.1 dholland * search for name in directory, to found or notfound
105 1.1 dholland * notfound:
106 1.1 dholland * if creating, return locked directory, leaving info on available slots
107 1.1 dholland * else return error
108 1.1 dholland * found:
109 1.1 dholland * if at end of path and deleting, return information to allow delete
110 1.1 dholland * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
111 1.1 dholland * inode and return info to allow rewrite
112 1.1 dholland * if not at end, add name to cache; if at end and neither creating
113 1.1 dholland * nor deleting, add name to cache
114 1.1 dholland */
115 1.1 dholland int
116 1.4 dholland ulfs_lookup(void *v)
117 1.1 dholland {
118 1.19 hannken struct vop_lookup_v2_args /* {
119 1.1 dholland struct vnode *a_dvp;
120 1.1 dholland struct vnode **a_vpp;
121 1.1 dholland struct componentname *a_cnp;
122 1.1 dholland } */ *ap = v;
123 1.1 dholland struct vnode *vdp = ap->a_dvp; /* vnode for directory being searched */
124 1.1 dholland struct inode *dp = VTOI(vdp); /* inode for directory being searched */
125 1.1 dholland struct buf *bp; /* a buffer of directory entries */
126 1.7 dholland struct lfs_direct *ep; /* the current directory entry */
127 1.1 dholland int entryoffsetinblock; /* offset of ep in bp's buffer */
128 1.1 dholland enum {
129 1.1 dholland NONE, /* need to search a slot for our new entry */
130 1.1 dholland COMPACT, /* a compaction can make a slot in the current
131 1.1 dholland DIRBLKSIZ block */
132 1.1 dholland FOUND, /* found a slot (or no need to search) */
133 1.1 dholland } slotstatus;
134 1.1 dholland doff_t slotoffset; /* offset of area with free space.
135 1.1 dholland a special value -1 for invalid */
136 1.1 dholland int slotsize; /* size of area at slotoffset */
137 1.1 dholland int slotfreespace; /* accumulated amount of space free in
138 1.1 dholland the current DIRBLKSIZ block */
139 1.1 dholland int slotneeded; /* size of the entry we're seeking */
140 1.1 dholland int numdirpasses; /* strategy for directory search */
141 1.1 dholland doff_t endsearch; /* offset to end directory search */
142 1.1 dholland doff_t prevoff; /* previous value of ulr_offset */
143 1.24 hannken struct vnode *tdp; /* returned by vcache_get */
144 1.1 dholland doff_t enduseful; /* pointer past last used dir slot.
145 1.1 dholland used for directory truncation. */
146 1.1 dholland u_long bmask; /* block offset mask */
147 1.1 dholland int error;
148 1.1 dholland struct vnode **vpp = ap->a_vpp;
149 1.1 dholland struct componentname *cnp = ap->a_cnp;
150 1.1 dholland kauth_cred_t cred = cnp->cn_cred;
151 1.1 dholland int flags;
152 1.1 dholland int nameiop = cnp->cn_nameiop;
153 1.15 dholland struct lfs *fs = dp->i_lfs;
154 1.15 dholland int dirblksiz = fs->um_dirblksiz;
155 1.1 dholland ino_t foundino;
156 1.4 dholland struct ulfs_lookup_results *results;
157 1.1 dholland int iswhiteout; /* temp result from cache_lookup() */
158 1.1 dholland
159 1.1 dholland flags = cnp->cn_flags;
160 1.1 dholland
161 1.1 dholland bp = NULL;
162 1.1 dholland slotoffset = -1;
163 1.1 dholland *vpp = NULL;
164 1.1 dholland endsearch = 0; /* silence compiler warning */
165 1.1 dholland
166 1.1 dholland /*
167 1.1 dholland * Produce the auxiliary lookup results into i_crap. Increment
168 1.1 dholland * its serial number so elsewhere we can tell if we're using
169 1.1 dholland * stale results. This should not be done this way. XXX.
170 1.1 dholland */
171 1.1 dholland results = &dp->i_crap;
172 1.1 dholland dp->i_crapcounter++;
173 1.1 dholland
174 1.1 dholland /*
175 1.1 dholland * Check accessiblity of directory.
176 1.1 dholland */
177 1.1 dholland if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
178 1.1 dholland return (error);
179 1.1 dholland
180 1.1 dholland if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
181 1.1 dholland (nameiop == DELETE || nameiop == RENAME))
182 1.1 dholland return (EROFS);
183 1.1 dholland
184 1.1 dholland /*
185 1.1 dholland * We now have a segment name to search for, and a directory to search.
186 1.1 dholland *
187 1.1 dholland * Before tediously performing a linear scan of the directory,
188 1.1 dholland * check the name cache to see if the directory/name pair
189 1.1 dholland * we are looking for is known already.
190 1.1 dholland */
191 1.1 dholland if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
192 1.1 dholland cnp->cn_nameiop, cnp->cn_flags, &iswhiteout, vpp)) {
193 1.1 dholland if (iswhiteout) {
194 1.1 dholland cnp->cn_flags |= ISWHITEOUT;
195 1.1 dholland }
196 1.1 dholland return *vpp == NULLVP ? ENOENT : 0;
197 1.1 dholland }
198 1.1 dholland if (iswhiteout) {
199 1.1 dholland /*
200 1.1 dholland * The namecache set iswhiteout without finding a
201 1.1 dholland * cache entry. As of this writing (20121014), this
202 1.1 dholland * can happen if there was a whiteout entry that has
203 1.1 dholland * been invalidated by the lookup. It is not clear if
204 1.1 dholland * it is correct to set ISWHITEOUT in this case or
205 1.1 dholland * not; however, doing so retains the prior behavior,
206 1.1 dholland * so we'll go with that until some clearer answer
207 1.1 dholland * appears. XXX
208 1.1 dholland */
209 1.1 dholland cnp->cn_flags |= ISWHITEOUT;
210 1.1 dholland }
211 1.1 dholland
212 1.1 dholland fstrans_start(vdp->v_mount, FSTRANS_SHARED);
213 1.1 dholland
214 1.1 dholland /*
215 1.1 dholland * Suppress search for slots unless creating
216 1.1 dholland * file and at end of pathname, in which case
217 1.1 dholland * we watch for a place to put the new file in
218 1.1 dholland * case it doesn't already exist.
219 1.1 dholland */
220 1.1 dholland slotstatus = FOUND;
221 1.1 dholland slotfreespace = slotsize = slotneeded = 0;
222 1.1 dholland if ((nameiop == CREATE || nameiop == RENAME) && (flags & ISLASTCN)) {
223 1.1 dholland slotstatus = NONE;
224 1.9 dholland slotneeded = LFS_DIRECTSIZ(cnp->cn_namelen);
225 1.1 dholland }
226 1.1 dholland
227 1.1 dholland /*
228 1.1 dholland * If there is cached information on a previous search of
229 1.1 dholland * this directory, pick up where we last left off.
230 1.1 dholland * We cache only lookups as these are the most common
231 1.1 dholland * and have the greatest payoff. Caching CREATE has little
232 1.1 dholland * benefit as it usually must search the entire directory
233 1.1 dholland * to determine that the entry does not exist. Caching the
234 1.1 dholland * location of the last DELETE or RENAME has not reduced
235 1.1 dholland * profiling time and hence has been removed in the interest
236 1.1 dholland * of simplicity.
237 1.1 dholland */
238 1.1 dholland bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
239 1.1 dholland
240 1.3 dholland #ifdef LFS_DIRHASH
241 1.1 dholland /*
242 1.1 dholland * Use dirhash for fast operations on large directories. The logic
243 1.1 dholland * to determine whether to hash the directory is contained within
244 1.4 dholland * ulfsdirhash_build(); a zero return means that it decided to hash
245 1.1 dholland * this directory and it successfully built up the hash table.
246 1.1 dholland */
247 1.4 dholland if (ulfsdirhash_build(dp) == 0) {
248 1.1 dholland /* Look for a free slot if needed. */
249 1.1 dholland enduseful = dp->i_size;
250 1.1 dholland if (slotstatus != FOUND) {
251 1.4 dholland slotoffset = ulfsdirhash_findfree(dp, slotneeded,
252 1.1 dholland &slotsize);
253 1.1 dholland if (slotoffset >= 0) {
254 1.1 dholland slotstatus = COMPACT;
255 1.4 dholland enduseful = ulfsdirhash_enduseful(dp);
256 1.1 dholland if (enduseful < 0)
257 1.1 dholland enduseful = dp->i_size;
258 1.1 dholland }
259 1.1 dholland }
260 1.1 dholland /* Look up the component. */
261 1.1 dholland numdirpasses = 1;
262 1.1 dholland entryoffsetinblock = 0; /* silence compiler warning */
263 1.4 dholland switch (ulfsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
264 1.1 dholland &results->ulr_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
265 1.1 dholland case 0:
266 1.7 dholland ep = (struct lfs_direct *)((char *)bp->b_data +
267 1.1 dholland (results->ulr_offset & bmask));
268 1.1 dholland goto foundentry;
269 1.1 dholland case ENOENT:
270 1.1 dholland results->ulr_offset = roundup(dp->i_size, dirblksiz);
271 1.1 dholland goto notfound;
272 1.1 dholland default:
273 1.1 dholland /* Something failed; just do a linear search. */
274 1.1 dholland break;
275 1.1 dholland }
276 1.1 dholland }
277 1.3 dholland #endif /* LFS_DIRHASH */
278 1.1 dholland
279 1.1 dholland if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
280 1.1 dholland results->ulr_diroff >= dp->i_size) {
281 1.1 dholland entryoffsetinblock = 0;
282 1.1 dholland results->ulr_offset = 0;
283 1.1 dholland numdirpasses = 1;
284 1.1 dholland } else {
285 1.1 dholland results->ulr_offset = results->ulr_diroff;
286 1.1 dholland if ((entryoffsetinblock = results->ulr_offset & bmask) &&
287 1.4 dholland (error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
288 1.1 dholland NULL, &bp, false)))
289 1.1 dholland goto out;
290 1.1 dholland numdirpasses = 2;
291 1.21 joerg namecache_count_2passes();
292 1.1 dholland }
293 1.1 dholland prevoff = results->ulr_offset;
294 1.1 dholland endsearch = roundup(dp->i_size, dirblksiz);
295 1.1 dholland enduseful = 0;
296 1.1 dholland
297 1.1 dholland searchloop:
298 1.1 dholland while (results->ulr_offset < endsearch) {
299 1.1 dholland if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
300 1.1 dholland preempt();
301 1.1 dholland /*
302 1.1 dholland * If necessary, get the next directory block.
303 1.1 dholland */
304 1.1 dholland if ((results->ulr_offset & bmask) == 0) {
305 1.1 dholland if (bp != NULL)
306 1.1 dholland brelse(bp, 0);
307 1.4 dholland error = ulfs_blkatoff(vdp, (off_t)results->ulr_offset,
308 1.1 dholland NULL, &bp, false);
309 1.1 dholland if (error)
310 1.1 dholland goto out;
311 1.1 dholland entryoffsetinblock = 0;
312 1.1 dholland }
313 1.1 dholland /*
314 1.1 dholland * If still looking for a slot, and at a DIRBLKSIZ
315 1.1 dholland * boundary, have to start looking for free space again.
316 1.1 dholland */
317 1.1 dholland if (slotstatus == NONE &&
318 1.1 dholland (entryoffsetinblock & (dirblksiz - 1)) == 0) {
319 1.1 dholland slotoffset = -1;
320 1.1 dholland slotfreespace = 0;
321 1.1 dholland }
322 1.1 dholland /*
323 1.1 dholland * Get pointer to next entry.
324 1.1 dholland * Full validation checks are slow, so we only check
325 1.1 dholland * enough to insure forward progress through the
326 1.1 dholland * directory. Complete checks can be run by patching
327 1.5 dholland * "lfs_dirchk" to be true.
328 1.1 dholland */
329 1.1 dholland KASSERT(bp != NULL);
330 1.7 dholland ep = (struct lfs_direct *)((char *)bp->b_data + entryoffsetinblock);
331 1.27 dholland if (lfs_dir_getreclen(fs, ep) == 0 ||
332 1.5 dholland (lfs_dirchk && ulfs_dirbadentry(vdp, ep, entryoffsetinblock))) {
333 1.1 dholland int i;
334 1.1 dholland
335 1.4 dholland ulfs_dirbad(dp, results->ulr_offset, "mangled entry");
336 1.1 dholland i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
337 1.1 dholland results->ulr_offset += i;
338 1.1 dholland entryoffsetinblock += i;
339 1.1 dholland continue;
340 1.1 dholland }
341 1.1 dholland
342 1.1 dholland /*
343 1.1 dholland * If an appropriate sized slot has not yet been found,
344 1.1 dholland * check to see if one is available. Also accumulate space
345 1.1 dholland * in the current block so that we can determine if
346 1.1 dholland * compaction is viable.
347 1.1 dholland */
348 1.1 dholland if (slotstatus != FOUND) {
349 1.27 dholland int size = lfs_dir_getreclen(fs, ep);
350 1.1 dholland
351 1.27 dholland if (lfs_dir_getino(fs, ep) != 0)
352 1.26 dholland size -= LFS_DIRSIZ(fs, ep);
353 1.1 dholland if (size > 0) {
354 1.1 dholland if (size >= slotneeded) {
355 1.1 dholland slotstatus = FOUND;
356 1.1 dholland slotoffset = results->ulr_offset;
357 1.27 dholland slotsize = lfs_dir_getreclen(fs, ep);
358 1.1 dholland } else if (slotstatus == NONE) {
359 1.1 dholland slotfreespace += size;
360 1.1 dholland if (slotoffset == -1)
361 1.1 dholland slotoffset = results->ulr_offset;
362 1.1 dholland if (slotfreespace >= slotneeded) {
363 1.1 dholland slotstatus = COMPACT;
364 1.1 dholland slotsize = results->ulr_offset +
365 1.27 dholland lfs_dir_getreclen(fs, ep) -
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.27 dholland if (lfs_dir_getino(fs, ep)) {
376 1.1 dholland int namlen;
377 1.1 dholland
378 1.26 dholland namlen = lfs_dir_getnamlen(fs, ep);
379 1.1 dholland if (namlen == cnp->cn_namelen &&
380 1.1 dholland !memcmp(cnp->cn_nameptr, ep->d_name,
381 1.1 dholland (unsigned)namlen)) {
382 1.3 dholland #ifdef LFS_DIRHASH
383 1.1 dholland foundentry:
384 1.1 dholland #endif
385 1.1 dholland /*
386 1.1 dholland * Save directory entry's inode number and
387 1.1 dholland * reclen, and release directory buffer.
388 1.1 dholland */
389 1.26 dholland if (!FSFMT(vdp) && lfs_dir_gettype(fs, ep) == LFS_DT_WHT) {
390 1.1 dholland slotstatus = FOUND;
391 1.1 dholland slotoffset = results->ulr_offset;
392 1.27 dholland slotsize = lfs_dir_getreclen(fs, ep);
393 1.1 dholland results->ulr_reclen = slotsize;
394 1.1 dholland /*
395 1.1 dholland * This is used to set
396 1.1 dholland * results->ulr_endoff,
397 1.4 dholland * which may be used by ulfs_direnter()
398 1.1 dholland * as a length to truncate the
399 1.1 dholland * directory to. Therefore, it must
400 1.1 dholland * point past the end of the last
401 1.1 dholland * non-empty directory entry. We don't
402 1.1 dholland * know where that is in this case, so
403 1.1 dholland * we effectively disable shrinking by
404 1.1 dholland * using the existing size of the
405 1.1 dholland * directory.
406 1.1 dholland *
407 1.1 dholland * Note that we wouldn't expect to
408 1.1 dholland * shrink the directory while rewriting
409 1.1 dholland * an existing entry anyway.
410 1.1 dholland */
411 1.1 dholland enduseful = endsearch;
412 1.1 dholland cnp->cn_flags |= ISWHITEOUT;
413 1.1 dholland numdirpasses--;
414 1.1 dholland goto notfound;
415 1.1 dholland }
416 1.27 dholland foundino = lfs_dir_getino(fs, ep);
417 1.27 dholland results->ulr_reclen = lfs_dir_getreclen(fs, ep);
418 1.1 dholland goto found;
419 1.1 dholland }
420 1.1 dholland }
421 1.1 dholland prevoff = results->ulr_offset;
422 1.27 dholland results->ulr_offset += lfs_dir_getreclen(fs, ep);
423 1.27 dholland entryoffsetinblock += lfs_dir_getreclen(fs, ep);
424 1.27 dholland if (lfs_dir_getino(fs, ep))
425 1.1 dholland enduseful = results->ulr_offset;
426 1.1 dholland }
427 1.1 dholland notfound:
428 1.1 dholland /*
429 1.1 dholland * If we started in the middle of the directory and failed
430 1.1 dholland * to find our target, we must check the beginning as well.
431 1.1 dholland */
432 1.1 dholland if (numdirpasses == 2) {
433 1.1 dholland numdirpasses--;
434 1.1 dholland results->ulr_offset = 0;
435 1.1 dholland endsearch = results->ulr_diroff;
436 1.1 dholland goto searchloop;
437 1.1 dholland }
438 1.1 dholland if (bp != NULL)
439 1.1 dholland brelse(bp, 0);
440 1.1 dholland /*
441 1.1 dholland * If creating, and at end of pathname and current
442 1.1 dholland * directory has not been removed, then can consider
443 1.1 dholland * allowing file to be created.
444 1.1 dholland */
445 1.1 dholland if ((nameiop == CREATE || nameiop == RENAME ||
446 1.1 dholland (nameiop == DELETE &&
447 1.1 dholland (cnp->cn_flags & DOWHITEOUT) &&
448 1.1 dholland (cnp->cn_flags & ISWHITEOUT))) &&
449 1.1 dholland (flags & ISLASTCN) && dp->i_nlink != 0) {
450 1.1 dholland /*
451 1.1 dholland * Access for write is interpreted as allowing
452 1.1 dholland * creation of files in the directory.
453 1.1 dholland */
454 1.1 dholland error = VOP_ACCESS(vdp, VWRITE, cred);
455 1.1 dholland if (error)
456 1.1 dholland goto out;
457 1.1 dholland /*
458 1.1 dholland * Return an indication of where the new directory
459 1.1 dholland * entry should be put. If we didn't find a slot,
460 1.1 dholland * then set results->ulr_count to 0 indicating
461 1.1 dholland * that the new slot belongs at the end of the
462 1.1 dholland * directory. If we found a slot, then the new entry
463 1.1 dholland * can be put in the range from results->ulr_offset to
464 1.1 dholland * results->ulr_offset + results->ulr_count.
465 1.1 dholland */
466 1.1 dholland if (slotstatus == NONE) {
467 1.1 dholland results->ulr_offset = roundup(dp->i_size, dirblksiz);
468 1.1 dholland results->ulr_count = 0;
469 1.1 dholland enduseful = results->ulr_offset;
470 1.1 dholland } else if (nameiop == DELETE) {
471 1.1 dholland results->ulr_offset = slotoffset;
472 1.1 dholland if ((results->ulr_offset & (dirblksiz - 1)) == 0)
473 1.1 dholland results->ulr_count = 0;
474 1.1 dholland else
475 1.1 dholland results->ulr_count =
476 1.1 dholland results->ulr_offset - prevoff;
477 1.1 dholland } else {
478 1.1 dholland results->ulr_offset = slotoffset;
479 1.1 dholland results->ulr_count = slotsize;
480 1.1 dholland if (enduseful < slotoffset + slotsize)
481 1.1 dholland enduseful = slotoffset + slotsize;
482 1.1 dholland }
483 1.1 dholland results->ulr_endoff = roundup(enduseful, dirblksiz);
484 1.1 dholland #if 0 /* commented out by dbj. none of the on disk fields changed */
485 1.1 dholland dp->i_flag |= IN_CHANGE | IN_UPDATE;
486 1.1 dholland #endif
487 1.1 dholland /*
488 1.1 dholland * We return with the directory locked, so that
489 1.1 dholland * the parameters we set up above will still be
490 1.1 dholland * valid if we actually decide to do a direnter().
491 1.1 dholland * We return ni_vp == NULL to indicate that the entry
492 1.1 dholland * does not currently exist; we leave a pointer to
493 1.1 dholland * the (locked) directory inode in ndp->ni_dvp.
494 1.1 dholland *
495 1.1 dholland * NB - if the directory is unlocked, then this
496 1.1 dholland * information cannot be used.
497 1.1 dholland */
498 1.1 dholland error = EJUSTRETURN;
499 1.1 dholland goto out;
500 1.1 dholland }
501 1.1 dholland /*
502 1.1 dholland * Insert name into cache (as non-existent) if appropriate.
503 1.1 dholland */
504 1.1 dholland if (nameiop != CREATE) {
505 1.1 dholland cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
506 1.1 dholland cnp->cn_flags);
507 1.1 dholland }
508 1.1 dholland error = ENOENT;
509 1.1 dholland goto out;
510 1.1 dholland
511 1.1 dholland found:
512 1.1 dholland if (numdirpasses == 2)
513 1.21 joerg namecache_count_pass2();
514 1.1 dholland /*
515 1.1 dholland * Check that directory length properly reflects presence
516 1.1 dholland * of this entry.
517 1.1 dholland */
518 1.26 dholland if (results->ulr_offset + LFS_DIRSIZ(fs, ep) > dp->i_size) {
519 1.4 dholland ulfs_dirbad(dp, results->ulr_offset, "i_size too small");
520 1.1 dholland dp->i_size =
521 1.26 dholland results->ulr_offset + LFS_DIRSIZ(fs, ep);
522 1.1 dholland DIP_ASSIGN(dp, size, dp->i_size);
523 1.1 dholland dp->i_flag |= IN_CHANGE | IN_UPDATE;
524 1.1 dholland }
525 1.1 dholland brelse(bp, 0);
526 1.1 dholland
527 1.1 dholland /*
528 1.1 dholland * Found component in pathname.
529 1.1 dholland * If the final component of path name, save information
530 1.1 dholland * in the cache as to where the entry was found.
531 1.1 dholland */
532 1.1 dholland if ((flags & ISLASTCN) && nameiop == LOOKUP)
533 1.1 dholland results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
534 1.1 dholland
535 1.1 dholland /*
536 1.1 dholland * If deleting, and at end of pathname, return
537 1.1 dholland * parameters which can be used to remove file.
538 1.1 dholland * Lock the inode, being careful with ".".
539 1.1 dholland */
540 1.1 dholland if (nameiop == DELETE && (flags & ISLASTCN)) {
541 1.1 dholland /*
542 1.1 dholland * Return pointer to current entry in results->ulr_offset,
543 1.1 dholland * and distance past previous entry (if there
544 1.1 dholland * is a previous entry in this block) in results->ulr_count.
545 1.1 dholland * Save directory inode pointer in ndp->ni_dvp for dirremove().
546 1.1 dholland */
547 1.1 dholland if ((results->ulr_offset & (dirblksiz - 1)) == 0)
548 1.1 dholland results->ulr_count = 0;
549 1.1 dholland else
550 1.1 dholland results->ulr_count = results->ulr_offset - prevoff;
551 1.1 dholland if (dp->i_number == foundino) {
552 1.1 dholland vref(vdp);
553 1.1 dholland tdp = vdp;
554 1.1 dholland } else {
555 1.24 hannken error = vcache_get(vdp->v_mount,
556 1.24 hannken &foundino, sizeof(foundino), &tdp);
557 1.1 dholland if (error)
558 1.1 dholland goto out;
559 1.1 dholland }
560 1.1 dholland /*
561 1.1 dholland * Write access to directory required to delete files.
562 1.1 dholland */
563 1.1 dholland error = VOP_ACCESS(vdp, VWRITE, cred);
564 1.1 dholland if (error) {
565 1.24 hannken vrele(tdp);
566 1.1 dholland goto out;
567 1.1 dholland }
568 1.1 dholland /*
569 1.1 dholland * If directory is "sticky", then user must own
570 1.1 dholland * the directory, or the file in it, else she
571 1.1 dholland * may not delete it (unless she's root). This
572 1.1 dholland * implements append-only directories.
573 1.1 dholland */
574 1.1 dholland if (dp->i_mode & ISVTX) {
575 1.1 dholland error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
576 1.1 dholland tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
577 1.1 dholland VTOI(tdp)->i_uid));
578 1.1 dholland if (error) {
579 1.24 hannken vrele(tdp);
580 1.1 dholland error = EPERM;
581 1.1 dholland goto out;
582 1.1 dholland }
583 1.1 dholland }
584 1.1 dholland *vpp = tdp;
585 1.1 dholland error = 0;
586 1.1 dholland goto out;
587 1.1 dholland }
588 1.1 dholland
589 1.1 dholland /*
590 1.1 dholland * If rewriting (RENAME), return the inode and the
591 1.1 dholland * information required to rewrite the present directory
592 1.1 dholland * Must get inode of directory entry to verify it's a
593 1.1 dholland * regular file, or empty directory.
594 1.1 dholland */
595 1.1 dholland if (nameiop == RENAME && (flags & ISLASTCN)) {
596 1.1 dholland error = VOP_ACCESS(vdp, VWRITE, cred);
597 1.1 dholland if (error)
598 1.1 dholland goto out;
599 1.1 dholland /*
600 1.1 dholland * Careful about locking second inode.
601 1.1 dholland * This can only occur if the target is ".".
602 1.1 dholland */
603 1.1 dholland if (dp->i_number == foundino) {
604 1.1 dholland error = EISDIR;
605 1.1 dholland goto out;
606 1.1 dholland }
607 1.24 hannken error = vcache_get(vdp->v_mount,
608 1.24 hannken &foundino, sizeof(foundino), &tdp);
609 1.1 dholland if (error)
610 1.1 dholland goto out;
611 1.1 dholland *vpp = tdp;
612 1.1 dholland error = 0;
613 1.1 dholland goto out;
614 1.1 dholland }
615 1.1 dholland
616 1.24 hannken if (dp->i_number == foundino) {
617 1.1 dholland vref(vdp); /* we want ourself, ie "." */
618 1.1 dholland *vpp = vdp;
619 1.1 dholland } else {
620 1.24 hannken error = vcache_get(vdp->v_mount,
621 1.24 hannken &foundino, sizeof(foundino), &tdp);
622 1.1 dholland if (error)
623 1.1 dholland goto out;
624 1.1 dholland *vpp = tdp;
625 1.1 dholland }
626 1.1 dholland
627 1.1 dholland /*
628 1.1 dholland * Insert name into cache if appropriate.
629 1.1 dholland */
630 1.1 dholland cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
631 1.1 dholland error = 0;
632 1.1 dholland
633 1.1 dholland out:
634 1.1 dholland fstrans_done(vdp->v_mount);
635 1.1 dholland return error;
636 1.1 dholland }
637 1.1 dholland
638 1.1 dholland void
639 1.4 dholland ulfs_dirbad(struct inode *ip, doff_t offset, const char *how)
640 1.1 dholland {
641 1.1 dholland struct mount *mp;
642 1.1 dholland
643 1.1 dholland mp = ITOV(ip)->v_mount;
644 1.1 dholland printf("%s: bad dir ino %llu at offset %d: %s\n",
645 1.1 dholland mp->mnt_stat.f_mntonname, (unsigned long long)ip->i_number,
646 1.1 dholland offset, how);
647 1.25 mlelstv if ((mp->mnt_flag & MNT_RDONLY) == 0)
648 1.1 dholland panic("bad dir");
649 1.1 dholland }
650 1.1 dholland
651 1.1 dholland /*
652 1.1 dholland * Do consistency checking on a directory entry:
653 1.1 dholland * record length must be multiple of 4
654 1.1 dholland * entry must fit in rest of its DIRBLKSIZ block
655 1.1 dholland * record must be large enough to contain entry
656 1.8 dholland * name is not longer than LFS_MAXNAMLEN
657 1.1 dholland * name must be as long as advertised, and null terminated
658 1.1 dholland */
659 1.1 dholland int
660 1.7 dholland ulfs_dirbadentry(struct vnode *dp, struct lfs_direct *ep, int entryoffsetinblock)
661 1.1 dholland {
662 1.1 dholland int i;
663 1.1 dholland int namlen;
664 1.27 dholland unsigned reclen;
665 1.4 dholland struct ulfsmount *ump = VFSTOULFS(dp->v_mount);
666 1.15 dholland struct lfs *fs = ump->um_lfs;
667 1.15 dholland int dirblksiz = fs->um_dirblksiz;
668 1.1 dholland
669 1.26 dholland namlen = lfs_dir_getnamlen(fs, ep);
670 1.27 dholland reclen = lfs_dir_getreclen(fs, ep);
671 1.27 dholland if ((reclen & 0x3) != 0 ||
672 1.27 dholland reclen > dirblksiz - (entryoffsetinblock & (dirblksiz - 1)) ||
673 1.27 dholland reclen < LFS_DIRSIZ(fs, ep) || namlen > LFS_MAXNAMLEN) {
674 1.1 dholland /*return (1); */
675 1.1 dholland printf("First bad, reclen=%#x, DIRSIZ=%lu, namlen=%d, "
676 1.1 dholland "flags=%#x, entryoffsetinblock=%d, dirblksiz = %d\n",
677 1.27 dholland lfs_dir_getreclen(fs, ep),
678 1.26 dholland (u_long)LFS_DIRSIZ(fs, ep),
679 1.1 dholland namlen, dp->v_mount->mnt_flag, entryoffsetinblock,
680 1.1 dholland dirblksiz);
681 1.1 dholland goto bad;
682 1.1 dholland }
683 1.27 dholland if (lfs_dir_getino(fs, ep) == 0)
684 1.1 dholland return (0);
685 1.1 dholland for (i = 0; i < namlen; i++)
686 1.1 dholland if (ep->d_name[i] == '\0') {
687 1.1 dholland /*return (1); */
688 1.1 dholland printf("Second bad\n");
689 1.1 dholland goto bad;
690 1.1 dholland }
691 1.1 dholland if (ep->d_name[i])
692 1.1 dholland goto bad;
693 1.1 dholland return (0);
694 1.1 dholland bad:
695 1.1 dholland return (1);
696 1.1 dholland }
697 1.1 dholland
698 1.1 dholland /*
699 1.29 dholland * Assign the contents of directory entry DIRP, on volume FS.
700 1.29 dholland *
701 1.29 dholland * NAME/NAMLEN is the name, which is not necessarily null terminated.
702 1.29 dholland * INUM is the inode number, and DTYPE is the type code (LFS_DT_*).
703 1.29 dholland *
704 1.29 dholland * Note that these values typically come from:
705 1.29 dholland * cnp->cn_nameptr
706 1.29 dholland * cnp->cn_namelen
707 1.29 dholland * ip->i_number
708 1.29 dholland * LFS_IFTODT(ip->i_mode)
709 1.26 dholland *
710 1.26 dholland * Does not set d_reclen.
711 1.1 dholland */
712 1.28 dholland static void
713 1.29 dholland ulfs_direntry_assign(struct lfs *fs, struct lfs_direct *dirp,
714 1.29 dholland const char *name, size_t namlen,
715 1.29 dholland ino_t inum, unsigned dtype)
716 1.26 dholland {
717 1.29 dholland lfs_dir_setino(fs, dirp, inum);
718 1.29 dholland lfs_dir_setnamlen(fs, dirp, namlen);
719 1.29 dholland lfs_dir_settype(fs, dirp, dtype);
720 1.29 dholland memcpy(dirp->d_name, name, namlen);
721 1.29 dholland dirp->d_name[namlen] = '\0';
722 1.1 dholland }
723 1.1 dholland
724 1.1 dholland /*
725 1.1 dholland * Write a directory entry after a call to namei, using the parameters
726 1.4 dholland * that ulfs_lookup left in nameidata and in the ulfs_lookup_results.
727 1.1 dholland *
728 1.1 dholland * DVP is the directory to be updated. It must be locked.
729 1.4 dholland * ULR is the ulfs_lookup_results structure from the final lookup step.
730 1.1 dholland * TVP is not used. (XXX: why is it here? remove it)
731 1.1 dholland * CNP is the componentname from the final lookup step.
732 1.28 dholland * INUM is the inode number to insert into the new directory entry.
733 1.28 dholland * DTYPE is the type code (LFS_DT_*) to insert into the new directory entry.
734 1.1 dholland * NEWDIRBP is not used and (XXX) should be removed. The previous
735 1.1 dholland * comment here said it was used by the now-removed softupdates code.
736 1.1 dholland *
737 1.1 dholland * The link count of the target inode is *not* incremented; the
738 1.1 dholland * caller does that.
739 1.1 dholland *
740 1.4 dholland * If ulr->ulr_count is 0, ulfs_lookup did not find space to insert the
741 1.1 dholland * directory entry. ulr_offset, which is the place to put the entry,
742 1.1 dholland * should be on a block boundary (and should be at the end of the
743 1.1 dholland * directory AFAIK) and a fresh block is allocated to put the new
744 1.1 dholland * directory entry in.
745 1.1 dholland *
746 1.4 dholland * If ulr->ulr_count is not zero, ulfs_lookup found a slot to insert
747 1.1 dholland * the entry into. This slot ranges from ulr_offset to ulr_offset +
748 1.1 dholland * ulr_count. However, this slot may already be partially populated
749 1.1 dholland * requiring compaction. See notes below.
750 1.1 dholland *
751 1.1 dholland * Furthermore, if ulr_count is not zero and ulr_endoff is not the
752 1.1 dholland * same as i_size, the directory is truncated to size ulr_endoff.
753 1.1 dholland */
754 1.1 dholland int
755 1.4 dholland ulfs_direnter(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
756 1.28 dholland struct vnode *tvp,
757 1.28 dholland struct componentname *cnp, ino_t inum, unsigned dtype,
758 1.28 dholland struct buf *newdirbp)
759 1.1 dholland {
760 1.1 dholland kauth_cred_t cr;
761 1.1 dholland int newentrysize;
762 1.1 dholland struct inode *dp;
763 1.1 dholland struct buf *bp;
764 1.1 dholland u_int dsize;
765 1.7 dholland struct lfs_direct *ep, *nep;
766 1.12 christos int error, ret, lfs_blkoff, loc, spacefree;
767 1.1 dholland char *dirbuf;
768 1.1 dholland struct timespec ts;
769 1.4 dholland struct ulfsmount *ump = VFSTOULFS(dvp->v_mount);
770 1.15 dholland struct lfs *fs = ump->um_lfs;
771 1.15 dholland int dirblksiz = fs->um_dirblksiz;
772 1.29 dholland const char *name;
773 1.29 dholland unsigned namlen, reclen;
774 1.29 dholland #ifdef LFS_DIRHASH
775 1.29 dholland int dohashadd;
776 1.29 dholland #endif
777 1.1 dholland
778 1.1 dholland error = 0;
779 1.29 dholland name = cnp->cn_nameptr; /* note: not null-terminated */
780 1.29 dholland namlen = cnp->cn_namelen;
781 1.1 dholland cr = cnp->cn_cred;
782 1.1 dholland
783 1.1 dholland dp = VTOI(dvp);
784 1.29 dholland newentrysize = LFS_DIRECTSIZ(namlen);
785 1.1 dholland
786 1.1 dholland if (ulr->ulr_count == 0) {
787 1.1 dholland /*
788 1.1 dholland * If ulr_count is 0, then namei could find no
789 1.1 dholland * space in the directory. Here, ulr_offset will
790 1.1 dholland * be on a directory block boundary and we will write the
791 1.1 dholland * new entry into a fresh block.
792 1.1 dholland */
793 1.1 dholland if (ulr->ulr_offset & (dirblksiz - 1))
794 1.4 dholland panic("ulfs_direnter: newblk");
795 1.14 dholland if ((error = lfs_balloc(dvp, (off_t)ulr->ulr_offset, dirblksiz,
796 1.1 dholland cr, B_CLRBUF | B_SYNC, &bp)) != 0) {
797 1.1 dholland return (error);
798 1.1 dholland }
799 1.1 dholland dp->i_size = ulr->ulr_offset + dirblksiz;
800 1.1 dholland DIP_ASSIGN(dp, size, dp->i_size);
801 1.1 dholland dp->i_flag |= IN_CHANGE | IN_UPDATE;
802 1.1 dholland uvm_vnp_setsize(dvp, dp->i_size);
803 1.12 christos lfs_blkoff = ulr->ulr_offset & (ump->um_mountp->mnt_stat.f_iosize - 1);
804 1.29 dholland ep = (struct lfs_direct *)((char *)bp->b_data + lfs_blkoff);
805 1.29 dholland ulfs_direntry_assign(fs, ep, name, namlen, inum, dtype);
806 1.29 dholland lfs_dir_setreclen(fs, ep, dirblksiz);
807 1.3 dholland #ifdef LFS_DIRHASH
808 1.1 dholland if (dp->i_dirhash != NULL) {
809 1.4 dholland ulfsdirhash_newblk(dp, ulr->ulr_offset);
810 1.29 dholland ulfsdirhash_add(dp, ep, ulr->ulr_offset);
811 1.12 christos ulfsdirhash_checkblock(dp, (char *)bp->b_data + lfs_blkoff,
812 1.1 dholland ulr->ulr_offset);
813 1.1 dholland }
814 1.1 dholland #endif
815 1.1 dholland error = VOP_BWRITE(bp->b_vp, bp);
816 1.1 dholland vfs_timestamp(&ts);
817 1.14 dholland ret = lfs_update(dvp, &ts, &ts, UPDATE_DIROP);
818 1.1 dholland if (error == 0)
819 1.1 dholland return (ret);
820 1.1 dholland return (error);
821 1.1 dholland }
822 1.1 dholland
823 1.1 dholland /*
824 1.1 dholland * If ulr_count is non-zero, then namei found space for the new
825 1.1 dholland * entry in the range ulr_offset to ulr_offset + ulr_count
826 1.1 dholland * in the directory. To use this space, we may have to compact
827 1.1 dholland * the entries located there, by copying them together towards the
828 1.1 dholland * beginning of the block, leaving the free space in one usable
829 1.1 dholland * chunk at the end.
830 1.1 dholland */
831 1.1 dholland
832 1.1 dholland /*
833 1.1 dholland * Increase size of directory if entry eats into new space.
834 1.1 dholland * This should never push the size past a new multiple of
835 1.1 dholland * DIRBLKSIZ.
836 1.1 dholland *
837 1.1 dholland * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
838 1.1 dholland */
839 1.1 dholland if (ulr->ulr_offset + ulr->ulr_count > dp->i_size) {
840 1.1 dholland #ifdef DIAGNOSTIC
841 1.4 dholland printf("ulfs_direnter: reached 4.2-only block, "
842 1.1 dholland "not supposed to happen\n");
843 1.1 dholland #endif
844 1.1 dholland dp->i_size = ulr->ulr_offset + ulr->ulr_count;
845 1.1 dholland DIP_ASSIGN(dp, size, dp->i_size);
846 1.1 dholland dp->i_flag |= IN_CHANGE | IN_UPDATE;
847 1.1 dholland }
848 1.1 dholland /*
849 1.1 dholland * Get the block containing the space for the new directory entry.
850 1.1 dholland */
851 1.4 dholland error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp, true);
852 1.1 dholland if (error) {
853 1.1 dholland return (error);
854 1.1 dholland }
855 1.1 dholland /*
856 1.1 dholland * Find space for the new entry. In the simple case, the entry at
857 1.1 dholland * offset base will have the space. If it does not, then namei
858 1.1 dholland * arranged that compacting the region ulr_offset to
859 1.1 dholland * ulr_offset + ulr_count would yield the space.
860 1.1 dholland */
861 1.7 dholland ep = (struct lfs_direct *)dirbuf;
862 1.27 dholland dsize = (lfs_dir_getino(fs, ep) != 0) ? LFS_DIRSIZ(fs, ep) : 0;
863 1.27 dholland spacefree = lfs_dir_getreclen(fs, ep) - dsize;
864 1.27 dholland for (loc = lfs_dir_getreclen(fs, ep); loc < ulr->ulr_count; ) {
865 1.7 dholland nep = (struct lfs_direct *)(dirbuf + loc);
866 1.1 dholland
867 1.1 dholland /* Trim the existing slot (NB: dsize may be zero). */
868 1.27 dholland lfs_dir_setreclen(fs, ep, dsize);
869 1.27 dholland ep = LFS_NEXTDIR(fs, ep);
870 1.1 dholland
871 1.27 dholland reclen = lfs_dir_getreclen(fs, nep);
872 1.1 dholland loc += reclen;
873 1.27 dholland if (lfs_dir_getino(fs, nep) == 0) {
874 1.1 dholland /*
875 1.1 dholland * A mid-block unused entry. Such entries are
876 1.1 dholland * never created by the kernel, but fsck_ffs
877 1.1 dholland * can create them (and it doesn't fix them).
878 1.1 dholland *
879 1.1 dholland * Add up the free space, and initialise the
880 1.1 dholland * relocated entry since we don't memcpy it.
881 1.1 dholland */
882 1.1 dholland spacefree += reclen;
883 1.27 dholland lfs_dir_setino(fs, ep, 0);
884 1.1 dholland dsize = 0;
885 1.1 dholland continue;
886 1.1 dholland }
887 1.26 dholland dsize = LFS_DIRSIZ(fs, nep);
888 1.1 dholland spacefree += reclen - dsize;
889 1.3 dholland #ifdef LFS_DIRHASH
890 1.1 dholland if (dp->i_dirhash != NULL)
891 1.4 dholland ulfsdirhash_move(dp, nep,
892 1.1 dholland ulr->ulr_offset + ((char *)nep - dirbuf),
893 1.1 dholland ulr->ulr_offset + ((char *)ep - dirbuf));
894 1.1 dholland #endif
895 1.1 dholland memcpy((void *)ep, (void *)nep, dsize);
896 1.1 dholland }
897 1.1 dholland /*
898 1.1 dholland * Here, `ep' points to a directory entry containing `dsize' in-use
899 1.1 dholland * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
900 1.1 dholland * then the entry is completely unused (dsize == 0). The value
901 1.1 dholland * of ep->d_reclen is always indeterminate.
902 1.1 dholland *
903 1.1 dholland * Update the pointer fields in the previous entry (if any),
904 1.1 dholland * copy in the new entry, and write out the block.
905 1.1 dholland */
906 1.27 dholland if (lfs_dir_getino(fs, ep) == 0 ||
907 1.27 dholland (lfs_dir_getino(fs, ep) == ULFS_WINO &&
908 1.29 dholland memcmp(ep->d_name, name, namlen) == 0)) {
909 1.1 dholland if (spacefree + dsize < newentrysize)
910 1.4 dholland panic("ulfs_direnter: compact1");
911 1.29 dholland reclen = spacefree + dsize;
912 1.29 dholland #ifdef LFS_DIRHASH
913 1.29 dholland dohashadd = (lfs_dir_getino(fs, ep) == 0);
914 1.29 dholland #endif
915 1.1 dholland } else {
916 1.1 dholland if (spacefree < newentrysize)
917 1.4 dholland panic("ulfs_direnter: compact2");
918 1.29 dholland reclen = spacefree;
919 1.27 dholland lfs_dir_setreclen(fs, ep, dsize);
920 1.27 dholland ep = LFS_NEXTDIR(fs, ep);
921 1.29 dholland #ifdef LFS_DIRHASH
922 1.29 dholland dohashadd = 1;
923 1.29 dholland #endif
924 1.27 dholland }
925 1.27 dholland
926 1.29 dholland ulfs_direntry_assign(fs, ep, name, namlen, inum, dtype);
927 1.29 dholland lfs_dir_setreclen(fs, ep, reclen);
928 1.3 dholland #ifdef LFS_DIRHASH
929 1.29 dholland if (dp->i_dirhash != NULL && dohashadd)
930 1.29 dholland ulfsdirhash_add(dp, ep, ulr->ulr_offset + ((char *)ep - dirbuf));
931 1.1 dholland if (dp->i_dirhash != NULL)
932 1.4 dholland ulfsdirhash_checkblock(dp, dirbuf -
933 1.1 dholland (ulr->ulr_offset & (dirblksiz - 1)),
934 1.1 dholland ulr->ulr_offset & ~(dirblksiz - 1));
935 1.1 dholland #endif
936 1.1 dholland error = VOP_BWRITE(bp->b_vp, bp);
937 1.1 dholland dp->i_flag |= IN_CHANGE | IN_UPDATE;
938 1.1 dholland /*
939 1.1 dholland * If all went well, and the directory can be shortened, proceed
940 1.1 dholland * with the truncation. Note that we have to unlock the inode for
941 1.1 dholland * the entry that we just entered, as the truncation may need to
942 1.1 dholland * lock other inodes which can lead to deadlock if we also hold a
943 1.1 dholland * lock on the newly entered node.
944 1.1 dholland */
945 1.1 dholland if (error == 0 && ulr->ulr_endoff && ulr->ulr_endoff < dp->i_size) {
946 1.3 dholland #ifdef LFS_DIRHASH
947 1.1 dholland if (dp->i_dirhash != NULL)
948 1.4 dholland ulfsdirhash_dirtrunc(dp, ulr->ulr_endoff);
949 1.1 dholland #endif
950 1.14 dholland (void) lfs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC, cr);
951 1.1 dholland }
952 1.1 dholland return (error);
953 1.1 dholland }
954 1.1 dholland
955 1.1 dholland /*
956 1.1 dholland * Remove a directory entry after a call to namei, using the
957 1.4 dholland * parameters that ulfs_lookup left in nameidata and in the
958 1.4 dholland * ulfs_lookup_results.
959 1.1 dholland *
960 1.1 dholland * DVP is the directory to be updated. It must be locked.
961 1.4 dholland * ULR is the ulfs_lookup_results structure from the final lookup step.
962 1.1 dholland * IP, if not null, is the inode being unlinked.
963 1.1 dholland * FLAGS may contain DOWHITEOUT.
964 1.1 dholland * ISRMDIR is not used and (XXX) should be removed.
965 1.1 dholland *
966 1.1 dholland * If FLAGS contains DOWHITEOUT the entry is replaced with a whiteout
967 1.1 dholland * instead of being cleared.
968 1.1 dholland *
969 1.1 dholland * ulr->ulr_offset contains the position of the directory entry
970 1.1 dholland * to be removed.
971 1.1 dholland *
972 1.1 dholland * ulr->ulr_reclen contains the size of the directory entry to be
973 1.1 dholland * removed.
974 1.1 dholland *
975 1.1 dholland * ulr->ulr_count contains the size of the *previous* directory
976 1.1 dholland * entry. This allows finding it, for free space management. If
977 1.1 dholland * ulr_count is 0, the target entry is at the beginning of the
978 1.1 dholland * directory. (Does this ever happen? The first entry should be ".",
979 1.1 dholland * which should only be removed at rmdir time. Does rmdir come here
980 1.1 dholland * to clear out the "." and ".." entries? Perhaps, but I doubt it.)
981 1.1 dholland *
982 1.1 dholland * The space is marked free by adding it to the record length (not
983 1.1 dholland * name length) of the preceding entry. If the first entry becomes
984 1.1 dholland * free, it is marked free by setting the inode number to 0.
985 1.1 dholland *
986 1.1 dholland * The link count of IP is decremented. Note that this is not the
987 1.4 dholland * inverse behavior of ulfs_direnter, which does not adjust link
988 1.1 dholland * counts. Sigh.
989 1.1 dholland */
990 1.1 dholland int
991 1.4 dholland ulfs_dirremove(struct vnode *dvp, const struct ulfs_lookup_results *ulr,
992 1.1 dholland struct inode *ip, int flags, int isrmdir)
993 1.1 dholland {
994 1.1 dholland struct inode *dp = VTOI(dvp);
995 1.26 dholland struct lfs *fs = dp->i_lfs;
996 1.7 dholland struct lfs_direct *ep;
997 1.1 dholland struct buf *bp;
998 1.1 dholland int error;
999 1.1 dholland
1000 1.1 dholland if (flags & DOWHITEOUT) {
1001 1.1 dholland /*
1002 1.4 dholland * Whiteout entry: set d_ino to ULFS_WINO.
1003 1.1 dholland */
1004 1.4 dholland error = ulfs_blkatoff(dvp, (off_t)ulr->ulr_offset, (void *)&ep,
1005 1.1 dholland &bp, true);
1006 1.1 dholland if (error)
1007 1.1 dholland return (error);
1008 1.27 dholland lfs_dir_setino(fs, ep, ULFS_WINO);
1009 1.26 dholland lfs_dir_settype(fs, ep, LFS_DT_WHT);
1010 1.1 dholland goto out;
1011 1.1 dholland }
1012 1.1 dholland
1013 1.4 dholland if ((error = ulfs_blkatoff(dvp,
1014 1.1 dholland (off_t)(ulr->ulr_offset - ulr->ulr_count), (void *)&ep, &bp, true)) != 0)
1015 1.1 dholland return (error);
1016 1.1 dholland
1017 1.3 dholland #ifdef LFS_DIRHASH
1018 1.1 dholland /*
1019 1.1 dholland * Remove the dirhash entry. This is complicated by the fact
1020 1.1 dholland * that `ep' is the previous entry when ulr_count != 0.
1021 1.1 dholland */
1022 1.1 dholland if (dp->i_dirhash != NULL)
1023 1.4 dholland ulfsdirhash_remove(dp, (ulr->ulr_count == 0) ? ep :
1024 1.27 dholland LFS_NEXTDIR(fs, ep), ulr->ulr_offset);
1025 1.1 dholland #endif
1026 1.1 dholland
1027 1.1 dholland if (ulr->ulr_count == 0) {
1028 1.1 dholland /*
1029 1.1 dholland * First entry in block: set d_ino to zero.
1030 1.1 dholland */
1031 1.27 dholland lfs_dir_setino(fs, ep, 0);
1032 1.1 dholland } else {
1033 1.1 dholland /*
1034 1.1 dholland * Collapse new free space into previous entry.
1035 1.1 dholland */
1036 1.27 dholland lfs_dir_setreclen(fs, ep,
1037 1.27 dholland lfs_dir_getreclen(fs, ep) + ulr->ulr_reclen);
1038 1.1 dholland }
1039 1.1 dholland
1040 1.3 dholland #ifdef LFS_DIRHASH
1041 1.1 dholland if (dp->i_dirhash != NULL) {
1042 1.15 dholland int dirblksiz = ip->i_lfs->um_dirblksiz;
1043 1.4 dholland ulfsdirhash_checkblock(dp, (char *)ep -
1044 1.1 dholland ((ulr->ulr_offset - ulr->ulr_count) & (dirblksiz - 1)),
1045 1.1 dholland ulr->ulr_offset & ~(dirblksiz - 1));
1046 1.1 dholland }
1047 1.1 dholland #endif
1048 1.1 dholland
1049 1.1 dholland out:
1050 1.1 dholland if (ip) {
1051 1.1 dholland ip->i_nlink--;
1052 1.1 dholland DIP_ASSIGN(ip, nlink, ip->i_nlink);
1053 1.1 dholland ip->i_flag |= IN_CHANGE;
1054 1.1 dholland }
1055 1.1 dholland /*
1056 1.1 dholland * XXX did it ever occur to anyone that it might be a good
1057 1.1 dholland * idea to restore ip->i_nlink if this fails? Or something?
1058 1.1 dholland * Currently on error return from this function the state of
1059 1.1 dholland * ip->i_nlink depends on what happened, and callers
1060 1.1 dholland * definitely do not take this into account.
1061 1.1 dholland */
1062 1.1 dholland error = VOP_BWRITE(bp->b_vp, bp);
1063 1.1 dholland dp->i_flag |= IN_CHANGE | IN_UPDATE;
1064 1.1 dholland /*
1065 1.1 dholland * If the last named reference to a snapshot goes away,
1066 1.1 dholland * drop its snapshot reference so that it will be reclaimed
1067 1.1 dholland * when last open reference goes away.
1068 1.1 dholland */
1069 1.1 dholland if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 &&
1070 1.1 dholland ip->i_nlink == 0)
1071 1.6 dholland ulfs_snapgone(ip);
1072 1.1 dholland return (error);
1073 1.1 dholland }
1074 1.1 dholland
1075 1.1 dholland /*
1076 1.1 dholland * Rewrite an existing directory entry to point at the inode supplied.
1077 1.1 dholland *
1078 1.1 dholland * DP is the directory to update.
1079 1.1 dholland * OFFSET is the position of the entry in question. It may come
1080 1.4 dholland * from ulr_offset of a ulfs_lookup_results.
1081 1.1 dholland * OIP is the old inode the directory previously pointed to.
1082 1.1 dholland * NEWINUM is the number of the new inode.
1083 1.1 dholland * NEWTYPE is the new value for the type field of the directory entry.
1084 1.1 dholland * (This is ignored if the fs doesn't support that.)
1085 1.1 dholland * ISRMDIR is not used and (XXX) should be removed.
1086 1.1 dholland * IFLAGS are added to DP's inode flags.
1087 1.1 dholland *
1088 1.1 dholland * The link count of OIP is decremented. Note that the link count of
1089 1.1 dholland * the new inode is *not* incremented. Yay for symmetry.
1090 1.1 dholland */
1091 1.1 dholland int
1092 1.4 dholland ulfs_dirrewrite(struct inode *dp, off_t offset,
1093 1.1 dholland struct inode *oip, ino_t newinum, int newtype,
1094 1.1 dholland int isrmdir, int iflags)
1095 1.1 dholland {
1096 1.26 dholland struct lfs *fs = dp->i_lfs;
1097 1.1 dholland struct buf *bp;
1098 1.7 dholland struct lfs_direct *ep;
1099 1.1 dholland struct vnode *vdp = ITOV(dp);
1100 1.1 dholland int error;
1101 1.1 dholland
1102 1.4 dholland error = ulfs_blkatoff(vdp, offset, (void *)&ep, &bp, true);
1103 1.1 dholland if (error)
1104 1.1 dholland return (error);
1105 1.27 dholland lfs_dir_setino(fs, ep, newinum);
1106 1.26 dholland lfs_dir_settype(fs, ep, newtype);
1107 1.1 dholland oip->i_nlink--;
1108 1.1 dholland DIP_ASSIGN(oip, nlink, oip->i_nlink);
1109 1.1 dholland oip->i_flag |= IN_CHANGE;
1110 1.1 dholland error = VOP_BWRITE(bp->b_vp, bp);
1111 1.1 dholland dp->i_flag |= iflags;
1112 1.1 dholland /*
1113 1.1 dholland * If the last named reference to a snapshot goes away,
1114 1.1 dholland * drop its snapshot reference so that it will be reclaimed
1115 1.1 dholland * when last open reference goes away.
1116 1.1 dholland */
1117 1.1 dholland if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_nlink == 0)
1118 1.6 dholland ulfs_snapgone(oip);
1119 1.1 dholland return (error);
1120 1.1 dholland }
1121 1.1 dholland
1122 1.1 dholland /*
1123 1.1 dholland * Check if a directory is empty or not.
1124 1.1 dholland * Inode supplied must be locked.
1125 1.1 dholland *
1126 1.7 dholland * Using a struct lfs_dirtemplate here is not precisely
1127 1.7 dholland * what we want, but better than using a struct lfs_direct.
1128 1.1 dholland *
1129 1.1 dholland * NB: does not handle corrupted directories.
1130 1.1 dholland */
1131 1.1 dholland int
1132 1.4 dholland ulfs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
1133 1.1 dholland {
1134 1.26 dholland struct lfs *fs = ip->i_lfs;
1135 1.1 dholland doff_t off;
1136 1.7 dholland struct lfs_dirtemplate dbuf;
1137 1.7 dholland struct lfs_direct *dp = (struct lfs_direct *)&dbuf;
1138 1.1 dholland int error, namlen;
1139 1.1 dholland size_t count;
1140 1.7 dholland #define MINDIRSIZ (sizeof (struct lfs_dirtemplate) / 2)
1141 1.1 dholland
1142 1.27 dholland for (off = 0; off < ip->i_size; off += lfs_dir_getreclen(fs, dp)) {
1143 1.22 riastrad error = ulfs_bufio(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ,
1144 1.22 riastrad off, IO_NODELOCKED, cred, &count, NULL);
1145 1.1 dholland /*
1146 1.1 dholland * Since we read MINDIRSIZ, residual must
1147 1.1 dholland * be 0 unless we're at end of file.
1148 1.1 dholland */
1149 1.1 dholland if (error || count != 0)
1150 1.1 dholland return (0);
1151 1.1 dholland /* avoid infinite loops */
1152 1.27 dholland if (lfs_dir_getreclen(fs, dp) == 0)
1153 1.1 dholland return (0);
1154 1.1 dholland /* skip empty entries */
1155 1.27 dholland if (lfs_dir_getino(fs, dp) == 0 ||
1156 1.27 dholland lfs_dir_getino(fs, dp) == ULFS_WINO)
1157 1.1 dholland continue;
1158 1.1 dholland /* accept only "." and ".." */
1159 1.26 dholland namlen = lfs_dir_getnamlen(fs, dp);
1160 1.1 dholland if (namlen > 2)
1161 1.1 dholland return (0);
1162 1.1 dholland if (dp->d_name[0] != '.')
1163 1.1 dholland return (0);
1164 1.1 dholland /*
1165 1.1 dholland * At this point namlen must be 1 or 2.
1166 1.1 dholland * 1 implies ".", 2 implies ".." if second
1167 1.1 dholland * char is also "."
1168 1.1 dholland */
1169 1.27 dholland if (namlen == 1 && lfs_dir_getino(fs, dp) == ip->i_number)
1170 1.1 dholland continue;
1171 1.27 dholland if (dp->d_name[1] == '.' && lfs_dir_getino(fs, dp) == parentino)
1172 1.1 dholland continue;
1173 1.1 dholland return (0);
1174 1.1 dholland }
1175 1.1 dholland return (1);
1176 1.1 dholland }
1177 1.1 dholland
1178 1.4 dholland #define ULFS_DIRRABLKS 0
1179 1.4 dholland int ulfs_dirrablks = ULFS_DIRRABLKS;
1180 1.1 dholland
1181 1.1 dholland /*
1182 1.4 dholland * ulfs_blkatoff: Return buffer with the contents of block "offset" from
1183 1.1 dholland * the beginning of directory "vp". If "res" is non-NULL, fill it in with
1184 1.1 dholland * a pointer to the remaining space in the directory. If the caller intends
1185 1.1 dholland * to modify the buffer returned, "modify" must be true.
1186 1.1 dholland */
1187 1.1 dholland
1188 1.1 dholland int
1189 1.4 dholland ulfs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp,
1190 1.1 dholland bool modify)
1191 1.1 dholland {
1192 1.17 martin struct inode *ip __diagused;
1193 1.1 dholland struct buf *bp;
1194 1.1 dholland daddr_t lbn;
1195 1.4 dholland const int dirrablks = ulfs_dirrablks;
1196 1.1 dholland daddr_t *blks;
1197 1.1 dholland int *blksizes;
1198 1.1 dholland int run, error;
1199 1.1 dholland struct mount *mp = vp->v_mount;
1200 1.1 dholland const int bshift = mp->mnt_fs_bshift;
1201 1.1 dholland const int bsize = 1 << bshift;
1202 1.1 dholland off_t eof;
1203 1.1 dholland
1204 1.1 dholland blks = kmem_alloc((1 + dirrablks) * sizeof(daddr_t), KM_SLEEP);
1205 1.1 dholland blksizes = kmem_alloc((1 + dirrablks) * sizeof(int), KM_SLEEP);
1206 1.1 dholland ip = VTOI(vp);
1207 1.1 dholland KASSERT(vp->v_size == ip->i_size);
1208 1.1 dholland GOP_SIZE(vp, vp->v_size, &eof, 0);
1209 1.1 dholland lbn = offset >> bshift;
1210 1.1 dholland
1211 1.1 dholland for (run = 0; run <= dirrablks;) {
1212 1.1 dholland const off_t curoff = lbn << bshift;
1213 1.1 dholland const int size = MIN(eof - curoff, bsize);
1214 1.1 dholland
1215 1.1 dholland if (size == 0) {
1216 1.1 dholland break;
1217 1.1 dholland }
1218 1.1 dholland KASSERT(curoff < eof);
1219 1.1 dholland blks[run] = lbn;
1220 1.1 dholland blksizes[run] = size;
1221 1.1 dholland lbn++;
1222 1.1 dholland run++;
1223 1.1 dholland if (size != bsize) {
1224 1.1 dholland break;
1225 1.1 dholland }
1226 1.1 dholland }
1227 1.1 dholland KASSERT(run >= 1);
1228 1.1 dholland error = breadn(vp, blks[0], blksizes[0], &blks[1], &blksizes[1],
1229 1.23 maxv run - 1, (modify ? B_MODIFY : 0), &bp);
1230 1.1 dholland if (error != 0) {
1231 1.1 dholland *bpp = NULL;
1232 1.1 dholland goto out;
1233 1.1 dholland }
1234 1.1 dholland if (res) {
1235 1.1 dholland *res = (char *)bp->b_data + (offset & (bsize - 1));
1236 1.1 dholland }
1237 1.1 dholland *bpp = bp;
1238 1.1 dholland
1239 1.1 dholland out:
1240 1.1 dholland kmem_free(blks, (1 + dirrablks) * sizeof(daddr_t));
1241 1.1 dholland kmem_free(blksizes, (1 + dirrablks) * sizeof(int));
1242 1.1 dholland return error;
1243 1.1 dholland }
1244