1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/8.1/sys/ufs/ufs/ufs_lookup.c 207598 2010-05-04 05:25:48Z kib $");
39
40 #include "opt_ffs_broken_fixme.h"
41 #include "opt_ufs.h"
42 #include "opt_quota.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/namei.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/stat.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/sysctl.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_extern.h>
58
59 #include <ufs/ufs/extattr.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/dir.h>
63 #ifdef UFS_DIRHASH
64 #include <ufs/ufs/dirhash.h>
65 #endif
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/ufs_extern.h>
68
69 #ifdef DIAGNOSTIC
70 static int dirchk = 1;
71 #else
72 static int dirchk = 0;
73 #endif
74
75 SYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
76
77 /* true if old FS format...*/
78 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
79
80 static int ufs_lookup_(struct vnode *, struct vnode **, struct componentname *,
81 ino_t *);
82
83 static int
84 ufs_delete_denied(struct vnode *vdp, struct vnode *tdp, struct ucred *cred,
85 struct thread *td)
86 {
87 int error;
88
89 #ifdef UFS_ACL
90 /*
91 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
92 *
93 * 3.16.2.1. ACE4_DELETE vs. ACE4_DELETE_CHILD
94 */
95
96 /*
97 * XXX: Is this check required?
98 */
99 error = VOP_ACCESS(vdp, VEXEC, cred, td);
100 if (error)
101 return (error);
102
103 error = VOP_ACCESSX(tdp, VDELETE, cred, td);
104 if (error == 0)
105 return (0);
106
107 error = VOP_ACCESSX(vdp, VDELETE_CHILD, cred, td);
108 if (error == 0)
109 return (0);
110
111 error = VOP_ACCESSX(vdp, VEXPLICIT_DENY | VDELETE_CHILD, cred, td);
112 if (error)
113 return (error);
114
115 #endif /* !UFS_ACL */
116
117 /*
118 * Standard Unix access control - delete access requires VWRITE.
119 */
120 error = VOP_ACCESS(vdp, VWRITE, cred, td);
121 if (error)
122 return (error);
123
124 /*
125 * If directory is "sticky", then user must own
126 * the directory, or the file in it, else she
127 * may not delete it (unless she's root). This
128 * implements append-only directories.
129 */
130 if ((VTOI(vdp)->i_mode & ISVTX) &&
131 VOP_ACCESS(vdp, VADMIN, cred, td) &&
132 VOP_ACCESS(tdp, VADMIN, cred, td))
133 return (EPERM);
134
135 return (0);
136 }
137
138 /*
139 * Convert a component of a pathname into a pointer to a locked inode.
140 * This is a very central and rather complicated routine.
141 * If the filesystem is not maintained in a strict tree hierarchy,
142 * this can result in a deadlock situation (see comments in code below).
143 *
144 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
145 * on whether the name is to be looked up, created, renamed, or deleted.
146 * When CREATE, RENAME, or DELETE is specified, information usable in
147 * creating, renaming, or deleting a directory entry may be calculated.
148 * If flag has LOCKPARENT or'ed into it and the target of the pathname
149 * exists, lookup returns both the target and its parent directory locked.
150 * When creating or renaming and LOCKPARENT is specified, the target may
151 * not be ".". When deleting and LOCKPARENT is specified, the target may
152 * be "."., but the caller must check to ensure it does an vrele and vput
153 * instead of two vputs.
154 *
155 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
156 * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP
157 * method.
158 *
159 * vfs_cache_lookup() performs the following for us:
160 * check that it is a directory
161 * check accessibility of directory
162 * check for modification attempts on read-only mounts
163 * if name found in cache
164 * if at end of path and deleting or creating
165 * drop it
166 * else
167 * return name.
168 * return VOP_CACHEDLOOKUP()
169 *
170 * Overall outline of ufs_lookup:
171 *
172 * search for name in directory, to found or notfound
173 * notfound:
174 * if creating, return locked directory, leaving info on available slots
175 * else return error
176 * found:
177 * if at end of path and deleting, return information to allow delete
178 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
179 * inode and return info to allow rewrite
180 * if not at end, add name to cache; if at end and neither creating
181 * nor deleting, add name to cache
182 */
183 int
184 ufs_lookup(ap)
185 struct vop_cachedlookup_args /* {
186 struct vnode *a_dvp;
187 struct vnode **a_vpp;
188 struct componentname *a_cnp;
189 } */ *ap;
190 {
191
192 return (ufs_lookup_(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
193 }
194
195 static int
196 ufs_lookup_(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
197 ino_t *dd_ino)
198 {
199 struct inode *dp; /* inode for directory being searched */
200 struct buf *bp; /* a buffer of directory entries */
201 struct direct *ep; /* the current directory entry */
202 int entryoffsetinblock; /* offset of ep in bp's buffer */
203 enum {NONE, COMPACT, FOUND} slotstatus;
204 doff_t slotoffset; /* offset of area with free space */
205 doff_t i_diroff; /* cached i_diroff value. */
206 doff_t i_offset; /* cached i_offset value. */
207 int slotsize; /* size of area at slotoffset */
208 int slotfreespace; /* amount of space free in slot */
209 int slotneeded; /* size of the entry we're seeking */
210 int numdirpasses; /* strategy for directory search */
211 doff_t endsearch; /* offset to end directory search */
212 doff_t prevoff; /* prev entry dp->i_offset */
213 struct vnode *pdp; /* saved dp during symlink work */
214 struct vnode *tdp; /* returned by VFS_VGET */
215 doff_t enduseful; /* pointer past last used dir slot */
216 u_long bmask; /* block offset mask */
217 int namlen, error;
218 struct ucred *cred = cnp->cn_cred;
219 int flags = cnp->cn_flags;
220 int nameiop = cnp->cn_nameiop;
221 ino_t ino, ino1;
222 int ltype;
223
224 if (vpp != NULL)
225 *vpp = NULL;
226
227 dp = VTOI(vdp);
228
229 /*
230 * Create a vm object if vmiodirenable is enabled.
231 * Alternatively we could call vnode_create_vobject
232 * in VFS_VGET but we could end up creating objects
233 * that are never used.
234 */
235 vnode_create_vobject(vdp, DIP(dp, i_size), cnp->cn_thread);
236
237 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
238
239 restart:
240 bp = NULL;
241 slotoffset = -1;
242
243 /*
244 * We now have a segment name to search for, and a directory to search.
245 *
246 * Suppress search for slots unless creating
247 * file and at end of pathname, in which case
248 * we watch for a place to put the new file in
249 * case it doesn't already exist.
250 */
251 ino = 0;
252 i_diroff = dp->i_diroff;
253 slotstatus = FOUND;
254 slotfreespace = slotsize = slotneeded = 0;
255 if ((nameiop == CREATE || nameiop == RENAME) &&
256 (flags & ISLASTCN)) {
257 slotstatus = NONE;
258 slotneeded = DIRECTSIZ(cnp->cn_namelen);
259 }
260
261 #ifdef UFS_DIRHASH
262 /*
263 * Use dirhash for fast operations on large directories. The logic
264 * to determine whether to hash the directory is contained within
265 * ufsdirhash_build(); a zero return means that it decided to hash
266 * this directory and it successfully built up the hash table.
267 */
268 if (ufsdirhash_build(dp) == 0) {
269 /* Look for a free slot if needed. */
270 enduseful = dp->i_size;
271 if (slotstatus != FOUND) {
272 slotoffset = ufsdirhash_findfree(dp, slotneeded,
273 &slotsize);
274 if (slotoffset >= 0) {
275 slotstatus = COMPACT;
276 enduseful = ufsdirhash_enduseful(dp);
277 if (enduseful < 0)
278 enduseful = dp->i_size;
279 }
280 }
281 /* Look up the component. */
282 numdirpasses = 1;
283 entryoffsetinblock = 0; /* silence compiler warning */
284 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
285 &i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
286 case 0:
287 ep = (struct direct *)((char *)bp->b_data +
288 (i_offset & bmask));
289 goto foundentry;
290 case ENOENT:
291 i_offset = roundup2(dp->i_size, DIRBLKSIZ);
292 goto notfound;
293 default:
294 /* Something failed; just do a linear search. */
295 break;
296 }
297 }
298 #endif /* UFS_DIRHASH */
299 /*
300 * If there is cached information on a previous search of
301 * this directory, pick up where we last left off.
302 * We cache only lookups as these are the most common
303 * and have the greatest payoff. Caching CREATE has little
304 * benefit as it usually must search the entire directory
305 * to determine that the entry does not exist. Caching the
306 * location of the last DELETE or RENAME has not reduced
307 * profiling time and hence has been removed in the interest
308 * of simplicity.
309 */
310 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff >= dp->i_size) {
311 entryoffsetinblock = 0;
312 i_offset = 0;
313 numdirpasses = 1;
314 } else {
315 i_offset = i_diroff;
316 if ((entryoffsetinblock = i_offset & bmask) &&
317 (error = UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp)))
318 return (error);
319 numdirpasses = 2;
320 nchstats.ncs_2passes++;
321 }
322 prevoff = i_offset;
323 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
324 enduseful = 0;
325
326 searchloop:
327 while (i_offset < endsearch) {
328 /*
329 * If necessary, get the next directory block.
330 */
331 if ((i_offset & bmask) == 0) {
332 if (bp != NULL)
333 brelse(bp);
334 error =
335 UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp);
336 if (error)
337 return (error);
338 entryoffsetinblock = 0;
339 }
340 /*
341 * If still looking for a slot, and at a DIRBLKSIZE
342 * boundary, have to start looking for free space again.
343 */
344 if (slotstatus == NONE &&
345 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
346 slotoffset = -1;
347 slotfreespace = 0;
348 }
349 /*
350 * Get pointer to next entry.
351 * Full validation checks are slow, so we only check
352 * enough to insure forward progress through the
353 * directory. Complete checks can be run by patching
354 * "dirchk" to be true.
355 */
356 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
357 if (ep->d_reclen == 0 || ep->d_reclen >
358 DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
359 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
360 int i;
361
362 ufs_dirbad(dp, i_offset, "mangled entry");
363 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
364 i_offset += i;
365 entryoffsetinblock += i;
366 continue;
367 }
368
369 /*
370 * If an appropriate sized slot has not yet been found,
371 * check to see if one is available. Also accumulate space
372 * in the current block so that we can determine if
373 * compaction is viable.
374 */
375 if (slotstatus != FOUND) {
376 int size = ep->d_reclen;
377
378 if (ep->d_ino != 0)
379 size -= DIRSIZ(OFSFMT(vdp), ep);
380 if (size > 0) {
381 if (size >= slotneeded) {
382 slotstatus = FOUND;
383 slotoffset = i_offset;
384 slotsize = ep->d_reclen;
385 } else if (slotstatus == NONE) {
386 slotfreespace += size;
387 if (slotoffset == -1)
388 slotoffset = i_offset;
389 if (slotfreespace >= slotneeded) {
390 slotstatus = COMPACT;
391 slotsize = i_offset +
392 ep->d_reclen - slotoffset;
393 }
394 }
395 }
396 }
397
398 /*
399 * Check for a name match.
400 */
401 if (ep->d_ino) {
402 # if (BYTE_ORDER == LITTLE_ENDIAN)
403 if (OFSFMT(vdp))
404 namlen = ep->d_type;
405 else
406 namlen = ep->d_namlen;
407 # else
408 namlen = ep->d_namlen;
409 # endif
410 if (namlen == cnp->cn_namelen &&
411 (cnp->cn_nameptr[0] == ep->d_name[0]) &&
412 !bcmp(cnp->cn_nameptr, ep->d_name,
413 (unsigned)namlen)) {
414 #ifdef UFS_DIRHASH
415 foundentry:
416 #endif
417 /*
418 * Save directory entry's inode number and
419 * reclen in ndp->ni_ufs area, and release
420 * directory buffer.
421 */
422 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
423 ep->d_type == DT_WHT) {
424 slotstatus = FOUND;
425 slotoffset = i_offset;
426 slotsize = ep->d_reclen;
427 enduseful = dp->i_size;
428 cnp->cn_flags |= ISWHITEOUT;
429 numdirpasses--;
430 goto notfound;
431 }
432 ino = ep->d_ino;
433 goto found;
434 }
435 }
436 prevoff = i_offset;
437 i_offset += ep->d_reclen;
438 entryoffsetinblock += ep->d_reclen;
439 if (ep->d_ino)
440 enduseful = i_offset;
441 }
442 notfound:
443 /*
444 * If we started in the middle of the directory and failed
445 * to find our target, we must check the beginning as well.
446 */
447 if (numdirpasses == 2) {
448 numdirpasses--;
449 i_offset = 0;
450 endsearch = i_diroff;
451 goto searchloop;
452 }
453 if (bp != NULL)
454 brelse(bp);
455 /*
456 * If creating, and at end of pathname and current
457 * directory has not been removed, then can consider
458 * allowing file to be created.
459 */
460 if ((nameiop == CREATE || nameiop == RENAME ||
461 (nameiop == DELETE &&
462 (cnp->cn_flags & DOWHITEOUT) &&
463 (cnp->cn_flags & ISWHITEOUT))) &&
464 (flags & ISLASTCN) && dp->i_effnlink != 0) {
465 /*
466 * Access for write is interpreted as allowing
467 * creation of files in the directory.
468 *
469 * XXX: Fix the comment above.
470 */
471 if (flags & WILLBEDIR)
472 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
473 else
474 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
475 if (error)
476 return (error);
477 /*
478 * Return an indication of where the new directory
479 * entry should be put. If we didn't find a slot,
480 * then set dp->i_count to 0 indicating
481 * that the new slot belongs at the end of the
482 * directory. If we found a slot, then the new entry
483 * can be put in the range from dp->i_offset to
484 * dp->i_offset + dp->i_count.
485 */
486 if (slotstatus == NONE) {
487 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
488 dp->i_count = 0;
489 enduseful = dp->i_offset;
490 } else if (nameiop == DELETE) {
491 dp->i_offset = slotoffset;
492 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
493 dp->i_count = 0;
494 else
495 dp->i_count = dp->i_offset - prevoff;
496 } else {
497 dp->i_offset = slotoffset;
498 dp->i_count = slotsize;
499 if (enduseful < slotoffset + slotsize)
500 enduseful = slotoffset + slotsize;
501 }
502 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
503 /*
504 * We return with the directory locked, so that
505 * the parameters we set up above will still be
506 * valid if we actually decide to do a direnter().
507 * We return ni_vp == NULL to indicate that the entry
508 * does not currently exist; we leave a pointer to
509 * the (locked) directory inode in ndp->ni_dvp.
510 * The pathname buffer is saved so that the name
511 * can be obtained later.
512 *
513 * NB - if the directory is unlocked, then this
514 * information cannot be used.
515 */
516 cnp->cn_flags |= SAVENAME;
517 return (EJUSTRETURN);
518 }
519 /*
520 * Insert name into cache (as non-existent) if appropriate.
521 */
522 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
523 cache_enter(vdp, NULL, cnp);
524 return (ENOENT);
525
526 found:
527 if (numdirpasses == 2)
528 nchstats.ncs_pass2++;
529 /*
530 * Check that directory length properly reflects presence
531 * of this entry.
532 */
533 if (i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
534 ufs_dirbad(dp, i_offset, "i_size too small");
535 dp->i_size = i_offset + DIRSIZ(OFSFMT(vdp), ep);
536 DIP_SET(dp, i_size, dp->i_size);
537 dp->i_flag |= IN_CHANGE | IN_UPDATE;
538 }
539 brelse(bp);
540
541 /*
542 * Found component in pathname.
543 * If the final component of path name, save information
544 * in the cache as to where the entry was found.
545 */
546 if ((flags & ISLASTCN) && nameiop == LOOKUP)
547 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1);
548
549 if (dd_ino != NULL) {
550 *dd_ino = ino;
551 return (0);
552 }
553
554 /*
555 * If deleting, and at end of pathname, return
556 * parameters which can be used to remove file.
557 */
558 if (nameiop == DELETE && (flags & ISLASTCN)) {
559 if (flags & LOCKPARENT)
560 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
561 if ((error = VFS_VGET(vdp->v_mount, ino,
562 LK_EXCLUSIVE, &tdp)) != 0)
563 return (error);
564
565 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread);
566 if (error) {
567 vput(tdp);
568 return (error);
569 }
570
571
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 * Technically we shouldn't be setting these in the
579 * WANTPARENT case (first lookup in rename()), but any
580 * lookups that will result in directory changes will
581 * overwrite these.
582 */
583 dp->i_offset = i_offset;
584 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
585 dp->i_count = 0;
586 else
587 dp->i_count = dp->i_offset - prevoff;
588 if (dp->i_number == ino) {
589 VREF(vdp);
590 *vpp = vdp;
591 vput(tdp);
592 return (0);
593 }
594
595 *vpp = tdp;
596 return (0);
597 }
598
599 /*
600 * If rewriting (RENAME), return the inode and the
601 * information required to rewrite the present directory
602 * Must get inode of directory entry to verify it's a
603 * regular file, or empty directory.
604 */
605 if (nameiop == RENAME && (flags & ISLASTCN)) {
606 if (flags & WILLBEDIR)
607 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
608 else
609 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
610 if (error)
611 return (error);
612 /*
613 * Careful about locking second inode.
614 * This can only occur if the target is ".".
615 */
616 dp->i_offset = i_offset;
617 if (dp->i_number == ino)
618 return (EISDIR);
619 if ((error = VFS_VGET(vdp->v_mount, ino,
620 LK_EXCLUSIVE, &tdp)) != 0)
621 return (error);
622
623 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread);
624 if (error) {
625 vput(tdp);
626 return (error);
627 }
628
629 #ifdef SunOS_doesnt_do_that
630 /*
631 * The only purpose of this check is to return the correct
632 * error. Assume that we want to rename directory "a"
633 * to a file "b", and that we have no ACL_WRITE_DATA on
634 * a containing directory, but we _do_ have ACL_APPEND_DATA.
635 * In that case, the VOP_ACCESS check above will return 0,
636 * and the operation will fail with ENOTDIR instead
637 * of EACCESS.
638 */
639 if (tdp->v_type == VDIR)
640 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
641 else
642 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
643 if (error) {
644 vput(tdp);
645 return (error);
646 }
647 #endif
648
649 *vpp = tdp;
650 cnp->cn_flags |= SAVENAME;
651 return (0);
652 }
653
654 /*
655 * Step through the translation in the name. We do not `vput' the
656 * directory because we may need it again if a symbolic link
657 * is relative to the current directory. Instead we save it
658 * unlocked as "pdp". We must get the target inode before unlocking
659 * the directory to insure that the inode will not be removed
660 * before we get it. We prevent deadlock by always fetching
661 * inodes from the root, moving down the directory tree. Thus
662 * when following backward pointers ".." we must unlock the
663 * parent directory before getting the requested directory.
664 * There is a potential race condition here if both the current
665 * and parent directories are removed before the VFS_VGET for the
666 * inode associated with ".." returns. We hope that this occurs
667 * infrequently since we cannot avoid this race condition without
668 * implementing a sophisticated deadlock detection algorithm.
669 * Note also that this simple deadlock detection scheme will not
670 * work if the filesystem has any hard links other than ".."
671 * that point backwards in the directory structure.
672 */
673 pdp = vdp;
674 if (flags & ISDOTDOT) {
675 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
676 if (error)
677 return (error);
678
679 /*
680 * Recheck that ".." entry in the vdp directory points
681 * to the inode we looked up before vdp lock was
682 * dropped.
683 */
684 error = ufs_lookup_(pdp, NULL, cnp, &ino1);
685 if (error) {
686 vput(tdp);
687 return (error);
688 }
689 if (ino1 != ino) {
690 vput(tdp);
691 goto restart;
692 }
693
694 *vpp = tdp;
695 } else if (dp->i_number == ino) {
696 VREF(vdp); /* we want ourself, ie "." */
697 /*
698 * When we lookup "." we still can be asked to lock it
699 * differently.
700 */
701 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
702 if (ltype != VOP_ISLOCKED(vdp)) {
703 if (ltype == LK_EXCLUSIVE)
704 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
705 else /* if (ltype == LK_SHARED) */
706 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
707 /*
708 * Relock for the "." case may left us with
709 * reclaimed vnode.
710 */
711 if (vdp->v_iflag & VI_DOOMED) {
712 vrele(vdp);
713 return (ENOENT);
714 }
715 }
716 *vpp = vdp;
717 } else {
718 error = VFS_VGET(pdp->v_mount, ino, cnp->cn_lkflags, &tdp);
719 if (error)
720 return (error);
721 *vpp = tdp;
722 }
723
724 /*
725 * Insert name into cache if appropriate.
726 */
727 if (cnp->cn_flags & MAKEENTRY)
728 cache_enter(vdp, *vpp, cnp);
729 return (0);
730 }
731
732 void
733 ufs_dirbad(ip, offset, how)
734 struct inode *ip;
735 doff_t offset;
736 char *how;
737 {
738 struct mount *mp;
739
740 mp = ITOV(ip)->v_mount;
741 if ((mp->mnt_flag & MNT_RDONLY) == 0)
742 panic("ufs_dirbad: %s: bad dir ino %lu at offset %ld: %s",
743 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
744 else
745 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
746 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
747 }
748
749 /*
750 * Do consistency checking on a directory entry:
751 * record length must be multiple of 4
752 * entry must fit in rest of its DIRBLKSIZ block
753 * record must be large enough to contain entry
754 * name is not longer than MAXNAMLEN
755 * name must be as long as advertised, and null terminated
756 */
757 int
758 ufs_dirbadentry(dp, ep, entryoffsetinblock)
759 struct vnode *dp;
760 struct direct *ep;
761 int entryoffsetinblock;
762 {
763 int i, namlen;
764
765 # if (BYTE_ORDER == LITTLE_ENDIAN)
766 if (OFSFMT(dp))
767 namlen = ep->d_type;
768 else
769 namlen = ep->d_namlen;
770 # else
771 namlen = ep->d_namlen;
772 # endif
773 if ((ep->d_reclen & 0x3) != 0 ||
774 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
775 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > MAXNAMLEN) {
776 /*return (1); */
777 printf("First bad\n");
778 goto bad;
779 }
780 if (ep->d_ino == 0)
781 return (0);
782 for (i = 0; i < namlen; i++)
783 if (ep->d_name[i] == '\0') {
784 /*return (1); */
785 printf("Second bad\n");
786 goto bad;
787 }
788 if (ep->d_name[i])
789 goto bad;
790 return (0);
791 bad:
792 return (1);
793 }
794
795 /*
796 * Construct a new directory entry after a call to namei, using the
797 * parameters that it left in the componentname argument cnp. The
798 * argument ip is the inode to which the new directory entry will refer.
799 */
800 void
801 ufs_makedirentry(ip, cnp, newdirp)
802 struct inode *ip;
803 struct componentname *cnp;
804 struct direct *newdirp;
805 {
806
807 #ifdef INVARIANTS
808 if ((cnp->cn_flags & SAVENAME) == 0)
809 panic("ufs_makedirentry: missing name");
810 #endif
811 newdirp->d_ino = ip->i_number;
812 newdirp->d_namlen = cnp->cn_namelen;
813 bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1);
814 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
815 newdirp->d_type = IFTODT(ip->i_mode);
816 else {
817 newdirp->d_type = 0;
818 # if (BYTE_ORDER == LITTLE_ENDIAN)
819 { u_char tmp = newdirp->d_namlen;
820 newdirp->d_namlen = newdirp->d_type;
821 newdirp->d_type = tmp; }
822 # endif
823 }
824 }
825
826 /*
827 * Write a directory entry after a call to namei, using the parameters
828 * that it left in nameidata. The argument dirp is the new directory
829 * entry contents. Dvp is a pointer to the directory to be written,
830 * which was left locked by namei. Remaining parameters (dp->i_offset,
831 * dp->i_count) indicate how the space for the new entry is to be obtained.
832 * Non-null bp indicates that a directory is being created (for the
833 * soft dependency code).
834 */
835 int
836 ufs_direnter(dvp, tvp, dirp, cnp, newdirbp)
837 struct vnode *dvp;
838 struct vnode *tvp;
839 struct direct *dirp;
840 struct componentname *cnp;
841 struct buf *newdirbp;
842 {
843 struct ucred *cr;
844 struct thread *td;
845 int newentrysize;
846 struct inode *dp;
847 struct buf *bp;
848 u_int dsize;
849 struct direct *ep, *nep;
850 int error, ret, blkoff, loc, spacefree, flags, namlen;
851 char *dirbuf;
852
853 td = curthread; /* XXX */
854 cr = td->td_ucred;
855
856 dp = VTOI(dvp);
857 newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
858
859 if (dp->i_count == 0) {
860 /*
861 * If dp->i_count is 0, then namei could find no
862 * space in the directory. Here, dp->i_offset will
863 * be on a directory block boundary and we will write the
864 * new entry into a fresh block.
865 */
866 if (dp->i_offset & (DIRBLKSIZ - 1))
867 panic("ufs_direnter: newblk");
868 flags = BA_CLRBUF;
869 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
870 flags |= IO_SYNC;
871 #ifdef QUOTA
872 if ((error = getinoquota(dp)) != 0) {
873 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
874 bdwrite(newdirbp);
875 return (error);
876 }
877 #endif
878 if ((error = UFS_BALLOC(dvp, (off_t)dp->i_offset, DIRBLKSIZ,
879 cr, flags, &bp)) != 0) {
880 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
881 bdwrite(newdirbp);
882 return (error);
883 }
884 dp->i_size = dp->i_offset + DIRBLKSIZ;
885 DIP_SET(dp, i_size, dp->i_size);
886 dp->i_flag |= IN_CHANGE | IN_UPDATE;
887 vnode_pager_setsize(dvp, (u_long)dp->i_size);
888 dirp->d_reclen = DIRBLKSIZ;
889 blkoff = dp->i_offset &
890 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
891 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
892 #ifdef UFS_DIRHASH
893 if (dp->i_dirhash != NULL) {
894 ufsdirhash_newblk(dp, dp->i_offset);
895 ufsdirhash_add(dp, dirp, dp->i_offset);
896 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
897 dp->i_offset);
898 }
899 #endif
900 if (DOINGSOFTDEP(dvp)) {
901 /*
902 * Ensure that the entire newly allocated block is a
903 * valid directory so that future growth within the
904 * block does not have to ensure that the block is
905 * written before the inode.
906 */
907 blkoff += DIRBLKSIZ;
908 while (blkoff < bp->b_bcount) {
909 ((struct direct *)
910 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
911 blkoff += DIRBLKSIZ;
912 }
913 if (softdep_setup_directory_add(bp, dp, dp->i_offset,
914 dirp->d_ino, newdirbp, 1) == 0) {
915 bdwrite(bp);
916 return (UFS_UPDATE(dvp, 0));
917 }
918 /* We have just allocated a directory block in an
919 * indirect block. Rather than tracking when it gets
920 * claimed by the inode, we simply do a VOP_FSYNC
921 * now to ensure that it is there (in case the user
922 * does a future fsync). Note that we have to unlock
923 * the inode for the entry that we just entered, as
924 * the VOP_FSYNC may need to lock other inodes which
925 * can lead to deadlock if we also hold a lock on
926 * the newly entered node.
927 */
928 if ((error = bwrite(bp)))
929 return (error);
930 if (tvp != NULL)
931 VOP_UNLOCK(tvp, 0);
932 error = VOP_FSYNC(dvp, MNT_WAIT, td);
933 if (tvp != NULL)
934 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
935 return (error);
936 }
937 if (DOINGASYNC(dvp)) {
938 bdwrite(bp);
939 return (UFS_UPDATE(dvp, 0));
940 }
941 error = bwrite(bp);
942 ret = UFS_UPDATE(dvp, 1);
943 if (error == 0)
944 return (ret);
945 return (error);
946 }
947
948 /*
949 * If dp->i_count is non-zero, then namei found space for the new
950 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
951 * in the directory. To use this space, we may have to compact
952 * the entries located there, by copying them together towards the
953 * beginning of the block, leaving the free space in one usable
954 * chunk at the end.
955 */
956
957 /*
958 * Increase size of directory if entry eats into new space.
959 * This should never push the size past a new multiple of
960 * DIRBLKSIZE.
961 *
962 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
963 */
964 if (dp->i_offset + dp->i_count > dp->i_size) {
965 dp->i_size = dp->i_offset + dp->i_count;
966 DIP_SET(dp, i_size, dp->i_size);
967 }
968 /*
969 * Get the block containing the space for the new directory entry.
970 */
971 error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
972 if (error) {
973 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
974 bdwrite(newdirbp);
975 return (error);
976 }
977 /*
978 * Find space for the new entry. In the simple case, the entry at
979 * offset base will have the space. If it does not, then namei
980 * arranged that compacting the region dp->i_offset to
981 * dp->i_offset + dp->i_count would yield the space.
982 */
983 ep = (struct direct *)dirbuf;
984 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
985 spacefree = ep->d_reclen - dsize;
986 for (loc = ep->d_reclen; loc < dp->i_count; ) {
987 nep = (struct direct *)(dirbuf + loc);
988
989 /* Trim the existing slot (NB: dsize may be zero). */
990 ep->d_reclen = dsize;
991 ep = (struct direct *)((char *)ep + dsize);
992
993 /* Read nep->d_reclen now as the bcopy() may clobber it. */
994 loc += nep->d_reclen;
995 if (nep->d_ino == 0) {
996 /*
997 * A mid-block unused entry. Such entries are
998 * never created by the kernel, but fsck_ffs
999 * can create them (and it doesn't fix them).
1000 *
1001 * Add up the free space, and initialise the
1002 * relocated entry since we don't bcopy it.
1003 */
1004 spacefree += nep->d_reclen;
1005 ep->d_ino = 0;
1006 dsize = 0;
1007 continue;
1008 }
1009 dsize = DIRSIZ(OFSFMT(dvp), nep);
1010 spacefree += nep->d_reclen - dsize;
1011 #ifdef UFS_DIRHASH
1012 if (dp->i_dirhash != NULL)
1013 ufsdirhash_move(dp, nep,
1014 dp->i_offset + ((char *)nep - dirbuf),
1015 dp->i_offset + ((char *)ep - dirbuf));
1016 #endif
1017 if (DOINGSOFTDEP(dvp))
1018 softdep_change_directoryentry_offset(dp, dirbuf,
1019 (caddr_t)nep, (caddr_t)ep, dsize);
1020 else
1021 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1022 }
1023 /*
1024 * Here, `ep' points to a directory entry containing `dsize' in-use
1025 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
1026 * then the entry is completely unused (dsize == 0). The value
1027 * of ep->d_reclen is always indeterminate.
1028 *
1029 * Update the pointer fields in the previous entry (if any),
1030 * copy in the new entry, and write out the block.
1031 */
1032 # if (BYTE_ORDER == LITTLE_ENDIAN)
1033 if (OFSFMT(dvp))
1034 namlen = ep->d_type;
1035 else
1036 namlen = ep->d_namlen;
1037 # else
1038 namlen = ep->d_namlen;
1039 # endif
1040 if (ep->d_ino == 0 ||
1041 (ep->d_ino == WINO && namlen == dirp->d_namlen &&
1042 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
1043 if (spacefree + dsize < newentrysize)
1044 panic("ufs_direnter: compact1");
1045 dirp->d_reclen = spacefree + dsize;
1046 } else {
1047 if (spacefree < newentrysize)
1048 panic("ufs_direnter: compact2");
1049 dirp->d_reclen = spacefree;
1050 ep->d_reclen = dsize;
1051 ep = (struct direct *)((char *)ep + dsize);
1052 }
1053 #ifdef UFS_DIRHASH
1054 if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
1055 dirp->d_reclen == spacefree))
1056 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf));
1057 #endif
1058 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
1059 #ifdef UFS_DIRHASH
1060 if (dp->i_dirhash != NULL)
1061 ufsdirhash_checkblock(dp, dirbuf -
1062 (dp->i_offset & (DIRBLKSIZ - 1)),
1063 dp->i_offset & ~(DIRBLKSIZ - 1));
1064 #endif
1065
1066 if (DOINGSOFTDEP(dvp)) {
1067 (void) softdep_setup_directory_add(bp, dp,
1068 dp->i_offset + (caddr_t)ep - dirbuf,
1069 dirp->d_ino, newdirbp, 0);
1070 bdwrite(bp);
1071 } else {
1072 if (DOINGASYNC(dvp)) {
1073 bdwrite(bp);
1074 error = 0;
1075 } else {
1076 error = bwrite(bp);
1077 }
1078 }
1079 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1080 /*
1081 * If all went well, and the directory can be shortened, proceed
1082 * with the truncation. Note that we have to unlock the inode for
1083 * the entry that we just entered, as the truncation may need to
1084 * lock other inodes which can lead to deadlock if we also hold a
1085 * lock on the newly entered node.
1086 */
1087 if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_size) {
1088 if (tvp != NULL)
1089 VOP_UNLOCK(tvp, 0);
1090 #ifdef UFS_DIRHASH
1091 if (dp->i_dirhash != NULL)
1092 ufsdirhash_dirtrunc(dp, dp->i_endoff);
1093 #endif
1094 (void) UFS_TRUNCATE(dvp, (off_t)dp->i_endoff,
1095 IO_NORMAL | IO_SYNC, cr, td);
1096 if (tvp != NULL)
1097 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
1098 }
1099 return (error);
1100 }
1101
1102 /*
1103 * Remove a directory entry after a call to namei, using
1104 * the parameters which it left in nameidata. The entry
1105 * dp->i_offset contains the offset into the directory of the
1106 * entry to be eliminated. The dp->i_count field contains the
1107 * size of the previous record in the directory. If this
1108 * is 0, the first entry is being deleted, so we need only
1109 * zero the inode number to mark the entry as free. If the
1110 * entry is not the first in the directory, we must reclaim
1111 * the space of the now empty record by adding the record size
1112 * to the size of the previous entry.
1113 */
1114 int
1115 ufs_dirremove(dvp, ip, flags, isrmdir)
1116 struct vnode *dvp;
1117 struct inode *ip;
1118 int flags;
1119 int isrmdir;
1120 {
1121 struct inode *dp;
1122 struct direct *ep, *rep;
1123 struct buf *bp;
1124 int error;
1125
1126 dp = VTOI(dvp);
1127
1128 if (flags & DOWHITEOUT) {
1129 /*
1130 * Whiteout entry: set d_ino to WINO.
1131 */
1132 if ((error =
1133 UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
1134 return (error);
1135 ep->d_ino = WINO;
1136 ep->d_type = DT_WHT;
1137 goto out;
1138 }
1139
1140 if ((error = UFS_BLKATOFF(dvp,
1141 (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0)
1142 return (error);
1143
1144 /* Set 'rep' to the entry being removed. */
1145 if (dp->i_count == 0)
1146 rep = ep;
1147 else
1148 rep = (struct direct *)((char *)ep + ep->d_reclen);
1149 #ifdef UFS_DIRHASH
1150 /*
1151 * Remove the dirhash entry. This is complicated by the fact
1152 * that `ep' is the previous entry when dp->i_count != 0.
1153 */
1154 if (dp->i_dirhash != NULL)
1155 ufsdirhash_remove(dp, rep, dp->i_offset);
1156 #endif
1157 if (dp->i_count == 0) {
1158 /*
1159 * First entry in block: set d_ino to zero.
1160 */
1161 ep->d_ino = 0;
1162 } else {
1163 /*
1164 * Collapse new free space into previous entry.
1165 */
1166 ep->d_reclen += rep->d_reclen;
1167 }
1168 #ifdef UFS_DIRHASH
1169 if (dp->i_dirhash != NULL)
1170 ufsdirhash_checkblock(dp, (char *)ep -
1171 ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
1172 dp->i_offset & ~(DIRBLKSIZ - 1));
1173 #endif
1174 out:
1175 if (DOINGSOFTDEP(dvp)) {
1176 if (ip) {
1177 ip->i_effnlink--;
1178 softdep_change_linkcnt(ip);
1179 softdep_setup_remove(bp, dp, ip, isrmdir);
1180 }
1181 if (softdep_slowdown(dvp)) {
1182 error = bwrite(bp);
1183 } else {
1184 bdwrite(bp);
1185 error = 0;
1186 }
1187 } else {
1188 if (ip) {
1189 ip->i_effnlink--;
1190 ip->i_nlink--;
1191 DIP_SET(ip, i_nlink, ip->i_nlink);
1192 ip->i_flag |= IN_CHANGE;
1193 }
1194 if (flags & DOWHITEOUT)
1195 error = bwrite(bp);
1196 else if (DOINGASYNC(dvp) && dp->i_count != 0) {
1197 bdwrite(bp);
1198 error = 0;
1199 } else
1200 error = bwrite(bp);
1201 }
1202 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1203 /*
1204 * If the last named reference to a snapshot goes away,
1205 * drop its snapshot reference so that it will be reclaimed
1206 * when last open reference goes away.
1207 */
1208 #if defined(FFS) || defined(IFS)
1209 if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 && ip->i_effnlink == 0)
1210 ffs_snapgone(ip);
1211 #endif
1212 return (error);
1213 }
1214
1215 /*
1216 * Rewrite an existing directory entry to point at the inode
1217 * supplied. The parameters describing the directory entry are
1218 * set up by a call to namei.
1219 */
1220 int
1221 ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir)
1222 struct inode *dp, *oip;
1223 ino_t newinum;
1224 int newtype;
1225 int isrmdir;
1226 {
1227 struct buf *bp;
1228 struct direct *ep;
1229 struct vnode *vdp = ITOV(dp);
1230 int error;
1231
1232 error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
1233 if (error)
1234 return (error);
1235 ep->d_ino = newinum;
1236 if (!OFSFMT(vdp))
1237 ep->d_type = newtype;
1238 oip->i_effnlink--;
1239 if (DOINGSOFTDEP(vdp)) {
1240 softdep_change_linkcnt(oip);
1241 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1242 bdwrite(bp);
1243 } else {
1244 oip->i_nlink--;
1245 DIP_SET(oip, i_nlink, oip->i_nlink);
1246 oip->i_flag |= IN_CHANGE;
1247 if (DOINGASYNC(vdp)) {
1248 bdwrite(bp);
1249 error = 0;
1250 } else {
1251 error = bwrite(bp);
1252 }
1253 }
1254 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1255 /*
1256 * If the last named reference to a snapshot goes away,
1257 * drop its snapshot reference so that it will be reclaimed
1258 * when last open reference goes away.
1259 */
1260 #if defined(FFS) || defined(IFS)
1261 if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_effnlink == 0)
1262 ffs_snapgone(oip);
1263 #endif
1264 return (error);
1265 }
1266
1267 /*
1268 * Check if a directory is empty or not.
1269 * Inode supplied must be locked.
1270 *
1271 * Using a struct dirtemplate here is not precisely
1272 * what we want, but better than using a struct direct.
1273 *
1274 * NB: does not handle corrupted directories.
1275 */
1276 int
1277 ufs_dirempty(ip, parentino, cred)
1278 struct inode *ip;
1279 ino_t parentino;
1280 struct ucred *cred;
1281 {
1282 doff_t off;
1283 struct dirtemplate dbuf;
1284 struct direct *dp = (struct direct *)&dbuf;
1285 int error, count, namlen;
1286 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1287
1288 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1289 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1290 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1291 NOCRED, &count, (struct thread *)0);
1292 /*
1293 * Since we read MINDIRSIZ, residual must
1294 * be 0 unless we're at end of file.
1295 */
1296 if (error || count != 0)
1297 return (0);
1298 /* avoid infinite loops */
1299 if (dp->d_reclen == 0)
1300 return (0);
1301 /* skip empty entries */
1302 if (dp->d_ino == 0 || dp->d_ino == WINO)
1303 continue;
1304 /* accept only "." and ".." */
1305 # if (BYTE_ORDER == LITTLE_ENDIAN)
1306 if (OFSFMT(ITOV(ip)))
1307 namlen = dp->d_type;
1308 else
1309 namlen = dp->d_namlen;
1310 # else
1311 namlen = dp->d_namlen;
1312 # endif
1313 if (namlen > 2)
1314 return (0);
1315 if (dp->d_name[0] != '.')
1316 return (0);
1317 /*
1318 * At this point namlen must be 1 or 2.
1319 * 1 implies ".", 2 implies ".." if second
1320 * char is also "."
1321 */
1322 if (namlen == 1 && dp->d_ino == ip->i_number)
1323 continue;
1324 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1325 continue;
1326 return (0);
1327 }
1328 return (1);
1329 }
1330
1331 static int
1332 ufs_dir_dd_ino(struct vnode *vp, struct ucred *cred, ino_t *dd_ino)
1333 {
1334 struct dirtemplate dirbuf;
1335 int error, namlen;
1336
1337 if (vp->v_type != VDIR)
1338 return (ENOTDIR);
1339 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1340 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1341 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, NULL);
1342 if (error != 0)
1343 return (error);
1344 #if (BYTE_ORDER == LITTLE_ENDIAN)
1345 if (OFSFMT(vp))
1346 namlen = dirbuf.dotdot_type;
1347 else
1348 namlen = dirbuf.dotdot_namlen;
1349 #else
1350 namlen = dirbuf.dotdot_namlen;
1351 #endif
1352 if (namlen != 2 || dirbuf.dotdot_name[0] != '.' ||
1353 dirbuf.dotdot_name[1] != '.')
1354 return (ENOTDIR);
1355 *dd_ino = dirbuf.dotdot_ino;
1356 return (0);
1357 }
1358
1359 /*
1360 * Check if source directory is in the path of the target directory.
1361 * Target is supplied locked, source is unlocked.
1362 * The target is always vput before returning.
1363 */
1364 int
1365 ufs_checkpath(ino_t source_ino, struct inode *target, struct ucred *cred)
1366 {
1367 struct vnode *vp, *vp1;
1368 int error;
1369 ino_t dd_ino;
1370
1371 vp = ITOV(target);
1372 if (target->i_number == source_ino) {
1373 error = EEXIST;
1374 goto out;
1375 }
1376 error = 0;
1377 if (target->i_number == ROOTINO)
1378 goto out;
1379
1380 for (;;) {
1381 error = ufs_dir_dd_ino(vp, cred, &dd_ino);
1382 if (error != 0)
1383 break;
1384 if (dd_ino == source_ino) {
1385 error = EINVAL;
1386 break;
1387 }
1388 if (dd_ino == ROOTINO)
1389 break;
1390 error = vn_vget_ino(vp, dd_ino, LK_EXCLUSIVE, &vp1);
1391 if (error != 0)
1392 break;
1393 /* Recheck that ".." still points to vp1 after relock of vp */
1394 error = ufs_dir_dd_ino(vp, cred, &dd_ino);
1395 if (error != 0) {
1396 vput(vp1);
1397 break;
1398 }
1399 /* Redo the check of ".." if directory was reparented */
1400 if (dd_ino != VTOI(vp1)->i_number) {
1401 vput(vp1);
1402 continue;
1403 }
1404 vput(vp);
1405 vp = vp1;
1406 }
1407
1408 out:
1409 if (error == ENOTDIR)
1410 printf("checkpath: .. not a directory\n");
1411 if (vp != NULL)
1412 vput(vp);
1413 return (error);
1414 }
Cache object: 6be7b817ea54567c50444e62f5295877
|