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