1*21014Smckusick /* vfs_lookup.c 6.23 85/05/22 */ 230Sbill 317100Sbloom #include "param.h" 417100Sbloom #include "systm.h" 517100Sbloom #include "inode.h" 617100Sbloom #include "fs.h" 717100Sbloom #include "mount.h" 817100Sbloom #include "dir.h" 917100Sbloom #include "user.h" 1017100Sbloom #include "buf.h" 1117100Sbloom #include "conf.h" 1217100Sbloom #include "uio.h" 1317100Sbloom #include "kernel.h" 1430Sbill 157605Ssam struct buf *blkatoff(); 1616681Smckusick struct buf *freenamebuf; 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 * 4516688Smckusick * The segflg defines whether the name is to be copied from user 4616688Smckusick * space or kernel space. 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 * 5816688Smckusick * The FOLLOW flag is set 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: 9016688Smckusick * copy next component of name to ndp->ni_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 * 11316688Smckusick namei(ndp) 11416688Smckusick register struct nameidata *ndp; 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 */ 13416688Smckusick int prevoff; /* ndp->ni_offset of previous entry */ 1357534Sroot int nlink = 0; /* number of symbolic links taken */ 1367534Sroot struct inode *pdp; /* saved dp during symlink work */ 13716688Smckusick int error, i; 1389166Ssam int lockparent; 13918109Smckusick int docache; /* == 0 do not cache last component */ 14018109Smckusick int makeentry; /* != 0 if name to be added to cache */ 14115798Smckusick unsigned hash; /* value of name hash for entry */ 14215798Smckusick union nchash *nhp; /* cache chain head for entry */ 14315798Smckusick int isdotdot; /* != 0 if current name is ".." */ 14416688Smckusick int flag; /* op ie, LOOKUP, CREATE, or DELETE */ 14518027Smckusick off_t enduseful; /* pointer past last used dir slot */ 14630Sbill 14716688Smckusick lockparent = ndp->ni_nameiop & LOCKPARENT; 14816688Smckusick docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE; 14916688Smckusick flag = ndp->ni_nameiop &~ (LOCKPARENT|NOCACHE|FOLLOW); 15018109Smckusick if (flag == DELETE || lockparent) 15115798Smckusick docache = 0; 15230Sbill /* 1537534Sroot * Get a buffer for the name to be translated, and copy the 1547534Sroot * name into the buffer. 1555972Swnj */ 15616681Smckusick nbp = freenamebuf; 15716681Smckusick if (nbp == NULL) 15816681Smckusick nbp = geteblk(MAXPATHLEN); 15916681Smckusick else 16016681Smckusick freenamebuf = nbp->av_forw; 16116688Smckusick if (ndp->ni_segflg == UIO_SYSSPACE) 16216705Smckusick error = copystr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN, 16316705Smckusick (u_int *)0); 16416688Smckusick else 16516705Smckusick error = copyinstr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN, 16616705Smckusick (u_int *)0); 16716688Smckusick if (error) { 16816688Smckusick u.u_error = error; 16916688Smckusick goto bad; 1705972Swnj } 1717534Sroot 1725972Swnj /* 1737534Sroot * Get starting directory. 17430Sbill */ 1757534Sroot cp = nbp->b_un.b_addr; 1765972Swnj if (*cp == '/') { 1775972Swnj while (*cp == '/') 1785972Swnj cp++; 17930Sbill if ((dp = u.u_rdir) == NULL) 18030Sbill dp = rootdir; 1817534Sroot } else 1827534Sroot dp = u.u_cdir; 1837534Sroot fs = dp->i_fs; 18416666Smckusick ILOCK(dp); 1855972Swnj dp->i_count++; 18616688Smckusick ndp->ni_pdir = (struct inode *)0xc0000000; /* illegal */ 18718027Smckusick ndp->ni_endoff = 0; 1887534Sroot 1897534Sroot /* 1907534Sroot * We come to dirloop to search a new directory. 1917534Sroot * The directory must be locked so that it can be 1927534Sroot * iput, and fs must be already set to dp->i_fs. 1937534Sroot */ 1946571Smckusic dirloop: 19530Sbill /* 1967534Sroot * Check accessiblity of directory. 19730Sbill */ 1987534Sroot if ((dp->i_mode&IFMT) != IFDIR) { 19930Sbill u.u_error = ENOTDIR; 2007534Sroot goto bad; 2017534Sroot } 2027534Sroot if (access(dp, IEXEC)) 2037534Sroot goto bad; 2047534Sroot 2056384Swnj dirloop2: 2067534Sroot /* 20716688Smckusick * Copy next component of name to ndp->ni_dent. 2087534Sroot */ 20915798Smckusick hash = 0; 2107534Sroot for (i = 0; *cp != 0 && *cp != '/'; cp++) { 2116571Smckusic if (i >= MAXNAMLEN) { 212*21014Smckusick u.u_error = ENAMETOOLONG; 2137534Sroot goto bad; 2145972Swnj } 215*21014Smckusick if (*cp & 0200) 216*21014Smckusick if ((*cp&0377) == ('/'|0200) || flag != DELETE) { 217*21014Smckusick u.u_error = EINVAL; 218*21014Smckusick goto bad; 219*21014Smckusick } 22016688Smckusick ndp->ni_dent.d_name[i++] = *cp; 22115798Smckusick hash += (unsigned char)*cp * i; 2225972Swnj } 22316688Smckusick ndp->ni_dent.d_namlen = i; 22416688Smckusick ndp->ni_dent.d_name[i] = '\0'; 22516658Smckusick isdotdot = (i == 2 && 22616688Smckusick ndp->ni_dent.d_name[0] == '.' && ndp->ni_dent.d_name[1] == '.'); 22718109Smckusick makeentry = 1; 22818109Smckusick if (*cp == '\0' && docache == 0) 22918109Smckusick makeentry = 0; 2307534Sroot 2317534Sroot /* 2327534Sroot * Check for degenerate name (e.g. / or "") 2337534Sroot * which is a way of talking about a directory, 2347534Sroot * e.g. like "/." or ".". 2357534Sroot */ 23616688Smckusick if (ndp->ni_dent.d_name[0] == '\0') { 23715798Smckusick if (flag != LOOKUP || lockparent) { 23814937Smckusick u.u_error = EISDIR; 2397534Sroot goto bad; 2405972Swnj } 24116681Smckusick nbp->av_forw = freenamebuf; 24216681Smckusick freenamebuf = nbp; 2436571Smckusic return (dp); 2445972Swnj } 2457534Sroot 2466571Smckusic /* 24715798Smckusick * We now have a segment name to search for, and a directory to search. 24815798Smckusick * 24915798Smckusick * Before tediously performing a linear scan of the directory, 25015798Smckusick * check the name cache to see if the directory/name pair 25115798Smckusick * we are looking for is known already. We don't do this 25215798Smckusick * if the segment name is long, simply so the cache can avoid 25315798Smckusick * holding long names (which would either waste space, or 25415798Smckusick * add greatly to the complexity). 25515798Smckusick */ 25616688Smckusick if (ndp->ni_dent.d_namlen > NCHNAMLEN) { 25715798Smckusick nchstats.ncs_long++; 25818109Smckusick makeentry = 0; 25915798Smckusick } else { 26015798Smckusick nhp = &nchash[NHASH(hash, dp->i_number, dp->i_dev)]; 26115798Smckusick for (ncp = nhp->nch_forw; ncp != (struct nch *)nhp; 26215798Smckusick ncp = ncp->nc_forw) { 26315798Smckusick if (ncp->nc_ino == dp->i_number && 26415798Smckusick ncp->nc_dev == dp->i_dev && 26516688Smckusick ncp->nc_nlen == ndp->ni_dent.d_namlen && 26616688Smckusick !bcmp(ncp->nc_name, ndp->ni_dent.d_name, 26716688Smckusick ncp->nc_nlen)) 26815798Smckusick break; 26915798Smckusick } 27015798Smckusick 27115798Smckusick if (ncp == (struct nch *)nhp) { 27215798Smckusick nchstats.ncs_miss++; 27315798Smckusick ncp = NULL; 27415798Smckusick } else { 27516658Smckusick if (ncp->nc_id != ncp->nc_ip->i_id) { 27616643Ssam nchstats.ncs_falsehits++; 27718109Smckusick } else if (!makeentry) { 27816658Smckusick nchstats.ncs_badhits++; 27916658Smckusick } else { 28015798Smckusick 28115798Smckusick /* 28215798Smckusick * move this slot to end of LRU 28315798Smckusick * chain, if not already there 28415798Smckusick */ 28515798Smckusick if (ncp->nc_nxt) { 28615798Smckusick /* remove from LRU chain */ 28715798Smckusick *ncp->nc_prev = ncp->nc_nxt; 28815798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 28915798Smckusick 29015798Smckusick /* and replace at end of it */ 29115798Smckusick ncp->nc_nxt = NULL; 29215798Smckusick ncp->nc_prev = nchtail; 29315798Smckusick *nchtail = ncp; 29415798Smckusick nchtail = &ncp->nc_nxt; 29515798Smckusick } 29615798Smckusick 29716658Smckusick /* 29816658Smckusick * Get the next inode in the path. 29916666Smckusick * See comment above other `IUNLOCK' code for 30016658Smckusick * an explaination of the locking protocol. 30116658Smckusick */ 30215798Smckusick pdp = dp; 30318109Smckusick if (!isdotdot || dp != u.u_rdir) 30418109Smckusick dp = ncp->nc_ip; 30515798Smckusick if (dp == NULL) 30615798Smckusick panic("nami: null cache ino"); 30718109Smckusick if (pdp == dp) { 30816643Ssam dp->i_count++; 30918109Smckusick } else if (isdotdot) { 31018109Smckusick IUNLOCK(pdp); 31118109Smckusick igrab(dp); 31218109Smckusick } else { 31318109Smckusick igrab(dp); 31418109Smckusick IUNLOCK(pdp); 31516643Ssam } 31615798Smckusick 31716658Smckusick /* 31816658Smckusick * Verify that the inode that we got 31916658Smckusick * did not change while we were waiting 32016658Smckusick * for it to be locked. 32116658Smckusick */ 32216658Smckusick if (ncp->nc_id != ncp->nc_ip->i_id) { 32316658Smckusick iput(dp); 32416666Smckusick ILOCK(pdp); 32516658Smckusick dp = pdp; 32616658Smckusick nchstats.ncs_falsehits++; 32716658Smckusick } else { 32816688Smckusick ndp->ni_dent.d_ino = dp->i_number; 32916688Smckusick /* ni_dent.d_reclen is garbage ... */ 33016658Smckusick nchstats.ncs_goodhits++; 33116658Smckusick goto haveino; 33216658Smckusick } 33316658Smckusick } 33415798Smckusick 33515798Smckusick /* 33616643Ssam * Last component and we are renaming or deleting, 33716643Ssam * the cache entry is invalid, or otherwise don't 33816643Ssam * want cache entry to exist. 33915798Smckusick */ 34015798Smckusick 34115798Smckusick /* remove from LRU chain */ 34215798Smckusick *ncp->nc_prev = ncp->nc_nxt; 34315798Smckusick if (ncp->nc_nxt) 34415798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 34515798Smckusick else 34615798Smckusick nchtail = ncp->nc_prev; 34715798Smckusick 34815798Smckusick /* remove from hash chain */ 34915798Smckusick remque(ncp); 35015798Smckusick 35115798Smckusick /* insert at head of LRU list (first to grab) */ 35215798Smckusick ncp->nc_nxt = nchhead; 35315798Smckusick ncp->nc_prev = &nchhead; 35415798Smckusick nchhead->nc_prev = &ncp->nc_nxt; 35515798Smckusick nchhead = ncp; 35615798Smckusick 35715798Smckusick /* and make a dummy hash chain */ 35815798Smckusick ncp->nc_forw = ncp; 35915798Smckusick ncp->nc_back = ncp; 36015798Smckusick 36115798Smckusick ncp = NULL; 36215798Smckusick } 36315798Smckusick } 36415798Smckusick 36515798Smckusick /* 3667534Sroot * Suppress search for slots unless creating 3677534Sroot * file and at end of pathname, in which case 3687534Sroot * we watch for a place to put the new file in 3697534Sroot * case it doesn't already exist. 3706571Smckusic */ 3717534Sroot slotstatus = FOUND; 3729166Ssam if (flag == CREATE && *cp == 0) { 3737534Sroot slotstatus = NONE; 3747534Sroot slotfreespace = 0; 37516688Smckusick slotneeded = DIRSIZ(&ndp->ni_dent); 3767534Sroot } 37715660Smckusick /* 37815660Smckusick * If this is the same directory that this process 37915660Smckusick * previously searched, pick up where we last left off. 38015798Smckusick * We cache only lookups as these are the most common 38115660Smckusick * and have the greatest payoff. Caching CREATE has little 38215660Smckusick * benefit as it usually must search the entire directory 38315660Smckusick * to determine that the entry does not exist. Caching the 38415660Smckusick * location of the last DELETE has not reduced profiling time 38515660Smckusick * and hence has been removed in the interest of simplicity. 38615660Smckusick */ 38715660Smckusick if (flag != LOOKUP || dp->i_number != u.u_ncache.nc_inumber || 38815660Smckusick dp->i_dev != u.u_ncache.nc_dev) { 38916688Smckusick ndp->ni_offset = 0; 39015660Smckusick numdirpasses = 1; 39115660Smckusick } else { 39215798Smckusick if ((dp->i_flag & ICHG) || dp->i_ctime >= u.u_ncache.nc_time) { 39317698Smckusick if (u.u_ncache.nc_prevoffset > dp->i_size) 39417698Smckusick u.u_ncache.nc_prevoffset = 0; 39517698Smckusick else 39617698Smckusick u.u_ncache.nc_prevoffset &= ~(DIRBLKSIZ - 1); 39715660Smckusick u.u_ncache.nc_time = time.tv_sec; 39815660Smckusick } 39916688Smckusick ndp->ni_offset = u.u_ncache.nc_prevoffset; 40016688Smckusick entryoffsetinblock = blkoff(fs, ndp->ni_offset); 40115660Smckusick if (entryoffsetinblock != 0) { 40216688Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)0); 40315660Smckusick if (bp == 0) 40415660Smckusick goto bad; 40515660Smckusick } 40615660Smckusick numdirpasses = 2; 40715798Smckusick nchstats.ncs_2passes++; 40815660Smckusick } 40915660Smckusick endsearch = roundup(dp->i_size, DIRBLKSIZ); 41018027Smckusick enduseful = 0; 4117534Sroot 41215660Smckusick searchloop: 41316688Smckusick while (ndp->ni_offset < endsearch) { 4145972Swnj /* 4155972Swnj * If offset is on a block boundary, 4165972Swnj * read the next directory block. 4175972Swnj * Release previous if it exists. 4185972Swnj */ 41916688Smckusick if (blkoff(fs, ndp->ni_offset) == 0) { 4205972Swnj if (bp != NULL) 4215972Swnj brelse(bp); 42216688Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)0); 4237534Sroot if (bp == 0) 4247534Sroot goto bad; 4257534Sroot entryoffsetinblock = 0; 4265972Swnj } 4277534Sroot 4285972Swnj /* 4297534Sroot * If still looking for a slot, and at a DIRBLKSIZE 43016657Smckusick * boundary, have to start looking for free space again. 4316571Smckusic */ 4327534Sroot if (slotstatus == NONE && 4337534Sroot (entryoffsetinblock&(DIRBLKSIZ-1)) == 0) { 4347534Sroot slotoffset = -1; 4357534Sroot slotfreespace = 0; 4367534Sroot } 4377534Sroot 4387534Sroot /* 43916657Smckusick * Get pointer to next entry. 44016657Smckusick * Full validation checks are slow, so we only check 44116657Smckusick * enough to insure forward progress through the 44216657Smckusick * directory. Complete checks can be run by patching 44316657Smckusick * "dirchk" to be true. 4447534Sroot */ 4457534Sroot ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock); 44616657Smckusick if (ep->d_reclen <= 0 || 44716657Smckusick dirchk && dirbadentry(ep, entryoffsetinblock)) { 44816688Smckusick dirbad(dp, ndp->ni_offset, "mangled entry"); 44916657Smckusick i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)); 45016688Smckusick ndp->ni_offset += i; 4517534Sroot entryoffsetinblock += i; 4526571Smckusic continue; 4536571Smckusic } 4547534Sroot 4556571Smckusic /* 4567534Sroot * If an appropriate sized slot has not yet been found, 4576571Smckusic * check to see if one is available. Also accumulate space 4586571Smckusic * in the current block so that we can determine if 4596571Smckusic * compaction is viable. 4606571Smckusic */ 4617534Sroot if (slotstatus != FOUND) { 4627534Sroot int size = ep->d_reclen; 4637534Sroot 4646571Smckusic if (ep->d_ino != 0) 4656571Smckusic size -= DIRSIZ(ep); 4666571Smckusic if (size > 0) { 4677534Sroot if (size >= slotneeded) { 4687534Sroot slotstatus = FOUND; 46916688Smckusick slotoffset = ndp->ni_offset; 4707534Sroot slotsize = ep->d_reclen; 4717534Sroot } else if (slotstatus == NONE) { 4727534Sroot slotfreespace += size; 4737534Sroot if (slotoffset == -1) 47416688Smckusick slotoffset = ndp->ni_offset; 4757534Sroot if (slotfreespace >= slotneeded) { 4767534Sroot slotstatus = COMPACT; 47716688Smckusick slotsize = ndp->ni_offset + 47816688Smckusick ep->d_reclen - slotoffset; 4797534Sroot } 4806571Smckusic } 4816571Smckusic } 4826571Smckusic } 4837534Sroot 4846571Smckusic /* 4857534Sroot * Check for a name match. 4865972Swnj */ 4877534Sroot if (ep->d_ino) { 48816688Smckusick if (ep->d_namlen == ndp->ni_dent.d_namlen && 48916688Smckusick !bcmp(ndp->ni_dent.d_name, ep->d_name, 49016688Smckusick ep->d_namlen)) 4917534Sroot goto found; 4927534Sroot } 49316688Smckusick prevoff = ndp->ni_offset; 49416688Smckusick ndp->ni_offset += ep->d_reclen; 4957534Sroot entryoffsetinblock += ep->d_reclen; 49618027Smckusick if (ep->d_ino) 49718027Smckusick enduseful = ndp->ni_offset; 4987534Sroot } 49915798Smckusick /* notfound: */ 50015660Smckusick /* 50115798Smckusick * If we started in the middle of the directory and failed 50215660Smckusick * to find our target, we must check the beginning as well. 50315660Smckusick */ 50415660Smckusick if (numdirpasses == 2) { 50515660Smckusick numdirpasses--; 50616688Smckusick ndp->ni_offset = 0; 50715660Smckusick endsearch = u.u_ncache.nc_prevoffset; 50815660Smckusick goto searchloop; 50915660Smckusick } 5107534Sroot /* 5117534Sroot * If creating, and at end of pathname and current 5129166Ssam * directory has not been removed, then can consider 5139166Ssam * allowing file to be created. 5147534Sroot */ 5159166Ssam if (flag == CREATE && *cp == 0 && dp->i_nlink != 0) { 5165972Swnj /* 5177534Sroot * Access for write is interpreted as allowing 5187534Sroot * creation of files in the directory. 5195972Swnj */ 5207534Sroot if (access(dp, IWRITE)) 5217534Sroot goto bad; 5225972Swnj /* 5237534Sroot * Return an indication of where the new directory 5247534Sroot * entry should be put. If we didn't find a slot, 52516688Smckusick * then set ndp->ni_count to 0 indicating that the new 52616688Smckusick * slot belongs at the end of the directory. If we found 52716688Smckusick * a slot, then the new entry can be put in the range 52816688Smckusick * [ndp->ni_offset .. ndp->ni_offset + ndp->ni_count) 5295972Swnj */ 53015660Smckusick if (slotstatus == NONE) { 53116688Smckusick ndp->ni_offset = roundup(dp->i_size, DIRBLKSIZ); 53216688Smckusick ndp->ni_count = 0; 53318027Smckusick enduseful = ndp->ni_offset; 53415660Smckusick } else { 53516688Smckusick ndp->ni_offset = slotoffset; 53616688Smckusick ndp->ni_count = slotsize; 53718027Smckusick if (enduseful < slotoffset + slotsize) 53818027Smckusick enduseful = slotoffset + slotsize; 5395972Swnj } 54018027Smckusick ndp->ni_endoff = roundup(enduseful, DIRBLKSIZ); 5417534Sroot dp->i_flag |= IUPD|ICHG; 5427534Sroot if (bp) 5437534Sroot brelse(bp); 54416681Smckusick nbp->av_forw = freenamebuf; 54516681Smckusick freenamebuf = nbp; 5465972Swnj /* 5477534Sroot * We return with the directory locked, so that 5487534Sroot * the parameters we set up above will still be 5497534Sroot * valid if we actually decide to do a direnter(). 5507534Sroot * We return NULL to indicate that the entry doesn't 5517534Sroot * currently exist, leaving a pointer to the (locked) 55216688Smckusick * directory inode in ndp->ni_pdir. 5535972Swnj */ 55416688Smckusick ndp->ni_pdir = dp; 5557534Sroot return (NULL); 5567534Sroot } 5577534Sroot u.u_error = ENOENT; 5587534Sroot goto bad; 5597534Sroot found: 56015798Smckusick if (numdirpasses == 2) 56115798Smckusick nchstats.ncs_pass2++; 5627534Sroot /* 5637534Sroot * Check that directory length properly reflects presence 5647534Sroot * of this entry. 5657534Sroot */ 5667605Ssam if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) { 56716688Smckusick dirbad(dp, ndp->ni_offset, "i_size too small"); 5687605Ssam dp->i_size = entryoffsetinblock + DIRSIZ(ep); 5697534Sroot dp->i_flag |= IUPD|ICHG; 5707534Sroot } 5717534Sroot 5727534Sroot /* 57315660Smckusick * Found component in pathname. 57415798Smckusick * If the final component of path name, save information 57515660Smckusick * in the cache as to where the entry was found. 5767534Sroot */ 57715660Smckusick if (*cp == '\0' && flag == LOOKUP) { 57816688Smckusick u.u_ncache.nc_prevoffset = ndp->ni_offset; 57915660Smckusick u.u_ncache.nc_inumber = dp->i_number; 58015660Smckusick u.u_ncache.nc_dev = dp->i_dev; 58115660Smckusick u.u_ncache.nc_time = time.tv_sec; 58215660Smckusick } 58315660Smckusick /* 58418109Smckusick * Save directory entry's inode number and reclen in ndp->ni_dent, 58515660Smckusick * and release directory buffer. 58615660Smckusick */ 58718109Smckusick ndp->ni_dent.d_ino = ep->d_ino; 58818109Smckusick ndp->ni_dent.d_reclen = ep->d_reclen; 5897534Sroot brelse(bp); 5907534Sroot bp = NULL; 5917534Sroot 5927534Sroot /* 5937534Sroot * If deleting, and at end of pathname, return 5947534Sroot * parameters which can be used to remove file. 5959166Ssam * If the lockparent flag isn't set, we return only 59616688Smckusick * the directory (in ndp->ni_pdir), otherwise we go 5979166Ssam * on and lock the inode, being careful with ".". 5987534Sroot */ 5999166Ssam if (flag == DELETE && *cp == 0) { 6007534Sroot /* 6017534Sroot * Write access to directory required to delete files. 6027534Sroot */ 6037534Sroot if (access(dp, IWRITE)) 6047534Sroot goto bad; 60516688Smckusick ndp->ni_pdir = dp; /* for dirremove() */ 6067534Sroot /* 60716688Smckusick * Return pointer to current entry in ndp->ni_offset, 6087534Sroot * and distance past previous entry (if there 60916688Smckusick * is a previous entry in this block) in ndp->ni_count. 61016688Smckusick * Save directory inode pointer in ndp->ni_pdir for dirremove(). 6117534Sroot */ 61216688Smckusick if ((ndp->ni_offset&(DIRBLKSIZ-1)) == 0) 61316688Smckusick ndp->ni_count = 0; 6147534Sroot else 61516688Smckusick ndp->ni_count = ndp->ni_offset - prevoff; 6169166Ssam if (lockparent) { 61716688Smckusick if (dp->i_number == ndp->ni_dent.d_ino) 6189166Ssam dp->i_count++; 6199166Ssam else { 62016688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 6219166Ssam if (dp == NULL) { 62216688Smckusick iput(ndp->ni_pdir); 6239166Ssam goto bad; 6249166Ssam } 62515798Smckusick /* 62616046Skarels * If directory is "sticky", then user must own 62715798Smckusick * the directory, or the file in it, else he 62815798Smckusick * may not delete it (unless he's root). This 62915798Smckusick * implements append-only directories. 63015798Smckusick */ 63116688Smckusick if ((ndp->ni_pdir->i_mode & ISVTX) && 63215798Smckusick u.u_uid != 0 && 63316688Smckusick u.u_uid != ndp->ni_pdir->i_uid && 63415798Smckusick dp->i_uid != u.u_uid) { 63516688Smckusick iput(ndp->ni_pdir); 63615798Smckusick u.u_error = EPERM; 63715798Smckusick goto bad; 63815798Smckusick } 6399166Ssam } 6409166Ssam } 64116681Smckusick nbp->av_forw = freenamebuf; 64216681Smckusick freenamebuf = nbp; 6437534Sroot return (dp); 6447534Sroot } 6457534Sroot 6467534Sroot /* 6477534Sroot * Special handling for ".." allowing chdir out of mounted 6487534Sroot * file system: indirect .. in root inode to reevaluate 6497534Sroot * in directory file system was mounted on. 6507534Sroot */ 65116658Smckusick if (isdotdot) { 65218109Smckusick if (dp == u.u_rdir) { 65316688Smckusick ndp->ni_dent.d_ino = dp->i_number; 65418109Smckusick makeentry = 0; 65518109Smckusick } else if (ndp->ni_dent.d_ino == ROOTINO && 6567534Sroot dp->i_number == ROOTINO) { 6577534Sroot for (i = 1; i < NMOUNT; i++) 6587534Sroot if (mount[i].m_bufp != NULL && 6597534Sroot mount[i].m_dev == dp->i_dev) { 6606571Smckusic iput(dp); 6617534Sroot dp = mount[i].m_inodp; 66216666Smckusick ILOCK(dp); 6635972Swnj dp->i_count++; 6647534Sroot fs = dp->i_fs; 6657534Sroot cp -= 2; /* back over .. */ 6667534Sroot goto dirloop2; 6675972Swnj } 66830Sbill } 6697534Sroot } 6707534Sroot 6717534Sroot /* 6729166Ssam * If rewriting (rename), return the inode and the 6739166Ssam * information required to rewrite the present directory 6749166Ssam * Must get inode of directory entry to verify it's a 6759166Ssam * regular file, or empty directory. 6769166Ssam */ 6779166Ssam if ((flag == CREATE && lockparent) && *cp == 0) { 6789166Ssam if (access(dp, IWRITE)) 6799166Ssam goto bad; 68016688Smckusick ndp->ni_pdir = dp; /* for dirrewrite() */ 6819166Ssam /* 6829166Ssam * Careful about locking second inode. 6839166Ssam * This can only occur if the target is ".". 6849166Ssam */ 68516688Smckusick if (dp->i_number == ndp->ni_dent.d_ino) { 6869166Ssam u.u_error = EISDIR; /* XXX */ 6879166Ssam goto bad; 6889166Ssam } 68916688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 6909166Ssam if (dp == NULL) { 69116688Smckusick iput(ndp->ni_pdir); 6929166Ssam goto bad; 6939166Ssam } 69416681Smckusick nbp->av_forw = freenamebuf; 69516681Smckusick freenamebuf = nbp; 6969166Ssam return (dp); 6979166Ssam } 6989166Ssam 6999166Ssam /* 70012011Smckusick * Check for symbolic link, which may require us to massage the 70112011Smckusick * name before we continue translation. We do not `iput' the 70212011Smckusick * directory because we may need it again if the symbolic link 70312011Smckusick * is relative to the current directory. Instead we save it 70412011Smckusick * unlocked as "pdp". We must get the target inode before unlocking 70512011Smckusick * the directory to insure that the inode will not be removed 70612011Smckusick * before we get it. We prevent deadlock by always fetching 70712011Smckusick * inodes from the root, moving down the directory tree. Thus 70812011Smckusick * when following backward pointers ".." we must unlock the 70912011Smckusick * parent directory before getting the requested directory. 71012011Smckusick * There is a potential race condition here if both the current 71112011Smckusick * and parent directories are removed before the `iget' for the 71212011Smckusick * inode associated with ".." returns. We hope that this occurs 71312011Smckusick * infrequently since we cannot avoid this race condition without 71412492Ssam * implementing a sophisticated deadlock detection algorithm. 71512011Smckusick * Note also that this simple deadlock detection scheme will not 71612011Smckusick * work if the file system has any hard links other than ".." 71712011Smckusick * that point backwards in the directory structure. 7187534Sroot */ 7197534Sroot pdp = dp; 72015798Smckusick if (isdotdot) { 72116666Smckusick IUNLOCK(pdp); /* race to get the inode */ 72216688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 72312011Smckusick if (dp == NULL) 72412011Smckusick goto bad2; 72516688Smckusick } else if (dp->i_number == ndp->ni_dent.d_ino) { 72612011Smckusick dp->i_count++; /* we want ourself, ie "." */ 72712011Smckusick } else { 72816688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 72916666Smckusick IUNLOCK(pdp); 73012011Smckusick if (dp == NULL) 73112011Smckusick goto bad2; 73212011Smckusick } 73315798Smckusick 73415798Smckusick /* 735*21014Smckusick * insert name into cache if appropriate 73615798Smckusick */ 73718109Smckusick if (makeentry) { 73815798Smckusick if (ncp != NULL) 73915798Smckusick panic("nami: duplicating cache"); 74015798Smckusick 74115798Smckusick /* 74215798Smckusick * free the cache slot at head of lru chain 74315798Smckusick */ 74415798Smckusick if (ncp = nchhead) { 74515798Smckusick /* remove from lru chain */ 74615798Smckusick *ncp->nc_prev = ncp->nc_nxt; 74715798Smckusick if (ncp->nc_nxt) 74815798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 74915798Smckusick else 75015798Smckusick nchtail = ncp->nc_prev; 75115798Smckusick 75215798Smckusick /* remove from old hash chain */ 75315798Smckusick remque(ncp); 75415798Smckusick 75515798Smckusick /* grab the inode we just found */ 75615798Smckusick ncp->nc_ip = dp; 75715798Smckusick 75815798Smckusick /* fill in cache info */ 75915798Smckusick ncp->nc_ino = pdp->i_number; /* parents inum */ 76015798Smckusick ncp->nc_dev = pdp->i_dev; /* & device */ 76115798Smckusick ncp->nc_idev = dp->i_dev; /* our device */ 76216643Ssam ncp->nc_id = dp->i_id; /* identifier */ 76316688Smckusick ncp->nc_nlen = ndp->ni_dent.d_namlen; 76416688Smckusick bcopy(ndp->ni_dent.d_name, ncp->nc_name, ncp->nc_nlen); 76515798Smckusick 76615798Smckusick /* link at end of lru chain */ 76715798Smckusick ncp->nc_nxt = NULL; 76815798Smckusick ncp->nc_prev = nchtail; 76915798Smckusick *nchtail = ncp; 77015798Smckusick nchtail = &ncp->nc_nxt; 77115798Smckusick 77215798Smckusick /* and insert on hash chain */ 77315798Smckusick insque(ncp, nhp); 77415798Smckusick } 77515798Smckusick } 77615798Smckusick 77715798Smckusick haveino: 7787534Sroot fs = dp->i_fs; 7797534Sroot 7807534Sroot /* 7817534Sroot * Check for symbolic link 7827534Sroot */ 78316688Smckusick if ((dp->i_mode & IFMT) == IFLNK && 78416688Smckusick ((ndp->ni_nameiop & FOLLOW) || *cp == '/')) { 7857825Sroot u_int pathlen = strlen(cp) + 1; 7867534Sroot 787*21014Smckusick if (dp->i_size + pathlen >= MAXPATHLEN - 1) { 788*21014Smckusick u.u_error = ENAMETOOLONG; 789*21014Smckusick goto bad2; 790*21014Smckusick } 791*21014Smckusick if (++nlink > MAXSYMLINKS) { 7927534Sroot u.u_error = ELOOP; 7937534Sroot goto bad2; 7947534Sroot } 7958957Sroot ovbcopy(cp, nbp->b_un.b_addr + dp->i_size, pathlen); 7967751Sroot u.u_error = 7979166Ssam rdwri(UIO_READ, dp, nbp->b_un.b_addr, (int)dp->i_size, 7987825Sroot 0, 1, (int *)0); 7997534Sroot if (u.u_error) 8007534Sroot goto bad2; 8017534Sroot cp = nbp->b_un.b_addr; 8027534Sroot iput(dp); 8035972Swnj if (*cp == '/') { 8047534Sroot irele(pdp); 8055972Swnj while (*cp == '/') 8065972Swnj cp++; 8077534Sroot if ((dp = u.u_rdir) == NULL) 8087534Sroot dp = rootdir; 80916666Smckusick ILOCK(dp); 8107534Sroot dp->i_count++; 8117534Sroot } else { 8127534Sroot dp = pdp; 81316666Smckusick ILOCK(dp); 8145972Swnj } 8157534Sroot fs = dp->i_fs; 8167534Sroot goto dirloop; 81730Sbill } 8187534Sroot 81930Sbill /* 8207534Sroot * Not a symbolic link. If more pathname, 8217534Sroot * continue at next component, else return. 82230Sbill */ 8237534Sroot if (*cp == '/') { 8247534Sroot while (*cp == '/') 8257534Sroot cp++; 8269166Ssam irele(pdp); 8277534Sroot goto dirloop; 82830Sbill } 82916681Smckusick nbp->av_forw = freenamebuf; 83016681Smckusick freenamebuf = nbp; 8319166Ssam if (lockparent) 83216688Smckusick ndp->ni_pdir = pdp; 8339166Ssam else 8349166Ssam irele(pdp); 8357534Sroot return (dp); 8367534Sroot bad2: 8377534Sroot irele(pdp); 8387534Sroot bad: 8397534Sroot if (bp) 8407534Sroot brelse(bp); 8417534Sroot if (dp) 8427534Sroot iput(dp); 84316681Smckusick nbp->av_forw = freenamebuf; 84416681Smckusick freenamebuf = nbp; 8456571Smckusic return (NULL); 84630Sbill } 84730Sbill 84815798Smckusick 84916688Smckusick dirbad(ip, offset, how) 8507534Sroot struct inode *ip; 85116688Smckusick off_t offset; 8527534Sroot char *how; 8537534Sroot { 8547534Sroot 8557534Sroot printf("%s: bad dir ino %d at offset %d: %s\n", 85616688Smckusick ip->i_fs->fs_fsmnt, ip->i_number, offset, how); 8577534Sroot } 8587534Sroot 85916657Smckusick /* 86016657Smckusick * Do consistency checking on a directory entry: 86116657Smckusick * record length must be multiple of 4 86216657Smckusick * record length must not be non-negative 86316657Smckusick * entry must fit in rest of its DIRBLKSIZ block 86416657Smckusick * record must be large enough to contain entry 86516657Smckusick * name is not longer than MAXNAMLEN 86616657Smckusick * name must be as long as advertised, and null terminated 86716657Smckusick */ 86816657Smckusick dirbadentry(ep, entryoffsetinblock) 8697534Sroot register struct direct *ep; 87016657Smckusick int entryoffsetinblock; 8717534Sroot { 8727534Sroot register int i; 8737534Sroot 87416657Smckusick if ((ep->d_reclen & 0x3) != 0 || ep->d_reclen <= 0 || 87516657Smckusick ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 87616657Smckusick ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN) 87716657Smckusick return (1); 8787534Sroot for (i = 0; i < ep->d_namlen; i++) 87916688Smckusick if (ep->d_name[i] == '\0') 8807534Sroot return (1); 8817534Sroot return (ep->d_name[i]); 8827534Sroot } 8837534Sroot 88430Sbill /* 8857534Sroot * Write a directory entry after a call to namei, using the parameters 8867534Sroot * which it left in the u. area. The argument ip is the inode which 88716688Smckusick * the new directory entry will refer to. The u. area field ndp->ni_pdir is 8887534Sroot * a pointer to the directory to be written, which was left locked by 88916688Smckusick * namei. Remaining parameters (ndp->ni_offset, ndp->ni_count) indicate 8907534Sroot * how the space for the new entry is to be gotten. 8917534Sroot */ 89216688Smckusick direnter(ip, ndp) 8937534Sroot struct inode *ip; 89416688Smckusick register struct nameidata *ndp; 8955972Swnj { 8967534Sroot register struct direct *ep, *nep; 89718027Smckusick register struct inode *dp = ndp->ni_pdir; 8987534Sroot struct buf *bp; 89911639Ssam int loc, spacefree, error = 0; 9008631Sroot u_int dsize; 9018631Sroot int newentrysize; 9027534Sroot char *dirbuf; 9035972Swnj 90416688Smckusick ndp->ni_dent.d_ino = ip->i_number; 90516688Smckusick newentrysize = DIRSIZ(&ndp->ni_dent); 90616688Smckusick if (ndp->ni_count == 0) { 9077534Sroot /* 90816688Smckusick * If ndp->ni_count is 0, then namei could find no space in the 90916688Smckusick * directory. In this case ndp->ni_offset will be on a directory 9107534Sroot * block boundary and we will write the new entry into a fresh 9117534Sroot * block. 9127534Sroot */ 91316688Smckusick if (ndp->ni_offset&(DIRBLKSIZ-1)) 9147534Sroot panic("wdir: newblk"); 91516688Smckusick ndp->ni_dent.d_reclen = DIRBLKSIZ; 91618027Smckusick error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 91716688Smckusick newentrysize, ndp->ni_offset, 1, (int *)0); 91818104Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 91918104Smckusick panic("wdir: blksize"); /* XXX - should grow w/bmap() */ 92018104Smckusick else 92118104Smckusick dp->i_size = roundup(dp->i_size, DIRBLKSIZ); 92218027Smckusick iput(dp); 92310849Ssam return (error); 9247534Sroot } 9257534Sroot 9267534Sroot /* 92716688Smckusick * If ndp->ni_count is non-zero, then namei found space for the new 92816688Smckusick * entry in the range ndp->ni_offset to ndp->ni_offset + ndp->ni_count. 9297534Sroot * in the directory. To use this space, we may have to compact 9307534Sroot * the entries located there, by copying them together towards 9317534Sroot * the beginning of the block, leaving the free space in 9327534Sroot * one usable chunk at the end. 9337534Sroot */ 9347534Sroot 9357534Sroot /* 9367534Sroot * Increase size of directory if entry eats into new space. 9377534Sroot * This should never push the size past a new multiple of 9387534Sroot * DIRBLKSIZE. 93918104Smckusick * 94018104Smckusick * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 9417534Sroot */ 94218027Smckusick if (ndp->ni_offset + ndp->ni_count > dp->i_size) 94318027Smckusick dp->i_size = ndp->ni_offset + ndp->ni_count; 9447534Sroot 9457534Sroot /* 9467534Sroot * Get the block containing the space for the new directory 94710849Ssam * entry. Should return error by result instead of u.u_error. 9487534Sroot */ 94918027Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)&dirbuf); 9509166Ssam if (bp == 0) { 95118027Smckusick iput(dp); 95210849Ssam return (u.u_error); 9539166Ssam } 9547534Sroot 9557534Sroot /* 9567534Sroot * Find space for the new entry. In the simple case, the 9577534Sroot * entry at offset base will have the space. If it does 9587534Sroot * not, then namei arranged that compacting the region 95916688Smckusick * ndp->ni_offset to ndp->ni_offset+ndp->ni_count would yield the space. 9607534Sroot */ 9617534Sroot ep = (struct direct *)dirbuf; 9627534Sroot dsize = DIRSIZ(ep); 96311639Ssam spacefree = ep->d_reclen - dsize; 96416688Smckusick for (loc = ep->d_reclen; loc < ndp->ni_count; ) { 9657534Sroot nep = (struct direct *)(dirbuf + loc); 9667534Sroot if (ep->d_ino) { 9677534Sroot /* trim the existing slot */ 9687534Sroot ep->d_reclen = dsize; 9697534Sroot ep = (struct direct *)((char *)ep + dsize); 9707534Sroot } else { 9717534Sroot /* overwrite; nothing there; header is ours */ 97211639Ssam spacefree += dsize; 9737534Sroot } 9747534Sroot dsize = DIRSIZ(nep); 97511639Ssam spacefree += nep->d_reclen - dsize; 9767534Sroot loc += nep->d_reclen; 9777825Sroot bcopy((caddr_t)nep, (caddr_t)ep, dsize); 9787534Sroot } 9797534Sroot /* 9807534Sroot * Update the pointer fields in the previous entry (if any), 9817534Sroot * copy in the new entry, and write out the block. 9827534Sroot */ 9837534Sroot if (ep->d_ino == 0) { 98411639Ssam if (spacefree + dsize < newentrysize) 9857534Sroot panic("wdir: compact1"); 98616688Smckusick ndp->ni_dent.d_reclen = spacefree + dsize; 9877534Sroot } else { 98811639Ssam if (spacefree < newentrysize) 9897534Sroot panic("wdir: compact2"); 99016688Smckusick ndp->ni_dent.d_reclen = spacefree; 9917534Sroot ep->d_reclen = dsize; 9927534Sroot ep = (struct direct *)((char *)ep + dsize); 9937534Sroot } 99416688Smckusick bcopy((caddr_t)&ndp->ni_dent, (caddr_t)ep, (u_int)newentrysize); 9957534Sroot bwrite(bp); 99618027Smckusick dp->i_flag |= IUPD|ICHG; 99718027Smckusick if (ndp->ni_endoff && ndp->ni_endoff < dp->i_size) 99818027Smckusick itrunc(dp, ndp->ni_endoff); 99918027Smckusick iput(dp); 100010849Ssam return (error); 10015972Swnj } 10026571Smckusic 10039166Ssam /* 10049166Ssam * Remove a directory entry after a call to namei, using the 10059166Ssam * parameters which it left in the u. area. The u. entry 100616688Smckusick * ni_offset contains the offset into the directory of the 100716688Smckusick * entry to be eliminated. The ni_count field contains the 10089166Ssam * size of the previous record in the directory. If this 10099166Ssam * is 0, the first entry is being deleted, so we need only 10109166Ssam * zero the inode number to mark the entry as free. If the 10119166Ssam * entry isn't the first in the directory, we must reclaim 10129166Ssam * the space of the now empty record by adding the record size 10139166Ssam * to the size of the previous entry. 10149166Ssam */ 101516688Smckusick dirremove(ndp) 101616688Smckusick register struct nameidata *ndp; 10176571Smckusic { 101816688Smckusick register struct inode *dp = ndp->ni_pdir; 10197534Sroot register struct buf *bp; 10207534Sroot struct direct *ep; 10216571Smckusic 102216688Smckusick if (ndp->ni_count == 0) { 10237534Sroot /* 10247534Sroot * First entry in block: set d_ino to zero. 10257534Sroot */ 102616688Smckusick ndp->ni_dent.d_ino = 0; 102716688Smckusick (void) rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 102816688Smckusick (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0); 10299269Ssam } else { 10307534Sroot /* 10317534Sroot * Collapse new free space into previous entry. 10327534Sroot */ 103316688Smckusick bp = blkatoff(dp, (int)(ndp->ni_offset - ndp->ni_count), 103416688Smckusick (char **)&ep); 10357534Sroot if (bp == 0) 10367534Sroot return (0); 103716688Smckusick ep->d_reclen += ndp->ni_dent.d_reclen; 10387534Sroot bwrite(bp); 10397534Sroot dp->i_flag |= IUPD|ICHG; 10407534Sroot } 10417534Sroot return (1); 10426571Smckusic } 10437534Sroot 10447605Ssam /* 10459166Ssam * Rewrite an existing directory entry to point at the inode 10469166Ssam * supplied. The parameters describing the directory entry are 10479166Ssam * set up by a call to namei. 10489166Ssam */ 104916688Smckusick dirrewrite(dp, ip, ndp) 10509166Ssam struct inode *dp, *ip; 105116688Smckusick struct nameidata *ndp; 10529166Ssam { 10539166Ssam 105416688Smckusick ndp->ni_dent.d_ino = ip->i_number; 105516688Smckusick u.u_error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 105616688Smckusick (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0); 10579166Ssam iput(dp); 10589166Ssam } 10599166Ssam 10609166Ssam /* 10617605Ssam * Return buffer with contents of block "offset" 10627605Ssam * from the beginning of directory "ip". If "res" 10637605Ssam * is non-zero, fill it in with a pointer to the 10647605Ssam * remaining space in the directory. 10657605Ssam */ 10667534Sroot struct buf * 10677605Ssam blkatoff(ip, offset, res) 10687534Sroot struct inode *ip; 10697534Sroot off_t offset; 10707534Sroot char **res; 10717534Sroot { 10727534Sroot register struct fs *fs = ip->i_fs; 10738672Sroot daddr_t lbn = lblkno(fs, offset); 10747534Sroot int bsize = blksize(fs, ip, lbn); 10757534Sroot register struct buf *bp; 107618663Smckusick daddr_t bn; 10777534Sroot 107818663Smckusick bn = bmap(ip, lbn, B_READ, bsize); 10797534Sroot if (u.u_error) 10807534Sroot return (0); 108118663Smckusick if (bn == (daddr_t)-1) { 108218663Smckusick dirbad(ip, offset, "hole in dir"); 108318663Smckusick return (0); 108418663Smckusick } 108518663Smckusick bp = bread(ip->i_dev, fsbtodb(fs, bn), bsize); 10867534Sroot if (bp->b_flags & B_ERROR) { 10877534Sroot brelse(bp); 10887534Sroot return (0); 10897534Sroot } 10907534Sroot if (res) 109118663Smckusick *res = bp->b_un.b_addr + blkoff(fs, offset); 10927534Sroot return (bp); 10937534Sroot } 10949166Ssam 10959166Ssam /* 10969166Ssam * Check if a directory is empty or not. 10979166Ssam * Inode supplied must be locked. 109812817Ssam * 109912817Ssam * Using a struct dirtemplate here is not precisely 110012817Ssam * what we want, but better than using a struct direct. 110112817Ssam * 110212817Ssam * NB: does not handle corrupted directories. 11039166Ssam */ 110416777Smckusick dirempty(ip, parentino) 11059863Ssam register struct inode *ip; 110616777Smckusick ino_t parentino; 11079166Ssam { 11089166Ssam register off_t off; 110912817Ssam struct dirtemplate dbuf; 111012817Ssam register struct direct *dp = (struct direct *)&dbuf; 11119863Ssam int error, count; 111212817Ssam #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 11139166Ssam 11149166Ssam for (off = 0; off < ip->i_size; off += dp->d_reclen) { 111518028Smckusick if (dp->d_reclen <= 0) 111618028Smckusick return (0); 111712817Ssam error = rdwri(UIO_READ, ip, (caddr_t)dp, MINDIRSIZ, 111812817Ssam off, 1, &count); 111912817Ssam /* 112012817Ssam * Since we read MINDIRSIZ, residual must 112112817Ssam * be 0 unless we're at end of file. 112212817Ssam */ 112312817Ssam if (error || count != 0) 11249166Ssam return (0); 112512817Ssam /* skip empty entries */ 11269166Ssam if (dp->d_ino == 0) 11279166Ssam continue; 112812817Ssam /* accept only "." and ".." */ 112912817Ssam if (dp->d_namlen > 2) 113012817Ssam return (0); 11319166Ssam if (dp->d_name[0] != '.') 11329166Ssam return (0); 113312817Ssam /* 113412817Ssam * At this point d_namlen must be 1 or 2. 113512817Ssam * 1 implies ".", 2 implies ".." if second 113612817Ssam * char is also "." 113712817Ssam */ 113816777Smckusick if (dp->d_namlen == 1) 11399166Ssam continue; 114016777Smckusick if (dp->d_name[1] == '.' && dp->d_ino == parentino) 114116777Smckusick continue; 11429166Ssam return (0); 11439166Ssam } 11449166Ssam return (1); 11459166Ssam } 114612815Smckusick 114712815Smckusick /* 114812815Smckusick * Check if source directory is in the path of the target directory. 114912815Smckusick * Target is supplied locked, source is unlocked. 115012815Smckusick * The target is always iput() before returning. 115112815Smckusick */ 115212815Smckusick checkpath(source, target) 115312815Smckusick struct inode *source, *target; 115412815Smckusick { 115512815Smckusick struct dirtemplate dirbuf; 115612815Smckusick register struct inode *ip; 115712815Smckusick int error = 0; 115812815Smckusick 115912815Smckusick ip = target; 116012815Smckusick if (ip->i_number == source->i_number) { 116112815Smckusick error = EEXIST; 116212815Smckusick goto out; 116312815Smckusick } 116412815Smckusick if (ip->i_number == ROOTINO) 116512815Smckusick goto out; 116612815Smckusick 116712815Smckusick for (;;) { 116812815Smckusick if ((ip->i_mode&IFMT) != IFDIR) { 116912815Smckusick error = ENOTDIR; 117012815Smckusick break; 117112815Smckusick } 117212815Smckusick error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf, 117312815Smckusick sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 117412815Smckusick if (error != 0) 117512815Smckusick break; 117612815Smckusick if (dirbuf.dotdot_namlen != 2 || 117716658Smckusick dirbuf.dotdot_name[0] != '.' || 117816658Smckusick dirbuf.dotdot_name[1] != '.') { 117912815Smckusick error = ENOTDIR; 118012815Smckusick break; 118112815Smckusick } 118212815Smckusick if (dirbuf.dotdot_ino == source->i_number) { 118312815Smckusick error = EINVAL; 118412815Smckusick break; 118512815Smckusick } 118612815Smckusick if (dirbuf.dotdot_ino == ROOTINO) 118712815Smckusick break; 118812815Smckusick iput(ip); 118912815Smckusick ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino); 119012815Smckusick if (ip == NULL) { 119112815Smckusick error = u.u_error; 119212815Smckusick break; 119312815Smckusick } 119412815Smckusick } 119512815Smckusick 119612815Smckusick out: 119712815Smckusick if (error == ENOTDIR) 119812815Smckusick printf("checkpath: .. not a directory\n"); 119912815Smckusick if (ip != NULL) 120012815Smckusick iput(ip); 120112815Smckusick return (error); 120212815Smckusick } 120315798Smckusick 120415798Smckusick /* 120515798Smckusick * Name cache initialization, from main() when we are booting 120615798Smckusick */ 120715798Smckusick nchinit() 120815798Smckusick { 120915798Smckusick register union nchash *nchp; 121015798Smckusick register struct nch *ncp; 121115798Smckusick 121215798Smckusick nchhead = 0; 121315798Smckusick nchtail = &nchhead; 121415798Smckusick 121515798Smckusick for (ncp = nch; ncp < &nch[nchsize]; ncp++) { 121615798Smckusick ncp->nc_forw = ncp; /* hash chain */ 121715798Smckusick ncp->nc_back = ncp; 121815798Smckusick 121915798Smckusick ncp->nc_nxt = NULL; /* lru chain */ 122015798Smckusick *nchtail = ncp; 122115798Smckusick ncp->nc_prev = nchtail; 122215798Smckusick nchtail = &ncp->nc_nxt; 122315798Smckusick 122415798Smckusick /* all else is zero already */ 122515798Smckusick } 122615798Smckusick 122715798Smckusick for (nchp = nchash; nchp < &nchash[NCHHASH]; nchp++) { 122815798Smckusick nchp->nch_head[0] = nchp; 122915798Smckusick nchp->nch_head[1] = nchp; 123015798Smckusick } 123115798Smckusick } 123215798Smckusick 123315798Smckusick /* 123415798Smckusick * Cache flush, called when filesys is umounted to 123515798Smckusick * remove entries that would now be invalid 123615798Smckusick * 123715798Smckusick * The line "nxtcp = nchhead" near the end is to avoid potential problems 123815798Smckusick * if the cache lru chain is modified while we are dumping the 123915798Smckusick * inode. This makes the algorithm O(n^2), but do you think I care? 124015798Smckusick */ 124115798Smckusick nchinval(dev) 124215798Smckusick register dev_t dev; 124315798Smckusick { 124415798Smckusick register struct nch *ncp, *nxtcp; 124515798Smckusick 124615798Smckusick for (ncp = nchhead; ncp; ncp = nxtcp) { 124715798Smckusick nxtcp = ncp->nc_nxt; 124815798Smckusick 124915798Smckusick if (ncp->nc_ip == NULL || 125015798Smckusick (ncp->nc_idev != dev && ncp->nc_dev != dev)) 125115798Smckusick continue; 125215798Smckusick 125316658Smckusick /* free the resources we had */ 125415798Smckusick ncp->nc_idev = NODEV; 125515798Smckusick ncp->nc_dev = NODEV; 125616658Smckusick ncp->nc_id = NULL; 125715798Smckusick ncp->nc_ino = 0; 125816658Smckusick ncp->nc_ip = NULL; 125915798Smckusick 126016658Smckusick 126115798Smckusick /* remove the entry from its hash chain */ 126215798Smckusick remque(ncp); 126315798Smckusick /* and make a dummy one */ 126415798Smckusick ncp->nc_forw = ncp; 126515798Smckusick ncp->nc_back = ncp; 126615798Smckusick 126715798Smckusick /* delete this entry from LRU chain */ 126815798Smckusick *ncp->nc_prev = nxtcp; 126915798Smckusick if (nxtcp) 127015798Smckusick nxtcp->nc_prev = ncp->nc_prev; 127115798Smckusick else 127215798Smckusick nchtail = ncp->nc_prev; 127315798Smckusick 127415798Smckusick /* cause rescan of list, it may have altered */ 127515798Smckusick nxtcp = nchhead; 127615798Smckusick /* put the now-free entry at head of LRU */ 127715798Smckusick ncp->nc_nxt = nxtcp; 127815798Smckusick ncp->nc_prev = &nchhead; 127915798Smckusick nxtcp->nc_prev = &ncp->nc_nxt; 128015798Smckusick nchhead = ncp; 128115798Smckusick } 128215798Smckusick } 128317704Smckusick 128417704Smckusick /* 128517704Smckusick * Name cache invalidation of all entries. 128617704Smckusick */ 128717704Smckusick cacheinvalall() 128817704Smckusick { 128917704Smckusick register struct nch *ncp; 129017704Smckusick 129117704Smckusick for (ncp = nch; ncp < &nch[nchsize]; ncp++) { 129217704Smckusick ncp->nc_id = 0; 129317704Smckusick } 129417704Smckusick } 1295