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