1*16657Smckusick /* vfs_lookup.c 6.8 84/07/02 */ 230Sbill 330Sbill #include "../h/param.h" 430Sbill #include "../h/systm.h" 530Sbill #include "../h/inode.h" 66571Smckusic #include "../h/fs.h" 730Sbill #include "../h/mount.h" 830Sbill #include "../h/dir.h" 930Sbill #include "../h/user.h" 1030Sbill #include "../h/buf.h" 112275Swnj #include "../h/conf.h" 127825Sroot #include "../h/uio.h" 139166Ssam #include "../h/nami.h" 1415660Smckusick #include "../h/kernel.h" 1530Sbill 167605Ssam struct buf *blkatoff(); 179166Ssam int dirchk = 0; 1815798Smckusick 1930Sbill /* 2015798Smckusick * Structures associated with name cacheing. 2115798Smckusick */ 2215798Smckusick #define NCHHASH 32 /* size of hash table */ 2315798Smckusick 2415798Smckusick #if ((NCHHASH)&((NCHHASH)-1)) != 0 2515798Smckusick #define NHASH(h, i, d) ((unsigned)((h) + (i) + 13 * (int)(d)) % (NCHHASH)) 2615798Smckusick #else 2715798Smckusick #define NHASH(h, i, d) ((unsigned)((h) + (i) + 13 * (int)(d)) & ((NCHHASH)-1)) 2815798Smckusick #endif 2915798Smckusick 3015798Smckusick union nchash { 3115798Smckusick union nchash *nch_head[2]; 3215798Smckusick struct nch *nch_chain[2]; 3315798Smckusick } nchash[NCHHASH]; 3415798Smckusick #define nch_forw nch_chain[0] 3515798Smckusick #define nch_back nch_chain[1] 3615798Smckusick 3715809Smckusick struct nch *nchhead, **nchtail; /* LRU chain pointers */ 3815809Smckusick struct nchstats nchstats; /* cache effectiveness statistics */ 3915798Smckusick 4015798Smckusick /* 417534Sroot * Convert a pathname into a pointer to a locked inode, 427534Sroot * with side effects usable in creating and removing files. 437534Sroot * This is a very central and rather complicated routine. 4430Sbill * 457534Sroot * The func argument gives the routine which returns successive 469166Ssam * characters of the name to be translated. 477534Sroot * 489166Ssam * The flag argument is (LOOKUP, CREATE, DELETE) depending on whether 499166Ssam * the name is to be (looked up, created, deleted). If flag has 509166Ssam * LOCKPARENT or'ed into it and the target of the pathname exists, 519166Ssam * namei returns both the target and its parent directory locked. 529166Ssam * If the file system is not maintained in a strict tree hierarchy, 539166Ssam * this can result in a deadlock situation. When creating and 549166Ssam * LOCKPARENT is specified, the target may not be ".". When deleting 559166Ssam * and LOCKPARENT is specified, the target may be ".", but the caller 569166Ssam * must check to insure it does an irele and iput instead of two iputs. 579166Ssam * 589166Ssam * The follow argument is 1 when symbolic links are to be followed 599166Ssam * when they occur at the end of the name translation process. 609166Ssam * 6115798Smckusick * Name caching works as follows: 627534Sroot * 6315798Smckusick * names found by directory scans are retained in a cache 6415798Smckusick * for future reference. It is managed LRU, so frequently 6515798Smckusick * used names will hang around. Cache is indexed by hash value 6615798Smckusick * obtained from (ino,dev,name) where ino & dev refer to the 6715798Smckusick * directory containing name. 6815798Smckusick * 6915798Smckusick * For simplicity (and economy of storage), names longer than 7015798Smckusick * some (small) maximum length are not cached, they occur 7115798Smckusick * infrequently in any case, and are almost never of interest. 7215798Smckusick * 7315798Smckusick * Upon reaching the last segment of a path, if the reference 7415798Smckusick * is for DELETE, or NOCACHE is set (rewrite), and the 7515798Smckusick * name is located in the cache, it will be dropped. 7615798Smckusick * 7715798Smckusick * We must be sure never to enter the name ".." into the cache 7815798Smckusick * because of the extremely kludgey way that rename() alters 7915798Smckusick * ".." in a situation like 8015798Smckusick * mv a/x b/x 8115798Smckusick * where x is a directory, and x/.. is the ".." in question. 8215798Smckusick * 8315798Smckusick * Overall outline of namei: 8415798Smckusick * 857534Sroot * copy in name 867534Sroot * get starting directory 877534Sroot * dirloop: 887534Sroot * check accessibility of directory 897534Sroot * dirloop2: 907534Sroot * copy next component of name to u.u_dent 917534Sroot * handle degenerate case where name is null string 9215798Smckusick * look for name in cache, if found, then if at end of path 9315798Smckusick * and deleting or creating, drop it, else to haveino 947534Sroot * search for name in directory, to found or notfound 957534Sroot * notfound: 969166Ssam * if creating, return locked directory, leaving info on avail. slots 977534Sroot * else return error 987534Sroot * found: 997534Sroot * if at end of path and deleting, return information to allow delete 10015798Smckusick * if at end of path and rewriting (create and LOCKPARENT), lock target 1019166Ssam * inode and return info to allow rewrite 1027534Sroot * if .. and on mounted filesys, look in mount table for parent 10315798Smckusick * if not at end, if neither creating nor deleting, add name to cache 10415798Smckusick * haveino: 1057534Sroot * if symbolic link, massage name in buffer and continue at dirloop 1067534Sroot * if more components of name, do next level at dirloop 1077534Sroot * return the answer as locked inode 1089166Ssam * 1099166Ssam * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode, 1109166Ssam * but unlocked. 11130Sbill */ 11230Sbill struct inode * 1135972Swnj namei(func, flag, follow) 1145972Swnj int (*func)(), flag, follow; 11530Sbill { 1167534Sroot register char *cp; /* pointer into pathname argument */ 1177534Sroot /* these variables refer to things which must be freed or unlocked */ 1187534Sroot register struct inode *dp = 0; /* the directory we are searching */ 11915798Smckusick register struct nch *ncp; /* cache slot for entry */ 1207534Sroot register struct fs *fs; /* file system that directory is in */ 1217534Sroot register struct buf *bp = 0; /* a buffer of directory entries */ 1227534Sroot register struct direct *ep; /* the current directory entry */ 1237534Sroot int entryoffsetinblock; /* offset of ep in bp's buffer */ 1247534Sroot register struct buf *nbp; /* buffer storing path name argument */ 1257534Sroot /* these variables hold information about the search for a slot */ 1267534Sroot enum {NONE, COMPACT, FOUND} slotstatus; 1277534Sroot int slotoffset = -1; /* offset of area with free space */ 1287534Sroot int slotsize; /* size of area at slotoffset */ 1297534Sroot int slotfreespace; /* amount of space free in slot */ 1307534Sroot int slotneeded; /* size of the entry we're seeking */ 1317534Sroot /* */ 13215660Smckusick int numdirpasses; /* strategy for directory search */ 13315660Smckusick int endsearch; /* offset to end directory search */ 1347534Sroot int prevoff; /* u.u_offset of previous entry */ 1357534Sroot int nlink = 0; /* number of symbolic links taken */ 1367534Sroot struct inode *pdp; /* saved dp during symlink work */ 1377534Sroot int i; 1389166Ssam int lockparent; 13915798Smckusick int docache; 14015798Smckusick unsigned hash; /* value of name hash for entry */ 14115798Smckusick union nchash *nhp; /* cache chain head for entry */ 14215798Smckusick int isdotdot; /* != 0 if current name is ".." */ 14330Sbill 1449166Ssam lockparent = flag & LOCKPARENT; 14515798Smckusick docache = (flag & NOCACHE) ^ NOCACHE; 14615798Smckusick flag &= ~(LOCKPARENT|NOCACHE); 14715798Smckusick if (flag == DELETE) 14815798Smckusick docache = 0; 14930Sbill /* 1507534Sroot * Get a buffer for the name to be translated, and copy the 1517534Sroot * name into the buffer. 1525972Swnj */ 1536571Smckusic nbp = geteblk(MAXPATHLEN); 1547534Sroot for (cp = nbp->b_un.b_addr; *cp = (*func)(); ) { 15516643Ssam if ((*cp&0377) == ('/'|0200) || (*cp&0200) && flag != DELETE) { 1566066Sroot u.u_error = EPERM; 1577534Sroot goto bad; 1586066Sroot } 1596066Sroot cp++; 1606571Smckusic if (cp >= nbp->b_un.b_addr + MAXPATHLEN) { 1615972Swnj u.u_error = ENOENT; 1627534Sroot goto bad; 1635972Swnj } 1645972Swnj } 1657534Sroot if (u.u_error) 1667534Sroot goto bad; 1677534Sroot 1685972Swnj /* 1697534Sroot * Get starting directory. 17030Sbill */ 1717534Sroot cp = nbp->b_un.b_addr; 1725972Swnj if (*cp == '/') { 1735972Swnj while (*cp == '/') 1745972Swnj cp++; 17530Sbill if ((dp = u.u_rdir) == NULL) 17630Sbill dp = rootdir; 1777534Sroot } else 1787534Sroot dp = u.u_cdir; 1797534Sroot fs = dp->i_fs; 1805972Swnj ilock(dp); 1815972Swnj dp->i_count++; 1827534Sroot u.u_pdir = (struct inode *)0xc0000000; /* illegal */ 1837534Sroot 1847534Sroot /* 1857534Sroot * We come to dirloop to search a new directory. 1867534Sroot * The directory must be locked so that it can be 1877534Sroot * iput, and fs must be already set to dp->i_fs. 1887534Sroot */ 1896571Smckusic dirloop: 19030Sbill /* 1917534Sroot * Check accessiblity of directory. 19230Sbill */ 1937534Sroot if ((dp->i_mode&IFMT) != IFDIR) { 19430Sbill u.u_error = ENOTDIR; 1957534Sroot goto bad; 1967534Sroot } 1977534Sroot if (access(dp, IEXEC)) 1987534Sroot goto bad; 1997534Sroot 2006384Swnj dirloop2: 2017534Sroot /* 2027534Sroot * Copy next component of name to u.u_dent. 2037534Sroot */ 20415798Smckusick hash = 0; 2057534Sroot for (i = 0; *cp != 0 && *cp != '/'; cp++) { 2066571Smckusic if (i >= MAXNAMLEN) { 2075972Swnj u.u_error = ENOENT; 2087534Sroot goto bad; 2095972Swnj } 2107534Sroot u.u_dent.d_name[i++] = *cp; 21115798Smckusick hash += (unsigned char)*cp * i; 2125972Swnj } 2136571Smckusic u.u_dent.d_namlen = i; 2147534Sroot u.u_dent.d_name[i] = 0; 2157534Sroot 2167534Sroot /* 2177534Sroot * Check for degenerate name (e.g. / or "") 2187534Sroot * which is a way of talking about a directory, 2197534Sroot * e.g. like "/." or ".". 2207534Sroot */ 2217534Sroot if (u.u_dent.d_name[0] == 0) { 22215798Smckusick if (flag != LOOKUP || lockparent) { 22314937Smckusick u.u_error = EISDIR; 2247534Sroot goto bad; 2255972Swnj } 2266571Smckusic brelse(nbp); 2276571Smckusic return (dp); 2285972Swnj } 2297534Sroot 2306571Smckusic /* 23115798Smckusick * We now have a segment name to search for, and a directory to search. 23215798Smckusick * 23315798Smckusick * Before tediously performing a linear scan of the directory, 23415798Smckusick * check the name cache to see if the directory/name pair 23515798Smckusick * we are looking for is known already. We don't do this 23615798Smckusick * if the segment name is long, simply so the cache can avoid 23715798Smckusick * holding long names (which would either waste space, or 23815798Smckusick * add greatly to the complexity). 23915798Smckusick */ 24015798Smckusick if (u.u_dent.d_namlen > NCHNAMLEN) { 24115798Smckusick nchstats.ncs_long++; 24215798Smckusick docache = 0; 24315798Smckusick } else { 24415798Smckusick nhp = &nchash[NHASH(hash, dp->i_number, dp->i_dev)]; 24515798Smckusick for (ncp = nhp->nch_forw; ncp != (struct nch *)nhp; 24615798Smckusick ncp = ncp->nc_forw) { 24715798Smckusick if (ncp->nc_ino == dp->i_number && 24815798Smckusick ncp->nc_dev == dp->i_dev && 24915798Smckusick ncp->nc_nlen == u.u_dent.d_namlen && 25015798Smckusick !bcmp(ncp->nc_name, u.u_dent.d_name, ncp->nc_nlen)) 25115798Smckusick break; 25215798Smckusick } 25315798Smckusick 25415798Smckusick if (ncp == (struct nch *)nhp) { 25515798Smckusick nchstats.ncs_miss++; 25615798Smckusick ncp = NULL; 25715798Smckusick } else { 25816643Ssam if (ncp->nc_id != ncp->nc_ip->i_id) 25916643Ssam nchstats.ncs_falsehits++; 26016643Ssam else if (*cp == '/' || docache) { 26115798Smckusick 26215798Smckusick nchstats.ncs_goodhits++; 26315798Smckusick 26415798Smckusick /* 26515798Smckusick * move this slot to end of LRU 26615798Smckusick * chain, if not already there 26715798Smckusick */ 26815798Smckusick if (ncp->nc_nxt) { 26915798Smckusick /* remove from LRU chain */ 27015798Smckusick *ncp->nc_prev = ncp->nc_nxt; 27115798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 27215798Smckusick 27315798Smckusick /* and replace at end of it */ 27415798Smckusick ncp->nc_nxt = NULL; 27515798Smckusick ncp->nc_prev = nchtail; 27615798Smckusick *nchtail = ncp; 27715798Smckusick nchtail = &ncp->nc_nxt; 27815798Smckusick } 27915798Smckusick 28015798Smckusick pdp = dp; 28115798Smckusick dp = ncp->nc_ip; 28215798Smckusick if (dp == NULL) 28315798Smckusick panic("nami: null cache ino"); 28416643Ssam if (pdp == dp) 28516643Ssam dp->i_count++; 28616643Ssam else if (dp->i_count) { 28716643Ssam dp->i_count++; 28815798Smckusick ilock(dp); 28915798Smckusick iunlock(pdp); 29016643Ssam } else { 29116643Ssam igrab(dp); 29216643Ssam iunlock(pdp); 29316643Ssam } 29415798Smckusick 29515798Smckusick u.u_dent.d_ino = dp->i_number; 29615798Smckusick /* u_dent.d_reclen is garbage ... */ 29715798Smckusick 29815798Smckusick goto haveino; 29916643Ssam } else 30016643Ssam nchstats.ncs_badhits++; 30115798Smckusick 30215798Smckusick /* 30316643Ssam * Last component and we are renaming or deleting, 30416643Ssam * the cache entry is invalid, or otherwise don't 30516643Ssam * want cache entry to exist. 30615798Smckusick */ 30715798Smckusick 30815798Smckusick /* remove from LRU chain */ 30915798Smckusick *ncp->nc_prev = ncp->nc_nxt; 31015798Smckusick if (ncp->nc_nxt) 31115798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 31215798Smckusick else 31315798Smckusick nchtail = ncp->nc_prev; 31415798Smckusick 31515798Smckusick /* remove from hash chain */ 31615798Smckusick remque(ncp); 31715798Smckusick 31815798Smckusick /* insert at head of LRU list (first to grab) */ 31915798Smckusick ncp->nc_nxt = nchhead; 32015798Smckusick ncp->nc_prev = &nchhead; 32115798Smckusick nchhead->nc_prev = &ncp->nc_nxt; 32215798Smckusick nchhead = ncp; 32315798Smckusick 32415798Smckusick /* and make a dummy hash chain */ 32515798Smckusick ncp->nc_forw = ncp; 32615798Smckusick ncp->nc_back = ncp; 32715798Smckusick 32815798Smckusick ncp = NULL; 32915798Smckusick } 33015798Smckusick } 33115798Smckusick 33215798Smckusick /* 3337534Sroot * Suppress search for slots unless creating 3347534Sroot * file and at end of pathname, in which case 3357534Sroot * we watch for a place to put the new file in 3367534Sroot * case it doesn't already exist. 3376571Smckusic */ 3387534Sroot slotstatus = FOUND; 3399166Ssam if (flag == CREATE && *cp == 0) { 3407534Sroot slotstatus = NONE; 3417534Sroot slotfreespace = 0; 3427534Sroot slotneeded = DIRSIZ(&u.u_dent); 3437534Sroot } 34415660Smckusick /* 34515660Smckusick * If this is the same directory that this process 34615660Smckusick * previously searched, pick up where we last left off. 34715798Smckusick * We cache only lookups as these are the most common 34815660Smckusick * and have the greatest payoff. Caching CREATE has little 34915660Smckusick * benefit as it usually must search the entire directory 35015660Smckusick * to determine that the entry does not exist. Caching the 35115660Smckusick * location of the last DELETE has not reduced profiling time 35215660Smckusick * and hence has been removed in the interest of simplicity. 35315660Smckusick */ 35415660Smckusick if (flag != LOOKUP || dp->i_number != u.u_ncache.nc_inumber || 35515660Smckusick dp->i_dev != u.u_ncache.nc_dev) { 35615660Smckusick u.u_offset = 0; 35715660Smckusick numdirpasses = 1; 35815660Smckusick } else { 35915798Smckusick if ((dp->i_flag & ICHG) || dp->i_ctime >= u.u_ncache.nc_time) { 36015660Smckusick u.u_ncache.nc_prevoffset &= ~(DIRBLKSIZ - 1); 36115660Smckusick u.u_ncache.nc_time = time.tv_sec; 36215660Smckusick } 36315660Smckusick u.u_offset = u.u_ncache.nc_prevoffset; 36415660Smckusick entryoffsetinblock = blkoff(fs, u.u_offset); 36515660Smckusick if (entryoffsetinblock != 0) { 36615660Smckusick bp = blkatoff(dp, u.u_offset, (char **)0); 36715660Smckusick if (bp == 0) 36815660Smckusick goto bad; 36915660Smckusick } 37015660Smckusick numdirpasses = 2; 37115798Smckusick nchstats.ncs_2passes++; 37215660Smckusick } 37315660Smckusick endsearch = roundup(dp->i_size, DIRBLKSIZ); 3747534Sroot 37515660Smckusick searchloop: 37615660Smckusick while (u.u_offset < endsearch) { 3775972Swnj /* 3785972Swnj * If offset is on a block boundary, 3795972Swnj * read the next directory block. 3805972Swnj * Release previous if it exists. 3815972Swnj */ 3826571Smckusic if (blkoff(fs, u.u_offset) == 0) { 3835972Swnj if (bp != NULL) 3845972Swnj brelse(bp); 3857605Ssam bp = blkatoff(dp, u.u_offset, (char **)0); 3867534Sroot if (bp == 0) 3877534Sroot goto bad; 3887534Sroot entryoffsetinblock = 0; 3895972Swnj } 3907534Sroot 3915972Swnj /* 3927534Sroot * If still looking for a slot, and at a DIRBLKSIZE 393*16657Smckusick * boundary, have to start looking for free space again. 3946571Smckusic */ 3957534Sroot if (slotstatus == NONE && 3967534Sroot (entryoffsetinblock&(DIRBLKSIZ-1)) == 0) { 3977534Sroot slotoffset = -1; 3987534Sroot slotfreespace = 0; 3997534Sroot } 4007534Sroot 4017534Sroot /* 402*16657Smckusick * Get pointer to next entry. 403*16657Smckusick * Full validation checks are slow, so we only check 404*16657Smckusick * enough to insure forward progress through the 405*16657Smckusick * directory. Complete checks can be run by patching 406*16657Smckusick * "dirchk" to be true. 4077534Sroot */ 4087534Sroot ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock); 409*16657Smckusick if (ep->d_reclen <= 0 || 410*16657Smckusick dirchk && dirbadentry(ep, entryoffsetinblock)) { 4117534Sroot dirbad(dp, "mangled entry"); 412*16657Smckusick i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)); 4136571Smckusic u.u_offset += i; 4147534Sroot entryoffsetinblock += i; 4156571Smckusic continue; 4166571Smckusic } 4177534Sroot 4186571Smckusic /* 4197534Sroot * If an appropriate sized slot has not yet been found, 4206571Smckusic * check to see if one is available. Also accumulate space 4216571Smckusic * in the current block so that we can determine if 4226571Smckusic * compaction is viable. 4236571Smckusic */ 4247534Sroot if (slotstatus != FOUND) { 4257534Sroot int size = ep->d_reclen; 4267534Sroot 4276571Smckusic if (ep->d_ino != 0) 4286571Smckusic size -= DIRSIZ(ep); 4296571Smckusic if (size > 0) { 4307534Sroot if (size >= slotneeded) { 4317534Sroot slotstatus = FOUND; 4327534Sroot slotoffset = u.u_offset; 4337534Sroot slotsize = ep->d_reclen; 4347534Sroot } else if (slotstatus == NONE) { 4357534Sroot slotfreespace += size; 4367534Sroot if (slotoffset == -1) 4377534Sroot slotoffset = u.u_offset; 4387534Sroot if (slotfreespace >= slotneeded) { 4397534Sroot slotstatus = COMPACT; 4407534Sroot slotsize = 4417534Sroot u.u_offset+ep->d_reclen - 4427534Sroot slotoffset; 4437534Sroot } 4446571Smckusic } 4456571Smckusic } 4466571Smckusic } 4477534Sroot 4486571Smckusic /* 4497534Sroot * Check for a name match. 4505972Swnj */ 4517534Sroot if (ep->d_ino) { 4527534Sroot if (ep->d_namlen == u.u_dent.d_namlen && 4537534Sroot !bcmp(u.u_dent.d_name, ep->d_name, ep->d_namlen)) 4547534Sroot goto found; 4557534Sroot } 4567534Sroot prevoff = u.u_offset; 4576571Smckusic u.u_offset += ep->d_reclen; 4587534Sroot entryoffsetinblock += ep->d_reclen; 4597534Sroot } 46015798Smckusick /* notfound: */ 46115660Smckusick /* 46215798Smckusick * If we started in the middle of the directory and failed 46315660Smckusick * to find our target, we must check the beginning as well. 46415660Smckusick */ 46515660Smckusick if (numdirpasses == 2) { 46615660Smckusick numdirpasses--; 46715660Smckusick u.u_offset = 0; 46815660Smckusick endsearch = u.u_ncache.nc_prevoffset; 46915660Smckusick goto searchloop; 47015660Smckusick } 4717534Sroot /* 4727534Sroot * If creating, and at end of pathname and current 4739166Ssam * directory has not been removed, then can consider 4749166Ssam * allowing file to be created. 4757534Sroot */ 4769166Ssam if (flag == CREATE && *cp == 0 && dp->i_nlink != 0) { 4775972Swnj /* 4787534Sroot * Access for write is interpreted as allowing 4797534Sroot * creation of files in the directory. 4805972Swnj */ 4817534Sroot if (access(dp, IWRITE)) 4827534Sroot goto bad; 4835972Swnj /* 4847534Sroot * Return an indication of where the new directory 4857534Sroot * entry should be put. If we didn't find a slot, 4867534Sroot * then set u.u_count to 0 indicating that the 4877534Sroot * new slot belongs at the end of the directory. 4887534Sroot * If we found a slot, then the new entry can be 4897534Sroot * put in the range [u.u_offset..u.u_offset+u.u_count) 4905972Swnj */ 49115660Smckusick if (slotstatus == NONE) { 49215660Smckusick u.u_offset = roundup(dp->i_size, DIRBLKSIZ); 4937534Sroot u.u_count = 0; 49415660Smckusick } else { 4957534Sroot u.u_offset = slotoffset; 4967534Sroot u.u_count = slotsize; 4975972Swnj } 4987534Sroot dp->i_flag |= IUPD|ICHG; 4997534Sroot if (bp) 5007534Sroot brelse(bp); 5017534Sroot brelse(nbp); 5025972Swnj /* 5037534Sroot * We return with the directory locked, so that 5047534Sroot * the parameters we set up above will still be 5057534Sroot * valid if we actually decide to do a direnter(). 5067534Sroot * We return NULL to indicate that the entry doesn't 5077534Sroot * currently exist, leaving a pointer to the (locked) 5087534Sroot * directory inode in u.u_pdir. 5095972Swnj */ 5107534Sroot u.u_pdir = dp; 5117534Sroot return (NULL); 5127534Sroot } 5137534Sroot u.u_error = ENOENT; 5147534Sroot goto bad; 5157534Sroot found: 51615798Smckusick if (numdirpasses == 2) 51715798Smckusick nchstats.ncs_pass2++; 5187534Sroot /* 5197534Sroot * Check that directory length properly reflects presence 5207534Sroot * of this entry. 5217534Sroot */ 5227605Ssam if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) { 5237534Sroot dirbad(dp, "i_size too small"); 5247605Ssam dp->i_size = entryoffsetinblock + DIRSIZ(ep); 5257534Sroot dp->i_flag |= IUPD|ICHG; 5267534Sroot } 5277534Sroot 5287534Sroot /* 52915660Smckusick * Found component in pathname. 53015798Smckusick * If the final component of path name, save information 53115660Smckusick * in the cache as to where the entry was found. 5327534Sroot */ 53315660Smckusick if (*cp == '\0' && flag == LOOKUP) { 53415660Smckusick u.u_ncache.nc_prevoffset = u.u_offset; 53515660Smckusick u.u_ncache.nc_inumber = dp->i_number; 53615660Smckusick u.u_ncache.nc_dev = dp->i_dev; 53715660Smckusick u.u_ncache.nc_time = time.tv_sec; 53815660Smckusick } 53915660Smckusick /* 54015660Smckusick * Save directory entry in u.u_dent, 54115660Smckusick * and release directory buffer. 54215660Smckusick */ 5437825Sroot bcopy((caddr_t)ep, (caddr_t)&u.u_dent, (u_int)DIRSIZ(ep)); 5447534Sroot brelse(bp); 5457534Sroot bp = NULL; 5467534Sroot 5477534Sroot /* 5487534Sroot * If deleting, and at end of pathname, return 5497534Sroot * parameters which can be used to remove file. 5509166Ssam * If the lockparent flag isn't set, we return only 5519166Ssam * the directory (in u.u_pdir), otherwise we go 5529166Ssam * on and lock the inode, being careful with ".". 5537534Sroot */ 5549166Ssam if (flag == DELETE && *cp == 0) { 5557534Sroot /* 5567534Sroot * Write access to directory required to delete files. 5577534Sroot */ 5587534Sroot if (access(dp, IWRITE)) 5597534Sroot goto bad; 5609166Ssam u.u_pdir = dp; /* for dirremove() */ 5617534Sroot /* 5627534Sroot * Return pointer to current entry in u.u_offset, 5637534Sroot * and distance past previous entry (if there 5647534Sroot * is a previous entry in this block) in u.u_count. 5657534Sroot * Save directory inode pointer in u.u_pdir for dirremove(). 5667534Sroot */ 5677534Sroot if ((u.u_offset&(DIRBLKSIZ-1)) == 0) 5687534Sroot u.u_count = 0; 5697534Sroot else 5707534Sroot u.u_count = u.u_offset - prevoff; 5719166Ssam if (lockparent) { 5729166Ssam if (dp->i_number == u.u_dent.d_ino) 5739166Ssam dp->i_count++; 5749166Ssam else { 5759166Ssam dp = iget(dp->i_dev, fs, u.u_dent.d_ino); 5769166Ssam if (dp == NULL) { 5779166Ssam iput(u.u_pdir); 5789166Ssam goto bad; 5799166Ssam } 58015798Smckusick /* 58116046Skarels * If directory is "sticky", then user must own 58215798Smckusick * the directory, or the file in it, else he 58315798Smckusick * may not delete it (unless he's root). This 58415798Smckusick * implements append-only directories. 58515798Smckusick */ 58616046Skarels if ((u.u_pdir->i_mode & ISVTX) && 58715798Smckusick u.u_uid != 0 && 58815798Smckusick u.u_uid != u.u_pdir->i_uid && 58915798Smckusick dp->i_uid != u.u_uid) { 59015798Smckusick iput(u.u_pdir); 59115798Smckusick u.u_error = EPERM; 59215798Smckusick goto bad; 59315798Smckusick } 5949166Ssam } 5959166Ssam } 5967534Sroot brelse(nbp); 5977534Sroot return (dp); 5987534Sroot } 5997534Sroot 6007534Sroot /* 6017534Sroot * Special handling for ".." allowing chdir out of mounted 6027534Sroot * file system: indirect .. in root inode to reevaluate 6037534Sroot * in directory file system was mounted on. 6047534Sroot */ 60515798Smckusick isdotdot = 0; 60615798Smckusick if (bcmp(u.u_dent.d_name, "..", 3) == 0) { 60715798Smckusick isdotdot++; 6087534Sroot if (dp == u.u_rdir) 6097534Sroot u.u_dent.d_ino = dp->i_number; 6107534Sroot else if (u.u_dent.d_ino == ROOTINO && 6117534Sroot dp->i_number == ROOTINO) { 6127534Sroot for (i = 1; i < NMOUNT; i++) 6137534Sroot if (mount[i].m_bufp != NULL && 6147534Sroot mount[i].m_dev == dp->i_dev) { 6156571Smckusic iput(dp); 6167534Sroot dp = mount[i].m_inodp; 6175972Swnj ilock(dp); 6185972Swnj dp->i_count++; 6197534Sroot fs = dp->i_fs; 6207534Sroot cp -= 2; /* back over .. */ 6217534Sroot goto dirloop2; 6225972Swnj } 62330Sbill } 6247534Sroot } 6257534Sroot 6267534Sroot /* 6279166Ssam * If rewriting (rename), return the inode and the 6289166Ssam * information required to rewrite the present directory 6299166Ssam * Must get inode of directory entry to verify it's a 6309166Ssam * regular file, or empty directory. 6319166Ssam */ 6329166Ssam if ((flag == CREATE && lockparent) && *cp == 0) { 6339166Ssam if (access(dp, IWRITE)) 6349166Ssam goto bad; 6359166Ssam u.u_pdir = dp; /* for dirrewrite() */ 6369166Ssam /* 6379166Ssam * Careful about locking second inode. 6389166Ssam * This can only occur if the target is ".". 6399166Ssam */ 6409166Ssam if (dp->i_number == u.u_dent.d_ino) { 6419166Ssam u.u_error = EISDIR; /* XXX */ 6429166Ssam goto bad; 6439166Ssam } 6449166Ssam dp = iget(dp->i_dev, fs, u.u_dent.d_ino); 6459166Ssam if (dp == NULL) { 6469166Ssam iput(u.u_pdir); 6479166Ssam goto bad; 6489166Ssam } 6499166Ssam brelse(nbp); 6509166Ssam return (dp); 6519166Ssam } 6529166Ssam 6539166Ssam /* 65412011Smckusick * Check for symbolic link, which may require us to massage the 65512011Smckusick * name before we continue translation. We do not `iput' the 65612011Smckusick * directory because we may need it again if the symbolic link 65712011Smckusick * is relative to the current directory. Instead we save it 65812011Smckusick * unlocked as "pdp". We must get the target inode before unlocking 65912011Smckusick * the directory to insure that the inode will not be removed 66012011Smckusick * before we get it. We prevent deadlock by always fetching 66112011Smckusick * inodes from the root, moving down the directory tree. Thus 66212011Smckusick * when following backward pointers ".." we must unlock the 66312011Smckusick * parent directory before getting the requested directory. 66412011Smckusick * There is a potential race condition here if both the current 66512011Smckusick * and parent directories are removed before the `iget' for the 66612011Smckusick * inode associated with ".." returns. We hope that this occurs 66712011Smckusick * infrequently since we cannot avoid this race condition without 66812492Ssam * implementing a sophisticated deadlock detection algorithm. 66912011Smckusick * Note also that this simple deadlock detection scheme will not 67012011Smckusick * work if the file system has any hard links other than ".." 67112011Smckusick * that point backwards in the directory structure. 6727534Sroot */ 6737534Sroot pdp = dp; 67415798Smckusick if (isdotdot) { 67512011Smckusick iunlock(pdp); /* race to get the inode */ 67612011Smckusick dp = iget(dp->i_dev, fs, u.u_dent.d_ino); 67712011Smckusick if (dp == NULL) 67812011Smckusick goto bad2; 67912011Smckusick } else if (dp->i_number == u.u_dent.d_ino) { 68012011Smckusick dp->i_count++; /* we want ourself, ie "." */ 68112011Smckusick } else { 68212011Smckusick dp = iget(dp->i_dev, fs, u.u_dent.d_ino); 68312011Smckusick iunlock(pdp); 68412011Smckusick if (dp == NULL) 68512011Smckusick goto bad2; 68612011Smckusick } 68715798Smckusick 68815798Smckusick /* 68915798Smckusick * insert name into cache (if we want it, and it isn't "." or "..") 69015798Smckusick * 69115798Smckusick * all other cases where making a cache entry would be wrong 69215798Smckusick * have already departed from the code sequence somewhere above. 69315798Smckusick */ 69416643Ssam if (docache) { 69515798Smckusick if (ncp != NULL) 69615798Smckusick panic("nami: duplicating cache"); 69715798Smckusick 69815798Smckusick /* 69915798Smckusick * free the cache slot at head of lru chain 70015798Smckusick */ 70115798Smckusick if (ncp = nchhead) { 70215798Smckusick /* remove from lru chain */ 70315798Smckusick *ncp->nc_prev = ncp->nc_nxt; 70415798Smckusick if (ncp->nc_nxt) 70515798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 70615798Smckusick else 70715798Smckusick nchtail = ncp->nc_prev; 70815798Smckusick 70915798Smckusick /* remove from old hash chain */ 71015798Smckusick remque(ncp); 71115798Smckusick 71215798Smckusick /* grab the inode we just found */ 71315798Smckusick ncp->nc_ip = dp; 71415798Smckusick 71515798Smckusick /* fill in cache info */ 71615798Smckusick ncp->nc_ino = pdp->i_number; /* parents inum */ 71715798Smckusick ncp->nc_dev = pdp->i_dev; /* & device */ 71815798Smckusick ncp->nc_idev = dp->i_dev; /* our device */ 71916643Ssam ncp->nc_id = dp->i_id; /* identifier */ 72015798Smckusick ncp->nc_nlen = u.u_dent.d_namlen; 72115798Smckusick bcopy(u.u_dent.d_name, ncp->nc_name, ncp->nc_nlen); 72215798Smckusick 72315798Smckusick /* link at end of lru chain */ 72415798Smckusick ncp->nc_nxt = NULL; 72515798Smckusick ncp->nc_prev = nchtail; 72615798Smckusick *nchtail = ncp; 72715798Smckusick nchtail = &ncp->nc_nxt; 72815798Smckusick 72915798Smckusick /* and insert on hash chain */ 73015798Smckusick insque(ncp, nhp); 73115798Smckusick } 73215798Smckusick } 73315798Smckusick 73415798Smckusick haveino: 7357534Sroot fs = dp->i_fs; 7367534Sroot 7377534Sroot /* 7387534Sroot * Check for symbolic link 7397534Sroot */ 7407534Sroot if ((dp->i_mode & IFMT) == IFLNK && (follow || *cp == '/')) { 7417825Sroot u_int pathlen = strlen(cp) + 1; 7427534Sroot 7437534Sroot if (dp->i_size + pathlen >= MAXPATHLEN - 1 || 7447534Sroot ++nlink > MAXSYMLINKS) { 7457534Sroot u.u_error = ELOOP; 7467534Sroot goto bad2; 7477534Sroot } 7488957Sroot ovbcopy(cp, nbp->b_un.b_addr + dp->i_size, pathlen); 7497751Sroot u.u_error = 7509166Ssam rdwri(UIO_READ, dp, nbp->b_un.b_addr, (int)dp->i_size, 7517825Sroot 0, 1, (int *)0); 7527534Sroot if (u.u_error) 7537534Sroot goto bad2; 7547534Sroot cp = nbp->b_un.b_addr; 7557534Sroot iput(dp); 7565972Swnj if (*cp == '/') { 7577534Sroot irele(pdp); 7585972Swnj while (*cp == '/') 7595972Swnj cp++; 7607534Sroot if ((dp = u.u_rdir) == NULL) 7617534Sroot dp = rootdir; 7627534Sroot ilock(dp); 7637534Sroot dp->i_count++; 7647534Sroot } else { 7657534Sroot dp = pdp; 7667534Sroot ilock(dp); 7675972Swnj } 7687534Sroot fs = dp->i_fs; 7697534Sroot goto dirloop; 77030Sbill } 7717534Sroot 77230Sbill /* 7737534Sroot * Not a symbolic link. If more pathname, 7747534Sroot * continue at next component, else return. 77530Sbill */ 7767534Sroot if (*cp == '/') { 7777534Sroot while (*cp == '/') 7787534Sroot cp++; 7799166Ssam irele(pdp); 7807534Sroot goto dirloop; 78130Sbill } 7825972Swnj brelse(nbp); 7839166Ssam if (lockparent) 7849166Ssam u.u_pdir = pdp; 7859166Ssam else 7869166Ssam irele(pdp); 7877534Sroot return (dp); 7887534Sroot bad2: 7897534Sroot irele(pdp); 7907534Sroot bad: 7917534Sroot if (bp) 7927534Sroot brelse(bp); 7937534Sroot if (dp) 7947534Sroot iput(dp); 7957534Sroot brelse(nbp); 7966571Smckusic return (NULL); 79730Sbill } 79830Sbill 79915798Smckusick 8007534Sroot dirbad(ip, how) 8017534Sroot struct inode *ip; 8027534Sroot char *how; 8037534Sroot { 8047534Sroot 8057534Sroot printf("%s: bad dir ino %d at offset %d: %s\n", 8067534Sroot ip->i_fs->fs_fsmnt, ip->i_number, u.u_offset, how); 8077534Sroot } 8087534Sroot 809*16657Smckusick /* 810*16657Smckusick * Do consistency checking on a directory entry: 811*16657Smckusick * record length must be multiple of 4 812*16657Smckusick * record length must not be non-negative 813*16657Smckusick * entry must fit in rest of its DIRBLKSIZ block 814*16657Smckusick * record must be large enough to contain entry 815*16657Smckusick * name is not longer than MAXNAMLEN 816*16657Smckusick * name must be as long as advertised, and null terminated 817*16657Smckusick */ 818*16657Smckusick dirbadentry(ep, entryoffsetinblock) 8197534Sroot register struct direct *ep; 820*16657Smckusick int entryoffsetinblock; 8217534Sroot { 8227534Sroot register int i; 8237534Sroot 824*16657Smckusick if ((ep->d_reclen & 0x3) != 0 || ep->d_reclen <= 0 || 825*16657Smckusick ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 826*16657Smckusick ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN) 827*16657Smckusick return (1); 8287534Sroot for (i = 0; i < ep->d_namlen; i++) 8297534Sroot if (ep->d_name[i] == 0) 8307534Sroot return (1); 8317534Sroot return (ep->d_name[i]); 8327534Sroot } 8337534Sroot 83430Sbill /* 8357534Sroot * Write a directory entry after a call to namei, using the parameters 8367534Sroot * which it left in the u. area. The argument ip is the inode which 8377534Sroot * the new directory entry will refer to. The u. area field u.u_pdir is 8387534Sroot * a pointer to the directory to be written, which was left locked by 8397534Sroot * namei. Remaining parameters (u.u_offset, u.u_count) indicate 8407534Sroot * how the space for the new entry is to be gotten. 8417534Sroot */ 8427534Sroot direnter(ip) 8437534Sroot struct inode *ip; 8445972Swnj { 8457534Sroot register struct direct *ep, *nep; 8467534Sroot struct buf *bp; 84711639Ssam int loc, spacefree, error = 0; 8488631Sroot u_int dsize; 8498631Sroot int newentrysize; 8507534Sroot char *dirbuf; 8515972Swnj 8527534Sroot u.u_dent.d_ino = ip->i_number; 8537534Sroot u.u_segflg = 1; 8547534Sroot newentrysize = DIRSIZ(&u.u_dent); 8557534Sroot if (u.u_count == 0) { 8567534Sroot /* 8577534Sroot * If u.u_count is 0, then namei could find no space in the 8587534Sroot * directory. In this case u.u_offset will be on a directory 8597534Sroot * block boundary and we will write the new entry into a fresh 8607534Sroot * block. 8617534Sroot */ 8627534Sroot if (u.u_offset&(DIRBLKSIZ-1)) 8637534Sroot panic("wdir: newblk"); 8647534Sroot u.u_dent.d_reclen = DIRBLKSIZ; 86510849Ssam error = rdwri(UIO_WRITE, u.u_pdir, (caddr_t)&u.u_dent, 8668631Sroot newentrysize, u.u_offset, 1, (int *)0); 8677534Sroot iput(u.u_pdir); 86810849Ssam return (error); 8697534Sroot } 8707534Sroot 8717534Sroot /* 8727534Sroot * If u.u_count is non-zero, then namei found space for the 8737534Sroot * new entry in the range u.u_offset to u.u_offset+u.u_count. 8747534Sroot * in the directory. To use this space, we may have to compact 8757534Sroot * the entries located there, by copying them together towards 8767534Sroot * the beginning of the block, leaving the free space in 8777534Sroot * one usable chunk at the end. 8787534Sroot */ 8797534Sroot 8807534Sroot /* 8817534Sroot * Increase size of directory if entry eats into new space. 8827534Sroot * This should never push the size past a new multiple of 8837534Sroot * DIRBLKSIZE. 8847534Sroot */ 8859166Ssam if (u.u_offset + u.u_count > u.u_pdir->i_size) 8867534Sroot u.u_pdir->i_size = u.u_offset + u.u_count; 8877534Sroot 8887534Sroot /* 8897534Sroot * Get the block containing the space for the new directory 89010849Ssam * entry. Should return error by result instead of u.u_error. 8917534Sroot */ 8927605Ssam bp = blkatoff(u.u_pdir, u.u_offset, (char **)&dirbuf); 8939166Ssam if (bp == 0) { 8949166Ssam iput(u.u_pdir); 89510849Ssam return (u.u_error); 8969166Ssam } 8977534Sroot 8987534Sroot /* 8997534Sroot * Find space for the new entry. In the simple case, the 9007534Sroot * entry at offset base will have the space. If it does 9017534Sroot * not, then namei arranged that compacting the region 9027534Sroot * u.u_offset to u.u_offset+u.u_count would yield the space. 9037534Sroot */ 9047534Sroot ep = (struct direct *)dirbuf; 9057534Sroot dsize = DIRSIZ(ep); 90611639Ssam spacefree = ep->d_reclen - dsize; 9077534Sroot for (loc = ep->d_reclen; loc < u.u_count; ) { 9087534Sroot nep = (struct direct *)(dirbuf + loc); 9097534Sroot if (ep->d_ino) { 9107534Sroot /* trim the existing slot */ 9117534Sroot ep->d_reclen = dsize; 9127534Sroot ep = (struct direct *)((char *)ep + dsize); 9137534Sroot } else { 9147534Sroot /* overwrite; nothing there; header is ours */ 91511639Ssam spacefree += dsize; 9167534Sroot } 9177534Sroot dsize = DIRSIZ(nep); 91811639Ssam spacefree += nep->d_reclen - dsize; 9197534Sroot loc += nep->d_reclen; 9207825Sroot bcopy((caddr_t)nep, (caddr_t)ep, dsize); 9217534Sroot } 9227534Sroot /* 9237534Sroot * Update the pointer fields in the previous entry (if any), 9247534Sroot * copy in the new entry, and write out the block. 9257534Sroot */ 9267534Sroot if (ep->d_ino == 0) { 92711639Ssam if (spacefree + dsize < newentrysize) 9287534Sroot panic("wdir: compact1"); 92911639Ssam u.u_dent.d_reclen = spacefree + dsize; 9307534Sroot } else { 93111639Ssam if (spacefree < newentrysize) 9327534Sroot panic("wdir: compact2"); 93311639Ssam u.u_dent.d_reclen = spacefree; 9347534Sroot ep->d_reclen = dsize; 9357534Sroot ep = (struct direct *)((char *)ep + dsize); 9367534Sroot } 9378672Sroot bcopy((caddr_t)&u.u_dent, (caddr_t)ep, (u_int)newentrysize); 9387534Sroot bwrite(bp); 9397534Sroot u.u_pdir->i_flag |= IUPD|ICHG; 9407534Sroot iput(u.u_pdir); 94110849Ssam return (error); 9425972Swnj } 9436571Smckusic 9449166Ssam /* 9459166Ssam * Remove a directory entry after a call to namei, using the 9469166Ssam * parameters which it left in the u. area. The u. entry 9479166Ssam * u_offset contains the offset into the directory of the 9489166Ssam * entry to be eliminated. The u_count field contains the 9499166Ssam * size of the previous record in the directory. If this 9509166Ssam * is 0, the first entry is being deleted, so we need only 9519166Ssam * zero the inode number to mark the entry as free. If the 9529166Ssam * entry isn't the first in the directory, we must reclaim 9539166Ssam * the space of the now empty record by adding the record size 9549166Ssam * to the size of the previous entry. 9559166Ssam */ 9567534Sroot dirremove() 9576571Smckusic { 9587534Sroot register struct inode *dp = u.u_pdir; 9597534Sroot register struct buf *bp; 9607534Sroot struct direct *ep; 9616571Smckusic 9629269Ssam if (u.u_count == 0) { 9637534Sroot /* 9647534Sroot * First entry in block: set d_ino to zero. 9657534Sroot */ 9669269Ssam u.u_dent.d_ino = 0; 9678619Sroot (void) rdwri(UIO_WRITE, dp, (caddr_t)&u.u_dent, 9688631Sroot (int)DIRSIZ(&u.u_dent), u.u_offset, 1, (int *)0); 9699269Ssam } else { 9707534Sroot /* 9717534Sroot * Collapse new free space into previous entry. 9727534Sroot */ 9737825Sroot bp = blkatoff(dp, (int)(u.u_offset - u.u_count), (char **)&ep); 9747534Sroot if (bp == 0) 9757534Sroot return (0); 9767534Sroot ep->d_reclen += u.u_dent.d_reclen; 9777534Sroot bwrite(bp); 9787534Sroot dp->i_flag |= IUPD|ICHG; 9797534Sroot } 9807534Sroot return (1); 9816571Smckusic } 9827534Sroot 9837605Ssam /* 9849166Ssam * Rewrite an existing directory entry to point at the inode 9859166Ssam * supplied. The parameters describing the directory entry are 9869166Ssam * set up by a call to namei. 9879166Ssam */ 9889166Ssam dirrewrite(dp, ip) 9899166Ssam struct inode *dp, *ip; 9909166Ssam { 9919166Ssam 9929166Ssam u.u_dent.d_ino = ip->i_number; 9939166Ssam u.u_error = rdwri(UIO_WRITE, dp, (caddr_t)&u.u_dent, 9949166Ssam (int)DIRSIZ(&u.u_dent), u.u_offset, 1, (int *)0); 9959166Ssam iput(dp); 9969166Ssam } 9979166Ssam 9989166Ssam /* 9997605Ssam * Return buffer with contents of block "offset" 10007605Ssam * from the beginning of directory "ip". If "res" 10017605Ssam * is non-zero, fill it in with a pointer to the 10027605Ssam * remaining space in the directory. 10037605Ssam */ 10047534Sroot struct buf * 10057605Ssam blkatoff(ip, offset, res) 10067534Sroot struct inode *ip; 10077534Sroot off_t offset; 10087534Sroot char **res; 10097534Sroot { 10107534Sroot register struct fs *fs = ip->i_fs; 10118672Sroot daddr_t lbn = lblkno(fs, offset); 10127534Sroot int base = blkoff(fs, offset); 10137534Sroot int bsize = blksize(fs, ip, lbn); 10148672Sroot daddr_t bn = fsbtodb(fs, bmap(ip, lbn, B_WRITE, base, bsize)); 10157534Sroot register struct buf *bp; 10167534Sroot 10177534Sroot if (u.u_error) 10187534Sroot return (0); 10197534Sroot bp = bread(ip->i_dev, bn, bsize); 10207534Sroot if (bp->b_flags & B_ERROR) { 10217534Sroot brelse(bp); 10227534Sroot return (0); 10237534Sroot } 10247534Sroot if (res) 10257534Sroot *res = bp->b_un.b_addr + base; 10267534Sroot return (bp); 10277534Sroot } 10289166Ssam 10299166Ssam /* 10309166Ssam * Check if a directory is empty or not. 10319166Ssam * Inode supplied must be locked. 103212817Ssam * 103312817Ssam * Using a struct dirtemplate here is not precisely 103412817Ssam * what we want, but better than using a struct direct. 103512817Ssam * 103612817Ssam * NB: does not handle corrupted directories. 10379166Ssam */ 10389166Ssam dirempty(ip) 10399863Ssam register struct inode *ip; 10409166Ssam { 10419166Ssam register off_t off; 104212817Ssam struct dirtemplate dbuf; 104312817Ssam register struct direct *dp = (struct direct *)&dbuf; 10449863Ssam int error, count; 104512817Ssam #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 10469166Ssam 10479166Ssam for (off = 0; off < ip->i_size; off += dp->d_reclen) { 104812817Ssam error = rdwri(UIO_READ, ip, (caddr_t)dp, MINDIRSIZ, 104912817Ssam off, 1, &count); 105012817Ssam /* 105112817Ssam * Since we read MINDIRSIZ, residual must 105212817Ssam * be 0 unless we're at end of file. 105312817Ssam */ 105412817Ssam if (error || count != 0) 10559166Ssam return (0); 105612817Ssam /* skip empty entries */ 10579166Ssam if (dp->d_ino == 0) 10589166Ssam continue; 105912817Ssam /* accept only "." and ".." */ 106012817Ssam if (dp->d_namlen > 2) 106112817Ssam return (0); 10629166Ssam if (dp->d_name[0] != '.') 10639166Ssam return (0); 106412817Ssam /* 106512817Ssam * At this point d_namlen must be 1 or 2. 106612817Ssam * 1 implies ".", 2 implies ".." if second 106712817Ssam * char is also "." 106812817Ssam */ 106912817Ssam if (dp->d_namlen == 1 || dp->d_name[1] == '.') 10709166Ssam continue; 10719166Ssam return (0); 10729166Ssam } 10739166Ssam return (1); 10749166Ssam } 107512815Smckusick 107612815Smckusick /* 107712815Smckusick * Check if source directory is in the path of the target directory. 107812815Smckusick * Target is supplied locked, source is unlocked. 107912815Smckusick * The target is always iput() before returning. 108012815Smckusick */ 108112815Smckusick checkpath(source, target) 108212815Smckusick struct inode *source, *target; 108312815Smckusick { 108412815Smckusick struct dirtemplate dirbuf; 108512815Smckusick register struct inode *ip; 108612815Smckusick int error = 0; 108712815Smckusick 108812815Smckusick ip = target; 108912815Smckusick if (ip->i_number == source->i_number) { 109012815Smckusick error = EEXIST; 109112815Smckusick goto out; 109212815Smckusick } 109312815Smckusick if (ip->i_number == ROOTINO) 109412815Smckusick goto out; 109512815Smckusick 109612815Smckusick for (;;) { 109712815Smckusick if ((ip->i_mode&IFMT) != IFDIR) { 109812815Smckusick error = ENOTDIR; 109912815Smckusick break; 110012815Smckusick } 110112815Smckusick error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf, 110212815Smckusick sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 110312815Smckusick if (error != 0) 110412815Smckusick break; 110512815Smckusick if (dirbuf.dotdot_namlen != 2 || 110612815Smckusick bcmp(dirbuf.dotdot_name, "..", 3) != 0) { 110712815Smckusick error = ENOTDIR; 110812815Smckusick break; 110912815Smckusick } 111012815Smckusick if (dirbuf.dotdot_ino == source->i_number) { 111112815Smckusick error = EINVAL; 111212815Smckusick break; 111312815Smckusick } 111412815Smckusick if (dirbuf.dotdot_ino == ROOTINO) 111512815Smckusick break; 111612815Smckusick iput(ip); 111712815Smckusick ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino); 111812815Smckusick if (ip == NULL) { 111912815Smckusick error = u.u_error; 112012815Smckusick break; 112112815Smckusick } 112212815Smckusick } 112312815Smckusick 112412815Smckusick out: 112512815Smckusick if (error == ENOTDIR) 112612815Smckusick printf("checkpath: .. not a directory\n"); 112712815Smckusick if (ip != NULL) 112812815Smckusick iput(ip); 112912815Smckusick return (error); 113012815Smckusick } 113115798Smckusick 113215798Smckusick /* 113315798Smckusick * Name cache initialization, from main() when we are booting 113415798Smckusick */ 113515798Smckusick nchinit() 113615798Smckusick { 113715798Smckusick register union nchash *nchp; 113815798Smckusick register struct nch *ncp; 113915798Smckusick 114015798Smckusick nchhead = 0; 114115798Smckusick nchtail = &nchhead; 114215798Smckusick 114315798Smckusick for (ncp = nch; ncp < &nch[nchsize]; ncp++) { 114415798Smckusick ncp->nc_forw = ncp; /* hash chain */ 114515798Smckusick ncp->nc_back = ncp; 114615798Smckusick 114715798Smckusick ncp->nc_nxt = NULL; /* lru chain */ 114815798Smckusick *nchtail = ncp; 114915798Smckusick ncp->nc_prev = nchtail; 115015798Smckusick nchtail = &ncp->nc_nxt; 115115798Smckusick 115215798Smckusick /* all else is zero already */ 115315798Smckusick } 115415798Smckusick 115515798Smckusick for (nchp = nchash; nchp < &nchash[NCHHASH]; nchp++) { 115615798Smckusick nchp->nch_head[0] = nchp; 115715798Smckusick nchp->nch_head[1] = nchp; 115815798Smckusick } 115915798Smckusick } 116015798Smckusick 116115798Smckusick /* 116215798Smckusick * Cache flush, called when filesys is umounted to 116315798Smckusick * remove entries that would now be invalid 116415798Smckusick * 116515798Smckusick * The line "nxtcp = nchhead" near the end is to avoid potential problems 116615798Smckusick * if the cache lru chain is modified while we are dumping the 116715798Smckusick * inode. This makes the algorithm O(n^2), but do you think I care? 116815798Smckusick */ 116915798Smckusick nchinval(dev) 117015798Smckusick register dev_t dev; 117115798Smckusick { 117215798Smckusick register struct nch *ncp, *nxtcp; 117315798Smckusick 117415798Smckusick for (ncp = nchhead; ncp; ncp = nxtcp) { 117515798Smckusick nxtcp = ncp->nc_nxt; 117615798Smckusick 117715798Smckusick if (ncp->nc_ip == NULL || 117815798Smckusick (ncp->nc_idev != dev && ncp->nc_dev != dev)) 117915798Smckusick continue; 118015798Smckusick 118115798Smckusick ncp->nc_idev = NODEV; 118215798Smckusick ncp->nc_dev = NODEV; 118315798Smckusick ncp->nc_ino = 0; 118415798Smckusick 118515798Smckusick /* remove the entry from its hash chain */ 118615798Smckusick remque(ncp); 118715798Smckusick /* and make a dummy one */ 118815798Smckusick ncp->nc_forw = ncp; 118915798Smckusick ncp->nc_back = ncp; 119015798Smckusick 119115798Smckusick /* delete this entry from LRU chain */ 119215798Smckusick *ncp->nc_prev = nxtcp; 119315798Smckusick if (nxtcp) 119415798Smckusick nxtcp->nc_prev = ncp->nc_prev; 119515798Smckusick else 119615798Smckusick nchtail = ncp->nc_prev; 119715798Smckusick 119815798Smckusick /* free the inode we had */ 119915798Smckusick irele(ncp->nc_ip); 120015798Smckusick ncp->nc_ip = NULL; 120115798Smckusick 120215798Smckusick /* cause rescan of list, it may have altered */ 120315798Smckusick nxtcp = nchhead; 120415798Smckusick /* put the now-free entry at head of LRU */ 120515798Smckusick ncp->nc_nxt = nxtcp; 120615798Smckusick ncp->nc_prev = &nchhead; 120715798Smckusick nxtcp->nc_prev = &ncp->nc_nxt; 120815798Smckusick nchhead = ncp; 120915798Smckusick } 121015798Smckusick } 1211