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