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