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