+
+/*
+ * Check if source directory is in the path of the target directory.
+ * Target is supplied locked, source is unlocked.
+ * The target is always iput() before returning.
+ */
+checkpath(source, target)
+ struct inode *source, *target;
+{
+ struct dirtemplate dirbuf;
+ register struct inode *ip;
+ int error = 0;
+
+ ip = target;
+ if (ip->i_number == source->i_number) {
+ error = EEXIST;
+ goto out;
+ }
+ if (ip->i_number == ROOTINO)
+ goto out;
+
+ for (;;) {
+ if ((ip->i_mode&IFMT) != IFDIR) {
+ error = ENOTDIR;
+ break;
+ }
+ error = rdwri(UIO_READ, ip, (caddr_t)&dirbuf,
+ sizeof (struct dirtemplate), (off_t)0, 1, (int *)0);
+ if (error != 0)
+ break;
+ if (dirbuf.dotdot_namlen != 2 ||
+ dirbuf.dotdot_name[0] != '.' ||
+ dirbuf.dotdot_name[1] != '.') {
+ error = ENOTDIR;
+ break;
+ }
+ if (dirbuf.dotdot_ino == source->i_number) {
+ error = EINVAL;
+ break;
+ }
+ if (dirbuf.dotdot_ino == ROOTINO)
+ break;
+ iput(ip);
+ ip = iget(ip->i_dev, ip->i_fs, dirbuf.dotdot_ino);
+ if (ip == NULL) {
+ error = u.u_error;
+ break;
+ }
+ }
+
+out:
+ if (error == ENOTDIR)
+ printf("checkpath: .. not a directory\n");
+ if (ip != NULL)
+ iput(ip);
+ return (error);
+}
+
+/*
+ * Name cache initialization, from main() when we are booting
+ */
+nchinit()
+{
+ register union nchash *nchp;
+ register struct namecache *ncp;
+
+ nchhead = 0;
+ nchtail = &nchhead;
+ for (ncp = namecache; ncp < &namecache[nchsize]; ncp++) {
+ ncp->nc_forw = ncp; /* hash chain */
+ ncp->nc_back = ncp;
+ ncp->nc_nxt = NULL; /* lru chain */
+ *nchtail = ncp;
+ ncp->nc_prev = nchtail;
+ nchtail = &ncp->nc_nxt;
+ /* all else is zero already */
+ }
+ for (nchp = nchash; nchp < &nchash[NCHHASH]; nchp++) {
+ nchp->nch_head[0] = nchp;
+ nchp->nch_head[1] = nchp;
+ }
+}
+
+/*
+ * Cache flush, called when filesys is umounted to
+ * remove entries that would now be invalid
+ *
+ * The line "nxtcp = nchhead" near the end is to avoid potential problems
+ * if the cache lru chain is modified while we are dumping the
+ * inode. This makes the algorithm O(n^2), but do you think I care?
+ */
+nchinval(dev)
+ register dev_t dev;
+{
+ register struct namecache *ncp, *nxtcp;
+
+ for (ncp = nchhead; ncp; ncp = nxtcp) {
+ nxtcp = ncp->nc_nxt;
+ if (ncp->nc_ip == NULL ||
+ (ncp->nc_idev != dev && ncp->nc_dev != dev))
+ continue;
+ /* free the resources we had */
+ ncp->nc_idev = NODEV;
+ ncp->nc_dev = NODEV;
+ ncp->nc_id = NULL;
+ ncp->nc_ino = 0;
+ ncp->nc_ip = NULL;
+ remque(ncp); /* remove entry from its hash chain */
+ ncp->nc_forw = ncp; /* and make a dummy one */
+ ncp->nc_back = ncp;
+ /* delete this entry from LRU chain */
+ *ncp->nc_prev = nxtcp;
+ if (nxtcp)
+ nxtcp->nc_prev = ncp->nc_prev;
+ else
+ nchtail = ncp->nc_prev;
+ /* cause rescan of list, it may have altered */
+ nxtcp = nchhead;
+ /* put the now-free entry at head of LRU */
+ ncp->nc_nxt = nxtcp;
+ ncp->nc_prev = &nchhead;
+ nxtcp->nc_prev = &ncp->nc_nxt;
+ nchhead = ncp;
+ }
+}
+
+/*
+ * Name cache invalidation of all entries.
+ */
+cacheinvalall()
+{
+ register struct namecache *ncp;
+
+ for (ncp = namecache; ncp < &namecache[nchsize]; ncp++)
+ ncp->nc_id = 0;
+}