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