| File: | ufs/ufs/ufs_lookup.c |
| Warning: | line 618, column 2 Value stored to 'dp' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* $OpenBSD: ufs_lookup.c,v 1.60 2024/01/09 03:15:59 guenther Exp $ */ |
| 2 | /* $NetBSD: ufs_lookup.c,v 1.7 1996/02/09 22:36:06 christos Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1989, 1993 |
| 6 | * The Regents of the University of California. All rights reserved. |
| 7 | * (c) UNIX System Laboratories, Inc. |
| 8 | * All or some portions of this file are derived from material licensed |
| 9 | * to the University of California by American Telephone and Telegraph |
| 10 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
| 11 | * the permission of UNIX System Laboratories, Inc. |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
| 16 | * 1. Redistributions of source code must retain the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer. |
| 18 | * 2. Redistributions in binary form must reproduce the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer in the |
| 20 | * documentation and/or other materials provided with the distribution. |
| 21 | * 3. Neither the name of the University nor the names of its contributors |
| 22 | * may be used to endorse or promote products derived from this software |
| 23 | * without specific prior written permission. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 35 | * SUCH DAMAGE. |
| 36 | * |
| 37 | * @(#)ufs_lookup.c 8.9 (Berkeley) 8/11/94 |
| 38 | */ |
| 39 | |
| 40 | #include <sys/param.h> |
| 41 | #include <sys/systm.h> |
| 42 | #include <sys/kernel.h> |
| 43 | #include <sys/namei.h> |
| 44 | #include <sys/buf.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <sys/mount.h> |
| 47 | #include <sys/proc.h> |
| 48 | #include <sys/vnode.h> |
| 49 | |
| 50 | #include <ufs/ufs/quota.h> |
| 51 | #include <ufs/ufs/inode.h> |
| 52 | #include <ufs/ufs/dir.h> |
| 53 | #ifdef UFS_DIRHASH1 |
| 54 | #include <ufs/ufs/dirhash.h> |
| 55 | #endif |
| 56 | #include <ufs/ufs/ufsmount.h> |
| 57 | #include <ufs/ufs/ufs_extern.h> |
| 58 | |
| 59 | extern struct nchstats nchstats; |
| 60 | |
| 61 | #ifdef DIAGNOSTIC1 |
| 62 | int dirchk = 1; |
| 63 | #else |
| 64 | int dirchk = 0; |
| 65 | #endif |
| 66 | |
| 67 | /* |
| 68 | * Convert a component of a pathname into a pointer to a locked inode. |
| 69 | * This is a very central and rather complicated routine. |
| 70 | * If the file system is not maintained in a strict tree hierarchy, |
| 71 | * this can result in a deadlock situation (see comments in code below). |
| 72 | * |
| 73 | * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending |
| 74 | * on whether the name is to be looked up, created, renamed, or deleted. |
| 75 | * When CREATE, RENAME, or DELETE is specified, information usable in |
| 76 | * creating, renaming, or deleting a directory entry may be calculated. |
| 77 | * If flag has LOCKPARENT or'ed into it and the target of the pathname |
| 78 | * exists, lookup returns both the target and its parent directory locked. |
| 79 | * When creating or renaming and LOCKPARENT is specified, the target may |
| 80 | * not be ".". When deleting and LOCKPARENT is specified, the target may |
| 81 | * be "."., but the caller must check to ensure it does an vrele and vput |
| 82 | * instead of two vputs. |
| 83 | * |
| 84 | * Overall outline of ufs_lookup: |
| 85 | * |
| 86 | * check accessibility of directory |
| 87 | * look for name in cache, if found, then if at end of path |
| 88 | * and deleting or creating, drop it, else return name |
| 89 | * search for name in directory, to found or notfound |
| 90 | * notfound: |
| 91 | * if creating, return locked directory, leaving info on available slots |
| 92 | * else return error |
| 93 | * found: |
| 94 | * if at end of path and deleting, return information to allow delete |
| 95 | * if at end of path and rewriting (RENAME and LOCKPARENT), lock target |
| 96 | * inode and return info to allow rewrite |
| 97 | * if not at end, add name to cache; if at end and neither creating |
| 98 | * nor deleting, add name to cache |
| 99 | */ |
| 100 | int |
| 101 | ufs_lookup(void *v) |
| 102 | { |
| 103 | struct vop_lookup_args *ap = v; |
| 104 | struct vnode *vdp; /* vnode for directory being searched */ |
| 105 | struct inode *dp; /* inode for directory being searched */ |
| 106 | struct buf *bp; /* a buffer of directory entries */ |
| 107 | struct direct *ep; /* the current directory entry */ |
| 108 | int entryoffsetinblock; /* offset of ep in bp's buffer */ |
| 109 | enum {NONE, COMPACT, FOUND} slotstatus; |
| 110 | doff_tint32_t slotoffset; /* offset of area with free space */ |
| 111 | int slotsize; /* size of area at slotoffset */ |
| 112 | int slotfreespace; /* amount of space free in slot */ |
| 113 | int slotneeded; /* size of the entry we're seeking */ |
| 114 | int numdirpasses; /* strategy for directory search */ |
| 115 | doff_tint32_t endsearch; /* offset to end directory search */ |
| 116 | doff_tint32_t prevoff; /* prev entry dp->i_offset */ |
| 117 | struct vnode *pdp; /* saved dp during symlink work */ |
| 118 | struct vnode *tdp; /* returned by VFS_VGET */ |
| 119 | doff_tint32_t enduseful; /* pointer past last used dir slot */ |
| 120 | u_long bmask; /* block offset mask */ |
| 121 | int lockparent; /* 1 => lockparent flag is set */ |
| 122 | int wantparent; /* 1 => wantparent or lockparent flag */ |
| 123 | int namlen, error; |
| 124 | struct vnode **vpp = ap->a_vpp; |
| 125 | struct componentname *cnp = ap->a_cnp; |
| 126 | struct ucred *cred = cnp->cn_cred; |
| 127 | int flags; |
| 128 | int nameiop = cnp->cn_nameiop; |
| 129 | |
| 130 | cnp->cn_flags &= ~PDIRUNLOCK0x200000; |
| 131 | flags = cnp->cn_flags; |
| 132 | |
| 133 | bp = NULL((void *)0); |
| 134 | slotoffset = -1; |
| 135 | *vpp = NULL((void *)0); |
| 136 | vdp = ap->a_dvp; |
| 137 | dp = VTOI(vdp)((struct inode *)(vdp)->v_data); |
| 138 | lockparent = flags & LOCKPARENT0x0008; |
| 139 | wantparent = flags & (LOCKPARENT0x0008|WANTPARENT0x0010); |
| 140 | |
| 141 | /* |
| 142 | * Check accessibility of directory. |
| 143 | */ |
| 144 | if ((DIP(dp, mode)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_mode : (dp)->dinode_u.ffs2_din->di_mode) & IFMT0170000) != IFDIR0040000) |
| 145 | return (ENOTDIR20); |
| 146 | if ((error = VOP_ACCESS(vdp, VEXEC00100, cred, cnp->cn_proc)) != 0) |
| 147 | return (error); |
| 148 | |
| 149 | if ((flags & ISLASTCN0x008000) && (vdp->v_mount->mnt_flag & MNT_RDONLY0x00000001) && |
| 150 | (cnp->cn_nameiop == DELETE2 || cnp->cn_nameiop == RENAME3)) |
| 151 | return (EROFS30); |
| 152 | |
| 153 | /* |
| 154 | * We now have a segment name to search for, and a directory to search. |
| 155 | * |
| 156 | * Before tediously performing a linear scan of the directory, |
| 157 | * check the name cache to see if the directory/name pair |
| 158 | * we are looking for is known already. |
| 159 | */ |
| 160 | if ((error = cache_lookup(vdp, vpp, cnp)) >= 0) |
| 161 | return (error); |
| 162 | |
| 163 | /* |
| 164 | * Suppress search for slots unless creating |
| 165 | * file and at end of pathname, in which case |
| 166 | * we watch for a place to put the new file in |
| 167 | * case it doesn't already exist. |
| 168 | */ |
| 169 | slotstatus = FOUND; |
| 170 | slotfreespace = slotsize = slotneeded = 0; |
| 171 | if ((nameiop == CREATE1 || nameiop == RENAME3) && |
| 172 | (flags & ISLASTCN0x008000)) { |
| 173 | slotstatus = NONE; |
| 174 | slotneeded = (sizeof(struct direct) - MAXNAMLEN255 + |
| 175 | cnp->cn_namelen + 3) &~ 3; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * If there is cached information on a previous search of |
| 180 | * this directory, pick up where we last left off. |
| 181 | * We cache only lookups as these are the most common |
| 182 | * and have the greatest payoff. Caching CREATE has little |
| 183 | * benefit as it usually must search the entire directory |
| 184 | * to determine that the entry does not exist. Caching the |
| 185 | * location of the last DELETE or RENAME has not reduced |
| 186 | * profiling time and hence has been removed in the interest |
| 187 | * of simplicity. |
| 188 | */ |
| 189 | bmask = VFSTOUFS(vdp->v_mount)((struct ufsmount *)((vdp->v_mount)->mnt_data))->um_mountp->mnt_stat.f_iosize - 1; |
| 190 | |
| 191 | #ifdef UFS_DIRHASH1 |
| 192 | /* |
| 193 | * Use dirhash for fast operations on large directories. The logic |
| 194 | * to determine whether to hash the directory is contained within |
| 195 | * ufsdirhash_build(); a zero return means that it decided to hash |
| 196 | * this directory and it successfully built up the hash table. |
| 197 | */ |
| 198 | if (ufsdirhash_build(dp) == 0) { |
| 199 | /* Look for a free slot if needed. */ |
| 200 | enduseful = DIP(dp, size)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size); |
| 201 | if (slotstatus != FOUND) { |
| 202 | slotoffset = ufsdirhash_findfree(dp, slotneeded, |
| 203 | &slotsize); |
| 204 | if (slotoffset >= 0) { |
| 205 | slotstatus = COMPACT; |
| 206 | enduseful = ufsdirhash_enduseful(dp); |
| 207 | if (enduseful < 0) |
| 208 | enduseful = DIP(dp, size)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size); |
| 209 | } |
| 210 | } |
| 211 | /* Look up the component. */ |
| 212 | numdirpasses = 1; |
| 213 | entryoffsetinblock = 0; /* silence compiler warning */ |
| 214 | switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen, |
| 215 | &dp->i_offset, &bp, nameiop == DELETE2 ? &prevoff : NULL((void *)0))) { |
| 216 | case 0: |
| 217 | ep = (struct direct *)((char *)bp->b_data + |
| 218 | (dp->i_offset & bmask)); |
| 219 | goto foundentry; |
| 220 | case ENOENT2: |
| 221 | #define roundup2(x, y)(((x)+((y)-1))&(~((y)-1))) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ |
| 222 | dp->i_offset = roundup2(DIP(dp, size), DIRBLKSIZ)((((((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size))+(((1 << 9))-1))&(~(((1 << 9))-1))); |
| 223 | goto notfound; |
| 224 | default: |
| 225 | /* Something failed; just do a linear search. */ |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | #endif /* UFS_DIRHASH */ |
| 230 | |
| 231 | if (nameiop != LOOKUP0 || dp->i_diroff == 0 || |
| 232 | dp->i_diroff >= DIP(dp, size)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size)) { |
| 233 | entryoffsetinblock = 0; |
| 234 | dp->i_offset = 0; |
| 235 | numdirpasses = 1; |
| 236 | } else { |
| 237 | dp->i_offset = dp->i_diroff; |
| 238 | if ((entryoffsetinblock = dp->i_offset & bmask) && |
| 239 | (error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, NULL, &bp)((dp)->i_vtbl->iv_bufatoff)((dp), ((off_t)dp->i_offset ), (((void *)0)), (&bp)))) |
| 240 | return (error); |
| 241 | numdirpasses = 2; |
| 242 | nchstats.ncs_2passes++; |
| 243 | } |
| 244 | prevoff = dp->i_offset; |
| 245 | endsearch = roundup(DIP(dp, size), DIRBLKSIZ)(((((((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u. ffs1_din->di_size : (dp)->dinode_u.ffs2_din->di_size ))+(((1 << 9))-1))/((1 << 9)))*((1 << 9))); |
| 246 | enduseful = 0; |
| 247 | |
| 248 | searchloop: |
| 249 | while (dp->i_offset < endsearch) { |
| 250 | /* |
| 251 | * If necessary, get the next directory block. |
| 252 | */ |
| 253 | if ((dp->i_offset & bmask) == 0) { |
| 254 | if (bp != NULL((void *)0)) |
| 255 | brelse(bp); |
| 256 | error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, NULL,((dp)->i_vtbl->iv_bufatoff)((dp), ((off_t)dp->i_offset ), (((void *)0)), (&bp)) |
| 257 | &bp)((dp)->i_vtbl->iv_bufatoff)((dp), ((off_t)dp->i_offset ), (((void *)0)), (&bp)); |
| 258 | if (error) |
| 259 | return (error); |
| 260 | entryoffsetinblock = 0; |
| 261 | } |
| 262 | /* |
| 263 | * If still looking for a slot, and at a DIRBLKSIZE |
| 264 | * boundary, have to start looking for free space again. |
| 265 | */ |
| 266 | if (slotstatus == NONE && |
| 267 | (entryoffsetinblock & (DIRBLKSIZ(1 << 9) - 1)) == 0) { |
| 268 | slotoffset = -1; |
| 269 | slotfreespace = 0; |
| 270 | } |
| 271 | /* |
| 272 | * Get pointer to next entry. |
| 273 | * Full validation checks are slow, so we only check |
| 274 | * enough to insure forward progress through the |
| 275 | * directory. Complete checks can be run by patching |
| 276 | * "dirchk" to be true. |
| 277 | */ |
| 278 | ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock); |
| 279 | if (ep->d_reclen == 0 || |
| 280 | (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) { |
| 281 | int i; |
| 282 | |
| 283 | ufs_dirbad(dp, dp->i_offset, "mangled entry"); |
| 284 | i = DIRBLKSIZ(1 << 9) - (entryoffsetinblock & (DIRBLKSIZ(1 << 9) - 1)); |
| 285 | dp->i_offset += i; |
| 286 | entryoffsetinblock += i; |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * If an appropriate sized slot has not yet been found, |
| 292 | * check to see if one is available. Also accumulate space |
| 293 | * in the current block so that we can determine if |
| 294 | * compaction is viable. |
| 295 | */ |
| 296 | if (slotstatus != FOUND) { |
| 297 | int size = ep->d_reclen; |
| 298 | |
| 299 | if (ep->d_ino != 0) |
| 300 | size -= DIRSIZ(ep)((sizeof(struct direct) - (255 +1)) + (((ep)->d_namlen+1 + 3) &~ 3)); |
| 301 | if (size > 0) { |
| 302 | if (size >= slotneeded) { |
| 303 | slotstatus = FOUND; |
| 304 | slotoffset = dp->i_offset; |
| 305 | slotsize = ep->d_reclen; |
| 306 | } else if (slotstatus == NONE) { |
| 307 | slotfreespace += size; |
| 308 | if (slotoffset == -1) |
| 309 | slotoffset = dp->i_offset; |
| 310 | if (slotfreespace >= slotneeded) { |
| 311 | slotstatus = COMPACT; |
| 312 | slotsize = dp->i_offset + |
| 313 | ep->d_reclen - slotoffset; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Check for a name match. |
| 321 | */ |
| 322 | if (ep->d_ino) { |
| 323 | namlen = ep->d_namlen; |
| 324 | if (namlen == cnp->cn_namelen && |
| 325 | !memcmp(cnp->cn_nameptr, ep->d_name, namlen)__builtin_memcmp((cnp->cn_nameptr), (ep->d_name), (namlen ))) { |
| 326 | #ifdef UFS_DIRHASH1 |
| 327 | foundentry: |
| 328 | #endif |
| 329 | /* |
| 330 | * Save directory entry's inode number and |
| 331 | * reclen in ndp->ni_ufs area, and release |
| 332 | * directory buffer. |
| 333 | */ |
| 334 | dp->i_ino = ep->d_ino; |
| 335 | dp->i_reclen = ep->d_reclen; |
| 336 | goto found; |
| 337 | } |
| 338 | } |
| 339 | prevoff = dp->i_offset; |
| 340 | dp->i_offset += ep->d_reclen; |
| 341 | entryoffsetinblock += ep->d_reclen; |
| 342 | if (ep->d_ino) |
| 343 | enduseful = dp->i_offset; |
| 344 | } |
| 345 | #ifdef UFS_DIRHASH1 |
| 346 | notfound: |
| 347 | #endif |
| 348 | /* |
| 349 | * If we started in the middle of the directory and failed |
| 350 | * to find our target, we must check the beginning as well. |
| 351 | */ |
| 352 | if (numdirpasses == 2) { |
| 353 | numdirpasses--; |
| 354 | dp->i_offset = 0; |
| 355 | endsearch = dp->i_diroff; |
| 356 | goto searchloop; |
| 357 | } |
| 358 | if (bp != NULL((void *)0)) |
| 359 | brelse(bp); |
| 360 | /* |
| 361 | * If creating, and at end of pathname and current |
| 362 | * directory has not been removed, then can consider |
| 363 | * allowing file to be created. |
| 364 | */ |
| 365 | if ((nameiop == CREATE1 || nameiop == RENAME3) && |
| 366 | (flags & ISLASTCN0x008000) && dp->i_effnlink != 0) { |
| 367 | /* |
| 368 | * Access for write is interpreted as allowing |
| 369 | * creation of files in the directory. |
| 370 | */ |
| 371 | error = VOP_ACCESS(vdp, VWRITE00200, cred, cnp->cn_proc); |
| 372 | if (error) |
| 373 | return (error); |
| 374 | /* |
| 375 | * Return an indication of where the new directory |
| 376 | * entry should be put. If we didn't find a slot, |
| 377 | * then set dp->i_count to 0 indicating |
| 378 | * that the new slot belongs at the end of the |
| 379 | * directory. If we found a slot, then the new entry |
| 380 | * can be put in the range from dp->i_offset to |
| 381 | * dp->i_offset + dp->i_count. |
| 382 | */ |
| 383 | if (slotstatus == NONE) { |
| 384 | dp->i_offset = roundup(DIP(dp, size), DIRBLKSIZ)(((((((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u. ffs1_din->di_size : (dp)->dinode_u.ffs2_din->di_size ))+(((1 << 9))-1))/((1 << 9)))*((1 << 9))); |
| 385 | dp->i_count = 0; |
| 386 | enduseful = dp->i_offset; |
| 387 | } else if (nameiop == DELETE2) { |
| 388 | dp->i_offset = slotoffset; |
| 389 | if ((dp->i_offset & (DIRBLKSIZ(1 << 9) - 1)) == 0) |
| 390 | dp->i_count = 0; |
| 391 | else |
| 392 | dp->i_count = dp->i_offset - prevoff; |
| 393 | } else { |
| 394 | dp->i_offset = slotoffset; |
| 395 | dp->i_count = slotsize; |
| 396 | if (enduseful < slotoffset + slotsize) |
| 397 | enduseful = slotoffset + slotsize; |
| 398 | } |
| 399 | dp->i_endoff = roundup(enduseful, DIRBLKSIZ)((((enduseful)+(((1 << 9))-1))/((1 << 9)))*((1 << 9))); |
| 400 | /* |
| 401 | * We return with the directory locked, so that |
| 402 | * the parameters we set up above will still be |
| 403 | * valid if we actually decide to do a direnter(). |
| 404 | * We return ni_vp == NULL to indicate that the entry |
| 405 | * does not currently exist; we leave a pointer to |
| 406 | * the (locked) directory inode in ndp->ni_dvp. |
| 407 | * The pathname buffer is saved so that the name |
| 408 | * can be obtained later. |
| 409 | * |
| 410 | * NB - if the directory is unlocked, then this |
| 411 | * information cannot be used. |
| 412 | */ |
| 413 | cnp->cn_flags |= SAVENAME0x000800; |
| 414 | if (!lockparent) { |
| 415 | VOP_UNLOCK(vdp); |
| 416 | cnp->cn_flags |= PDIRUNLOCK0x200000; |
| 417 | } |
| 418 | return (EJUSTRETURN-2); |
| 419 | } |
| 420 | /* |
| 421 | * Insert name into cache (as non-existent) if appropriate. |
| 422 | */ |
| 423 | if ((cnp->cn_flags & MAKEENTRY0x004000) && nameiop != CREATE1) |
| 424 | cache_enter(vdp, *vpp, cnp); |
| 425 | return (ENOENT2); |
| 426 | |
| 427 | found: |
| 428 | if (numdirpasses == 2) |
| 429 | nchstats.ncs_pass2++; |
| 430 | /* |
| 431 | * Check that directory length properly reflects presence |
| 432 | * of this entry. |
| 433 | */ |
| 434 | if (dp->i_offset + DIRSIZ(ep)((sizeof(struct direct) - (255 +1)) + (((ep)->d_namlen+1 + 3) &~ 3)) > DIP(dp, size)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size)) { |
| 435 | ufs_dirbad(dp, dp->i_offset, "i_ffs_size too small"); |
| 436 | DIP_ASSIGN(dp, size, dp->i_offset + DIRSIZ(ep))do { if ((dp)->i_ump->um_fstype == 1) (dp)->dinode_u .ffs1_din->di_size = (dp->i_offset + ((sizeof(struct direct ) - (255 +1)) + (((ep)->d_namlen+1 + 3) &~ 3))); else ( dp)->dinode_u.ffs2_din->di_size = (dp->i_offset + (( sizeof(struct direct) - (255 +1)) + (((ep)->d_namlen+1 + 3 ) &~ 3))); } while (0); |
| 437 | dp->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
| 438 | } |
| 439 | brelse(bp); |
| 440 | |
| 441 | /* |
| 442 | * Found component in pathname. |
| 443 | * If the final component of path name, save information |
| 444 | * in the cache as to where the entry was found. |
| 445 | */ |
| 446 | if ((flags & ISLASTCN0x008000) && nameiop == LOOKUP0) |
| 447 | dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ(1 << 9) - 1); |
| 448 | |
| 449 | /* |
| 450 | * If deleting, and at end of pathname, return |
| 451 | * parameters which can be used to remove file. |
| 452 | * If the wantparent flag isn't set, we return only |
| 453 | * the directory (in ndp->ni_dvp), otherwise we go |
| 454 | * on and lock the inode, being careful with ".". |
| 455 | */ |
| 456 | if (nameiop == DELETE2 && (flags & ISLASTCN0x008000)) { |
| 457 | /* |
| 458 | * Write access to directory required to delete files. |
| 459 | */ |
| 460 | error = VOP_ACCESS(vdp, VWRITE00200, cred, cnp->cn_proc); |
| 461 | if (error) |
| 462 | return (error); |
| 463 | /* |
| 464 | * Return pointer to current entry in dp->i_offset, |
| 465 | * and distance past previous entry (if there |
| 466 | * is a previous entry in this block) in dp->i_count. |
| 467 | * Save directory inode pointer in ndp->ni_dvp for dirremove(). |
| 468 | */ |
| 469 | if ((dp->i_offset & (DIRBLKSIZ(1 << 9) - 1)) == 0) |
| 470 | dp->i_count = 0; |
| 471 | else |
| 472 | dp->i_count = dp->i_offset - prevoff; |
| 473 | if (dp->i_number == dp->i_ino) { |
| 474 | vref(vdp); |
| 475 | *vpp = vdp; |
| 476 | return (0); |
| 477 | } |
| 478 | error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)(*(vdp->v_mount)->mnt_op->vfs_vget)(vdp->v_mount, dp->i_ino, &tdp); |
| 479 | if (error) |
| 480 | return (error); |
| 481 | /* |
| 482 | * If directory is "sticky", then user must own |
| 483 | * the directory, or the file in it, else she |
| 484 | * may not delete it (unless she's root). This |
| 485 | * implements append-only directories. |
| 486 | */ |
| 487 | if ((DIP(dp, mode)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_mode : (dp)->dinode_u.ffs2_din->di_mode) & ISVTX0001000) && |
| 488 | cred->cr_uid != 0 && |
| 489 | cred->cr_uid != DIP(dp, uid)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_uid : (dp)->dinode_u.ffs2_din->di_uid) && |
| 490 | !vnoperm(vdp) && |
| 491 | DIP(VTOI(tdp), uid)(((((struct inode *)(tdp)->v_data))->i_ump->um_fstype == 1) ? (((struct inode *)(tdp)->v_data))->dinode_u.ffs1_din ->di_uid : (((struct inode *)(tdp)->v_data))->dinode_u .ffs2_din->di_uid) != cred->cr_uid) { |
| 492 | vput(tdp); |
| 493 | return (EPERM1); |
| 494 | } |
| 495 | *vpp = tdp; |
| 496 | if (!lockparent) { |
| 497 | VOP_UNLOCK(vdp); |
| 498 | cnp->cn_flags |= PDIRUNLOCK0x200000; |
| 499 | } |
| 500 | return (0); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * If rewriting (RENAME), return the inode and the |
| 505 | * information required to rewrite the present directory |
| 506 | * Must get inode of directory entry to verify it's a |
| 507 | * regular file, or empty directory. |
| 508 | */ |
| 509 | if (nameiop == RENAME3 && wantparent && |
| 510 | (flags & ISLASTCN0x008000)) { |
| 511 | error = VOP_ACCESS(vdp, VWRITE00200, cred, cnp->cn_proc); |
| 512 | if (error) |
| 513 | return (error); |
| 514 | /* |
| 515 | * Careful about locking second inode. |
| 516 | * This can only occur if the target is ".". |
| 517 | */ |
| 518 | if (dp->i_number == dp->i_ino) |
| 519 | return (EISDIR21); |
| 520 | error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)(*(vdp->v_mount)->mnt_op->vfs_vget)(vdp->v_mount, dp->i_ino, &tdp); |
| 521 | if (error) |
| 522 | return (error); |
| 523 | *vpp = tdp; |
| 524 | cnp->cn_flags |= SAVENAME0x000800; |
| 525 | if (!lockparent) { |
| 526 | VOP_UNLOCK(vdp); |
| 527 | cnp->cn_flags |= PDIRUNLOCK0x200000; |
| 528 | } |
| 529 | return (0); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Step through the translation in the name. We do not `vput' the |
| 534 | * directory because we may need it again if a symbolic link |
| 535 | * is relative to the current directory. Instead we save it |
| 536 | * unlocked as "pdp". We must get the target inode before unlocking |
| 537 | * the directory to insure that the inode will not be removed |
| 538 | * before we get it. We prevent deadlock by always fetching |
| 539 | * inodes from the root, moving down the directory tree. Thus |
| 540 | * when following backward pointers ".." we must unlock the |
| 541 | * parent directory before getting the requested directory. |
| 542 | * There is a potential race condition here if both the current |
| 543 | * and parent directories are removed before the VFS_VGET for the |
| 544 | * inode associated with ".." returns. We hope that this occurs |
| 545 | * infrequently since we cannot avoid this race condition without |
| 546 | * implementing a sophisticated deadlock detection algorithm. |
| 547 | * Note also that this simple deadlock detection scheme will not |
| 548 | * work if the file system has any hard links other than ".." |
| 549 | * that point backwards in the directory structure. |
| 550 | */ |
| 551 | pdp = vdp; |
| 552 | if (flags & ISDOTDOT0x002000) { |
| 553 | VOP_UNLOCK(pdp); /* race to get the inode */ |
| 554 | cnp->cn_flags |= PDIRUNLOCK0x200000; |
| 555 | error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)(*(vdp->v_mount)->mnt_op->vfs_vget)(vdp->v_mount, dp->i_ino, &tdp); |
| 556 | if (error) { |
| 557 | if (vn_lock(pdp, LK_EXCLUSIVE0x0001UL | LK_RETRY0x2000UL) == 0) |
| 558 | cnp->cn_flags &= ~PDIRUNLOCK0x200000; |
| 559 | return (error); |
| 560 | } |
| 561 | if (lockparent && (flags & ISLASTCN0x008000)) { |
| 562 | if ((error = vn_lock(pdp, LK_EXCLUSIVE0x0001UL))) { |
| 563 | vput(tdp); |
| 564 | return (error); |
| 565 | } |
| 566 | cnp->cn_flags &= ~PDIRUNLOCK0x200000; |
| 567 | } |
| 568 | *vpp = tdp; |
| 569 | } else if (dp->i_number == dp->i_ino) { |
| 570 | vref(vdp); /* we want ourself, ie "." */ |
| 571 | *vpp = vdp; |
| 572 | } else { |
| 573 | error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)(*(vdp->v_mount)->mnt_op->vfs_vget)(vdp->v_mount, dp->i_ino, &tdp); |
| 574 | if (error) |
| 575 | return (error); |
| 576 | if (!lockparent || !(flags & ISLASTCN0x008000)) { |
| 577 | VOP_UNLOCK(pdp); |
| 578 | cnp->cn_flags |= PDIRUNLOCK0x200000; |
| 579 | } |
| 580 | *vpp = tdp; |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Insert name into cache if appropriate. |
| 585 | */ |
| 586 | if (cnp->cn_flags & MAKEENTRY0x004000) |
| 587 | cache_enter(vdp, *vpp, cnp); |
| 588 | return (0); |
| 589 | } |
| 590 | |
| 591 | void |
| 592 | ufs_dirbad(struct inode *ip, doff_tint32_t offset, char *how) |
| 593 | { |
| 594 | struct mount *mp; |
| 595 | |
| 596 | mp = ITOV(ip)((ip)->i_vnode)->v_mount; |
| 597 | (void)printf("%s: bad dir ino %u at offset %d: %s\n", |
| 598 | mp->mnt_stat.f_mntonname, ip->i_number, offset, how); |
| 599 | if ((mp->mnt_stat.f_flags & MNT_RDONLY0x00000001) == 0) |
| 600 | panic("bad dir"); |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Do consistency checking on a directory entry: |
| 605 | * record length must be multiple of 4 |
| 606 | * entry must fit in rest of its DIRBLKSIZ block |
| 607 | * record must be large enough to contain entry |
| 608 | * name is not longer than MAXNAMLEN |
| 609 | * name must be as long as advertised, and null terminated |
| 610 | */ |
| 611 | int |
| 612 | ufs_dirbadentry(struct vnode *vdp, struct direct *ep, int entryoffsetinblock) |
| 613 | { |
| 614 | struct inode *dp; |
| 615 | int i; |
| 616 | int namlen; |
| 617 | |
| 618 | dp = VTOI(vdp)((struct inode *)(vdp)->v_data); |
Value stored to 'dp' is never read | |
| 619 | |
| 620 | namlen = ep->d_namlen; |
| 621 | if ((ep->d_reclen & 0x3) != 0 || |
| 622 | ep->d_reclen > DIRBLKSIZ(1 << 9) - (entryoffsetinblock & (DIRBLKSIZ(1 << 9) - 1)) || |
| 623 | ep->d_reclen < DIRSIZ(ep)((sizeof(struct direct) - (255 +1)) + (((ep)->d_namlen+1 + 3) &~ 3)) || namlen > MAXNAMLEN255) { |
| 624 | /*return (1); */ |
| 625 | printf("First bad\n"); |
| 626 | goto bad; |
| 627 | } |
| 628 | if (ep->d_ino == 0) |
| 629 | return (0); |
| 630 | for (i = 0; i < namlen; i++) |
| 631 | if (ep->d_name[i] == '\0') { |
| 632 | /*return (1); */ |
| 633 | printf("Second bad\n"); |
| 634 | goto bad; |
| 635 | } |
| 636 | if (ep->d_name[i]) |
| 637 | goto bad; |
| 638 | return (0); |
| 639 | bad: |
| 640 | return (1); |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Construct a new directory entry after a call to namei, using the |
| 645 | * parameters that it left in the componentname argument cnp. The |
| 646 | * argument ip is the inode to which the new directory entry will refer. |
| 647 | */ |
| 648 | void |
| 649 | ufs_makedirentry(struct inode *ip, struct componentname *cnp, |
| 650 | struct direct *newdirp) |
| 651 | { |
| 652 | #ifdef DIAGNOSTIC1 |
| 653 | if ((cnp->cn_flags & SAVENAME0x000800) == 0) |
| 654 | panic("ufs_makedirentry: missing name"); |
| 655 | #endif |
| 656 | newdirp->d_ino = ip->i_number; |
| 657 | newdirp->d_namlen = cnp->cn_namelen; |
| 658 | memset(newdirp->d_name + (cnp->cn_namelen & ~(DIR_ROUNDUP-1)),__builtin_memset((newdirp->d_name + (cnp->cn_namelen & ~(4 -1))), (0), (4)) |
| 659 | 0, DIR_ROUNDUP)__builtin_memset((newdirp->d_name + (cnp->cn_namelen & ~(4 -1))), (0), (4)); |
| 660 | memcpy(newdirp->d_name, cnp->cn_nameptr, cnp->cn_namelen)__builtin_memcpy((newdirp->d_name), (cnp->cn_nameptr), ( cnp->cn_namelen)); |
| 661 | newdirp->d_type = IFTODT(DIP(ip, mode))((((((ip)->i_ump->um_fstype == 1) ? (ip)->dinode_u.ffs1_din ->di_mode : (ip)->dinode_u.ffs2_din->di_mode)) & 0170000) >> 12); |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Write a directory entry after a call to namei, using the parameters |
| 666 | * that it left in nameidata. The argument dirp is the new directory |
| 667 | * entry contents. Dvp is a pointer to the directory to be written, |
| 668 | * which was left locked by namei. Remaining parameters (dp->i_offset, |
| 669 | * dp->i_count) indicate how the space for the new entry is to be obtained. |
| 670 | * Non-null bp indicates that a directory is being created (for the |
| 671 | * soft dependency code). |
| 672 | */ |
| 673 | int |
| 674 | ufs_direnter(struct vnode *dvp, struct vnode *tvp, struct direct *dirp, |
| 675 | struct componentname *cnp, struct buf *newdirbp) |
| 676 | { |
| 677 | struct ucred *cr; |
| 678 | struct proc *p; |
| 679 | int newentrysize; |
| 680 | struct inode *dp; |
| 681 | struct buf *bp; |
| 682 | u_int dsize; |
| 683 | struct direct *ep, *nep; |
| 684 | int error, ret, blkoff, loc, spacefree, flags; |
| 685 | char *dirbuf; |
| 686 | |
| 687 | error = 0; |
| 688 | cr = cnp->cn_cred; |
| 689 | p = cnp->cn_proc; |
| 690 | dp = VTOI(dvp)((struct inode *)(dvp)->v_data); |
| 691 | newentrysize = DIRSIZ(dirp)((sizeof(struct direct) - (255 +1)) + (((dirp)->d_namlen+1 + 3) &~ 3)); |
| 692 | |
| 693 | if (dp->i_count == 0) { |
| 694 | /* |
| 695 | * If dp->i_count is 0, then namei could find no |
| 696 | * space in the directory. Here, dp->i_offset will |
| 697 | * be on a directory block boundary and we will write the |
| 698 | * new entry into a fresh block. |
| 699 | */ |
| 700 | if (dp->i_offset & (DIRBLKSIZ(1 << 9) - 1)) |
| 701 | panic("ufs_direnter: newblk"); |
| 702 | flags = B_CLRBUF0x01; |
| 703 | if (!DOINGSOFTDEP(dvp)((dvp)->v_mount->mnt_flag & 0x04000000)) |
| 704 | flags |= B_SYNC0x02; |
| 705 | if ((error = UFS_BUF_ALLOC(dp, (off_t)dp->i_offset, DIRBLKSIZ,((dp)->i_vtbl->iv_buf_alloc)((dp), ((off_t)dp->i_offset ), ((1 << 9)), (cr), (flags), (&bp)) |
| 706 | cr, flags, &bp)((dp)->i_vtbl->iv_buf_alloc)((dp), ((off_t)dp->i_offset ), ((1 << 9)), (cr), (flags), (&bp))) != 0) { |
| 707 | if (DOINGSOFTDEP(dvp)((dvp)->v_mount->mnt_flag & 0x04000000) && newdirbp != NULL((void *)0)) |
| 708 | bdwrite(newdirbp); |
| 709 | return (error); |
| 710 | } |
| 711 | DIP_ASSIGN(dp, size, dp->i_offset + DIRBLKSIZ)do { if ((dp)->i_ump->um_fstype == 1) (dp)->dinode_u .ffs1_din->di_size = (dp->i_offset + (1 << 9)); else (dp)->dinode_u.ffs2_din->di_size = (dp->i_offset + ( 1 << 9)); } while (0); |
| 712 | dp->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
| 713 | uvm_vnp_setsize(dvp, DIP(dp, size)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size)); |
| 714 | dirp->d_reclen = DIRBLKSIZ(1 << 9); |
| 715 | blkoff = dp->i_offset & |
| 716 | (VFSTOUFS(dvp->v_mount)((struct ufsmount *)((dvp->v_mount)->mnt_data))->um_mountp->mnt_stat.f_iosize - 1); |
| 717 | memcpy(bp->b_data + blkoff, dirp, newentrysize)__builtin_memcpy((bp->b_data + blkoff), (dirp), (newentrysize )); |
| 718 | |
| 719 | #ifdef UFS_DIRHASH1 |
| 720 | if (dp->i_dirhashinode_ext.dirhash != NULL((void *)0)) { |
| 721 | ufsdirhash_newblk(dp, dp->i_offset); |
| 722 | ufsdirhash_add(dp, dirp, dp->i_offset); |
| 723 | ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff, |
| 724 | dp->i_offset); |
| 725 | } |
| 726 | #endif |
| 727 | |
| 728 | if (DOINGSOFTDEP(dvp)((dvp)->v_mount->mnt_flag & 0x04000000)) { |
| 729 | /* |
| 730 | * Ensure that the entire newly allocated block is a |
| 731 | * valid directory so that future growth within the |
| 732 | * block does not have to ensure that the block is |
| 733 | * written before the inode. |
| 734 | */ |
| 735 | blkoff += DIRBLKSIZ(1 << 9); |
| 736 | while (blkoff < bp->b_bcount) { |
| 737 | ((struct direct *) |
| 738 | (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ(1 << 9); |
| 739 | blkoff += DIRBLKSIZ(1 << 9); |
| 740 | } |
| 741 | if (softdep_setup_directory_add(bp, dp, dp->i_offset, |
| 742 | dirp->d_ino, newdirbp, 1) == 0) { |
| 743 | bdwrite(bp); |
| 744 | return (UFS_UPDATE(dp, 0)((dp)->i_vtbl->iv_update)((dp), (0))); |
| 745 | } |
| 746 | /* We have just allocated a directory block in an |
| 747 | * indirect block. Rather than tracking when it gets |
| 748 | * claimed by the inode, we simply do a VOP_FSYNC |
| 749 | * now to ensure that it is there (in case the user |
| 750 | * does a future fsync). Note that we have to unlock |
| 751 | * the inode for the entry that we just entered, as |
| 752 | * the VOP_FSYNC may need to lock other inodes which |
| 753 | * can lead to deadlock if we also hold a lock on |
| 754 | * the newly entered node. |
| 755 | */ |
| 756 | if ((error = VOP_BWRITE(bp))) |
| 757 | return (error); |
| 758 | if (tvp != NULL((void *)0)) |
| 759 | VOP_UNLOCK(tvp); |
| 760 | error = VOP_FSYNC(dvp, p->p_ucred, MNT_WAIT1, p); |
| 761 | if (tvp != NULL((void *)0)) |
| 762 | vn_lock(tvp, LK_EXCLUSIVE0x0001UL | LK_RETRY0x2000UL); |
| 763 | return (error); |
| 764 | } |
| 765 | error = VOP_BWRITE(bp); |
| 766 | ret = UFS_UPDATE(dp, !DOINGSOFTDEP(dvp))((dp)->i_vtbl->iv_update)((dp), (!((dvp)->v_mount-> mnt_flag & 0x04000000))); |
| 767 | if (error == 0) |
| 768 | return (ret); |
| 769 | return (error); |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * If dp->i_count is non-zero, then namei found space for the new |
| 774 | * entry in the range dp->i_offset to dp->i_offset + dp->i_count |
| 775 | * in the directory. To use this space, we may have to compact |
| 776 | * the entries located there, by copying them together towards the |
| 777 | * beginning of the block, leaving the free space in one usable |
| 778 | * chunk at the end. |
| 779 | */ |
| 780 | |
| 781 | /* |
| 782 | * Increase size of directory if entry eats into new space. |
| 783 | * This should never push the size past a new multiple of |
| 784 | * DIRBLKSIZE. |
| 785 | * |
| 786 | * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. |
| 787 | */ |
| 788 | if (dp->i_offset + dp->i_count > DIP(dp, size)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size)) |
| 789 | DIP_ASSIGN(dp, size, dp->i_offset + dp->i_count)do { if ((dp)->i_ump->um_fstype == 1) (dp)->dinode_u .ffs1_din->di_size = (dp->i_offset + dp->i_count); else (dp)->dinode_u.ffs2_din->di_size = (dp->i_offset + dp ->i_count); } while (0); |
| 790 | /* |
| 791 | * Get the block containing the space for the new directory entry. |
| 792 | */ |
| 793 | if ((error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, &dirbuf, &bp)((dp)->i_vtbl->iv_bufatoff)((dp), ((off_t)dp->i_offset ), (&dirbuf), (&bp))) |
| 794 | != 0) { |
| 795 | if (DOINGSOFTDEP(dvp)((dvp)->v_mount->mnt_flag & 0x04000000) && newdirbp != NULL((void *)0)) |
| 796 | bdwrite(newdirbp); |
| 797 | return (error); |
| 798 | } |
| 799 | /* |
| 800 | * Find space for the new entry. In the simple case, the entry at |
| 801 | * offset base will have the space. If it does not, then namei |
| 802 | * arranged that compacting the region dp->i_offset to |
| 803 | * dp->i_offset + dp->i_count would yield the space. |
| 804 | */ |
| 805 | ep = (struct direct *)dirbuf; |
| 806 | dsize = ep->d_ino ? DIRSIZ(ep)((sizeof(struct direct) - (255 +1)) + (((ep)->d_namlen+1 + 3) &~ 3)) : 0; |
| 807 | spacefree = ep->d_reclen - dsize; |
| 808 | for (loc = ep->d_reclen; loc < dp->i_count; ) { |
| 809 | nep = (struct direct *)(dirbuf + loc); |
| 810 | |
| 811 | /* Trim the existing slot (NB: dsize may be zero). */ |
| 812 | ep->d_reclen = dsize; |
| 813 | ep = (struct direct *)((char *)ep + dsize); |
| 814 | |
| 815 | /* Read nep->d_reclen now as the memmove() may clobber it. */ |
| 816 | loc += nep->d_reclen; |
| 817 | if (nep->d_ino == 0) { |
| 818 | /* |
| 819 | * A mid-block unused entry. Such entries are |
| 820 | * never created by the kernel, but fsck_ffs |
| 821 | * can create them (and it doesn't fix them). |
| 822 | * |
| 823 | * Add up the free space, and initialise the |
| 824 | * relocated entry since we don't memmove it. |
| 825 | */ |
| 826 | spacefree += nep->d_reclen; |
| 827 | ep->d_ino = 0; |
| 828 | dsize = 0; |
| 829 | continue; |
| 830 | } |
| 831 | dsize = DIRSIZ(nep)((sizeof(struct direct) - (255 +1)) + (((nep)->d_namlen+1 + 3) &~ 3)); |
| 832 | spacefree += nep->d_reclen - dsize; |
| 833 | #ifdef UFS_DIRHASH1 |
| 834 | if (dp->i_dirhashinode_ext.dirhash != NULL((void *)0)) |
| 835 | ufsdirhash_move(dp, nep, |
| 836 | dp->i_offset + ((char *)nep - dirbuf), |
| 837 | dp->i_offset + ((char *)ep - dirbuf)); |
| 838 | #endif |
| 839 | if (DOINGSOFTDEP(dvp)((dvp)->v_mount->mnt_flag & 0x04000000)) |
| 840 | softdep_change_directoryentry_offset(dp, dirbuf, |
| 841 | (caddr_t)nep, (caddr_t)ep, dsize); |
| 842 | else |
| 843 | memmove(ep, nep, dsize)__builtin_memmove((ep), (nep), (dsize)); |
| 844 | } |
| 845 | /* |
| 846 | * Here, `ep' points to a directory entry containing `dsize' in-use |
| 847 | * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0, |
| 848 | * then the entry is completely unused (dsize == 0). The value |
| 849 | * of ep->d_reclen is always indeterminate. |
| 850 | * |
| 851 | * Update the pointer fields in the previous entry (if any), |
| 852 | * copy in the new entry, and write out the block. |
| 853 | */ |
| 854 | if (ep->d_ino == 0) { |
| 855 | if (spacefree + dsize < newentrysize) |
| 856 | panic("ufs_direnter: compact1"); |
| 857 | dirp->d_reclen = spacefree + dsize; |
| 858 | } else { |
| 859 | if (spacefree < newentrysize) |
| 860 | panic("ufs_direnter: compact2"); |
| 861 | dirp->d_reclen = spacefree; |
| 862 | ep->d_reclen = dsize; |
| 863 | ep = (struct direct *)((char *)ep + dsize); |
| 864 | } |
| 865 | |
| 866 | #ifdef UFS_DIRHASH1 |
| 867 | if (dp->i_dirhashinode_ext.dirhash != NULL((void *)0) && (ep->d_ino == 0 || |
| 868 | dirp->d_reclen == spacefree)) |
| 869 | ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf)); |
| 870 | #endif |
| 871 | memcpy(ep, dirp, newentrysize)__builtin_memcpy((ep), (dirp), (newentrysize)); |
| 872 | #ifdef UFS_DIRHASH1 |
| 873 | if (dp->i_dirhashinode_ext.dirhash != NULL((void *)0)) |
| 874 | ufsdirhash_checkblock(dp, dirbuf - |
| 875 | (dp->i_offset & (DIRBLKSIZ(1 << 9) - 1)), |
| 876 | dp->i_offset & ~(DIRBLKSIZ(1 << 9) - 1)); |
| 877 | #endif |
| 878 | |
| 879 | if (DOINGSOFTDEP(dvp)((dvp)->v_mount->mnt_flag & 0x04000000)) { |
| 880 | (void)softdep_setup_directory_add(bp, dp, |
| 881 | dp->i_offset + (caddr_t)ep - dirbuf, |
| 882 | dirp->d_ino, newdirbp, 0); |
| 883 | bdwrite(bp); |
| 884 | } else { |
| 885 | error = VOP_BWRITE(bp); |
| 886 | } |
| 887 | dp->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
| 888 | |
| 889 | /* |
| 890 | * If all went well, and the directory can be shortened, proceed |
| 891 | * with the truncation. Note that we have to unlock the inode for |
| 892 | * the entry that we just entered, as the truncation may need to |
| 893 | * lock other inodes which can lead to deadlock if we also hold a |
| 894 | * lock on the newly entered node. |
| 895 | */ |
| 896 | |
| 897 | if (error == 0 && dp->i_endoff && dp->i_endoff < DIP(dp, size)(((dp)->i_ump->um_fstype == 1) ? (dp)->dinode_u.ffs1_din ->di_size : (dp)->dinode_u.ffs2_din->di_size)) { |
| 898 | if (tvp != NULL((void *)0)) |
| 899 | VOP_UNLOCK(tvp); |
| 900 | error = UFS_TRUNCATE(dp, (off_t)dp->i_endoff, IO_SYNC, cr)((dp)->i_vtbl->iv_truncate)((dp), ((off_t)dp->i_endoff ), (0x04), (cr)); |
| 901 | #ifdef UFS_DIRHASH1 |
| 902 | if (error == 0 && dp->i_dirhashinode_ext.dirhash != NULL((void *)0)) |
| 903 | ufsdirhash_dirtrunc(dp, dp->i_endoff); |
| 904 | #endif |
| 905 | if (tvp != NULL((void *)0)) |
| 906 | vn_lock(tvp, LK_EXCLUSIVE0x0001UL | LK_RETRY0x2000UL); |
| 907 | } |
| 908 | return (error); |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Remove a directory entry after a call to namei, using |
| 913 | * the parameters which it left in nameidata. The entry |
| 914 | * dp->i_offset contains the offset into the directory of the |
| 915 | * entry to be eliminated. The dp->i_count field contains the |
| 916 | * size of the previous record in the directory. If this |
| 917 | * is 0, the first entry is being deleted, so we need only |
| 918 | * zero the inode number to mark the entry as free. If the |
| 919 | * entry is not the first in the directory, we must reclaim |
| 920 | * the space of the now empty record by adding the record size |
| 921 | * to the size of the previous entry. |
| 922 | */ |
| 923 | int |
| 924 | ufs_dirremove(struct vnode *dvp, struct inode *ip, int flags, int isrmdir) |
| 925 | { |
| 926 | struct inode *dp; |
| 927 | struct direct *ep; |
| 928 | struct buf *bp; |
| 929 | int error; |
| 930 | |
| 931 | dp = VTOI(dvp)((struct inode *)(dvp)->v_data); |
| 932 | |
| 933 | if ((error = UFS_BUFATOFF(dp,((dp)->i_vtbl->iv_bufatoff)((dp), ((off_t)(dp->i_offset - dp->i_count)), ((char **)&ep), (&bp)) |
| 934 | (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)((dp)->i_vtbl->iv_bufatoff)((dp), ((off_t)(dp->i_offset - dp->i_count)), ((char **)&ep), (&bp))) != 0) |
| 935 | return (error); |
| 936 | #ifdef UFS_DIRHASH1 |
| 937 | /* |
| 938 | * Remove the dirhash entry. This is complicated by the fact |
| 939 | * that `ep' is the previous entry when dp->i_count != 0. |
| 940 | */ |
| 941 | if (dp->i_dirhashinode_ext.dirhash != NULL((void *)0)) |
| 942 | ufsdirhash_remove(dp, (dp->i_count == 0) ? ep : |
| 943 | (struct direct *)((char *)ep + ep->d_reclen), dp->i_offset); |
| 944 | #endif |
| 945 | |
| 946 | if (dp->i_count == 0) { |
| 947 | /* |
| 948 | * First entry in block: set d_ino to zero. |
| 949 | */ |
| 950 | ep->d_ino = 0; |
| 951 | } else { |
| 952 | /* |
| 953 | * Collapse new free space into previous entry. |
| 954 | */ |
| 955 | ep->d_reclen += dp->i_reclen; |
| 956 | } |
| 957 | #ifdef UFS_DIRHASH1 |
| 958 | if (dp->i_dirhashinode_ext.dirhash != NULL((void *)0)) |
| 959 | ufsdirhash_checkblock(dp, (char *)ep - |
| 960 | ((dp->i_offset - dp->i_count) & (DIRBLKSIZ(1 << 9) - 1)), |
| 961 | dp->i_offset & ~(DIRBLKSIZ(1 << 9) - 1)); |
| 962 | #endif |
| 963 | if (DOINGSOFTDEP(dvp)((dvp)->v_mount->mnt_flag & 0x04000000)) { |
| 964 | if (ip) { |
| 965 | ip->i_effnlink--; |
| 966 | softdep_change_linkcnt(ip, 0); |
| 967 | softdep_setup_remove(bp, dp, ip, isrmdir); |
| 968 | } |
| 969 | if (softdep_slowdown(dvp)) { |
| 970 | error = bwrite(bp); |
| 971 | } else { |
| 972 | bdwrite(bp); |
| 973 | error = 0; |
| 974 | } |
| 975 | } else { |
| 976 | if (ip) { |
| 977 | ip->i_effnlink--; |
| 978 | DIP_ADD(ip, nlink, -1)do { if ((ip)->i_ump->um_fstype == 1) (ip)->dinode_u .ffs1_din->di_nlink += (-1); else (ip)->dinode_u.ffs2_din ->di_nlink += (-1); } while (0); |
| 979 | ip->i_flag |= IN_CHANGE0x0002; |
| 980 | } |
| 981 | if (DOINGASYNC(dvp)((dvp)->v_mount->mnt_flag & 0x00000040) && dp->i_count != 0) { |
| 982 | bdwrite(bp); |
| 983 | error = 0; |
| 984 | } else |
| 985 | error = bwrite(bp); |
| 986 | } |
| 987 | dp->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
| 988 | return (error); |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Rewrite an existing directory entry to point at the inode |
| 993 | * supplied. The parameters describing the directory entry are |
| 994 | * set up by a call to namei. |
| 995 | */ |
| 996 | int |
| 997 | ufs_dirrewrite(struct inode *dp, struct inode *oip, ufsino_t newinum, |
| 998 | int newtype, int isrmdir) |
| 999 | { |
| 1000 | struct buf *bp; |
| 1001 | struct direct *ep; |
| 1002 | struct vnode *vdp = ITOV(dp)((dp)->i_vnode); |
| 1003 | int error; |
| 1004 | |
| 1005 | error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, (char **)&ep, &bp)((dp)->i_vtbl->iv_bufatoff)((dp), ((off_t)dp->i_offset ), ((char **)&ep), (&bp)); |
| 1006 | if (error) |
| 1007 | return (error); |
| 1008 | ep->d_ino = newinum; |
| 1009 | ep->d_type = newtype; |
| 1010 | oip->i_effnlink--; |
| 1011 | if (DOINGSOFTDEP(vdp)((vdp)->v_mount->mnt_flag & 0x04000000)) { |
| 1012 | softdep_change_linkcnt(oip, 0); |
| 1013 | softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir); |
| 1014 | bdwrite(bp); |
| 1015 | } else { |
| 1016 | DIP_ADD(oip, nlink, -1)do { if ((oip)->i_ump->um_fstype == 1) (oip)->dinode_u .ffs1_din->di_nlink += (-1); else (oip)->dinode_u.ffs2_din ->di_nlink += (-1); } while (0); |
| 1017 | oip->i_flag |= IN_CHANGE0x0002; |
| 1018 | if (DOINGASYNC(vdp)((vdp)->v_mount->mnt_flag & 0x00000040)) { |
| 1019 | bdwrite(bp); |
| 1020 | error = 0; |
| 1021 | } else { |
| 1022 | error = VOP_BWRITE(bp); |
| 1023 | } |
| 1024 | } |
| 1025 | dp->i_flag |= IN_CHANGE0x0002 | IN_UPDATE0x0004; |
| 1026 | return (error); |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Check if a directory is empty or not. |
| 1031 | * Inode supplied must be locked. |
| 1032 | * |
| 1033 | * Using a struct dirtemplate here is not precisely |
| 1034 | * what we want, but better than using a struct direct. |
| 1035 | * |
| 1036 | * NB: does not handle corrupted directories. |
| 1037 | */ |
| 1038 | int |
| 1039 | ufs_dirempty(struct inode *ip, ufsino_t parentino, struct ucred *cred) |
| 1040 | { |
| 1041 | off_t off, m; |
| 1042 | struct dirtemplate dbuf; |
| 1043 | struct direct *dp = (struct direct *)&dbuf; |
| 1044 | int error, namlen; |
| 1045 | size_t count; |
| 1046 | #define MINDIRSIZ(sizeof (struct dirtemplate) / 2) (sizeof (struct dirtemplate) / 2) |
| 1047 | |
| 1048 | m = DIP(ip, size)(((ip)->i_ump->um_fstype == 1) ? (ip)->dinode_u.ffs1_din ->di_size : (ip)->dinode_u.ffs2_din->di_size); |
| 1049 | for (off = 0; off < m; off += dp->d_reclen) { |
| 1050 | error = vn_rdwr(UIO_READ, ITOV(ip)((ip)->i_vnode), (caddr_t)dp, MINDIRSIZ(sizeof (struct dirtemplate) / 2), off, |
| 1051 | UIO_SYSSPACE, IO_NODELOCKED0x08, cred, &count, curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r" (__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self))); __ci;})->ci_curproc); |
| 1052 | /* |
| 1053 | * Since we read MINDIRSIZ, residual must |
| 1054 | * be 0 unless we're at end of file. |
| 1055 | */ |
| 1056 | if (error || count != 0) |
| 1057 | return (0); |
| 1058 | /* avoid infinite loops */ |
| 1059 | if (dp->d_reclen == 0) |
| 1060 | return (0); |
| 1061 | /* skip empty entries */ |
| 1062 | if (dp->d_ino == 0) |
| 1063 | continue; |
| 1064 | /* accept only "." and ".." */ |
| 1065 | namlen = dp->d_namlen; |
| 1066 | if (namlen > 2) |
| 1067 | return (0); |
| 1068 | if (dp->d_name[0] != '.') |
| 1069 | return (0); |
| 1070 | /* |
| 1071 | * At this point namlen must be 1 or 2. |
| 1072 | * 1 implies ".", 2 implies ".." if second |
| 1073 | * char is also "." |
| 1074 | */ |
| 1075 | if (namlen == 1 && dp->d_ino == ip->i_number) |
| 1076 | continue; |
| 1077 | if (dp->d_name[1] == '.' && dp->d_ino == parentino) |
| 1078 | continue; |
| 1079 | return (0); |
| 1080 | } |
| 1081 | return (1); |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * Check if source directory is in the path of the target directory. |
| 1086 | * Target is supplied locked, source is unlocked. |
| 1087 | * The target is always vput before returning. |
| 1088 | */ |
| 1089 | int |
| 1090 | ufs_checkpath(struct inode *source, struct inode *target, struct ucred *cred) |
| 1091 | { |
| 1092 | struct vnode *nextvp, *vp; |
| 1093 | int error, rootino, namlen; |
| 1094 | struct dirtemplate dirbuf; |
| 1095 | |
| 1096 | vp = ITOV(target)((target)->i_vnode); |
| 1097 | if (target->i_number == source->i_number) { |
| 1098 | error = EEXIST17; |
| 1099 | goto out; |
| 1100 | } |
| 1101 | rootino = ROOTINO((ufsino_t)2); |
| 1102 | error = 0; |
| 1103 | if (target->i_number == rootino) |
| 1104 | goto out; |
| 1105 | |
| 1106 | for (;;) { |
| 1107 | if (vp->v_type != VDIR) { |
| 1108 | error = ENOTDIR20; |
| 1109 | break; |
| 1110 | } |
| 1111 | error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf, |
| 1112 | sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE, |
| 1113 | IO_NODELOCKED0x08, cred, NULL((void *)0), curproc({struct cpu_info *__ci; asm volatile("movq %%gs:%P1,%0" : "=r" (__ci) :"n" (__builtin_offsetof(struct cpu_info, ci_self))); __ci;})->ci_curproc); |
| 1114 | if (error != 0) |
| 1115 | break; |
| 1116 | namlen = dirbuf.dotdot_namlen; |
| 1117 | if (namlen != 2 || |
| 1118 | dirbuf.dotdot_name[0] != '.' || |
| 1119 | dirbuf.dotdot_name[1] != '.') { |
| 1120 | error = ENOTDIR20; |
| 1121 | break; |
| 1122 | } |
| 1123 | if (dirbuf.dotdot_ino == source->i_number) { |
| 1124 | error = EINVAL22; |
| 1125 | break; |
| 1126 | } |
| 1127 | if (dirbuf.dotdot_ino == rootino) |
| 1128 | break; |
| 1129 | VOP_UNLOCK(vp); |
| 1130 | error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &nextvp)(*(vp->v_mount)->mnt_op->vfs_vget)(vp->v_mount, dirbuf .dotdot_ino, &nextvp); |
| 1131 | vrele(vp); |
| 1132 | if (error) { |
| 1133 | vp = NULL((void *)0); |
| 1134 | break; |
| 1135 | } |
| 1136 | vp = nextvp; |
| 1137 | } |
| 1138 | |
| 1139 | out: |
| 1140 | if (error == ENOTDIR20) |
| 1141 | printf("checkpath: .. not a directory\n"); |
| 1142 | if (vp != NULL((void *)0)) |
| 1143 | vput(vp); |
| 1144 | return (error); |
| 1145 | } |