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