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