Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
7188ac27 KM |
2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. |
3 | * All rights reserved. | |
da7c5cc6 | 4 | * |
7188ac27 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 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
16 | * | |
afd7e202 | 17 | * @(#)ffs_inode.c 7.8 (Berkeley) %G% |
da7c5cc6 | 18 | */ |
5d5124a1 | 19 | |
94368568 JB |
20 | #include "param.h" |
21 | #include "systm.h" | |
22 | #include "mount.h" | |
94368568 | 23 | #include "user.h" |
7188ac27 | 24 | #include "file.h" |
94368568 | 25 | #include "buf.h" |
0b355a6e | 26 | #include "cmap.h" |
7188ac27 KM |
27 | #include "vnode.h" |
28 | #include "../ufs/inode.h" | |
29 | #include "../ufs/fs.h" | |
30 | #include "../ufs/ufsmount.h" | |
b4567e9c | 31 | #ifdef QUOTA |
7188ac27 | 32 | #include "../ufs/quota.h" |
4147b3f6 | 33 | #endif |
94368568 | 34 | #include "kernel.h" |
b30358ab | 35 | #include "malloc.h" |
5d5124a1 | 36 | |
c22c66ff | 37 | #define INOHSZ 512 |
3ebac878 RE |
38 | #if ((INOHSZ&(INOHSZ-1)) == 0) |
39 | #define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1)) | |
40 | #else | |
a3a9487d | 41 | #define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ) |
3ebac878 RE |
42 | #endif |
43 | ||
7188ac27 KM |
44 | #define INSFREE(ip) {\ |
45 | if (ifreeh) { \ | |
46 | *ifreet = (ip); \ | |
47 | (ip)->i_freeb = ifreet; \ | |
48 | } else { \ | |
49 | ifreeh = (ip); \ | |
50 | (ip)->i_freeb = &ifreeh; \ | |
51 | } \ | |
52 | (ip)->i_freef = NULL; \ | |
53 | ifreet = &(ip)->i_freef; \ | |
54 | } | |
55 | ||
3ebac878 RE |
56 | union ihead { /* inode LRU cache, Chris Maltby */ |
57 | union ihead *ih_head[2]; | |
58 | struct inode *ih_chain[2]; | |
59 | } ihead[INOHSZ]; | |
60 | ||
7188ac27 | 61 | struct inode *ifreeh, **ifreet, *bdevlisth; |
5d5124a1 BJ |
62 | |
63 | /* | |
64 | * Initialize hash links for inodes | |
65 | * and build inode free list. | |
66 | */ | |
67 | ihinit() | |
68 | { | |
69 | register int i; | |
75105cf0 | 70 | register struct inode *ip = inode; |
3ebac878 | 71 | register union ihead *ih = ihead; |
5d5124a1 | 72 | |
3ebac878 RE |
73 | for (i = INOHSZ; --i >= 0; ih++) { |
74 | ih->ih_head[0] = ih; | |
75 | ih->ih_head[1] = ih; | |
76 | } | |
77 | ifreeh = ip; | |
78 | ifreet = &ip->i_freef; | |
79 | ip->i_freeb = &ifreeh; | |
80 | ip->i_forw = ip; | |
81 | ip->i_back = ip; | |
7188ac27 | 82 | ITOV(ip)->v_data = (qaddr_t)ip; |
3ebac878 RE |
83 | for (i = ninode; --i > 0; ) { |
84 | ++ip; | |
85 | ip->i_forw = ip; | |
86 | ip->i_back = ip; | |
7188ac27 | 87 | ITOV(ip)->v_data = (qaddr_t)ip; |
3ebac878 RE |
88 | *ifreet = ip; |
89 | ip->i_freeb = ifreet; | |
90 | ifreet = &ip->i_freef; | |
91 | } | |
92 | ip->i_freef = NULL; | |
5d5124a1 BJ |
93 | } |
94 | ||
3ebac878 | 95 | /* |
7188ac27 | 96 | * Look up an vnode/inode by device,inumber. |
5d5124a1 BJ |
97 | * If it is in core (in the inode structure), |
98 | * honor the locking protocol. | |
99 | * If it is not in core, read it in from the | |
100 | * specified device. | |
7188ac27 | 101 | * Callers must check for mount points!! |
5d5124a1 BJ |
102 | * In all cases, a pointer to a locked |
103 | * inode structure is returned. | |
5d5124a1 | 104 | */ |
7188ac27 KM |
105 | iget(xp, ino, ipp) |
106 | struct inode *xp; | |
7494ef16 | 107 | ino_t ino; |
7188ac27 | 108 | struct inode **ipp; |
5d5124a1 | 109 | { |
7188ac27 KM |
110 | dev_t dev = xp->i_dev; |
111 | struct mount *mntp = ITOV(xp)->v_mount; | |
112 | register struct fs *fs = VFSTOUFS(mntp)->um_fs; | |
113 | register struct inode *ip, *iq; | |
114 | register struct vnode *vp; | |
115 | struct inode *nip; | |
116 | struct buf *bp; | |
117 | struct dinode tdip, *dp; | |
118 | union ihead *ih; | |
119 | int error; | |
2e64ab65 | 120 | |
5d5124a1 | 121 | loop: |
3ebac878 RE |
122 | ih = &ihead[INOHASH(dev, ino)]; |
123 | for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) | |
7494ef16 | 124 | if (ino == ip->i_number && dev == ip->i_dev) { |
8ac1234a SL |
125 | /* |
126 | * Following is essentially an inline expanded | |
127 | * copy of igrab(), expanded inline for speed, | |
128 | * and so that the test for a mounted on inode | |
129 | * can be deferred until after we are sure that | |
130 | * the inode isn't busy. | |
131 | */ | |
5c2ba954 | 132 | if ((ip->i_flag&ILOCKED) != 0) { |
5d5124a1 BJ |
133 | ip->i_flag |= IWANT; |
134 | sleep((caddr_t)ip, PINOD); | |
135 | goto loop; | |
136 | } | |
7188ac27 KM |
137 | vp = ITOV(ip); |
138 | if (vp->v_count == 0) { /* ino on free list */ | |
3ebac878 RE |
139 | if (iq = ip->i_freef) |
140 | iq->i_freeb = ip->i_freeb; | |
141 | else | |
142 | ifreet = ip->i_freeb; | |
143 | *ip->i_freeb = iq; | |
144 | ip->i_freef = NULL; | |
145 | ip->i_freeb = NULL; | |
146 | } | |
aed86454 | 147 | ILOCK(ip); |
7188ac27 KM |
148 | vp->v_count++; |
149 | *ipp = ip; | |
150 | return(0); | |
151 | } | |
152 | if (error = getnewino(dev, ino, &nip)) { | |
153 | *ipp = 0; | |
154 | return (error); | |
155 | } | |
156 | ip = nip; | |
157 | /* | |
158 | * Read in the disk contents for the inode. | |
159 | */ | |
160 | if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)), | |
161 | (int)fs->fs_bsize, &bp)) { | |
162 | /* | |
163 | * The inode doesn't contain anything useful, so it would | |
164 | * be misleading to leave it on its hash chain. Iput() will | |
165 | * take care of putting it back on the free list. We also | |
166 | * lose its inumber, just in case. | |
167 | */ | |
168 | remque(ip); | |
169 | ip->i_forw = ip; | |
170 | ip->i_back = ip; | |
171 | ip->i_number = 0; | |
172 | INSFREE(ip); | |
aed86454 | 173 | iunlock(ip); |
7188ac27 KM |
174 | ip->i_flag = 0; |
175 | brelse(bp); | |
176 | *ipp = 0; | |
177 | return(error); | |
178 | } | |
179 | /* | |
180 | * Check to see if the new inode represents a block device | |
181 | * for which we already have an inode (either because of | |
182 | * bdevvp() or because of a different inode representing | |
183 | * the same block device). If such an alias exists, put the | |
184 | * just allocated inode back on the free list, and replace | |
185 | * the contents of the existing inode with the contents of | |
186 | * the new inode. | |
187 | */ | |
188 | dp = bp->b_un.b_dino; | |
189 | dp += itoo(fs, ino); | |
190 | if ((dp->di_mode & IFMT) != IFBLK) { | |
191 | ip->i_ic = dp->di_ic; | |
192 | brelse(bp); | |
193 | } else { | |
194 | again: | |
195 | for (iq = bdevlisth; iq; iq = iq->i_devlst) { | |
196 | if (dp->di_rdev != ITOV(iq)->v_rdev) | |
197 | continue; | |
198 | igrab(iq); | |
199 | if (dp->di_rdev != ITOV(iq)->v_rdev) { | |
200 | iput(iq); | |
201 | goto again; | |
202 | } | |
203 | /* | |
204 | * Discard unneeded inode. | |
205 | */ | |
206 | remque(ip); | |
207 | ip->i_forw = ip; | |
208 | ip->i_back = ip; | |
209 | ip->i_number = 0; | |
210 | INSFREE(ip); | |
aed86454 | 211 | iunlock(ip); |
7188ac27 KM |
212 | ip->i_flag = 0; |
213 | /* | |
214 | * Reinitialize aliased inode. | |
215 | * We must release the buffer that we just read | |
216 | * before doing the iupdat() to avoid a possible | |
217 | * deadlock with updating an inode in the same | |
218 | * disk block. | |
219 | */ | |
220 | ip = iq; | |
221 | vp = ITOV(iq); | |
222 | tdip.di_ic = dp->di_ic; | |
223 | brelse(bp); | |
224 | error = iupdat(ip, &time, &time, 1); | |
225 | ip->i_ic = tdip.di_ic; | |
226 | remque(ip); | |
227 | insque(ip, ih); | |
228 | ip->i_dev = dev; | |
229 | ip->i_number = ino; | |
230 | if (ip->i_devvp) { | |
231 | vrele(ip->i_devvp); | |
232 | ip->i_devvp = 0; | |
233 | } | |
234 | cache_purge(vp); | |
235 | break; | |
236 | } | |
237 | if (iq == 0) { | |
238 | ip->i_ic = dp->di_ic; | |
239 | brelse(bp); | |
240 | ip->i_devlst = bdevlisth; | |
241 | bdevlisth = ip; | |
5d5124a1 | 242 | } |
7188ac27 KM |
243 | } |
244 | /* | |
245 | * Finish inode initialization. | |
246 | */ | |
247 | ip->i_fs = fs; | |
248 | ip->i_devvp = VFSTOUFS(mntp)->um_devvp; | |
249 | ip->i_devvp->v_count++; | |
250 | /* | |
251 | * Initialize the associated vnode | |
252 | */ | |
253 | vp = ITOV(ip); | |
254 | vinit(vp, mntp, IFTOVT(ip->i_mode), &ufs_vnodeops); | |
255 | if (vp->v_type == VCHR || vp->v_type == VBLK) { | |
256 | vp->v_rdev = ip->i_rdev; | |
257 | vp->v_op = &blk_vnodeops; | |
258 | } | |
259 | if (ino == ROOTINO) | |
260 | vp->v_flag |= VROOT; | |
261 | #ifdef QUOTA | |
262 | if (ip->i_mode != 0) | |
263 | ip->i_dquot = inoquota(ip); | |
264 | #endif | |
afd7e202 KM |
265 | /* |
266 | * Set up a generation number for this inode if it does not | |
267 | * already have one. This should only happen on old filesystems. | |
268 | */ | |
269 | if (ip->i_gen == 0) { | |
270 | if (++nextgennumber < (u_long)time.tv_sec) | |
271 | nextgennumber = time.tv_sec; | |
272 | ip->i_gen = nextgennumber; | |
273 | if ((vp->v_mount->m_flag & M_RDONLY) == 0) | |
274 | ip->i_flag |= IMOD; | |
275 | } | |
7188ac27 KM |
276 | *ipp = ip; |
277 | return (0); | |
278 | } | |
3ebac878 | 279 | |
7188ac27 KM |
280 | /* |
281 | * Allocate a new inode. | |
282 | * | |
283 | * Put it onto its hash chain and lock it so that other requests for | |
284 | * this inode will block if they arrive while we are sleeping waiting | |
285 | * for old data structures to be purged or for the contents of the disk | |
286 | * portion of this inode to be read. | |
287 | */ | |
288 | getnewino(dev, ino, ipp) | |
289 | dev_t dev; | |
290 | ino_t ino; | |
291 | struct inode **ipp; | |
292 | { | |
293 | union ihead *ih; | |
294 | register struct inode *ip, *iq; | |
295 | register struct vnode *vp; | |
296 | ||
297 | /* | |
298 | * Remove the next inode from the free list. | |
299 | */ | |
3ebac878 | 300 | if ((ip = ifreeh) == NULL) { |
945fbb1b | 301 | tablefull("inode"); |
7188ac27 KM |
302 | *ipp = 0; |
303 | return(ENFILE); | |
5d5124a1 | 304 | } |
7188ac27 KM |
305 | vp = ITOV(ip); |
306 | if (vp->v_count) | |
bed1bb6e | 307 | panic("free inode isn't"); |
3ebac878 RE |
308 | if (iq = ip->i_freef) |
309 | iq->i_freeb = &ifreeh; | |
310 | ifreeh = iq; | |
311 | ip->i_freef = NULL; | |
312 | ip->i_freeb = NULL; | |
313 | /* | |
314 | * Now to take inode off the hash chain it was on | |
315 | * (initially, or after an iflush, it is on a "hash chain" | |
7188ac27 KM |
316 | * consisting entirely of itself, and pointed to by no-one) |
317 | * and put it on the chain for its new (ino, dev) pair. | |
3ebac878 | 318 | */ |
32dc2b7e | 319 | remque(ip); |
5d5124a1 BJ |
320 | ip->i_dev = dev; |
321 | ip->i_number = ino; | |
7188ac27 KM |
322 | if (dev != NODEV) { |
323 | ih = &ihead[INOHASH(dev, ino)]; | |
324 | insque(ip, ih); | |
325 | } | |
aed86454 KM |
326 | ip->i_flag = 0; |
327 | ILOCK(ip); | |
6459ebe0 | 328 | ip->i_lastr = 0; |
ec67a3ce | 329 | #endif SECSIZE |
5d5124a1 | 330 | /* |
7188ac27 | 331 | * Purge old data structures associated with the inode. |
5d5124a1 | 332 | */ |
7188ac27 KM |
333 | cache_purge(vp); |
334 | if (ip->i_devvp) { | |
335 | vrele(ip->i_devvp); | |
336 | ip->i_devvp = 0; | |
5d5124a1 | 337 | } |
b4567e9c | 338 | #ifdef QUOTA |
7188ac27 KM |
339 | dqrele(ip->i_dquot); |
340 | ip->i_dquot = NODQUOT; | |
89045c38 | 341 | #endif |
7188ac27 KM |
342 | if (vp->v_type == VBLK) { |
343 | if (bdevlisth == ip) { | |
344 | bdevlisth = ip->i_devlst; | |
345 | } else { | |
346 | for (iq = bdevlisth; iq; iq = iq->i_devlst) { | |
347 | if (iq->i_devlst != ip) | |
348 | continue; | |
349 | iq->i_devlst = ip->i_devlst; | |
350 | break; | |
351 | } | |
352 | if (iq == NULL) | |
353 | panic("missing bdev"); | |
354 | } | |
355 | } | |
356 | *ipp = ip; | |
357 | return (0); | |
5d5124a1 BJ |
358 | } |
359 | ||
8ac1234a SL |
360 | /* |
361 | * Convert a pointer to an inode into a reference to an inode. | |
362 | * | |
363 | * This is basically the internal piece of iget (after the | |
364 | * inode pointer is located) but without the test for mounted | |
365 | * filesystems. It is caller's responsibility to check that | |
366 | * the inode pointer is valid. | |
367 | */ | |
368 | igrab(ip) | |
369 | register struct inode *ip; | |
370 | { | |
7188ac27 KM |
371 | register struct vnode *vp = ITOV(ip); |
372 | ||
8ac1234a SL |
373 | while ((ip->i_flag&ILOCKED) != 0) { |
374 | ip->i_flag |= IWANT; | |
375 | sleep((caddr_t)ip, PINOD); | |
376 | } | |
7188ac27 | 377 | if (vp->v_count == 0) { /* ino on free list */ |
8ac1234a SL |
378 | register struct inode *iq; |
379 | ||
380 | if (iq = ip->i_freef) | |
381 | iq->i_freeb = ip->i_freeb; | |
382 | else | |
383 | ifreet = ip->i_freeb; | |
384 | *ip->i_freeb = iq; | |
385 | ip->i_freef = NULL; | |
386 | ip->i_freeb = NULL; | |
387 | } | |
7188ac27 | 388 | vp->v_count++; |
aed86454 | 389 | ILOCK(ip); |
8ac1234a SL |
390 | } |
391 | ||
7188ac27 KM |
392 | /* |
393 | * Create a vnode for a block device. | |
394 | * Used for root filesystem, argdev, and swap areas. | |
395 | */ | |
396 | bdevvp(dev, vpp) | |
397 | dev_t dev; | |
398 | struct vnode **vpp; | |
399 | { | |
400 | register struct inode *ip; | |
401 | register struct vnode *vp; | |
402 | struct inode *nip; | |
403 | int error; | |
404 | ||
405 | /* | |
406 | * Check for the existence of an existing vnode. | |
407 | */ | |
408 | again: | |
409 | for (ip = bdevlisth; ip; ip = ip->i_devlst) { | |
410 | vp = ITOV(ip); | |
411 | if (dev != vp->v_rdev) | |
412 | continue; | |
413 | igrab(ip); | |
414 | if (dev != vp->v_rdev) { | |
415 | iput(ip); | |
416 | goto again; | |
417 | } | |
418 | IUNLOCK(ip); | |
419 | *vpp = vp; | |
420 | return (0); | |
421 | } | |
422 | if (error = getnewino(NODEV, (ino_t)0, &nip)) { | |
423 | *vpp = 0; | |
424 | return (error); | |
425 | } | |
426 | ip = nip; | |
427 | ip->i_fs = 0; | |
428 | ip->i_devlst = bdevlisth; | |
429 | bdevlisth = ip; | |
430 | vp = ITOV(ip); | |
431 | vinit(vp, 0, VBLK, &blk_vnodeops); | |
432 | vp->v_rdev = dev; | |
433 | IUNLOCK(ip); | |
434 | *vpp = vp; | |
435 | return (0); | |
436 | } | |
437 | ||
5d5124a1 BJ |
438 | /* |
439 | * Decrement reference count of | |
440 | * an inode structure. | |
441 | * On the last reference, | |
442 | * write the inode out and if necessary, | |
443 | * truncate and deallocate the file. | |
444 | */ | |
445 | iput(ip) | |
7494ef16 | 446 | register struct inode *ip; |
5d5124a1 | 447 | { |
ff56f48a | 448 | |
5c2ba954 | 449 | if ((ip->i_flag & ILOCKED) == 0) |
ff56f48a | 450 | panic("iput"); |
a388503d | 451 | IUNLOCK(ip); |
7188ac27 | 452 | vrele(ITOV(ip)); |
ff56f48a KM |
453 | } |
454 | ||
7188ac27 KM |
455 | |
456 | ufs_inactive(vp) | |
457 | struct vnode *vp; | |
ff56f48a | 458 | { |
7188ac27 KM |
459 | register struct inode *ip = VTOI(vp); |
460 | int mode, error; | |
461 | ||
462 | if (ITOV(ip)->v_count != 0) | |
463 | panic("ufs_inactive: not inactive"); | |
aed86454 | 464 | ILOCK(ip); |
7188ac27 KM |
465 | if (ip->i_nlink <= 0 && (ITOV(ip)->v_mount->m_flag&M_RDONLY) == 0) { |
466 | error = itrunc(ip, (u_long)0); | |
467 | mode = ip->i_mode; | |
468 | ip->i_mode = 0; | |
469 | ip->i_rdev = 0; | |
470 | ip->i_flag |= IUPD|ICHG; | |
471 | ifree(ip, ip->i_number, mode); | |
b4567e9c | 472 | #ifdef QUOTA |
7188ac27 KM |
473 | (void) chkiq(ip->i_dev, ip, ip->i_uid, 0); |
474 | dqrele(ip->i_dquot); | |
475 | ip->i_dquot = NODQUOT; | |
89045c38 | 476 | #endif |
7188ac27 KM |
477 | } |
478 | IUPDAT(ip, &time, &time, 0); | |
479 | IUNLOCK(ip); | |
480 | ip->i_flag = 0; | |
481 | /* | |
482 | * Put the inode on the end of the free list. | |
483 | * Possibly in some cases it would be better to | |
484 | * put the inode at the head of the free list, | |
485 | * (eg: where i_mode == 0 || i_number == 0). | |
486 | */ | |
487 | INSFREE(ip); | |
488 | return (error); | |
5d5124a1 BJ |
489 | } |
490 | ||
491 | /* | |
492 | * Check accessed and update flags on | |
493 | * an inode structure. | |
494 | * If any is on, update the inode | |
495 | * with the current time. | |
c0bb1685 BJ |
496 | * If waitfor is given, then must insure |
497 | * i/o order so wait for write to complete. | |
5d5124a1 | 498 | */ |
c0bb1685 | 499 | iupdat(ip, ta, tm, waitfor) |
7494ef16 | 500 | register struct inode *ip; |
b32450f4 | 501 | struct timeval *ta, *tm; |
7494ef16 | 502 | int waitfor; |
5d5124a1 | 503 | { |
7188ac27 KM |
504 | struct buf *bp; |
505 | struct vnode *vp = ITOV(ip); | |
5d5124a1 | 506 | struct dinode *dp; |
ec67a3ce | 507 | register struct fs *fs; |
5d5124a1 | 508 | |
ec67a3ce | 509 | fs = ip->i_fs; |
7188ac27 KM |
510 | if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) |
511 | return (0); | |
512 | if (vp->v_mount->m_flag & M_RDONLY) | |
513 | return (0); | |
514 | error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), | |
515 | (int)fs->fs_bsize, &bp); | |
516 | if (error) { | |
517 | brelse(bp); | |
518 | return (error); | |
519 | } | |
520 | if (ip->i_flag&IACC) | |
521 | ip->i_atime = ta->tv_sec; | |
522 | if (ip->i_flag&IUPD) | |
523 | ip->i_mtime = tm->tv_sec; | |
524 | if (ip->i_flag&ICHG) | |
525 | ip->i_ctime = time.tv_sec; | |
526 | ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); | |
527 | dp = bp->b_un.b_dino + itoo(fs, ip->i_number); | |
528 | dp->di_ic = ip->i_ic; | |
529 | if (waitfor) { | |
530 | return (bwrite(bp)); | |
531 | } else { | |
532 | bdwrite(bp); | |
533 | return (0); | |
5d5124a1 BJ |
534 | } |
535 | } | |
536 | ||
9c03b2c0 SL |
537 | #define SINGLE 0 /* index of single indirect block */ |
538 | #define DOUBLE 1 /* index of double indirect block */ | |
539 | #define TRIPLE 2 /* index of triple indirect block */ | |
5d5124a1 | 540 | /* |
528f664c SL |
541 | * Truncate the inode ip to at most |
542 | * length size. Free affected disk | |
543 | * blocks -- the blocks of the file | |
544 | * are removed in reverse order. | |
9c03b2c0 SL |
545 | * |
546 | * NB: triple indirect blocks are untested. | |
5d5124a1 | 547 | */ |
9c03b2c0 | 548 | itrunc(oip, length) |
28821bc5 | 549 | register struct inode *oip; |
4f083fd7 | 550 | u_long length; |
5d5124a1 | 551 | { |
4f083fd7 | 552 | register daddr_t lastblock; |
a5e62f37 | 553 | daddr_t bn, lbn, lastiblock[NIADDR]; |
6459ebe0 | 554 | register struct fs *fs; |
9c03b2c0 | 555 | register struct inode *ip; |
28821bc5 | 556 | struct buf *bp; |
7188ac27 KM |
557 | int offset, osize, size, level; |
558 | long count, nblocks, blocksreleased = 0; | |
28821bc5 | 559 | register int i; |
7188ac27 | 560 | int error, allerror = 0; |
9c03b2c0 | 561 | struct inode tip; |
4f083fd7 | 562 | |
7b2e4f05 SL |
563 | if (oip->i_size <= length) { |
564 | oip->i_flag |= ICHG|IUPD; | |
7188ac27 KM |
565 | error = iupdat(oip, &time, &time, 1); |
566 | return (error); | |
7b2e4f05 | 567 | } |
c0bb1685 | 568 | /* |
9c03b2c0 SL |
569 | * Calculate index into inode's block list of |
570 | * last direct and indirect blocks (if any) | |
571 | * which we want to keep. Lastblock is -1 when | |
572 | * the file is truncated to 0. | |
c0bb1685 | 573 | */ |
9c03b2c0 | 574 | fs = oip->i_fs; |
4f083fd7 | 575 | lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; |
9c03b2c0 SL |
576 | lastiblock[SINGLE] = lastblock - NDADDR; |
577 | lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); | |
578 | lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); | |
08d9a8ec | 579 | nblocks = btodb(fs->fs_bsize); |
6459ebe0 | 580 | /* |
28821bc5 KM |
581 | * Update the size of the file. If the file is not being |
582 | * truncated to a block boundry, the contents of the | |
583 | * partial block following the end of the file must be | |
584 | * zero'ed in case it ever become accessable again because | |
585 | * of subsequent file growth. | |
586 | */ | |
587 | osize = oip->i_size; | |
588 | offset = blkoff(fs, length); | |
589 | if (offset == 0) { | |
590 | oip->i_size = length; | |
591 | } else { | |
592 | lbn = lblkno(fs, length); | |
7188ac27 KM |
593 | error = balloc(oip, lbn, offset, &bn, B_CLRBUF); |
594 | if (error) | |
595 | return (error); | |
596 | if ((long)bn < 0) | |
597 | panic("itrunc: hole"); | |
28821bc5 KM |
598 | oip->i_size = length; |
599 | size = blksize(fs, oip, lbn); | |
ec67a3ce | 600 | count = howmany(size, CLBYTES); |
7188ac27 KM |
601 | munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); |
602 | error = bread(oip->i_devvp, bn, size, &bp); | |
603 | if (error) { | |
28821bc5 KM |
604 | oip->i_size = osize; |
605 | brelse(bp); | |
7188ac27 | 606 | return (error); |
28821bc5 | 607 | } |
a5e62f37 | 608 | bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); |
28821bc5 KM |
609 | bdwrite(bp); |
610 | } | |
611 | /* | |
612 | * Update file and block pointers | |
9c03b2c0 SL |
613 | * on disk before we start freeing blocks. |
614 | * If we crash before free'ing blocks below, | |
615 | * the blocks will be returned to the free list. | |
616 | * lastiblock values are also normalized to -1 | |
617 | * for calls to indirtrunc below. | |
6459ebe0 | 618 | */ |
9c03b2c0 | 619 | tip = *oip; |
28821bc5 | 620 | tip.i_size = osize; |
9c03b2c0 SL |
621 | for (level = TRIPLE; level >= SINGLE; level--) |
622 | if (lastiblock[level] < 0) { | |
623 | oip->i_ib[level] = 0; | |
624 | lastiblock[level] = -1; | |
4f083fd7 | 625 | } |
9c03b2c0 SL |
626 | for (i = NDADDR - 1; i > lastblock; i--) |
627 | oip->i_db[i] = 0; | |
9c03b2c0 | 628 | oip->i_flag |= ICHG|IUPD; |
7188ac27 | 629 | allerror = syncip(oip); |
9c03b2c0 | 630 | |
6459ebe0 | 631 | /* |
9c03b2c0 | 632 | * Indirect blocks first. |
6459ebe0 | 633 | */ |
28821bc5 | 634 | ip = &tip; |
9c03b2c0 SL |
635 | for (level = TRIPLE; level >= SINGLE; level--) { |
636 | bn = ip->i_ib[level]; | |
4f083fd7 | 637 | if (bn != 0) { |
7188ac27 KM |
638 | error = indirtrunc(ip, bn, lastiblock[level], level, |
639 | &count); | |
640 | if (error) | |
641 | allerror = error; | |
642 | blocksreleased += count; | |
9c03b2c0 SL |
643 | if (lastiblock[level] < 0) { |
644 | ip->i_ib[level] = 0; | |
ced3a252 | 645 | blkfree(ip, bn, (off_t)fs->fs_bsize); |
9c03b2c0 | 646 | blocksreleased += nblocks; |
9c03b2c0 SL |
647 | } |
648 | } | |
649 | if (lastiblock[level] >= 0) | |
650 | goto done; | |
4f083fd7 | 651 | } |
9c03b2c0 | 652 | |
6459ebe0 | 653 | /* |
9c03b2c0 | 654 | * All whole direct blocks or frags. |
6459ebe0 | 655 | */ |
4f083fd7 | 656 | for (i = NDADDR - 1; i > lastblock; i--) { |
8011f5df | 657 | register off_t bsize; |
4f083fd7 | 658 | |
6459ebe0 | 659 | bn = ip->i_db[i]; |
4f083fd7 | 660 | if (bn == 0) |
5d5124a1 | 661 | continue; |
4f083fd7 | 662 | ip->i_db[i] = 0; |
0b355a6e | 663 | bsize = (off_t)blksize(fs, ip, i); |
ced3a252 | 664 | blkfree(ip, bn, bsize); |
0b355a6e | 665 | blocksreleased += btodb(bsize); |
4f083fd7 | 666 | } |
9c03b2c0 SL |
667 | if (lastblock < 0) |
668 | goto done; | |
669 | ||
4f083fd7 SL |
670 | /* |
671 | * Finally, look for a change in size of the | |
672 | * last direct block; release any frags. | |
673 | */ | |
9c03b2c0 SL |
674 | bn = ip->i_db[lastblock]; |
675 | if (bn != 0) { | |
8011f5df | 676 | off_t oldspace, newspace; |
9c03b2c0 | 677 | |
4f083fd7 SL |
678 | /* |
679 | * Calculate amount of space we're giving | |
680 | * back as old block size minus new block size. | |
681 | */ | |
9c03b2c0 | 682 | oldspace = blksize(fs, ip, lastblock); |
4f083fd7 | 683 | ip->i_size = length; |
9c03b2c0 SL |
684 | newspace = blksize(fs, ip, lastblock); |
685 | if (newspace == 0) | |
686 | panic("itrunc: newspace"); | |
687 | if (oldspace - newspace > 0) { | |
4f083fd7 SL |
688 | /* |
689 | * Block number of space to be free'd is | |
690 | * the old block # plus the number of frags | |
691 | * required for the storage we're keeping. | |
692 | */ | |
9c03b2c0 | 693 | bn += numfrags(fs, newspace); |
ced3a252 | 694 | blkfree(ip, bn, oldspace - newspace); |
08d9a8ec | 695 | blocksreleased += btodb(oldspace - newspace); |
4f083fd7 | 696 | } |
5d5124a1 | 697 | } |
4f083fd7 | 698 | done: |
9c03b2c0 SL |
699 | /* BEGIN PARANOIA */ |
700 | for (level = SINGLE; level <= TRIPLE; level++) | |
701 | if (ip->i_ib[level] != oip->i_ib[level]) | |
702 | panic("itrunc1"); | |
703 | for (i = 0; i < NDADDR; i++) | |
704 | if (ip->i_db[i] != oip->i_db[i]) | |
705 | panic("itrunc2"); | |
706 | /* END PARANOIA */ | |
08d9a8ec SL |
707 | oip->i_blocks -= blocksreleased; |
708 | if (oip->i_blocks < 0) /* sanity */ | |
709 | oip->i_blocks = 0; | |
710 | oip->i_flag |= ICHG; | |
b4567e9c | 711 | #ifdef QUOTA |
08d9a8ec | 712 | (void) chkdq(oip, -blocksreleased, 0); |
89045c38 | 713 | #endif |
7188ac27 | 714 | return (allerror); |
5d5124a1 BJ |
715 | } |
716 | ||
4f083fd7 SL |
717 | /* |
718 | * Release blocks associated with the inode ip and | |
719 | * stored in the indirect block bn. Blocks are free'd | |
720 | * in LIFO order up to (but not including) lastbn. If | |
9c03b2c0 SL |
721 | * level is greater than SINGLE, the block is an indirect |
722 | * block and recursive calls to indirtrunc must be used to | |
723 | * cleanse other indirect blocks. | |
724 | * | |
725 | * NB: triple indirect blocks are untested. | |
4f083fd7 | 726 | */ |
7188ac27 | 727 | indirtrunc(ip, bn, lastbn, level, countp) |
6459ebe0 | 728 | register struct inode *ip; |
4f083fd7 | 729 | daddr_t bn, lastbn; |
9c03b2c0 | 730 | int level; |
7188ac27 | 731 | long *countp; |
5d5124a1 | 732 | { |
4f083fd7 | 733 | register int i; |
b30358ab | 734 | struct buf *bp; |
9c03b2c0 | 735 | register struct fs *fs = ip->i_fs; |
b30358ab KM |
736 | register daddr_t *bap; |
737 | daddr_t *copy, nb, last; | |
7188ac27 KM |
738 | long blkcount, factor; |
739 | int nblocks, blocksreleased = 0; | |
740 | int error, allerror = 0; | |
5d5124a1 | 741 | |
9c03b2c0 SL |
742 | /* |
743 | * Calculate index in current block of last | |
744 | * block to be kept. -1 indicates the entire | |
745 | * block so we need not calculate the index. | |
746 | */ | |
747 | factor = 1; | |
748 | for (i = SINGLE; i < level; i++) | |
749 | factor *= NINDIR(fs); | |
4f083fd7 | 750 | last = lastbn; |
9c03b2c0 SL |
751 | if (lastbn > 0) |
752 | last /= factor; | |
08d9a8ec | 753 | nblocks = btodb(fs->fs_bsize); |
9c03b2c0 SL |
754 | /* |
755 | * Get buffer of block pointers, zero those | |
756 | * entries corresponding to blocks to be free'd, | |
757 | * and update on disk copy first. | |
758 | */ | |
ec67a3ce MK |
759 | #ifdef SECSIZE |
760 | bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize, | |
761 | fs->fs_dbsize); | |
762 | #else SECSIZE | |
7188ac27 KM |
763 | error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, &bp); |
764 | if (error) { | |
9c03b2c0 | 765 | brelse(bp); |
7188ac27 KM |
766 | *countp = 0; |
767 | return (error); | |
9c03b2c0 SL |
768 | } |
769 | bap = bp->b_un.b_daddr; | |
b30358ab KM |
770 | MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); |
771 | bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); | |
9c03b2c0 SL |
772 | bzero((caddr_t)&bap[last + 1], |
773 | (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); | |
7188ac27 KM |
774 | error = bwrite(bp); |
775 | if (error) | |
776 | allerror = error; | |
b30358ab | 777 | bap = copy; |
4f083fd7 | 778 | |
9c03b2c0 SL |
779 | /* |
780 | * Recursively free totally unused blocks. | |
781 | */ | |
782 | for (i = NINDIR(fs) - 1; i > last; i--) { | |
5d5124a1 | 783 | nb = bap[i]; |
4f083fd7 | 784 | if (nb == 0) |
5d5124a1 | 785 | continue; |
7188ac27 KM |
786 | if (level > SINGLE) { |
787 | error = indirtrunc(ip, nb, (daddr_t)-1, level - 1, | |
788 | &blkcount); | |
789 | if (error) | |
790 | allerror = error; | |
791 | blocksreleased += blkcount; | |
792 | } | |
ced3a252 | 793 | blkfree(ip, nb, (off_t)fs->fs_bsize); |
4f083fd7 | 794 | blocksreleased += nblocks; |
4f083fd7 | 795 | } |
9c03b2c0 SL |
796 | |
797 | /* | |
798 | * Recursively free last partial block. | |
799 | */ | |
800 | if (level > SINGLE && lastbn >= 0) { | |
801 | last = lastbn % factor; | |
4f083fd7 | 802 | nb = bap[i]; |
7188ac27 KM |
803 | if (nb != 0) { |
804 | error = indirtrunc(ip, nb, last, level - 1, &blkcount); | |
805 | if (error) | |
806 | allerror = error; | |
807 | blocksreleased += blkcount; | |
808 | } | |
5d5124a1 | 809 | } |
b30358ab | 810 | FREE(copy, M_TEMP); |
7188ac27 KM |
811 | *countp = blocksreleased; |
812 | return (allerror); | |
5d5124a1 BJ |
813 | } |
814 | ||
3ebac878 | 815 | /* |
ec67a3ce | 816 | * Remove any inodes in the inode cache belonging to dev. |
3ebac878 RE |
817 | * |
818 | * There should not be any active ones, return error if any are found | |
ec67a3ce | 819 | * (nb: this is a user error, not a system err). |
3ebac878 | 820 | */ |
b4567e9c | 821 | #ifdef QUOTA |
4147b3f6 | 822 | iflush(dev, iq) |
89045c38 | 823 | dev_t dev; |
4147b3f6 | 824 | struct inode *iq; |
89045c38 | 825 | #else |
3ebac878 RE |
826 | iflush(dev) |
827 | dev_t dev; | |
89045c38 | 828 | #endif |
3ebac878 | 829 | { |
32dc2b7e | 830 | register struct inode *ip; |
3ebac878 RE |
831 | |
832 | for (ip = inode; ip < inodeNINODE; ip++) { | |
b4567e9c | 833 | #ifdef QUOTA |
89045c38 RE |
834 | if (ip != iq && ip->i_dev == dev) |
835 | #else | |
3ebac878 | 836 | if (ip->i_dev == dev) |
89045c38 | 837 | #endif |
7188ac27 | 838 | if (ITOV(ip)->v_count) |
ec67a3ce | 839 | return (EBUSY); |
3ebac878 | 840 | else { |
32dc2b7e | 841 | remque(ip); |
3ebac878 RE |
842 | ip->i_forw = ip; |
843 | ip->i_back = ip; | |
844 | /* | |
7188ac27 | 845 | * as v_count == 0, the inode was on the free |
3ebac878 RE |
846 | * list already, just leave it there, it will |
847 | * fall off the bottom eventually. We could | |
848 | * perhaps move it to the head of the free | |
849 | * list, but as umounts are done so | |
850 | * infrequently, we would gain very little, | |
851 | * while making the code bigger. | |
852 | */ | |
b4567e9c | 853 | #ifdef QUOTA |
89045c38 RE |
854 | dqrele(ip->i_dquot); |
855 | ip->i_dquot = NODQUOT; | |
856 | #endif | |
7188ac27 KM |
857 | if (ip->i_devvp) { |
858 | vrele(ip->i_devvp); | |
859 | ip->i_devvp = 0; | |
860 | } | |
3ebac878 | 861 | } |
3ebac878 | 862 | } |
ec67a3ce | 863 | return (0); |
3ebac878 RE |
864 | } |
865 | ||
d6a210b8 | 866 | /* |
7494ef16 | 867 | * Lock an inode. If its already locked, set the WANT bit and sleep. |
d6a210b8 | 868 | */ |
7494ef16 BJ |
869 | ilock(ip) |
870 | register struct inode *ip; | |
d6a210b8 BJ |
871 | { |
872 | ||
7188ac27 KM |
873 | while (ip->i_flag & ILOCKED) { |
874 | ip->i_flag |= IWANT; | |
875 | (void) sleep((caddr_t)ip, PINOD); | |
876 | } | |
877 | ip->i_flag |= ILOCKED; | |
d6a210b8 BJ |
878 | } |
879 | ||
880 | /* | |
7494ef16 | 881 | * Unlock an inode. If WANT bit is on, wakeup. |
d6a210b8 | 882 | */ |
ff56f48a | 883 | iunlock(ip) |
7494ef16 | 884 | register struct inode *ip; |
d6a210b8 BJ |
885 | { |
886 | ||
7188ac27 KM |
887 | if ((ip->i_flag & ILOCKED) == 0) |
888 | printf("unlocking unlocked inode %d on dev 0x%x\n", | |
889 | ip->i_number, ip->i_dev); | |
890 | ip->i_flag &= ~ILOCKED; | |
891 | if (ip->i_flag&IWANT) { | |
892 | ip->i_flag &= ~IWANT; | |
893 | wakeup((caddr_t)ip); | |
894 | } | |
895 | } | |
896 | ||
897 | /* | |
898 | * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. | |
899 | * The mode is shifted to select the owner/group/other fields. The | |
900 | * super user is granted all permissions. | |
901 | * | |
902 | * NB: Called from vnode op table. It seems this could all be done | |
903 | * using vattr's but... | |
904 | */ | |
905 | iaccess(ip, mode, cred) | |
906 | register struct inode *ip; | |
907 | register int mode; | |
908 | struct ucred *cred; | |
909 | { | |
910 | register gid_t *gp; | |
911 | register struct vnode *vp = ITOV(ip); | |
912 | int i; | |
913 | ||
914 | /* | |
915 | * If you're the super-user, | |
916 | * you always get access. | |
917 | */ | |
918 | if (cred->cr_uid == 0) | |
919 | return (0); | |
920 | /* | |
921 | * Access check is based on only one of owner, group, public. | |
922 | * If not owner, then check group. If not a member of the | |
923 | * group, then check public access. | |
924 | */ | |
925 | if (cred->cr_uid != ip->i_uid) { | |
926 | mode >>= 3; | |
927 | gp = cred->cr_groups; | |
928 | for (i = 0; i < cred->cr_ngroups; i++, gp++) | |
929 | if (ip->i_gid == *gp) | |
930 | goto found; | |
931 | mode >>= 3; | |
932 | found: | |
933 | ; | |
934 | } | |
935 | if ((ip->i_mode & mode) != 0) | |
936 | return (0); | |
937 | return (EACCES); | |
d6a210b8 | 938 | } |