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