ext2fs_lookup.c revision 1.11 1 /* $NetBSD: ext2fs_lookup.c,v 1.11 1999/08/04 18:40:47 wrstuden 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 ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
306 return (error);
307
308 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
309 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
310 return (EROFS);
311
312 /*
313 * We now have a segment name to search for, and a directory to search.
314 *
315 * Before tediously performing a linear scan of the directory,
316 * check the name cache to see if the directory/name pair
317 * we are looking for is known already.
318 */
319 if ((error = cache_lookup(vdp, vpp, cnp)) != 0) {
320 int vpid; /* capability number of vnode */
321
322 if (error == ENOENT)
323 return (error);
324 /*
325 * Get the next vnode in the path.
326 * See comment below starting `Step through' for
327 * an explaination of the locking protocol.
328 */
329 pdp = vdp;
330 dp = VTOI(*vpp);
331 vdp = *vpp;
332 vpid = vdp->v_id;
333 if (pdp == vdp) { /* lookup on "." */
334 VREF(vdp);
335 error = 0;
336 } else if (flags & ISDOTDOT) {
337 VOP_UNLOCK(pdp, 0);
338 cnp->cn_flags |= PDIRUNLOCK;
339 error = vget(vdp, LK_EXCLUSIVE);
340 if (!error && lockparent && (flags & ISLASTCN)) {
341 error = vn_lock(pdp, LK_EXCLUSIVE);
342 if (error == 0)
343 cnp->cn_flags &= ~PDIRUNLOCK;
344 }
345 } else {
346 error = vget(vdp, LK_EXCLUSIVE);
347 if (!lockparent || error || !(flags & ISLASTCN)) {
348 VOP_UNLOCK(pdp, 0);
349 cnp->cn_flags |= PDIRUNLOCK;
350 }
351 }
352 /*
353 * Check that the capability number did not change
354 * while we were waiting for the lock.
355 */
356 if (!error) {
357 if (vpid == vdp->v_id)
358 return (0);
359 vput(vdp);
360 if (lockparent && pdp != vdp && (flags & ISLASTCN)) {
361 VOP_UNLOCK(pdp, 0);
362 cnp->cn_flags |= PDIRUNLOCK;
363 }
364 }
365 if ((error = vn_lock(pdp, LK_EXCLUSIVE)) != 0)
366 return (error);
367 cnp->cn_flags &= ~PDIRUNLOCK;
368 vdp = pdp;
369 dp = VTOI(pdp);
370 *vpp = NULL;
371 }
372
373 /*
374 * Suppress search for slots unless creating
375 * file and at end of pathname, in which case
376 * we watch for a place to put the new file in
377 * case it doesn't already exist.
378 */
379 slotstatus = FOUND;
380 slotfreespace = slotsize = slotneeded = 0;
381 if ((nameiop == CREATE || nameiop == RENAME) &&
382 (flags & ISLASTCN)) {
383 slotstatus = NONE;
384 slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
385 }
386
387 /*
388 * If there is cached information on a previous search of
389 * this directory, pick up where we last left off.
390 * We cache only lookups as these are the most common
391 * and have the greatest payoff. Caching CREATE has little
392 * benefit as it usually must search the entire directory
393 * to determine that the entry does not exist. Caching the
394 * location of the last DELETE or RENAME has not reduced
395 * profiling time and hence has been removed in the interest
396 * of simplicity.
397 */
398 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
399 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
400 dp->i_diroff > dp->i_e2fs_size) {
401 entryoffsetinblock = 0;
402 dp->i_offset = 0;
403 numdirpasses = 1;
404 } else {
405 dp->i_offset = dp->i_diroff;
406 if ((entryoffsetinblock = dp->i_offset & bmask) &&
407 (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
408 return (error);
409 numdirpasses = 2;
410 }
411 prevoff = dp->i_offset;
412 endsearch = roundup(dp->i_e2fs_size, dirblksize);
413 enduseful = 0;
414
415 searchloop:
416 while (dp->i_offset < endsearch) {
417 /*
418 * If necessary, get the next directory block.
419 */
420 if ((dp->i_offset & bmask) == 0) {
421 if (bp != NULL)
422 brelse(bp);
423 error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset,
424 NULL, &bp);
425 if (error != 0)
426 return (error);
427 entryoffsetinblock = 0;
428 }
429 /*
430 * If still looking for a slot, and at a dirblksize
431 * boundary, have to start looking for free space again.
432 */
433 if (slotstatus == NONE &&
434 (entryoffsetinblock & (dirblksize - 1)) == 0) {
435 slotoffset = -1;
436 slotfreespace = 0;
437 }
438 /*
439 * Get pointer to next entry.
440 * Full validation checks are slow, so we only check
441 * enough to insure forward progress through the
442 * directory. Complete checks can be run by patching
443 * "dirchk" to be true.
444 */
445 ep = (struct ext2fs_direct *)
446 ((char *)bp->b_data + entryoffsetinblock);
447 if (ep->e2d_reclen == 0 ||
448 (dirchk &&
449 ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
450 int i;
451 ufs_dirbad(dp, dp->i_offset, "mangled entry");
452 i = dirblksize -
453 (entryoffsetinblock & (dirblksize - 1));
454 dp->i_offset += i;
455 entryoffsetinblock += i;
456 continue;
457 }
458
459 /*
460 * If an appropriate sized slot has not yet been found,
461 * check to see if one is available. Also accumulate space
462 * in the current block so that we can determine if
463 * compaction is viable.
464 */
465 if (slotstatus != FOUND) {
466 int size = fs2h16(ep->e2d_reclen);
467
468 if (ep->e2d_ino != 0)
469 size -= EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen));
470 if (size > 0) {
471 if (size >= slotneeded) {
472 slotstatus = FOUND;
473 slotoffset = dp->i_offset;
474 slotsize = fs2h16(ep->e2d_reclen);
475 } else if (slotstatus == NONE) {
476 slotfreespace += size;
477 if (slotoffset == -1)
478 slotoffset = dp->i_offset;
479 if (slotfreespace >= slotneeded) {
480 slotstatus = COMPACT;
481 slotsize = dp->i_offset +
482 fs2h16(ep->e2d_reclen) - slotoffset;
483 }
484 }
485 }
486 }
487
488 /*
489 * Check for a name match.
490 */
491 if (ep->e2d_ino) {
492 namlen = fs2h16(ep->e2d_namlen);
493 if (namlen == cnp->cn_namelen &&
494 !memcmp(cnp->cn_nameptr, ep->e2d_name,
495 (unsigned)namlen)) {
496 /*
497 * Save directory entry's inode number and
498 * reclen in ndp->ni_ufs area, and release
499 * directory buffer.
500 */
501 dp->i_ino = fs2h32(ep->e2d_ino);
502 dp->i_reclen = fs2h16(ep->e2d_reclen);
503 brelse(bp);
504 goto found;
505 }
506 }
507 prevoff = dp->i_offset;
508 dp->i_offset += fs2h16(ep->e2d_reclen);
509 entryoffsetinblock += fs2h16(ep->e2d_reclen);
510 if (ep->e2d_ino)
511 enduseful = dp->i_offset;
512 }
513 /* notfound: */
514 /*
515 * If we started in the middle of the directory and failed
516 * to find our target, we must check the beginning as well.
517 */
518 if (numdirpasses == 2) {
519 numdirpasses--;
520 dp->i_offset = 0;
521 endsearch = dp->i_diroff;
522 goto searchloop;
523 }
524 if (bp != NULL)
525 brelse(bp);
526 /*
527 * If creating, and at end of pathname and current
528 * directory has not been removed, then can consider
529 * allowing file to be created.
530 */
531 if ((nameiop == CREATE || nameiop == RENAME) &&
532 (flags & ISLASTCN) && dp->i_e2fs_nlink != 0) {
533 /*
534 * Access for write is interpreted as allowing
535 * creation of files in the directory.
536 */
537 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0)
538 return (error);
539 /*
540 * Return an indication of where the new directory
541 * entry should be put. If we didn't find a slot,
542 * then set dp->i_count to 0 indicating
543 * that the new slot belongs at the end of the
544 * directory. If we found a slot, then the new entry
545 * can be put in the range from dp->i_offset to
546 * dp->i_offset + dp->i_count.
547 */
548 if (slotstatus == NONE) {
549 dp->i_offset = roundup(dp->i_e2fs_size, dirblksize);
550 dp->i_count = 0;
551 enduseful = dp->i_offset;
552 } else {
553 dp->i_offset = slotoffset;
554 dp->i_count = slotsize;
555 if (enduseful < slotoffset + slotsize)
556 enduseful = slotoffset + slotsize;
557 }
558 dp->i_endoff = roundup(enduseful, dirblksize);
559 dp->i_flag |= IN_CHANGE | IN_UPDATE;
560 /*
561 * We return with the directory locked, so that
562 * the parameters we set up above will still be
563 * valid if we actually decide to do a direnter().
564 * We return ni_vp == NULL to indicate that the entry
565 * does not currently exist; we leave a pointer to
566 * the (locked) directory inode in ndp->ni_dvp.
567 * The pathname buffer is saved so that the name
568 * can be obtained later.
569 *
570 * NB - if the directory is unlocked, then this
571 * information cannot be used.
572 */
573 cnp->cn_flags |= SAVENAME;
574 if (!lockparent) {
575 VOP_UNLOCK(vdp, 0);
576 cnp->cn_flags |= PDIRUNLOCK;
577 }
578 return (EJUSTRETURN);
579 }
580 /*
581 * Insert name into cache (as non-existent) if appropriate.
582 */
583 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
584 cache_enter(vdp, *vpp, cnp);
585 return (ENOENT);
586
587 found:
588 /*
589 * Check that directory length properly reflects presence
590 * of this entry.
591 */
592 if (entryoffsetinblock + EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen))
593 > dp->i_e2fs_size) {
594 ufs_dirbad(dp, dp->i_offset, "i_size too small");
595 dp->i_e2fs_size = entryoffsetinblock +
596 EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen));
597 dp->i_flag |= IN_CHANGE | IN_UPDATE;
598 }
599
600 /*
601 * Found component in pathname.
602 * If the final component of path name, save information
603 * in the cache as to where the entry was found.
604 */
605 if ((flags & ISLASTCN) && nameiop == LOOKUP)
606 dp->i_diroff = dp->i_offset &~ (dirblksize - 1);
607
608 /*
609 * If deleting, and at end of pathname, return
610 * parameters which can be used to remove file.
611 * If the wantparent flag isn't set, we return only
612 * the directory (in ndp->ni_dvp), otherwise we go
613 * on and lock the inode, being careful with ".".
614 */
615 if (nameiop == DELETE && (flags & ISLASTCN)) {
616 /*
617 * Write access to directory required to delete files.
618 */
619 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0)
620 return (error);
621 /*
622 * Return pointer to current entry in dp->i_offset,
623 * and distance past previous entry (if there
624 * is a previous entry in this block) in dp->i_count.
625 * Save directory inode pointer in ndp->ni_dvp for dirremove().
626 */
627 if ((dp->i_offset & (dirblksize - 1)) == 0)
628 dp->i_count = 0;
629 else
630 dp->i_count = dp->i_offset - prevoff;
631 if (dp->i_number == dp->i_ino) {
632 VREF(vdp);
633 *vpp = vdp;
634 return (0);
635 }
636 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
637 return (error);
638 /*
639 * If directory is "sticky", then user must own
640 * the directory, or the file in it, else she
641 * may not delete it (unless she's root). This
642 * implements append-only directories.
643 */
644 if ((dp->i_e2fs_mode & ISVTX) &&
645 cred->cr_uid != 0 &&
646 cred->cr_uid != dp->i_e2fs_uid &&
647 VTOI(tdp)->i_e2fs_uid != cred->cr_uid) {
648 vput(tdp);
649 return (EPERM);
650 }
651 *vpp = tdp;
652 if (!lockparent) {
653 VOP_UNLOCK(vdp, 0);
654 cnp->cn_flags |= PDIRUNLOCK;
655 }
656 return (0);
657 }
658
659 /*
660 * If rewriting (RENAME), return the inode and the
661 * information required to rewrite the present directory
662 * Must get inode of directory entry to verify it's a
663 * regular file, or empty directory.
664 */
665 if (nameiop == RENAME && wantparent &&
666 (flags & ISLASTCN)) {
667 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
668 if (error)
669 return (error);
670 /*
671 * Careful about locking second inode.
672 * This can only occur if the target is ".".
673 */
674 if (dp->i_number == dp->i_ino)
675 return (EISDIR);
676 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
677 if (error)
678 return (error);
679 *vpp = tdp;
680 cnp->cn_flags |= SAVENAME;
681 if (!lockparent) {
682 VOP_UNLOCK(vdp, 0);
683 cnp->cn_flags |= PDIRUNLOCK;
684 }
685 return (0);
686 }
687
688 /*
689 * Step through the translation in the name. We do not `vput' the
690 * directory because we may need it again if a symbolic link
691 * is relative to the current directory. Instead we save it
692 * unlocked as "pdp". We must get the target inode before unlocking
693 * the directory to insure that the inode will not be removed
694 * before we get it. We prevent deadlock by always fetching
695 * inodes from the root, moving down the directory tree. Thus
696 * when following backward pointers ".." we must unlock the
697 * parent directory before getting the requested directory.
698 * There is a potential race condition here if both the current
699 * and parent directories are removed before the VFS_VGET for the
700 * inode associated with ".." returns. We hope that this occurs
701 * infrequently since we cannot avoid this race condition without
702 * implementing a sophisticated deadlock detection algorithm.
703 * Note also that this simple deadlock detection scheme will not
704 * work if the file system has any hard links other than ".."
705 * that point backwards in the directory structure.
706 */
707 pdp = vdp;
708 if (flags & ISDOTDOT) {
709 VOP_UNLOCK(pdp, 0); /* race to get the inode */
710 cnp->cn_flags |= PDIRUNLOCK;
711 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
712 if (error) {
713 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0)
714 cnp->cn_flags &= ~PDIRUNLOCK;
715 return (error);
716 }
717 if (lockparent && (flags & ISLASTCN)) {
718 if ((error = vn_lock(pdp, LK_EXCLUSIVE))) {
719 vput(tdp);
720 return (error);
721 }
722 cnp->cn_flags &= ~PDIRUNLOCK;
723 }
724 *vpp = tdp;
725 } else if (dp->i_number == dp->i_ino) {
726 VREF(vdp); /* we want ourself, ie "." */
727 *vpp = vdp;
728 } else {
729 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
730 return (error);
731 if (!lockparent || !(flags & ISLASTCN)) {
732 VOP_UNLOCK(pdp, 0);
733 cnp->cn_flags |= PDIRUNLOCK;
734 }
735 *vpp = tdp;
736 }
737
738 /*
739 * Insert name into cache if appropriate.
740 */
741 if (cnp->cn_flags & MAKEENTRY)
742 cache_enter(vdp, *vpp, cnp);
743 return (0);
744 }
745
746 /*
747 * Do consistency checking on a directory entry:
748 * record length must be multiple of 4
749 * entry must fit in rest of its dirblksize block
750 * record must be large enough to contain entry
751 * name is not longer than MAXNAMLEN
752 * name must be as long as advertised, and null terminated
753 */
754 /*
755 * changed so that it confirms to ext2fs_check_dir_entry
756 */
757 static int
758 ext2fs_dirbadentry(dp, de, entryoffsetinblock)
759 struct vnode *dp;
760 register struct ext2fs_direct *de;
761 int entryoffsetinblock;
762 {
763 int dirblksize = VTOI(dp)->i_e2fs->e2fs_bsize;
764
765 char * error_msg = NULL;
766 int reclen = fs2h16(de->e2d_reclen);
767 int namlen = fs2h16(de->e2d_namlen);
768
769 if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
770 error_msg = "rec_len is smaller than minimal";
771 else if (reclen % 4 != 0)
772 error_msg = "rec_len % 4 != 0";
773 else if (reclen < EXT2FS_DIRSIZ(namlen))
774 error_msg = "reclen is too small for name_len";
775 else if (entryoffsetinblock + reclen > dirblksize)
776 error_msg = "directory entry across blocks";
777 else if (fs2h32(de->e2d_ino) >
778 VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
779 error_msg = "inode out of bounds";
780
781 if (error_msg != NULL) {
782 printf( "bad directory entry: %s\n"
783 "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
784 error_msg, entryoffsetinblock,
785 (unsigned long) fs2h32(de->e2d_ino),
786 reclen, namlen);
787 panic("ext2fs_dirbadentry");
788 }
789 return error_msg == NULL ? 0 : 1;
790 }
791
792 /*
793 * Write a directory entry after a call to namei, using the parameters
794 * that it left in nameidata. The argument ip is the inode which the new
795 * directory entry will refer to. Dvp is a pointer to the directory to
796 * be written, which was left locked by namei. Remaining parameters
797 * (dp->i_offset, dp->i_count) indicate how the space for the new
798 * entry is to be obtained.
799 */
800 int
801 ext2fs_direnter(ip, dvp, cnp)
802 struct inode *ip;
803 struct vnode *dvp;
804 register struct componentname *cnp;
805 {
806 register struct ext2fs_direct *ep, *nep;
807 register struct inode *dp;
808 struct buf *bp;
809 struct ext2fs_direct newdir;
810 struct iovec aiov;
811 struct uio auio;
812 u_int dsize;
813 int error, loc, newentrysize, spacefree;
814 char *dirbuf;
815 int dirblksize = ip->i_e2fs->e2fs_bsize;
816
817
818 #ifdef DIAGNOSTIC
819 if ((cnp->cn_flags & SAVENAME) == 0)
820 panic("direnter: missing name");
821 #endif
822 dp = VTOI(dvp);
823 newdir.e2d_ino = h2fs32(ip->i_number);
824 newdir.e2d_namlen = h2fs16(cnp->cn_namelen);
825 memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
826 newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
827 if (dp->i_count == 0) {
828 /*
829 * If dp->i_count is 0, then namei could find no
830 * space in the directory. Here, dp->i_offset will
831 * be on a directory block boundary and we will write the
832 * new entry into a fresh block.
833 */
834 if (dp->i_offset & (dirblksize - 1))
835 panic("ext2fs_direnter: newblk");
836 auio.uio_offset = dp->i_offset;
837 newdir.e2d_reclen = h2fs16(dirblksize);
838 auio.uio_resid = newentrysize;
839 aiov.iov_len = newentrysize;
840 aiov.iov_base = (caddr_t)&newdir;
841 auio.uio_iov = &aiov;
842 auio.uio_iovcnt = 1;
843 auio.uio_rw = UIO_WRITE;
844 auio.uio_segflg = UIO_SYSSPACE;
845 auio.uio_procp = (struct proc *)0;
846 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
847 if (dirblksize >
848 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
849 /* XXX should grow with balloc() */
850 panic("ext2fs_direnter: frag size");
851 else if (!error) {
852 dp->i_e2fs_size = roundup(dp->i_e2fs_size, dirblksize);
853 dp->i_flag |= IN_CHANGE;
854 }
855 return (error);
856 }
857
858 /*
859 * If dp->i_count is non-zero, then namei found space
860 * for the new entry in the range dp->i_offset to
861 * dp->i_offset + dp->i_count in the directory.
862 * To use this space, we may have to compact the entries located
863 * there, by copying them together towards the beginning of the
864 * block, leaving the free space in one usable chunk at the end.
865 */
866
867 /*
868 * Get the block containing the space for the new directory entry.
869 */
870 if ((error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0)
871 return (error);
872 /*
873 * Find space for the new entry. In the simple case, the entry at
874 * offset base will have the space. If it does not, then namei
875 * arranged that compacting the region dp->i_offset to
876 * dp->i_offset + dp->i_count would yield the
877 * space.
878 */
879 ep = (struct ext2fs_direct *)dirbuf;
880 dsize = EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen));
881 spacefree = fs2h16(ep->e2d_reclen) - dsize;
882 for (loc = fs2h16(ep->e2d_reclen); loc < dp->i_count; ) {
883 nep = (struct ext2fs_direct *)(dirbuf + loc);
884 if (ep->e2d_ino) {
885 /* trim the existing slot */
886 ep->e2d_reclen = h2fs16(dsize);
887 ep = (struct ext2fs_direct *)((char *)ep + dsize);
888 } else {
889 /* overwrite; nothing there; header is ours */
890 spacefree += dsize;
891 }
892 dsize = EXT2FS_DIRSIZ(fs2h16(nep->e2d_namlen));
893 spacefree += fs2h16(nep->e2d_reclen) - dsize;
894 loc += fs2h16(nep->e2d_reclen);
895 memcpy((caddr_t)ep, (caddr_t)nep, dsize);
896 }
897 /*
898 * Update the pointer fields in the previous entry (if any),
899 * copy in the new entry, and write out the block.
900 */
901 if (ep->e2d_ino == 0) {
902 #ifdef DIAGNOSTIC
903 if (spacefree + dsize < newentrysize)
904 panic("ext2fs_direnter: compact1");
905 #endif
906 newdir.e2d_reclen = h2fs16(spacefree + dsize);
907 } else {
908 #ifdef DIAGNOSTIC
909 if (spacefree < newentrysize) {
910 printf("ext2fs_direnter: compact2 %u %u",
911 (u_int)spacefree, (u_int)newentrysize);
912 panic("ext2fs_direnter: compact2");
913 }
914 #endif
915 newdir.e2d_reclen = h2fs16(spacefree);
916 ep->e2d_reclen = h2fs16(dsize);
917 ep = (struct ext2fs_direct *)((char *)ep + dsize);
918 }
919 memcpy((caddr_t)ep, (caddr_t)&newdir, (u_int)newentrysize);
920 error = VOP_BWRITE(bp);
921 dp->i_flag |= IN_CHANGE | IN_UPDATE;
922 if (!error && dp->i_endoff && dp->i_endoff < dp->i_e2fs_size)
923 error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC,
924 cnp->cn_cred, cnp->cn_proc);
925 return (error);
926 }
927
928 /*
929 * Remove a directory entry after a call to namei, using
930 * the parameters which it left in nameidata. The entry
931 * dp->i_offset contains the offset into the directory of the
932 * entry to be eliminated. The dp->i_count field contains the
933 * size of the previous record in the directory. If this
934 * is 0, the first entry is being deleted, so we need only
935 * zero the inode number to mark the entry as free. If the
936 * entry is not the first in the directory, we must reclaim
937 * the space of the now empty record by adding the record size
938 * to the size of the previous entry.
939 */
940 int
941 ext2fs_dirremove(dvp, cnp)
942 struct vnode *dvp;
943 struct componentname *cnp;
944 {
945 register struct inode *dp;
946 struct ext2fs_direct *ep;
947 struct buf *bp;
948 int error;
949
950 dp = VTOI(dvp);
951 if (dp->i_count == 0) {
952 /*
953 * First entry in block: set d_ino to zero.
954 */
955 error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset,
956 (char **)&ep, &bp);
957 if (error != 0)
958 return (error);
959 ep->e2d_ino = 0;
960 error = VOP_BWRITE(bp);
961 dp->i_flag |= IN_CHANGE | IN_UPDATE;
962 return (error);
963 }
964 /*
965 * Collapse new free space into previous entry.
966 */
967 error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
968 (char **)&ep, &bp);
969 if (error != 0)
970 return (error);
971 ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + dp->i_reclen);
972 error = VOP_BWRITE(bp);
973 dp->i_flag |= IN_CHANGE | IN_UPDATE;
974 return (error);
975 }
976
977 /*
978 * Rewrite an existing directory entry to point at the inode
979 * supplied. The parameters describing the directory entry are
980 * set up by a call to namei.
981 */
982 int
983 ext2fs_dirrewrite(dp, ip, cnp)
984 struct inode *dp, *ip;
985 struct componentname *cnp;
986 {
987 struct buf *bp;
988 struct ext2fs_direct *ep;
989 struct vnode *vdp = ITOV(dp);
990 int error;
991
992 error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
993 if (error != 0)
994 return (error);
995 ep->e2d_ino = h2fs32(ip->i_number);
996 error = VOP_BWRITE(bp);
997 dp->i_flag |= IN_CHANGE | IN_UPDATE;
998 return (error);
999 }
1000
1001 /*
1002 * Check if a directory is empty or not.
1003 * Inode supplied must be locked.
1004 *
1005 * Using a struct dirtemplate here is not precisely
1006 * what we want, but better than using a struct ext2fs_direct.
1007 *
1008 * NB: does not handle corrupted directories.
1009 */
1010 int
1011 ext2fs_dirempty(ip, parentino, cred)
1012 register struct inode *ip;
1013 ino_t parentino;
1014 struct ucred *cred;
1015 {
1016 register off_t off;
1017 struct ext2fs_dirtemplate dbuf;
1018 register struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
1019 int error, namlen;
1020 size_t count;
1021
1022 #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
1023
1024 for (off = 0; off < ip->i_e2fs_size; off += fs2h16(dp->e2d_reclen)) {
1025 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
1026 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
1027 /*
1028 * Since we read MINDIRSIZ, residual must
1029 * be 0 unless we're at end of file.
1030 */
1031 if (error || count != 0)
1032 return (0);
1033 /* avoid infinite loops */
1034 if (dp->e2d_reclen == 0)
1035 return (0);
1036 /* skip empty entries */
1037 if (dp->e2d_ino == 0)
1038 continue;
1039 /* accept only "." and ".." */
1040 namlen = fs2h16(dp->e2d_namlen);
1041 if (namlen > 2)
1042 return (0);
1043 if (dp->e2d_name[0] != '.')
1044 return (0);
1045 /*
1046 * At this point namlen must be 1 or 2.
1047 * 1 implies ".", 2 implies ".." if second
1048 * char is also "."
1049 */
1050 if (namlen == 1)
1051 continue;
1052 if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
1053 continue;
1054 return (0);
1055 }
1056 return (1);
1057 }
1058
1059 /*
1060 * Check if source directory is in the path of the target directory.
1061 * Target is supplied locked, source is unlocked.
1062 * The target is always vput before returning.
1063 */
1064 int
1065 ext2fs_checkpath(source, target, cred)
1066 struct inode *source, *target;
1067 struct ucred *cred;
1068 {
1069 struct vnode *vp;
1070 int error, rootino, namlen;
1071 struct ext2fs_dirtemplate dirbuf;
1072 u_int32_t ino;
1073
1074 vp = ITOV(target);
1075 if (target->i_number == source->i_number) {
1076 error = EEXIST;
1077 goto out;
1078 }
1079 rootino = ROOTINO;
1080 error = 0;
1081 if (target->i_number == rootino)
1082 goto out;
1083
1084 for (;;) {
1085 if (vp->v_type != VDIR) {
1086 error = ENOTDIR;
1087 break;
1088 }
1089 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1090 sizeof (struct ext2fs_dirtemplate), (off_t)0,
1091 UIO_SYSSPACE, IO_NODELOCKED, cred, (size_t *)0,
1092 (struct proc *)0);
1093 if (error != 0)
1094 break;
1095 namlen = fs2h16(dirbuf.dotdot_namlen);
1096 if (namlen != 2 ||
1097 dirbuf.dotdot_name[0] != '.' ||
1098 dirbuf.dotdot_name[1] != '.') {
1099 error = ENOTDIR;
1100 break;
1101 }
1102 ino = fs2h32(dirbuf.dotdot_ino);
1103 if (ino == source->i_number) {
1104 error = EINVAL;
1105 break;
1106 }
1107 if (ino == rootino)
1108 break;
1109 vput(vp);
1110 error = VFS_VGET(vp->v_mount, ino, &vp);
1111 if (error != 0) {
1112 vp = NULL;
1113 break;
1114 }
1115 }
1116
1117 out:
1118 if (error == ENOTDIR) {
1119 printf("checkpath: .. not a directory\n");
1120 panic("checkpath");
1121 }
1122 if (vp != NULL)
1123 vput(vp);
1124 return (error);
1125 }
1126