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