]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_inode.c
3b7b729851e990ff0c3e85ab03d6b016f4b9194a
[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.h>
64 #include <sys/proc.h>
65 #include <sys/file.h>
66 #include <sys/buf.h>
67 #include <sys/vnode.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
74 #include <sys/vm.h>
75
76 #include <ufs/ufs/quota.h>
77 #include <ufs/ufs/inode.h>
78 #include <ufs/ufs/ufsmount.h>
79 #include <ufs/ufs/ufs_extern.h>
80
81 #include <ufs/ffs/fs.h>
82 #include <ufs/ffs/ffs_extern.h>
83
84 #if REV_ENDIAN_FS
85 #include <ufs/ufs/ufs_byte_order.h>
86 #include <architecture/byte_order.h>
87 #endif /* REV_ENDIAN_FS */
88
89 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
90 ufs_daddr_t, int, long *));
91
92 /*
93 * Update the access, modified, and inode change times as specified by the
94 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
95 * used to specify that the inode needs to be updated but that the times have
96 * already been set. The access and modified times are taken from the second
97 * and third parameters; the inode change time is always taken from the current
98 * time. If waitfor is set, then wait for the disk write of the inode to
99 * complete.
100 */
101 int
102 ffs_update(ap)
103 struct vop_update_args /* {
104 struct vnode *a_vp;
105 struct timeval *a_access;
106 struct timeval *a_modify;
107 int a_waitfor;
108 } */ *ap;
109 {
110 register struct fs *fs;
111 struct buf *bp;
112 struct inode *ip;
113 int error;
114 #if REV_ENDIAN_FS
115 struct mount *mp=(ap->a_vp)->v_mount;
116 int rev_endian=(mp->mnt_flag & MNT_REVEND);
117 #endif /* REV_ENDIAN_FS */
118
119 ip = VTOI(ap->a_vp);
120 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) {
121 ip->i_flag &=
122 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
123 return (0);
124 }
125 if ((ip->i_flag &
126 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
127 return (0);
128 if (ip->i_flag & IN_ACCESS)
129 ip->i_atime = ap->a_access->tv_sec;
130 if (ip->i_flag & IN_UPDATE) {
131 ip->i_mtime = ap->a_modify->tv_sec;
132 ip->i_modrev++;
133 }
134 if (ip->i_flag & IN_CHANGE)
135 ip->i_ctime = time.tv_sec;
136 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
137 fs = ip->i_fs;
138 /*
139 * Ensure that uid and gid are correct. This is a temporary
140 * fix until fsck has been changed to do the update.
141 */
142 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
143 ip->i_din.di_ouid = ip->i_uid; /* XXX */
144 ip->i_din.di_ogid = ip->i_gid; /* XXX */
145 } /* XXX */
146 if (error = bread(ip->i_devvp,
147 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
148 (int)fs->fs_bsize, NOCRED, &bp)) {
149 brelse(bp);
150 return (error);
151 }
152 #if REV_ENDIAN_FS
153 if (rev_endian)
154 byte_swap_inode_out(ip, ((struct dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)));
155 else {
156 #endif /* REV_ENDIAN_FS */
157 *((struct dinode *)bp->b_data +
158 ino_to_fsbo(fs, ip->i_number)) = ip->i_din;
159 #if REV_ENDIAN_FS
160 }
161 #endif /* REV_ENDIAN_FS */
162
163 if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
164 return (bwrite(bp));
165 else {
166 bdwrite(bp);
167 return (0);
168 }
169 }
170
171 #define SINGLE 0 /* index of single indirect block */
172 #define DOUBLE 1 /* index of double indirect block */
173 #define TRIPLE 2 /* index of triple indirect block */
174 /*
175 * Truncate the inode oip to at most length size, freeing the
176 * disk blocks.
177 */
178 ffs_truncate(ap)
179 struct vop_truncate_args /* {
180 struct vnode *a_vp;
181 off_t a_length;
182 int a_flags;
183 struct ucred *a_cred;
184 struct proc *a_p;
185 } */ *ap;
186 {
187 register struct vnode *ovp = ap->a_vp;
188 ufs_daddr_t lastblock;
189 register struct inode *oip;
190 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
191 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
192 off_t length = ap->a_length;
193 register struct fs *fs;
194 struct buf *bp;
195 int offset, size, level;
196 long count, nblocks, vflags, blocksreleased = 0;
197 struct timeval tv;
198 register int i;
199 int aflags, error, allerror;
200 off_t osize;
201 int devBlockSize=0;
202
203 if (length < 0)
204 return (EINVAL);
205
206 oip = VTOI(ovp);
207 fs = oip->i_fs;
208
209 if (length > fs->fs_maxfilesize)
210 return (EFBIG);
211
212 tv = time;
213 if (ovp->v_type == VLNK &&
214 oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
215 #if DIAGNOSTIC
216 if (length != 0)
217 panic("ffs_truncate: partial truncate of symlink");
218 #endif
219 bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
220 oip->i_size = 0;
221 oip->i_flag |= IN_CHANGE | IN_UPDATE;
222 return (VOP_UPDATE(ovp, &tv, &tv, 1));
223 }
224
225 if (oip->i_size == length) {
226 oip->i_flag |= IN_CHANGE | IN_UPDATE;
227 return (VOP_UPDATE(ovp, &tv, &tv, 0));
228 }
229 #if QUOTA
230 if (error = getinoquota(oip))
231 return (error);
232 #endif
233 osize = oip->i_size;
234
235 /*
236 * Lengthen the size of the file. We must ensure that the
237 * last byte of the file is allocated. Since the smallest
238 * value of osize is 0, length will be at least 1.
239 */
240 if (osize < length) {
241 offset = blkoff(fs, length - 1);
242 lbn = lblkno(fs, length - 1);
243 aflags = B_CLRBUF;
244 if (ap->a_flags & IO_SYNC)
245 aflags |= B_SYNC;
246 if (error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
247 aflags , 0))
248 return (error);
249 oip->i_size = length;
250
251 if (UBCINFOEXISTS(ovp)) {
252 bp->b_flags |= B_INVAL;
253 bwrite(bp);
254 ubc_setsize(ovp, (off_t)length);
255 } else {
256 if (aflags & B_SYNC)
257 bwrite(bp);
258 else
259 bawrite(bp);
260 }
261 oip->i_flag |= IN_CHANGE | IN_UPDATE;
262 return (VOP_UPDATE(ovp, &tv, &tv, 1));
263 }
264 /*
265 * Shorten the size of the file. If the file is not being
266 * truncated to a block boundry, the contents of the
267 * partial block following the end of the file must be
268 * zero'ed in case it ever become accessable again because
269 * of subsequent file growth.
270 */
271 if (UBCINFOEXISTS(ovp))
272 ubc_setsize(ovp, (off_t)length);
273
274 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
275 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
276
277
278 offset = blkoff(fs, length);
279 if (offset == 0) {
280 oip->i_size = length;
281 } else {
282 lbn = lblkno(fs, length);
283 aflags = B_CLRBUF;
284 if (ap->a_flags & IO_SYNC)
285 aflags |= B_SYNC;
286 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp,
287 aflags, 0))
288 return (error);
289 oip->i_size = length;
290 size = blksize(fs, oip, lbn);
291 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
292 allocbuf(bp, size);
293 if (UBCINFOEXISTS(ovp)) {
294 bp->b_flags |= B_INVAL;
295 bwrite(bp);
296 } else {
297 if (aflags & B_SYNC)
298 bwrite(bp);
299 else
300 bawrite(bp);
301 }
302 }
303 /*
304 * Calculate index into inode's block list of
305 * last direct and indirect blocks (if any)
306 * which we want to keep. Lastblock is -1 when
307 * the file is truncated to 0.
308 */
309 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
310 lastiblock[SINGLE] = lastblock - NDADDR;
311 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
312 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
313 VOP_DEVBLOCKSIZE(oip->i_devvp,&devBlockSize);
314 nblocks = btodb(fs->fs_bsize, devBlockSize);
315
316 /*
317 * Update file and block pointers on disk before we start freeing
318 * blocks. If we crash before free'ing blocks below, the blocks
319 * will be returned to the free list. lastiblock values are also
320 * normalized to -1 for calls to ffs_indirtrunc below.
321 */
322 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
323 for (level = TRIPLE; level >= SINGLE; level--)
324 if (lastiblock[level] < 0) {
325 oip->i_ib[level] = 0;
326 lastiblock[level] = -1;
327 }
328 for (i = NDADDR - 1; i > lastblock; i--)
329 oip->i_db[i] = 0;
330 oip->i_flag |= IN_CHANGE | IN_UPDATE;
331 if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
332 allerror = error;
333 /*
334 * Having written the new inode to disk, save its new configuration
335 * and put back the old block pointers long enough to process them.
336 * Note that we save the new block configuration so we can check it
337 * when we are done.
338 */
339 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
340 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
341 oip->i_size = osize;
342 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
343 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
344
345 /*
346 * Indirect blocks first.
347 */
348 indir_lbn[SINGLE] = -NDADDR;
349 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
350 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
351 for (level = TRIPLE; level >= SINGLE; level--) {
352 bn = oip->i_ib[level];
353 if (bn != 0) {
354 error = ffs_indirtrunc(oip, indir_lbn[level],
355 fsbtodb(fs, bn), lastiblock[level], level, &count);
356 if (error)
357 allerror = error;
358 blocksreleased += count;
359 if (lastiblock[level] < 0) {
360 oip->i_ib[level] = 0;
361 ffs_blkfree(oip, bn, fs->fs_bsize);
362 blocksreleased += nblocks;
363 }
364 }
365 if (lastiblock[level] >= 0)
366 goto done;
367 }
368
369 /*
370 * All whole direct blocks or frags.
371 */
372 for (i = NDADDR - 1; i > lastblock; i--) {
373 register long bsize;
374
375 bn = oip->i_db[i];
376 if (bn == 0)
377 continue;
378 oip->i_db[i] = 0;
379 bsize = blksize(fs, oip, i);
380 ffs_blkfree(oip, bn, bsize);
381 blocksreleased += btodb(bsize, devBlockSize);
382 }
383 if (lastblock < 0)
384 goto done;
385
386 /*
387 * Finally, look for a change in size of the
388 * last direct block; release any frags.
389 */
390 bn = oip->i_db[lastblock];
391 if (bn != 0) {
392 long oldspace, newspace;
393
394 /*
395 * Calculate amount of space we're giving
396 * back as old block size minus new block size.
397 */
398 oldspace = blksize(fs, oip, lastblock);
399 oip->i_size = length;
400 newspace = blksize(fs, oip, lastblock);
401 if (newspace == 0)
402 panic("itrunc: newspace");
403 if (oldspace - newspace > 0) {
404 /*
405 * Block number of space to be free'd is
406 * the old block # plus the number of frags
407 * required for the storage we're keeping.
408 */
409 bn += numfrags(fs, newspace);
410 ffs_blkfree(oip, bn, oldspace - newspace);
411 blocksreleased += btodb(oldspace - newspace, devBlockSize);
412 }
413 }
414 done:
415 #if DIAGNOSTIC
416 for (level = SINGLE; level <= TRIPLE; level++)
417 if (newblks[NDADDR + level] != oip->i_ib[level])
418 panic("itrunc1");
419 for (i = 0; i < NDADDR; i++)
420 if (newblks[i] != oip->i_db[i])
421 panic("itrunc2");
422 if (length == 0 &&
423 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
424 panic("itrunc3");
425 #endif /* DIAGNOSTIC */
426 /*
427 * Put back the real size.
428 */
429 oip->i_size = length;
430 oip->i_blocks -= blocksreleased;
431 if (oip->i_blocks < 0) /* sanity */
432 oip->i_blocks = 0;
433 oip->i_flag |= IN_CHANGE;
434 #if QUOTA
435 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
436 #endif
437 return (allerror);
438 }
439
440 /*
441 * Release blocks associated with the inode ip and stored in the indirect
442 * block bn. Blocks are free'd in LIFO order up to (but not including)
443 * lastbn. If level is greater than SINGLE, the block is an indirect block
444 * and recursive calls to indirtrunc must be used to cleanse other indirect
445 * blocks.
446 *
447 * NB: triple indirect blocks are untested.
448 */
449 static int
450 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
451 register struct inode *ip;
452 ufs_daddr_t lbn, lastbn;
453 ufs_daddr_t dbn;
454 int level;
455 long *countp;
456 {
457 register int i;
458 struct buf *bp;
459 struct buf *tbp;
460 register struct fs *fs = ip->i_fs;
461 register ufs_daddr_t *bap;
462 struct vnode *vp=ITOV(ip);
463 ufs_daddr_t *copy, nb, nlbn, last;
464 long blkcount, factor;
465 int nblocks, blocksreleased = 0;
466 int error = 0, allerror = 0;
467 int devBlockSize=0;
468 #if REV_ENDIAN_FS
469 struct mount *mp=vp->v_mount;
470 int rev_endian=(mp->mnt_flag & MNT_REVEND);
471 #endif /* REV_ENDIAN_FS */
472
473 /*
474 * Calculate index in current block of last
475 * block to be kept. -1 indicates the entire
476 * block so we need not calculate the index.
477 */
478 factor = 1;
479 for (i = SINGLE; i < level; i++)
480 factor *= NINDIR(fs);
481 last = lastbn;
482 if (lastbn > 0)
483 last /= factor;
484 VOP_DEVBLOCKSIZE(ip->i_devvp,&devBlockSize);
485 nblocks = btodb(fs->fs_bsize, devBlockSize);
486
487 /* Doing a MALLOC here is asking for trouble. We can still
488 * deadlock on pagerfile lock, in case we are running
489 * low on memory and block in MALLOC
490 */
491
492 tbp = geteblk(fs->fs_bsize);
493 copy = (ufs_daddr_t *)tbp->b_data;
494
495 /*
496 * Get buffer of block pointers, zero those entries corresponding
497 * to blocks to be free'd, and update on disk copy first. Since
498 * double(triple) indirect before single(double) indirect, calls
499 * to bmap on these blocks will fail. However, we already have
500 * the on disk address, so we have to set the b_blkno field
501 * explicitly instead of letting bread do everything for us.
502 */
503
504 vp = ITOV(ip);
505 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0, BLK_META);
506 if (bp->b_flags & (B_DONE | B_DELWRI)) {
507 /* Braces must be here in case trace evaluates to nothing. */
508 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
509 } else {
510 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
511 current_proc()->p_stats->p_ru.ru_inblock++; /* pay for read */
512 bp->b_flags |= B_READ;
513 if (bp->b_bcount > bp->b_bufsize)
514 panic("ffs_indirtrunc: bad buffer size");
515 bp->b_blkno = dbn;
516 VOP_STRATEGY(bp);
517 error = biowait(bp);
518 }
519 if (error) {
520 brelse(bp);
521 *countp = 0;
522 brelse(tbp);
523 return (error);
524 }
525
526 bap = (ufs_daddr_t *)bp->b_data;
527 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
528 bzero((caddr_t)&bap[last + 1],
529 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
530 if (last == -1)
531 bp->b_flags |= B_INVAL;
532 error = bwrite(bp);
533 if (error)
534 allerror = error;
535 bap = copy;
536
537 /*
538 * Recursively free totally unused blocks.
539 */
540 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
541 i--, nlbn += factor) {
542 #if REV_ENDIAN_FS
543 if (rev_endian)
544 nb = NXSwapLong(bap[i]);
545 else {
546 #endif /* REV_ENDIAN_FS */
547 nb = bap[i];
548 #if REV_ENDIAN_FS
549 }
550 #endif /* REV_ENDIAN_FS */
551 if (nb == 0)
552 continue;
553 if (level > SINGLE) {
554 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
555 (ufs_daddr_t)-1, level - 1, &blkcount))
556 allerror = error;
557 blocksreleased += blkcount;
558 }
559 ffs_blkfree(ip, nb, fs->fs_bsize);
560 blocksreleased += nblocks;
561 }
562
563 /*
564 * Recursively free last partial block.
565 */
566 if (level > SINGLE && lastbn >= 0) {
567 last = lastbn % factor;
568 #if REV_ENDIAN_FS
569 if (rev_endian)
570 nb = NXSwapLong(bap[i]);
571 else {
572 #endif /* REV_ENDIAN_FS */
573 nb = bap[i];
574 #if REV_ENDIAN_FS
575 }
576 #endif /* REV_ENDIAN_FS */
577 if (nb != 0) {
578 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
579 last, level - 1, &blkcount))
580 allerror = error;
581 blocksreleased += blkcount;
582 }
583 }
584 brelse(tbp);
585 *countp = blocksreleased;
586 return (allerror);
587 }
588