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