| 1 | /* vfs_lookup.c 4.8 81/11/08 */ |
| 2 | |
| 3 | #include "../h/param.h" |
| 4 | #include "../h/systm.h" |
| 5 | #include "../h/inode.h" |
| 6 | #include "../h/mount.h" |
| 7 | #include "../h/dir.h" |
| 8 | #include "../h/user.h" |
| 9 | #include "../h/buf.h" |
| 10 | #include "../h/conf.h" |
| 11 | |
| 12 | /* |
| 13 | * Convert a pathname into a pointer to |
| 14 | * an inode. Note that the inode is locked. |
| 15 | * |
| 16 | * func = function called to get next char of name |
| 17 | * &uchar if name is in user space |
| 18 | * &schar if name is in system space |
| 19 | * flag = 0 if name is sought |
| 20 | * 1 if name is to be created |
| 21 | * 2 if name is to be deleted |
| 22 | */ |
| 23 | struct inode * |
| 24 | namei(func, flag) |
| 25 | int (*func)(); |
| 26 | { |
| 27 | register struct inode *dp; |
| 28 | register c; |
| 29 | register char *cp; |
| 30 | struct buf *bp; |
| 31 | register struct direct *ep; |
| 32 | int i; |
| 33 | dev_t d; |
| 34 | off_t eo; |
| 35 | |
| 36 | /* |
| 37 | * If name starts with '/' start from |
| 38 | * root; otherwise start from current dir. |
| 39 | */ |
| 40 | |
| 41 | dp = u.u_cdir; |
| 42 | if ((c=(*func)()) == '/') |
| 43 | if ((dp = u.u_rdir) == NULL) |
| 44 | dp = rootdir; |
| 45 | (void) iget(dp->i_dev, dp->i_number); |
| 46 | while (c == '/') |
| 47 | c = (*func)(); |
| 48 | if (c == '\0' && flag != 0) |
| 49 | u.u_error = ENOENT; |
| 50 | |
| 51 | cloop: |
| 52 | /* |
| 53 | * Here dp contains pointer |
| 54 | * to last component matched. |
| 55 | */ |
| 56 | |
| 57 | if (u.u_error) |
| 58 | goto out; |
| 59 | if (c == '\0') |
| 60 | return(dp); |
| 61 | |
| 62 | /* |
| 63 | * If there is another component, |
| 64 | * Gather up name into |
| 65 | * users' dir buffer. |
| 66 | */ |
| 67 | |
| 68 | cp = &u.u_dbuf[0]; |
| 69 | while (c != '/' && c != '\0' && u.u_error == 0 ) { |
| 70 | if (flag==1 && c == ('/'|0200)) { |
| 71 | u.u_error = ENOENT; |
| 72 | goto out; |
| 73 | } |
| 74 | if (cp < &u.u_dbuf[DIRSIZ]) |
| 75 | *cp++ = c; |
| 76 | c = (*func)(); |
| 77 | } |
| 78 | while (cp < &u.u_dbuf[DIRSIZ]) |
| 79 | *cp++ = '\0'; |
| 80 | while (c == '/') |
| 81 | c = (*func)(); |
| 82 | |
| 83 | seloop: |
| 84 | /* |
| 85 | * dp must be a directory and |
| 86 | * must have X permission. |
| 87 | */ |
| 88 | |
| 89 | if ((dp->i_mode&IFMT) != IFDIR) |
| 90 | u.u_error = ENOTDIR; |
| 91 | (void) access(dp, IEXEC); |
| 92 | if (u.u_error) |
| 93 | goto out; |
| 94 | |
| 95 | /* |
| 96 | * set up to search a directory |
| 97 | */ |
| 98 | u.u_offset = 0; |
| 99 | u.u_segflg = 1; |
| 100 | eo = 0; |
| 101 | bp = NULL; |
| 102 | if (dp == u.u_rdir && u.u_dbuf[0] == '.' && |
| 103 | u.u_dbuf[1] == '.' && u.u_dbuf[2] == 0) |
| 104 | goto cloop; |
| 105 | |
| 106 | eloop: |
| 107 | |
| 108 | /* |
| 109 | * If at the end of the directory, |
| 110 | * the search failed. Report what |
| 111 | * is appropriate as per flag. |
| 112 | */ |
| 113 | |
| 114 | if (u.u_offset >= dp->i_size) { |
| 115 | if (bp != NULL) |
| 116 | brelse(bp); |
| 117 | if (flag==1 && c=='\0' && dp->i_nlink) { |
| 118 | if (access(dp, IWRITE)) |
| 119 | goto out; |
| 120 | u.u_pdir = dp; |
| 121 | if (eo) |
| 122 | u.u_offset = eo-sizeof(struct direct); |
| 123 | else |
| 124 | dp->i_flag |= IUPD|ICHG; |
| 125 | return(NULL); |
| 126 | } |
| 127 | u.u_error = ENOENT; |
| 128 | goto out; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * If offset is on a block boundary, |
| 133 | * read the next directory block. |
| 134 | * Release previous if it exists. |
| 135 | */ |
| 136 | |
| 137 | if ((u.u_offset&BMASK) == 0) { |
| 138 | if (bp != NULL) |
| 139 | brelse(bp); |
| 140 | bp = bread(dp->i_dev, |
| 141 | bmap(dp, (daddr_t)(u.u_offset>>BSHIFT), B_READ)); |
| 142 | if (bp->b_flags & B_ERROR) { |
| 143 | brelse(bp); |
| 144 | goto out; |
| 145 | } |
| 146 | ep = (struct direct *)bp->b_un.b_addr; |
| 147 | } else |
| 148 | ep++; |
| 149 | |
| 150 | /* |
| 151 | * Note first empty directory slot |
| 152 | * in eo for possible creat. |
| 153 | * String compare the directory entry |
| 154 | * and the current component. |
| 155 | * If they do not match, go back to eloop. |
| 156 | */ |
| 157 | |
| 158 | u.u_offset += sizeof(struct direct); |
| 159 | if (ep->d_ino == 0) { |
| 160 | if (eo == 0) |
| 161 | eo = u.u_offset; |
| 162 | goto eloop; |
| 163 | } |
| 164 | for(i=0; i<DIRSIZ; i++) { |
| 165 | if (u.u_dbuf[i] != ep->d_name[i]) |
| 166 | goto eloop; |
| 167 | if (u.u_dbuf[i] == 0) |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Here a component matched in a directory. |
| 173 | * If there is more pathname, go back to |
| 174 | * cloop, otherwise return. |
| 175 | */ |
| 176 | bcopy((caddr_t)ep, (caddr_t)&u.u_dent, sizeof(struct direct)); |
| 177 | if (bp != NULL) |
| 178 | brelse(bp); |
| 179 | if (flag==2 && c=='\0') { |
| 180 | if (access(dp, IWRITE)) |
| 181 | goto out; |
| 182 | return(dp); |
| 183 | } |
| 184 | d = dp->i_dev; |
| 185 | if (u.u_dent.d_ino == ROOTINO) |
| 186 | if (dp->i_number == ROOTINO) |
| 187 | if (u.u_dent.d_name[1] == '.') |
| 188 | for(i=1; i<NMOUNT; i++) |
| 189 | if (mount[i].m_bufp != NULL) |
| 190 | if (mount[i].m_dev == d) { |
| 191 | iput(dp); |
| 192 | dp = mount[i].m_inodp; |
| 193 | dp->i_count++; |
| 194 | ilock(dp); |
| 195 | goto seloop; |
| 196 | } |
| 197 | iput(dp); |
| 198 | dp = iget(d, u.u_dent.d_ino); |
| 199 | if (dp == NULL) |
| 200 | return(NULL); |
| 201 | goto cloop; |
| 202 | |
| 203 | out: |
| 204 | iput(dp); |
| 205 | return(NULL); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Return the next character from the |
| 210 | * kernel string pointed at by dirp. |
| 211 | */ |
| 212 | schar() |
| 213 | { |
| 214 | |
| 215 | return(*u.u_dirp++ & 0377); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Return the next character from the |
| 220 | * user string pointed at by dirp. |
| 221 | */ |
| 222 | uchar() |
| 223 | { |
| 224 | register c; |
| 225 | |
| 226 | c = fubyte(u.u_dirp++); |
| 227 | if (c == -1) |
| 228 | u.u_error = EFAULT; |
| 229 | return(c); |
| 230 | } |