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