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