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