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