ext2fs_lookup.c revision 1.35 1 /* $NetBSD: ext2fs_lookup.c,v 1.35 2005/08/19 02:04:08 christos 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. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.35 2005/08/19 02:04:08 christos Exp $");
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/namei.h>
56 #include <sys/buf.h>
57 #include <sys/file.h>
58 #include <sys/mount.h>
59 #include <sys/vnode.h>
60 #include <sys/malloc.h>
61 #include <sys/dirent.h>
62
63 #include <ufs/ufs/inode.h>
64 #include <ufs/ufs/ufsmount.h>
65 #include <ufs/ufs/ufs_extern.h>
66
67 #include <ufs/ext2fs/ext2fs_extern.h>
68 #include <ufs/ext2fs/ext2fs_dir.h>
69 #include <ufs/ext2fs/ext2fs.h>
70
71 extern int dirchk;
72
73 static void ext2fs_dirconv2ffs __P((struct ext2fs_direct *e2dir,
74 struct dirent *ffsdir));
75 static int ext2fs_dirbadentry __P((struct vnode *dp,
76 struct ext2fs_direct *de,
77 int entryoffsetinblock));
78
79 /*
80 * the problem that is tackled below is the fact that FFS
81 * includes the terminating zero on disk while EXT2FS doesn't
82 * this implies that we need to introduce some padding.
83 * For instance, a filename "sbin" has normally a reclen 12
84 * in EXT2, but 16 in FFS.
85 * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...'
86 * If it wasn't for that, the complete ufs code for directories would
87 * have worked w/o changes (except for the difference in DIRBLKSIZ)
88 */
89 static void
90 ext2fs_dirconv2ffs( e2dir, ffsdir)
91 struct ext2fs_direct *e2dir;
92 struct dirent *ffsdir;
93 {
94 memset(ffsdir, 0, sizeof(struct dirent));
95 ffsdir->d_fileno = fs2h32(e2dir->e2d_ino);
96 ffsdir->d_namlen = e2dir->e2d_namlen;
97
98 ffsdir->d_type = DT_UNKNOWN; /* don't know more here */
99 #ifdef DIAGNOSTIC
100 /*
101 * XXX Right now this can't happen, but if one day
102 * MAXNAMLEN != E2FS_MAXNAMLEN we should handle this more gracefully !
103 */
104 #if 0
105 if (e2dir->e2d_namlen > MAXNAMLEN)
106 panic("ext2fs: e2dir->e2d_namlen");
107 #endif
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 = ext2fs_size(VTOI(ap->a_vp)) <= 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 = ap->a_dvp; /* vnode for directory being searched */
267 struct inode *dp = VTOI(vdp); /* 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;
290 int nameiop = cnp->cn_nameiop;
291 struct ufsmount *ump = dp->i_ump;
292 int dirblksiz = ump->um_dirblksiz;
293 ino_t foundino;
294
295 cnp->cn_flags &= ~PDIRUNLOCK;
296 flags = cnp->cn_flags;
297
298 bp = NULL;
299 slotoffset = -1;
300 *vpp = NULL;
301 lockparent = flags & LOCKPARENT;
302 wantparent = flags & (LOCKPARENT|WANTPARENT);
303 /*
304 * Check accessiblity of directory.
305 */
306 if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
307 return (error);
308
309 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
310 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
311 return (EROFS);
312
313 /*
314 * We now have a segment name to search for, and a directory to search.
315 *
316 * Before tediously performing a linear scan of the directory,
317 * check the name cache to see if the directory/name pair
318 * we are looking for is known already.
319 */
320 if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
321 return (error);
322
323 /*
324 * Suppress search for slots unless creating
325 * file and at end of pathname, in which case
326 * we watch for a place to put the new file in
327 * case it doesn't already exist.
328 */
329 slotstatus = FOUND;
330 slotfreespace = slotsize = slotneeded = 0;
331 if ((nameiop == CREATE || nameiop == RENAME) &&
332 (flags & ISLASTCN)) {
333 slotstatus = NONE;
334 slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
335 }
336
337 /*
338 * If there is cached information on a previous search of
339 * this directory, pick up where we last left off.
340 * We cache only lookups as these are the most common
341 * and have the greatest payoff. Caching CREATE has little
342 * benefit as it usually must search the entire directory
343 * to determine that the entry does not exist. Caching the
344 * location of the last DELETE or RENAME has not reduced
345 * profiling time and hence has been removed in the interest
346 * of simplicity.
347 */
348 bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
349 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
350 dp->i_diroff >= ext2fs_size(dp)) {
351 entryoffsetinblock = 0;
352 dp->i_offset = 0;
353 numdirpasses = 1;
354 } else {
355 dp->i_offset = dp->i_diroff;
356 if ((entryoffsetinblock = dp->i_offset & bmask) &&
357 (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
358 return (error);
359 numdirpasses = 2;
360 nchstats.ncs_2passes++;
361 }
362 prevoff = dp->i_offset;
363 endsearch = roundup(ext2fs_size(dp), dirblksiz);
364 enduseful = 0;
365
366 searchloop:
367 while (dp->i_offset < endsearch) {
368 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
369 preempt(1);
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, NULL,
377 &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 & (dirblksiz - 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
405 ufs_dirbad(dp, dp->i_offset, "mangled entry");
406 i = dirblksiz - (entryoffsetinblock & (dirblksiz - 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) -
436 slotoffset;
437 }
438 }
439 }
440 }
441
442 /*
443 * Check for a name match.
444 */
445 if (ep->e2d_ino) {
446 namlen = ep->e2d_namlen;
447 if (namlen == cnp->cn_namelen &&
448 !memcmp(cnp->cn_nameptr, ep->e2d_name,
449 (unsigned)namlen)) {
450 /*
451 * Save directory entry's inode number and
452 * reclen in ndp->ni_ufs area, and release
453 * directory buffer.
454 */
455 foundino = fs2h32(ep->e2d_ino);
456 dp->i_reclen = fs2h16(ep->e2d_reclen);
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 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
491 if (error)
492 return (error);
493 /*
494 * Return an indication of where the new directory
495 * entry should be put. If we didn't find a slot,
496 * then set dp->i_count to 0 indicating
497 * that the new slot belongs at the end of the
498 * directory. If we found a slot, then the new entry
499 * can be put in the range from dp->i_offset to
500 * dp->i_offset + dp->i_count.
501 */
502 if (slotstatus == NONE) {
503 dp->i_offset = roundup(ext2fs_size(dp), dirblksiz);
504 dp->i_count = 0;
505 enduseful = dp->i_offset;
506 } else if (nameiop == DELETE) {
507 dp->i_offset = slotoffset;
508 if ((dp->i_offset & (dirblksiz - 1)) == 0)
509 dp->i_count = 0;
510 else
511 dp->i_count = dp->i_offset - prevoff;
512 } else {
513 dp->i_offset = slotoffset;
514 dp->i_count = slotsize;
515 if (enduseful < slotoffset + slotsize)
516 enduseful = slotoffset + slotsize;
517 }
518 dp->i_endoff = roundup(enduseful, dirblksiz);
519 #if 0
520 dp->i_flag |= IN_CHANGE | IN_UPDATE;
521 #endif
522 /*
523 * We return with the directory locked, so that
524 * the parameters we set up above will still be
525 * valid if we actually decide to do a direnter().
526 * We return ni_vp == NULL to indicate that the entry
527 * does not currently exist; we leave a pointer to
528 * the (locked) directory inode in ndp->ni_dvp.
529 * The pathname buffer is saved so that the name
530 * can be obtained later.
531 *
532 * NB - if the directory is unlocked, then this
533 * information cannot be used.
534 */
535 cnp->cn_flags |= SAVENAME;
536 if (!lockparent) {
537 VOP_UNLOCK(vdp, 0);
538 cnp->cn_flags |= PDIRUNLOCK;
539 }
540 return (EJUSTRETURN);
541 }
542 /*
543 * Insert name into cache (as non-existent) if appropriate.
544 */
545 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
546 cache_enter(vdp, *vpp, cnp);
547 return (ENOENT);
548
549 found:
550 if (numdirpasses == 2)
551 nchstats.ncs_pass2++;
552 /*
553 * Check that directory length properly reflects presence
554 * of this entry.
555 */
556 if (dp->i_offset + EXT2FS_DIRSIZ(ep->e2d_namlen) > ext2fs_size(dp)) {
557 ufs_dirbad(dp, dp->i_offset, "i_size too small");
558 error = ext2fs_setsize(dp,
559 dp->i_offset + EXT2FS_DIRSIZ(ep->e2d_namlen));
560 if (error) {
561 brelse(bp);
562 return (error);
563 }
564 dp->i_flag |= IN_CHANGE | IN_UPDATE;
565 uvm_vnp_setsize(vdp, ext2fs_size(dp));
566 }
567 brelse(bp);
568
569 /*
570 * Found component in pathname.
571 * If the final component of path name, save information
572 * in the cache as to where the entry was found.
573 */
574 if ((flags & ISLASTCN) && nameiop == LOOKUP)
575 dp->i_diroff = dp->i_offset &~ (dirblksiz - 1);
576
577 /*
578 * If deleting, and at end of pathname, return
579 * parameters which can be used to remove file.
580 * If the wantparent flag isn't set, we return only
581 * the directory (in ndp->ni_dvp), otherwise we go
582 * on and lock the inode, being careful with ".".
583 */
584 if (nameiop == DELETE && (flags & ISLASTCN)) {
585 /*
586 * Write access to directory required to delete files.
587 */
588 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc)) != 0)
589 return (error);
590 /*
591 * Return pointer to current entry in dp->i_offset,
592 * and distance past previous entry (if there
593 * is a previous entry in this block) in dp->i_count.
594 * Save directory inode pointer in ndp->ni_dvp for dirremove().
595 */
596 if ((dp->i_offset & (dirblksiz - 1)) == 0)
597 dp->i_count = 0;
598 else
599 dp->i_count = dp->i_offset - prevoff;
600 if (dp->i_number == foundino) {
601 VREF(vdp);
602 *vpp = vdp;
603 return (0);
604 }
605 if (flags & ISDOTDOT)
606 VOP_UNLOCK(vdp, 0); /* race to get the inode */
607 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
608 if (flags & ISDOTDOT)
609 vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
610 if (error)
611 return (error);
612 /*
613 * If directory is "sticky", then user must own
614 * the directory, or the file in it, else she
615 * may not delete it (unless she's root). This
616 * implements append-only directories.
617 */
618 if ((dp->i_e2fs_mode & ISVTX) &&
619 cred->cr_uid != 0 &&
620 cred->cr_uid != dp->i_e2fs_uid &&
621 VTOI(tdp)->i_e2fs_uid != cred->cr_uid) {
622 vput(tdp);
623 return (EPERM);
624 }
625 *vpp = tdp;
626 if (!lockparent) {
627 VOP_UNLOCK(vdp, 0);
628 cnp->cn_flags |= PDIRUNLOCK;
629 }
630 return (0);
631 }
632
633 /*
634 * If rewriting (RENAME), return the inode and the
635 * information required to rewrite the present directory
636 * Must get inode of directory entry to verify it's a
637 * regular file, or empty directory.
638 */
639 if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
640 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
641 if (error)
642 return (error);
643 /*
644 * Careful about locking second inode.
645 * This can only occur if the target is ".".
646 */
647 if (dp->i_number == foundino)
648 return (EISDIR);
649 if (flags & ISDOTDOT)
650 VOP_UNLOCK(vdp, 0); /* race to get the inode */
651 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
652 if (flags & ISDOTDOT)
653 vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
654 if (error)
655 return (error);
656 *vpp = tdp;
657 cnp->cn_flags |= SAVENAME;
658 if (!lockparent) {
659 VOP_UNLOCK(vdp, 0);
660 cnp->cn_flags |= PDIRUNLOCK;
661 }
662 return (0);
663 }
664
665 /*
666 * Step through the translation in the name. We do not `vput' the
667 * directory because we may need it again if a symbolic link
668 * is relative to the current directory. Instead we save it
669 * unlocked as "pdp". We must get the target inode before unlocking
670 * the directory to insure that the inode will not be removed
671 * before we get it. We prevent deadlock by always fetching
672 * inodes from the root, moving down the directory tree. Thus
673 * when following backward pointers ".." we must unlock the
674 * parent directory before getting the requested directory.
675 * There is a potential race condition here if both the current
676 * and parent directories are removed before the VFS_VGET for the
677 * inode associated with ".." returns. We hope that this occurs
678 * infrequently since we cannot avoid this race condition without
679 * implementing a sophisticated deadlock detection algorithm.
680 * Note also that this simple deadlock detection scheme will not
681 * work if the file system has any hard links other than ".."
682 * that point backwards in the directory structure.
683 */
684 pdp = vdp;
685 if (flags & ISDOTDOT) {
686 VOP_UNLOCK(pdp, 0); /* race to get the inode */
687 cnp->cn_flags |= PDIRUNLOCK;
688 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
689 if (error) {
690 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0)
691 cnp->cn_flags &= ~PDIRUNLOCK;
692 return (error);
693 }
694 if (lockparent && (flags & ISLASTCN)) {
695 if ((error = vn_lock(pdp, LK_EXCLUSIVE))) {
696 vput(tdp);
697 return (error);
698 }
699 cnp->cn_flags &= ~PDIRUNLOCK;
700 }
701 *vpp = tdp;
702 } else if (dp->i_number == foundino) {
703 VREF(vdp); /* we want ourself, ie "." */
704 *vpp = vdp;
705 } else {
706 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
707 if (error)
708 return (error);
709 if (!lockparent || !(flags & ISLASTCN)) {
710 VOP_UNLOCK(pdp, 0);
711 cnp->cn_flags |= PDIRUNLOCK;
712 }
713 *vpp = tdp;
714 }
715
716 /*
717 * Insert name into cache if appropriate.
718 */
719 if (cnp->cn_flags & MAKEENTRY)
720 cache_enter(vdp, *vpp, cnp);
721 return (0);
722 }
723
724 /*
725 * Do consistency checking on a directory entry:
726 * record length must be multiple of 4
727 * entry must fit in rest of its dirblksize block
728 * record must be large enough to contain entry
729 * name is not longer than MAXNAMLEN
730 * name must be as long as advertised, and null terminated
731 */
732 /*
733 * changed so that it confirms to ext2fs_check_dir_entry
734 */
735 static int
736 ext2fs_dirbadentry(dp, de, entryoffsetinblock)
737 struct vnode *dp;
738 struct ext2fs_direct *de;
739 int entryoffsetinblock;
740 {
741 struct ufsmount *ump = VFSTOUFS(dp->v_mount);
742 int dirblksiz = ump->um_dirblksiz;
743
744 const char *error_msg = NULL;
745 int reclen = fs2h16(de->e2d_reclen);
746 int namlen = de->e2d_namlen;
747
748 if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
749 error_msg = "rec_len is smaller than minimal";
750 else if (reclen % 4 != 0)
751 error_msg = "rec_len % 4 != 0";
752 else if (reclen < EXT2FS_DIRSIZ(namlen))
753 error_msg = "reclen is too small for name_len";
754 else if (entryoffsetinblock + reclen > dirblksiz)
755 error_msg = "directory entry across blocks";
756 else if (fs2h32(de->e2d_ino) >
757 VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
758 error_msg = "inode out of bounds";
759
760 if (error_msg != NULL) {
761 printf( "bad directory entry: %s\n"
762 "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
763 error_msg, entryoffsetinblock,
764 (unsigned long) fs2h32(de->e2d_ino),
765 reclen, namlen);
766 panic("ext2fs_dirbadentry");
767 }
768 return error_msg == NULL ? 0 : 1;
769 }
770
771 /*
772 * Write a directory entry after a call to namei, using the parameters
773 * that it left in nameidata. The argument ip is the inode which the new
774 * directory entry will refer to. Dvp is a pointer to the directory to
775 * be written, which was left locked by namei. Remaining parameters
776 * (dp->i_offset, dp->i_count) indicate how the space for the new
777 * entry is to be obtained.
778 */
779 int
780 ext2fs_direnter(ip, dvp, cnp)
781 struct inode *ip;
782 struct vnode *dvp;
783 struct componentname *cnp;
784 {
785 struct ext2fs_direct *ep, *nep;
786 struct inode *dp;
787 struct buf *bp;
788 struct ext2fs_direct newdir;
789 struct iovec aiov;
790 struct uio auio;
791 u_int dsize;
792 int error, loc, newentrysize, spacefree;
793 char *dirbuf;
794 struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
795 int dirblksiz = ump->um_dirblksiz;
796
797 #ifdef DIAGNOSTIC
798 if ((cnp->cn_flags & SAVENAME) == 0)
799 panic("direnter: missing name");
800 #endif
801 dp = VTOI(dvp);
802 newdir.e2d_ino = h2fs32(ip->i_number);
803 newdir.e2d_namlen = cnp->cn_namelen;
804 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
805 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
806 newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
807 } else {
808 newdir.e2d_type = 0;
809 };
810 memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
811 newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
812 if (dp->i_count == 0) {
813 /*
814 * If dp->i_count is 0, then namei could find no
815 * space in the directory. Here, dp->i_offset will
816 * be on a directory block boundary and we will write the
817 * new entry into a fresh block.
818 */
819 if (dp->i_offset & (dirblksiz - 1))
820 panic("ext2fs_direnter: newblk");
821 auio.uio_offset = dp->i_offset;
822 newdir.e2d_reclen = h2fs16(dirblksiz);
823 auio.uio_resid = newentrysize;
824 aiov.iov_len = newentrysize;
825 aiov.iov_base = (caddr_t)&newdir;
826 auio.uio_iov = &aiov;
827 auio.uio_iovcnt = 1;
828 auio.uio_rw = UIO_WRITE;
829 auio.uio_segflg = UIO_SYSSPACE;
830 auio.uio_procp = NULL;
831 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
832 if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
833 /* XXX should grow with balloc() */
834 panic("ext2fs_direnter: frag size");
835 else if (!error) {
836 error = ext2fs_setsize(dp,
837 roundup(ext2fs_size(dp), dirblksiz));
838 if (error)
839 return (error);
840 dp->i_flag |= IN_CHANGE;
841 uvm_vnp_setsize(dvp, ext2fs_size(dp));
842 }
843 return (error);
844 }
845
846 /*
847 * If dp->i_count is non-zero, then namei found space
848 * for the new entry in the range dp->i_offset to
849 * dp->i_offset + dp->i_count in the directory.
850 * To use this space, we may have to compact the entries located
851 * there, by copying them together towards the beginning of the
852 * block, leaving the free space in one usable chunk at the end.
853 */
854
855 /*
856 * Get the block containing the space for the new directory entry.
857 */
858 if ((error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0)
859 return (error);
860 /*
861 * Find space for the new entry. In the simple case, the entry at
862 * offset base will have the space. If it does not, then namei
863 * arranged that compacting the region dp->i_offset to
864 * dp->i_offset + dp->i_count would yield the
865 * space.
866 */
867 ep = (struct ext2fs_direct *)dirbuf;
868 dsize = EXT2FS_DIRSIZ(ep->e2d_namlen);
869 spacefree = fs2h16(ep->e2d_reclen) - dsize;
870 for (loc = fs2h16(ep->e2d_reclen); loc < dp->i_count; ) {
871 nep = (struct ext2fs_direct *)(dirbuf + loc);
872 if (ep->e2d_ino) {
873 /* trim the existing slot */
874 ep->e2d_reclen = h2fs16(dsize);
875 ep = (struct ext2fs_direct *)((char *)ep + dsize);
876 } else {
877 /* overwrite; nothing there; header is ours */
878 spacefree += dsize;
879 }
880 dsize = EXT2FS_DIRSIZ(nep->e2d_namlen);
881 spacefree += fs2h16(nep->e2d_reclen) - dsize;
882 loc += fs2h16(nep->e2d_reclen);
883 memcpy((caddr_t)ep, (caddr_t)nep, dsize);
884 }
885 /*
886 * Update the pointer fields in the previous entry (if any),
887 * copy in the new entry, and write out the block.
888 */
889 if (ep->e2d_ino == 0) {
890 #ifdef DIAGNOSTIC
891 if (spacefree + dsize < newentrysize)
892 panic("ext2fs_direnter: compact1");
893 #endif
894 newdir.e2d_reclen = h2fs16(spacefree + dsize);
895 } else {
896 #ifdef DIAGNOSTIC
897 if (spacefree < newentrysize) {
898 printf("ext2fs_direnter: compact2 %u %u",
899 (u_int)spacefree, (u_int)newentrysize);
900 panic("ext2fs_direnter: compact2");
901 }
902 #endif
903 newdir.e2d_reclen = h2fs16(spacefree);
904 ep->e2d_reclen = h2fs16(dsize);
905 ep = (struct ext2fs_direct *)((char *)ep + dsize);
906 }
907 memcpy((caddr_t)ep, (caddr_t)&newdir, (u_int)newentrysize);
908 error = VOP_BWRITE(bp);
909 dp->i_flag |= IN_CHANGE | IN_UPDATE;
910 if (!error && dp->i_endoff && dp->i_endoff < ext2fs_size(dp))
911 error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC,
912 cnp->cn_cred, cnp->cn_proc);
913 return (error);
914 }
915
916 /*
917 * Remove a directory entry after a call to namei, using
918 * the parameters which it left in nameidata. The entry
919 * dp->i_offset contains the offset into the directory of the
920 * entry to be eliminated. The dp->i_count field contains the
921 * size of the previous record in the directory. If this
922 * is 0, the first entry is being deleted, so we need only
923 * zero the inode number to mark the entry as free. If the
924 * entry is not the first in the directory, we must reclaim
925 * the space of the now empty record by adding the record size
926 * to the size of the previous entry.
927 */
928 int
929 ext2fs_dirremove(dvp, cnp)
930 struct vnode *dvp;
931 struct componentname *cnp;
932 {
933 struct inode *dp;
934 struct ext2fs_direct *ep;
935 struct buf *bp;
936 int error;
937
938 dp = VTOI(dvp);
939 if (dp->i_count == 0) {
940 /*
941 * First entry in block: set d_ino to zero.
942 */
943 error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset,
944 (void *)&ep, &bp);
945 if (error != 0)
946 return (error);
947 ep->e2d_ino = 0;
948 error = VOP_BWRITE(bp);
949 dp->i_flag |= IN_CHANGE | IN_UPDATE;
950 return (error);
951 }
952 /*
953 * Collapse new free space into previous entry.
954 */
955 error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
956 (void *)&ep, &bp);
957 if (error != 0)
958 return (error);
959 ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + dp->i_reclen);
960 error = VOP_BWRITE(bp);
961 dp->i_flag |= IN_CHANGE | IN_UPDATE;
962 return (error);
963 }
964
965 /*
966 * Rewrite an existing directory entry to point at the inode
967 * supplied. The parameters describing the directory entry are
968 * set up by a call to namei.
969 */
970 int
971 ext2fs_dirrewrite(dp, ip, cnp)
972 struct inode *dp, *ip;
973 struct componentname *cnp;
974 {
975 struct buf *bp;
976 struct ext2fs_direct *ep;
977 struct vnode *vdp = ITOV(dp);
978 int error;
979
980 error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (void *)&ep, &bp);
981 if (error != 0)
982 return (error);
983 ep->e2d_ino = h2fs32(ip->i_number);
984 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
985 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
986 ep->e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
987 } else {
988 ep->e2d_type = 0;
989 }
990 error = VOP_BWRITE(bp);
991 dp->i_flag |= IN_CHANGE | IN_UPDATE;
992 return (error);
993 }
994
995 /*
996 * Check if a directory is empty or not.
997 * Inode supplied must be locked.
998 *
999 * Using a struct dirtemplate here is not precisely
1000 * what we want, but better than using a struct ext2fs_direct.
1001 *
1002 * NB: does not handle corrupted directories.
1003 */
1004 int
1005 ext2fs_dirempty(ip, parentino, cred)
1006 struct inode *ip;
1007 ino_t parentino;
1008 struct ucred *cred;
1009 {
1010 off_t off;
1011 struct ext2fs_dirtemplate dbuf;
1012 struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
1013 int error, namlen;
1014 size_t count;
1015
1016 #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
1017
1018 for (off = 0; off < ext2fs_size(ip); off += fs2h16(dp->e2d_reclen)) {
1019 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
1020 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, NULL);
1021 /*
1022 * Since we read MINDIRSIZ, residual must
1023 * be 0 unless we're at end of file.
1024 */
1025 if (error || count != 0)
1026 return (0);
1027 /* avoid infinite loops */
1028 if (dp->e2d_reclen == 0)
1029 return (0);
1030 /* skip empty entries */
1031 if (dp->e2d_ino == 0)
1032 continue;
1033 /* accept only "." and ".." */
1034 namlen = dp->e2d_namlen;
1035 if (namlen > 2)
1036 return (0);
1037 if (dp->e2d_name[0] != '.')
1038 return (0);
1039 /*
1040 * At this point namlen must be 1 or 2.
1041 * 1 implies ".", 2 implies ".." if second
1042 * char is also "."
1043 */
1044 if (namlen == 1)
1045 continue;
1046 if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
1047 continue;
1048 return (0);
1049 }
1050 return (1);
1051 }
1052
1053 /*
1054 * Check if source directory is in the path of the target directory.
1055 * Target is supplied locked, source is unlocked.
1056 * The target is always vput before returning.
1057 */
1058 int
1059 ext2fs_checkpath(source, target, cred)
1060 struct inode *source, *target;
1061 struct ucred *cred;
1062 {
1063 struct vnode *vp;
1064 int error, rootino, namlen;
1065 struct ext2fs_dirtemplate dirbuf;
1066 u_int32_t ino;
1067
1068 vp = ITOV(target);
1069 if (target->i_number == source->i_number) {
1070 error = EEXIST;
1071 goto out;
1072 }
1073 rootino = ROOTINO;
1074 error = 0;
1075 if (target->i_number == rootino)
1076 goto out;
1077
1078 for (;;) {
1079 if (vp->v_type != VDIR) {
1080 error = ENOTDIR;
1081 break;
1082 }
1083 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1084 sizeof (struct ext2fs_dirtemplate), (off_t)0,
1085 UIO_SYSSPACE, IO_NODELOCKED, cred, (size_t *)0,
1086 NULL);
1087 if (error != 0)
1088 break;
1089 namlen = dirbuf.dotdot_namlen;
1090 if (namlen != 2 ||
1091 dirbuf.dotdot_name[0] != '.' ||
1092 dirbuf.dotdot_name[1] != '.') {
1093 error = ENOTDIR;
1094 break;
1095 }
1096 ino = fs2h32(dirbuf.dotdot_ino);
1097 if (ino == source->i_number) {
1098 error = EINVAL;
1099 break;
1100 }
1101 if (ino == rootino)
1102 break;
1103 vput(vp);
1104 error = VFS_VGET(vp->v_mount, ino, &vp);
1105 if (error != 0) {
1106 vp = NULL;
1107 break;
1108 }
1109 }
1110
1111 out:
1112 if (error == ENOTDIR) {
1113 printf("checkpath: .. not a directory\n");
1114 panic("checkpath");
1115 }
1116 if (vp != NULL)
1117 vput(vp);
1118 return (error);
1119 }
1120