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 | * | |
4b61628b | 17 | * @(#)lfs_inode.c 7.30 (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" |
71c4cb8d | 24 | #include "proc.h" |
7188ac27 | 25 | #include "file.h" |
94368568 | 26 | #include "buf.h" |
0b355a6e | 27 | #include "cmap.h" |
7188ac27 | 28 | #include "vnode.h" |
4b61628b | 29 | #include "../ufs/quota.h" |
7188ac27 KM |
30 | #include "../ufs/inode.h" |
31 | #include "../ufs/fs.h" | |
32 | #include "../ufs/ufsmount.h" | |
94368568 | 33 | #include "kernel.h" |
b30358ab | 34 | #include "malloc.h" |
5d5124a1 | 35 | |
c22c66ff | 36 | #define INOHSZ 512 |
3ebac878 RE |
37 | #if ((INOHSZ&(INOHSZ-1)) == 0) |
38 | #define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1)) | |
39 | #else | |
a3a9487d | 40 | #define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ) |
3ebac878 RE |
41 | #endif |
42 | ||
a1e9dd57 | 43 | union ihead { |
3ebac878 RE |
44 | union ihead *ih_head[2]; |
45 | struct inode *ih_chain[2]; | |
46 | } ihead[INOHSZ]; | |
47 | ||
2bf2d153 KM |
48 | int prtactive; /* 1 => print out reclaim of active vnodes */ |
49 | ||
5d5124a1 | 50 | /* |
a1e9dd57 | 51 | * Initialize hash links for inodes. |
5d5124a1 | 52 | */ |
1259a9f9 | 53 | ufs_init() |
5d5124a1 BJ |
54 | { |
55 | register int i; | |
a1e9dd57 | 56 | register union ihead *ih = ihead; |
5d5124a1 | 57 | |
ff4fb102 | 58 | #ifndef lint |
a1e9dd57 KM |
59 | if (VN_MAXPRIVATE < sizeof(struct inode)) |
60 | panic("ihinit: too small"); | |
ff4fb102 | 61 | #endif /* not lint */ |
3ebac878 RE |
62 | for (i = INOHSZ; --i >= 0; ih++) { |
63 | ih->ih_head[0] = ih; | |
64 | ih->ih_head[1] = ih; | |
65 | } | |
4b61628b KM |
66 | #ifdef QUOTA |
67 | dqinit(); | |
68 | #endif /* QUOTA */ | |
5d5124a1 BJ |
69 | } |
70 | ||
3ebac878 | 71 | /* |
7188ac27 | 72 | * Look up an vnode/inode by device,inumber. |
5d5124a1 BJ |
73 | * If it is in core (in the inode structure), |
74 | * honor the locking protocol. | |
75 | * If it is not in core, read it in from the | |
76 | * specified device. | |
7188ac27 | 77 | * Callers must check for mount points!! |
5d5124a1 BJ |
78 | * In all cases, a pointer to a locked |
79 | * inode structure is returned. | |
5d5124a1 | 80 | */ |
7188ac27 KM |
81 | iget(xp, ino, ipp) |
82 | struct inode *xp; | |
7494ef16 | 83 | ino_t ino; |
7188ac27 | 84 | struct inode **ipp; |
5d5124a1 | 85 | { |
7188ac27 KM |
86 | dev_t dev = xp->i_dev; |
87 | struct mount *mntp = ITOV(xp)->v_mount; | |
88 | register struct fs *fs = VFSTOUFS(mntp)->um_fs; | |
1259a9f9 | 89 | extern struct vnodeops ufs_vnodeops, spec_inodeops; |
7188ac27 KM |
90 | register struct inode *ip, *iq; |
91 | register struct vnode *vp; | |
1259a9f9 | 92 | struct vnode *nvp; |
7188ac27 | 93 | struct buf *bp; |
1259a9f9 | 94 | struct dinode *dp; |
4b61628b KM |
95 | union ihead *ih; |
96 | int i, error; | |
2e64ab65 | 97 | |
3ebac878 | 98 | ih = &ihead[INOHASH(dev, ino)]; |
1259a9f9 | 99 | loop: |
a1e9dd57 KM |
100 | for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) { |
101 | if (ino != ip->i_number || dev != ip->i_dev) | |
102 | continue; | |
a1e9dd57 KM |
103 | if ((ip->i_flag&ILOCKED) != 0) { |
104 | ip->i_flag |= IWANT; | |
105 | sleep((caddr_t)ip, PINOD); | |
106 | goto loop; | |
7188ac27 | 107 | } |
1259a9f9 KM |
108 | if (vget(ITOV(ip))) |
109 | goto loop; | |
a1e9dd57 KM |
110 | *ipp = ip; |
111 | return(0); | |
112 | } | |
1259a9f9 KM |
113 | /* |
114 | * Allocate a new inode. | |
115 | */ | |
116 | if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) { | |
7188ac27 KM |
117 | *ipp = 0; |
118 | return (error); | |
119 | } | |
1259a9f9 KM |
120 | ip = VTOI(nvp); |
121 | ip->i_vnode = nvp; | |
122 | ip->i_flag = 0; | |
123 | ip->i_devvp = 0; | |
1259a9f9 | 124 | ip->i_mode = 0; |
c9ad8afc | 125 | ip->i_diroff = 0; |
1259a9f9 | 126 | #ifdef QUOTA |
4b61628b KM |
127 | for (i = 0; i < MAXQUOTAS; i++) |
128 | ip->i_dquot[i] = NODQUOT; | |
1259a9f9 KM |
129 | #endif |
130 | /* | |
131 | * Put it onto its hash chain and lock it so that other requests for | |
132 | * this inode will block if they arrive while we are sleeping waiting | |
133 | * for old data structures to be purged or for the contents of the | |
134 | * disk portion of this inode to be read. | |
135 | */ | |
136 | ip->i_dev = dev; | |
137 | ip->i_number = ino; | |
138 | insque(ip, ih); | |
139 | ILOCK(ip); | |
7188ac27 KM |
140 | /* |
141 | * Read in the disk contents for the inode. | |
142 | */ | |
143 | if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)), | |
a937f856 | 144 | (int)fs->fs_bsize, NOCRED, &bp)) { |
7188ac27 | 145 | /* |
a1e9dd57 | 146 | * Unlock and discard unneeded inode. |
7188ac27 | 147 | */ |
1259a9f9 | 148 | iput(ip); |
7188ac27 KM |
149 | brelse(bp); |
150 | *ipp = 0; | |
1259a9f9 | 151 | return (error); |
7188ac27 | 152 | } |
7188ac27 KM |
153 | dp = bp->b_un.b_dino; |
154 | dp += itoo(fs, ino); | |
1259a9f9 KM |
155 | ip->i_din = *dp; |
156 | brelse(bp); | |
157 | /* | |
158 | * Initialize the associated vnode | |
159 | */ | |
160 | vp = ITOV(ip); | |
161 | vp->v_type = IFTOVT(ip->i_mode); | |
4c7b0b09 KM |
162 | if (vp->v_type == VFIFO) { |
163 | #ifdef FIFO | |
164 | extern struct vnodeops fifo_inodeops; | |
165 | vp->v_op = &fifo_inodeops; | |
166 | #else | |
167 | iput(ip); | |
168 | *ipp = 0; | |
169 | return (EOPNOTSUPP); | |
170 | #endif /* FIFO */ | |
171 | } | |
1259a9f9 | 172 | if (vp->v_type == VCHR || vp->v_type == VBLK) { |
1259a9f9 | 173 | vp->v_op = &spec_inodeops; |
43fc727e | 174 | if (nvp = checkalias(vp, ip->i_rdev, mntp)) { |
7188ac27 | 175 | /* |
1259a9f9 | 176 | * Reinitialize aliased inode. |
7188ac27 | 177 | */ |
1259a9f9 KM |
178 | vp = nvp; |
179 | iq = VTOI(vp); | |
180 | iq->i_vnode = vp; | |
a97f9198 | 181 | iq->i_flag = 0; |
1259a9f9 KM |
182 | ILOCK(iq); |
183 | iq->i_din = ip->i_din; | |
184 | iq->i_dev = dev; | |
185 | iq->i_number = ino; | |
186 | insque(iq, ih); | |
7188ac27 | 187 | /* |
1259a9f9 | 188 | * Discard unneeded vnode |
7188ac27 | 189 | */ |
1259a9f9 KM |
190 | ip->i_mode = 0; |
191 | iput(ip); | |
7188ac27 | 192 | ip = iq; |
5d5124a1 | 193 | } |
7188ac27 | 194 | } |
1259a9f9 KM |
195 | if (ino == ROOTINO) |
196 | vp->v_flag |= VROOT; | |
7188ac27 KM |
197 | /* |
198 | * Finish inode initialization. | |
199 | */ | |
200 | ip->i_fs = fs; | |
201 | ip->i_devvp = VFSTOUFS(mntp)->um_devvp; | |
8fe1c702 | 202 | VREF(ip->i_devvp); |
afd7e202 KM |
203 | /* |
204 | * Set up a generation number for this inode if it does not | |
205 | * already have one. This should only happen on old filesystems. | |
206 | */ | |
207 | if (ip->i_gen == 0) { | |
208 | if (++nextgennumber < (u_long)time.tv_sec) | |
209 | nextgennumber = time.tv_sec; | |
210 | ip->i_gen = nextgennumber; | |
211 | if ((vp->v_mount->m_flag & M_RDONLY) == 0) | |
212 | ip->i_flag |= IMOD; | |
213 | } | |
7188ac27 KM |
214 | *ipp = ip; |
215 | return (0); | |
216 | } | |
3ebac878 | 217 | |
5d5124a1 | 218 | /* |
a1e9dd57 | 219 | * Unlock and decrement the reference count of an inode structure. |
5d5124a1 BJ |
220 | */ |
221 | iput(ip) | |
7494ef16 | 222 | register struct inode *ip; |
5d5124a1 | 223 | { |
ff56f48a | 224 | |
5c2ba954 | 225 | if ((ip->i_flag & ILOCKED) == 0) |
ff56f48a | 226 | panic("iput"); |
a388503d | 227 | IUNLOCK(ip); |
7188ac27 | 228 | vrele(ITOV(ip)); |
ff56f48a KM |
229 | } |
230 | ||
a1e9dd57 KM |
231 | /* |
232 | * Last reference to an inode, write the inode out and if necessary, | |
233 | * truncate and deallocate the file. | |
234 | */ | |
7188ac27 KM |
235 | ufs_inactive(vp) |
236 | struct vnode *vp; | |
ff56f48a | 237 | { |
7188ac27 | 238 | register struct inode *ip = VTOI(vp); |
a1e9dd57 | 239 | int mode, error = 0; |
7188ac27 | 240 | |
0811b508 | 241 | if (prtactive && vp->v_usecount != 0) |
e038406d | 242 | vprint("ufs_inactive: pushing active", vp); |
b5ea418e KM |
243 | /* |
244 | * Get rid of inodes related to stale file handles. | |
245 | */ | |
1259a9f9 | 246 | if (ip->i_mode == 0) { |
e038406d KM |
247 | if ((vp->v_flag & VXLOCK) == 0) |
248 | vgone(vp); | |
1259a9f9 KM |
249 | return (0); |
250 | } | |
aed86454 | 251 | ILOCK(ip); |
fd80ddd5 | 252 | if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) { |
4b61628b KM |
253 | #ifdef QUOTA |
254 | if (!getinoquota(ip)) | |
255 | (void) chkiq(ip, -1, NOCRED, 0); | |
256 | #endif | |
e038406d | 257 | error = itrunc(ip, (u_long)0, 0); |
7188ac27 KM |
258 | mode = ip->i_mode; |
259 | ip->i_mode = 0; | |
7188ac27 KM |
260 | ip->i_flag |= IUPD|ICHG; |
261 | ifree(ip, ip->i_number, mode); | |
7188ac27 KM |
262 | } |
263 | IUPDAT(ip, &time, &time, 0); | |
d11dbec7 KM |
264 | IUNLOCK(ip); |
265 | ip->i_flag = 0; | |
7188ac27 | 266 | /* |
a1e9dd57 KM |
267 | * If we are done with the inode, reclaim it |
268 | * so that it can be reused immediately. | |
7188ac27 | 269 | */ |
4b61628b | 270 | if (vp->v_usecount == 0 && ip->i_mode == 0) |
d11dbec7 | 271 | vgone(vp); |
7188ac27 | 272 | return (error); |
5d5124a1 BJ |
273 | } |
274 | ||
275 | /* | |
a1e9dd57 KM |
276 | * Reclaim an inode so that it can be used for other purposes. |
277 | */ | |
278 | ufs_reclaim(vp) | |
279 | register struct vnode *vp; | |
280 | { | |
ff4fb102 | 281 | register struct inode *ip = VTOI(vp); |
4b61628b | 282 | int i; |
a1e9dd57 | 283 | |
0811b508 | 284 | if (prtactive && vp->v_usecount != 0) |
e038406d | 285 | vprint("ufs_reclaim: pushing active", vp); |
a1e9dd57 KM |
286 | /* |
287 | * Remove the inode from its hash chain. | |
288 | */ | |
289 | remque(ip); | |
290 | ip->i_forw = ip; | |
291 | ip->i_back = ip; | |
292 | /* | |
293 | * Purge old data structures associated with the inode. | |
294 | */ | |
295 | cache_purge(vp); | |
296 | if (ip->i_devvp) { | |
297 | vrele(ip->i_devvp); | |
298 | ip->i_devvp = 0; | |
299 | } | |
300 | #ifdef QUOTA | |
4b61628b KM |
301 | for (i = 0; i < MAXQUOTAS; i++) { |
302 | if (ip->i_dquot[i] != NODQUOT) { | |
303 | dqrele(vp, ip->i_dquot[i]); | |
304 | ip->i_dquot[i] = NODQUOT; | |
305 | } | |
306 | } | |
a1e9dd57 | 307 | #endif |
a1e9dd57 KM |
308 | ip->i_flag = 0; |
309 | return (0); | |
310 | } | |
311 | ||
312 | /* | |
313 | * Check accessed and update flags on an inode structure. | |
314 | * If any is on, update the inode with the current time. | |
315 | * If waitfor is given, then must ensure I/O order, | |
316 | * so wait for write to complete. | |
5d5124a1 | 317 | */ |
c0bb1685 | 318 | iupdat(ip, ta, tm, waitfor) |
7494ef16 | 319 | register struct inode *ip; |
b32450f4 | 320 | struct timeval *ta, *tm; |
7494ef16 | 321 | int waitfor; |
5d5124a1 | 322 | { |
7188ac27 KM |
323 | struct buf *bp; |
324 | struct vnode *vp = ITOV(ip); | |
5d5124a1 | 325 | struct dinode *dp; |
ec67a3ce | 326 | register struct fs *fs; |
5d5124a1 | 327 | |
ec67a3ce | 328 | fs = ip->i_fs; |
7188ac27 KM |
329 | if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) |
330 | return (0); | |
331 | if (vp->v_mount->m_flag & M_RDONLY) | |
332 | return (0); | |
333 | error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), | |
a937f856 | 334 | (int)fs->fs_bsize, NOCRED, &bp); |
7188ac27 KM |
335 | if (error) { |
336 | brelse(bp); | |
337 | return (error); | |
338 | } | |
339 | if (ip->i_flag&IACC) | |
340 | ip->i_atime = ta->tv_sec; | |
341 | if (ip->i_flag&IUPD) | |
342 | ip->i_mtime = tm->tv_sec; | |
343 | if (ip->i_flag&ICHG) | |
344 | ip->i_ctime = time.tv_sec; | |
345 | ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); | |
346 | dp = bp->b_un.b_dino + itoo(fs, ip->i_number); | |
a1e9dd57 | 347 | *dp = ip->i_din; |
7188ac27 KM |
348 | if (waitfor) { |
349 | return (bwrite(bp)); | |
350 | } else { | |
351 | bdwrite(bp); | |
352 | return (0); | |
5d5124a1 BJ |
353 | } |
354 | } | |
355 | ||
9c03b2c0 SL |
356 | #define SINGLE 0 /* index of single indirect block */ |
357 | #define DOUBLE 1 /* index of double indirect block */ | |
358 | #define TRIPLE 2 /* index of triple indirect block */ | |
5d5124a1 | 359 | /* |
a1e9dd57 KM |
360 | * Truncate the inode ip to at most length size. Free affected disk |
361 | * blocks -- the blocks of the file are removed in reverse order. | |
9c03b2c0 SL |
362 | * |
363 | * NB: triple indirect blocks are untested. | |
5d5124a1 | 364 | */ |
e038406d | 365 | itrunc(oip, length, flags) |
28821bc5 | 366 | register struct inode *oip; |
4f083fd7 | 367 | u_long length; |
e038406d | 368 | int flags; |
5d5124a1 | 369 | { |
4f083fd7 | 370 | register daddr_t lastblock; |
a5e62f37 | 371 | daddr_t bn, lbn, lastiblock[NIADDR]; |
6459ebe0 | 372 | register struct fs *fs; |
9c03b2c0 | 373 | register struct inode *ip; |
28821bc5 | 374 | struct buf *bp; |
7188ac27 KM |
375 | int offset, osize, size, level; |
376 | long count, nblocks, blocksreleased = 0; | |
28821bc5 | 377 | register int i; |
e038406d | 378 | int aflags, error, allerror; |
9c03b2c0 | 379 | struct inode tip; |
4f083fd7 | 380 | |
7b2e4f05 SL |
381 | if (oip->i_size <= length) { |
382 | oip->i_flag |= ICHG|IUPD; | |
7188ac27 KM |
383 | error = iupdat(oip, &time, &time, 1); |
384 | return (error); | |
7b2e4f05 | 385 | } |
c0bb1685 | 386 | /* |
9c03b2c0 SL |
387 | * Calculate index into inode's block list of |
388 | * last direct and indirect blocks (if any) | |
389 | * which we want to keep. Lastblock is -1 when | |
390 | * the file is truncated to 0. | |
c0bb1685 | 391 | */ |
9c03b2c0 | 392 | fs = oip->i_fs; |
4f083fd7 | 393 | lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; |
9c03b2c0 SL |
394 | lastiblock[SINGLE] = lastblock - NDADDR; |
395 | lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); | |
396 | lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); | |
08d9a8ec | 397 | nblocks = btodb(fs->fs_bsize); |
6459ebe0 | 398 | /* |
28821bc5 KM |
399 | * Update the size of the file. If the file is not being |
400 | * truncated to a block boundry, the contents of the | |
401 | * partial block following the end of the file must be | |
402 | * zero'ed in case it ever become accessable again because | |
403 | * of subsequent file growth. | |
404 | */ | |
405 | osize = oip->i_size; | |
406 | offset = blkoff(fs, length); | |
407 | if (offset == 0) { | |
408 | oip->i_size = length; | |
409 | } else { | |
410 | lbn = lblkno(fs, length); | |
e038406d KM |
411 | aflags = B_CLRBUF; |
412 | if (flags & IO_SYNC) | |
413 | aflags |= B_SYNC; | |
4b61628b KM |
414 | #ifdef QUOTA |
415 | if (error = getinoquota(oip)) | |
416 | return (error); | |
417 | #endif | |
e038406d | 418 | if (error = balloc(oip, lbn, offset, &bp, aflags)) |
7188ac27 | 419 | return (error); |
28821bc5 KM |
420 | oip->i_size = length; |
421 | size = blksize(fs, oip, lbn); | |
e038406d | 422 | bn = bp->b_blkno; |
ec67a3ce | 423 | count = howmany(size, CLBYTES); |
7188ac27 | 424 | munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); |
a5e62f37 | 425 | bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); |
e038406d KM |
426 | brealloc(bp, size); |
427 | if (flags & IO_SYNC) | |
428 | bwrite(bp); | |
429 | else | |
430 | bdwrite(bp); | |
28821bc5 KM |
431 | } |
432 | /* | |
433 | * Update file and block pointers | |
9c03b2c0 SL |
434 | * on disk before we start freeing blocks. |
435 | * If we crash before free'ing blocks below, | |
436 | * the blocks will be returned to the free list. | |
437 | * lastiblock values are also normalized to -1 | |
438 | * for calls to indirtrunc below. | |
6459ebe0 | 439 | */ |
9c03b2c0 | 440 | tip = *oip; |
28821bc5 | 441 | tip.i_size = osize; |
9c03b2c0 SL |
442 | for (level = TRIPLE; level >= SINGLE; level--) |
443 | if (lastiblock[level] < 0) { | |
444 | oip->i_ib[level] = 0; | |
445 | lastiblock[level] = -1; | |
4f083fd7 | 446 | } |
9c03b2c0 SL |
447 | for (i = NDADDR - 1; i > lastblock; i--) |
448 | oip->i_db[i] = 0; | |
9c03b2c0 | 449 | oip->i_flag |= ICHG|IUPD; |
e038406d | 450 | vinvalbuf(ITOV(oip), (length > 0)); |
2fc1f182 | 451 | allerror = iupdat(oip, &time, &time, MNT_WAIT); |
9c03b2c0 | 452 | |
6459ebe0 | 453 | /* |
9c03b2c0 | 454 | * Indirect blocks first. |
6459ebe0 | 455 | */ |
28821bc5 | 456 | ip = &tip; |
9c03b2c0 SL |
457 | for (level = TRIPLE; level >= SINGLE; level--) { |
458 | bn = ip->i_ib[level]; | |
4f083fd7 | 459 | if (bn != 0) { |
7188ac27 KM |
460 | error = indirtrunc(ip, bn, lastiblock[level], level, |
461 | &count); | |
462 | if (error) | |
463 | allerror = error; | |
464 | blocksreleased += count; | |
9c03b2c0 SL |
465 | if (lastiblock[level] < 0) { |
466 | ip->i_ib[level] = 0; | |
ced3a252 | 467 | blkfree(ip, bn, (off_t)fs->fs_bsize); |
9c03b2c0 | 468 | blocksreleased += nblocks; |
9c03b2c0 SL |
469 | } |
470 | } | |
471 | if (lastiblock[level] >= 0) | |
472 | goto done; | |
4f083fd7 | 473 | } |
9c03b2c0 | 474 | |
6459ebe0 | 475 | /* |
9c03b2c0 | 476 | * All whole direct blocks or frags. |
6459ebe0 | 477 | */ |
4f083fd7 | 478 | for (i = NDADDR - 1; i > lastblock; i--) { |
8011f5df | 479 | register off_t bsize; |
4f083fd7 | 480 | |
6459ebe0 | 481 | bn = ip->i_db[i]; |
4f083fd7 | 482 | if (bn == 0) |
5d5124a1 | 483 | continue; |
4f083fd7 | 484 | ip->i_db[i] = 0; |
0b355a6e | 485 | bsize = (off_t)blksize(fs, ip, i); |
ced3a252 | 486 | blkfree(ip, bn, bsize); |
0b355a6e | 487 | blocksreleased += btodb(bsize); |
4f083fd7 | 488 | } |
9c03b2c0 SL |
489 | if (lastblock < 0) |
490 | goto done; | |
491 | ||
4f083fd7 SL |
492 | /* |
493 | * Finally, look for a change in size of the | |
494 | * last direct block; release any frags. | |
495 | */ | |
9c03b2c0 SL |
496 | bn = ip->i_db[lastblock]; |
497 | if (bn != 0) { | |
8011f5df | 498 | off_t oldspace, newspace; |
9c03b2c0 | 499 | |
4f083fd7 SL |
500 | /* |
501 | * Calculate amount of space we're giving | |
502 | * back as old block size minus new block size. | |
503 | */ | |
9c03b2c0 | 504 | oldspace = blksize(fs, ip, lastblock); |
4f083fd7 | 505 | ip->i_size = length; |
9c03b2c0 SL |
506 | newspace = blksize(fs, ip, lastblock); |
507 | if (newspace == 0) | |
508 | panic("itrunc: newspace"); | |
509 | if (oldspace - newspace > 0) { | |
4f083fd7 SL |
510 | /* |
511 | * Block number of space to be free'd is | |
512 | * the old block # plus the number of frags | |
513 | * required for the storage we're keeping. | |
514 | */ | |
9c03b2c0 | 515 | bn += numfrags(fs, newspace); |
ced3a252 | 516 | blkfree(ip, bn, oldspace - newspace); |
08d9a8ec | 517 | blocksreleased += btodb(oldspace - newspace); |
4f083fd7 | 518 | } |
5d5124a1 | 519 | } |
4f083fd7 | 520 | done: |
9c03b2c0 SL |
521 | /* BEGIN PARANOIA */ |
522 | for (level = SINGLE; level <= TRIPLE; level++) | |
523 | if (ip->i_ib[level] != oip->i_ib[level]) | |
524 | panic("itrunc1"); | |
525 | for (i = 0; i < NDADDR; i++) | |
526 | if (ip->i_db[i] != oip->i_db[i]) | |
527 | panic("itrunc2"); | |
528 | /* END PARANOIA */ | |
08d9a8ec SL |
529 | oip->i_blocks -= blocksreleased; |
530 | if (oip->i_blocks < 0) /* sanity */ | |
531 | oip->i_blocks = 0; | |
532 | oip->i_flag |= ICHG; | |
b4567e9c | 533 | #ifdef QUOTA |
4b61628b KM |
534 | if (!getinoquota(oip)) |
535 | (void) chkdq(oip, -blocksreleased, NOCRED, 0); | |
89045c38 | 536 | #endif |
7188ac27 | 537 | return (allerror); |
5d5124a1 BJ |
538 | } |
539 | ||
4f083fd7 SL |
540 | /* |
541 | * Release blocks associated with the inode ip and | |
542 | * stored in the indirect block bn. Blocks are free'd | |
543 | * in LIFO order up to (but not including) lastbn. If | |
9c03b2c0 SL |
544 | * level is greater than SINGLE, the block is an indirect |
545 | * block and recursive calls to indirtrunc must be used to | |
546 | * cleanse other indirect blocks. | |
547 | * | |
548 | * NB: triple indirect blocks are untested. | |
4f083fd7 | 549 | */ |
7188ac27 | 550 | indirtrunc(ip, bn, lastbn, level, countp) |
6459ebe0 | 551 | register struct inode *ip; |
4f083fd7 | 552 | daddr_t bn, lastbn; |
9c03b2c0 | 553 | int level; |
7188ac27 | 554 | long *countp; |
5d5124a1 | 555 | { |
4f083fd7 | 556 | register int i; |
b30358ab | 557 | struct buf *bp; |
9c03b2c0 | 558 | register struct fs *fs = ip->i_fs; |
b30358ab KM |
559 | register daddr_t *bap; |
560 | daddr_t *copy, nb, last; | |
7188ac27 KM |
561 | long blkcount, factor; |
562 | int nblocks, blocksreleased = 0; | |
563 | int error, allerror = 0; | |
5d5124a1 | 564 | |
9c03b2c0 SL |
565 | /* |
566 | * Calculate index in current block of last | |
567 | * block to be kept. -1 indicates the entire | |
568 | * block so we need not calculate the index. | |
569 | */ | |
570 | factor = 1; | |
571 | for (i = SINGLE; i < level; i++) | |
572 | factor *= NINDIR(fs); | |
4f083fd7 | 573 | last = lastbn; |
9c03b2c0 SL |
574 | if (lastbn > 0) |
575 | last /= factor; | |
08d9a8ec | 576 | nblocks = btodb(fs->fs_bsize); |
9c03b2c0 SL |
577 | /* |
578 | * Get buffer of block pointers, zero those | |
579 | * entries corresponding to blocks to be free'd, | |
580 | * and update on disk copy first. | |
581 | */ | |
ec67a3ce MK |
582 | #ifdef SECSIZE |
583 | bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize, | |
584 | fs->fs_dbsize); | |
585 | #else SECSIZE | |
a937f856 KM |
586 | error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, |
587 | NOCRED, &bp); | |
7188ac27 | 588 | if (error) { |
9c03b2c0 | 589 | brelse(bp); |
7188ac27 KM |
590 | *countp = 0; |
591 | return (error); | |
9c03b2c0 SL |
592 | } |
593 | bap = bp->b_un.b_daddr; | |
b30358ab KM |
594 | MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); |
595 | bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); | |
9c03b2c0 SL |
596 | bzero((caddr_t)&bap[last + 1], |
597 | (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); | |
e038406d KM |
598 | if (last == -1) |
599 | bp->b_flags |= B_INVAL; | |
7188ac27 KM |
600 | error = bwrite(bp); |
601 | if (error) | |
602 | allerror = error; | |
b30358ab | 603 | bap = copy; |
4f083fd7 | 604 | |
9c03b2c0 SL |
605 | /* |
606 | * Recursively free totally unused blocks. | |
607 | */ | |
608 | for (i = NINDIR(fs) - 1; i > last; i--) { | |
5d5124a1 | 609 | nb = bap[i]; |
4f083fd7 | 610 | if (nb == 0) |
5d5124a1 | 611 | continue; |
7188ac27 KM |
612 | if (level > SINGLE) { |
613 | error = indirtrunc(ip, nb, (daddr_t)-1, level - 1, | |
614 | &blkcount); | |
615 | if (error) | |
616 | allerror = error; | |
617 | blocksreleased += blkcount; | |
618 | } | |
ced3a252 | 619 | blkfree(ip, nb, (off_t)fs->fs_bsize); |
4f083fd7 | 620 | blocksreleased += nblocks; |
4f083fd7 | 621 | } |
9c03b2c0 SL |
622 | |
623 | /* | |
624 | * Recursively free last partial block. | |
625 | */ | |
626 | if (level > SINGLE && lastbn >= 0) { | |
627 | last = lastbn % factor; | |
4f083fd7 | 628 | nb = bap[i]; |
7188ac27 KM |
629 | if (nb != 0) { |
630 | error = indirtrunc(ip, nb, last, level - 1, &blkcount); | |
631 | if (error) | |
632 | allerror = error; | |
633 | blocksreleased += blkcount; | |
634 | } | |
5d5124a1 | 635 | } |
b30358ab | 636 | FREE(copy, M_TEMP); |
7188ac27 KM |
637 | *countp = blocksreleased; |
638 | return (allerror); | |
5d5124a1 BJ |
639 | } |
640 | ||
d6a210b8 | 641 | /* |
7494ef16 | 642 | * Lock an inode. If its already locked, set the WANT bit and sleep. |
d6a210b8 | 643 | */ |
7494ef16 BJ |
644 | ilock(ip) |
645 | register struct inode *ip; | |
d6a210b8 BJ |
646 | { |
647 | ||
7188ac27 KM |
648 | while (ip->i_flag & ILOCKED) { |
649 | ip->i_flag |= IWANT; | |
71c4cb8d KM |
650 | if (ip->i_spare0 == u.u_procp->p_pid) |
651 | panic("locking against myself"); | |
652 | ip->i_spare1 = u.u_procp->p_pid; | |
7188ac27 KM |
653 | (void) sleep((caddr_t)ip, PINOD); |
654 | } | |
71c4cb8d KM |
655 | ip->i_spare1 = 0; |
656 | ip->i_spare0 = u.u_procp->p_pid; | |
657 | u.u_spare[0]++; | |
7188ac27 | 658 | ip->i_flag |= ILOCKED; |
d6a210b8 BJ |
659 | } |
660 | ||
661 | /* | |
7494ef16 | 662 | * Unlock an inode. If WANT bit is on, wakeup. |
d6a210b8 | 663 | */ |
ff56f48a | 664 | iunlock(ip) |
7494ef16 | 665 | register struct inode *ip; |
d6a210b8 BJ |
666 | { |
667 | ||
7188ac27 | 668 | if ((ip->i_flag & ILOCKED) == 0) |
e038406d | 669 | vprint("iunlock: unlocked inode", ITOV(ip)); |
71c4cb8d KM |
670 | ip->i_spare0 = 0; |
671 | u.u_spare[0]--; | |
7188ac27 KM |
672 | ip->i_flag &= ~ILOCKED; |
673 | if (ip->i_flag&IWANT) { | |
674 | ip->i_flag &= ~IWANT; | |
675 | wakeup((caddr_t)ip); | |
676 | } | |
677 | } |