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