]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_inode.c
3973a6281452404ed1855842fb10388066f38a61
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
66 #include <rev_endian_fs.h>
67 #include <vm/vm_pager.h>
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/mount_internal.h>
72 #include <sys/proc_internal.h> /* for accessing p_stats */
74 #include <sys/buf_internal.h>
75 #include <sys/vnode_internal.h>
76 #include <sys/kernel.h>
77 #include <sys/malloc.h>
78 #include <sys/trace.h>
79 #include <sys/resourcevar.h>
81 #include <sys/quota.h>
85 #include <ufs/ufs/quota.h>
86 #include <ufs/ufs/inode.h>
87 #include <ufs/ufs/ufsmount.h>
88 #include <ufs/ufs/ufs_extern.h>
90 #include <ufs/ffs/fs.h>
91 #include <ufs/ffs/ffs_extern.h>
94 #include <ufs/ufs/ufs_byte_order.h>
95 #include <architecture/byte_order.h>
96 #endif /* REV_ENDIAN_FS */
98 static int ffs_indirtrunc(struct inode
*, ufs_daddr_t
, ufs_daddr_t
,
99 ufs_daddr_t
, int, long *);
102 * Update the access, modified, and inode change times as specified by the
103 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
104 * used to specify that the inode needs to be updated but that the times have
105 * already been set. The access and modified times are taken from the second
106 * and third parameters; the inode change time is always taken from the current
107 * time. If waitfor is set, then wait for the disk write of the inode to
111 ffs_update(struct vnode
*vp
, struct timeval
*access
, struct timeval
*modify
, int waitfor
)
113 register struct fs
*fs
;
119 struct mount
*mp
=(vp
)->v_mount
;
120 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
121 #endif /* REV_ENDIAN_FS */
124 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
126 ~(IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
);
130 (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 0)
132 if (ip
->i_flag
& IN_ACCESS
)
133 ip
->i_atime
= access
->tv_sec
;
134 if (ip
->i_flag
& IN_UPDATE
) {
135 ip
->i_mtime
= modify
->tv_sec
;
138 if (ip
->i_flag
& IN_CHANGE
) {
140 ip
->i_ctime
= tv
.tv_sec
;
142 ip
->i_flag
&= ~(IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
);
145 * Ensure that uid and gid are correct. This is a temporary
146 * fix until fsck has been changed to do the update.
148 if (fs
->fs_inodefmt
< FS_44INODEFMT
) { /* XXX */
149 ip
->i_din
.di_ouid
= ip
->i_uid
; /* XXX */
150 ip
->i_din
.di_ogid
= ip
->i_gid
; /* XXX */
152 if (error
= buf_bread(ip
->i_devvp
,
153 (daddr64_t
)((unsigned)fsbtodb(fs
, ino_to_fsba(fs
, ip
->i_number
))),
154 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
160 byte_swap_inode_out(ip
, ((struct dinode
*)buf_dataptr(bp
) + ino_to_fsbo(fs
, ip
->i_number
)));
162 #endif /* REV_ENDIAN_FS */
163 *((struct dinode
*)buf_dataptr(bp
) + ino_to_fsbo(fs
, ip
->i_number
)) = ip
->i_din
;
166 #endif /* REV_ENDIAN_FS */
168 if (waitfor
&& (vp
->v_mount
->mnt_flag
& MNT_ASYNC
) == 0)
169 return ((int)buf_bwrite(bp
));
177 #define SINGLE 0 /* index of single indirect block */
178 #define DOUBLE 1 /* index of double indirect block */
179 #define TRIPLE 2 /* index of triple indirect block */
182 ffs_truncate_internal(vnode_t ovp
, off_t length
, int flags
, ucred_t cred
)
186 ufs_daddr_t lastblock
;
187 ufs_daddr_t bn
, lbn
, lastiblock
[NIADDR
], indir_lbn
[NIADDR
];
188 ufs_daddr_t oldblks
[NDADDR
+ NIADDR
], newblks
[NDADDR
+ NIADDR
];
190 int offset
, size
, level
, i
;
191 long count
, nblocks
, vflags
, blocksreleased
= 0;
193 int aflags
, error
, allerror
;
197 int64_t change
; /* in bytes */
206 if (length
> fs
->fs_maxfilesize
)
210 if (ovp
->v_type
== VLNK
&&
211 oip
->i_size
< ovp
->v_mount
->mnt_maxsymlinklen
) {
214 panic("ffs_truncate: partial truncate of symlink");
216 bzero((char *)&oip
->i_shortlink
, (u_int
)oip
->i_size
);
218 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
219 return (ffs_update(ovp
, &tv
, &tv
, 1));
222 if (oip
->i_size
== length
) {
223 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
224 return (ffs_update(ovp
, &tv
, &tv
, 0));
227 if (error
= getinoquota(oip
))
233 * Lengthen the size of the file. We must ensure that the
234 * last byte of the file is allocated. Since the smallest
235 * value of osize is 0, length will be at least 1.
237 if (osize
< length
) {
238 offset
= blkoff(fs
, length
- 1);
239 lbn
= lblkno(fs
, length
- 1);
243 if (error
= ffs_balloc(oip
, lbn
, offset
+ 1, cred
, &bp
, aflags
, 0))
245 oip
->i_size
= length
;
247 if (UBCINFOEXISTS(ovp
)) {
250 ubc_setsize(ovp
, (off_t
)length
);
257 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
258 return (ffs_update(ovp
, &tv
, &tv
, 1));
261 * Shorten the size of the file. If the file is not being
262 * truncated to a block boundry, the contents of the
263 * partial block following the end of the file must be
264 * zero'ed in case it ever become accessable again because
265 * of subsequent file growth.
267 if (UBCINFOEXISTS(ovp
))
268 ubc_setsize(ovp
, (off_t
)length
);
270 vflags
= ((length
> 0) ? BUF_WRITE_DATA
: 0) | BUF_SKIP_META
;
272 if (vflags
& BUF_WRITE_DATA
)
273 ffs_fsync_internal(ovp
, MNT_WAIT
);
274 allerror
= buf_invalidateblks(ovp
, vflags
, 0, 0);
276 offset
= blkoff(fs
, length
);
278 oip
->i_size
= length
;
280 lbn
= lblkno(fs
, length
);
284 if (error
= ffs_balloc(oip
, lbn
, offset
, cred
, &bp
, aflags
, 0))
286 oip
->i_size
= length
;
287 size
= blksize(fs
, oip
, lbn
);
288 bzero((char *)buf_dataptr(bp
) + offset
, (u_int
)(size
- offset
));
290 if (UBCINFOEXISTS(ovp
)) {
301 * Calculate index into inode's block list of
302 * last direct and indirect blocks (if any)
303 * which we want to keep. Lastblock is -1 when
304 * the file is truncated to 0.
306 lastblock
= lblkno(fs
, length
+ fs
->fs_bsize
- 1) - 1;
307 lastiblock
[SINGLE
] = lastblock
- NDADDR
;
308 lastiblock
[DOUBLE
] = lastiblock
[SINGLE
] - NINDIR(fs
);
309 lastiblock
[TRIPLE
] = lastiblock
[DOUBLE
] - NINDIR(fs
) * NINDIR(fs
);
311 devBlockSize
= vfs_devblocksize(vnode_mount(ovp
));
312 nblocks
= btodb(fs
->fs_bsize
, devBlockSize
);
315 * Update file and block pointers on disk before we start freeing
316 * blocks. If we crash before free'ing blocks below, the blocks
317 * will be returned to the free list. lastiblock values are also
318 * normalized to -1 for calls to ffs_indirtrunc below.
320 bcopy((caddr_t
)&oip
->i_db
[0], (caddr_t
)oldblks
, sizeof oldblks
);
321 for (level
= TRIPLE
; level
>= SINGLE
; level
--)
322 if (lastiblock
[level
] < 0) {
323 oip
->i_ib
[level
] = 0;
324 lastiblock
[level
] = -1;
326 for (i
= NDADDR
- 1; i
> lastblock
; i
--)
328 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
329 if (error
= ffs_update(ovp
, &tv
, &tv
, MNT_WAIT
))
332 * Having written the new inode to disk, save its new configuration
333 * and put back the old block pointers long enough to process them.
334 * Note that we save the new block configuration so we can check it
337 bcopy((caddr_t
)&oip
->i_db
[0], (caddr_t
)newblks
, sizeof newblks
);
338 bcopy((caddr_t
)oldblks
, (caddr_t
)&oip
->i_db
[0], sizeof oldblks
);
341 vflags
= ((length
> 0) ? BUF_WRITE_DATA
: 0) | BUF_SKIP_META
;
343 if (vflags
& BUF_WRITE_DATA
)
344 ffs_fsync_internal(ovp
, MNT_WAIT
);
345 allerror
= buf_invalidateblks(ovp
, vflags
, 0, 0);
348 * Indirect blocks first.
350 indir_lbn
[SINGLE
] = -NDADDR
;
351 indir_lbn
[DOUBLE
] = indir_lbn
[SINGLE
] - NINDIR(fs
) - 1;
352 indir_lbn
[TRIPLE
] = indir_lbn
[DOUBLE
] - NINDIR(fs
) * NINDIR(fs
) - 1;
353 for (level
= TRIPLE
; level
>= SINGLE
; level
--) {
354 bn
= oip
->i_ib
[level
];
356 error
= ffs_indirtrunc(oip
, indir_lbn
[level
],
357 fsbtodb(fs
, bn
), lastiblock
[level
], level
, &count
);
360 blocksreleased
+= count
;
361 if (lastiblock
[level
] < 0) {
362 oip
->i_ib
[level
] = 0;
363 ffs_blkfree(oip
, bn
, fs
->fs_bsize
);
364 blocksreleased
+= nblocks
;
367 if (lastiblock
[level
] >= 0)
372 * All whole direct blocks or frags.
374 for (i
= NDADDR
- 1; i
> lastblock
; i
--) {
381 bsize
= blksize(fs
, oip
, i
);
382 ffs_blkfree(oip
, bn
, bsize
);
383 blocksreleased
+= btodb(bsize
, devBlockSize
);
389 * Finally, look for a change in size of the
390 * last direct block; release any frags.
392 bn
= oip
->i_db
[lastblock
];
394 long oldspace
, newspace
;
397 * Calculate amount of space we're giving
398 * back as old block size minus new block size.
400 oldspace
= blksize(fs
, oip
, lastblock
);
401 oip
->i_size
= length
;
402 newspace
= blksize(fs
, oip
, lastblock
);
404 panic("itrunc: newspace");
405 if (oldspace
- newspace
> 0) {
407 * Block number of space to be free'd is
408 * the old block # plus the number of frags
409 * required for the storage we're keeping.
411 bn
+= numfrags(fs
, newspace
);
412 ffs_blkfree(oip
, bn
, oldspace
- newspace
);
413 blocksreleased
+= btodb(oldspace
- newspace
, devBlockSize
);
418 for (level
= SINGLE
; level
<= TRIPLE
; level
++)
419 if (newblks
[NDADDR
+ level
] != oip
->i_ib
[level
])
421 for (i
= 0; i
< NDADDR
; i
++)
422 if (newblks
[i
] != oip
->i_db
[i
])
425 (vnode_hasdirtyblks(ovp
) || vnode_hascleanblks(ovp
)))
427 #endif /* DIAGNOSTIC */
429 * Put back the real size.
431 oip
->i_size
= length
;
432 oip
->i_blocks
-= blocksreleased
;
433 if (oip
->i_blocks
< 0) /* sanity */
435 oip
->i_flag
|= IN_CHANGE
;
437 change
= dbtob((int64_t)blocksreleased
,devBlockSize
);
438 (void) chkdq(oip
, -change
, NOCRED
, 0);
444 * Release blocks associated with the inode ip and stored in the indirect
445 * block bn. Blocks are free'd in LIFO order up to (but not including)
446 * lastbn. If level is greater than SINGLE, the block is an indirect block
447 * and recursive calls to indirtrunc must be used to cleanse other indirect
450 * NB: triple indirect blocks are untested.
453 ffs_indirtrunc(ip
, lbn
, dbn
, lastbn
, level
, countp
)
454 register struct inode
*ip
;
455 ufs_daddr_t lbn
, lastbn
;
463 register struct fs
*fs
= ip
->i_fs
;
464 register ufs_daddr_t
*bap
;
465 struct vnode
*vp
=ITOV(ip
);
466 ufs_daddr_t
*copy
, nb
, nlbn
, last
;
467 long blkcount
, factor
;
468 int nblocks
, blocksreleased
= 0;
469 errno_t error
= 0, allerror
= 0;
471 struct mount
*mp
=vp
->v_mount
;
473 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
474 #endif /* REV_ENDIAN_FS */
477 * Calculate index in current block of last
478 * block to be kept. -1 indicates the entire
479 * block so we need not calculate the index.
482 for (i
= SINGLE
; i
< level
; i
++)
483 factor
*= NINDIR(fs
);
488 devBlockSize
= vfs_devblocksize(mp
);
489 nblocks
= btodb(fs
->fs_bsize
, devBlockSize
);
491 /* Doing a MALLOC here is asking for trouble. We can still
492 * deadlock on pagerfile lock, in case we are running
493 * low on memory and block in MALLOC
496 tbp
= buf_geteblk(fs
->fs_bsize
);
497 copy
= (ufs_daddr_t
*)buf_dataptr(tbp
);
500 * Get buffer of block pointers, zero those entries corresponding
501 * to blocks to be free'd, and update on disk copy first. Since
502 * double(triple) indirect before single(double) indirect, calls
503 * to bmap on these blocks will fail. However, we already have
504 * the on disk address, so we have to set the blkno field
505 * explicitly instead of letting buf_bread do everything for us.
509 bp
= buf_getblk(vp
, (daddr64_t
)((unsigned)lbn
), (int)fs
->fs_bsize
, 0, 0, BLK_META
);
512 /* Braces must be here in case trace evaluates to nothing. */
513 trace(TR_BREADHIT
, pack(vp
, fs
->fs_bsize
), lbn
);
515 trace(TR_BREADMISS
, pack(vp
, fs
->fs_bsize
), lbn
);
516 current_proc()->p_stats
->p_ru
.ru_inblock
++; /* pay for read */
517 buf_setflags(bp
, B_READ
);
518 if (buf_count(bp
) > buf_size(bp
))
519 panic("ffs_indirtrunc: bad buffer size");
520 buf_setblkno(bp
, (daddr64_t
)((unsigned)dbn
));
522 error
= buf_biowait(bp
);
531 bap
= (ufs_daddr_t
*)buf_dataptr(bp
);
532 bcopy((caddr_t
)bap
, (caddr_t
)copy
, (u_int
)fs
->fs_bsize
);
533 bzero((caddr_t
)&bap
[last
+ 1],
534 (u_int
)(NINDIR(fs
) - (last
+ 1)) * sizeof (ufs_daddr_t
));
537 if (last
!= -1 && (vp
)->v_mount
->mnt_flag
& MNT_ASYNC
) {
541 error
= buf_bwrite(bp
);
548 * Recursively free totally unused blocks.
550 for (i
= NINDIR(fs
) - 1, nlbn
= lbn
+ 1 - i
* factor
; i
> last
;
551 i
--, nlbn
+= factor
) {
554 nb
= NXSwapLong(bap
[i
]);
556 #endif /* REV_ENDIAN_FS */
560 #endif /* REV_ENDIAN_FS */
563 if (level
> SINGLE
) {
564 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
565 (ufs_daddr_t
)-1, level
- 1, &blkcount
))
567 blocksreleased
+= blkcount
;
569 ffs_blkfree(ip
, nb
, fs
->fs_bsize
);
570 blocksreleased
+= nblocks
;
574 * Recursively free last partial block.
576 if (level
> SINGLE
&& lastbn
>= 0) {
577 last
= lastbn
% factor
;
580 nb
= NXSwapLong(bap
[i
]);
582 #endif /* REV_ENDIAN_FS */
586 #endif /* REV_ENDIAN_FS */
588 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
589 last
, level
- 1, &blkcount
))
591 blocksreleased
+= blkcount
;
595 *countp
= blocksreleased
;
596 return ((int)allerror
);