123401Smckusick /* 223401Smckusick * Copyright (c) 1982 Regents of the University of California. 323401Smckusick * All rights reserved. The Berkeley software License Agreement 423401Smckusick * specifies the terms and conditions for redistribution. 523401Smckusick * 6*26360Skarels * @(#)vfs_lookup.c 6.28 (Berkeley) 02/23/86 723401Smckusick */ 830Sbill 917100Sbloom #include "param.h" 1017100Sbloom #include "systm.h" 1117100Sbloom #include "inode.h" 1217100Sbloom #include "fs.h" 1317100Sbloom #include "mount.h" 1417100Sbloom #include "dir.h" 1517100Sbloom #include "user.h" 1617100Sbloom #include "buf.h" 1717100Sbloom #include "conf.h" 1817100Sbloom #include "uio.h" 1917100Sbloom #include "kernel.h" 2030Sbill 217605Ssam struct buf *blkatoff(); 2216681Smckusick struct buf *freenamebuf; 239166Ssam int dirchk = 0; 2415798Smckusick 2530Sbill /* 2615798Smckusick * Structures associated with name cacheing. 2715798Smckusick */ 2815798Smckusick #define NCHHASH 32 /* size of hash table */ 2915798Smckusick 3015798Smckusick #if ((NCHHASH)&((NCHHASH)-1)) != 0 3115798Smckusick #define NHASH(h, i, d) ((unsigned)((h) + (i) + 13 * (int)(d)) % (NCHHASH)) 3215798Smckusick #else 3315798Smckusick #define NHASH(h, i, d) ((unsigned)((h) + (i) + 13 * (int)(d)) & ((NCHHASH)-1)) 3415798Smckusick #endif 3515798Smckusick 3626273Skarels union nchash { 3726273Skarels union nchash *nch_head[2]; 3826306Skarels struct namecache *nch_chain[2]; 3915798Smckusick } nchash[NCHHASH]; 4015798Smckusick #define nch_forw nch_chain[0] 4115798Smckusick #define nch_back nch_chain[1] 4215798Smckusick 4326306Skarels struct namecache *nchhead, **nchtail; /* LRU chain pointers */ 4415809Smckusick struct nchstats nchstats; /* cache effectiveness statistics */ 4515798Smckusick 4615798Smckusick /* 477534Sroot * Convert a pathname into a pointer to a locked inode, 487534Sroot * with side effects usable in creating and removing files. 497534Sroot * This is a very central and rather complicated routine. 5030Sbill * 5116688Smckusick * The segflg defines whether the name is to be copied from user 5216688Smckusick * space or kernel space. 537534Sroot * 549166Ssam * The flag argument is (LOOKUP, CREATE, DELETE) depending on whether 559166Ssam * the name is to be (looked up, created, deleted). If flag has 569166Ssam * LOCKPARENT or'ed into it and the target of the pathname exists, 579166Ssam * namei returns both the target and its parent directory locked. 589166Ssam * If the file system is not maintained in a strict tree hierarchy, 599166Ssam * this can result in a deadlock situation. When creating and 609166Ssam * LOCKPARENT is specified, the target may not be ".". When deleting 619166Ssam * and LOCKPARENT is specified, the target may be ".", but the caller 629166Ssam * must check to insure it does an irele and iput instead of two iputs. 639166Ssam * 6416688Smckusick * The FOLLOW flag is set when symbolic links are to be followed 659166Ssam * when they occur at the end of the name translation process. 669166Ssam * 6715798Smckusick * Name caching works as follows: 687534Sroot * 6926273Skarels * Names found by directory scans are retained in a cache 7026273Skarels * for future reference. It is managed LRU, so frequently 7126273Skarels * used names will hang around. Cache is indexed by hash value 7226273Skarels * obtained from (ino,dev,name) where ino & dev refer to the 7326273Skarels * directory containing name. 7415798Smckusick * 7526273Skarels * For simplicity (and economy of storage), names longer than 7626273Skarels * some (small) maximum length are not cached, they occur 7726273Skarels * infrequently in any case, and are almost never of interest. 7815798Smckusick * 7926273Skarels * Upon reaching the last segment of a path, if the reference 8026273Skarels * is for DELETE, or NOCACHE is set (rewrite), and the 8126273Skarels * name is located in the cache, it will be dropped. 8215798Smckusick * 8326273Skarels * We must be sure never to enter the name ".." into the cache 8426273Skarels * because of the extremely kludgey way that rename() alters 8526273Skarels * ".." in a situation like 8626273Skarels * mv a/x b/x 8726273Skarels * where x is a directory, and x/.. is the ".." in question. 8815798Smckusick * 8915798Smckusick * Overall outline of namei: 9015798Smckusick * 917534Sroot * copy in name 927534Sroot * get starting directory 937534Sroot * dirloop: 947534Sroot * check accessibility of directory 957534Sroot * dirloop2: 9616688Smckusick * copy next component of name to ndp->ni_dent 977534Sroot * handle degenerate case where name is null string 9815798Smckusick * look for name in cache, if found, then if at end of path 9915798Smckusick * and deleting or creating, drop it, else to haveino 1007534Sroot * search for name in directory, to found or notfound 1017534Sroot * notfound: 1029166Ssam * if creating, return locked directory, leaving info on avail. slots 1037534Sroot * else return error 1047534Sroot * found: 1057534Sroot * if at end of path and deleting, return information to allow delete 10615798Smckusick * if at end of path and rewriting (create and LOCKPARENT), lock target 1079166Ssam * inode and return info to allow rewrite 1087534Sroot * if .. and on mounted filesys, look in mount table for parent 10915798Smckusick * if not at end, if neither creating nor deleting, add name to cache 11015798Smckusick * haveino: 1117534Sroot * if symbolic link, massage name in buffer and continue at dirloop 1127534Sroot * if more components of name, do next level at dirloop 1137534Sroot * return the answer as locked inode 1149166Ssam * 1159166Ssam * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode, 1169166Ssam * but unlocked. 11730Sbill */ 11830Sbill struct inode * 11916688Smckusick namei(ndp) 12016688Smckusick register struct nameidata *ndp; 12130Sbill { 1227534Sroot register char *cp; /* pointer into pathname argument */ 1237534Sroot /* these variables refer to things which must be freed or unlocked */ 1247534Sroot register struct inode *dp = 0; /* the directory we are searching */ 12526306Skarels register struct namecache *ncp; /* cache slot for entry */ 1267534Sroot register struct fs *fs; /* file system that directory is in */ 1277534Sroot register struct buf *bp = 0; /* a buffer of directory entries */ 1287534Sroot register struct direct *ep; /* the current directory entry */ 1297534Sroot int entryoffsetinblock; /* offset of ep in bp's buffer */ 1307534Sroot register struct buf *nbp; /* buffer storing path name argument */ 1317534Sroot /* these variables hold information about the search for a slot */ 1327534Sroot enum {NONE, COMPACT, FOUND} slotstatus; 1337534Sroot int slotoffset = -1; /* offset of area with free space */ 1347534Sroot int slotsize; /* size of area at slotoffset */ 1357534Sroot int slotfreespace; /* amount of space free in slot */ 1367534Sroot int slotneeded; /* size of the entry we're seeking */ 1377534Sroot /* */ 13815660Smckusick int numdirpasses; /* strategy for directory search */ 13915660Smckusick int endsearch; /* offset to end directory search */ 14016688Smckusick int prevoff; /* ndp->ni_offset of previous entry */ 1417534Sroot int nlink = 0; /* number of symbolic links taken */ 1427534Sroot struct inode *pdp; /* saved dp during symlink work */ 14316688Smckusick int error, i; 1449166Ssam int lockparent; 14518109Smckusick int docache; /* == 0 do not cache last component */ 14618109Smckusick int makeentry; /* != 0 if name to be added to cache */ 14715798Smckusick unsigned hash; /* value of name hash for entry */ 14815798Smckusick union nchash *nhp; /* cache chain head for entry */ 14915798Smckusick int isdotdot; /* != 0 if current name is ".." */ 15016688Smckusick int flag; /* op ie, LOOKUP, CREATE, or DELETE */ 15118027Smckusick off_t enduseful; /* pointer past last used dir slot */ 15230Sbill 15316688Smckusick lockparent = ndp->ni_nameiop & LOCKPARENT; 15416688Smckusick docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE; 15516688Smckusick flag = ndp->ni_nameiop &~ (LOCKPARENT|NOCACHE|FOLLOW); 15618109Smckusick if (flag == DELETE || lockparent) 15715798Smckusick docache = 0; 15830Sbill /* 1597534Sroot * Get a buffer for the name to be translated, and copy the 1607534Sroot * name into the buffer. 1615972Swnj */ 16216681Smckusick nbp = freenamebuf; 16316681Smckusick if (nbp == NULL) 16416681Smckusick nbp = geteblk(MAXPATHLEN); 16516681Smckusick else 16616681Smckusick freenamebuf = nbp->av_forw; 16716688Smckusick if (ndp->ni_segflg == UIO_SYSSPACE) 16816705Smckusick error = copystr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN, 16916705Smckusick (u_int *)0); 17016688Smckusick else 17116705Smckusick error = copyinstr(ndp->ni_dirp, nbp->b_un.b_addr, MAXPATHLEN, 17216705Smckusick (u_int *)0); 17316688Smckusick if (error) { 17416688Smckusick u.u_error = error; 17516688Smckusick goto bad; 1765972Swnj } 1777534Sroot 1785972Swnj /* 1797534Sroot * Get starting directory. 18030Sbill */ 1817534Sroot cp = nbp->b_un.b_addr; 1825972Swnj if (*cp == '/') { 1835972Swnj while (*cp == '/') 1845972Swnj cp++; 18530Sbill if ((dp = u.u_rdir) == NULL) 18630Sbill dp = rootdir; 1877534Sroot } else 1887534Sroot dp = u.u_cdir; 1897534Sroot fs = dp->i_fs; 19016666Smckusick ILOCK(dp); 1915972Swnj dp->i_count++; 19216688Smckusick ndp->ni_pdir = (struct inode *)0xc0000000; /* illegal */ 19318027Smckusick ndp->ni_endoff = 0; 1947534Sroot 1957534Sroot /* 1967534Sroot * We come to dirloop to search a new directory. 1977534Sroot * The directory must be locked so that it can be 1987534Sroot * iput, and fs must be already set to dp->i_fs. 1997534Sroot */ 2006571Smckusic dirloop: 20130Sbill /* 2027534Sroot * Check accessiblity of directory. 20330Sbill */ 2047534Sroot if ((dp->i_mode&IFMT) != IFDIR) { 20530Sbill u.u_error = ENOTDIR; 2067534Sroot goto bad; 2077534Sroot } 2087534Sroot if (access(dp, IEXEC)) 2097534Sroot goto bad; 2107534Sroot 2116384Swnj dirloop2: 2127534Sroot /* 21316688Smckusick * Copy next component of name to ndp->ni_dent. 2147534Sroot */ 21515798Smckusick hash = 0; 2167534Sroot for (i = 0; *cp != 0 && *cp != '/'; cp++) { 2176571Smckusic if (i >= MAXNAMLEN) { 21821014Smckusick u.u_error = ENAMETOOLONG; 2197534Sroot goto bad; 2205972Swnj } 22121014Smckusick if (*cp & 0200) 22221014Smckusick if ((*cp&0377) == ('/'|0200) || flag != DELETE) { 22321014Smckusick u.u_error = EINVAL; 22421014Smckusick goto bad; 22521014Smckusick } 22616688Smckusick ndp->ni_dent.d_name[i++] = *cp; 22715798Smckusick hash += (unsigned char)*cp * i; 2285972Swnj } 22916688Smckusick ndp->ni_dent.d_namlen = i; 23016688Smckusick ndp->ni_dent.d_name[i] = '\0'; 23116658Smckusick isdotdot = (i == 2 && 23216688Smckusick ndp->ni_dent.d_name[0] == '.' && ndp->ni_dent.d_name[1] == '.'); 23318109Smckusick makeentry = 1; 23418109Smckusick if (*cp == '\0' && docache == 0) 23518109Smckusick makeentry = 0; 2367534Sroot 2377534Sroot /* 2387534Sroot * Check for degenerate name (e.g. / or "") 2397534Sroot * which is a way of talking about a directory, 2407534Sroot * e.g. like "/." or ".". 2417534Sroot */ 24216688Smckusick if (ndp->ni_dent.d_name[0] == '\0') { 24315798Smckusick if (flag != LOOKUP || lockparent) { 24414937Smckusick u.u_error = EISDIR; 2457534Sroot goto bad; 2465972Swnj } 24716681Smckusick nbp->av_forw = freenamebuf; 24816681Smckusick freenamebuf = nbp; 2496571Smckusic return (dp); 2505972Swnj } 2517534Sroot 2526571Smckusic /* 25315798Smckusick * We now have a segment name to search for, and a directory to search. 25415798Smckusick * 25515798Smckusick * Before tediously performing a linear scan of the directory, 25615798Smckusick * check the name cache to see if the directory/name pair 25715798Smckusick * we are looking for is known already. We don't do this 25815798Smckusick * if the segment name is long, simply so the cache can avoid 25915798Smckusick * holding long names (which would either waste space, or 26015798Smckusick * add greatly to the complexity). 26115798Smckusick */ 26216688Smckusick if (ndp->ni_dent.d_namlen > NCHNAMLEN) { 26315798Smckusick nchstats.ncs_long++; 26418109Smckusick makeentry = 0; 26515798Smckusick } else { 26615798Smckusick nhp = &nchash[NHASH(hash, dp->i_number, dp->i_dev)]; 26726306Skarels for (ncp = nhp->nch_forw; ncp != (struct namecache *)nhp; 26815798Smckusick ncp = ncp->nc_forw) { 26915798Smckusick if (ncp->nc_ino == dp->i_number && 27015798Smckusick ncp->nc_dev == dp->i_dev && 27116688Smckusick ncp->nc_nlen == ndp->ni_dent.d_namlen && 27216688Smckusick !bcmp(ncp->nc_name, ndp->ni_dent.d_name, 27326273Skarels (unsigned)ncp->nc_nlen)) 27415798Smckusick break; 27515798Smckusick } 27626306Skarels if (ncp == (struct namecache *)nhp) { 27715798Smckusick nchstats.ncs_miss++; 27815798Smckusick ncp = NULL; 27915798Smckusick } else { 28026306Skarels if (ncp->nc_id != ncp->nc_ip->i_id) 28116643Ssam nchstats.ncs_falsehits++; 28226306Skarels else if (!makeentry) 28316658Smckusick nchstats.ncs_badhits++; 28426306Skarels else { 28526273Skarels /* 28626273Skarels * move this slot to end of LRU 28726273Skarels * chain, if not already there 28826273Skarels */ 28915798Smckusick if (ncp->nc_nxt) { 29026273Skarels /* remove from LRU chain */ 29115798Smckusick *ncp->nc_prev = ncp->nc_nxt; 29215798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 29315798Smckusick 29426273Skarels /* and replace at end of it */ 29515798Smckusick ncp->nc_nxt = NULL; 29615798Smckusick ncp->nc_prev = nchtail; 29715798Smckusick *nchtail = ncp; 29815798Smckusick nchtail = &ncp->nc_nxt; 29915798Smckusick } 30015798Smckusick 30116658Smckusick /* 30216658Smckusick * Get the next inode in the path. 30316666Smckusick * See comment above other `IUNLOCK' code for 30416658Smckusick * an explaination of the locking protocol. 30516658Smckusick */ 30615798Smckusick pdp = dp; 30718109Smckusick if (!isdotdot || dp != u.u_rdir) 30818109Smckusick dp = ncp->nc_ip; 30915798Smckusick if (dp == NULL) 31015798Smckusick panic("nami: null cache ino"); 31126306Skarels if (pdp == dp) 31216643Ssam dp->i_count++; 31326306Skarels else if (isdotdot) { 31418109Smckusick IUNLOCK(pdp); 31518109Smckusick igrab(dp); 31618109Smckusick } else { 31718109Smckusick igrab(dp); 31818109Smckusick IUNLOCK(pdp); 31916643Ssam } 32015798Smckusick 32116658Smckusick /* 32216658Smckusick * Verify that the inode that we got 32316658Smckusick * did not change while we were waiting 32416658Smckusick * for it to be locked. 32516658Smckusick */ 32616658Smckusick if (ncp->nc_id != ncp->nc_ip->i_id) { 32716658Smckusick iput(dp); 32816666Smckusick ILOCK(pdp); 32916658Smckusick dp = pdp; 33016658Smckusick nchstats.ncs_falsehits++; 33116658Smckusick } else { 33216688Smckusick ndp->ni_dent.d_ino = dp->i_number; 33316688Smckusick /* ni_dent.d_reclen is garbage ... */ 33416658Smckusick nchstats.ncs_goodhits++; 33516658Smckusick goto haveino; 33616658Smckusick } 33716658Smckusick } 33815798Smckusick 33915798Smckusick /* 34016643Ssam * Last component and we are renaming or deleting, 34116643Ssam * the cache entry is invalid, or otherwise don't 34216643Ssam * want cache entry to exist. 34315798Smckusick */ 34426273Skarels /* remove from LRU chain */ 34515798Smckusick *ncp->nc_prev = ncp->nc_nxt; 34615798Smckusick if (ncp->nc_nxt) 34715798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 34815798Smckusick else 34915798Smckusick nchtail = ncp->nc_prev; 35026273Skarels remque(ncp); /* remove from hash chain */ 35126273Skarels /* 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; 35626273Skarels /* and make a dummy hash chain */ 35715798Smckusick ncp->nc_forw = ncp; 35815798Smckusick ncp->nc_back = ncp; 35915798Smckusick ncp = NULL; 36015798Smckusick } 36115798Smckusick } 36215798Smckusick 36315798Smckusick /* 3647534Sroot * Suppress search for slots unless creating 3657534Sroot * file and at end of pathname, in which case 3667534Sroot * we watch for a place to put the new file in 3677534Sroot * case it doesn't already exist. 3686571Smckusic */ 3697534Sroot slotstatus = FOUND; 3709166Ssam if (flag == CREATE && *cp == 0) { 3717534Sroot slotstatus = NONE; 3727534Sroot slotfreespace = 0; 37316688Smckusick slotneeded = DIRSIZ(&ndp->ni_dent); 3747534Sroot } 37515660Smckusick /* 37615660Smckusick * If this is the same directory that this process 37715660Smckusick * previously searched, pick up where we last left off. 37815798Smckusick * We cache only lookups as these are the most common 37915660Smckusick * and have the greatest payoff. Caching CREATE has little 38015660Smckusick * benefit as it usually must search the entire directory 38115660Smckusick * to determine that the entry does not exist. Caching the 38215660Smckusick * location of the last DELETE has not reduced profiling time 38315660Smckusick * and hence has been removed in the interest of simplicity. 38415660Smckusick */ 38515660Smckusick if (flag != LOOKUP || dp->i_number != u.u_ncache.nc_inumber || 38615660Smckusick dp->i_dev != u.u_ncache.nc_dev) { 38716688Smckusick ndp->ni_offset = 0; 38815660Smckusick numdirpasses = 1; 38915660Smckusick } else { 39015798Smckusick if ((dp->i_flag & ICHG) || dp->i_ctime >= u.u_ncache.nc_time) { 39117698Smckusick if (u.u_ncache.nc_prevoffset > dp->i_size) 39217698Smckusick u.u_ncache.nc_prevoffset = 0; 39317698Smckusick else 39417698Smckusick u.u_ncache.nc_prevoffset &= ~(DIRBLKSIZ - 1); 39515660Smckusick u.u_ncache.nc_time = time.tv_sec; 39615660Smckusick } 39716688Smckusick ndp->ni_offset = u.u_ncache.nc_prevoffset; 39816688Smckusick entryoffsetinblock = blkoff(fs, ndp->ni_offset); 39915660Smckusick if (entryoffsetinblock != 0) { 40016688Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)0); 40115660Smckusick if (bp == 0) 40215660Smckusick goto bad; 40315660Smckusick } 40415660Smckusick numdirpasses = 2; 40515798Smckusick nchstats.ncs_2passes++; 40615660Smckusick } 40715660Smckusick endsearch = roundup(dp->i_size, DIRBLKSIZ); 40818027Smckusick enduseful = 0; 4097534Sroot 41015660Smckusick searchloop: 41116688Smckusick while (ndp->ni_offset < endsearch) { 4125972Swnj /* 4135972Swnj * If offset is on a block boundary, 4145972Swnj * read the next directory block. 4155972Swnj * Release previous if it exists. 4165972Swnj */ 41716688Smckusick if (blkoff(fs, ndp->ni_offset) == 0) { 4185972Swnj if (bp != NULL) 4195972Swnj brelse(bp); 42016688Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)0); 4217534Sroot if (bp == 0) 4227534Sroot goto bad; 4237534Sroot entryoffsetinblock = 0; 4245972Swnj } 4255972Swnj /* 4267534Sroot * If still looking for a slot, and at a DIRBLKSIZE 42716657Smckusick * boundary, have to start looking for free space again. 4286571Smckusic */ 4297534Sroot if (slotstatus == NONE && 4307534Sroot (entryoffsetinblock&(DIRBLKSIZ-1)) == 0) { 4317534Sroot slotoffset = -1; 4327534Sroot slotfreespace = 0; 4337534Sroot } 4347534Sroot /* 43516657Smckusick * Get pointer to next entry. 43616657Smckusick * Full validation checks are slow, so we only check 43716657Smckusick * enough to insure forward progress through the 43816657Smckusick * directory. Complete checks can be run by patching 43916657Smckusick * "dirchk" to be true. 4407534Sroot */ 4417534Sroot ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock); 44226273Skarels if (ep->d_reclen == 0 || 44316657Smckusick dirchk && dirbadentry(ep, entryoffsetinblock)) { 44416688Smckusick dirbad(dp, ndp->ni_offset, "mangled entry"); 44516657Smckusick i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)); 44616688Smckusick ndp->ni_offset += i; 4477534Sroot entryoffsetinblock += i; 4486571Smckusic continue; 4496571Smckusic } 4507534Sroot 4516571Smckusic /* 4527534Sroot * If an appropriate sized slot has not yet been found, 4536571Smckusic * check to see if one is available. Also accumulate space 4546571Smckusic * in the current block so that we can determine if 4556571Smckusic * compaction is viable. 4566571Smckusic */ 4577534Sroot if (slotstatus != FOUND) { 4587534Sroot int size = ep->d_reclen; 4597534Sroot 4606571Smckusic if (ep->d_ino != 0) 4616571Smckusic size -= DIRSIZ(ep); 4626571Smckusic if (size > 0) { 4637534Sroot if (size >= slotneeded) { 4647534Sroot slotstatus = FOUND; 46516688Smckusick slotoffset = ndp->ni_offset; 4667534Sroot slotsize = ep->d_reclen; 4677534Sroot } else if (slotstatus == NONE) { 4687534Sroot slotfreespace += size; 4697534Sroot if (slotoffset == -1) 47016688Smckusick slotoffset = ndp->ni_offset; 4717534Sroot if (slotfreespace >= slotneeded) { 4727534Sroot slotstatus = COMPACT; 47316688Smckusick slotsize = ndp->ni_offset + 47416688Smckusick ep->d_reclen - slotoffset; 4757534Sroot } 4766571Smckusic } 4776571Smckusic } 4786571Smckusic } 4797534Sroot 4806571Smckusic /* 4817534Sroot * Check for a name match. 4825972Swnj */ 4837534Sroot if (ep->d_ino) { 48416688Smckusick if (ep->d_namlen == ndp->ni_dent.d_namlen && 48516688Smckusick !bcmp(ndp->ni_dent.d_name, ep->d_name, 48626273Skarels (unsigned)ep->d_namlen)) 4877534Sroot goto found; 4887534Sroot } 48916688Smckusick prevoff = ndp->ni_offset; 49016688Smckusick ndp->ni_offset += ep->d_reclen; 4917534Sroot entryoffsetinblock += ep->d_reclen; 49218027Smckusick if (ep->d_ino) 49318027Smckusick enduseful = ndp->ni_offset; 4947534Sroot } 49515798Smckusick /* notfound: */ 49615660Smckusick /* 49715798Smckusick * If we started in the middle of the directory and failed 49815660Smckusick * to find our target, we must check the beginning as well. 49915660Smckusick */ 50015660Smckusick if (numdirpasses == 2) { 50115660Smckusick numdirpasses--; 50216688Smckusick ndp->ni_offset = 0; 50315660Smckusick endsearch = u.u_ncache.nc_prevoffset; 50415660Smckusick goto searchloop; 50515660Smckusick } 5067534Sroot /* 5077534Sroot * If creating, and at end of pathname and current 5089166Ssam * directory has not been removed, then can consider 5099166Ssam * allowing file to be created. 5107534Sroot */ 5119166Ssam if (flag == CREATE && *cp == 0 && dp->i_nlink != 0) { 5125972Swnj /* 5137534Sroot * Access for write is interpreted as allowing 5147534Sroot * creation of files in the directory. 5155972Swnj */ 5167534Sroot if (access(dp, IWRITE)) 5177534Sroot goto bad; 5185972Swnj /* 5197534Sroot * Return an indication of where the new directory 5207534Sroot * entry should be put. If we didn't find a slot, 52116688Smckusick * then set ndp->ni_count to 0 indicating that the new 52216688Smckusick * slot belongs at the end of the directory. If we found 52316688Smckusick * a slot, then the new entry can be put in the range 52416688Smckusick * [ndp->ni_offset .. ndp->ni_offset + ndp->ni_count) 5255972Swnj */ 52615660Smckusick if (slotstatus == NONE) { 52716688Smckusick ndp->ni_offset = roundup(dp->i_size, DIRBLKSIZ); 52816688Smckusick ndp->ni_count = 0; 52918027Smckusick enduseful = ndp->ni_offset; 53015660Smckusick } else { 53116688Smckusick ndp->ni_offset = slotoffset; 53216688Smckusick ndp->ni_count = slotsize; 53318027Smckusick if (enduseful < slotoffset + slotsize) 53418027Smckusick enduseful = slotoffset + slotsize; 5355972Swnj } 53618027Smckusick ndp->ni_endoff = roundup(enduseful, DIRBLKSIZ); 5377534Sroot dp->i_flag |= IUPD|ICHG; 5387534Sroot if (bp) 5397534Sroot brelse(bp); 54016681Smckusick nbp->av_forw = freenamebuf; 54116681Smckusick freenamebuf = nbp; 5425972Swnj /* 5437534Sroot * We return with the directory locked, so that 5447534Sroot * the parameters we set up above will still be 5457534Sroot * valid if we actually decide to do a direnter(). 5467534Sroot * We return NULL to indicate that the entry doesn't 5477534Sroot * currently exist, leaving a pointer to the (locked) 54816688Smckusick * directory inode in ndp->ni_pdir. 5495972Swnj */ 55016688Smckusick ndp->ni_pdir = dp; 5517534Sroot return (NULL); 5527534Sroot } 5537534Sroot u.u_error = ENOENT; 5547534Sroot goto bad; 5557534Sroot found: 55615798Smckusick if (numdirpasses == 2) 55715798Smckusick nchstats.ncs_pass2++; 5587534Sroot /* 5597534Sroot * Check that directory length properly reflects presence 5607534Sroot * of this entry. 5617534Sroot */ 5627605Ssam if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) { 56316688Smckusick dirbad(dp, ndp->ni_offset, "i_size too small"); 5647605Ssam dp->i_size = entryoffsetinblock + DIRSIZ(ep); 5657534Sroot dp->i_flag |= IUPD|ICHG; 5667534Sroot } 5677534Sroot 5687534Sroot /* 56915660Smckusick * Found component in pathname. 57015798Smckusick * If the final component of path name, save information 57115660Smckusick * in the cache as to where the entry was found. 5727534Sroot */ 57315660Smckusick if (*cp == '\0' && flag == LOOKUP) { 57416688Smckusick u.u_ncache.nc_prevoffset = ndp->ni_offset; 57515660Smckusick u.u_ncache.nc_inumber = dp->i_number; 57615660Smckusick u.u_ncache.nc_dev = dp->i_dev; 57715660Smckusick u.u_ncache.nc_time = time.tv_sec; 57815660Smckusick } 57915660Smckusick /* 58018109Smckusick * Save directory entry's inode number and reclen in ndp->ni_dent, 58115660Smckusick * and release directory buffer. 58215660Smckusick */ 58318109Smckusick ndp->ni_dent.d_ino = ep->d_ino; 58418109Smckusick ndp->ni_dent.d_reclen = ep->d_reclen; 5857534Sroot brelse(bp); 5867534Sroot bp = NULL; 5877534Sroot 5887534Sroot /* 5897534Sroot * If deleting, and at end of pathname, return 5907534Sroot * parameters which can be used to remove file. 5919166Ssam * If the lockparent flag isn't set, we return only 59216688Smckusick * the directory (in ndp->ni_pdir), otherwise we go 5939166Ssam * on and lock the inode, being careful with ".". 5947534Sroot */ 5959166Ssam if (flag == DELETE && *cp == 0) { 5967534Sroot /* 5977534Sroot * Write access to directory required to delete files. 5987534Sroot */ 5997534Sroot if (access(dp, IWRITE)) 6007534Sroot goto bad; 60116688Smckusick ndp->ni_pdir = dp; /* for dirremove() */ 6027534Sroot /* 60316688Smckusick * Return pointer to current entry in ndp->ni_offset, 6047534Sroot * and distance past previous entry (if there 60516688Smckusick * is a previous entry in this block) in ndp->ni_count. 60616688Smckusick * Save directory inode pointer in ndp->ni_pdir for dirremove(). 6077534Sroot */ 60816688Smckusick if ((ndp->ni_offset&(DIRBLKSIZ-1)) == 0) 60916688Smckusick ndp->ni_count = 0; 6107534Sroot else 61116688Smckusick ndp->ni_count = ndp->ni_offset - prevoff; 6129166Ssam if (lockparent) { 61316688Smckusick if (dp->i_number == ndp->ni_dent.d_ino) 6149166Ssam dp->i_count++; 6159166Ssam else { 61616688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 6179166Ssam if (dp == NULL) { 61816688Smckusick iput(ndp->ni_pdir); 6199166Ssam goto bad; 6209166Ssam } 62115798Smckusick /* 62216046Skarels * If directory is "sticky", then user must own 62315798Smckusick * the directory, or the file in it, else he 62415798Smckusick * may not delete it (unless he's root). This 62515798Smckusick * implements append-only directories. 62615798Smckusick */ 62716688Smckusick if ((ndp->ni_pdir->i_mode & ISVTX) && 62815798Smckusick u.u_uid != 0 && 62916688Smckusick u.u_uid != ndp->ni_pdir->i_uid && 63015798Smckusick dp->i_uid != u.u_uid) { 63116688Smckusick iput(ndp->ni_pdir); 63215798Smckusick u.u_error = EPERM; 63315798Smckusick goto bad; 63415798Smckusick } 6359166Ssam } 6369166Ssam } 63716681Smckusick nbp->av_forw = freenamebuf; 63816681Smckusick freenamebuf = nbp; 6397534Sroot return (dp); 6407534Sroot } 6417534Sroot 6427534Sroot /* 6437534Sroot * Special handling for ".." allowing chdir out of mounted 6447534Sroot * file system: indirect .. in root inode to reevaluate 6457534Sroot * in directory file system was mounted on. 6467534Sroot */ 64716658Smckusick if (isdotdot) { 64818109Smckusick if (dp == u.u_rdir) { 64916688Smckusick ndp->ni_dent.d_ino = dp->i_number; 65018109Smckusick makeentry = 0; 65118109Smckusick } else if (ndp->ni_dent.d_ino == ROOTINO && 6527534Sroot dp->i_number == ROOTINO) { 6537534Sroot for (i = 1; i < NMOUNT; i++) 6547534Sroot if (mount[i].m_bufp != NULL && 6557534Sroot mount[i].m_dev == dp->i_dev) { 6566571Smckusic iput(dp); 6577534Sroot dp = mount[i].m_inodp; 65816666Smckusick ILOCK(dp); 6595972Swnj dp->i_count++; 6607534Sroot fs = dp->i_fs; 6617534Sroot cp -= 2; /* back over .. */ 6627534Sroot goto dirloop2; 6635972Swnj } 66430Sbill } 6657534Sroot } 6667534Sroot 6677534Sroot /* 6689166Ssam * If rewriting (rename), return the inode and the 6699166Ssam * information required to rewrite the present directory 6709166Ssam * Must get inode of directory entry to verify it's a 6719166Ssam * regular file, or empty directory. 6729166Ssam */ 6739166Ssam if ((flag == CREATE && lockparent) && *cp == 0) { 6749166Ssam if (access(dp, IWRITE)) 6759166Ssam goto bad; 67616688Smckusick ndp->ni_pdir = dp; /* for dirrewrite() */ 6779166Ssam /* 6789166Ssam * Careful about locking second inode. 6799166Ssam * This can only occur if the target is ".". 6809166Ssam */ 68116688Smckusick if (dp->i_number == ndp->ni_dent.d_ino) { 6829166Ssam u.u_error = EISDIR; /* XXX */ 6839166Ssam goto bad; 6849166Ssam } 68516688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 6869166Ssam if (dp == NULL) { 68716688Smckusick iput(ndp->ni_pdir); 6889166Ssam goto bad; 6899166Ssam } 69016681Smckusick nbp->av_forw = freenamebuf; 69116681Smckusick freenamebuf = nbp; 6929166Ssam return (dp); 6939166Ssam } 6949166Ssam 6959166Ssam /* 69612011Smckusick * Check for symbolic link, which may require us to massage the 69712011Smckusick * name before we continue translation. We do not `iput' the 69812011Smckusick * directory because we may need it again if the symbolic link 69912011Smckusick * is relative to the current directory. Instead we save it 70012011Smckusick * unlocked as "pdp". We must get the target inode before unlocking 70112011Smckusick * the directory to insure that the inode will not be removed 70212011Smckusick * before we get it. We prevent deadlock by always fetching 70312011Smckusick * inodes from the root, moving down the directory tree. Thus 70412011Smckusick * when following backward pointers ".." we must unlock the 70512011Smckusick * parent directory before getting the requested directory. 70612011Smckusick * There is a potential race condition here if both the current 70712011Smckusick * and parent directories are removed before the `iget' for the 70812011Smckusick * inode associated with ".." returns. We hope that this occurs 70912011Smckusick * infrequently since we cannot avoid this race condition without 71012492Ssam * implementing a sophisticated deadlock detection algorithm. 71112011Smckusick * Note also that this simple deadlock detection scheme will not 71212011Smckusick * work if the file system has any hard links other than ".." 71312011Smckusick * that point backwards in the directory structure. 7147534Sroot */ 7157534Sroot pdp = dp; 71615798Smckusick if (isdotdot) { 71716666Smckusick IUNLOCK(pdp); /* race to get the inode */ 71816688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 71912011Smckusick if (dp == NULL) 72012011Smckusick goto bad2; 72116688Smckusick } else if (dp->i_number == ndp->ni_dent.d_ino) { 72212011Smckusick dp->i_count++; /* we want ourself, ie "." */ 72312011Smckusick } else { 72416688Smckusick dp = iget(dp->i_dev, fs, ndp->ni_dent.d_ino); 72516666Smckusick IUNLOCK(pdp); 72612011Smckusick if (dp == NULL) 72712011Smckusick goto bad2; 72812011Smckusick } 72915798Smckusick 73015798Smckusick /* 73126273Skarels * Insert name into cache if appropriate. 73215798Smckusick */ 73318109Smckusick if (makeentry) { 73415798Smckusick if (ncp != NULL) 73515798Smckusick panic("nami: duplicating cache"); 73626273Skarels /* 73726273Skarels * Free the cache slot at head of lru chain. 73826273Skarels */ 73915798Smckusick if (ncp = nchhead) { 74026273Skarels /* remove from lru chain */ 74115798Smckusick *ncp->nc_prev = ncp->nc_nxt; 74215798Smckusick if (ncp->nc_nxt) 74315798Smckusick ncp->nc_nxt->nc_prev = ncp->nc_prev; 74415798Smckusick else 74515798Smckusick nchtail = ncp->nc_prev; 74626273Skarels remque(ncp); /* remove from old hash chain */ 74726273Skarels /* grab the inode we just found */ 74815798Smckusick ncp->nc_ip = dp; 74926273Skarels /* fill in cache info */ 75015798Smckusick ncp->nc_ino = pdp->i_number; /* parents inum */ 75115798Smckusick ncp->nc_dev = pdp->i_dev; /* & device */ 75215798Smckusick ncp->nc_idev = dp->i_dev; /* our device */ 75316643Ssam ncp->nc_id = dp->i_id; /* identifier */ 75416688Smckusick ncp->nc_nlen = ndp->ni_dent.d_namlen; 75526273Skarels bcopy(ndp->ni_dent.d_name, ncp->nc_name, 75626273Skarels (unsigned)ncp->nc_nlen); 75726273Skarels /* link at end of lru chain */ 75815798Smckusick ncp->nc_nxt = NULL; 75915798Smckusick ncp->nc_prev = nchtail; 76015798Smckusick *nchtail = ncp; 76115798Smckusick nchtail = &ncp->nc_nxt; 76226273Skarels /* and insert on hash chain */ 76315798Smckusick insque(ncp, nhp); 76415798Smckusick } 76515798Smckusick } 76615798Smckusick 76715798Smckusick haveino: 7687534Sroot fs = dp->i_fs; 7697534Sroot 7707534Sroot /* 7717534Sroot * Check for symbolic link 7727534Sroot */ 77316688Smckusick if ((dp->i_mode & IFMT) == IFLNK && 77416688Smckusick ((ndp->ni_nameiop & FOLLOW) || *cp == '/')) { 7757825Sroot u_int pathlen = strlen(cp) + 1; 7767534Sroot 77721014Smckusick if (dp->i_size + pathlen >= MAXPATHLEN - 1) { 77821014Smckusick u.u_error = ENAMETOOLONG; 77921014Smckusick goto bad2; 78021014Smckusick } 78121014Smckusick if (++nlink > MAXSYMLINKS) { 7827534Sroot u.u_error = ELOOP; 7837534Sroot goto bad2; 7847534Sroot } 7858957Sroot ovbcopy(cp, nbp->b_un.b_addr + dp->i_size, pathlen); 7867751Sroot u.u_error = 7879166Ssam rdwri(UIO_READ, dp, nbp->b_un.b_addr, (int)dp->i_size, 788*26360Skarels (off_t)0, 1, (int *)0); 7897534Sroot if (u.u_error) 7907534Sroot goto bad2; 7917534Sroot cp = nbp->b_un.b_addr; 7927534Sroot iput(dp); 7935972Swnj if (*cp == '/') { 7947534Sroot irele(pdp); 7955972Swnj while (*cp == '/') 7965972Swnj cp++; 7977534Sroot if ((dp = u.u_rdir) == NULL) 7987534Sroot dp = rootdir; 79916666Smckusick ILOCK(dp); 8007534Sroot dp->i_count++; 8017534Sroot } else { 8027534Sroot dp = pdp; 80316666Smckusick ILOCK(dp); 8045972Swnj } 8057534Sroot fs = dp->i_fs; 8067534Sroot goto dirloop; 80730Sbill } 8087534Sroot 80930Sbill /* 8107534Sroot * Not a symbolic link. If more pathname, 8117534Sroot * continue at next component, else return. 81230Sbill */ 8137534Sroot if (*cp == '/') { 8147534Sroot while (*cp == '/') 8157534Sroot cp++; 8169166Ssam irele(pdp); 8177534Sroot goto dirloop; 81830Sbill } 81916681Smckusick nbp->av_forw = freenamebuf; 82016681Smckusick freenamebuf = nbp; 8219166Ssam if (lockparent) 82216688Smckusick ndp->ni_pdir = pdp; 8239166Ssam else 8249166Ssam irele(pdp); 8257534Sroot return (dp); 8267534Sroot bad2: 8277534Sroot irele(pdp); 8287534Sroot bad: 8297534Sroot if (bp) 8307534Sroot brelse(bp); 8317534Sroot if (dp) 8327534Sroot iput(dp); 83316681Smckusick nbp->av_forw = freenamebuf; 83416681Smckusick freenamebuf = nbp; 8356571Smckusic return (NULL); 83630Sbill } 83730Sbill 83815798Smckusick 83916688Smckusick dirbad(ip, offset, how) 8407534Sroot struct inode *ip; 84116688Smckusick off_t offset; 8427534Sroot char *how; 8437534Sroot { 8447534Sroot 8457534Sroot printf("%s: bad dir ino %d at offset %d: %s\n", 84616688Smckusick ip->i_fs->fs_fsmnt, ip->i_number, offset, how); 8477534Sroot } 8487534Sroot 84916657Smckusick /* 85016657Smckusick * Do consistency checking on a directory entry: 85116657Smckusick * record length must be multiple of 4 85216657Smckusick * entry must fit in rest of its DIRBLKSIZ block 85316657Smckusick * record must be large enough to contain entry 85416657Smckusick * name is not longer than MAXNAMLEN 85516657Smckusick * name must be as long as advertised, and null terminated 85616657Smckusick */ 85716657Smckusick dirbadentry(ep, entryoffsetinblock) 8587534Sroot register struct direct *ep; 85916657Smckusick int entryoffsetinblock; 8607534Sroot { 8617534Sroot register int i; 8627534Sroot 86326273Skarels if ((ep->d_reclen & 0x3) != 0 || 86416657Smckusick ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || 86516657Smckusick ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN) 86616657Smckusick return (1); 8677534Sroot for (i = 0; i < ep->d_namlen; i++) 86816688Smckusick if (ep->d_name[i] == '\0') 8697534Sroot return (1); 8707534Sroot return (ep->d_name[i]); 8717534Sroot } 8727534Sroot 87330Sbill /* 8747534Sroot * Write a directory entry after a call to namei, using the parameters 8757534Sroot * which it left in the u. area. The argument ip is the inode which 87616688Smckusick * the new directory entry will refer to. The u. area field ndp->ni_pdir is 8777534Sroot * a pointer to the directory to be written, which was left locked by 87816688Smckusick * namei. Remaining parameters (ndp->ni_offset, ndp->ni_count) indicate 8797534Sroot * how the space for the new entry is to be gotten. 8807534Sroot */ 88116688Smckusick direnter(ip, ndp) 8827534Sroot struct inode *ip; 88316688Smckusick register struct nameidata *ndp; 8845972Swnj { 8857534Sroot register struct direct *ep, *nep; 88618027Smckusick register struct inode *dp = ndp->ni_pdir; 8877534Sroot struct buf *bp; 88811639Ssam int loc, spacefree, error = 0; 8898631Sroot u_int dsize; 8908631Sroot int newentrysize; 8917534Sroot char *dirbuf; 8925972Swnj 89316688Smckusick ndp->ni_dent.d_ino = ip->i_number; 89416688Smckusick newentrysize = DIRSIZ(&ndp->ni_dent); 89516688Smckusick if (ndp->ni_count == 0) { 8967534Sroot /* 89716688Smckusick * If ndp->ni_count is 0, then namei could find no space in the 89816688Smckusick * directory. In this case ndp->ni_offset will be on a directory 8997534Sroot * block boundary and we will write the new entry into a fresh 9007534Sroot * block. 9017534Sroot */ 90216688Smckusick if (ndp->ni_offset&(DIRBLKSIZ-1)) 9037534Sroot panic("wdir: newblk"); 90416688Smckusick ndp->ni_dent.d_reclen = DIRBLKSIZ; 90518027Smckusick error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 90616688Smckusick newentrysize, ndp->ni_offset, 1, (int *)0); 90718104Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 90818104Smckusick panic("wdir: blksize"); /* XXX - should grow w/bmap() */ 90918104Smckusick else 91018104Smckusick dp->i_size = roundup(dp->i_size, DIRBLKSIZ); 91118027Smckusick iput(dp); 91210849Ssam return (error); 9137534Sroot } 9147534Sroot 9157534Sroot /* 91616688Smckusick * If ndp->ni_count is non-zero, then namei found space for the new 91716688Smckusick * entry in the range ndp->ni_offset to ndp->ni_offset + ndp->ni_count. 9187534Sroot * in the directory. To use this space, we may have to compact 9197534Sroot * the entries located there, by copying them together towards 9207534Sroot * the beginning of the block, leaving the free space in 9217534Sroot * one usable chunk at the end. 9227534Sroot */ 9237534Sroot 9247534Sroot /* 9257534Sroot * Increase size of directory if entry eats into new space. 9267534Sroot * This should never push the size past a new multiple of 9277534Sroot * DIRBLKSIZE. 92818104Smckusick * 92918104Smckusick * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. 9307534Sroot */ 93118027Smckusick if (ndp->ni_offset + ndp->ni_count > dp->i_size) 93218027Smckusick dp->i_size = ndp->ni_offset + ndp->ni_count; 9337534Sroot /* 9347534Sroot * Get the block containing the space for the new directory 93510849Ssam * entry. Should return error by result instead of u.u_error. 9367534Sroot */ 93718027Smckusick bp = blkatoff(dp, ndp->ni_offset, (char **)&dirbuf); 9389166Ssam if (bp == 0) { 93918027Smckusick iput(dp); 94010849Ssam return (u.u_error); 9419166Ssam } 9427534Sroot /* 9437534Sroot * Find space for the new entry. In the simple case, the 9447534Sroot * entry at offset base will have the space. If it does 9457534Sroot * not, then namei arranged that compacting the region 94616688Smckusick * ndp->ni_offset to ndp->ni_offset+ndp->ni_count would yield the space. 9477534Sroot */ 9487534Sroot ep = (struct direct *)dirbuf; 9497534Sroot dsize = DIRSIZ(ep); 95011639Ssam spacefree = ep->d_reclen - dsize; 95116688Smckusick for (loc = ep->d_reclen; loc < ndp->ni_count; ) { 9527534Sroot nep = (struct direct *)(dirbuf + loc); 9537534Sroot if (ep->d_ino) { 9547534Sroot /* trim the existing slot */ 9557534Sroot ep->d_reclen = dsize; 9567534Sroot ep = (struct direct *)((char *)ep + dsize); 9577534Sroot } else { 9587534Sroot /* overwrite; nothing there; header is ours */ 95911639Ssam spacefree += dsize; 9607534Sroot } 9617534Sroot dsize = DIRSIZ(nep); 96211639Ssam spacefree += nep->d_reclen - dsize; 9637534Sroot loc += nep->d_reclen; 9647825Sroot bcopy((caddr_t)nep, (caddr_t)ep, dsize); 9657534Sroot } 9667534Sroot /* 9677534Sroot * Update the pointer fields in the previous entry (if any), 9687534Sroot * copy in the new entry, and write out the block. 9697534Sroot */ 9707534Sroot if (ep->d_ino == 0) { 97111639Ssam if (spacefree + dsize < newentrysize) 9727534Sroot panic("wdir: compact1"); 97316688Smckusick ndp->ni_dent.d_reclen = spacefree + dsize; 9747534Sroot } else { 97511639Ssam if (spacefree < newentrysize) 9767534Sroot panic("wdir: compact2"); 97716688Smckusick ndp->ni_dent.d_reclen = spacefree; 9787534Sroot ep->d_reclen = dsize; 9797534Sroot ep = (struct direct *)((char *)ep + dsize); 9807534Sroot } 98116688Smckusick bcopy((caddr_t)&ndp->ni_dent, (caddr_t)ep, (u_int)newentrysize); 9827534Sroot bwrite(bp); 98318027Smckusick dp->i_flag |= IUPD|ICHG; 98418027Smckusick if (ndp->ni_endoff && ndp->ni_endoff < dp->i_size) 98526273Skarels itrunc(dp, (u_long)ndp->ni_endoff); 98618027Smckusick iput(dp); 98710849Ssam return (error); 9885972Swnj } 9896571Smckusic 9909166Ssam /* 9919166Ssam * Remove a directory entry after a call to namei, using the 9929166Ssam * parameters which it left in the u. area. The u. entry 99316688Smckusick * ni_offset contains the offset into the directory of the 99416688Smckusick * entry to be eliminated. The ni_count field contains the 9959166Ssam * size of the previous record in the directory. If this 9969166Ssam * is 0, the first entry is being deleted, so we need only 9979166Ssam * zero the inode number to mark the entry as free. If the 9989166Ssam * entry isn't the first in the directory, we must reclaim 9999166Ssam * the space of the now empty record by adding the record size 10009166Ssam * to the size of the previous entry. 10019166Ssam */ 100216688Smckusick dirremove(ndp) 100316688Smckusick register struct nameidata *ndp; 10046571Smckusic { 100516688Smckusick register struct inode *dp = ndp->ni_pdir; 10067534Sroot register struct buf *bp; 10077534Sroot struct direct *ep; 10086571Smckusic 100916688Smckusick if (ndp->ni_count == 0) { 10107534Sroot /* 10117534Sroot * First entry in block: set d_ino to zero. 10127534Sroot */ 101316688Smckusick ndp->ni_dent.d_ino = 0; 101416688Smckusick (void) rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 101516688Smckusick (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0); 10169269Ssam } else { 10177534Sroot /* 10187534Sroot * Collapse new free space into previous entry. 10197534Sroot */ 1020*26360Skarels bp = blkatoff(dp, ndp->ni_offset - ndp->ni_count, (char **)&ep); 10217534Sroot if (bp == 0) 10227534Sroot return (0); 102316688Smckusick ep->d_reclen += ndp->ni_dent.d_reclen; 10247534Sroot bwrite(bp); 10257534Sroot dp->i_flag |= IUPD|ICHG; 10267534Sroot } 10277534Sroot return (1); 10286571Smckusic } 10297534Sroot 10307605Ssam /* 10319166Ssam * Rewrite an existing directory entry to point at the inode 10329166Ssam * supplied. The parameters describing the directory entry are 10339166Ssam * set up by a call to namei. 10349166Ssam */ 103516688Smckusick dirrewrite(dp, ip, ndp) 10369166Ssam struct inode *dp, *ip; 103716688Smckusick struct nameidata *ndp; 10389166Ssam { 10399166Ssam 104016688Smckusick ndp->ni_dent.d_ino = ip->i_number; 104116688Smckusick u.u_error = rdwri(UIO_WRITE, dp, (caddr_t)&ndp->ni_dent, 104216688Smckusick (int)DIRSIZ(&ndp->ni_dent), ndp->ni_offset, 1, (int *)0); 10439166Ssam iput(dp); 10449166Ssam } 10459166Ssam 10469166Ssam /* 10477605Ssam * Return buffer with contents of block "offset" 10487605Ssam * from the beginning of directory "ip". If "res" 10497605Ssam * is non-zero, fill it in with a pointer to the 10507605Ssam * remaining space in the directory. 10517605Ssam */ 10527534Sroot struct buf * 10537605Ssam blkatoff(ip, offset, res) 10547534Sroot struct inode *ip; 10557534Sroot off_t offset; 10567534Sroot char **res; 10577534Sroot { 10587534Sroot register struct fs *fs = ip->i_fs; 10598672Sroot daddr_t lbn = lblkno(fs, offset); 10607534Sroot int bsize = blksize(fs, ip, lbn); 10617534Sroot register struct buf *bp; 106218663Smckusick daddr_t bn; 10637534Sroot 106418663Smckusick bn = bmap(ip, lbn, B_READ, bsize); 10657534Sroot if (u.u_error) 10667534Sroot return (0); 106718663Smckusick if (bn == (daddr_t)-1) { 106818663Smckusick dirbad(ip, offset, "hole in dir"); 106918663Smckusick return (0); 107018663Smckusick } 107118663Smckusick bp = bread(ip->i_dev, fsbtodb(fs, bn), bsize); 10727534Sroot if (bp->b_flags & B_ERROR) { 10737534Sroot brelse(bp); 10747534Sroot return (0); 10757534Sroot } 10767534Sroot if (res) 107718663Smckusick *res = bp->b_un.b_addr + blkoff(fs, offset); 10787534Sroot return (bp); 10797534Sroot } 10809166Ssam 10819166Ssam /* 10829166Ssam * Check if a directory is empty or not. 10839166Ssam * Inode supplied must be locked. 108412817Ssam * 108512817Ssam * Using a struct dirtemplate here is not precisely 108612817Ssam * what we want, but better than using a struct direct. 108712817Ssam * 108812817Ssam * NB: does not handle corrupted directories. 10899166Ssam */ 109016777Smckusick dirempty(ip, parentino) 10919863Ssam register struct inode *ip; 109216777Smckusick ino_t parentino; 10939166Ssam { 10949166Ssam register off_t off; 109512817Ssam struct dirtemplate dbuf; 109612817Ssam register struct direct *dp = (struct direct *)&dbuf; 10979863Ssam int error, count; 109812817Ssam #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) 10999166Ssam 11009166Ssam for (off = 0; off < ip->i_size; off += dp->d_reclen) { 110112817Ssam error = rdwri(UIO_READ, ip, (caddr_t)dp, MINDIRSIZ, 110212817Ssam off, 1, &count); 110312817Ssam /* 110412817Ssam * Since we read MINDIRSIZ, residual must 110512817Ssam * be 0 unless we're at end of file. 110612817Ssam */ 110712817Ssam if (error || count != 0) 11089166Ssam return (0); 110924534Smckusick /* avoid infinite loops */ 111026273Skarels if (dp->d_reclen == 0) 111124534Smckusick return (0); 111212817Ssam /* skip empty entries */ 11139166Ssam if (dp->d_ino == 0) 11149166Ssam continue; 111512817Ssam /* accept only "." and ".." */ 111612817Ssam if (dp->d_namlen > 2) 111712817Ssam return (0); 11189166Ssam if (dp->d_name[0] != '.') 11199166Ssam return (0); 112012817Ssam /* 112112817Ssam * At this point d_namlen must be 1 or 2. 112212817Ssam * 1 implies ".", 2 implies ".." if second 112312817Ssam * char is also "." 112412817Ssam */ 112516777Smckusick if (dp->d_namlen == 1) 11269166Ssam continue; 112716777Smckusick if (dp->d_name[1] == '.' && dp->d_ino == parentino) 112816777Smckusick continue; 11299166Ssam return (0); 11309166Ssam } 11319166Ssam return (1); 11329166Ssam } 113312815Smckusick 113412815Smckusick /* 113512815Smckusick * Check if source directory is in the path of the target directory. 113612815Smckusick * Target is supplied locked, source is unlocked. 113712815Smckusick * The target is always iput() before returning. 113812815Smckusick */ 113912815Smckusick checkpath(source, target) 114012815Smckusick struct inode *source, *target; 114112815Smckusick { 114212815Smckusick struct dirtemplate dirbuf; 114312815Smckusick register struct inode *ip; 114412815Smckusick int error = 0; 114512815Smckusick 114612815Smckusick ip = target; 114712815Smckusick if (ip->i_number == source->i_number) { 114812815Smckusick error = EEXIST; 114912815Smckusick goto out; 115012815Smckusick } 115112815Smckusick if (ip->i_number == ROOTINO) 115212815Smckusick goto out; 115312815Smckusick 115412815Smckusick for (;;) { 115512815Smckusick if ((ip->i_mode&IFMT) != IFDIR) { 115612815Smckusick error = ENOTDIR; 115712815Smckusick break; 115812815Smckusick } 115912815Smckusick error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf, 116012815Smckusick sizeof (struct dirtemplate), (off_t)0, 1, (int *)0); 116112815Smckusick if (error != 0) 116212815Smckusick break; 116312815Smckusick if (dirbuf.dotdot_namlen != 2 || 116416658Smckusick dirbuf.dotdot_name[0] != '.' || 116516658Smckusick dirbuf.dotdot_name[1] != '.') { 116612815Smckusick error = ENOTDIR; 116712815Smckusick break; 116812815Smckusick } 116912815Smckusick if (dirbuf.dotdot_ino == source->i_number) { 117012815Smckusick error = EINVAL; 117112815Smckusick break; 117212815Smckusick } 117312815Smckusick if (dirbuf.dotdot_ino == ROOTINO) 117412815Smckusick break; 117512815Smckusick iput(ip); 117612815Smckusick ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino); 117712815Smckusick if (ip == NULL) { 117812815Smckusick error = u.u_error; 117912815Smckusick break; 118012815Smckusick } 118112815Smckusick } 118212815Smckusick 118312815Smckusick out: 118412815Smckusick if (error == ENOTDIR) 118512815Smckusick printf("checkpath: .. not a directory\n"); 118612815Smckusick if (ip != NULL) 118712815Smckusick iput(ip); 118812815Smckusick return (error); 118912815Smckusick } 119015798Smckusick 119115798Smckusick /* 119215798Smckusick * Name cache initialization, from main() when we are booting 119315798Smckusick */ 119415798Smckusick nchinit() 119515798Smckusick { 119615798Smckusick register union nchash *nchp; 119726306Skarels register struct namecache *ncp; 119815798Smckusick 119915798Smckusick nchhead = 0; 120015798Smckusick nchtail = &nchhead; 120126306Skarels for (ncp = namecache; ncp < &namecache[nchsize]; ncp++) { 120215798Smckusick ncp->nc_forw = ncp; /* hash chain */ 120315798Smckusick ncp->nc_back = ncp; 120415798Smckusick ncp->nc_nxt = NULL; /* lru chain */ 120515798Smckusick *nchtail = ncp; 120615798Smckusick ncp->nc_prev = nchtail; 120715798Smckusick nchtail = &ncp->nc_nxt; 120815798Smckusick /* all else is zero already */ 120915798Smckusick } 121015798Smckusick for (nchp = nchash; nchp < &nchash[NCHHASH]; nchp++) { 121115798Smckusick nchp->nch_head[0] = nchp; 121215798Smckusick nchp->nch_head[1] = nchp; 121315798Smckusick } 121415798Smckusick } 121515798Smckusick 121615798Smckusick /* 121715798Smckusick * Cache flush, called when filesys is umounted to 121815798Smckusick * remove entries that would now be invalid 121915798Smckusick * 122015798Smckusick * The line "nxtcp = nchhead" near the end is to avoid potential problems 122115798Smckusick * if the cache lru chain is modified while we are dumping the 122215798Smckusick * inode. This makes the algorithm O(n^2), but do you think I care? 122315798Smckusick */ 122415798Smckusick nchinval(dev) 122515798Smckusick register dev_t dev; 122615798Smckusick { 122726306Skarels register struct namecache *ncp, *nxtcp; 122815798Smckusick 122915798Smckusick for (ncp = nchhead; ncp; ncp = nxtcp) { 123015798Smckusick nxtcp = ncp->nc_nxt; 123115798Smckusick if (ncp->nc_ip == NULL || 123215798Smckusick (ncp->nc_idev != dev && ncp->nc_dev != dev)) 123315798Smckusick continue; 123426273Skarels /* free the resources we had */ 123515798Smckusick ncp->nc_idev = NODEV; 123615798Smckusick ncp->nc_dev = NODEV; 123716658Smckusick ncp->nc_id = NULL; 123815798Smckusick ncp->nc_ino = 0; 123916658Smckusick ncp->nc_ip = NULL; 124026273Skarels remque(ncp); /* remove entry from its hash chain */ 124126273Skarels ncp->nc_forw = ncp; /* and make a dummy one */ 124215798Smckusick ncp->nc_back = ncp; 124326273Skarels /* delete this entry from LRU chain */ 124415798Smckusick *ncp->nc_prev = nxtcp; 124515798Smckusick if (nxtcp) 124615798Smckusick nxtcp->nc_prev = ncp->nc_prev; 124715798Smckusick else 124815798Smckusick nchtail = ncp->nc_prev; 124926273Skarels /* cause rescan of list, it may have altered */ 125015798Smckusick nxtcp = nchhead; 125126273Skarels /* put the now-free entry at head of LRU */ 125215798Smckusick ncp->nc_nxt = nxtcp; 125315798Smckusick ncp->nc_prev = &nchhead; 125415798Smckusick nxtcp->nc_prev = &ncp->nc_nxt; 125515798Smckusick nchhead = ncp; 125615798Smckusick } 125715798Smckusick } 125817704Smckusick 125917704Smckusick /* 126017704Smckusick * Name cache invalidation of all entries. 126117704Smckusick */ 126217704Smckusick cacheinvalall() 126317704Smckusick { 126426306Skarels register struct namecache *ncp; 126517704Smckusick 126626306Skarels for (ncp = namecache; ncp < &namecache[nchsize]; ncp++) 126717704Smckusick ncp->nc_id = 0; 126817704Smckusick } 1269