1 /*-
2 * modified for Lites 1.1
3 *
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
6 */
7 /*-
8 * SPDX-License-Identifier: BSD-3-Clause
9 *
10 * Copyright (c) 1989, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
43 * $FreeBSD$
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/endian.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/sdt.h>
57 #include <sys/sysctl.h>
58
59 #include <ufs/ufs/dir.h>
60
61 #include <fs/ext2fs/fs.h>
62 #include <fs/ext2fs/inode.h>
63 #include <fs/ext2fs/ext2_mount.h>
64 #include <fs/ext2fs/ext2fs.h>
65 #include <fs/ext2fs/ext2_dinode.h>
66 #include <fs/ext2fs/ext2_dir.h>
67 #include <fs/ext2fs/ext2_extern.h>
68 #include <fs/ext2fs/fs.h>
69
70 SDT_PROVIDER_DECLARE(ext2fs);
71 /*
72 * ext2fs trace probe:
73 * arg0: verbosity. Higher numbers give more verbose messages
74 * arg1: Textual message
75 */
76 SDT_PROBE_DEFINE2(ext2fs, , lookup, trace, "int", "char*");
77 SDT_PROBE_DEFINE4(ext2fs, , trace, ext2_dirbad_error,
78 "char*", "ino_t", "doff_t", "char*");
79 SDT_PROBE_DEFINE5(ext2fs, , trace, ext2_dirbadentry_error,
80 "char*", "int", "uint32_t", "uint16_t", "uint8_t");
81
82 #ifdef INVARIANTS
83 static int dirchk = 1;
84 #else
85 static int dirchk = 0;
86 #endif
87
88 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
89 "EXT2FS filesystem");
90 SYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
91
92 /*
93 DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
94 while it is the native blocksize in ext2fs - thus, a #define
95 is no longer appropriate
96 */
97 #undef DIRBLKSIZ
98
99 static u_char ext2_ft_to_dt[] = {
100 DT_UNKNOWN, /* EXT2_FT_UNKNOWN */
101 DT_REG, /* EXT2_FT_REG_FILE */
102 DT_DIR, /* EXT2_FT_DIR */
103 DT_CHR, /* EXT2_FT_CHRDEV */
104 DT_BLK, /* EXT2_FT_BLKDEV */
105 DT_FIFO, /* EXT2_FT_FIFO */
106 DT_SOCK, /* EXT2_FT_SOCK */
107 DT_LNK, /* EXT2_FT_SYMLINK */
108 };
109 #define FTTODT(ft) \
110 ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
111
112 static u_char dt_to_ext2_ft[] = {
113 EXT2_FT_UNKNOWN, /* DT_UNKNOWN */
114 EXT2_FT_FIFO, /* DT_FIFO */
115 EXT2_FT_CHRDEV, /* DT_CHR */
116 EXT2_FT_UNKNOWN, /* unused */
117 EXT2_FT_DIR, /* DT_DIR */
118 EXT2_FT_UNKNOWN, /* unused */
119 EXT2_FT_BLKDEV, /* DT_BLK */
120 EXT2_FT_UNKNOWN, /* unused */
121 EXT2_FT_REG_FILE, /* DT_REG */
122 EXT2_FT_UNKNOWN, /* unused */
123 EXT2_FT_SYMLINK, /* DT_LNK */
124 EXT2_FT_UNKNOWN, /* unused */
125 EXT2_FT_SOCK, /* DT_SOCK */
126 EXT2_FT_UNKNOWN, /* unused */
127 EXT2_FT_UNKNOWN, /* DT_WHT */
128 };
129 #define DTTOFT(dt) \
130 ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
131
132 static int ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
133 int entryoffsetinblock);
134 static int ext2_is_dot_entry(struct componentname *cnp);
135 static int ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
136 struct componentname *cnp, ino_t *dd_ino);
137
138 static int
139 ext2_is_dot_entry(struct componentname *cnp)
140 {
141 if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
142 (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0'))
143 return (1);
144 return (0);
145 }
146
147 /*
148 * Vnode op for reading directories.
149 */
150 int
151 ext2_readdir(struct vop_readdir_args *ap)
152 {
153 struct vnode *vp = ap->a_vp;
154 struct uio *uio = ap->a_uio;
155 struct buf *bp;
156 struct inode *ip;
157 struct ext2fs_direct_2 *dp, *edp;
158 u_long *cookies;
159 struct dirent dstdp;
160 off_t offset, startoffset;
161 size_t readcnt, skipcnt;
162 ssize_t startresid;
163 u_int ncookies;
164 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
165 int error;
166
167 if (uio->uio_offset < 0)
168 return (EINVAL);
169 ip = VTOI(vp);
170 if (ap->a_ncookies != NULL) {
171 if (uio->uio_resid < 0)
172 ncookies = 0;
173 else
174 ncookies = uio->uio_resid;
175 if (uio->uio_offset >= ip->i_size)
176 ncookies = 0;
177 else if (ip->i_size - uio->uio_offset < ncookies)
178 ncookies = ip->i_size - uio->uio_offset;
179 ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
180 e2d_namlen) + 4) + 1;
181 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
182 *ap->a_ncookies = ncookies;
183 *ap->a_cookies = cookies;
184 } else {
185 ncookies = 0;
186 cookies = NULL;
187 }
188 offset = startoffset = uio->uio_offset;
189 startresid = uio->uio_resid;
190 error = 0;
191 while (error == 0 && uio->uio_resid > 0 &&
192 uio->uio_offset < ip->i_size) {
193 error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
194 if (error)
195 break;
196 if (bp->b_offset + bp->b_bcount > ip->i_size)
197 readcnt = ip->i_size - bp->b_offset;
198 else
199 readcnt = bp->b_bcount;
200 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
201 ~(size_t)(DIRBLKSIZ - 1);
202 offset = bp->b_offset + skipcnt;
203 dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
204 edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
205 while (error == 0 && uio->uio_resid > 0 && dp < edp) {
206 if (le16toh(dp->e2d_reclen) <= offsetof(struct ext2fs_direct_2,
207 e2d_namlen) || (caddr_t)dp + le16toh(dp->e2d_reclen) >
208 (caddr_t)edp) {
209 error = EIO;
210 break;
211 }
212 /*-
213 * "New" ext2fs directory entries differ in 3 ways
214 * from ufs on-disk ones:
215 * - the name is not necessarily NUL-terminated.
216 * - the file type field always exists and always
217 * follows the name length field.
218 * - the file type is encoded in a different way.
219 *
220 * "Old" ext2fs directory entries need no special
221 * conversions, since they are binary compatible
222 * with "new" entries having a file type of 0 (i.e.,
223 * EXT2_FT_UNKNOWN). Splitting the old name length
224 * field didn't make a mess like it did in ufs,
225 * because ext2fs uses a machine-independent disk
226 * layout.
227 */
228 dstdp.d_namlen = dp->e2d_namlen;
229 dstdp.d_type = FTTODT(dp->e2d_type);
230 if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
231 dstdp.d_namlen > le16toh(dp->e2d_reclen)) {
232 error = EIO;
233 break;
234 }
235 if (offset < startoffset || le32toh(dp->e2d_ino) == 0)
236 goto nextentry;
237 dstdp.d_fileno = le32toh(dp->e2d_ino);
238 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
239 bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
240 /* NOTE: d_off is the offset of the *next* entry. */
241 dstdp.d_off = offset + le16toh(dp->e2d_reclen);
242 dirent_terminate(&dstdp);
243 if (dstdp.d_reclen > uio->uio_resid) {
244 if (uio->uio_resid == startresid)
245 error = EINVAL;
246 else
247 error = EJUSTRETURN;
248 break;
249 }
250 /* Advance dp. */
251 error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
252 if (error)
253 break;
254 if (cookies != NULL) {
255 KASSERT(ncookies > 0,
256 ("ext2_readdir: cookies buffer too small"));
257 *cookies = offset + le16toh(dp->e2d_reclen);
258 cookies++;
259 ncookies--;
260 }
261 nextentry:
262 offset += le16toh(dp->e2d_reclen);
263 dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
264 le16toh(dp->e2d_reclen));
265 }
266 bqrelse(bp);
267 uio->uio_offset = offset;
268 }
269 /* We need to correct uio_offset. */
270 uio->uio_offset = offset;
271 if (error == EJUSTRETURN)
272 error = 0;
273 if (ap->a_ncookies != NULL) {
274 if (error == 0) {
275 ap->a_ncookies -= ncookies;
276 } else {
277 free(*ap->a_cookies, M_TEMP);
278 *ap->a_ncookies = 0;
279 *ap->a_cookies = NULL;
280 }
281 }
282 if (error == 0 && ap->a_eofflag)
283 *ap->a_eofflag = ip->i_size <= uio->uio_offset;
284 return (error);
285 }
286
287 /*
288 * Convert a component of a pathname into a pointer to a locked inode.
289 * This is a very central and rather complicated routine.
290 * If the file system is not maintained in a strict tree hierarchy,
291 * this can result in a deadlock situation (see comments in code below).
292 *
293 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
294 * on whether the name is to be looked up, created, renamed, or deleted.
295 * When CREATE, RENAME, or DELETE is specified, information usable in
296 * creating, renaming, or deleting a directory entry may be calculated.
297 * If flag has LOCKPARENT or'ed into it and the target of the pathname
298 * exists, lookup returns both the target and its parent directory locked.
299 * When creating or renaming and LOCKPARENT is specified, the target may
300 * not be ".". When deleting and LOCKPARENT is specified, the target may
301 * be "."., but the caller must check to ensure it does an vrele and vput
302 * instead of two vputs.
303 *
304 * Overall outline of ext2_lookup:
305 *
306 * search for name in directory, to found or notfound
307 * notfound:
308 * if creating, return locked directory, leaving info on available slots
309 * else return error
310 * found:
311 * if at end of path and deleting, return information to allow delete
312 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
313 * inode and return info to allow rewrite
314 * if not at end, add name to cache; if at end and neither creating
315 * nor deleting, add name to cache
316 */
317 int
318 ext2_lookup(struct vop_cachedlookup_args *ap)
319 {
320
321 return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
322 }
323
324 static int
325 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
326 ino_t *dd_ino)
327 {
328 struct inode *dp; /* inode for directory being searched */
329 struct buf *bp; /* a buffer of directory entries */
330 struct ext2fs_direct_2 *ep; /* the current directory entry */
331 int entryoffsetinblock; /* offset of ep in bp's buffer */
332 struct ext2fs_searchslot ss;
333 doff_t i_diroff; /* cached i_diroff value */
334 doff_t i_offset; /* cached i_offset value */
335 int numdirpasses; /* strategy for directory search */
336 doff_t endsearch; /* offset to end directory search */
337 doff_t prevoff; /* prev entry dp->i_offset */
338 struct vnode *pdp; /* saved dp during symlink work */
339 struct vnode *tdp; /* returned by VFS_VGET */
340 doff_t enduseful; /* pointer past last used dir slot */
341 u_long bmask; /* block offset mask */
342 int error;
343 struct ucred *cred = cnp->cn_cred;
344 int flags = cnp->cn_flags;
345 int nameiop = cnp->cn_nameiop;
346 ino_t ino, ino1;
347 int ltype;
348 int entry_found = 0;
349
350 int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
351
352 if (vpp != NULL)
353 *vpp = NULL;
354
355 dp = VTOI(vdp);
356 bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
357 restart:
358 bp = NULL;
359 ss.slotoffset = -1;
360
361 /*
362 * We now have a segment name to search for, and a directory to search.
363 *
364 * Suppress search for slots unless creating
365 * file and at end of pathname, in which case
366 * we watch for a place to put the new file in
367 * case it doesn't already exist.
368 */
369 i_diroff = dp->i_diroff;
370 ss.slotstatus = FOUND;
371 ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
372 if ((nameiop == CREATE || nameiop == RENAME) &&
373 (flags & ISLASTCN)) {
374 ss.slotstatus = NONE;
375 ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
376 /*
377 * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
378 * cnp->cn_namelen + 3) &~ 3;
379 */
380 }
381 /*
382 * Try to lookup dir entry using htree directory index.
383 *
384 * If we got an error or we want to find '.' or '..' entry,
385 * we will fall back to linear search.
386 */
387 if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
388 numdirpasses = 1;
389 entryoffsetinblock = 0;
390 switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
391 &bp, &entryoffsetinblock, &i_offset, &prevoff,
392 &enduseful, &ss)) {
393 case 0:
394 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
395 (i_offset & bmask));
396 goto foundentry;
397 case ENOENT:
398 i_offset = roundup2(dp->i_size, DIRBLKSIZ);
399 goto notfound;
400 default:
401 /*
402 * Something failed; just fallback to do a linear
403 * search.
404 */
405 break;
406 }
407 }
408
409 /*
410 * If there is cached information on a previous search of
411 * this directory, pick up where we last left off.
412 * We cache only lookups as these are the most common
413 * and have the greatest payoff. Caching CREATE has little
414 * benefit as it usually must search the entire directory
415 * to determine that the entry does not exist. Caching the
416 * location of the last DELETE or RENAME has not reduced
417 * profiling time and hence has been removed in the interest
418 * of simplicity.
419 */
420 if (nameiop != LOOKUP || i_diroff == 0 ||
421 i_diroff > dp->i_size) {
422 entryoffsetinblock = 0;
423 i_offset = 0;
424 numdirpasses = 1;
425 } else {
426 i_offset = i_diroff;
427 if ((entryoffsetinblock = i_offset & bmask) &&
428 (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
429 &bp)))
430 return (error);
431 numdirpasses = 2;
432 nchstats.ncs_2passes++;
433 }
434 prevoff = i_offset;
435 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
436 enduseful = 0;
437
438 searchloop:
439 while (i_offset < endsearch) {
440 /*
441 * If necessary, get the next directory block.
442 */
443 if (bp != NULL)
444 brelse(bp);
445 error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
446 if (error != 0)
447 return (error);
448
449 entryoffsetinblock = 0;
450 if (ss.slotstatus == NONE) {
451 ss.slotoffset = -1;
452 ss.slotfreespace = 0;
453 }
454
455 error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
456 cnp->cn_nameptr, cnp->cn_namelen,
457 &entryoffsetinblock, &i_offset, &prevoff,
458 &enduseful, &ss);
459 if (error != 0) {
460 brelse(bp);
461 return (error);
462 }
463 if (entry_found) {
464 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
465 (entryoffsetinblock & bmask));
466 foundentry:
467 ino = le32toh(ep->e2d_ino);
468 goto found;
469 }
470 }
471 notfound:
472 /*
473 * If we started in the middle of the directory and failed
474 * to find our target, we must check the beginning as well.
475 */
476 if (numdirpasses == 2) {
477 numdirpasses--;
478 i_offset = 0;
479 endsearch = i_diroff;
480 goto searchloop;
481 }
482 if (bp != NULL)
483 brelse(bp);
484 /*
485 * If creating, and at end of pathname and current
486 * directory has not been removed, then can consider
487 * allowing file to be created.
488 */
489 if ((nameiop == CREATE || nameiop == RENAME) &&
490 (flags & ISLASTCN) && dp->i_nlink != 0) {
491 /*
492 * Access for write is interpreted as allowing
493 * creation of files in the directory.
494 */
495 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
496 return (error);
497 /*
498 * Return an indication of where the new directory
499 * entry should be put. If we didn't find a slot,
500 * then set dp->i_count to 0 indicating
501 * that the new slot belongs at the end of the
502 * directory. If we found a slot, then the new entry
503 * can be put in the range from dp->i_offset to
504 * dp->i_offset + dp->i_count.
505 */
506 if (ss.slotstatus == NONE) {
507 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
508 dp->i_count = 0;
509 enduseful = dp->i_offset;
510 } else {
511 dp->i_offset = ss.slotoffset;
512 dp->i_count = ss.slotsize;
513 if (enduseful < ss.slotoffset + ss.slotsize)
514 enduseful = ss.slotoffset + ss.slotsize;
515 }
516 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
517 /*
518 * We return with the directory locked, so that
519 * the parameters we set up above will still be
520 * valid if we actually decide to do a direnter().
521 * We return ni_vp == NULL to indicate that the entry
522 * does not currently exist; we leave a pointer to
523 * the (locked) directory inode in ndp->ni_dvp.
524 * The pathname buffer is saved so that the name
525 * can be obtained later.
526 *
527 * NB - if the directory is unlocked, then this
528 * information cannot be used.
529 */
530 cnp->cn_flags |= SAVENAME;
531 return (EJUSTRETURN);
532 }
533 /*
534 * Insert name into cache (as non-existent) if appropriate.
535 */
536 if ((cnp->cn_flags & MAKEENTRY) != 0)
537 cache_enter(vdp, NULL, cnp);
538 return (ENOENT);
539
540 found:
541 if (dd_ino != NULL)
542 *dd_ino = ino;
543 if (numdirpasses == 2)
544 nchstats.ncs_pass2++;
545 /*
546 * Check that directory length properly reflects presence
547 * of this entry.
548 */
549 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen) >
550 dp->i_size) {
551 ext2_dirbad(dp, i_offset, "i_size too small");
552 dp->i_size = entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen);
553 dp->i_flag |= IN_CHANGE | IN_UPDATE;
554 }
555 brelse(bp);
556
557 /*
558 * Found component in pathname.
559 * If the final component of path name, save information
560 * in the cache as to where the entry was found.
561 */
562 if ((flags & ISLASTCN) && nameiop == LOOKUP)
563 dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
564 /*
565 * If deleting, and at end of pathname, return
566 * parameters which can be used to remove file.
567 */
568 if (nameiop == DELETE && (flags & ISLASTCN)) {
569 if (flags & LOCKPARENT)
570 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
571 /*
572 * Write access to directory required to delete files.
573 */
574 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
575 return (error);
576 /*
577 * Return pointer to current entry in dp->i_offset,
578 * and distance past previous entry (if there
579 * is a previous entry in this block) in dp->i_count.
580 * Save directory inode pointer in ndp->ni_dvp for dirremove().
581 *
582 * Technically we shouldn't be setting these in the
583 * WANTPARENT case (first lookup in rename()), but any
584 * lookups that will result in directory changes will
585 * overwrite these.
586 */
587 dp->i_offset = i_offset;
588 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
589 dp->i_count = 0;
590 else
591 dp->i_count = dp->i_offset - prevoff;
592 if (dd_ino != NULL)
593 return (0);
594 if (dp->i_number == ino) {
595 VREF(vdp);
596 *vpp = vdp;
597 return (0);
598 }
599 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
600 &tdp)) != 0)
601 return (error);
602 /*
603 * If directory is "sticky", then user must own
604 * the directory, or the file in it, else she
605 * may not delete it (unless she's root). This
606 * implements append-only directories.
607 */
608 if ((dp->i_mode & ISVTX) &&
609 cred->cr_uid != 0 &&
610 cred->cr_uid != dp->i_uid &&
611 VTOI(tdp)->i_uid != cred->cr_uid) {
612 vput(tdp);
613 return (EPERM);
614 }
615 *vpp = tdp;
616 return (0);
617 }
618
619 /*
620 * If rewriting (RENAME), return the inode and the
621 * information required to rewrite the present directory
622 * Must get inode of directory entry to verify it's a
623 * regular file, or empty directory.
624 */
625 if (nameiop == RENAME && (flags & ISLASTCN)) {
626 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
627 return (error);
628 /*
629 * Careful about locking second inode.
630 * This can only occur if the target is ".".
631 */
632 dp->i_offset = i_offset;
633 if (dp->i_number == ino)
634 return (EISDIR);
635 if (dd_ino != NULL)
636 return (0);
637 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
638 &tdp)) != 0)
639 return (error);
640 *vpp = tdp;
641 cnp->cn_flags |= SAVENAME;
642 return (0);
643 }
644 if (dd_ino != NULL)
645 return (0);
646
647 /*
648 * Step through the translation in the name. We do not `vput' the
649 * directory because we may need it again if a symbolic link
650 * is relative to the current directory. Instead we save it
651 * unlocked as "pdp". We must get the target inode before unlocking
652 * the directory to insure that the inode will not be removed
653 * before we get it. We prevent deadlock by always fetching
654 * inodes from the root, moving down the directory tree. Thus
655 * when following backward pointers ".." we must unlock the
656 * parent directory before getting the requested directory.
657 * There is a potential race condition here if both the current
658 * and parent directories are removed before the VFS_VGET for the
659 * inode associated with ".." returns. We hope that this occurs
660 * infrequently since we cannot avoid this race condition without
661 * implementing a sophisticated deadlock detection algorithm.
662 * Note also that this simple deadlock detection scheme will not
663 * work if the file system has any hard links other than ".."
664 * that point backwards in the directory structure.
665 */
666 pdp = vdp;
667 if (flags & ISDOTDOT) {
668 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
669 if (VN_IS_DOOMED(pdp)) {
670 if (error == 0)
671 vput(tdp);
672 error = ENOENT;
673 }
674 if (error)
675 return (error);
676 /*
677 * Recheck that ".." entry in the vdp directory points
678 * to the inode we looked up before vdp lock was
679 * dropped.
680 */
681 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
682 if (error) {
683 vput(tdp);
684 return (error);
685 }
686 if (ino1 != ino) {
687 vput(tdp);
688 goto restart;
689 }
690 *vpp = tdp;
691 } else if (dp->i_number == ino) {
692 VREF(vdp); /* we want ourself, ie "." */
693 /*
694 * When we lookup "." we still can be asked to lock it
695 * differently.
696 */
697 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
698 if (ltype != VOP_ISLOCKED(vdp)) {
699 if (ltype == LK_EXCLUSIVE)
700 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
701 else /* if (ltype == LK_SHARED) */
702 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
703 }
704 *vpp = vdp;
705 } else {
706 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
707 &tdp)) != 0)
708 return (error);
709 *vpp = tdp;
710 }
711
712 /*
713 * Insert name into cache if appropriate.
714 */
715 if (cnp->cn_flags & MAKEENTRY)
716 cache_enter(vdp, *vpp, cnp);
717 return (0);
718 }
719
720 int
721 ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
722 const char *name, int namelen, int *entryoffsetinblockp,
723 doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
724 struct ext2fs_searchslot *ssp)
725 {
726 struct vnode *vdp;
727 struct ext2fs_direct_2 *ep, *top;
728 uint32_t bsize = ip->i_e2fs->e2fs_bsize;
729 int offset = *entryoffsetinblockp;
730 int namlen;
731
732 vdp = ITOV(ip);
733
734 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
735 top = (struct ext2fs_direct_2 *)((char *)data + bsize);
736 while (ep < top) {
737 /*
738 * Full validation checks are slow, so we only check
739 * enough to insure forward progress through the
740 * directory. Complete checks can be run by setting
741 * "vfs.e2fs.dirchk" to be true.
742 */
743 if (le16toh(ep->e2d_reclen) == 0 ||
744 (dirchk && ext2_dirbadentry(vdp, ep, offset))) {
745 int i;
746
747 ext2_dirbad(ip, *offp, "mangled entry");
748 i = bsize - (offset & (bsize - 1));
749 *offp += i;
750 offset += i;
751 continue;
752 }
753
754 /*
755 * If an appropriate sized slot has not yet been found,
756 * check to see if one is available. Also accumulate space
757 * in the current block so that we can determine if
758 * compaction is viable.
759 */
760 if (ssp->slotstatus != FOUND) {
761 int size = le16toh(ep->e2d_reclen);
762
763 if (ep->e2d_ino != 0)
764 size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
765 else if (ext2_is_dirent_tail(ip, ep))
766 size -= sizeof(struct ext2fs_direct_tail);
767 if (size > 0) {
768 if (size >= ssp->slotneeded) {
769 ssp->slotstatus = FOUND;
770 ssp->slotoffset = *offp;
771 ssp->slotsize = le16toh(ep->e2d_reclen);
772 } else if (ssp->slotstatus == NONE) {
773 ssp->slotfreespace += size;
774 if (ssp->slotoffset == -1)
775 ssp->slotoffset = *offp;
776 if (ssp->slotfreespace >= ssp->slotneeded) {
777 ssp->slotstatus = COMPACT;
778 ssp->slotsize = *offp +
779 le16toh(ep->e2d_reclen) -
780 ssp->slotoffset;
781 }
782 }
783 }
784 }
785 /*
786 * Check for a name match.
787 */
788 if (ep->e2d_ino != 0) {
789 namlen = ep->e2d_namlen;
790 if (namlen == namelen &&
791 !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
792 /*
793 * Save directory entry's inode number and
794 * reclen in ndp->ni_ufs area, and release
795 * directory buffer.
796 */
797 *foundp = 1;
798 return (0);
799 }
800 }
801 *prevoffp = *offp;
802 *offp += le16toh(ep->e2d_reclen);
803 offset += le16toh(ep->e2d_reclen);
804 *entryoffsetinblockp = offset;
805 if (ep->e2d_ino != 0)
806 *endusefulp = *offp;
807 /*
808 * Get pointer to the next entry.
809 */
810 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
811 }
812
813 return (0);
814 }
815
816 void
817 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
818 {
819 struct mount *mp;
820
821 mp = ITOV(ip)->v_mount;
822 if ((mp->mnt_flag & MNT_RDONLY) == 0)
823 panic("ext2_dirbad: %s: bad dir ino %ju at offset %ld: %s\n",
824 mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
825 (long)offset, how);
826 else
827 SDT_PROBE4(ext2fs, , trace, ext2_dirbad_error,
828 mp->mnt_stat.f_mntonname, ip->i_number, offset, how);
829 }
830
831 /*
832 * Do consistency checking on a directory entry:
833 * record length must be multiple of 4
834 * entry must fit in rest of its DIRBLKSIZ block
835 * record must be large enough to contain entry
836 * name is not longer than MAXNAMLEN
837 * name must be as long as advertised, and null terminated
838 */
839 /*
840 * changed so that it confirms to ext2_check_dir_entry
841 */
842 static int
843 ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
844 int entryoffsetinblock)
845 {
846 int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize;
847
848 char *error_msg = NULL;
849
850 if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(1))
851 error_msg = "rec_len is smaller than minimal";
852 else if (le16toh(de->e2d_reclen) % 4 != 0)
853 error_msg = "rec_len % 4 != 0";
854 else if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(de->e2d_namlen))
855 error_msg = "reclen is too small for name_len";
856 else if (entryoffsetinblock + le16toh(de->e2d_reclen)> DIRBLKSIZ)
857 error_msg = "directory entry across blocks";
858 /* else LATER
859 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
860 error_msg = "inode out of bounds";
861 */
862
863 if (error_msg != NULL) {
864 SDT_PROBE5(ext2fs, , trace, ext2_dirbadentry_error,
865 error_msg, entryoffsetinblock,
866 le32toh(de->e2d_ino), le16toh(de->e2d_reclen),
867 de->e2d_namlen);
868 }
869 return (error_msg == NULL ? 0 : 1);
870 }
871
872 /*
873 * Insert an entry into the fresh directory block.
874 * Initialize entry tail if the metadata_csum feature is turned on.
875 */
876 static int
877 ext2_add_first_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry,
878 struct componentname *cnp)
879 {
880 struct inode *dp;
881 struct iovec aiov;
882 struct uio auio;
883 char* buf = NULL;
884 int dirblksize, error;
885
886 dp = VTOI(dvp);
887 dirblksize = dp->i_e2fs->e2fs_bsize;
888
889 if (dp->i_offset & (dirblksize - 1))
890 panic("ext2_add_first_entry: bad directory offset");
891
892 if (EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs,
893 EXT2F_ROCOMPAT_METADATA_CKSUM)) {
894 entry->e2d_reclen = htole16(dirblksize -
895 sizeof(struct ext2fs_direct_tail));
896 buf = malloc(dirblksize, M_TEMP, M_WAITOK);
897 memcpy(buf, entry, EXT2_DIR_REC_LEN(entry->e2d_namlen));
898 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, dirblksize));
899 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf);
900
901 auio.uio_offset = dp->i_offset;
902 auio.uio_resid = dirblksize;
903 aiov.iov_len = auio.uio_resid;
904 aiov.iov_base = (caddr_t)buf;
905 } else {
906 entry->e2d_reclen = htole16(dirblksize);
907 auio.uio_offset = dp->i_offset;
908 auio.uio_resid = EXT2_DIR_REC_LEN(entry->e2d_namlen);
909 aiov.iov_len = auio.uio_resid;
910 aiov.iov_base = (caddr_t)entry;
911 }
912
913 auio.uio_iov = &aiov;
914 auio.uio_iovcnt = 1;
915 auio.uio_rw = UIO_WRITE;
916 auio.uio_segflg = UIO_SYSSPACE;
917 auio.uio_td = (struct thread *)0;
918 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
919 if (error)
920 goto out;
921
922 dp->i_size = roundup2(dp->i_size, dirblksize);
923 dp->i_flag |= IN_CHANGE;
924
925 out:
926 free(buf, M_TEMP);
927 return (error);
928
929 }
930
931 /*
932 * Write a directory entry after a call to namei, using the parameters
933 * that it left in nameidata. The argument ip is the inode which the new
934 * directory entry will refer to. Dvp is a pointer to the directory to
935 * be written, which was left locked by namei. Remaining parameters
936 * (dp->i_offset, dp->i_count) indicate how the space for the new
937 * entry is to be obtained.
938 */
939 int
940 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
941 {
942 struct inode *dp;
943 struct ext2fs_direct_2 newdir;
944 int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
945 int error;
946
947 #ifdef INVARIANTS
948 if ((cnp->cn_flags & SAVENAME) == 0)
949 panic("ext2_direnter: missing name");
950 #endif
951 dp = VTOI(dvp);
952 newdir.e2d_ino = htole32(ip->i_number);
953 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
954 EXT2F_INCOMPAT_FTYPE)) {
955 newdir.e2d_namlen = cnp->cn_namelen;
956 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
957 } else
958 newdir.e2d_namlen = htole16(cnp->cn_namelen);
959
960 bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
961
962 if (ext2_htree_has_idx(dp)) {
963 error = ext2_htree_add_entry(dvp, &newdir, cnp);
964 if (error) {
965 dp->i_flag &= ~IN_E3INDEX;
966 dp->i_flag |= IN_CHANGE | IN_UPDATE;
967 }
968 return (error);
969 }
970
971 if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
972 !ext2_htree_has_idx(dp)) {
973 if ((dp->i_size / DIRBLKSIZ) == 1 &&
974 dp->i_offset == DIRBLKSIZ) {
975 /*
976 * Making indexed directory when one block is not
977 * enough to save all entries.
978 */
979 return ext2_htree_create_index(dvp, cnp, &newdir);
980 }
981 }
982
983 /*
984 * If dp->i_count is 0, then namei could find no
985 * space in the directory. Here, dp->i_offset will
986 * be on a directory block boundary and we will write the
987 * new entry into a fresh block.
988 */
989 if (dp->i_count == 0)
990 return ext2_add_first_entry(dvp, &newdir, cnp);
991
992 error = ext2_add_entry(dvp, &newdir);
993 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
994 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
995 cnp->cn_cred, cnp->cn_thread);
996 return (error);
997 }
998
999 /*
1000 * Insert an entry into the directory block.
1001 * Compact the contents.
1002 */
1003 int
1004 ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
1005 {
1006 struct ext2fs_direct_2 *ep, *nep;
1007 struct inode *dp;
1008 struct buf *bp;
1009 u_int dsize;
1010 int error, loc, newentrysize, spacefree;
1011 char *dirbuf;
1012
1013 dp = VTOI(dvp);
1014
1015 /*
1016 * If dp->i_count is non-zero, then namei found space
1017 * for the new entry in the range dp->i_offset to
1018 * dp->i_offset + dp->i_count in the directory.
1019 * To use this space, we may have to compact the entries located
1020 * there, by copying them together towards the beginning of the
1021 * block, leaving the free space in one usable chunk at the end.
1022 */
1023
1024 /*
1025 * Increase size of directory if entry eats into new space.
1026 * This should never push the size past a new multiple of
1027 * DIRBLKSIZE.
1028 *
1029 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
1030 */
1031 if (dp->i_offset + dp->i_count > dp->i_size)
1032 dp->i_size = dp->i_offset + dp->i_count;
1033 /*
1034 * Get the block containing the space for the new directory entry.
1035 */
1036 if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
1037 &bp)) != 0)
1038 return (error);
1039 /*
1040 * Find space for the new entry. In the simple case, the entry at
1041 * offset base will have the space. If it does not, then namei
1042 * arranged that compacting the region dp->i_offset to
1043 * dp->i_offset + dp->i_count would yield the
1044 * space.
1045 */
1046 newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
1047 ep = (struct ext2fs_direct_2 *)dirbuf;
1048 dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
1049 spacefree = le16toh(ep->e2d_reclen) - dsize;
1050 for (loc = le16toh(ep->e2d_reclen); loc < dp->i_count; ) {
1051 nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
1052 if (le32toh(ep->e2d_ino)) {
1053 /* trim the existing slot */
1054 ep->e2d_reclen = htole16(dsize);
1055 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1056 } else {
1057 /* overwrite; nothing there; header is ours */
1058 spacefree += dsize;
1059 }
1060 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
1061 spacefree += le16toh(nep->e2d_reclen) - dsize;
1062 loc += le16toh(nep->e2d_reclen);
1063 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1064 }
1065 /*
1066 * Update the pointer fields in the previous entry (if any),
1067 * copy in the new entry, and write out the block.
1068 */
1069 if (ep->e2d_ino == 0) {
1070 if (spacefree + dsize < newentrysize)
1071 panic("ext2_direnter: compact1");
1072 entry->e2d_reclen = htole16(spacefree + dsize);
1073 } else {
1074 if (spacefree < newentrysize)
1075 panic("ext2_direnter: compact2");
1076 entry->e2d_reclen = htole16(spacefree);
1077 ep->e2d_reclen = htole16(dsize);
1078 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1079 }
1080 bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
1081 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1082 if (DOINGASYNC(dvp)) {
1083 bdwrite(bp);
1084 error = 0;
1085 } else {
1086 error = bwrite(bp);
1087 }
1088 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1089 return (error);
1090 }
1091
1092 /*
1093 * Remove a directory entry after a call to namei, using
1094 * the parameters which it left in nameidata. The entry
1095 * dp->i_offset contains the offset into the directory of the
1096 * entry to be eliminated. The dp->i_count field contains the
1097 * size of the previous record in the directory. If this
1098 * is 0, the first entry is being deleted, so we need only
1099 * zero the inode number to mark the entry as free. If the
1100 * entry is not the first in the directory, we must reclaim
1101 * the space of the now empty record by adding the record size
1102 * to the size of the previous entry.
1103 */
1104 int
1105 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
1106 {
1107 struct inode *dp;
1108 struct ext2fs_direct_2 *ep, *rep;
1109 struct buf *bp;
1110 int error;
1111
1112 dp = VTOI(dvp);
1113 if (dp->i_count == 0) {
1114 /*
1115 * First entry in block: set d_ino to zero.
1116 */
1117 if ((error =
1118 ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
1119 &bp)) != 0)
1120 return (error);
1121 ep->e2d_ino = 0;
1122 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1123 error = bwrite(bp);
1124 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1125 return (error);
1126 }
1127 /*
1128 * Collapse new free space into previous entry.
1129 */
1130 if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
1131 (char **)&ep, &bp)) != 0)
1132 return (error);
1133
1134 /* Set 'rep' to the entry being removed. */
1135 if (dp->i_count == 0)
1136 rep = ep;
1137 else
1138 rep = (struct ext2fs_direct_2 *)((char *)ep +
1139 le16toh(ep->e2d_reclen));
1140 ep->e2d_reclen += rep->e2d_reclen;
1141 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1142 if (DOINGASYNC(dvp) && dp->i_count != 0)
1143 bdwrite(bp);
1144 else
1145 error = bwrite(bp);
1146 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1147 return (error);
1148 }
1149
1150 /*
1151 * Rewrite an existing directory entry to point at the inode
1152 * supplied. The parameters describing the directory entry are
1153 * set up by a call to namei.
1154 */
1155 int
1156 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
1157 {
1158 struct buf *bp;
1159 struct ext2fs_direct_2 *ep;
1160 struct vnode *vdp = ITOV(dp);
1161 int error;
1162
1163 if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
1164 &bp)) != 0)
1165 return (error);
1166 ep->e2d_ino = htole32(ip->i_number);
1167 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1168 EXT2F_INCOMPAT_FTYPE))
1169 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
1170 else
1171 ep->e2d_type = EXT2_FT_UNKNOWN;
1172 ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1173 error = bwrite(bp);
1174 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1175 return (error);
1176 }
1177
1178 /*
1179 * Check if a directory is empty or not.
1180 * Inode supplied must be locked.
1181 *
1182 * Using a struct dirtemplate here is not precisely
1183 * what we want, but better than using a struct direct.
1184 *
1185 * NB: does not handle corrupted directories.
1186 */
1187 int
1188 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1189 {
1190 off_t off;
1191 struct dirtemplate dbuf;
1192 struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1193 int error, namlen;
1194 ssize_t count;
1195 #define MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1196
1197 for (off = 0; off < ip->i_size; off += le16toh(dp->e2d_reclen)) {
1198 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1199 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1200 NOCRED, &count, (struct thread *)0);
1201 /*
1202 * Since we read MINDIRSIZ, residual must
1203 * be 0 unless we're at end of file.
1204 */
1205 if (error || count != 0)
1206 return (0);
1207 /* avoid infinite loops */
1208 if (dp->e2d_reclen == 0)
1209 return (0);
1210 /* skip empty entries */
1211 if (dp->e2d_ino == 0)
1212 continue;
1213 /* accept only "." and ".." */
1214 namlen = dp->e2d_namlen;
1215 if (namlen > 2)
1216 return (0);
1217 if (dp->e2d_name[0] != '.')
1218 return (0);
1219 /*
1220 * At this point namlen must be 1 or 2.
1221 * 1 implies ".", 2 implies ".." if second
1222 * char is also "."
1223 */
1224 if (namlen == 1)
1225 continue;
1226 if (dp->e2d_name[1] == '.' && le32toh(dp->e2d_ino) == parentino)
1227 continue;
1228 return (0);
1229 }
1230 return (1);
1231 }
1232
1233 /*
1234 * Check if source directory is in the path of the target directory.
1235 * Target is supplied locked, source is unlocked.
1236 * The target is always vput before returning.
1237 */
1238 int
1239 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1240 {
1241 struct vnode *vp;
1242 int error, namlen;
1243 struct dirtemplate dirbuf;
1244
1245 vp = ITOV(target);
1246 if (target->i_number == source->i_number) {
1247 error = EEXIST;
1248 goto out;
1249 }
1250 if (target->i_number == EXT2_ROOTINO) {
1251 error = 0;
1252 goto out;
1253 }
1254
1255 for (;;) {
1256 if (vp->v_type != VDIR) {
1257 error = ENOTDIR;
1258 break;
1259 }
1260 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1261 sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1262 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1263 NULL);
1264 if (error != 0)
1265 break;
1266 namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1267 if (namlen != 2 ||
1268 dirbuf.dotdot_name[0] != '.' ||
1269 dirbuf.dotdot_name[1] != '.') {
1270 error = ENOTDIR;
1271 break;
1272 }
1273 if (le32toh(dirbuf.dotdot_ino) == source->i_number) {
1274 error = EINVAL;
1275 break;
1276 }
1277 if (le32toh(dirbuf.dotdot_ino) == EXT2_ROOTINO)
1278 break;
1279 vput(vp);
1280 if ((error = VFS_VGET(vp->v_mount, le32toh(dirbuf.dotdot_ino),
1281 LK_EXCLUSIVE, &vp)) != 0) {
1282 vp = NULL;
1283 break;
1284 }
1285 }
1286
1287 out:
1288 if (error == ENOTDIR)
1289 SDT_PROBE2(ext2fs, , lookup, trace, 1,
1290 "checkpath: .. not a directory");
1291 if (vp != NULL)
1292 vput(vp);
1293 return (error);
1294 }
Cache object: 7b3e1cdab8a4994485a368a9c5165cf0
|