Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
7604aef4 KM |
2 | * Copyright (c) 1989 The Regents of the University of California. |
3 | * All rights reserved. | |
da7c5cc6 | 4 | * |
7604aef4 KM |
5 | * Redistribution and use in source and binary forms are permitted |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
16 | * | |
a48aecd3 | 17 | * @(#)ufs_lookup.c 7.21 (Berkeley) %G% |
da7c5cc6 | 18 | */ |
10873320 | 19 | |
94368568 | 20 | #include "param.h" |
94368568 JB |
21 | #include "user.h" |
22 | #include "buf.h" | |
7604aef4 KM |
23 | #include "file.h" |
24 | #include "vnode.h" | |
a48aecd3 | 25 | #include "../ufs/quota.h" |
7604aef4 KM |
26 | #include "../ufs/inode.h" |
27 | #include "../ufs/fs.h" | |
10873320 | 28 | |
7604aef4 | 29 | struct nchstats nchstats; |
658f5fdc | 30 | int dirchk = 1; |
f93197fc KM |
31 | |
32 | /* | |
7604aef4 | 33 | * Convert a component of a pathname into a pointer to a locked inode. |
6bd0bb92 | 34 | * This is a very central and rather complicated routine. |
4f083fd7 | 35 | * If the file system is not maintained in a strict tree hierarchy, |
7f69b0e6 KM |
36 | * this can result in a deadlock situation (see comments in code below). |
37 | * | |
7604aef4 KM |
38 | * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on |
39 | * whether the name is to be looked up, created, renamed, or deleted. | |
40 | * When CREATE, RENAME, or DELETE is specified, information usable in | |
41 | * creating, renaming, or deleting a directory entry may be calculated. | |
42 | * If flag has LOCKPARENT or'ed into it and the target of the pathname | |
43 | * exists, lookup returns both the target and its parent directory locked. | |
44 | * When creating or renaming and LOCKPARENT is specified, the target may | |
45 | * not be ".". When deleting and LOCKPARENT is specified, the target may | |
46 | * be "."., but the caller must check to ensure it does an vrele and iput | |
47 | * instead of two iputs. | |
f93197fc | 48 | * |
7604aef4 | 49 | * Overall outline of ufs_lookup: |
f93197fc | 50 | * |
6bd0bb92 | 51 | * check accessibility of directory |
f93197fc | 52 | * look for name in cache, if found, then if at end of path |
7604aef4 | 53 | * and deleting or creating, drop it, else return name |
6bd0bb92 BJ |
54 | * search for name in directory, to found or notfound |
55 | * notfound: | |
7604aef4 | 56 | * if creating, return locked directory, leaving info on available slots |
6bd0bb92 BJ |
57 | * else return error |
58 | * found: | |
59 | * if at end of path and deleting, return information to allow delete | |
7604aef4 | 60 | * if at end of path and rewriting (RENAME and LOCKPARENT), lock target |
4f083fd7 | 61 | * inode and return info to allow rewrite |
7f69b0e6 KM |
62 | * if not at end, add name to cache; if at end and neither creating |
63 | * nor deleting, add name to cache | |
4f083fd7 | 64 | * |
7604aef4 | 65 | * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. |
10873320 | 66 | */ |
a48aecd3 KM |
67 | ufs_lookup(vdp, ndp) |
68 | register struct vnode *vdp; | |
d870be74 | 69 | register struct nameidata *ndp; |
10873320 | 70 | { |
0be364c5 | 71 | register struct inode *dp; /* the directory we are searching */ |
6bd0bb92 | 72 | register struct fs *fs; /* file system that directory is in */ |
7604aef4 | 73 | struct buf *bp = 0; /* a buffer of directory entries */ |
6bd0bb92 BJ |
74 | register struct direct *ep; /* the current directory entry */ |
75 | int entryoffsetinblock; /* offset of ep in bp's buffer */ | |
6bd0bb92 BJ |
76 | enum {NONE, COMPACT, FOUND} slotstatus; |
77 | int slotoffset = -1; /* offset of area with free space */ | |
78 | int slotsize; /* size of area at slotoffset */ | |
79 | int slotfreespace; /* amount of space free in slot */ | |
80 | int slotneeded; /* size of the entry we're seeking */ | |
84ef30ec KM |
81 | int numdirpasses; /* strategy for directory search */ |
82 | int endsearch; /* offset to end directory search */ | |
d870be74 | 83 | int prevoff; /* ndp->ni_offset of previous entry */ |
6bd0bb92 | 84 | struct inode *pdp; /* saved dp during symlink work */ |
7604aef4 | 85 | struct inode *tdp; /* returned by iget */ |
9e7c949b | 86 | off_t enduseful; /* pointer past last used dir slot */ |
7604aef4 KM |
87 | int flag; /* LOOKUP, CREATE, RENAME, or DELETE */ |
88 | int lockparent; /* 1 => lockparent flag is set */ | |
89 | int wantparent; /* 1 => wantparent or lockparent flag */ | |
90 | int error; | |
91 | ||
a48aecd3 | 92 | ndp->ni_dvp = vdp; |
7604aef4 | 93 | ndp->ni_vp = NULL; |
a48aecd3 | 94 | dp = VTOI(vdp); |
6bd0bb92 | 95 | fs = dp->i_fs; |
7604aef4 KM |
96 | lockparent = ndp->ni_nameiop & LOCKPARENT; |
97 | flag = ndp->ni_nameiop & OPFLAG; | |
98 | wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT); | |
6bd0bb92 | 99 | |
a0aead5a | 100 | /* |
6bd0bb92 | 101 | * Check accessiblity of directory. |
a0aead5a | 102 | */ |
7604aef4 KM |
103 | if ((dp->i_mode&IFMT) != IFDIR) |
104 | return (ENOTDIR); | |
a48aecd3 | 105 | if (error = ufs_access(vdp, VEXEC, ndp->ni_cred)) |
7604aef4 | 106 | return (error); |
e47da406 | 107 | |
f93197fc KM |
108 | /* |
109 | * We now have a segment name to search for, and a directory to search. | |
110 | * | |
111 | * Before tediously performing a linear scan of the directory, | |
112 | * check the name cache to see if the directory/name pair | |
7604aef4 | 113 | * we are looking for is known already. |
f93197fc | 114 | */ |
2bd0d28d KM |
115 | if (error = cache_lookup(ndp)) { |
116 | int vpid; /* capability number of vnode */ | |
117 | ||
118 | if (error == ENOENT) | |
119 | return (error); | |
a48aecd3 | 120 | if (vdp == ndp->ni_rdir && ndp->ni_isdotdot) |
27c192da | 121 | panic("ufs_lookup: .. through root"); |
7604aef4 KM |
122 | /* |
123 | * Get the next vnode in the path. | |
2bd0d28d | 124 | * See comment below starting `Step through' for |
7604aef4 KM |
125 | * an explaination of the locking protocol. |
126 | */ | |
127 | pdp = dp; | |
0be364c5 | 128 | dp = VTOI(ndp->ni_vp); |
27c192da | 129 | vdp = ndp->ni_vp; |
2bd0d28d | 130 | vpid = vdp->v_id; |
7604aef4 | 131 | if (pdp == dp) { |
8fe1c702 | 132 | VREF(vdp); |
27c192da | 133 | error = 0; |
7604aef4 KM |
134 | } else if (ndp->ni_isdotdot) { |
135 | IUNLOCK(pdp); | |
27c192da | 136 | error = vget(vdp); |
f93197fc | 137 | } else { |
27c192da | 138 | error = vget(vdp); |
7604aef4 | 139 | IUNLOCK(pdp); |
f93197fc | 140 | } |
2bd0d28d KM |
141 | /* |
142 | * Check that the capability number did not change | |
143 | * while we were waiting for the lock. | |
144 | */ | |
27c192da KM |
145 | if (!error) { |
146 | if (vpid == vdp->v_id) | |
147 | return (0); | |
148 | else | |
149 | iput(dp); | |
150 | } | |
2bd0d28d KM |
151 | ILOCK(pdp); |
152 | dp = pdp; | |
a48aecd3 | 153 | vdp = ITOV(dp); |
2bd0d28d | 154 | ndp->ni_vp = NULL; |
f93197fc KM |
155 | } |
156 | ||
6459ebe0 | 157 | /* |
6bd0bb92 BJ |
158 | * Suppress search for slots unless creating |
159 | * file and at end of pathname, in which case | |
160 | * we watch for a place to put the new file in | |
161 | * case it doesn't already exist. | |
6459ebe0 | 162 | */ |
6bd0bb92 | 163 | slotstatus = FOUND; |
7604aef4 | 164 | if ((flag == CREATE || flag == RENAME) && *ndp->ni_next == 0) { |
6bd0bb92 BJ |
165 | slotstatus = NONE; |
166 | slotfreespace = 0; | |
d870be74 | 167 | slotneeded = DIRSIZ(&ndp->ni_dent); |
6bd0bb92 | 168 | } |
7604aef4 | 169 | |
84ef30ec | 170 | /* |
7604aef4 KM |
171 | * If there is cached information on a previous search of |
172 | * this directory, pick up where we last left off. | |
f93197fc | 173 | * We cache only lookups as these are the most common |
84ef30ec KM |
174 | * and have the greatest payoff. Caching CREATE has little |
175 | * benefit as it usually must search the entire directory | |
176 | * to determine that the entry does not exist. Caching the | |
7604aef4 KM |
177 | * location of the last DELETE or RENAME has not reduced |
178 | * profiling time and hence has been removed in the interest | |
179 | * of simplicity. | |
84ef30ec | 180 | */ |
7604aef4 | 181 | if (flag != LOOKUP || dp->i_diroff == 0 || dp->i_diroff > dp->i_size) { |
d870be74 | 182 | ndp->ni_offset = 0; |
84ef30ec KM |
183 | numdirpasses = 1; |
184 | } else { | |
7604aef4 | 185 | ndp->ni_offset = dp->i_diroff; |
d870be74 | 186 | entryoffsetinblock = blkoff(fs, ndp->ni_offset); |
84ef30ec | 187 | if (entryoffsetinblock != 0) { |
7604aef4 KM |
188 | error = blkatoff(dp, ndp->ni_offset, (char **)0, &bp); |
189 | if (error) | |
190 | return (error); | |
84ef30ec KM |
191 | } |
192 | numdirpasses = 2; | |
f93197fc | 193 | nchstats.ncs_2passes++; |
84ef30ec KM |
194 | } |
195 | endsearch = roundup(dp->i_size, DIRBLKSIZ); | |
9e7c949b | 196 | enduseful = 0; |
6bd0bb92 | 197 | |
84ef30ec | 198 | searchloop: |
d870be74 | 199 | while (ndp->ni_offset < endsearch) { |
f5039631 BJ |
200 | /* |
201 | * If offset is on a block boundary, | |
202 | * read the next directory block. | |
203 | * Release previous if it exists. | |
204 | */ | |
d870be74 | 205 | if (blkoff(fs, ndp->ni_offset) == 0) { |
f5039631 BJ |
206 | if (bp != NULL) |
207 | brelse(bp); | |
7604aef4 KM |
208 | error = blkatoff(dp, ndp->ni_offset, (char **)0, &bp); |
209 | if (error) | |
210 | return (error); | |
6bd0bb92 | 211 | entryoffsetinblock = 0; |
6459ebe0 KM |
212 | } |
213 | /* | |
6bd0bb92 | 214 | * If still looking for a slot, and at a DIRBLKSIZE |
d82d5deb | 215 | * boundary, have to start looking for free space again. |
6459ebe0 | 216 | */ |
6bd0bb92 | 217 | if (slotstatus == NONE && |
7604aef4 | 218 | (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) { |
6bd0bb92 BJ |
219 | slotoffset = -1; |
220 | slotfreespace = 0; | |
221 | } | |
6bd0bb92 | 222 | /* |
d82d5deb KM |
223 | * Get pointer to next entry. |
224 | * Full validation checks are slow, so we only check | |
225 | * enough to insure forward progress through the | |
226 | * directory. Complete checks can be run by patching | |
227 | * "dirchk" to be true. | |
6bd0bb92 BJ |
228 | */ |
229 | ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock); | |
a5e62f37 | 230 | if (ep->d_reclen == 0 || |
d82d5deb | 231 | dirchk && dirbadentry(ep, entryoffsetinblock)) { |
7604aef4 KM |
232 | int i; |
233 | ||
d870be74 | 234 | dirbad(dp, ndp->ni_offset, "mangled entry"); |
d82d5deb | 235 | i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)); |
d870be74 | 236 | ndp->ni_offset += i; |
6bd0bb92 | 237 | entryoffsetinblock += i; |
6459ebe0 KM |
238 | continue; |
239 | } | |
6bd0bb92 | 240 | |
6459ebe0 | 241 | /* |
6bd0bb92 | 242 | * If an appropriate sized slot has not yet been found, |
6459ebe0 KM |
243 | * check to see if one is available. Also accumulate space |
244 | * in the current block so that we can determine if | |
245 | * compaction is viable. | |
246 | */ | |
6bd0bb92 BJ |
247 | if (slotstatus != FOUND) { |
248 | int size = ep->d_reclen; | |
249 | ||
6459ebe0 KM |
250 | if (ep->d_ino != 0) |
251 | size -= DIRSIZ(ep); | |
252 | if (size > 0) { | |
6bd0bb92 BJ |
253 | if (size >= slotneeded) { |
254 | slotstatus = FOUND; | |
d870be74 | 255 | slotoffset = ndp->ni_offset; |
6bd0bb92 BJ |
256 | slotsize = ep->d_reclen; |
257 | } else if (slotstatus == NONE) { | |
258 | slotfreespace += size; | |
259 | if (slotoffset == -1) | |
d870be74 | 260 | slotoffset = ndp->ni_offset; |
6bd0bb92 BJ |
261 | if (slotfreespace >= slotneeded) { |
262 | slotstatus = COMPACT; | |
d870be74 KM |
263 | slotsize = ndp->ni_offset + |
264 | ep->d_reclen - slotoffset; | |
6bd0bb92 | 265 | } |
6459ebe0 | 266 | } |
f5039631 | 267 | } |
f5039631 | 268 | } |
6bd0bb92 | 269 | |
f5039631 | 270 | /* |
6bd0bb92 | 271 | * Check for a name match. |
f5039631 | 272 | */ |
6bd0bb92 | 273 | if (ep->d_ino) { |
d870be74 | 274 | if (ep->d_namlen == ndp->ni_dent.d_namlen && |
7604aef4 KM |
275 | !bcmp(ndp->ni_ptr, ep->d_name, |
276 | (unsigned)ep->d_namlen)) { | |
277 | /* | |
278 | * Save directory entry's inode number and | |
279 | * reclen in ndp->ni_dent, and release | |
280 | * directory buffer. | |
281 | */ | |
282 | ndp->ni_dent.d_ino = ep->d_ino; | |
283 | ndp->ni_dent.d_reclen = ep->d_reclen; | |
284 | brelse(bp); | |
6bd0bb92 | 285 | goto found; |
7604aef4 | 286 | } |
6bd0bb92 | 287 | } |
d870be74 KM |
288 | prevoff = ndp->ni_offset; |
289 | ndp->ni_offset += ep->d_reclen; | |
6bd0bb92 | 290 | entryoffsetinblock += ep->d_reclen; |
9e7c949b KM |
291 | if (ep->d_ino) |
292 | enduseful = ndp->ni_offset; | |
6bd0bb92 | 293 | } |
f93197fc | 294 | /* notfound: */ |
84ef30ec | 295 | /* |
f93197fc | 296 | * If we started in the middle of the directory and failed |
84ef30ec KM |
297 | * to find our target, we must check the beginning as well. |
298 | */ | |
299 | if (numdirpasses == 2) { | |
300 | numdirpasses--; | |
d870be74 | 301 | ndp->ni_offset = 0; |
7604aef4 | 302 | endsearch = dp->i_diroff; |
84ef30ec KM |
303 | goto searchloop; |
304 | } | |
7604aef4 KM |
305 | if (bp != NULL) |
306 | brelse(bp); | |
6bd0bb92 BJ |
307 | /* |
308 | * If creating, and at end of pathname and current | |
4f083fd7 SL |
309 | * directory has not been removed, then can consider |
310 | * allowing file to be created. | |
6bd0bb92 | 311 | */ |
7604aef4 KM |
312 | if ((flag == CREATE || flag == RENAME) && |
313 | *ndp->ni_next == 0 && dp->i_nlink != 0) { | |
f5039631 | 314 | /* |
6bd0bb92 BJ |
315 | * Access for write is interpreted as allowing |
316 | * creation of files in the directory. | |
f5039631 | 317 | */ |
a48aecd3 | 318 | if (error = ufs_access(vdp, VWRITE, ndp->ni_cred)) |
7604aef4 | 319 | return (error); |
f5039631 | 320 | /* |
6bd0bb92 BJ |
321 | * Return an indication of where the new directory |
322 | * entry should be put. If we didn't find a slot, | |
d870be74 KM |
323 | * then set ndp->ni_count to 0 indicating that the new |
324 | * slot belongs at the end of the directory. If we found | |
325 | * a slot, then the new entry can be put in the range | |
326 | * [ndp->ni_offset .. ndp->ni_offset + ndp->ni_count) | |
f5039631 | 327 | */ |
84ef30ec | 328 | if (slotstatus == NONE) { |
d870be74 KM |
329 | ndp->ni_offset = roundup(dp->i_size, DIRBLKSIZ); |
330 | ndp->ni_count = 0; | |
9e7c949b | 331 | enduseful = ndp->ni_offset; |
84ef30ec | 332 | } else { |
d870be74 KM |
333 | ndp->ni_offset = slotoffset; |
334 | ndp->ni_count = slotsize; | |
9e7c949b KM |
335 | if (enduseful < slotoffset + slotsize) |
336 | enduseful = slotoffset + slotsize; | |
5485e062 | 337 | } |
9e7c949b | 338 | ndp->ni_endoff = roundup(enduseful, DIRBLKSIZ); |
6bd0bb92 | 339 | dp->i_flag |= IUPD|ICHG; |
f5039631 | 340 | /* |
6bd0bb92 BJ |
341 | * We return with the directory locked, so that |
342 | * the parameters we set up above will still be | |
343 | * valid if we actually decide to do a direnter(). | |
7604aef4 KM |
344 | * We return ni_vp == NULL to indicate that the entry |
345 | * does not currently exist; we leave a pointer to | |
346 | * the (locked) directory inode in ndp->ni_dvp. | |
347 | * | |
348 | * NB - if the directory is unlocked, then this | |
349 | * information cannot be used. | |
f5039631 | 350 | */ |
7604aef4 KM |
351 | if (!lockparent) |
352 | IUNLOCK(dp); | |
6bd0bb92 | 353 | } |
2bd0d28d KM |
354 | /* |
355 | * Insert name into cache (as non-existent) if appropriate. | |
356 | */ | |
357 | if (ndp->ni_makeentry) | |
358 | cache_enter(ndp); | |
7604aef4 KM |
359 | return (ENOENT); |
360 | ||
6bd0bb92 | 361 | found: |
f93197fc KM |
362 | if (numdirpasses == 2) |
363 | nchstats.ncs_pass2++; | |
6bd0bb92 BJ |
364 | /* |
365 | * Check that directory length properly reflects presence | |
366 | * of this entry. | |
367 | */ | |
4a0415d6 | 368 | if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) { |
d870be74 | 369 | dirbad(dp, ndp->ni_offset, "i_size too small"); |
4a0415d6 | 370 | dp->i_size = entryoffsetinblock + DIRSIZ(ep); |
6bd0bb92 BJ |
371 | dp->i_flag |= IUPD|ICHG; |
372 | } | |
373 | ||
374 | /* | |
84ef30ec | 375 | * Found component in pathname. |
f93197fc | 376 | * If the final component of path name, save information |
84ef30ec KM |
377 | * in the cache as to where the entry was found. |
378 | */ | |
7604aef4 KM |
379 | if (*ndp->ni_next == '\0' && flag == LOOKUP) |
380 | dp->i_diroff = ndp->ni_offset &~ (DIRBLKSIZ - 1); | |
6bd0bb92 BJ |
381 | |
382 | /* | |
383 | * If deleting, and at end of pathname, return | |
384 | * parameters which can be used to remove file. | |
7604aef4 KM |
385 | * If the wantparent flag isn't set, we return only |
386 | * the directory (in ndp->ni_dvp), otherwise we go | |
4f083fd7 | 387 | * on and lock the inode, being careful with ".". |
6bd0bb92 | 388 | */ |
7604aef4 | 389 | if (flag == DELETE && *ndp->ni_next == 0) { |
6bd0bb92 BJ |
390 | /* |
391 | * Write access to directory required to delete files. | |
392 | */ | |
a48aecd3 | 393 | if (error = ufs_access(vdp, VWRITE, ndp->ni_cred)) |
7604aef4 | 394 | return (error); |
6bd0bb92 | 395 | /* |
d870be74 | 396 | * Return pointer to current entry in ndp->ni_offset, |
6bd0bb92 | 397 | * and distance past previous entry (if there |
d870be74 | 398 | * is a previous entry in this block) in ndp->ni_count. |
2bd0d28d | 399 | * Save directory inode pointer in ndp->ni_dvp for dirremove(). |
6bd0bb92 | 400 | */ |
d870be74 KM |
401 | if ((ndp->ni_offset&(DIRBLKSIZ-1)) == 0) |
402 | ndp->ni_count = 0; | |
6bd0bb92 | 403 | else |
d870be74 | 404 | ndp->ni_count = ndp->ni_offset - prevoff; |
7604aef4 | 405 | if (dp->i_number == ndp->ni_dent.d_ino) { |
8fe1c702 | 406 | VREF(vdp); |
a48aecd3 KM |
407 | ndp->ni_vp = vdp; |
408 | return (0); | |
4f083fd7 | 409 | } |
a48aecd3 KM |
410 | if (error = iget(dp, ndp->ni_dent.d_ino, &tdp)) |
411 | return (error); | |
412 | /* | |
413 | * If directory is "sticky", then user must own | |
414 | * the directory, or the file in it, else she | |
415 | * may not delete it (unless she's root). This | |
416 | * implements append-only directories. | |
417 | */ | |
418 | if ((dp->i_mode & ISVTX) && | |
419 | ndp->ni_cred->cr_uid != 0 && | |
420 | ndp->ni_cred->cr_uid != dp->i_uid && | |
421 | tdp->i_uid != ndp->ni_cred->cr_uid) { | |
422 | iput(tdp); | |
423 | return (EPERM); | |
424 | } | |
425 | ndp->ni_vp = ITOV(tdp); | |
426 | if (!lockparent) | |
427 | IUNLOCK(dp); | |
7604aef4 | 428 | return (0); |
6bd0bb92 BJ |
429 | } |
430 | ||
4f083fd7 | 431 | /* |
7604aef4 | 432 | * If rewriting (RENAME), return the inode and the |
4f083fd7 SL |
433 | * information required to rewrite the present directory |
434 | * Must get inode of directory entry to verify it's a | |
7604aef4 | 435 | * regular file, or empty directory. |
4f083fd7 | 436 | */ |
7604aef4 | 437 | if (flag == RENAME && wantparent && *ndp->ni_next == 0) { |
a48aecd3 | 438 | if (error = ufs_access(vdp, VWRITE, ndp->ni_cred)) |
7604aef4 | 439 | return (error); |
4f083fd7 | 440 | /* |
7604aef4 KM |
441 | * Careful about locking second inode. |
442 | * This can only occur if the target is ".". | |
4f083fd7 | 443 | */ |
7604aef4 KM |
444 | if (dp->i_number == ndp->ni_dent.d_ino) |
445 | return (EISDIR); | |
446 | if (error = iget(dp, ndp->ni_dent.d_ino, &tdp)) | |
447 | return (error); | |
448 | ndp->ni_vp = ITOV(tdp); | |
449 | if (!lockparent) | |
450 | IUNLOCK(dp); | |
451 | return (0); | |
4f083fd7 SL |
452 | } |
453 | ||
6bd0bb92 | 454 | /* |
7604aef4 KM |
455 | * Step through the translation in the name. We do not `iput' the |
456 | * directory because we may need it again if a symbolic link | |
bde63aa5 KM |
457 | * is relative to the current directory. Instead we save it |
458 | * unlocked as "pdp". We must get the target inode before unlocking | |
459 | * the directory to insure that the inode will not be removed | |
460 | * before we get it. We prevent deadlock by always fetching | |
461 | * inodes from the root, moving down the directory tree. Thus | |
462 | * when following backward pointers ".." we must unlock the | |
463 | * parent directory before getting the requested directory. | |
464 | * There is a potential race condition here if both the current | |
465 | * and parent directories are removed before the `iget' for the | |
466 | * inode associated with ".." returns. We hope that this occurs | |
467 | * infrequently since we cannot avoid this race condition without | |
a4358a25 | 468 | * implementing a sophisticated deadlock detection algorithm. |
bde63aa5 KM |
469 | * Note also that this simple deadlock detection scheme will not |
470 | * work if the file system has any hard links other than ".." | |
471 | * that point backwards in the directory structure. | |
6bd0bb92 BJ |
472 | */ |
473 | pdp = dp; | |
7604aef4 | 474 | if (ndp->ni_isdotdot) { |
56700195 | 475 | IUNLOCK(pdp); /* race to get the inode */ |
7604aef4 KM |
476 | if (error = iget(dp, ndp->ni_dent.d_ino, &tdp)) { |
477 | ILOCK(pdp); | |
478 | return (error); | |
479 | } | |
a6ad75d0 KM |
480 | if (lockparent && *ndp->ni_next == '\0') |
481 | ILOCK(pdp); | |
7604aef4 | 482 | ndp->ni_vp = ITOV(tdp); |
d870be74 | 483 | } else if (dp->i_number == ndp->ni_dent.d_ino) { |
8fe1c702 | 484 | VREF(vdp); /* we want ourself, ie "." */ |
7604aef4 | 485 | ndp->ni_vp = vdp; |
bde63aa5 | 486 | } else { |
7604aef4 KM |
487 | if (error = iget(dp, ndp->ni_dent.d_ino, &tdp)) |
488 | return (error); | |
a6ad75d0 KM |
489 | if (!lockparent || *ndp->ni_next != '\0') |
490 | IUNLOCK(pdp); | |
7604aef4 | 491 | ndp->ni_vp = ITOV(tdp); |
bde63aa5 | 492 | } |
f93197fc KM |
493 | |
494 | /* | |
a5e62f37 | 495 | * Insert name into cache if appropriate. |
f93197fc | 496 | */ |
7604aef4 KM |
497 | if (ndp->ni_makeentry) |
498 | cache_enter(ndp); | |
499 | return (0); | |
10873320 BJ |
500 | } |
501 | ||
f93197fc | 502 | |
d870be74 | 503 | dirbad(ip, offset, how) |
6bd0bb92 | 504 | struct inode *ip; |
d870be74 | 505 | off_t offset; |
6bd0bb92 BJ |
506 | char *how; |
507 | { | |
508 | ||
509 | printf("%s: bad dir ino %d at offset %d: %s\n", | |
d870be74 | 510 | ip->i_fs->fs_fsmnt, ip->i_number, offset, how); |
da089146 | 511 | panic("bad dir"); |
6bd0bb92 BJ |
512 | } |
513 | ||
d82d5deb KM |
514 | /* |
515 | * Do consistency checking on a directory entry: | |
516 | * record length must be multiple of 4 | |
d82d5deb KM |
517 | * entry must fit in rest of its DIRBLKSIZ block |
518 | * record must be large enough to contain entry | |
519 | * name is not longer than MAXNAMLEN | |
520 | * name must be as long as advertised, and null terminated | |
521 | */ | |
522 | dirbadentry(ep, entryoffsetinblock) | |
6bd0bb92 | 523 | register struct direct *ep; |
d82d5deb | 524 | int entryoffsetinblock; |
6bd0bb92 | 525 | { |
6bd0bb92 BJ |
526 | register int i; |
527 | ||
a5e62f37 | 528 | if ((ep->d_reclen & 0x3) != 0 || |
d82d5deb KM |
529 | ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) || |
530 | ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN) | |
531 | return (1); | |
6bd0bb92 | 532 | for (i = 0; i < ep->d_namlen; i++) |
d870be74 | 533 | if (ep->d_name[i] == '\0') |
6bd0bb92 BJ |
534 | return (1); |
535 | return (ep->d_name[i]); | |
536 | } | |
537 | ||
6bd0bb92 BJ |
538 | /* |
539 | * Write a directory entry after a call to namei, using the parameters | |
7604aef4 KM |
540 | * which it left in nameidata. The argument ip is the inode which the |
541 | * new directory entry will refer to. The nameidata field ndp->ni_dvp | |
542 | * is a pointer to the directory to be written, which was left locked by | |
d870be74 | 543 | * namei. Remaining parameters (ndp->ni_offset, ndp->ni_count) indicate |
6bd0bb92 BJ |
544 | * how the space for the new entry is to be gotten. |
545 | */ | |
d870be74 | 546 | direnter(ip, ndp) |
6bd0bb92 | 547 | struct inode *ip; |
d870be74 | 548 | register struct nameidata *ndp; |
f5039631 | 549 | { |
6bd0bb92 | 550 | register struct direct *ep, *nep; |
7604aef4 | 551 | register struct inode *dp = VTOI(ndp->ni_dvp); |
6bd0bb92 | 552 | struct buf *bp; |
efa3a91c | 553 | int loc, spacefree, error = 0; |
b32450f4 BJ |
554 | u_int dsize; |
555 | int newentrysize; | |
6bd0bb92 | 556 | char *dirbuf; |
f5039631 | 557 | |
d870be74 KM |
558 | ndp->ni_dent.d_ino = ip->i_number; |
559 | newentrysize = DIRSIZ(&ndp->ni_dent); | |
560 | if (ndp->ni_count == 0) { | |
6bd0bb92 | 561 | /* |
d870be74 KM |
562 | * If ndp->ni_count is 0, then namei could find no space in the |
563 | * directory. In this case ndp->ni_offset will be on a directory | |
6bd0bb92 BJ |
564 | * block boundary and we will write the new entry into a fresh |
565 | * block. | |
566 | */ | |
d870be74 | 567 | if (ndp->ni_offset&(DIRBLKSIZ-1)) |
6bd0bb92 | 568 | panic("wdir: newblk"); |
d870be74 | 569 | ndp->ni_dent.d_reclen = DIRBLKSIZ; |
5c299d0e KM |
570 | ndp->ni_count = newentrysize; |
571 | ndp->ni_resid = newentrysize; | |
5f9e6a68 | 572 | ndp->ni_base = (caddr_t)&ndp->ni_dent; |
7dda6faa | 573 | ndp->ni_uioseg = UIO_SYSSPACE; |
6c41489b KM |
574 | error = |
575 | ufs_write(ndp->ni_dvp, &ndp->ni_uio, IO_SYNC, ndp->ni_cred); | |
23de9f20 | 576 | if (DIRBLKSIZ > dp->i_fs->fs_fsize) |
7604aef4 | 577 | panic("wdir: blksize"); /* XXX - should grow w/balloc */ |
23de9f20 KM |
578 | else |
579 | dp->i_size = roundup(dp->i_size, DIRBLKSIZ); | |
9e7c949b | 580 | iput(dp); |
f2a5ad78 | 581 | return (error); |
6bd0bb92 BJ |
582 | } |
583 | ||
584 | /* | |
d870be74 KM |
585 | * If ndp->ni_count is non-zero, then namei found space for the new |
586 | * entry in the range ndp->ni_offset to ndp->ni_offset + ndp->ni_count. | |
6bd0bb92 BJ |
587 | * in the directory. To use this space, we may have to compact |
588 | * the entries located there, by copying them together towards | |
589 | * the beginning of the block, leaving the free space in | |
590 | * one usable chunk at the end. | |
591 | */ | |
592 | ||
593 | /* | |
594 | * Increase size of directory if entry eats into new space. | |
595 | * This should never push the size past a new multiple of | |
596 | * DIRBLKSIZE. | |
23de9f20 KM |
597 | * |
598 | * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN. | |
6bd0bb92 | 599 | */ |
9e7c949b KM |
600 | if (ndp->ni_offset + ndp->ni_count > dp->i_size) |
601 | dp->i_size = ndp->ni_offset + ndp->ni_count; | |
6bd0bb92 | 602 | /* |
7604aef4 | 603 | * Get the block containing the space for the new directory entry. |
6bd0bb92 | 604 | */ |
7604aef4 | 605 | if (error = blkatoff(dp, ndp->ni_offset, (char **)&dirbuf, &bp)) { |
9e7c949b | 606 | iput(dp); |
7604aef4 | 607 | return (error); |
4f083fd7 | 608 | } |
6bd0bb92 BJ |
609 | /* |
610 | * Find space for the new entry. In the simple case, the | |
611 | * entry at offset base will have the space. If it does | |
612 | * not, then namei arranged that compacting the region | |
d870be74 | 613 | * ndp->ni_offset to ndp->ni_offset+ndp->ni_count would yield the space. |
6bd0bb92 BJ |
614 | */ |
615 | ep = (struct direct *)dirbuf; | |
616 | dsize = DIRSIZ(ep); | |
efa3a91c | 617 | spacefree = ep->d_reclen - dsize; |
d870be74 | 618 | for (loc = ep->d_reclen; loc < ndp->ni_count; ) { |
6bd0bb92 BJ |
619 | nep = (struct direct *)(dirbuf + loc); |
620 | if (ep->d_ino) { | |
621 | /* trim the existing slot */ | |
622 | ep->d_reclen = dsize; | |
623 | ep = (struct direct *)((char *)ep + dsize); | |
624 | } else { | |
625 | /* overwrite; nothing there; header is ours */ | |
7604aef4 | 626 | spacefree += dsize; |
6bd0bb92 BJ |
627 | } |
628 | dsize = DIRSIZ(nep); | |
efa3a91c | 629 | spacefree += nep->d_reclen - dsize; |
6bd0bb92 | 630 | loc += nep->d_reclen; |
9f024ffc | 631 | bcopy((caddr_t)nep, (caddr_t)ep, dsize); |
6bd0bb92 BJ |
632 | } |
633 | /* | |
634 | * Update the pointer fields in the previous entry (if any), | |
635 | * copy in the new entry, and write out the block. | |
636 | */ | |
637 | if (ep->d_ino == 0) { | |
efa3a91c | 638 | if (spacefree + dsize < newentrysize) |
6bd0bb92 | 639 | panic("wdir: compact1"); |
d870be74 | 640 | ndp->ni_dent.d_reclen = spacefree + dsize; |
6bd0bb92 | 641 | } else { |
efa3a91c | 642 | if (spacefree < newentrysize) |
6bd0bb92 | 643 | panic("wdir: compact2"); |
d870be74 | 644 | ndp->ni_dent.d_reclen = spacefree; |
6bd0bb92 BJ |
645 | ep->d_reclen = dsize; |
646 | ep = (struct direct *)((char *)ep + dsize); | |
647 | } | |
d870be74 | 648 | bcopy((caddr_t)&ndp->ni_dent, (caddr_t)ep, (u_int)newentrysize); |
7604aef4 | 649 | error = bwrite(bp); |
9e7c949b | 650 | dp->i_flag |= IUPD|ICHG; |
a48aecd3 | 651 | if (!error && ndp->ni_endoff && ndp->ni_endoff < dp->i_size) |
bcf1400c | 652 | error = itrunc(dp, (u_long)ndp->ni_endoff, IO_SYNC); |
9e7c949b | 653 | iput(dp); |
f2a5ad78 | 654 | return (error); |
6bd0bb92 BJ |
655 | } |
656 | ||
4f083fd7 | 657 | /* |
5c299d0e KM |
658 | * Remove a directory entry after a call to namei, using |
659 | * the parameters which it left in nameidata. The entry | |
d870be74 KM |
660 | * ni_offset contains the offset into the directory of the |
661 | * entry to be eliminated. The ni_count field contains the | |
4f083fd7 SL |
662 | * size of the previous record in the directory. If this |
663 | * is 0, the first entry is being deleted, so we need only | |
664 | * zero the inode number to mark the entry as free. If the | |
665 | * entry isn't the first in the directory, we must reclaim | |
666 | * the space of the now empty record by adding the record size | |
667 | * to the size of the previous entry. | |
668 | */ | |
d870be74 KM |
669 | dirremove(ndp) |
670 | register struct nameidata *ndp; | |
6bd0bb92 | 671 | { |
7604aef4 | 672 | register struct inode *dp = VTOI(ndp->ni_dvp); |
6bd0bb92 | 673 | struct direct *ep; |
7604aef4 KM |
674 | struct buf *bp; |
675 | int error; | |
6bd0bb92 | 676 | |
d870be74 | 677 | if (ndp->ni_count == 0) { |
6bd0bb92 BJ |
678 | /* |
679 | * First entry in block: set d_ino to zero. | |
680 | */ | |
d870be74 | 681 | ndp->ni_dent.d_ino = 0; |
5c299d0e | 682 | ndp->ni_count = ndp->ni_resid = DIRSIZ(&ndp->ni_dent); |
5f9e6a68 | 683 | ndp->ni_base = (caddr_t)&ndp->ni_dent; |
7dda6faa | 684 | ndp->ni_uioseg = UIO_SYSSPACE; |
6c41489b KM |
685 | error = |
686 | ufs_write(ndp->ni_dvp, &ndp->ni_uio, IO_SYNC, ndp->ni_cred); | |
36d4ed87 | 687 | } else { |
6bd0bb92 BJ |
688 | /* |
689 | * Collapse new free space into previous entry. | |
690 | */ | |
7604aef4 KM |
691 | if (error = blkatoff(dp, ndp->ni_offset - ndp->ni_count, |
692 | (char **)&ep, &bp)) { | |
693 | return (error); | |
694 | } | |
d870be74 | 695 | ep->d_reclen += ndp->ni_dent.d_reclen; |
7604aef4 | 696 | error = bwrite(bp); |
6bd0bb92 BJ |
697 | dp->i_flag |= IUPD|ICHG; |
698 | } | |
7604aef4 | 699 | return (error); |
10873320 | 700 | } |
6459ebe0 | 701 | |
4f083fd7 SL |
702 | /* |
703 | * Rewrite an existing directory entry to point at the inode | |
704 | * supplied. The parameters describing the directory entry are | |
705 | * set up by a call to namei. | |
706 | */ | |
d870be74 | 707 | dirrewrite(dp, ip, ndp) |
4f083fd7 | 708 | struct inode *dp, *ip; |
d870be74 | 709 | struct nameidata *ndp; |
4f083fd7 SL |
710 | { |
711 | ||
d870be74 | 712 | ndp->ni_dent.d_ino = ip->i_number; |
5c299d0e | 713 | ndp->ni_count = ndp->ni_resid = DIRSIZ(&ndp->ni_dent); |
5f9e6a68 | 714 | ndp->ni_base = (caddr_t)&ndp->ni_dent; |
7dda6faa | 715 | ndp->ni_uioseg = UIO_SYSSPACE; |
6c41489b | 716 | return (ufs_write(ITOV(dp), &ndp->ni_uio, IO_SYNC, ndp->ni_cred)); |
4f083fd7 SL |
717 | } |
718 | ||
4a0415d6 SL |
719 | /* |
720 | * Return buffer with contents of block "offset" | |
721 | * from the beginning of directory "ip". If "res" | |
722 | * is non-zero, fill it in with a pointer to the | |
723 | * remaining space in the directory. | |
724 | */ | |
7604aef4 | 725 | blkatoff(ip, offset, res, bpp) |
6bd0bb92 BJ |
726 | struct inode *ip; |
727 | off_t offset; | |
728 | char **res; | |
7604aef4 | 729 | struct buf **bpp; |
6459ebe0 | 730 | { |
6bd0bb92 | 731 | register struct fs *fs = ip->i_fs; |
3fd23f5c | 732 | daddr_t lbn = lblkno(fs, offset); |
6bd0bb92 | 733 | int bsize = blksize(fs, ip, lbn); |
7604aef4 | 734 | struct buf *bp; |
7e4f159c | 735 | daddr_t bn; |
7604aef4 | 736 | int error; |
6459ebe0 | 737 | |
7604aef4 | 738 | *bpp = 0; |
bcf1400c | 739 | if (error = bread(ITOV(ip), lbn, bsize, NOCRED, &bp)) { |
6bd0bb92 | 740 | brelse(bp); |
7604aef4 | 741 | return (error); |
6bd0bb92 BJ |
742 | } |
743 | if (res) | |
7e4f159c | 744 | *res = bp->b_un.b_addr + blkoff(fs, offset); |
7604aef4 KM |
745 | *bpp = bp; |
746 | return (0); | |
6459ebe0 | 747 | } |
4f083fd7 SL |
748 | |
749 | /* | |
750 | * Check if a directory is empty or not. | |
751 | * Inode supplied must be locked. | |
17d11193 SL |
752 | * |
753 | * Using a struct dirtemplate here is not precisely | |
754 | * what we want, but better than using a struct direct. | |
755 | * | |
756 | * NB: does not handle corrupted directories. | |
4f083fd7 | 757 | */ |
7604aef4 | 758 | dirempty(ip, parentino, cred) |
05d2bbce | 759 | register struct inode *ip; |
68f21562 | 760 | ino_t parentino; |
7604aef4 | 761 | struct ucred *cred; |
4f083fd7 SL |
762 | { |
763 | register off_t off; | |
17d11193 SL |
764 | struct dirtemplate dbuf; |
765 | register struct direct *dp = (struct direct *)&dbuf; | |
05d2bbce | 766 | int error, count; |
17d11193 | 767 | #define MINDIRSIZ (sizeof (struct dirtemplate) / 2) |
4f083fd7 SL |
768 | |
769 | for (off = 0; off < ip->i_size; off += dp->d_reclen) { | |
6c41489b KM |
770 | error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, |
771 | off, UIO_SYSSPACE, IO_NODELOCKED, cred, &count); | |
17d11193 SL |
772 | /* |
773 | * Since we read MINDIRSIZ, residual must | |
774 | * be 0 unless we're at end of file. | |
775 | */ | |
776 | if (error || count != 0) | |
4f083fd7 | 777 | return (0); |
d14b14c3 | 778 | /* avoid infinite loops */ |
a5e62f37 | 779 | if (dp->d_reclen == 0) |
d14b14c3 | 780 | return (0); |
17d11193 | 781 | /* skip empty entries */ |
4f083fd7 SL |
782 | if (dp->d_ino == 0) |
783 | continue; | |
17d11193 SL |
784 | /* accept only "." and ".." */ |
785 | if (dp->d_namlen > 2) | |
786 | return (0); | |
4f083fd7 SL |
787 | if (dp->d_name[0] != '.') |
788 | return (0); | |
17d11193 SL |
789 | /* |
790 | * At this point d_namlen must be 1 or 2. | |
791 | * 1 implies ".", 2 implies ".." if second | |
792 | * char is also "." | |
793 | */ | |
68f21562 KM |
794 | if (dp->d_namlen == 1) |
795 | continue; | |
796 | if (dp->d_name[1] == '.' && dp->d_ino == parentino) | |
4f083fd7 SL |
797 | continue; |
798 | return (0); | |
799 | } | |
800 | return (1); | |
801 | } | |
b1aa93b9 KM |
802 | |
803 | /* | |
804 | * Check if source directory is in the path of the target directory. | |
805 | * Target is supplied locked, source is unlocked. | |
806 | * The target is always iput() before returning. | |
807 | */ | |
7604aef4 | 808 | checkpath(source, target, cred) |
b1aa93b9 | 809 | struct inode *source, *target; |
7604aef4 | 810 | struct ucred *cred; |
b1aa93b9 KM |
811 | { |
812 | struct dirtemplate dirbuf; | |
7604aef4 | 813 | struct inode *ip; |
b1aa93b9 KM |
814 | int error = 0; |
815 | ||
816 | ip = target; | |
817 | if (ip->i_number == source->i_number) { | |
818 | error = EEXIST; | |
819 | goto out; | |
820 | } | |
821 | if (ip->i_number == ROOTINO) | |
822 | goto out; | |
823 | ||
824 | for (;;) { | |
825 | if ((ip->i_mode&IFMT) != IFDIR) { | |
826 | error = ENOTDIR; | |
827 | break; | |
828 | } | |
6c41489b | 829 | error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)&dirbuf, |
7604aef4 | 830 | sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE, |
6c41489b | 831 | IO_NODELOCKED, cred, (int *)0); |
b1aa93b9 KM |
832 | if (error != 0) |
833 | break; | |
834 | if (dirbuf.dotdot_namlen != 2 || | |
4a287f19 KM |
835 | dirbuf.dotdot_name[0] != '.' || |
836 | dirbuf.dotdot_name[1] != '.') { | |
b1aa93b9 KM |
837 | error = ENOTDIR; |
838 | break; | |
839 | } | |
840 | if (dirbuf.dotdot_ino == source->i_number) { | |
841 | error = EINVAL; | |
842 | break; | |
843 | } | |
844 | if (dirbuf.dotdot_ino == ROOTINO) | |
845 | break; | |
846 | iput(ip); | |
7604aef4 | 847 | if (error = iget(ip, dirbuf.dotdot_ino, &ip)) |
b1aa93b9 | 848 | break; |
b1aa93b9 KM |
849 | } |
850 | ||
851 | out: | |
852 | if (error == ENOTDIR) | |
853 | printf("checkpath: .. not a directory\n"); | |
854 | if (ip != NULL) | |
855 | iput(ip); | |
856 | return (error); | |
857 | } |