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