]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_inode.c
3b7b729851e990ff0c3e85ab03d6b016f4b9194a
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.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>
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>
81 #include <ufs/ffs/fs.h>
82 #include <ufs/ffs/ffs_extern.h>
85 #include <ufs/ufs/ufs_byte_order.h>
86 #include <architecture/byte_order.h>
87 #endif /* REV_ENDIAN_FS */
89 static int ffs_indirtrunc
__P((struct inode
*, ufs_daddr_t
, ufs_daddr_t
,
90 ufs_daddr_t
, int, long *));
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
103 struct vop_update_args
/* {
105 struct timeval *a_access;
106 struct timeval *a_modify;
110 register struct fs
*fs
;
115 struct mount
*mp
=(ap
->a_vp
)->v_mount
;
116 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
117 #endif /* REV_ENDIAN_FS */
120 if (ap
->a_vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
122 ~(IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
);
126 (IN_ACCESS
| IN_CHANGE
| IN_MODIFIED
| IN_UPDATE
)) == 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
;
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
);
139 * Ensure that uid and gid are correct. This is a temporary
140 * fix until fsck has been changed to do the update.
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 */
146 if (error
= bread(ip
->i_devvp
,
147 fsbtodb(fs
, ino_to_fsba(fs
, ip
->i_number
)),
148 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
154 byte_swap_inode_out(ip
, ((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ip
->i_number
)));
156 #endif /* REV_ENDIAN_FS */
157 *((struct dinode
*)bp
->b_data
+
158 ino_to_fsbo(fs
, ip
->i_number
)) = ip
->i_din
;
161 #endif /* REV_ENDIAN_FS */
163 if (ap
->a_waitfor
&& (ap
->a_vp
->v_mount
->mnt_flag
& MNT_ASYNC
) == 0)
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 */
175 * Truncate the inode oip to at most length size, freeing the
179 struct vop_truncate_args
/* {
183 struct ucred *a_cred;
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
;
195 int offset
, size
, level
;
196 long count
, nblocks
, vflags
, blocksreleased
= 0;
199 int aflags
, error
, allerror
;
209 if (length
> fs
->fs_maxfilesize
)
213 if (ovp
->v_type
== VLNK
&&
214 oip
->i_size
< ovp
->v_mount
->mnt_maxsymlinklen
) {
217 panic("ffs_truncate: partial truncate of symlink");
219 bzero((char *)&oip
->i_shortlink
, (u_int
)oip
->i_size
);
221 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
222 return (VOP_UPDATE(ovp
, &tv
, &tv
, 1));
225 if (oip
->i_size
== length
) {
226 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
227 return (VOP_UPDATE(ovp
, &tv
, &tv
, 0));
230 if (error
= getinoquota(oip
))
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.
240 if (osize
< length
) {
241 offset
= blkoff(fs
, length
- 1);
242 lbn
= lblkno(fs
, length
- 1);
244 if (ap
->a_flags
& IO_SYNC
)
246 if (error
= ffs_balloc(oip
, lbn
, offset
+ 1, ap
->a_cred
, &bp
,
249 oip
->i_size
= length
;
251 if (UBCINFOEXISTS(ovp
)) {
252 bp
->b_flags
|= B_INVAL
;
254 ubc_setsize(ovp
, (off_t
)length
);
261 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
262 return (VOP_UPDATE(ovp
, &tv
, &tv
, 1));
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.
271 if (UBCINFOEXISTS(ovp
))
272 ubc_setsize(ovp
, (off_t
)length
);
274 vflags
= ((length
> 0) ? V_SAVE
: 0) | V_SAVEMETA
;
275 allerror
= vinvalbuf(ovp
, vflags
, ap
->a_cred
, ap
->a_p
, 0, 0);
278 offset
= blkoff(fs
, length
);
280 oip
->i_size
= length
;
282 lbn
= lblkno(fs
, length
);
284 if (ap
->a_flags
& IO_SYNC
)
286 if (error
= ffs_balloc(oip
, lbn
, offset
, ap
->a_cred
, &bp
,
289 oip
->i_size
= length
;
290 size
= blksize(fs
, oip
, lbn
);
291 bzero((char *)bp
->b_data
+ offset
, (u_int
)(size
- offset
));
293 if (UBCINFOEXISTS(ovp
)) {
294 bp
->b_flags
|= B_INVAL
;
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.
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
);
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.
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;
328 for (i
= NDADDR
- 1; i
> lastblock
; i
--)
330 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
331 if (error
= VOP_UPDATE(ovp
, &tv
, &tv
, MNT_WAIT
))
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
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
);
342 vflags
= ((length
> 0) ? V_SAVE
: 0) | V_SAVEMETA
;
343 allerror
= vinvalbuf(ovp
, vflags
, ap
->a_cred
, ap
->a_p
, 0, 0);
346 * Indirect blocks first.
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
];
354 error
= ffs_indirtrunc(oip
, indir_lbn
[level
],
355 fsbtodb(fs
, bn
), lastiblock
[level
], level
, &count
);
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
;
365 if (lastiblock
[level
] >= 0)
370 * All whole direct blocks or frags.
372 for (i
= NDADDR
- 1; i
> lastblock
; i
--) {
379 bsize
= blksize(fs
, oip
, i
);
380 ffs_blkfree(oip
, bn
, bsize
);
381 blocksreleased
+= btodb(bsize
, devBlockSize
);
387 * Finally, look for a change in size of the
388 * last direct block; release any frags.
390 bn
= oip
->i_db
[lastblock
];
392 long oldspace
, newspace
;
395 * Calculate amount of space we're giving
396 * back as old block size minus new block size.
398 oldspace
= blksize(fs
, oip
, lastblock
);
399 oip
->i_size
= length
;
400 newspace
= blksize(fs
, oip
, lastblock
);
402 panic("itrunc: newspace");
403 if (oldspace
- newspace
> 0) {
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.
409 bn
+= numfrags(fs
, newspace
);
410 ffs_blkfree(oip
, bn
, oldspace
- newspace
);
411 blocksreleased
+= btodb(oldspace
- newspace
, devBlockSize
);
416 for (level
= SINGLE
; level
<= TRIPLE
; level
++)
417 if (newblks
[NDADDR
+ level
] != oip
->i_ib
[level
])
419 for (i
= 0; i
< NDADDR
; i
++)
420 if (newblks
[i
] != oip
->i_db
[i
])
423 (ovp
->v_dirtyblkhd
.lh_first
|| ovp
->v_cleanblkhd
.lh_first
))
425 #endif /* DIAGNOSTIC */
427 * Put back the real size.
429 oip
->i_size
= length
;
430 oip
->i_blocks
-= blocksreleased
;
431 if (oip
->i_blocks
< 0) /* sanity */
433 oip
->i_flag
|= IN_CHANGE
;
435 (void) chkdq(oip
, -blocksreleased
, NOCRED
, 0);
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
447 * NB: triple indirect blocks are untested.
450 ffs_indirtrunc(ip
, lbn
, dbn
, lastbn
, level
, countp
)
451 register struct inode
*ip
;
452 ufs_daddr_t lbn
, lastbn
;
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;
469 struct mount
*mp
=vp
->v_mount
;
470 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
471 #endif /* REV_ENDIAN_FS */
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.
479 for (i
= SINGLE
; i
< level
; i
++)
480 factor
*= NINDIR(fs
);
484 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
485 nblocks
= btodb(fs
->fs_bsize
, devBlockSize
);
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
492 tbp
= geteblk(fs
->fs_bsize
);
493 copy
= (ufs_daddr_t
*)tbp
->b_data
;
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.
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
);
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");
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
));
531 bp
->b_flags
|= B_INVAL
;
538 * Recursively free totally unused blocks.
540 for (i
= NINDIR(fs
) - 1, nlbn
= lbn
+ 1 - i
* factor
; i
> last
;
541 i
--, nlbn
+= factor
) {
544 nb
= NXSwapLong(bap
[i
]);
546 #endif /* REV_ENDIAN_FS */
550 #endif /* REV_ENDIAN_FS */
553 if (level
> SINGLE
) {
554 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
555 (ufs_daddr_t
)-1, level
- 1, &blkcount
))
557 blocksreleased
+= blkcount
;
559 ffs_blkfree(ip
, nb
, fs
->fs_bsize
);
560 blocksreleased
+= nblocks
;
564 * Recursively free last partial block.
566 if (level
> SINGLE
&& lastbn
>= 0) {
567 last
= lastbn
% factor
;
570 nb
= NXSwapLong(bap
[i
]);
572 #endif /* REV_ENDIAN_FS */
576 #endif /* REV_ENDIAN_FS */
578 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
579 last
, level
- 1, &blkcount
))
581 blocksreleased
+= blkcount
;
585 *countp
= blocksreleased
;