1*16688Smckusick /* vfs_lookup.c 6.12 84/07/08 */ 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" 1315660Smckusick #include "../h/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 * 45*16688Smckusick * The segflg defines whether the name is to be copied from user 46*16688Smckusick * 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 * 58*16688Smckusick * 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: 90*16688Smckusick * 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 * 113*16688Smckusick namei(ndp) 114*16688Smckusick 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 */ 134*16688Smckusick 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 */ 137*16688Smckusick int error, 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 ".." */ 143*16688Smckusick int flag; /* op ie, LOOKUP, CREATE, or DELETE */ 14430Sbill 145*16688Smckusick lockparent = ndp->ni_nameiop & LOCKPARENT; 146*16688Smckusick docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE; 147*16688Smckusick flag = ndp->ni_nameiop &~ (LOCKPARENT|NOCACHE|FOLLOW); 14815798Smckusick if (flag == DELETE) 14915798Smckusick docache = 0; 15030Sbill /* 1517534Sroot * Get a buffer for the name to be translated, and copy the 1527534Sroot * name into the buffer. 1535972Swnj */ 15416681Smckusick nbp = freenamebuf; 15516681Smckusick if (nbp == NULL) 15616681Smckusick nbp = geteblk(MAXPATHLEN); 15716681Smckusick else 15816681Smckusick freenamebuf = nbp->av_forw; 159*16688Smckusick if (ndp->ni_segflg == UIO_SYSSPACE) 160*16688Smckusick error = copystr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN); 161*16688Smckusick else 162*16688Smckusick error = copyinstr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN); 163*16688Smckusick if (error) { 164*16688Smckusick u.u_error = error; 165*16688Smckusick goto bad; 1665972Swnj } 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; 18016666Smckusick ILOCK(dp); 1815972Swnj dp->i_count++; 182*16688Smckusick ndp->ni_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 /* 202*16688Smckusick * Copy next component of name to ndp->ni_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 } 210*16688Smckusick if ((*cp&0377) == ('/'|0200) || (*cp&0200) && flag != DELETE) { 211*16688Smckusick u.u_error = EPERM; 212*16688Smckusick goto bad; 213*16688Smckusick } 214*16688Smckusick ndp->ni_dent.d_name[i++] = *cp; 21515798Smckusick hash += (unsigned char)*cp * i; 2165972Swnj } 217*16688Smckusick ndp->ni_dent.d_namlen = i; 218*16688Smckusick ndp->ni_dent.d_name[i] = '\0'; 21916658Smckusick isdotdot = (i == 2 && 220*16688Smckusick ndp->ni_dent.d_name[0] == '.' && ndp->ni_dent.d_name[1] == '.'); 2217534Sroot 2227534Sroot /* 2237534Sroot * Check for degenerate name (e.g. / or "") 2247534Sroot * which is a way of talking about a directory, 2257534Sroot * e.g. like "/." or ".". 2267534Sroot */ 227*16688Smckusick if (ndp->ni_dent.d_name[0] == '\0') { 22815798Smckusick if (flag != LOOKUP || lockparent) { 22914937Smckusick u.u_error = EISDIR; 2307534Sroot goto bad; 2315972Swnj } 23216681Smckusick nbp->av_forw = freenamebuf; 23316681Smckusick freenamebuf = nbp; 2346571Smckusic return (dp); 2355972Swnj } 2367534Sroot 2376571Smckusic /* 23815798Smckusick * We now have a segment name to search for, and a directory to search. 23915798Smckusick * 24015798Smckusick * Before tediously performing a linear scan of the directory, 24115798Smckusick * check the name cache to see if the directory/name pair 24215798Smckusick * we are looking for is known already. We don't do this 24315798Smckusick * if the segment name is long, simply so the cache can avoid 24415798Smckusick * holding long names (which would either waste space, or 24515798Smckusick * add greatly to the complexity). 24615798Smckusick */ 247*16688Smckusick if (ndp->ni_dent.d_namlen > NCHNAMLEN) { 24815798Smckusick nchstats.ncs_long++; 24915798Smckusick docache = 0; 25015798Smckusick } else { 25115798Smckusick nhp = &nchash[NHASH(hash, dp->i_number, dp->i_dev)]; 25215798Smckusick for (ncp = nhp->nch_forw; ncp != (struct nch *)nhp; 25315798Smckusick ncp = ncp->nc_forw) { 25415798Smckusick if (ncp->nc_ino == dp->i_number && 25515798Smckusick ncp->nc_dev == dp->i_dev && 256*16688Smckusick ncp->nc_nlen == ndp->ni_dent.d_namlen && 257*16688Smckusick !bcmp(ncp->nc_name, ndp->ni_dent.d_name, 258*16688Smckusick ncp->nc_nlen)) 25915798Smckusick break; 26015798Smckusick } 26115798Smckusick 26215798Smckusick if (ncp == (struct nch *)nhp) { 26315798Smckusick nchstats.ncs_miss++; 26415798Smckusick ncp = NULL; 26515798Smckusick } else { 26616658Smckusick if (ncp->nc_id != ncp->nc_ip->i_id) { 26716643Ssam nchstats.ncs_falsehits++; 26816658Smckusick } else if (*cp == '\0' && !docache) { 26916658Smckusick nchstats.ncs_badhits++; 27016658Smckusick } else { 27115798Smckusick 27215798Smckusick /* 27315798Smckusick * move this slot to end of LRU 27415798Smckusick * chain, if not already there 27515798Smckusick */ 27615798Smckusick if (ncp->nc_nxt) { 27715798Smckusick /* remove from LRU chain */ 27815798Smckusick *ncp->nc_prev = ncp->nc_nxt; 27915798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 28015798Smckusick 28115798Smckusick /* and replace at end of it */ 28215798Smckusick ncp->nc_nxt = NULL; 28315798Smckusick ncp->nc_prev = nchtail; 28415798Smckusick *nchtail = ncp; 28515798Smckusick nchtail = &ncp->nc_nxt; 28615798Smckusick } 28715798Smckusick 28816658Smckusick /* 28916658Smckusick * Get the next inode in the path. 29016666Smckusick * See comment above other `IUNLOCK' code for 29116658Smckusick * an explaination of the locking protocol. 29216658Smckusick */ 29315798Smckusick pdp = dp; 29415798Smckusick dp = ncp->nc_ip; 29515798Smckusick if (dp == NULL) 29615798Smckusick panic("nami: null cache ino"); 29716643Ssam if (pdp == dp) 29816643Ssam dp->i_count++; 29916666Smckusick else { 30016658Smckusick if (isdotdot) { 30116666Smckusick IUNLOCK(pdp); 30216658Smckusick igrab(dp); 30316658Smckusick } else { 30416658Smckusick igrab(dp); 30516666Smckusick IUNLOCK(pdp); 30616658Smckusick } 30716643Ssam } 30815798Smckusick 30916658Smckusick /* 31016658Smckusick * Verify that the inode that we got 31116658Smckusick * did not change while we were waiting 31216658Smckusick * for it to be locked. 31316658Smckusick */ 31416658Smckusick if (ncp->nc_id != ncp->nc_ip->i_id) { 31516658Smckusick iput(dp); 31616666Smckusick ILOCK(pdp); 31716658Smckusick dp = pdp; 31816658Smckusick nchstats.ncs_falsehits++; 31916658Smckusick } else { 320*16688Smckusick ndp->ni_dent.d_ino = dp->i_number; 321*16688Smckusick /* ni_dent.d_reclen is garbage ... */ 32216658Smckusick nchstats.ncs_goodhits++; 32316658Smckusick goto haveino; 32416658Smckusick } 32516658Smckusick } 32615798Smckusick 32715798Smckusick /* 32816643Ssam * Last component and we are renaming or deleting, 32916643Ssam * the cache entry is invalid, or otherwise don't 33016643Ssam * want cache entry to exist. 33115798Smckusick */ 33215798Smckusick 33315798Smckusick /* remove from LRU chain */ 33415798Smckusick *ncp->nc_prev = ncp->nc_nxt; 33515798Smckusick if (ncp->nc_nxt) 33615798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 33715798Smckusick else 33815798Smckusick nchtail = ncp->nc_prev; 33915798Smckusick 34015798Smckusick /* remove from hash chain */ 34115798Smckusick remque(ncp); 34215798Smckusick 34315798Smckusick /* insert at head of LRU list (first to grab) */ 34415798Smckusick ncp->nc_nxt = nchhead; 34515798Smckusick ncp->nc_prev = &nchhead; 34615798Smckusick nchhead->nc_prev = &ncp->nc_nxt; 34715798Smckusick nchhead = ncp; 34815798Smckusick 34915798Smckusick /* and make a dummy hash chain */ 35015798Smckusick ncp->nc_forw = ncp; 35115798Smckusick ncp->nc_back = ncp; 35215798Smckusick 35315798Smckusick ncp = NULL; 35415798Smckusick } 35515798Smckusick } 35615798Smckusick 35715798Smckusick /* 3587534Sroot * Suppress search for slots unless creating 3597534Sroot * file and at end of pathname, in which case 3607534Sroot * we watch for a place to put the new file in 3617534Sroot * case it doesn't already exist. 3626571Smckusic */ 3637534Sroot slotstatus = FOUND; 3649166Ssam if (flag == CREATE && *cp == 0) { 3657534Sroot slotstatus = NONE; 3667534Sroot slotfreespace = 0; 367*16688Smckusick slotneeded = DIRSIZ(&ndp->ni_dent); 3687534Sroot } 36915660Smckusick /* 37015660Smckusick * If this is the same directory that this process 37115660Smckusick * previously searched, pick up where we last left off. 37215798Smckusick * We cache only lookups as these are the most common 37315660Smckusick * and have the greatest payoff. Caching CREATE has little 37415660Smckusick * benefit as it usually must search the entire directory 37515660Smckusick * to determine that the entry does not exist. Caching the 37615660Smckusick * location of the last DELETE has not reduced profiling time 37715660Smckusick * and hence has been removed in the interest of simplicity. 37815660Smckusick */ 37915660Smckusick if (flag != LOOKUP || dp->i_number != u.u_ncache.nc_inumber || 38015660Smckusick dp->i_dev != u.u_ncache.nc_dev) { 381*16688Smckusick ndp->ni_offset = 0; 38215660Smckusick numdirpasses = 1; 38315660Smckusick } else { 38415798Smckusick if ((dp->i_flag & ICHG) || dp->i_ctime >= u.u_ncache.nc_time) { 38515660Smckusick u.u_ncache.nc_prevoffset &= ~(DIRBLKSIZ - 1); 38615660Smckusick u.u_ncache.nc_time = time.tv_sec; 38715660Smckusick } 388*16688Smckusick ndp->ni_offset = u.u_ncache.nc_prevoffset; 389*16688Smckusick entryoffsetinblock = blkoff(fs, ndp->ni_offset); 39015660Smckusick if (entryoffsetinblock != 0) { 391*16688Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)0); 39215660Smckusick if (bp == 0) 39315660Smckusick goto bad; 39415660Smckusick } 39515660Smckusick numdirpasses = 2; 39615798Smckusick nchstats.ncs_2passes++; 39715660Smckusick } 39815660Smckusick endsearch = roundup(dp->i_size, DIRBLKSIZ); 3997534Sroot 40015660Smckusick searchloop: 401*16688Smckusick while (ndp->ni_offset < endsearch) { 4025972Swnj /* 4035972Swnj * If offset is on a block boundary, 4045972Swnj * read the next directory block. 4055972Swnj * Release previous if it exists. 4065972Swnj */ 407*16688Smckusick if (blkoff(fs, ndp->ni_offset) == 0) { 4085972Swnj if (bp != NULL) 4095972Swnj brelse(bp); 410*16688Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)0); 4117534Sroot if (bp == 0) 4127534Sroot goto bad; 4137534Sroot entryoffsetinblock = 0; 4145972Swnj } 4157534Sroot 4165972Swnj /* 4177534Sroot * If still looking for a slot, and at a DIRBLKSIZE 41816657Smckusick * boundary, have to start looking for free space again. 4196571Smckusic */ 4207534Sroot if (slotstatus == NONE && 4217534Sroot (entryoffsetinblock&(DIRBLKSIZ-1)) == 0) { 4227534Sroot slotoffset = -1; 4237534Sroot slotfreespace = 0; 4247534Sroot } 4257534Sroot 4267534Sroot /* 42716657Smckusick * Get pointer to next entry. 42816657Smckusick * Full validation checks are slow, so we only check 42916657Smckusick * enough to insure forward progress through the 43016657Smckusick * directory. Complete checks can be run by patching 43116657Smckusick * "dirchk" to be true. 4327534Sroot */ 4337534Sroot ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock); 43416657Smckusick if (ep->d_reclen <= 0 || 43516657Smckusick dirchk && dirbadentry(ep, entryoffsetinblock)) { 436*16688Smckusick dirbad(dp, ndp->ni_offset, "mangled entry"); 43716657Smckusick i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)); 438*16688Smckusick ndp->ni_offset += i; 4397534Sroot entryoffsetinblock += i; 4406571Smckusic continue; 4416571Smckusic } 4427534Sroot 4436571Smckusic /* 4447534Sroot * If an appropriate sized slot has not yet been found, 4456571Smckusic * check to see if one is available. Also accumulate space 4466571Smckusic * in the current block so that we can determine if 4476571Smckusic * compaction is viable. 4486571Smckusic */ 4497534Sroot if (slotstatus != FOUND) { 4507534Sroot int size = ep->d_reclen; 4517534Sroot 4526571Smckusic if (ep->d_ino != 0) 4536571Smckusic size -= DIRSIZ(ep); 4546571Smckusic if (size > 0) { 4557534Sroot if (size >= slotneeded) { 4567534Sroot slotstatus = FOUND; 457*16688Smckusick slotoffset = ndp->ni_offset; 4587534Sroot slotsize = ep->d_reclen; 4597534Sroot } else if (slotstatus == NONE) { 4607534Sroot slotfreespace += size; 4617534Sroot if (slotoffset == -1) 462*16688Smckusick slotoffset = ndp->ni_offset; 4637534Sroot if (slotfreespace >= slotneeded) { 4647534Sroot slotstatus = COMPACT; 465*16688Smckusick slotsize = ndp->ni_offset + 466*16688Smckusick ep->d_reclen - slotoffset; 4677534Sroot } 4686571Smckusic } 4696571Smckusic } 4706571Smckusic } 4717534Sroot 4726571Smckusic /* 4737534Sroot * Check for a name match. 4745972Swnj */ 4757534Sroot if (ep->d_ino) { 476*16688Smckusick if (ep->d_namlen == ndp->ni_dent.d_namlen && 477*16688Smckusick !bcmp(ndp->ni_dent.d_name, ep->d_name, 478*16688Smckusick ep->d_namlen)) 4797534Sroot goto found; 4807534Sroot } 481*16688Smckusick prevoff = ndp->ni_offset; 482*16688Smckusick ndp->ni_offset += ep->d_reclen; 4837534Sroot entryoffsetinblock += ep->d_reclen; 4847534Sroot } 48515798Smckusick /* notfound: */ 48615660Smckusick /* 48715798Smckusick * If we started in the middle of the directory and failed 48815660Smckusick * to find our target, we must check the beginning as well. 48915660Smckusick */ 49015660Smckusick if (numdirpasses == 2) { 49115660Smckusick numdirpasses--; 492*16688Smckusick ndp->ni_offset = 0; 49315660Smckusick endsearch = u.u_ncache.nc_prevoffset; 49415660Smckusick goto searchloop; 49515660Smckusick } 4967534Sroot /* 4977534Sroot * If creating, and at end of pathname and current 4989166Ssam * directory has not been removed, then can consider 4999166Ssam * allowing file to be created. 5007534Sroot */ 5019166Ssam if (flag == CREATE && *cp == 0 && dp->i_nlink != 0) { 5025972Swnj /* 5037534Sroot * Access for write is interpreted as allowing 5047534Sroot * creation of files in the directory. 5055972Swnj */ 5067534Sroot if (access(dp, IWRITE)) 5077534Sroot goto bad; 5085972Swnj /* 5097534Sroot * Return an indication of where the new directory 5107534Sroot * entry should be put. If we didn't find a slot, 511*16688Smckusick * then set ndp->ni_count to 0 indicating that the new 512*16688Smckusick * slot belongs at the end of the directory. If we found 513*16688Smckusick * a slot, then the new entry can be put in the range 514*16688Smckusick * [ndp->ni_offset .. ndp->ni_offset + ndp->ni_count) 5155972Swnj */ 51615660Smckusick if (slotstatus == NONE) { 517*16688Smckusick ndp->ni_offset = roundup(dp->i_size, DIRBLKSIZ); 518*16688Smckusick ndp->ni_count = 0; 51915660Smckusick } else { 520*16688Smckusick ndp->ni_offset = slotoffset; 521*16688Smckusick ndp->ni_count = slotsize; 5225972Swnj } 5237534Sroot dp->i_flag |= IUPD|ICHG; 5247534Sroot if (bp) 5257534Sroot brelse(bp); 52616681Smckusick nbp->av_forw = freenamebuf; 52716681Smckusick freenamebuf = nbp; 5285972Swnj /* 5297534Sroot * We return with the directory locked, so that 5307534Sroot * the parameters we set up above will still be 5317534Sroot * valid if we actually decide to do a direnter(). 5327534Sroot * We return NULL to indicate that the entry doesn't 5337534Sroot * currently exist, leaving a pointer to the (locked) 534*16688Smckusick * directory inode in ndp->ni_pdir. 5355972Swnj */ 536*16688Smckusick ndp->ni_pdir = dp; 5377534Sroot return (NULL); 5387534Sroot } 5397534Sroot u.u_error = ENOENT; 5407534Sroot goto bad; 5417534Sroot found: 54215798Smckusick if (numdirpasses == 2) 54315798Smckusick nchstats.ncs_pass2++; 5447534Sroot /* 5457534Sroot * Check that directory length properly reflects presence 5467534Sroot * of this entry. 5477534Sroot */ 5487605Ssam if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) { 549*16688Smckusick dirbad(dp, ndp->ni_offset, "i_size too small"); 5507605Ssam dp->i_size = entryoffsetinblock + DIRSIZ(ep); 5517534Sroot dp->i_flag |= IUPD|ICHG; 5527534Sroot } 5537534Sroot 5547534Sroot /* 55515660Smckusick * Found component in pathname. 55615798Smckusick * If the final component of path name, save information 55715660Smckusick * in the cache as to where the entry was found. 5587534Sroot */ 55915660Smckusick if (*cp == '\0' && flag == LOOKUP) { 560*16688Smckusick u.u_ncache.nc_prevoffset = ndp->ni_offset; 56115660Smckusick u.u_ncache.nc_inumber = dp->i_number; 56215660Smckusick u.u_ncache.nc_dev = dp->i_dev; 56315660Smckusick u.u_ncache.nc_time = time.tv_sec; 56415660Smckusick } 56515660Smckusick /* 566*16688Smckusick * Save directory entry in ndp->ni_dent, 56715660Smckusick * and release directory buffer. 56815660Smckusick */ 569*16688Smckusick bcopy((caddr_t)ep, (caddr_t)&ndp->ni_dent, (u_int)DIRSIZ(ep)); 5707534Sroot brelse(bp); 5717534Sroot bp = NULL; 5727534Sroot 5737534Sroot /* 5747534Sroot * If deleting, and at end of pathname, return 5757534Sroot * parameters which can be used to remove file. 5769166Ssam * If the lockparent flag isn't set, we return only 577*16688Smckusick * the directory (in ndp->ni_pdir), otherwise we go 5789166Ssam * on and lock the inode, being careful with ".". 5797534Sroot */ 5809166Ssam if (flag == DELETE && *cp == 0) { 5817534Sroot /* 5827534Sroot * Write access to directory required to delete files. 5837534Sroot */ 5847534Sroot if (access(dp, IWRITE)) 5857534Sroot goto bad; 586*16688Smckusick ndp->ni_pdir = dp; /* for dirremove() */ 5877534Sroot /* 588*16688Smckusick * Return pointer to current entry in ndp->ni_offset, 5897534Sroot * and distance past previous entry (if there 590*16688Smckusick * is a previous entry in this block) in ndp->ni_count. 591*16688Smckusick * Save directory inode pointer in ndp->ni_pdir for dirremove(). 5927534Sroot */ 593*16688Smckusick if ((ndp->ni_offset&(DIRBLKSIZ-1)) == 0) 594*16688Smckusick ndp->ni_count = 0; 5957534Sroot else 596*16688Smckusick ndp->ni_count = ndp->ni_offset - prevoff; 5979166Ssam if (lockparent) { 598*16688Smckusick if (dp->i_number == ndp->ni_dent.d_ino) 5999166Ssam dp->i_count++; 6009166Ssam else { 601*16688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 6029166Ssam if (dp == NULL) { 603*16688Smckusick iput(ndp->ni_pdir); 6049166Ssam goto bad; 6059166Ssam } 60615798Smckusick /* 60716046Skarels * If directory is "sticky", then user must own 60815798Smckusick * the directory, or the file in it, else he 60915798Smckusick * may not delete it (unless he's root). This 61015798Smckusick * implements append-only directories. 61115798Smckusick */ 612*16688Smckusick if ((ndp->ni_pdir->i_mode & ISVTX) && 61315798Smckusick u.u_uid != 0 && 614*16688Smckusick u.u_uid != ndp->ni_pdir->i_uid && 61515798Smckusick dp->i_uid != u.u_uid) { 616*16688Smckusick iput(ndp->ni_pdir); 61715798Smckusick u.u_error = EPERM; 61815798Smckusick goto bad; 61915798Smckusick } 6209166Ssam } 6219166Ssam } 62216681Smckusick nbp->av_forw = freenamebuf; 62316681Smckusick freenamebuf = nbp; 6247534Sroot return (dp); 6257534Sroot } 6267534Sroot 6277534Sroot /* 6287534Sroot * Special handling for ".." allowing chdir out of mounted 6297534Sroot * file system: indirect .. in root inode to reevaluate 6307534Sroot * in directory file system was mounted on. 6317534Sroot */ 63216658Smckusick if (isdotdot) { 6337534Sroot if (dp == u.u_rdir) 634*16688Smckusick ndp->ni_dent.d_ino = dp->i_number; 635*16688Smckusick else if (ndp->ni_dent.d_ino == ROOTINO && 6367534Sroot dp->i_number == ROOTINO) { 6377534Sroot for (i = 1; i < NMOUNT; i++) 6387534Sroot if (mount[i].m_bufp != NULL && 6397534Sroot mount[i].m_dev == dp->i_dev) { 6406571Smckusic iput(dp); 6417534Sroot dp = mount[i].m_inodp; 64216666Smckusick ILOCK(dp); 6435972Swnj dp->i_count++; 6447534Sroot fs = dp->i_fs; 6457534Sroot cp -= 2; /* back over .. */ 6467534Sroot goto dirloop2; 6475972Swnj } 64830Sbill } 6497534Sroot } 6507534Sroot 6517534Sroot /* 6529166Ssam * If rewriting (rename), return the inode and the 6539166Ssam * information required to rewrite the present directory 6549166Ssam * Must get inode of directory entry to verify it's a 6559166Ssam * regular file, or empty directory. 6569166Ssam */ 6579166Ssam if ((flag == CREATE && lockparent) && *cp == 0) { 6589166Ssam if (access(dp, IWRITE)) 6599166Ssam goto bad; 660*16688Smckusick ndp->ni_pdir = dp; /* for dirrewrite() */ 6619166Ssam /* 6629166Ssam * Careful about locking second inode. 6639166Ssam * This can only occur if the target is ".". 6649166Ssam */ 665*16688Smckusick if (dp->i_number == ndp->ni_dent.d_ino) { 6669166Ssam u.u_error = EISDIR; /* XXX */ 6679166Ssam goto bad; 6689166Ssam } 669*16688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 6709166Ssam if (dp == NULL) { 671*16688Smckusick iput(ndp->ni_pdir); 6729166Ssam goto bad; 6739166Ssam } 67416681Smckusick nbp->av_forw = freenamebuf; 67516681Smckusick freenamebuf = nbp; 6769166Ssam return (dp); 6779166Ssam } 6789166Ssam 6799166Ssam /* 68012011Smckusick * Check for symbolic link, which may require us to massage the 68112011Smckusick * name before we continue translation. We do not `iput' the 68212011Smckusick * directory because we may need it again if the symbolic link 68312011Smckusick * is relative to the current directory. Instead we save it 68412011Smckusick * unlocked as "pdp". We must get the target inode before unlocking 68512011Smckusick * the directory to insure that the inode will not be removed 68612011Smckusick * before we get it. We prevent deadlock by always fetching 68712011Smckusick * inodes from the root, moving down the directory tree. Thus 68812011Smckusick * when following backward pointers ".." we must unlock the 68912011Smckusick * parent directory before getting the requested directory. 69012011Smckusick * There is a potential race condition here if both the current 69112011Smckusick * and parent directories are removed before the `iget' for the 69212011Smckusick * inode associated with ".." returns. We hope that this occurs 69312011Smckusick * infrequently since we cannot avoid this race condition without 69412492Ssam * implementing a sophisticated deadlock detection algorithm. 69512011Smckusick * Note also that this simple deadlock detection scheme will not 69612011Smckusick * work if the file system has any hard links other than ".." 69712011Smckusick * that point backwards in the directory structure. 6987534Sroot */ 6997534Sroot pdp = dp; 70015798Smckusick if (isdotdot) { 70116666Smckusick IUNLOCK(pdp); /* race to get the inode */ 702*16688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 70312011Smckusick if (dp == NULL) 70412011Smckusick goto bad2; 705*16688Smckusick } else if (dp->i_number == ndp->ni_dent.d_ino) { 70612011Smckusick dp->i_count++; /* we want ourself, ie "." */ 70712011Smckusick } else { 708*16688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 70916666Smckusick IUNLOCK(pdp); 71012011Smckusick if (dp == NULL) 71112011Smckusick goto bad2; 71212011Smckusick } 71315798Smckusick 71415798Smckusick /* 71515798Smckusick * insert name into cache (if we want it, and it isn't "." or "..") 71615798Smckusick * 71715798Smckusick * all other cases where making a cache entry would be wrong 71815798Smckusick * have already departed from the code sequence somewhere above. 71915798Smckusick */ 72016643Ssam if (docache) { 72115798Smckusick if (ncp != NULL) 72215798Smckusick panic("nami: duplicating cache"); 72315798Smckusick 72415798Smckusick /* 72515798Smckusick * free the cache slot at head of lru chain 72615798Smckusick */ 72715798Smckusick if (ncp = nchhead) { 72815798Smckusick /* remove from lru chain */ 72915798Smckusick *ncp->nc_prev = ncp->nc_nxt; 73015798Smckusick if (ncp->nc_nxt) 73115798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 73215798Smckusick else 73315798Smckusick nchtail = ncp->nc_prev; 73415798Smckusick 73515798Smckusick /* remove from old hash chain */ 73615798Smckusick remque(ncp); 73715798Smckusick 73815798Smckusick /* grab the inode we just found */ 73915798Smckusick ncp->nc_ip = dp; 74015798Smckusick 74115798Smckusick /* fill in cache info */ 74215798Smckusick ncp->nc_ino = pdp->i_number; /* parents inum */ 74315798Smckusick ncp->nc_dev = pdp->i_dev; /* & device */ 74415798Smckusick ncp->nc_idev = dp->i_dev; /* our device */ 74516643Ssam ncp->nc_id = dp->i_id; /* identifier */ 746*16688Smckusick ncp->nc_nlen = ndp->ni_dent.d_namlen; 747*16688Smckusick bcopy(ndp->ni_dent.d_name, ncp->nc_name, ncp->nc_nlen); 74815798Smckusick 74915798Smckusick /* link at end of lru chain */ 75015798Smckusick ncp->nc_nxt = NULL; 75115798Smckusick ncp->nc_prev = nchtail; 75215798Smckusick *nchtail = ncp; 75315798Smckusick nchtail = &ncp->nc_nxt; 75415798Smckusick 75515798Smckusick /* and insert on hash chain */ 75615798Smckusick insque(ncp, nhp); 75715798Smckusick } 75815798Smckusick } 75915798Smckusick 76015798Smckusick haveino: 7617534Sroot fs = dp->i_fs; 7627534Sroot 7637534Sroot /* 7647534Sroot * Check for symbolic link 7657534Sroot */ 766*16688Smckusick if ((dp->i_mode & IFMT) == IFLNK && 767*16688Smckusick ((ndp->ni_nameiop & FOLLOW) || *cp == '/')) { 7687825Sroot u_int pathlen = strlen(cp) + 1; 7697534Sroot 7707534Sroot if (dp->i_size + pathlen >= MAXPATHLEN - 1 || 7717534Sroot ++nlink > MAXSYMLINKS) { 7727534Sroot u.u_error = ELOOP; 7737534Sroot goto bad2; 7747534Sroot } 7758957Sroot ovbcopy(cp, nbp->b_un.b_addr + dp->i_size, pathlen); 7767751Sroot u.u_error = 7779166Ssam rdwri(UIO_READ, dp, nbp->b_un.b_addr, (int)dp->i_size, 7787825Sroot 0, 1, (int *)0); 7797534Sroot if (u.u_error) 7807534Sroot goto bad2; 7817534Sroot cp = nbp->b_un.b_addr; 7827534Sroot iput(dp); 7835972Swnj if (*cp == '/') { 7847534Sroot irele(pdp); 7855972Swnj while (*cp == '/') 7865972Swnj cp++; 7877534Sroot if ((dp = u.u_rdir) == NULL) 7887534Sroot dp = rootdir; 78916666Smckusick ILOCK(dp); 7907534Sroot dp->i_count++; 7917534Sroot } else { 7927534Sroot dp = pdp; 79316666Smckusick ILOCK(dp); 7945972Swnj } 7957534Sroot fs = dp->i_fs; 7967534Sroot goto dirloop; 79730Sbill } 7987534Sroot 79930Sbill /* 8007534Sroot * Not a symbolic link. If more pathname, 8017534Sroot * continue at next component, else return. 80230Sbill */ 8037534Sroot if (*cp == '/') { 8047534Sroot while (*cp == '/') 8057534Sroot cp++; 8069166Ssam irele(pdp); 8077534Sroot goto dirloop; 80830Sbill } 80916681Smckusick nbp->av_forw = freenamebuf; 81016681Smckusick freenamebuf = nbp; 8119166Ssam if (lockparent) 812*16688Smckusick ndp->ni_pdir = pdp; 8139166Ssam else 8149166Ssam irele(pdp); 8157534Sroot return (dp); 8167534Sroot bad2: 8177534Sroot irele(pdp); 8187534Sroot bad: 8197534Sroot if (bp) 8207534Sroot brelse(bp); 8217534Sroot if (dp) 8227534Sroot iput(dp); 82316681Smckusick nbp->av_forw = freenamebuf; 82416681Smckusick freenamebuf = nbp; 8256571Smckusic return (NULL); 82630Sbill } 82730Sbill 82815798Smckusick 829*16688Smckusick dirbad(ip, offset, how) 8307534Sroot struct inode *ip; 831*16688Smckusick off_t offset; 8327534Sroot char *how; 8337534Sroot { 8347534Sroot 8357534Sroot printf("%s: bad dir ino %d at offset %d: %s\n", 836*16688Smckusick ip->i_fs->fs_fsmnt, ip->i_number, offset, how); 8377534Sroot } 8387534Sroot 83916657Smckusick /* 84016657Smckusick * Do consistency checking on a directory entry: 84116657Smckusick * record length must be multiple of 4 84216657Smckusick * record length must not be non-negative 84316657Smckusick * entry must fit in rest of its DIRBLKSIZ block 84416657Smckusick * record must be large enough to contain entry 84516657Smckusick * name is not longer than MAXNAMLEN 84616657Smckusick * name must be as long as advertised, and null terminated 84716657Smckusick */ 84816657Smckusick dirbadentry(ep, entryoffsetinblock) 8497534Sroot register struct direct *ep; 85016657Smckusick int entryoffsetinblock; 8517534Sroot { 8527534Sroot register int i; 8537534Sroot 85416657Smckusick if ((ep->d_reclen & 0x3) != 0 || ep->d_reclen <= 0 || 85516657Smckusick ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 85616657Smckusick ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN) 85716657Smckusick return (1); 8587534Sroot for (i = 0; i < ep->d_namlen; i++) 859*16688Smckusick if (ep->d_name[i] == '\0') 8607534Sroot return (1); 8617534Sroot return (ep->d_name[i]); 8627534Sroot } 8637534Sroot 86430Sbill /* 8657534Sroot * Write a directory entry after a call to namei, using the parameters 8667534Sroot * which it left in the u. area. The argument ip is the inode which 867*16688Smckusick * the new directory entry will refer to. The u. area field ndp->ni_pdir is 8687534Sroot * a pointer to the directory to be written, which was left locked by 869*16688Smckusick * namei. Remaining parameters (ndp->ni_offset, ndp->ni_count) indicate 8707534Sroot * how the space for the new entry is to be gotten. 8717534Sroot */ 872*16688Smckusick direnter(ip, ndp) 8737534Sroot struct inode *ip; 874*16688Smckusick register struct nameidata *ndp; 8755972Swnj { 8767534Sroot register struct direct *ep, *nep; 8777534Sroot struct buf *bp; 87811639Ssam int loc, spacefree, error = 0; 8798631Sroot u_int dsize; 8808631Sroot int newentrysize; 8817534Sroot char *dirbuf; 8825972Swnj 883*16688Smckusick ndp->ni_dent.d_ino = ip->i_number; 884*16688Smckusick newentrysize = DIRSIZ(&ndp->ni_dent); 885*16688Smckusick if (ndp->ni_count == 0) { 8867534Sroot /* 887*16688Smckusick * If ndp->ni_count is 0, then namei could find no space in the 888*16688Smckusick * directory. In this case ndp->ni_offset will be on a directory 8897534Sroot * block boundary and we will write the new entry into a fresh 8907534Sroot * block. 8917534Sroot */ 892*16688Smckusick if (ndp->ni_offset&(DIRBLKSIZ-1)) 8937534Sroot panic("wdir: newblk"); 894*16688Smckusick ndp->ni_dent.d_reclen = DIRBLKSIZ; 895*16688Smckusick error = rdwri(UIO_WRITE, ndp->ni_pdir, (caddr_t)&ndp->ni_dent, 896*16688Smckusick newentrysize, ndp->ni_offset, 1, (int *)0); 897*16688Smckusick iput(ndp->ni_pdir); 89810849Ssam return (error); 8997534Sroot } 9007534Sroot 9017534Sroot /* 902*16688Smckusick * If ndp->ni_count is non-zero, then namei found space for the new 903*16688Smckusick * entry in the range ndp->ni_offset to ndp->ni_offset + ndp->ni_count. 9047534Sroot * in the directory. To use this space, we may have to compact 9057534Sroot * the entries located there, by copying them together towards 9067534Sroot * the beginning of the block, leaving the free space in 9077534Sroot * one usable chunk at the end. 9087534Sroot */ 9097534Sroot 9107534Sroot /* 9117534Sroot * Increase size of directory if entry eats into new space. 9127534Sroot * This should never push the size past a new multiple of 9137534Sroot * DIRBLKSIZE. 9147534Sroot */ 915*16688Smckusick if (ndp->ni_offset + ndp->ni_count > ndp->ni_pdir->i_size) 916*16688Smckusick ndp->ni_pdir->i_size = ndp->ni_offset + ndp->ni_count; 9177534Sroot 9187534Sroot /* 9197534Sroot * Get the block containing the space for the new directory 92010849Ssam * entry. Should return error by result instead of u.u_error. 9217534Sroot */ 922*16688Smckusick bp = blkatoff(ndp->ni_pdir, ndp->ni_offset, (char **)&dirbuf); 9239166Ssam if (bp == 0) { 924*16688Smckusick iput(ndp->ni_pdir); 92510849Ssam return (u.u_error); 9269166Ssam } 9277534Sroot 9287534Sroot /* 9297534Sroot * Find space for the new entry. In the simple case, the 9307534Sroot * entry at offset base will have the space. If it does 9317534Sroot * not, then namei arranged that compacting the region 932*16688Smckusick * ndp->ni_offset to ndp->ni_offset+ndp->ni_count would yield the space. 9337534Sroot */ 9347534Sroot ep = (struct direct *)dirbuf; 9357534Sroot dsize = DIRSIZ(ep); 93611639Ssam spacefree = ep->d_reclen - dsize; 937*16688Smckusick for (loc = ep->d_reclen; loc < ndp->ni_count; ) { 9387534Sroot nep = (struct direct *)(dirbuf + loc); 9397534Sroot if (ep->d_ino) { 9407534Sroot /* trim the existing slot */ 9417534Sroot ep->d_reclen = dsize; 9427534Sroot ep = (struct direct *)((char *)ep + dsize); 9437534Sroot } else { 9447534Sroot /* overwrite; nothing there; header is ours */ 94511639Ssam spacefree += dsize; 9467534Sroot } 9477534Sroot dsize = DIRSIZ(nep); 94811639Ssam spacefree += nep->d_reclen - dsize; 9497534Sroot loc += nep->d_reclen; 9507825Sroot bcopy((caddr_t)nep, (caddr_t)ep, dsize); 9517534Sroot } 9527534Sroot /* 9537534Sroot * Update the pointer fields in the previous entry (if any), 9547534Sroot * copy in the new entry, and write out the block. 9557534Sroot */ 9567534Sroot if (ep->d_ino == 0) { 95711639Ssam if (spacefree + dsize < newentrysize) 9587534Sroot panic("wdir: compact1"); 959*16688Smckusick ndp->ni_dent.d_reclen = spacefree + dsize; 9607534Sroot } else { 96111639Ssam if (spacefree < newentrysize) 9627534Sroot panic("wdir: compact2"); 963*16688Smckusick ndp->ni_dent.d_reclen = spacefree; 9647534Sroot ep->d_reclen = dsize; 9657534Sroot ep = (struct direct *)((char *)ep + dsize); 9667534Sroot } 967*16688Smckusick bcopy((caddr_t)&ndp->ni_dent, (caddr_t)ep, (u_int)newentrysize); 9687534Sroot bwrite(bp); 969*16688Smckusick ndp->ni_pdir->i_flag |= IUPD|ICHG; 970*16688Smckusick iput(ndp->ni_pdir); 97110849Ssam return (error); 9725972Swnj } 9736571Smckusic 9749166Ssam /* 9759166Ssam * Remove a directory entry after a call to namei, using the 9769166Ssam * parameters which it left in the u. area. The u. entry 977*16688Smckusick * ni_offset contains the offset into the directory of the 978*16688Smckusick * entry to be eliminated. The ni_count field contains the 9799166Ssam * size of the previous record in the directory. If this 9809166Ssam * is 0, the first entry is being deleted, so we need only 9819166Ssam * zero the inode number to mark the entry as free. If the 9829166Ssam * entry isn't the first in the directory, we must reclaim 9839166Ssam * the space of the now empty record by adding the record size 9849166Ssam * to the size of the previous entry. 9859166Ssam */ 986*16688Smckusick dirremove(ndp) 987*16688Smckusick register struct nameidata *ndp; 9886571Smckusic { 989*16688Smckusick register struct inode *dp = ndp->ni_pdir; 9907534Sroot register struct buf *bp; 9917534Sroot struct direct *ep; 9926571Smckusic 993*16688Smckusick if (ndp->ni_count == 0) { 9947534Sroot /* 9957534Sroot * First entry in block: set d_ino to zero. 9967534Sroot */ 997*16688Smckusick ndp->ni_dent.d_ino = 0; 998*16688Smckusick (void) rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 999*16688Smckusick (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0); 10009269Ssam } else { 10017534Sroot /* 10027534Sroot * Collapse new free space into previous entry. 10037534Sroot */ 1004*16688Smckusick bp = blkatoff(dp, (int)(ndp->ni_offset - ndp->ni_count), 1005*16688Smckusick (char **)&ep); 10067534Sroot if (bp == 0) 10077534Sroot return (0); 1008*16688Smckusick ep->d_reclen += ndp->ni_dent.d_reclen; 10097534Sroot bwrite(bp); 10107534Sroot dp->i_flag |= IUPD|ICHG; 10117534Sroot } 10127534Sroot return (1); 10136571Smckusic } 10147534Sroot 10157605Ssam /* 10169166Ssam * Rewrite an existing directory entry to point at the inode 10179166Ssam * supplied. The parameters describing the directory entry are 10189166Ssam * set up by a call to namei. 10199166Ssam */ 1020*16688Smckusick dirrewrite(dp, ip, ndp) 10219166Ssam struct inode *dp, *ip; 1022*16688Smckusick struct nameidata *ndp; 10239166Ssam { 10249166Ssam 1025*16688Smckusick ndp->ni_dent.d_ino = ip->i_number; 1026*16688Smckusick u.u_error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 1027*16688Smckusick (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0); 10289166Ssam iput(dp); 10299166Ssam } 10309166Ssam 10319166Ssam /* 10327605Ssam * Return buffer with contents of block "offset" 10337605Ssam * from the beginning of directory "ip". If "res" 10347605Ssam * is non-zero, fill it in with a pointer to the 10357605Ssam * remaining space in the directory. 10367605Ssam */ 10377534Sroot struct buf * 10387605Ssam blkatoff(ip, offset, res) 10397534Sroot struct inode *ip; 10407534Sroot off_t offset; 10417534Sroot char **res; 10427534Sroot { 10437534Sroot register struct fs *fs = ip->i_fs; 10448672Sroot daddr_t lbn = lblkno(fs, offset); 10457534Sroot int base = blkoff(fs, offset); 10467534Sroot int bsize = blksize(fs, ip, lbn); 10478672Sroot daddr_t bn = fsbtodb(fs, bmap(ip, lbn, B_WRITE, base, bsize)); 10487534Sroot register struct buf *bp; 10497534Sroot 10507534Sroot if (u.u_error) 10517534Sroot return (0); 10527534Sroot bp = bread(ip->i_dev, bn, bsize); 10537534Sroot if (bp->b_flags & B_ERROR) { 10547534Sroot brelse(bp); 10557534Sroot return (0); 10567534Sroot } 10577534Sroot if (res) 10587534Sroot *res = bp->b_un.b_addr + base; 10597534Sroot return (bp); 10607534Sroot } 10619166Ssam 10629166Ssam /* 10639166Ssam * Check if a directory is empty or not. 10649166Ssam * Inode supplied must be locked. 106512817Ssam * 106612817Ssam * Using a struct dirtemplate here is not precisely 106712817Ssam * what we want, but better than using a struct direct. 106812817Ssam * 106912817Ssam * NB: does not handle corrupted directories. 10709166Ssam */ 10719166Ssam dirempty(ip) 10729863Ssam register struct inode *ip; 10739166Ssam { 10749166Ssam register off_t off; 107512817Ssam struct dirtemplate dbuf; 107612817Ssam register struct direct *dp = (struct direct *)&dbuf; 10779863Ssam int error, count; 107812817Ssam #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 10799166Ssam 10809166Ssam for (off = 0; off < ip->i_size; off += dp->d_reclen) { 108112817Ssam error = rdwri(UIO_READ, ip, (caddr_t)dp, MINDIRSIZ, 108212817Ssam off, 1, &count); 108312817Ssam /* 108412817Ssam * Since we read MINDIRSIZ, residual must 108512817Ssam * be 0 unless we're at end of file. 108612817Ssam */ 108712817Ssam if (error || count != 0) 10889166Ssam return (0); 108912817Ssam /* skip empty entries */ 10909166Ssam if (dp->d_ino == 0) 10919166Ssam continue; 109212817Ssam /* accept only "." and ".." */ 109312817Ssam if (dp->d_namlen > 2) 109412817Ssam return (0); 10959166Ssam if (dp->d_name[0] != '.') 10969166Ssam return (0); 109712817Ssam /* 109812817Ssam * At this point d_namlen must be 1 or 2. 109912817Ssam * 1 implies ".", 2 implies ".." if second 110012817Ssam * char is also "." 110112817Ssam */ 110212817Ssam if (dp->d_namlen == 1 || dp->d_name[1] == '.') 11039166Ssam continue; 11049166Ssam return (0); 11059166Ssam } 11069166Ssam return (1); 11079166Ssam } 110812815Smckusick 110912815Smckusick /* 111012815Smckusick * Check if source directory is in the path of the target directory. 111112815Smckusick * Target is supplied locked, source is unlocked. 111212815Smckusick * The target is always iput() before returning. 111312815Smckusick */ 111412815Smckusick checkpath(source, target) 111512815Smckusick struct inode *source, *target; 111612815Smckusick { 111712815Smckusick struct dirtemplate dirbuf; 111812815Smckusick register struct inode *ip; 111912815Smckusick int error = 0; 112012815Smckusick 112112815Smckusick ip = target; 112212815Smckusick if (ip->i_number == source->i_number) { 112312815Smckusick error = EEXIST; 112412815Smckusick goto out; 112512815Smckusick } 112612815Smckusick if (ip->i_number == ROOTINO) 112712815Smckusick goto out; 112812815Smckusick 112912815Smckusick for (;;) { 113012815Smckusick if ((ip->i_mode&IFMT) != IFDIR) { 113112815Smckusick error = ENOTDIR; 113212815Smckusick break; 113312815Smckusick } 113412815Smckusick error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf, 113512815Smckusick sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 113612815Smckusick if (error != 0) 113712815Smckusick break; 113812815Smckusick if (dirbuf.dotdot_namlen != 2 || 113916658Smckusick dirbuf.dotdot_name[0] != '.' || 114016658Smckusick dirbuf.dotdot_name[1] != '.') { 114112815Smckusick error = ENOTDIR; 114212815Smckusick break; 114312815Smckusick } 114412815Smckusick if (dirbuf.dotdot_ino == source->i_number) { 114512815Smckusick error = EINVAL; 114612815Smckusick break; 114712815Smckusick } 114812815Smckusick if (dirbuf.dotdot_ino == ROOTINO) 114912815Smckusick break; 115012815Smckusick iput(ip); 115112815Smckusick ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino); 115212815Smckusick if (ip == NULL) { 115312815Smckusick error = u.u_error; 115412815Smckusick break; 115512815Smckusick } 115612815Smckusick } 115712815Smckusick 115812815Smckusick out: 115912815Smckusick if (error == ENOTDIR) 116012815Smckusick printf("checkpath: .. not a directory\n"); 116112815Smckusick if (ip != NULL) 116212815Smckusick iput(ip); 116312815Smckusick return (error); 116412815Smckusick } 116515798Smckusick 116615798Smckusick /* 116715798Smckusick * Name cache initialization, from main() when we are booting 116815798Smckusick */ 116915798Smckusick nchinit() 117015798Smckusick { 117115798Smckusick register union nchash *nchp; 117215798Smckusick register struct nch *ncp; 117315798Smckusick 117415798Smckusick nchhead = 0; 117515798Smckusick nchtail = &nchhead; 117615798Smckusick 117715798Smckusick for (ncp = nch; ncp < &nch[nchsize]; ncp++) { 117815798Smckusick ncp->nc_forw = ncp; /* hash chain */ 117915798Smckusick ncp->nc_back = ncp; 118015798Smckusick 118115798Smckusick ncp->nc_nxt = NULL; /* lru chain */ 118215798Smckusick *nchtail = ncp; 118315798Smckusick ncp->nc_prev = nchtail; 118415798Smckusick nchtail = &ncp->nc_nxt; 118515798Smckusick 118615798Smckusick /* all else is zero already */ 118715798Smckusick } 118815798Smckusick 118915798Smckusick for (nchp = nchash; nchp < &nchash[NCHHASH]; nchp++) { 119015798Smckusick nchp->nch_head[0] = nchp; 119115798Smckusick nchp->nch_head[1] = nchp; 119215798Smckusick } 119315798Smckusick } 119415798Smckusick 119515798Smckusick /* 119615798Smckusick * Cache flush, called when filesys is umounted to 119715798Smckusick * remove entries that would now be invalid 119815798Smckusick * 119915798Smckusick * The line "nxtcp = nchhead" near the end is to avoid potential problems 120015798Smckusick * if the cache lru chain is modified while we are dumping the 120115798Smckusick * inode. This makes the algorithm O(n^2), but do you think I care? 120215798Smckusick */ 120315798Smckusick nchinval(dev) 120415798Smckusick register dev_t dev; 120515798Smckusick { 120615798Smckusick register struct nch *ncp, *nxtcp; 120715798Smckusick 120815798Smckusick for (ncp = nchhead; ncp; ncp = nxtcp) { 120915798Smckusick nxtcp = ncp->nc_nxt; 121015798Smckusick 121115798Smckusick if (ncp->nc_ip == NULL || 121215798Smckusick (ncp->nc_idev != dev && ncp->nc_dev != dev)) 121315798Smckusick continue; 121415798Smckusick 121516658Smckusick /* free the resources we had */ 121615798Smckusick ncp->nc_idev = NODEV; 121715798Smckusick ncp->nc_dev = NODEV; 121816658Smckusick ncp->nc_id = NULL; 121915798Smckusick ncp->nc_ino = 0; 122016658Smckusick ncp->nc_ip = NULL; 122115798Smckusick 122216658Smckusick 122315798Smckusick /* remove the entry from its hash chain */ 122415798Smckusick remque(ncp); 122515798Smckusick /* and make a dummy one */ 122615798Smckusick ncp->nc_forw = ncp; 122715798Smckusick ncp->nc_back = ncp; 122815798Smckusick 122915798Smckusick /* delete this entry from LRU chain */ 123015798Smckusick *ncp->nc_prev = nxtcp; 123115798Smckusick if (nxtcp) 123215798Smckusick nxtcp->nc_prev = ncp->nc_prev; 123315798Smckusick else 123415798Smckusick nchtail = ncp->nc_prev; 123515798Smckusick 123615798Smckusick /* cause rescan of list, it may have altered */ 123715798Smckusick nxtcp = nchhead; 123815798Smckusick /* put the now-free entry at head of LRU */ 123915798Smckusick ncp->nc_nxt = nxtcp; 124015798Smckusick ncp->nc_prev = &nchhead; 124115798Smckusick nxtcp->nc_prev = &ncp->nc_nxt; 124215798Smckusick nchhead = ncp; 124315798Smckusick } 124415798Smckusick } 1245