]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_inode.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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
55 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
58 #include <rev_endian_fs.h>
59 #include <vm/vm_pager.h>
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 */
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>
73 #include <sys/quota.h>
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>
82 #include <ufs/ffs/fs.h>
83 #include <ufs/ffs/ffs_extern.h>
86 #include <ufs/ufs/ufs_byte_order.h>
87 #include <libkern/OSByteOrder.h>
88 #endif /* REV_ENDIAN_FS */
90 static int ffs_indirtrunc(struct inode
*, ufs_daddr_t
, ufs_daddr_t
,
91 ufs_daddr_t
, int, long *);
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
103 ffs_update(struct vnode
*vp
, struct timeval
*access
, struct timeval
*modify
, int waitfor
)
105 register struct fs
*fs
;
111 struct mount
*mp
=(vp
)->v_mount
;
112 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
113 #endif /* REV_ENDIAN_FS */
116 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
118 ~(IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
);
122 (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 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
;
130 if (ip
->i_flag
& IN_CHANGE
) {
132 ip
->i_ctime
= tv
.tv_sec
;
134 ip
->i_flag
&= ~(IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
);
137 * Ensure that uid and gid are correct. This is a temporary
138 * fix until fsck has been changed to do the update.
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 */
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
)) {
152 byte_swap_inode_out(ip
, ((struct dinode
*)buf_dataptr(bp
) + ino_to_fsbo(fs
, ip
->i_number
)));
154 #endif /* REV_ENDIAN_FS */
155 *((struct dinode
*)buf_dataptr(bp
) + ino_to_fsbo(fs
, ip
->i_number
)) = ip
->i_din
;
158 #endif /* REV_ENDIAN_FS */
160 if (waitfor
&& (vp
->v_mount
->mnt_flag
& MNT_ASYNC
) == 0)
161 return ((int)buf_bwrite(bp
));
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 */
174 ffs_truncate_internal(vnode_t ovp
, off_t length
, int flags
, ucred_t cred
)
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
];
182 int offset
, size
, level
, i
;
183 long count
, nblocks
, vflags
, blocksreleased
= 0;
185 int aflags
, error
, allerror
;
189 int64_t change
; /* in bytes */
198 if (length
> fs
->fs_maxfilesize
)
202 if (ovp
->v_type
== VLNK
&&
203 oip
->i_size
< ovp
->v_mount
->mnt_maxsymlinklen
) {
206 panic("ffs_truncate: partial truncate of symlink");
208 bzero((char *)&oip
->i_shortlink
, (u_int
)oip
->i_size
);
210 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
211 return (ffs_update(ovp
, &tv
, &tv
, 1));
214 if (oip
->i_size
== length
) {
215 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
216 return (ffs_update(ovp
, &tv
, &tv
, 0));
219 if (error
= getinoquota(oip
))
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.
229 if (osize
< length
) {
230 offset
= blkoff(fs
, length
- 1);
231 lbn
= lblkno(fs
, length
- 1);
235 if (error
= ffs_balloc(oip
, lbn
, offset
+ 1, cred
, &bp
, aflags
, 0))
237 oip
->i_size
= length
;
239 if (UBCINFOEXISTS(ovp
)) {
242 ubc_setsize(ovp
, (off_t
)length
);
249 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
250 return (ffs_update(ovp
, &tv
, &tv
, 1));
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.
259 if (UBCINFOEXISTS(ovp
))
260 ubc_setsize(ovp
, (off_t
)length
);
262 vflags
= ((length
> 0) ? BUF_WRITE_DATA
: 0) | BUF_SKIP_META
;
264 if (vflags
& BUF_WRITE_DATA
)
265 ffs_fsync_internal(ovp
, MNT_WAIT
);
266 allerror
= buf_invalidateblks(ovp
, vflags
, 0, 0);
268 offset
= blkoff(fs
, length
);
270 oip
->i_size
= length
;
272 lbn
= lblkno(fs
, length
);
276 if (error
= ffs_balloc(oip
, lbn
, offset
, cred
, &bp
, aflags
, 0))
278 oip
->i_size
= length
;
279 size
= blksize(fs
, oip
, lbn
);
280 bzero((char *)buf_dataptr(bp
) + offset
, (u_int
)(size
- offset
));
282 if (UBCINFOEXISTS(ovp
)) {
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.
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
);
303 devBlockSize
= vfs_devblocksize(vnode_mount(ovp
));
304 nblocks
= btodb(fs
->fs_bsize
, devBlockSize
);
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.
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;
318 for (i
= NDADDR
- 1; i
> lastblock
; i
--)
320 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
321 if (error
= ffs_update(ovp
, &tv
, &tv
, MNT_WAIT
))
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
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
);
333 vflags
= ((length
> 0) ? BUF_WRITE_DATA
: 0) | BUF_SKIP_META
;
335 if (vflags
& BUF_WRITE_DATA
)
336 ffs_fsync_internal(ovp
, MNT_WAIT
);
337 allerror
= buf_invalidateblks(ovp
, vflags
, 0, 0);
340 * Indirect blocks first.
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
];
348 error
= ffs_indirtrunc(oip
, indir_lbn
[level
],
349 fsbtodb(fs
, bn
), lastiblock
[level
], level
, &count
);
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
;
359 if (lastiblock
[level
] >= 0)
364 * All whole direct blocks or frags.
366 for (i
= NDADDR
- 1; i
> lastblock
; i
--) {
373 bsize
= blksize(fs
, oip
, i
);
374 ffs_blkfree(oip
, bn
, bsize
);
375 blocksreleased
+= btodb(bsize
, devBlockSize
);
381 * Finally, look for a change in size of the
382 * last direct block; release any frags.
384 bn
= oip
->i_db
[lastblock
];
386 long oldspace
, newspace
;
389 * Calculate amount of space we're giving
390 * back as old block size minus new block size.
392 oldspace
= blksize(fs
, oip
, lastblock
);
393 oip
->i_size
= length
;
394 newspace
= blksize(fs
, oip
, lastblock
);
396 panic("itrunc: newspace");
397 if (oldspace
- newspace
> 0) {
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.
403 bn
+= numfrags(fs
, newspace
);
404 ffs_blkfree(oip
, bn
, oldspace
- newspace
);
405 blocksreleased
+= btodb(oldspace
- newspace
, devBlockSize
);
410 for (level
= SINGLE
; level
<= TRIPLE
; level
++)
411 if (newblks
[NDADDR
+ level
] != oip
->i_ib
[level
])
413 for (i
= 0; i
< NDADDR
; i
++)
414 if (newblks
[i
] != oip
->i_db
[i
])
417 (vnode_hasdirtyblks(ovp
) || vnode_hascleanblks(ovp
)))
419 #endif /* DIAGNOSTIC */
421 * Put back the real size.
423 oip
->i_size
= length
;
424 oip
->i_blocks
-= blocksreleased
;
425 if (oip
->i_blocks
< 0) /* sanity */
427 oip
->i_flag
|= IN_CHANGE
;
429 change
= dbtob((int64_t)blocksreleased
,devBlockSize
);
430 (void) chkdq(oip
, -change
, NOCRED
, 0);
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
442 * NB: triple indirect blocks are untested.
445 ffs_indirtrunc(ip
, lbn
, dbn
, lastbn
, level
, countp
)
446 register struct inode
*ip
;
447 ufs_daddr_t lbn
, lastbn
;
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;
463 struct mount
*mp
=vp
->v_mount
;
465 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
466 #endif /* REV_ENDIAN_FS */
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.
474 for (i
= SINGLE
; i
< level
; i
++)
475 factor
*= NINDIR(fs
);
480 devBlockSize
= vfs_devblocksize(mp
);
481 nblocks
= btodb(fs
->fs_bsize
, devBlockSize
);
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
488 tbp
= buf_geteblk(fs
->fs_bsize
);
489 copy
= (ufs_daddr_t
*)buf_dataptr(tbp
);
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.
501 bp
= buf_getblk(vp
, (daddr64_t
)((unsigned)lbn
), (int)fs
->fs_bsize
, 0, 0, BLK_META
);
504 /* Braces must be here in case trace evaluates to nothing. */
505 trace(TR_BREADHIT
, pack(vp
, fs
->fs_bsize
), lbn
);
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
));
514 error
= buf_biowait(bp
);
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
));
529 if (last
!= -1 && (vp
)->v_mount
->mnt_flag
& MNT_ASYNC
) {
533 error
= buf_bwrite(bp
);
540 * Recursively free totally unused blocks.
542 for (i
= NINDIR(fs
) - 1, nlbn
= lbn
+ 1 - i
* factor
; i
> last
;
543 i
--, nlbn
+= factor
) {
546 nb
= OSSwapInt32(bap
[i
]);
548 #endif /* REV_ENDIAN_FS */
552 #endif /* REV_ENDIAN_FS */
555 if (level
> SINGLE
) {
556 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
557 (ufs_daddr_t
)-1, level
- 1, &blkcount
))
559 blocksreleased
+= blkcount
;
561 ffs_blkfree(ip
, nb
, fs
->fs_bsize
);
562 blocksreleased
+= nblocks
;
566 * Recursively free last partial block.
568 if (level
> SINGLE
&& lastbn
>= 0) {
569 last
= lastbn
% factor
;
572 nb
= OSSwapInt32(bap
[i
]);
574 #endif /* REV_ENDIAN_FS */
578 #endif /* REV_ENDIAN_FS */
580 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
581 last
, level
- 1, &blkcount
))
583 blocksreleased
+= blkcount
;
587 *countp
= blocksreleased
;
588 return ((int)allerror
);