ext2fs_lookup.c revision 1.10 1 /* $NetBSD: ext2fs_lookup.c,v 1.10 1999/08/02 22:58:29 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 (error = vn_lock(pdp, LK_EXCLUSIVE))) {
719 vput(tdp);
720 return (error);
721 }
722 cnp->cn_flags &= ~PDIRUNLOCK;
723 *vpp = tdp;
724 } else if (dp->i_number == dp->i_ino) {
725 VREF(vdp); /* we want ourself, ie "." */
726 *vpp = vdp;
727 } else {
728 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
729 return (error);
730 if (!lockparent || !(flags & ISLASTCN)) {
731 VOP_UNLOCK(pdp, 0);
732 cnp->cn_flags |= PDIRUNLOCK;
733 }
734 *vpp = tdp;
735 }
736
737 /*
738 * Insert name into cache if appropriate.
739 */
740 if (cnp->cn_flags & MAKEENTRY)
741 cache_enter(vdp, *vpp, cnp);
742 return (0);
743 }
744
745 /*
746 * Do consistency checking on a directory entry:
747 * record length must be multiple of 4
748 * entry must fit in rest of its dirblksize block
749 * record must be large enough to contain entry
750 * name is not longer than MAXNAMLEN
751 * name must be as long as advertised, and null terminated
752 */
753 /*
754 * changed so that it confirms to ext2fs_check_dir_entry
755 */
756 static int
757 ext2fs_dirbadentry(dp, de, entryoffsetinblock)
758 struct vnode *dp;
759 register struct ext2fs_direct *de;
760 int entryoffsetinblock;
761 {
762 int dirblksize = VTOI(dp)->i_e2fs->e2fs_bsize;
763
764 char * error_msg = NULL;
765 int reclen = fs2h16(de->e2d_reclen);
766 int namlen = fs2h16(de->e2d_namlen);
767
768 if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
769 error_msg = "rec_len is smaller than minimal";
770 else if (reclen % 4 != 0)
771 error_msg = "rec_len % 4 != 0";
772 else if (reclen < EXT2FS_DIRSIZ(namlen))
773 error_msg = "reclen is too small for name_len";
774 else if (entryoffsetinblock + reclen > dirblksize)
775 error_msg = "directory entry across blocks";
776 else if (fs2h32(de->e2d_ino) >
777 VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
778 error_msg = "inode out of bounds";
779
780 if (error_msg != NULL) {
781 printf( "bad directory entry: %s\n"
782 "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
783 error_msg, entryoffsetinblock,
784 (unsigned long) fs2h32(de->e2d_ino),
785 reclen, namlen);
786 panic("ext2fs_dirbadentry");
787 }
788 return error_msg == NULL ? 0 : 1;
789 }
790
791 /*
792 * Write a directory entry after a call to namei, using the parameters
793 * that it left in nameidata. The argument ip is the inode which the new
794 * directory entry will refer to. Dvp is a pointer to the directory to
795 * be written, which was left locked by namei. Remaining parameters
796 * (dp->i_offset, dp->i_count) indicate how the space for the new
797 * entry is to be obtained.
798 */
799 int
800 ext2fs_direnter(ip, dvp, cnp)
801 struct inode *ip;
802 struct vnode *dvp;
803 register struct componentname *cnp;
804 {
805 register struct ext2fs_direct *ep, *nep;
806 register struct inode *dp;
807 struct buf *bp;
808 struct ext2fs_direct newdir;
809 struct iovec aiov;
810 struct uio auio;
811 u_int dsize;
812 int error, loc, newentrysize, spacefree;
813 char *dirbuf;
814 int dirblksize = ip->i_e2fs->e2fs_bsize;
815
816
817 #ifdef DIAGNOSTIC
818 if ((cnp->cn_flags & SAVENAME) == 0)
819 panic("direnter: missing name");
820 #endif
821 dp = VTOI(dvp);
822 newdir.e2d_ino = h2fs32(ip->i_number);
823 newdir.e2d_namlen = h2fs16(cnp->cn_namelen);
824 memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
825 newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
826 if (dp->i_count == 0) {
827 /*
828 * If dp->i_count is 0, then namei could find no
829 * space in the directory. Here, dp->i_offset will
830 * be on a directory block boundary and we will write the
831 * new entry into a fresh block.
832 */
833 if (dp->i_offset & (dirblksize - 1))
834 panic("ext2fs_direnter: newblk");
835 auio.uio_offset = dp->i_offset;
836 newdir.e2d_reclen = h2fs16(dirblksize);
837 auio.uio_resid = newentrysize;
838 aiov.iov_len = newentrysize;
839 aiov.iov_base = (caddr_t)&newdir;
840 auio.uio_iov = &aiov;
841 auio.uio_iovcnt = 1;
842 auio.uio_rw = UIO_WRITE;
843 auio.uio_segflg = UIO_SYSSPACE;
844 auio.uio_procp = (struct proc *)0;
845 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
846 if (dirblksize >
847 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
848 /* XXX should grow with balloc() */
849 panic("ext2fs_direnter: frag size");
850 else if (!error) {
851 dp->i_e2fs_size = roundup(dp->i_e2fs_size, dirblksize);
852 dp->i_flag |= IN_CHANGE;
853 }
854 return (error);
855 }
856
857 /*
858 * If dp->i_count is non-zero, then namei found space
859 * for the new entry in the range dp->i_offset to
860 * dp->i_offset + dp->i_count in the directory.
861 * To use this space, we may have to compact the entries located
862 * there, by copying them together towards the beginning of the
863 * block, leaving the free space in one usable chunk at the end.
864 */
865
866 /*
867 * Get the block containing the space for the new directory entry.
868 */
869 if ((error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0)
870 return (error);
871 /*
872 * Find space for the new entry. In the simple case, the entry at
873 * offset base will have the space. If it does not, then namei
874 * arranged that compacting the region dp->i_offset to
875 * dp->i_offset + dp->i_count would yield the
876 * space.
877 */
878 ep = (struct ext2fs_direct *)dirbuf;
879 dsize = EXT2FS_DIRSIZ(fs2h16(ep->e2d_namlen));
880 spacefree = fs2h16(ep->e2d_reclen) - dsize;
881 for (loc = fs2h16(ep->e2d_reclen); loc < dp->i_count; ) {
882 nep = (struct ext2fs_direct *)(dirbuf + loc);
883 if (ep->e2d_ino) {
884 /* trim the existing slot */
885 ep->e2d_reclen = h2fs16(dsize);
886 ep = (struct ext2fs_direct *)((char *)ep + dsize);
887 } else {
888 /* overwrite; nothing there; header is ours */
889 spacefree += dsize;
890 }
891 dsize = EXT2FS_DIRSIZ(fs2h16(nep->e2d_namlen));
892 spacefree += fs2h16(nep->e2d_reclen) - dsize;
893 loc += fs2h16(nep->e2d_reclen);
894 memcpy((caddr_t)ep, (caddr_t)nep, dsize);
895 }
896 /*
897 * Update the pointer fields in the previous entry (if any),
898 * copy in the new entry, and write out the block.
899 */
900 if (ep->e2d_ino == 0) {
901 #ifdef DIAGNOSTIC
902 if (spacefree + dsize < newentrysize)
903 panic("ext2fs_direnter: compact1");
904 #endif
905 newdir.e2d_reclen = h2fs16(spacefree + dsize);
906 } else {
907 #ifdef DIAGNOSTIC
908 if (spacefree < newentrysize) {
909 printf("ext2fs_direnter: compact2 %u %u",
910 (u_int)spacefree, (u_int)newentrysize);
911 panic("ext2fs_direnter: compact2");
912 }
913 #endif
914 newdir.e2d_reclen = h2fs16(spacefree);
915 ep->e2d_reclen = h2fs16(dsize);
916 ep = (struct ext2fs_direct *)((char *)ep + dsize);
917 }
918 memcpy((caddr_t)ep, (caddr_t)&newdir, (u_int)newentrysize);
919 error = VOP_BWRITE(bp);
920 dp->i_flag |= IN_CHANGE | IN_UPDATE;
921 if (!error && dp->i_endoff && dp->i_endoff < dp->i_e2fs_size)
922 error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC,
923 cnp->cn_cred, cnp->cn_proc);
924 return (error);
925 }
926
927 /*
928 * Remove a directory entry after a call to namei, using
929 * the parameters which it left in nameidata. The entry
930 * dp->i_offset contains the offset into the directory of the
931 * entry to be eliminated. The dp->i_count field contains the
932 * size of the previous record in the directory. If this
933 * is 0, the first entry is being deleted, so we need only
934 * zero the inode number to mark the entry as free. If the
935 * entry is not the first in the directory, we must reclaim
936 * the space of the now empty record by adding the record size
937 * to the size of the previous entry.
938 */
939 int
940 ext2fs_dirremove(dvp, cnp)
941 struct vnode *dvp;
942 struct componentname *cnp;
943 {
944 register struct inode *dp;
945 struct ext2fs_direct *ep;
946 struct buf *bp;
947 int error;
948
949 dp = VTOI(dvp);
950 if (dp->i_count == 0) {
951 /*
952 * First entry in block: set d_ino to zero.
953 */
954 error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset,
955 (char **)&ep, &bp);
956 if (error != 0)
957 return (error);
958 ep->e2d_ino = 0;
959 error = VOP_BWRITE(bp);
960 dp->i_flag |= IN_CHANGE | IN_UPDATE;
961 return (error);
962 }
963 /*
964 * Collapse new free space into previous entry.
965 */
966 error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
967 (char **)&ep, &bp);
968 if (error != 0)
969 return (error);
970 ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + dp->i_reclen);
971 error = VOP_BWRITE(bp);
972 dp->i_flag |= IN_CHANGE | IN_UPDATE;
973 return (error);
974 }
975
976 /*
977 * Rewrite an existing directory entry to point at the inode
978 * supplied. The parameters describing the directory entry are
979 * set up by a call to namei.
980 */
981 int
982 ext2fs_dirrewrite(dp, ip, cnp)
983 struct inode *dp, *ip;
984 struct componentname *cnp;
985 {
986 struct buf *bp;
987 struct ext2fs_direct *ep;
988 struct vnode *vdp = ITOV(dp);
989 int error;
990
991 error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
992 if (error != 0)
993 return (error);
994 ep->e2d_ino = h2fs32(ip->i_number);
995 error = VOP_BWRITE(bp);
996 dp->i_flag |= IN_CHANGE | IN_UPDATE;
997 return (error);
998 }
999
1000 /*
1001 * Check if a directory is empty or not.
1002 * Inode supplied must be locked.
1003 *
1004 * Using a struct dirtemplate here is not precisely
1005 * what we want, but better than using a struct ext2fs_direct.
1006 *
1007 * NB: does not handle corrupted directories.
1008 */
1009 int
1010 ext2fs_dirempty(ip, parentino, cred)
1011 register struct inode *ip;
1012 ino_t parentino;
1013 struct ucred *cred;
1014 {
1015 register off_t off;
1016 struct ext2fs_dirtemplate dbuf;
1017 register struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
1018 int error, namlen;
1019 size_t count;
1020
1021 #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
1022
1023 for (off = 0; off < ip->i_e2fs_size; off += fs2h16(dp->e2d_reclen)) {
1024 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
1025 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
1026 /*
1027 * Since we read MINDIRSIZ, residual must
1028 * be 0 unless we're at end of file.
1029 */
1030 if (error || count != 0)
1031 return (0);
1032 /* avoid infinite loops */
1033 if (dp->e2d_reclen == 0)
1034 return (0);
1035 /* skip empty entries */
1036 if (dp->e2d_ino == 0)
1037 continue;
1038 /* accept only "." and ".." */
1039 namlen = fs2h16(dp->e2d_namlen);
1040 if (namlen > 2)
1041 return (0);
1042 if (dp->e2d_name[0] != '.')
1043 return (0);
1044 /*
1045 * At this point namlen must be 1 or 2.
1046 * 1 implies ".", 2 implies ".." if second
1047 * char is also "."
1048 */
1049 if (namlen == 1)
1050 continue;
1051 if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
1052 continue;
1053 return (0);
1054 }
1055 return (1);
1056 }
1057
1058 /*
1059 * Check if source directory is in the path of the target directory.
1060 * Target is supplied locked, source is unlocked.
1061 * The target is always vput before returning.
1062 */
1063 int
1064 ext2fs_checkpath(source, target, cred)
1065 struct inode *source, *target;
1066 struct ucred *cred;
1067 {
1068 struct vnode *vp;
1069 int error, rootino, namlen;
1070 struct ext2fs_dirtemplate dirbuf;
1071 u_int32_t ino;
1072
1073 vp = ITOV(target);
1074 if (target->i_number == source->i_number) {
1075 error = EEXIST;
1076 goto out;
1077 }
1078 rootino = ROOTINO;
1079 error = 0;
1080 if (target->i_number == rootino)
1081 goto out;
1082
1083 for (;;) {
1084 if (vp->v_type != VDIR) {
1085 error = ENOTDIR;
1086 break;
1087 }
1088 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1089 sizeof (struct ext2fs_dirtemplate), (off_t)0,
1090 UIO_SYSSPACE, IO_NODELOCKED, cred, (size_t *)0,
1091 (struct proc *)0);
1092 if (error != 0)
1093 break;
1094 namlen = fs2h16(dirbuf.dotdot_namlen);
1095 if (namlen != 2 ||
1096 dirbuf.dotdot_name[0] != '.' ||
1097 dirbuf.dotdot_name[1] != '.') {
1098 error = ENOTDIR;
1099 break;
1100 }
1101 ino = fs2h32(dirbuf.dotdot_ino);
1102 if (ino == source->i_number) {
1103 error = EINVAL;
1104 break;
1105 }
1106 if (ino == rootino)
1107 break;
1108 vput(vp);
1109 error = VFS_VGET(vp->v_mount, ino, &vp);
1110 if (error != 0) {
1111 vp = NULL;
1112 break;
1113 }
1114 }
1115
1116 out:
1117 if (error == ENOTDIR) {
1118 printf("checkpath: .. not a directory\n");
1119 panic("checkpath");
1120 }
1121 if (vp != NULL)
1122 vput(vp);
1123 return (error);
1124 }
1125