ext2fs_lookup.c revision 1.44 1 /* $NetBSD: ext2fs_lookup.c,v 1.44 2006/05/14 21:32:21 elad Exp $ */
2
3 /*
4 * Modified for NetBSD 1.2E
5 * May 1997, Manuel Bouyer
6 * Laboratoire d'informatique de Paris VI
7 */
8 /*
9 * modified for Lites 1.1
10 *
11 * Aug 1995, Godmar Back (gback (at) cs.utah.edu)
12 * University of Utah, Department of Computer Science
13 */
14 /*
15 * Copyright (c) 1989, 1993
16 * The Regents of the University of California. All rights reserved.
17 * (c) UNIX System Laboratories, Inc.
18 * All or some portions of this file are derived from material licensed
19 * to the University of California by American Telephone and Telegraph
20 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
21 * the permission of UNIX System Laboratories, Inc.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.44 2006/05/14 21:32:21 elad Exp $");
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/namei.h>
56 #include <sys/buf.h>
57 #include <sys/file.h>
58 #include <sys/mount.h>
59 #include <sys/vnode.h>
60 #include <sys/malloc.h>
61 #include <sys/dirent.h>
62 #include <sys/kauth.h>
63
64 #include <ufs/ufs/inode.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <ufs/ufs/ufs_extern.h>
67
68 #include <ufs/ext2fs/ext2fs_extern.h>
69 #include <ufs/ext2fs/ext2fs_dir.h>
70 #include <ufs/ext2fs/ext2fs.h>
71
72 extern int dirchk;
73
74 static void ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir,
75 struct dirent *ffsdir);
76 static int ext2fs_dirbadentry(struct vnode *dp,
77 struct ext2fs_direct *de,
78 int entryoffsetinblock);
79
80 /*
81 * the problem that is tackled below is the fact that FFS
82 * includes the terminating zero on disk while EXT2FS doesn't
83 * this implies that we need to introduce some padding.
84 * For instance, a filename "sbin" has normally a reclen 12
85 * in EXT2, but 16 in FFS.
86 * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...'
87 * If it wasn't for that, the complete ufs code for directories would
88 * have worked w/o changes (except for the difference in DIRBLKSIZ)
89 */
90 static void
91 ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir, struct dirent *ffsdir)
92 {
93 memset(ffsdir, 0, sizeof(struct dirent));
94 ffsdir->d_fileno = fs2h32(e2dir->e2d_ino);
95 ffsdir->d_namlen = e2dir->e2d_namlen;
96
97 ffsdir->d_type = DT_UNKNOWN; /* don't know more here */
98 #ifdef DIAGNOSTIC
99 #if MAXNAMLEN < E2FS_MAXNAMLEN
100 /*
101 * we should handle this more gracefully !
102 */
103 if (e2dir->e2d_namlen > MAXNAMLEN)
104 panic("ext2fs: e2dir->e2d_namlen");
105 #endif
106 #endif
107 strncpy(ffsdir->d_name, e2dir->e2d_name, ffsdir->d_namlen);
108
109 /* Godmar thinks: since e2dir->e2d_reclen can be big and means
110 nothing anyway, we compute our own reclen according to what
111 we think is right
112 */
113 ffsdir->d_reclen = _DIRENT_SIZE(ffsdir);
114 }
115
116 /*
117 * Vnode op for reading directories.
118 *
119 * Convert the on-disk entries to <sys/dirent.h> entries.
120 * the problem is that the conversion will blow up some entries by four bytes,
121 * so it can't be done in place. This is too bad. Right now the conversion is
122 * done entry by entry, the converted entry is sent via uiomove.
123 *
124 * XXX allocate a buffer, convert as many entries as possible, then send
125 * the whole buffer to uiomove
126 */
127 int
128 ext2fs_readdir(void *v)
129 {
130 struct vop_readdir_args /* {
131 struct vnode *a_vp;
132 struct uio *a_uio;
133 kauth_cred_t a_cred;
134 int **a_eofflag;
135 off_t **a_cookies;
136 int ncookies;
137 } */ *ap = v;
138 struct uio *uio = ap->a_uio;
139 int error;
140 size_t e2fs_count, readcnt;
141 struct vnode *vp = ap->a_vp;
142 struct m_ext2fs *fs = VTOI(vp)->i_e2fs;
143
144 struct ext2fs_direct *dp;
145 struct dirent dstd;
146 struct uio auio;
147 struct iovec aiov;
148 caddr_t dirbuf;
149 off_t off = uio->uio_offset;
150 off_t *cookies = NULL;
151 int nc = 0, ncookies = 0;
152 int e2d_reclen;
153
154 if (vp->v_type != VDIR)
155 return (ENOTDIR);
156
157 e2fs_count = uio->uio_resid;
158 /* Make sure we don't return partial entries. */
159 e2fs_count -= (uio->uio_offset + e2fs_count) & (fs->e2fs_bsize -1);
160 if (e2fs_count <= 0)
161 return (EINVAL);
162
163 auio = *uio;
164 auio.uio_iov = &aiov;
165 auio.uio_iovcnt = 1;
166 aiov.iov_len = e2fs_count;
167 auio.uio_resid = e2fs_count;
168 UIO_SETUP_SYSSPACE(&auio);
169 dirbuf = malloc(e2fs_count, M_TEMP, M_WAITOK);
170 if (ap->a_ncookies) {
171 nc = ncookies = e2fs_count / 16;
172 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
173 *ap->a_cookies = cookies;
174 }
175 memset(dirbuf, 0, e2fs_count);
176 aiov.iov_base = dirbuf;
177
178 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
179 if (error == 0) {
180 readcnt = e2fs_count - auio.uio_resid;
181 for (dp = (struct ext2fs_direct *)dirbuf;
182 (char *)dp < (char *)dirbuf + readcnt; ) {
183 e2d_reclen = fs2h16(dp->e2d_reclen);
184 if (e2d_reclen == 0) {
185 error = EIO;
186 break;
187 }
188 ext2fs_dirconv2ffs(dp, &dstd);
189 if(dstd.d_reclen > uio->uio_resid) {
190 break;
191 }
192 if ((error = uiomove((caddr_t)&dstd, dstd.d_reclen, uio)) != 0) {
193 break;
194 }
195 off = off + e2d_reclen;
196 if (cookies != NULL) {
197 *cookies++ = off;
198 if (--ncookies <= 0){
199 break; /* out of cookies */
200 }
201 }
202 /* advance dp */
203 dp = (struct ext2fs_direct *) ((char *)dp + e2d_reclen);
204 }
205 /* we need to correct uio_offset */
206 uio->uio_offset = off;
207 }
208 FREE(dirbuf, M_TEMP);
209 *ap->a_eofflag = ext2fs_size(VTOI(ap->a_vp)) <= uio->uio_offset;
210 if (ap->a_ncookies) {
211 if (error) {
212 free(*ap->a_cookies, M_TEMP);
213 *ap->a_ncookies = 0;
214 *ap->a_cookies = NULL;
215 } else
216 *ap->a_ncookies = nc - ncookies;
217 }
218 return (error);
219 }
220
221 /*
222 * Convert a component of a pathname into a pointer to a locked inode.
223 * This is a very central and rather complicated routine.
224 * If the file system is not maintained in a strict tree hierarchy,
225 * this can result in a deadlock situation (see comments in code below).
226 *
227 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
228 * on whether the name is to be looked up, created, renamed, or deleted.
229 * When CREATE, RENAME, or DELETE is specified, information usable in
230 * creating, renaming, or deleting a directory entry may be calculated.
231 * If flag has LOCKPARENT or'ed into it and the target of the pathname
232 * exists, lookup returns both the target and its parent directory locked.
233 * When creating or renaming and LOCKPARENT is specified, the target may
234 * not be ".". When deleting and LOCKPARENT is specified, the target may
235 * be "."., but the caller must check to ensure it does an vrele and vput
236 * instead of two vputs.
237 *
238 * Overall outline of ext2fs_lookup:
239 *
240 * check accessibility of directory
241 * look for name in cache, if found, then if at end of path
242 * and deleting or creating, drop it, else return name
243 * search for name in directory, to found or notfound
244 * notfound:
245 * if creating, return locked directory, leaving info on available slots
246 * else return error
247 * found:
248 * if at end of path and deleting, return information to allow delete
249 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
250 * inode and return info to allow rewrite
251 * if not at end, add name to cache; if at end and neither creating
252 * nor deleting, add name to cache
253 */
254 int
255 ext2fs_lookup(void *v)
256 {
257 struct vop_lookup_args /* {
258 struct vnode *a_dvp;
259 struct vnode **a_vpp;
260 struct componentname *a_cnp;
261 } */ *ap = v;
262 struct vnode *vdp = ap->a_dvp; /* vnode for directory being searched */
263 struct inode *dp = VTOI(vdp); /* inode for directory being searched */
264 struct buf *bp; /* a buffer of directory entries */
265 struct ext2fs_direct *ep; /* the current directory entry */
266 int entryoffsetinblock; /* offset of ep in bp's buffer */
267 enum {NONE, COMPACT, FOUND} slotstatus;
268 doff_t slotoffset; /* offset of area with free space */
269 int slotsize; /* size of area at slotoffset */
270 int slotfreespace; /* amount of space free in slot */
271 int slotneeded; /* size of the entry we're seeking */
272 int numdirpasses; /* strategy for directory search */
273 doff_t endsearch; /* offset to end directory search */
274 doff_t prevoff; /* prev entry dp->i_offset */
275 struct vnode *pdp; /* saved dp during symlink work */
276 struct vnode *tdp; /* returned by VFS_VGET */
277 doff_t enduseful; /* pointer past last used dir slot */
278 u_long bmask; /* block offset mask */
279 int lockparent; /* 1 => lockparent flag is set */
280 int wantparent; /* 1 => wantparent or lockparent flag */
281 int namlen, error;
282 struct vnode **vpp = ap->a_vpp;
283 struct componentname *cnp = ap->a_cnp;
284 kauth_cred_t cred = cnp->cn_cred;
285 int flags;
286 int nameiop = cnp->cn_nameiop;
287 struct ufsmount *ump = dp->i_ump;
288 int dirblksiz = ump->um_dirblksiz;
289 ino_t foundino;
290
291 cnp->cn_flags &= ~PDIRUNLOCK;
292 flags = cnp->cn_flags;
293
294 bp = NULL;
295 slotoffset = -1;
296 *vpp = NULL;
297 lockparent = flags & LOCKPARENT;
298 wantparent = flags & (LOCKPARENT|WANTPARENT);
299 /*
300 * Check accessiblity of directory.
301 */
302 if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_lwp)) != 0)
303 return (error);
304
305 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
306 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
307 return (EROFS);
308
309 /*
310 * We now have a segment name to search for, and a directory to search.
311 *
312 * Before tediously performing a linear scan of the directory,
313 * check the name cache to see if the directory/name pair
314 * we are looking for is known already.
315 */
316 if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
317 return (error);
318
319 /*
320 * Suppress search for slots unless creating
321 * file and at end of pathname, in which case
322 * we watch for a place to put the new file in
323 * case it doesn't already exist.
324 */
325 slotstatus = FOUND;
326 slotfreespace = slotsize = slotneeded = 0;
327 if ((nameiop == CREATE || nameiop == RENAME) &&
328 (flags & ISLASTCN)) {
329 slotstatus = NONE;
330 slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
331 }
332
333 /*
334 * If there is cached information on a previous search of
335 * this directory, pick up where we last left off.
336 * We cache only lookups as these are the most common
337 * and have the greatest payoff. Caching CREATE has little
338 * benefit as it usually must search the entire directory
339 * to determine that the entry does not exist. Caching the
340 * location of the last DELETE or RENAME has not reduced
341 * profiling time and hence has been removed in the interest
342 * of simplicity.
343 */
344 bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
345 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
346 dp->i_diroff >= ext2fs_size(dp)) {
347 entryoffsetinblock = 0;
348 dp->i_offset = 0;
349 numdirpasses = 1;
350 } else {
351 dp->i_offset = dp->i_diroff;
352 if ((entryoffsetinblock = dp->i_offset & bmask) &&
353 (error = ext2fs_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)))
354 return (error);
355 numdirpasses = 2;
356 nchstats.ncs_2passes++;
357 }
358 prevoff = dp->i_offset;
359 endsearch = roundup(ext2fs_size(dp), dirblksiz);
360 enduseful = 0;
361
362 searchloop:
363 while (dp->i_offset < endsearch) {
364 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
365 preempt(1);
366 /*
367 * If necessary, get the next directory block.
368 */
369 if ((dp->i_offset & bmask) == 0) {
370 if (bp != NULL)
371 brelse(bp);
372 error = ext2fs_blkatoff(vdp, (off_t)dp->i_offset, NULL,
373 &bp);
374 if (error != 0)
375 return (error);
376 entryoffsetinblock = 0;
377 }
378 /*
379 * If still looking for a slot, and at a dirblksize
380 * boundary, have to start looking for free space again.
381 */
382 if (slotstatus == NONE &&
383 (entryoffsetinblock & (dirblksiz - 1)) == 0) {
384 slotoffset = -1;
385 slotfreespace = 0;
386 }
387 /*
388 * Get pointer to next entry.
389 * Full validation checks are slow, so we only check
390 * enough to insure forward progress through the
391 * directory. Complete checks can be run by patching
392 * "dirchk" to be true.
393 */
394 KASSERT(bp != NULL);
395 ep = (struct ext2fs_direct *)
396 ((char *)bp->b_data + entryoffsetinblock);
397 if (ep->e2d_reclen == 0 ||
398 (dirchk &&
399 ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
400 int i;
401
402 ufs_dirbad(dp, dp->i_offset, "mangled entry");
403 i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
404 dp->i_offset += i;
405 entryoffsetinblock += i;
406 continue;
407 }
408
409 /*
410 * If an appropriate sized slot has not yet been found,
411 * check to see if one is available. Also accumulate space
412 * in the current block so that we can determine if
413 * compaction is viable.
414 */
415 if (slotstatus != FOUND) {
416 int size = fs2h16(ep->e2d_reclen);
417
418 if (ep->e2d_ino != 0)
419 size -= EXT2FS_DIRSIZ(ep->e2d_namlen);
420 if (size > 0) {
421 if (size >= slotneeded) {
422 slotstatus = FOUND;
423 slotoffset = dp->i_offset;
424 slotsize = fs2h16(ep->e2d_reclen);
425 } else if (slotstatus == NONE) {
426 slotfreespace += size;
427 if (slotoffset == -1)
428 slotoffset = dp->i_offset;
429 if (slotfreespace >= slotneeded) {
430 slotstatus = COMPACT;
431 slotsize = dp->i_offset +
432 fs2h16(ep->e2d_reclen) -
433 slotoffset;
434 }
435 }
436 }
437 }
438
439 /*
440 * Check for a name match.
441 */
442 if (ep->e2d_ino) {
443 namlen = ep->e2d_namlen;
444 if (namlen == cnp->cn_namelen &&
445 !memcmp(cnp->cn_nameptr, ep->e2d_name,
446 (unsigned)namlen)) {
447 /*
448 * Save directory entry's inode number and
449 * reclen in ndp->ni_ufs area, and release
450 * directory buffer.
451 */
452 foundino = fs2h32(ep->e2d_ino);
453 dp->i_reclen = fs2h16(ep->e2d_reclen);
454 goto found;
455 }
456 }
457 prevoff = dp->i_offset;
458 dp->i_offset += fs2h16(ep->e2d_reclen);
459 entryoffsetinblock += fs2h16(ep->e2d_reclen);
460 if (ep->e2d_ino)
461 enduseful = dp->i_offset;
462 }
463 /* notfound: */
464 /*
465 * If we started in the middle of the directory and failed
466 * to find our target, we must check the beginning as well.
467 */
468 if (numdirpasses == 2) {
469 numdirpasses--;
470 dp->i_offset = 0;
471 endsearch = dp->i_diroff;
472 goto searchloop;
473 }
474 if (bp != NULL)
475 brelse(bp);
476 /*
477 * If creating, and at end of pathname and current
478 * directory has not been removed, then can consider
479 * allowing file to be created.
480 */
481 if ((nameiop == CREATE || nameiop == RENAME) &&
482 (flags & ISLASTCN) && dp->i_e2fs_nlink != 0) {
483 /*
484 * Access for write is interpreted as allowing
485 * creation of files in the directory.
486 */
487 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_lwp);
488 if (error)
489 return (error);
490 /*
491 * Return an indication of where the new directory
492 * entry should be put. If we didn't find a slot,
493 * then set dp->i_count to 0 indicating
494 * that the new slot belongs at the end of the
495 * directory. If we found a slot, then the new entry
496 * can be put in the range from dp->i_offset to
497 * dp->i_offset + dp->i_count.
498 */
499 if (slotstatus == NONE) {
500 dp->i_offset = roundup(ext2fs_size(dp), dirblksiz);
501 dp->i_count = 0;
502 enduseful = dp->i_offset;
503 } else {
504 dp->i_offset = slotoffset;
505 dp->i_count = slotsize;
506 if (enduseful < slotoffset + slotsize)
507 enduseful = slotoffset + slotsize;
508 }
509 dp->i_endoff = roundup(enduseful, dirblksiz);
510 #if 0
511 dp->i_flag |= IN_CHANGE | IN_UPDATE;
512 #endif
513 /*
514 * We return with the directory locked, so that
515 * the parameters we set up above will still be
516 * valid if we actually decide to do a direnter().
517 * We return ni_vp == NULL to indicate that the entry
518 * does not currently exist; we leave a pointer to
519 * the (locked) directory inode in ndp->ni_dvp.
520 * The pathname buffer is saved so that the name
521 * can be obtained later.
522 *
523 * NB - if the directory is unlocked, then this
524 * information cannot be used.
525 */
526 cnp->cn_flags |= SAVENAME;
527 if (!lockparent) {
528 VOP_UNLOCK(vdp, 0);
529 cnp->cn_flags |= PDIRUNLOCK;
530 }
531 return (EJUSTRETURN);
532 }
533 /*
534 * Insert name into cache (as non-existent) if appropriate.
535 */
536 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
537 cache_enter(vdp, *vpp, cnp);
538 return (ENOENT);
539
540 found:
541 if (numdirpasses == 2)
542 nchstats.ncs_pass2++;
543 /*
544 * Check that directory length properly reflects presence
545 * of this entry.
546 */
547 if (dp->i_offset + EXT2FS_DIRSIZ(ep->e2d_namlen) > ext2fs_size(dp)) {
548 ufs_dirbad(dp, dp->i_offset, "i_size too small");
549 error = ext2fs_setsize(dp,
550 dp->i_offset + EXT2FS_DIRSIZ(ep->e2d_namlen));
551 if (error) {
552 brelse(bp);
553 return (error);
554 }
555 dp->i_flag |= IN_CHANGE | IN_UPDATE;
556 uvm_vnp_setsize(vdp, ext2fs_size(dp));
557 }
558 brelse(bp);
559
560 /*
561 * Found component in pathname.
562 * If the final component of path name, save information
563 * in the cache as to where the entry was found.
564 */
565 if ((flags & ISLASTCN) && nameiop == LOOKUP)
566 dp->i_diroff = dp->i_offset &~ (dirblksiz - 1);
567
568 /*
569 * If deleting, and at end of pathname, return
570 * parameters which can be used to remove file.
571 * If the wantparent flag isn't set, we return only
572 * the directory (in ndp->ni_dvp), otherwise we go
573 * on and lock the inode, being careful with ".".
574 */
575 if (nameiop == DELETE && (flags & ISLASTCN)) {
576 /*
577 * Write access to directory required to delete files.
578 */
579 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_lwp)) != 0)
580 return (error);
581 /*
582 * Return pointer to current entry in dp->i_offset,
583 * and distance past previous entry (if there
584 * is a previous entry in this block) in dp->i_count.
585 * Save directory inode pointer in ndp->ni_dvp for dirremove().
586 */
587 if ((dp->i_offset & (dirblksiz - 1)) == 0)
588 dp->i_count = 0;
589 else
590 dp->i_count = dp->i_offset - prevoff;
591 if (dp->i_number == foundino) {
592 VREF(vdp);
593 *vpp = vdp;
594 return (0);
595 }
596 if (flags & ISDOTDOT)
597 VOP_UNLOCK(vdp, 0); /* race to get the inode */
598 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
599 if (flags & ISDOTDOT)
600 vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
601 if (error)
602 return (error);
603 /*
604 * If directory is "sticky", then user must own
605 * the directory, or the file in it, else she
606 * may not delete it (unless she's root). This
607 * implements append-only directories.
608 */
609 if ((dp->i_e2fs_mode & ISVTX) &&
610 kauth_cred_geteuid(cred) != 0 &&
611 kauth_cred_geteuid(cred) != dp->i_e2fs_uid &&
612 VTOI(tdp)->i_e2fs_uid != kauth_cred_geteuid(cred)) {
613 vput(tdp);
614 return (EPERM);
615 }
616 *vpp = tdp;
617 if (!lockparent) {
618 VOP_UNLOCK(vdp, 0);
619 cnp->cn_flags |= PDIRUNLOCK;
620 }
621 return (0);
622 }
623
624 /*
625 * If rewriting (RENAME), return the inode and the
626 * information required to rewrite the present directory
627 * Must get inode of directory entry to verify it's a
628 * regular file, or empty directory.
629 */
630 if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
631 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_lwp);
632 if (error)
633 return (error);
634 /*
635 * Careful about locking second inode.
636 * This can only occur if the target is ".".
637 */
638 if (dp->i_number == foundino)
639 return (EISDIR);
640 if (flags & ISDOTDOT)
641 VOP_UNLOCK(vdp, 0); /* race to get the inode */
642 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
643 if (flags & ISDOTDOT)
644 vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
645 if (error)
646 return (error);
647 *vpp = tdp;
648 cnp->cn_flags |= SAVENAME;
649 if (!lockparent) {
650 VOP_UNLOCK(vdp, 0);
651 cnp->cn_flags |= PDIRUNLOCK;
652 }
653 return (0);
654 }
655
656 /*
657 * Step through the translation in the name. We do not `vput' the
658 * directory because we may need it again if a symbolic link
659 * is relative to the current directory. Instead we save it
660 * unlocked as "pdp". We must get the target inode before unlocking
661 * the directory to insure that the inode will not be removed
662 * before we get it. We prevent deadlock by always fetching
663 * inodes from the root, moving down the directory tree. Thus
664 * when following backward pointers ".." we must unlock the
665 * parent directory before getting the requested directory.
666 * There is a potential race condition here if both the current
667 * and parent directories are removed before the VFS_VGET for the
668 * inode associated with ".." returns. We hope that this occurs
669 * infrequently since we cannot avoid this race condition without
670 * implementing a sophisticated deadlock detection algorithm.
671 * Note also that this simple deadlock detection scheme will not
672 * work if the file system has any hard links other than ".."
673 * that point backwards in the directory structure.
674 */
675 pdp = vdp;
676 if (flags & ISDOTDOT) {
677 VOP_UNLOCK(pdp, 0); /* race to get the inode */
678 cnp->cn_flags |= PDIRUNLOCK;
679 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
680 if (error) {
681 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0)
682 cnp->cn_flags &= ~PDIRUNLOCK;
683 return (error);
684 }
685 if (lockparent && (flags & ISLASTCN)) {
686 if ((error = vn_lock(pdp, LK_EXCLUSIVE))) {
687 vput(tdp);
688 return (error);
689 }
690 cnp->cn_flags &= ~PDIRUNLOCK;
691 }
692 *vpp = tdp;
693 } else if (dp->i_number == foundino) {
694 VREF(vdp); /* we want ourself, ie "." */
695 *vpp = vdp;
696 } else {
697 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
698 if (error)
699 return (error);
700 if (!lockparent || !(flags & ISLASTCN)) {
701 VOP_UNLOCK(pdp, 0);
702 cnp->cn_flags |= PDIRUNLOCK;
703 }
704 *vpp = tdp;
705 }
706
707 /*
708 * Insert name into cache if appropriate.
709 */
710 if (cnp->cn_flags & MAKEENTRY)
711 cache_enter(vdp, *vpp, cnp);
712 return (0);
713 }
714
715 /*
716 * Do consistency checking on a directory entry:
717 * record length must be multiple of 4
718 * entry must fit in rest of its dirblksize block
719 * record must be large enough to contain entry
720 * name is not longer than EXT2FS_MAXNAMLEN
721 * name must be as long as advertised, and null terminated
722 */
723 /*
724 * changed so that it confirms to ext2fs_check_dir_entry
725 */
726 static int
727 ext2fs_dirbadentry(struct vnode *dp, struct ext2fs_direct *de,
728 int entryoffsetinblock)
729 {
730 struct ufsmount *ump = VFSTOUFS(dp->v_mount);
731 int dirblksiz = ump->um_dirblksiz;
732
733 const char *error_msg = NULL;
734 int reclen = fs2h16(de->e2d_reclen);
735 int namlen = de->e2d_namlen;
736
737 if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
738 error_msg = "rec_len is smaller than minimal";
739 else if (reclen % 4 != 0)
740 error_msg = "rec_len % 4 != 0";
741 else if (namlen > EXT2FS_MAXNAMLEN)
742 error_msg = "namlen > EXT2FS_MAXNAMLEN";
743 else if (reclen < EXT2FS_DIRSIZ(namlen))
744 error_msg = "reclen is too small for name_len";
745 else if (entryoffsetinblock + reclen > dirblksiz)
746 error_msg = "directory entry across blocks";
747 else if (fs2h32(de->e2d_ino) >
748 VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
749 error_msg = "inode out of bounds";
750
751 if (error_msg != NULL) {
752 printf( "bad directory entry: %s\n"
753 "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
754 error_msg, entryoffsetinblock,
755 (unsigned long) fs2h32(de->e2d_ino),
756 reclen, namlen);
757 panic("ext2fs_dirbadentry");
758 }
759 return error_msg == NULL ? 0 : 1;
760 }
761
762 /*
763 * Write a directory entry after a call to namei, using the parameters
764 * that it left in nameidata. The argument ip is the inode which the new
765 * directory entry will refer to. Dvp is a pointer to the directory to
766 * be written, which was left locked by namei. Remaining parameters
767 * (dp->i_offset, dp->i_count) indicate how the space for the new
768 * entry is to be obtained.
769 */
770 int
771 ext2fs_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
772 {
773 struct ext2fs_direct *ep, *nep;
774 struct inode *dp;
775 struct buf *bp;
776 struct ext2fs_direct newdir;
777 struct iovec aiov;
778 struct uio auio;
779 u_int dsize;
780 int error, loc, newentrysize, spacefree;
781 char *dirbuf;
782 struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
783 int dirblksiz = ump->um_dirblksiz;
784
785 #ifdef DIAGNOSTIC
786 if ((cnp->cn_flags & SAVENAME) == 0)
787 panic("direnter: missing name");
788 #endif
789 dp = VTOI(dvp);
790 newdir.e2d_ino = h2fs32(ip->i_number);
791 newdir.e2d_namlen = cnp->cn_namelen;
792 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
793 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
794 newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
795 } else {
796 newdir.e2d_type = 0;
797 };
798 memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
799 newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
800 if (dp->i_count == 0) {
801 /*
802 * If dp->i_count is 0, then namei could find no
803 * space in the directory. Here, dp->i_offset will
804 * be on a directory block boundary and we will write the
805 * new entry into a fresh block.
806 */
807 if (dp->i_offset & (dirblksiz - 1))
808 panic("ext2fs_direnter: newblk");
809 auio.uio_offset = dp->i_offset;
810 newdir.e2d_reclen = h2fs16(dirblksiz);
811 auio.uio_resid = newentrysize;
812 aiov.iov_len = newentrysize;
813 aiov.iov_base = (caddr_t)&newdir;
814 auio.uio_iov = &aiov;
815 auio.uio_iovcnt = 1;
816 auio.uio_rw = UIO_WRITE;
817 UIO_SETUP_SYSSPACE(&auio);
818 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
819 if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
820 /* XXX should grow with balloc() */
821 panic("ext2fs_direnter: frag size");
822 else if (!error) {
823 error = ext2fs_setsize(dp,
824 roundup(ext2fs_size(dp), dirblksiz));
825 if (error)
826 return (error);
827 dp->i_flag |= IN_CHANGE;
828 uvm_vnp_setsize(dvp, ext2fs_size(dp));
829 }
830 return (error);
831 }
832
833 /*
834 * If dp->i_count is non-zero, then namei found space
835 * for the new entry in the range dp->i_offset to
836 * dp->i_offset + dp->i_count in the directory.
837 * To use this space, we may have to compact the entries located
838 * there, by copying them together towards the beginning of the
839 * block, leaving the free space in one usable chunk at the end.
840 */
841
842 /*
843 * Get the block containing the space for the new directory entry.
844 */
845 if ((error = ext2fs_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0)
846 return (error);
847 /*
848 * Find space for the new entry. In the simple case, the entry at
849 * offset base will have the space. If it does not, then namei
850 * arranged that compacting the region dp->i_offset to
851 * dp->i_offset + dp->i_count would yield the
852 * space.
853 */
854 ep = (struct ext2fs_direct *)dirbuf;
855 dsize = EXT2FS_DIRSIZ(ep->e2d_namlen);
856 spacefree = fs2h16(ep->e2d_reclen) - dsize;
857 for (loc = fs2h16(ep->e2d_reclen); loc < dp->i_count; ) {
858 nep = (struct ext2fs_direct *)(dirbuf + loc);
859 if (ep->e2d_ino) {
860 /* trim the existing slot */
861 ep->e2d_reclen = h2fs16(dsize);
862 ep = (struct ext2fs_direct *)((char *)ep + dsize);
863 } else {
864 /* overwrite; nothing there; header is ours */
865 spacefree += dsize;
866 }
867 dsize = EXT2FS_DIRSIZ(nep->e2d_namlen);
868 spacefree += fs2h16(nep->e2d_reclen) - dsize;
869 loc += fs2h16(nep->e2d_reclen);
870 memcpy((caddr_t)ep, (caddr_t)nep, dsize);
871 }
872 /*
873 * Update the pointer fields in the previous entry (if any),
874 * copy in the new entry, and write out the block.
875 */
876 if (ep->e2d_ino == 0) {
877 #ifdef DIAGNOSTIC
878 if (spacefree + dsize < newentrysize)
879 panic("ext2fs_direnter: compact1");
880 #endif
881 newdir.e2d_reclen = h2fs16(spacefree + dsize);
882 } else {
883 #ifdef DIAGNOSTIC
884 if (spacefree < newentrysize) {
885 printf("ext2fs_direnter: compact2 %u %u",
886 (u_int)spacefree, (u_int)newentrysize);
887 panic("ext2fs_direnter: compact2");
888 }
889 #endif
890 newdir.e2d_reclen = h2fs16(spacefree);
891 ep->e2d_reclen = h2fs16(dsize);
892 ep = (struct ext2fs_direct *)((char *)ep + dsize);
893 }
894 memcpy((caddr_t)ep, (caddr_t)&newdir, (u_int)newentrysize);
895 error = VOP_BWRITE(bp);
896 dp->i_flag |= IN_CHANGE | IN_UPDATE;
897 if (!error && dp->i_endoff && dp->i_endoff < ext2fs_size(dp))
898 error = ext2fs_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
899 cnp->cn_cred, cnp->cn_lwp->l_proc);
900 return (error);
901 }
902
903 /*
904 * Remove a directory entry after a call to namei, using
905 * the parameters which it left in nameidata. The entry
906 * dp->i_offset contains the offset into the directory of the
907 * entry to be eliminated. The dp->i_count field contains the
908 * size of the previous record in the directory. If this
909 * is 0, the first entry is being deleted, so we need only
910 * zero the inode number to mark the entry as free. If the
911 * entry is not the first in the directory, we must reclaim
912 * the space of the now empty record by adding the record size
913 * to the size of the previous entry.
914 */
915 int
916 ext2fs_dirremove(struct vnode *dvp, struct componentname *cnp)
917 {
918 struct inode *dp;
919 struct ext2fs_direct *ep;
920 struct buf *bp;
921 int error;
922
923 dp = VTOI(dvp);
924 if (dp->i_count == 0) {
925 /*
926 * First entry in block: set d_ino to zero.
927 */
928 error = ext2fs_blkatoff(dvp, (off_t)dp->i_offset,
929 (void *)&ep, &bp);
930 if (error != 0)
931 return (error);
932 ep->e2d_ino = 0;
933 error = VOP_BWRITE(bp);
934 dp->i_flag |= IN_CHANGE | IN_UPDATE;
935 return (error);
936 }
937 /*
938 * Collapse new free space into previous entry.
939 */
940 error = ext2fs_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
941 (void *)&ep, &bp);
942 if (error != 0)
943 return (error);
944 ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + dp->i_reclen);
945 error = VOP_BWRITE(bp);
946 dp->i_flag |= IN_CHANGE | IN_UPDATE;
947 return (error);
948 }
949
950 /*
951 * Rewrite an existing directory entry to point at the inode
952 * supplied. The parameters describing the directory entry are
953 * set up by a call to namei.
954 */
955 int
956 ext2fs_dirrewrite(struct inode *dp, struct inode *ip,
957 struct componentname *cnp)
958 {
959 struct buf *bp;
960 struct ext2fs_direct *ep;
961 struct vnode *vdp = ITOV(dp);
962 int error;
963
964 error = ext2fs_blkatoff(vdp, (off_t)dp->i_offset, (void *)&ep, &bp);
965 if (error != 0)
966 return (error);
967 ep->e2d_ino = h2fs32(ip->i_number);
968 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
969 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
970 ep->e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
971 } else {
972 ep->e2d_type = 0;
973 }
974 error = VOP_BWRITE(bp);
975 dp->i_flag |= IN_CHANGE | IN_UPDATE;
976 return (error);
977 }
978
979 /*
980 * Check if a directory is empty or not.
981 * Inode supplied must be locked.
982 *
983 * Using a struct dirtemplate here is not precisely
984 * what we want, but better than using a struct ext2fs_direct.
985 *
986 * NB: does not handle corrupted directories.
987 */
988 int
989 ext2fs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
990 {
991 off_t off;
992 struct ext2fs_dirtemplate dbuf;
993 struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
994 int error, namlen;
995 size_t count;
996
997 #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
998
999 for (off = 0; off < ext2fs_size(ip); off += fs2h16(dp->e2d_reclen)) {
1000 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
1001 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, NULL);
1002 /*
1003 * Since we read MINDIRSIZ, residual must
1004 * be 0 unless we're at end of file.
1005 */
1006 if (error || count != 0)
1007 return (0);
1008 /* avoid infinite loops */
1009 if (dp->e2d_reclen == 0)
1010 return (0);
1011 /* skip empty entries */
1012 if (dp->e2d_ino == 0)
1013 continue;
1014 /* accept only "." and ".." */
1015 namlen = dp->e2d_namlen;
1016 if (namlen > 2)
1017 return (0);
1018 if (dp->e2d_name[0] != '.')
1019 return (0);
1020 /*
1021 * At this point namlen must be 1 or 2.
1022 * 1 implies ".", 2 implies ".." if second
1023 * char is also "."
1024 */
1025 if (namlen == 1)
1026 continue;
1027 if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
1028 continue;
1029 return (0);
1030 }
1031 return (1);
1032 }
1033
1034 /*
1035 * Check if source directory is in the path of the target directory.
1036 * Target is supplied locked, source is unlocked.
1037 * The target is always vput before returning.
1038 */
1039 int
1040 ext2fs_checkpath(struct inode *source, struct inode *target,
1041 kauth_cred_t cred)
1042 {
1043 struct vnode *vp;
1044 int error, rootino, namlen;
1045 struct ext2fs_dirtemplate dirbuf;
1046 u_int32_t ino;
1047
1048 vp = ITOV(target);
1049 if (target->i_number == source->i_number) {
1050 error = EEXIST;
1051 goto out;
1052 }
1053 rootino = ROOTINO;
1054 error = 0;
1055 if (target->i_number == rootino)
1056 goto out;
1057
1058 for (;;) {
1059 if (vp->v_type != VDIR) {
1060 error = ENOTDIR;
1061 break;
1062 }
1063 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1064 sizeof (struct ext2fs_dirtemplate), (off_t)0,
1065 UIO_SYSSPACE, IO_NODELOCKED, cred, (size_t *)0,
1066 NULL);
1067 if (error != 0)
1068 break;
1069 namlen = dirbuf.dotdot_namlen;
1070 if (namlen != 2 ||
1071 dirbuf.dotdot_name[0] != '.' ||
1072 dirbuf.dotdot_name[1] != '.') {
1073 error = ENOTDIR;
1074 break;
1075 }
1076 ino = fs2h32(dirbuf.dotdot_ino);
1077 if (ino == source->i_number) {
1078 error = EINVAL;
1079 break;
1080 }
1081 if (ino == rootino)
1082 break;
1083 vput(vp);
1084 error = VFS_VGET(vp->v_mount, ino, &vp);
1085 if (error != 0) {
1086 vp = NULL;
1087 break;
1088 }
1089 }
1090
1091 out:
1092 if (error == ENOTDIR) {
1093 printf("checkpath: .. not a directory\n");
1094 panic("checkpath");
1095 }
1096 if (vp != NULL)
1097 vput(vp);
1098 return (error);
1099 }
1100