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