]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ff6e181a A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
1c79356b | 12 | * |
ff6e181a A |
13 | * The Original Code and all software distributed under the License are |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ff6e181a A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
1c79356b A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
24 | /* | |
25 | * Copyright (c) 1982, 1986, 1989, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
36 | * 3. All advertising materials mentioning features or use of this software | |
37 | * must display the following acknowledgement: | |
38 | * This product includes software developed by the University of | |
39 | * California, Berkeley and its contributors. | |
40 | * 4. Neither the name of the University nor the names of its contributors | |
41 | * may be used to endorse or promote products derived from this software | |
42 | * without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | * | |
56 | * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95 | |
57 | */ | |
58 | ||
59 | #include <rev_endian_fs.h> | |
60 | #include <vm/vm_pager.h> | |
61 | ||
62 | #include <sys/param.h> | |
63 | #include <sys/systm.h> | |
91447636 A |
64 | #include <sys/mount_internal.h> |
65 | #include <sys/proc_internal.h> /* for accessing p_stats */ | |
1c79356b | 66 | #include <sys/file.h> |
91447636 A |
67 | #include <sys/buf_internal.h> |
68 | #include <sys/vnode_internal.h> | |
1c79356b A |
69 | #include <sys/kernel.h> |
70 | #include <sys/malloc.h> | |
71 | #include <sys/trace.h> | |
72 | #include <sys/resourcevar.h> | |
73 | #include <sys/ubc.h> | |
9bccf70c | 74 | #include <sys/quota.h> |
1c79356b A |
75 | |
76 | #include <sys/vm.h> | |
77 | ||
78 | #include <ufs/ufs/quota.h> | |
79 | #include <ufs/ufs/inode.h> | |
80 | #include <ufs/ufs/ufsmount.h> | |
81 | #include <ufs/ufs/ufs_extern.h> | |
82 | ||
83 | #include <ufs/ffs/fs.h> | |
84 | #include <ufs/ffs/ffs_extern.h> | |
85 | ||
86 | #if REV_ENDIAN_FS | |
87 | #include <ufs/ufs/ufs_byte_order.h> | |
88 | #include <architecture/byte_order.h> | |
89 | #endif /* REV_ENDIAN_FS */ | |
90 | ||
91447636 A |
91 | static int ffs_indirtrunc(struct inode *, ufs_daddr_t, ufs_daddr_t, |
92 | ufs_daddr_t, int, long *); | |
1c79356b A |
93 | |
94 | /* | |
95 | * Update the access, modified, and inode change times as specified by the | |
96 | * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is | |
97 | * used to specify that the inode needs to be updated but that the times have | |
98 | * already been set. The access and modified times are taken from the second | |
99 | * and third parameters; the inode change time is always taken from the current | |
100 | * time. If waitfor is set, then wait for the disk write of the inode to | |
101 | * complete. | |
102 | */ | |
103 | int | |
91447636 | 104 | ffs_update(struct vnode *vp, struct timeval *access, struct timeval *modify, int waitfor) |
1c79356b A |
105 | { |
106 | register struct fs *fs; | |
107 | struct buf *bp; | |
108 | struct inode *ip; | |
91447636 A |
109 | struct timeval tv; |
110 | errno_t error; | |
1c79356b | 111 | #if REV_ENDIAN_FS |
91447636 | 112 | struct mount *mp=(vp)->v_mount; |
1c79356b A |
113 | int rev_endian=(mp->mnt_flag & MNT_REVEND); |
114 | #endif /* REV_ENDIAN_FS */ | |
115 | ||
91447636 A |
116 | ip = VTOI(vp); |
117 | if (vp->v_mount->mnt_flag & MNT_RDONLY) { | |
1c79356b A |
118 | ip->i_flag &= |
119 | ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE); | |
120 | return (0); | |
121 | } | |
122 | if ((ip->i_flag & | |
123 | (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) | |
124 | return (0); | |
125 | if (ip->i_flag & IN_ACCESS) | |
91447636 | 126 | ip->i_atime = access->tv_sec; |
1c79356b | 127 | if (ip->i_flag & IN_UPDATE) { |
91447636 | 128 | ip->i_mtime = modify->tv_sec; |
1c79356b A |
129 | ip->i_modrev++; |
130 | } | |
91447636 A |
131 | if (ip->i_flag & IN_CHANGE) { |
132 | microtime(&tv); | |
133 | ip->i_ctime = tv.tv_sec; | |
134 | } | |
1c79356b A |
135 | ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE); |
136 | fs = ip->i_fs; | |
137 | /* | |
138 | * Ensure that uid and gid are correct. This is a temporary | |
139 | * fix until fsck has been changed to do the update. | |
140 | */ | |
141 | if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ | |
142 | ip->i_din.di_ouid = ip->i_uid; /* XXX */ | |
143 | ip->i_din.di_ogid = ip->i_gid; /* XXX */ | |
144 | } /* XXX */ | |
91447636 A |
145 | if (error = buf_bread(ip->i_devvp, |
146 | (daddr64_t)((unsigned)fsbtodb(fs, ino_to_fsba(fs, ip->i_number))), | |
1c79356b | 147 | (int)fs->fs_bsize, NOCRED, &bp)) { |
91447636 A |
148 | buf_brelse(bp); |
149 | return ((int)error); | |
1c79356b A |
150 | } |
151 | #if REV_ENDIAN_FS | |
152 | if (rev_endian) | |
91447636 | 153 | byte_swap_inode_out(ip, ((struct dinode *)buf_dataptr(bp) + ino_to_fsbo(fs, ip->i_number))); |
1c79356b A |
154 | else { |
155 | #endif /* REV_ENDIAN_FS */ | |
91447636 | 156 | *((struct dinode *)buf_dataptr(bp) + ino_to_fsbo(fs, ip->i_number)) = ip->i_din; |
1c79356b A |
157 | #if REV_ENDIAN_FS |
158 | } | |
159 | #endif /* REV_ENDIAN_FS */ | |
160 | ||
91447636 A |
161 | if (waitfor && (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) |
162 | return ((int)buf_bwrite(bp)); | |
1c79356b | 163 | else { |
91447636 | 164 | buf_bdwrite(bp); |
1c79356b A |
165 | return (0); |
166 | } | |
167 | } | |
168 | ||
91447636 | 169 | |
1c79356b A |
170 | #define SINGLE 0 /* index of single indirect block */ |
171 | #define DOUBLE 1 /* index of double indirect block */ | |
172 | #define TRIPLE 2 /* index of triple indirect block */ | |
91447636 A |
173 | |
174 | int | |
175 | ffs_truncate_internal(vnode_t ovp, off_t length, int flags, ucred_t cred) | |
1c79356b | 176 | { |
91447636 A |
177 | struct inode *oip; |
178 | struct fs *fs; | |
1c79356b | 179 | ufs_daddr_t lastblock; |
1c79356b A |
180 | ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR]; |
181 | ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR]; | |
91447636 A |
182 | buf_t bp; |
183 | int offset, size, level, i; | |
184 | long count, nblocks, vflags, blocksreleased = 0; | |
185 | struct timeval tv; | |
186 | int aflags, error, allerror; | |
187 | off_t osize; | |
188 | int devBlockSize=0; | |
9bccf70c A |
189 | #if QUOTA |
190 | int64_t change; /* in bytes */ | |
191 | #endif /* QUOTA */ | |
1c79356b A |
192 | |
193 | if (length < 0) | |
194 | return (EINVAL); | |
195 | ||
196 | oip = VTOI(ovp); | |
197 | fs = oip->i_fs; | |
198 | ||
199 | if (length > fs->fs_maxfilesize) | |
200 | return (EFBIG); | |
201 | ||
91447636 | 202 | microtime(&tv); |
1c79356b A |
203 | if (ovp->v_type == VLNK && |
204 | oip->i_size < ovp->v_mount->mnt_maxsymlinklen) { | |
205 | #if DIAGNOSTIC | |
206 | if (length != 0) | |
207 | panic("ffs_truncate: partial truncate of symlink"); | |
208 | #endif | |
209 | bzero((char *)&oip->i_shortlink, (u_int)oip->i_size); | |
210 | oip->i_size = 0; | |
211 | oip->i_flag |= IN_CHANGE | IN_UPDATE; | |
91447636 | 212 | return (ffs_update(ovp, &tv, &tv, 1)); |
1c79356b A |
213 | } |
214 | ||
215 | if (oip->i_size == length) { | |
216 | oip->i_flag |= IN_CHANGE | IN_UPDATE; | |
91447636 | 217 | return (ffs_update(ovp, &tv, &tv, 0)); |
1c79356b A |
218 | } |
219 | #if QUOTA | |
220 | if (error = getinoquota(oip)) | |
221 | return (error); | |
222 | #endif | |
223 | osize = oip->i_size; | |
224 | ||
225 | /* | |
226 | * Lengthen the size of the file. We must ensure that the | |
227 | * last byte of the file is allocated. Since the smallest | |
228 | * value of osize is 0, length will be at least 1. | |
229 | */ | |
230 | if (osize < length) { | |
231 | offset = blkoff(fs, length - 1); | |
232 | lbn = lblkno(fs, length - 1); | |
233 | aflags = B_CLRBUF; | |
91447636 | 234 | if (flags & IO_SYNC) |
1c79356b | 235 | aflags |= B_SYNC; |
91447636 | 236 | if (error = ffs_balloc(oip, lbn, offset + 1, cred, &bp, aflags, 0)) |
1c79356b A |
237 | return (error); |
238 | oip->i_size = length; | |
239 | ||
240 | if (UBCINFOEXISTS(ovp)) { | |
91447636 A |
241 | buf_markinvalid(bp); |
242 | buf_bwrite(bp); | |
1c79356b A |
243 | ubc_setsize(ovp, (off_t)length); |
244 | } else { | |
245 | if (aflags & B_SYNC) | |
91447636 | 246 | buf_bwrite(bp); |
1c79356b | 247 | else |
91447636 | 248 | buf_bawrite(bp); |
1c79356b A |
249 | } |
250 | oip->i_flag |= IN_CHANGE | IN_UPDATE; | |
91447636 | 251 | return (ffs_update(ovp, &tv, &tv, 1)); |
1c79356b A |
252 | } |
253 | /* | |
254 | * Shorten the size of the file. If the file is not being | |
255 | * truncated to a block boundry, the contents of the | |
256 | * partial block following the end of the file must be | |
257 | * zero'ed in case it ever become accessable again because | |
258 | * of subsequent file growth. | |
259 | */ | |
260 | if (UBCINFOEXISTS(ovp)) | |
261 | ubc_setsize(ovp, (off_t)length); | |
262 | ||
91447636 | 263 | vflags = ((length > 0) ? BUF_WRITE_DATA : 0) | BUF_SKIP_META; |
1c79356b | 264 | |
91447636 A |
265 | if (vflags & BUF_WRITE_DATA) |
266 | ffs_fsync_internal(ovp, MNT_WAIT); | |
267 | allerror = buf_invalidateblks(ovp, vflags, 0, 0); | |
268 | ||
1c79356b A |
269 | offset = blkoff(fs, length); |
270 | if (offset == 0) { | |
271 | oip->i_size = length; | |
272 | } else { | |
273 | lbn = lblkno(fs, length); | |
274 | aflags = B_CLRBUF; | |
91447636 | 275 | if (flags & IO_SYNC) |
1c79356b | 276 | aflags |= B_SYNC; |
91447636 | 277 | if (error = ffs_balloc(oip, lbn, offset, cred, &bp, aflags, 0)) |
1c79356b A |
278 | return (error); |
279 | oip->i_size = length; | |
280 | size = blksize(fs, oip, lbn); | |
91447636 | 281 | bzero((char *)buf_dataptr(bp) + offset, (u_int)(size - offset)); |
1c79356b A |
282 | allocbuf(bp, size); |
283 | if (UBCINFOEXISTS(ovp)) { | |
91447636 A |
284 | buf_markinvalid(bp); |
285 | buf_bwrite(bp); | |
1c79356b A |
286 | } else { |
287 | if (aflags & B_SYNC) | |
91447636 | 288 | buf_bwrite(bp); |
1c79356b | 289 | else |
91447636 | 290 | buf_bawrite(bp); |
1c79356b A |
291 | } |
292 | } | |
293 | /* | |
294 | * Calculate index into inode's block list of | |
295 | * last direct and indirect blocks (if any) | |
296 | * which we want to keep. Lastblock is -1 when | |
297 | * the file is truncated to 0. | |
298 | */ | |
299 | lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; | |
300 | lastiblock[SINGLE] = lastblock - NDADDR; | |
301 | lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); | |
302 | lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); | |
91447636 A |
303 | |
304 | devBlockSize = vfs_devblocksize(vnode_mount(ovp)); | |
1c79356b A |
305 | nblocks = btodb(fs->fs_bsize, devBlockSize); |
306 | ||
307 | /* | |
308 | * Update file and block pointers on disk before we start freeing | |
309 | * blocks. If we crash before free'ing blocks below, the blocks | |
310 | * will be returned to the free list. lastiblock values are also | |
311 | * normalized to -1 for calls to ffs_indirtrunc below. | |
312 | */ | |
313 | bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks); | |
314 | for (level = TRIPLE; level >= SINGLE; level--) | |
315 | if (lastiblock[level] < 0) { | |
316 | oip->i_ib[level] = 0; | |
317 | lastiblock[level] = -1; | |
318 | } | |
319 | for (i = NDADDR - 1; i > lastblock; i--) | |
320 | oip->i_db[i] = 0; | |
321 | oip->i_flag |= IN_CHANGE | IN_UPDATE; | |
91447636 | 322 | if (error = ffs_update(ovp, &tv, &tv, MNT_WAIT)) |
1c79356b A |
323 | allerror = error; |
324 | /* | |
325 | * Having written the new inode to disk, save its new configuration | |
326 | * and put back the old block pointers long enough to process them. | |
327 | * Note that we save the new block configuration so we can check it | |
328 | * when we are done. | |
329 | */ | |
330 | bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks); | |
331 | bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks); | |
332 | oip->i_size = osize; | |
91447636 A |
333 | |
334 | vflags = ((length > 0) ? BUF_WRITE_DATA : 0) | BUF_SKIP_META; | |
335 | ||
336 | if (vflags & BUF_WRITE_DATA) | |
337 | ffs_fsync_internal(ovp, MNT_WAIT); | |
338 | allerror = buf_invalidateblks(ovp, vflags, 0, 0); | |
1c79356b A |
339 | |
340 | /* | |
341 | * Indirect blocks first. | |
342 | */ | |
343 | indir_lbn[SINGLE] = -NDADDR; | |
344 | indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1; | |
345 | indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1; | |
346 | for (level = TRIPLE; level >= SINGLE; level--) { | |
347 | bn = oip->i_ib[level]; | |
348 | if (bn != 0) { | |
349 | error = ffs_indirtrunc(oip, indir_lbn[level], | |
350 | fsbtodb(fs, bn), lastiblock[level], level, &count); | |
351 | if (error) | |
352 | allerror = error; | |
353 | blocksreleased += count; | |
354 | if (lastiblock[level] < 0) { | |
355 | oip->i_ib[level] = 0; | |
356 | ffs_blkfree(oip, bn, fs->fs_bsize); | |
357 | blocksreleased += nblocks; | |
358 | } | |
359 | } | |
360 | if (lastiblock[level] >= 0) | |
361 | goto done; | |
362 | } | |
363 | ||
364 | /* | |
365 | * All whole direct blocks or frags. | |
366 | */ | |
367 | for (i = NDADDR - 1; i > lastblock; i--) { | |
368 | register long bsize; | |
369 | ||
370 | bn = oip->i_db[i]; | |
371 | if (bn == 0) | |
372 | continue; | |
373 | oip->i_db[i] = 0; | |
374 | bsize = blksize(fs, oip, i); | |
375 | ffs_blkfree(oip, bn, bsize); | |
376 | blocksreleased += btodb(bsize, devBlockSize); | |
377 | } | |
378 | if (lastblock < 0) | |
379 | goto done; | |
380 | ||
381 | /* | |
382 | * Finally, look for a change in size of the | |
383 | * last direct block; release any frags. | |
384 | */ | |
385 | bn = oip->i_db[lastblock]; | |
386 | if (bn != 0) { | |
387 | long oldspace, newspace; | |
388 | ||
389 | /* | |
390 | * Calculate amount of space we're giving | |
391 | * back as old block size minus new block size. | |
392 | */ | |
393 | oldspace = blksize(fs, oip, lastblock); | |
394 | oip->i_size = length; | |
395 | newspace = blksize(fs, oip, lastblock); | |
396 | if (newspace == 0) | |
397 | panic("itrunc: newspace"); | |
398 | if (oldspace - newspace > 0) { | |
399 | /* | |
400 | * Block number of space to be free'd is | |
401 | * the old block # plus the number of frags | |
402 | * required for the storage we're keeping. | |
403 | */ | |
404 | bn += numfrags(fs, newspace); | |
405 | ffs_blkfree(oip, bn, oldspace - newspace); | |
406 | blocksreleased += btodb(oldspace - newspace, devBlockSize); | |
407 | } | |
408 | } | |
409 | done: | |
410 | #if DIAGNOSTIC | |
411 | for (level = SINGLE; level <= TRIPLE; level++) | |
412 | if (newblks[NDADDR + level] != oip->i_ib[level]) | |
413 | panic("itrunc1"); | |
414 | for (i = 0; i < NDADDR; i++) | |
415 | if (newblks[i] != oip->i_db[i]) | |
416 | panic("itrunc2"); | |
417 | if (length == 0 && | |
91447636 | 418 | (vnode_hasdirtyblks(ovp) || vnode_hascleanblks(ovp))) |
1c79356b A |
419 | panic("itrunc3"); |
420 | #endif /* DIAGNOSTIC */ | |
421 | /* | |
422 | * Put back the real size. | |
423 | */ | |
424 | oip->i_size = length; | |
425 | oip->i_blocks -= blocksreleased; | |
426 | if (oip->i_blocks < 0) /* sanity */ | |
427 | oip->i_blocks = 0; | |
428 | oip->i_flag |= IN_CHANGE; | |
429 | #if QUOTA | |
9bccf70c A |
430 | change = dbtob((int64_t)blocksreleased,devBlockSize); |
431 | (void) chkdq(oip, -change, NOCRED, 0); | |
1c79356b A |
432 | #endif |
433 | return (allerror); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Release blocks associated with the inode ip and stored in the indirect | |
438 | * block bn. Blocks are free'd in LIFO order up to (but not including) | |
439 | * lastbn. If level is greater than SINGLE, the block is an indirect block | |
440 | * and recursive calls to indirtrunc must be used to cleanse other indirect | |
441 | * blocks. | |
442 | * | |
443 | * NB: triple indirect blocks are untested. | |
444 | */ | |
445 | static int | |
446 | ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp) | |
447 | register struct inode *ip; | |
448 | ufs_daddr_t lbn, lastbn; | |
449 | ufs_daddr_t dbn; | |
450 | int level; | |
451 | long *countp; | |
452 | { | |
453 | register int i; | |
454 | struct buf *bp; | |
455 | struct buf *tbp; | |
456 | register struct fs *fs = ip->i_fs; | |
457 | register ufs_daddr_t *bap; | |
458 | struct vnode *vp=ITOV(ip); | |
459 | ufs_daddr_t *copy, nb, nlbn, last; | |
460 | long blkcount, factor; | |
461 | int nblocks, blocksreleased = 0; | |
91447636 | 462 | errno_t error = 0, allerror = 0; |
1c79356b | 463 | int devBlockSize=0; |
1c79356b | 464 | struct mount *mp=vp->v_mount; |
91447636 | 465 | #if REV_ENDIAN_FS |
1c79356b A |
466 | int rev_endian=(mp->mnt_flag & MNT_REVEND); |
467 | #endif /* REV_ENDIAN_FS */ | |
468 | ||
469 | /* | |
470 | * Calculate index in current block of last | |
471 | * block to be kept. -1 indicates the entire | |
472 | * block so we need not calculate the index. | |
473 | */ | |
474 | factor = 1; | |
475 | for (i = SINGLE; i < level; i++) | |
476 | factor *= NINDIR(fs); | |
477 | last = lastbn; | |
478 | if (lastbn > 0) | |
479 | last /= factor; | |
91447636 A |
480 | |
481 | devBlockSize = vfs_devblocksize(mp); | |
1c79356b A |
482 | nblocks = btodb(fs->fs_bsize, devBlockSize); |
483 | ||
484 | /* Doing a MALLOC here is asking for trouble. We can still | |
485 | * deadlock on pagerfile lock, in case we are running | |
486 | * low on memory and block in MALLOC | |
487 | */ | |
488 | ||
91447636 A |
489 | tbp = buf_geteblk(fs->fs_bsize); |
490 | copy = (ufs_daddr_t *)buf_dataptr(tbp); | |
1c79356b A |
491 | |
492 | /* | |
493 | * Get buffer of block pointers, zero those entries corresponding | |
494 | * to blocks to be free'd, and update on disk copy first. Since | |
495 | * double(triple) indirect before single(double) indirect, calls | |
496 | * to bmap on these blocks will fail. However, we already have | |
91447636 A |
497 | * the on disk address, so we have to set the blkno field |
498 | * explicitly instead of letting buf_bread do everything for us. | |
1c79356b A |
499 | */ |
500 | ||
501 | vp = ITOV(ip); | |
91447636 A |
502 | bp = buf_getblk(vp, (daddr64_t)((unsigned)lbn), (int)fs->fs_bsize, 0, 0, BLK_META); |
503 | ||
504 | if (buf_valid(bp)) { | |
1c79356b A |
505 | /* Braces must be here in case trace evaluates to nothing. */ |
506 | trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn); | |
507 | } else { | |
508 | trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn); | |
509 | current_proc()->p_stats->p_ru.ru_inblock++; /* pay for read */ | |
91447636 A |
510 | buf_setflags(bp, B_READ); |
511 | if (buf_count(bp) > buf_size(bp)) | |
1c79356b | 512 | panic("ffs_indirtrunc: bad buffer size"); |
91447636 A |
513 | buf_setblkno(bp, (daddr64_t)((unsigned)dbn)); |
514 | VNOP_STRATEGY(bp); | |
515 | error = buf_biowait(bp); | |
1c79356b A |
516 | } |
517 | if (error) { | |
91447636 | 518 | buf_brelse(bp); |
1c79356b | 519 | *countp = 0; |
91447636 A |
520 | buf_brelse(tbp); |
521 | return ((int)error); | |
1c79356b A |
522 | } |
523 | ||
91447636 | 524 | bap = (ufs_daddr_t *)buf_dataptr(bp); |
1c79356b A |
525 | bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); |
526 | bzero((caddr_t)&bap[last + 1], | |
527 | (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t)); | |
528 | if (last == -1) | |
91447636 | 529 | buf_markinvalid(bp); |
55e303ae A |
530 | if (last != -1 && (vp)->v_mount->mnt_flag & MNT_ASYNC) { |
531 | error = 0; | |
91447636 | 532 | buf_bdwrite(bp); |
55e303ae | 533 | } else { |
91447636 | 534 | error = buf_bwrite(bp); |
55e303ae A |
535 | if (error) |
536 | allerror = error; | |
537 | } | |
1c79356b A |
538 | bap = copy; |
539 | ||
540 | /* | |
541 | * Recursively free totally unused blocks. | |
542 | */ | |
543 | for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last; | |
544 | i--, nlbn += factor) { | |
545 | #if REV_ENDIAN_FS | |
546 | if (rev_endian) | |
547 | nb = NXSwapLong(bap[i]); | |
548 | else { | |
549 | #endif /* REV_ENDIAN_FS */ | |
550 | nb = bap[i]; | |
551 | #if REV_ENDIAN_FS | |
552 | } | |
553 | #endif /* REV_ENDIAN_FS */ | |
554 | if (nb == 0) | |
555 | continue; | |
556 | if (level > SINGLE) { | |
557 | if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), | |
558 | (ufs_daddr_t)-1, level - 1, &blkcount)) | |
559 | allerror = error; | |
560 | blocksreleased += blkcount; | |
561 | } | |
562 | ffs_blkfree(ip, nb, fs->fs_bsize); | |
563 | blocksreleased += nblocks; | |
564 | } | |
565 | ||
566 | /* | |
567 | * Recursively free last partial block. | |
568 | */ | |
569 | if (level > SINGLE && lastbn >= 0) { | |
570 | last = lastbn % factor; | |
571 | #if REV_ENDIAN_FS | |
572 | if (rev_endian) | |
573 | nb = NXSwapLong(bap[i]); | |
574 | else { | |
575 | #endif /* REV_ENDIAN_FS */ | |
576 | nb = bap[i]; | |
577 | #if REV_ENDIAN_FS | |
578 | } | |
579 | #endif /* REV_ENDIAN_FS */ | |
580 | if (nb != 0) { | |
581 | if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb), | |
582 | last, level - 1, &blkcount)) | |
583 | allerror = error; | |
584 | blocksreleased += blkcount; | |
585 | } | |
586 | } | |
91447636 | 587 | buf_brelse(tbp); |
1c79356b | 588 | *countp = blocksreleased; |
91447636 | 589 | return ((int)allerror); |
1c79356b A |
590 | } |
591 |