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