]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_inode.c
0ffc112e234b1618a333d238510df76c1b56f686
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
58 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
61 #include <rev_endian_fs.h>
62 #include <vm/vm_pager.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/mount.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>
76 #include <sys/quota.h>
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>
85 #include <ufs/ffs/fs.h>
86 #include <ufs/ffs/ffs_extern.h>
89 #include <ufs/ufs/ufs_byte_order.h>
90 #include <architecture/byte_order.h>
91 #endif /* REV_ENDIAN_FS */
93 static int ffs_indirtrunc
__P((struct inode
*, ufs_daddr_t
, ufs_daddr_t
,
94 ufs_daddr_t
, int, long *));
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
107 struct vop_update_args
/* {
109 struct timeval *a_access;
110 struct timeval *a_modify;
114 register struct fs
*fs
;
119 struct mount
*mp
=(ap
->a_vp
)->v_mount
;
120 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
121 #endif /* REV_ENDIAN_FS */
124 if (ap
->a_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
= ap
->a_access
->tv_sec
;
134 if (ip
->i_flag
& IN_UPDATE
) {
135 ip
->i_mtime
= ap
->a_modify
->tv_sec
;
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
);
143 * Ensure that uid and gid are correct. This is a temporary
144 * fix until fsck has been changed to do the update.
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 */
150 if (error
= bread(ip
->i_devvp
,
151 fsbtodb(fs
, ino_to_fsba(fs
, ip
->i_number
)),
152 (int)fs
->fs_bsize
, NOCRED
, &bp
)) {
158 byte_swap_inode_out(ip
, ((struct dinode
*)bp
->b_data
+ ino_to_fsbo(fs
, ip
->i_number
)));
160 #endif /* REV_ENDIAN_FS */
161 *((struct dinode
*)bp
->b_data
+
162 ino_to_fsbo(fs
, ip
->i_number
)) = ip
->i_din
;
165 #endif /* REV_ENDIAN_FS */
167 if (ap
->a_waitfor
&& (ap
->a_vp
->v_mount
->mnt_flag
& MNT_ASYNC
) == 0)
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 */
179 * Truncate the inode oip to at most length size, freeing the
183 struct vop_truncate_args
/* {
187 struct ucred *a_cred;
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
;
199 int offset
, size
, level
;
200 long count
, nblocks
, vflags
, blocksreleased
= 0;
203 int aflags
, error
, allerror
;
207 int64_t change
; /* in bytes */
216 if (length
> fs
->fs_maxfilesize
)
220 if (ovp
->v_type
== VLNK
&&
221 oip
->i_size
< ovp
->v_mount
->mnt_maxsymlinklen
) {
224 panic("ffs_truncate: partial truncate of symlink");
226 bzero((char *)&oip
->i_shortlink
, (u_int
)oip
->i_size
);
228 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
229 return (VOP_UPDATE(ovp
, &tv
, &tv
, 1));
232 if (oip
->i_size
== length
) {
233 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
234 return (VOP_UPDATE(ovp
, &tv
, &tv
, 0));
237 if (error
= getinoquota(oip
))
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.
247 if (osize
< length
) {
248 offset
= blkoff(fs
, length
- 1);
249 lbn
= lblkno(fs
, length
- 1);
251 if (ap
->a_flags
& IO_SYNC
)
253 if (error
= ffs_balloc(oip
, lbn
, offset
+ 1, ap
->a_cred
, &bp
,
256 oip
->i_size
= length
;
258 if (UBCINFOEXISTS(ovp
)) {
259 bp
->b_flags
|= B_INVAL
;
261 ubc_setsize(ovp
, (off_t
)length
);
268 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
269 return (VOP_UPDATE(ovp
, &tv
, &tv
, 1));
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.
278 if (UBCINFOEXISTS(ovp
))
279 ubc_setsize(ovp
, (off_t
)length
);
281 vflags
= ((length
> 0) ? V_SAVE
: 0) | V_SAVEMETA
;
282 allerror
= vinvalbuf(ovp
, vflags
, ap
->a_cred
, ap
->a_p
, 0, 0);
285 offset
= blkoff(fs
, length
);
287 oip
->i_size
= length
;
289 lbn
= lblkno(fs
, length
);
291 if (ap
->a_flags
& IO_SYNC
)
293 if (error
= ffs_balloc(oip
, lbn
, offset
, ap
->a_cred
, &bp
,
296 oip
->i_size
= length
;
297 size
= blksize(fs
, oip
, lbn
);
298 bzero((char *)bp
->b_data
+ offset
, (u_int
)(size
- offset
));
300 if (UBCINFOEXISTS(ovp
)) {
301 bp
->b_flags
|= B_INVAL
;
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.
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
);
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.
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;
335 for (i
= NDADDR
- 1; i
> lastblock
; i
--)
337 oip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
338 if (error
= VOP_UPDATE(ovp
, &tv
, &tv
, MNT_WAIT
))
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
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
);
349 vflags
= ((length
> 0) ? V_SAVE
: 0) | V_SAVEMETA
;
350 allerror
= vinvalbuf(ovp
, vflags
, ap
->a_cred
, ap
->a_p
, 0, 0);
353 * Indirect blocks first.
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
];
361 error
= ffs_indirtrunc(oip
, indir_lbn
[level
],
362 fsbtodb(fs
, bn
), lastiblock
[level
], level
, &count
);
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
;
372 if (lastiblock
[level
] >= 0)
377 * All whole direct blocks or frags.
379 for (i
= NDADDR
- 1; i
> lastblock
; i
--) {
386 bsize
= blksize(fs
, oip
, i
);
387 ffs_blkfree(oip
, bn
, bsize
);
388 blocksreleased
+= btodb(bsize
, devBlockSize
);
394 * Finally, look for a change in size of the
395 * last direct block; release any frags.
397 bn
= oip
->i_db
[lastblock
];
399 long oldspace
, newspace
;
402 * Calculate amount of space we're giving
403 * back as old block size minus new block size.
405 oldspace
= blksize(fs
, oip
, lastblock
);
406 oip
->i_size
= length
;
407 newspace
= blksize(fs
, oip
, lastblock
);
409 panic("itrunc: newspace");
410 if (oldspace
- newspace
> 0) {
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.
416 bn
+= numfrags(fs
, newspace
);
417 ffs_blkfree(oip
, bn
, oldspace
- newspace
);
418 blocksreleased
+= btodb(oldspace
- newspace
, devBlockSize
);
423 for (level
= SINGLE
; level
<= TRIPLE
; level
++)
424 if (newblks
[NDADDR
+ level
] != oip
->i_ib
[level
])
426 for (i
= 0; i
< NDADDR
; i
++)
427 if (newblks
[i
] != oip
->i_db
[i
])
430 (ovp
->v_dirtyblkhd
.lh_first
|| ovp
->v_cleanblkhd
.lh_first
))
432 #endif /* DIAGNOSTIC */
434 * Put back the real size.
436 oip
->i_size
= length
;
437 oip
->i_blocks
-= blocksreleased
;
438 if (oip
->i_blocks
< 0) /* sanity */
440 oip
->i_flag
|= IN_CHANGE
;
442 change
= dbtob((int64_t)blocksreleased
,devBlockSize
);
443 (void) chkdq(oip
, -change
, NOCRED
, 0);
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
455 * NB: triple indirect blocks are untested.
458 ffs_indirtrunc(ip
, lbn
, dbn
, lastbn
, level
, countp
)
459 register struct inode
*ip
;
460 ufs_daddr_t lbn
, lastbn
;
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;
477 struct mount
*mp
=vp
->v_mount
;
478 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
479 #endif /* REV_ENDIAN_FS */
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.
487 for (i
= SINGLE
; i
< level
; i
++)
488 factor
*= NINDIR(fs
);
492 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
493 nblocks
= btodb(fs
->fs_bsize
, devBlockSize
);
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
500 tbp
= geteblk(fs
->fs_bsize
);
501 copy
= (ufs_daddr_t
*)tbp
->b_data
;
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.
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
);
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");
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
));
539 bp
->b_flags
|= B_INVAL
;
546 * Recursively free totally unused blocks.
548 for (i
= NINDIR(fs
) - 1, nlbn
= lbn
+ 1 - i
* factor
; i
> last
;
549 i
--, nlbn
+= factor
) {
552 nb
= NXSwapLong(bap
[i
]);
554 #endif /* REV_ENDIAN_FS */
558 #endif /* REV_ENDIAN_FS */
561 if (level
> SINGLE
) {
562 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
563 (ufs_daddr_t
)-1, level
- 1, &blkcount
))
565 blocksreleased
+= blkcount
;
567 ffs_blkfree(ip
, nb
, fs
->fs_bsize
);
568 blocksreleased
+= nblocks
;
572 * Recursively free last partial block.
574 if (level
> SINGLE
&& lastbn
>= 0) {
575 last
= lastbn
% factor
;
578 nb
= NXSwapLong(bap
[i
]);
580 #endif /* REV_ENDIAN_FS */
584 #endif /* REV_ENDIAN_FS */
586 if (error
= ffs_indirtrunc(ip
, nlbn
, fsbtodb(fs
, nb
),
587 last
, level
- 1, &blkcount
))
589 blocksreleased
+= blkcount
;
593 *countp
= blocksreleased
;