]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ffs/ffs_balloc.c
a6c61ac8c75499bf34c9d0c557dde035228f7c7a
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_balloc.c 8.8 (Berkeley) 6/16/95
58 #include <rev_endian_fs.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
64 #include <sys/vnode.h>
67 #include <sys/mount.h>
68 #endif /* REV_ENDIAN_FS */
72 #include <ufs/ufs/quota.h>
73 #include <ufs/ufs/inode.h>
74 #include <ufs/ufs/ufs_extern.h>
76 #include <ufs/ffs/fs.h>
77 #include <ufs/ffs/ffs_extern.h>
80 #include <ufs/ufs/ufs_byte_order.h>
81 #include <architecture/byte_order.h>
82 #endif /* REV_ENDIAN_FS */
85 * Balloc defines the structure of file system storage
86 * by allocating the physical blocks on a device given
87 * the inode and the logical block number in a file.
89 ffs_balloc(ip
, lbn
, size
, cred
, bpp
, flags
, blk_alloc
)
90 register struct inode
*ip
;
91 register ufs_daddr_t lbn
;
98 register struct fs
*fs
;
99 register ufs_daddr_t nb
;
100 struct buf
*bp
, *nbp
;
101 struct vnode
*vp
= ITOV(ip
);
102 struct indir indirs
[NIADDR
+ 2];
103 ufs_daddr_t newb
, *bap
, pref
;
104 int deallocated
, osize
, nsize
, num
, i
, error
;
105 ufs_daddr_t
*allocib
, *blkp
, *allocblk
, allociblk
[NIADDR
+ 1];
107 int alloc_buffer
= 1;
109 struct mount
*mp
=vp
->v_mount
;
110 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
111 #endif /* REV_ENDIAN_FS */
117 if (flags
& B_NOBUFF
)
124 * If the next write will extend the file into a new block,
125 * and the file is currently composed of a fragment
126 * this fragment has to be extended to be a full block.
128 nb
= lblkno(fs
, ip
->i_size
);
129 if (nb
< NDADDR
&& nb
< lbn
) {
130 /* the filesize prior to this write can fit in direct
131 * blocks (ie. fragmentaion is possibly done)
132 * we are now extending the file write beyond
133 * the block which has end of file prior to this write
135 osize
= blksize(fs
, ip
, nb
);
136 /* osize gives disk allocated size in the last block. It is
137 * either in fragments or a file system block size */
138 if (osize
< fs
->fs_bsize
&& osize
> 0) {
139 /* few fragments are already allocated,since the
140 * current extends beyond this block
141 * allocate the complete block as fragments are only
144 error
= ffs_realloccg(ip
, nb
,
145 ffs_blkpref(ip
, nb
, (int)nb
, &ip
->i_db
[0]),
146 osize
, (int)fs
->fs_bsize
, cred
, &bp
);
149 /* adjust the innode size we just grew */
150 /* it is in nb+1 as nb starts from 0 */
151 ip
->i_size
= (nb
+ 1) * fs
->fs_bsize
;
153 ubc_setsize(vp
, (off_t
)ip
->i_size
); /* XXX check error */
154 ip
->i_db
[nb
] = dbtofsb(fs
, bp
->b_blkno
);
155 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
156 if ((flags
& B_SYNC
) || (!alloc_buffer
)) {
158 SET(bp
->b_flags
, B_INVAL
);
162 /* note that bp is already released here */
166 * The first NDADDR blocks are direct blocks
170 if (nb
!= 0 && ip
->i_size
>= (lbn
+ 1) * fs
->fs_bsize
) {
172 error
= bread(vp
, lbn
, fs
->fs_bsize
, NOCRED
, &bp
);
183 * Consider need to reallocate a fragment.
185 osize
= fragroundup(fs
, blkoff(fs
, ip
->i_size
));
186 nsize
= fragroundup(fs
, size
);
187 if (nsize
<= osize
) {
189 error
= bread(vp
, lbn
, osize
, NOCRED
, &bp
);
194 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
199 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
203 error
= ffs_realloccg(ip
, lbn
,
204 ffs_blkpref(ip
, lbn
, (int)lbn
,
205 &ip
->i_db
[0]), osize
, nsize
, cred
, &bp
);
208 ip
->i_db
[lbn
] = dbtofsb(fs
, bp
->b_blkno
);
209 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
211 SET(bp
->b_flags
, B_INVAL
);
219 if (ip
->i_size
< (lbn
+ 1) * fs
->fs_bsize
)
220 nsize
= fragroundup(fs
, size
);
222 nsize
= fs
->fs_bsize
;
223 error
= ffs_alloc(ip
, lbn
,
224 ffs_blkpref(ip
, lbn
, (int)lbn
, &ip
->i_db
[0]),
229 bp
= getblk(vp
, lbn
, nsize
, 0, 0, BLK_WRITE
);
230 bp
->b_blkno
= fsbtodb(fs
, newb
);
231 if (flags
& B_CLRBUF
)
234 ip
->i_db
[lbn
] = newb
;
235 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
245 * Determine the number of levels of indirection.
248 if (error
= ufs_getlbns(vp
, lbn
, indirs
, &num
))
252 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
255 * Fetch the first indirect block allocating if necessary.
258 nb
= ip
->i_ib
[indirs
[0].in_off
];
260 allocblk
= allociblk
;
262 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
263 if (error
= ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
,
268 bp
= getblk(vp
, indirs
[1].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
269 bp
->b_blkno
= fsbtodb(fs
, nb
);
272 * Write synchronously so that indirect blocks
273 * never point at garbage.
275 if (error
= bwrite(bp
))
277 allocib
= &ip
->i_ib
[indirs
[0].in_off
];
279 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
282 * Fetch through the indirect blocks, allocating as necessary.
285 error
= meta_bread(vp
,
286 indirs
[i
].in_lbn
, (int)fs
->fs_bsize
, NOCRED
, &bp
);
291 bap
= (ufs_daddr_t
*)bp
->b_data
;
294 nb
= NXSwapLong(bap
[indirs
[i
].in_off
]);
296 #endif /* REV_ENDIAN_FS */
297 nb
= bap
[indirs
[i
].in_off
];
300 #endif /* REV_ENDIAN_FS */
309 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
311 ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
317 nbp
= getblk(vp
, indirs
[i
].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
318 nbp
->b_blkno
= fsbtodb(fs
, nb
);
321 * Write synchronously so that indirect blocks
322 * never point at garbage.
324 if (error
= bwrite(nbp
)) {
330 bap
[indirs
[i
- 1].in_off
] = NXSwapLong(nb
);
332 #endif /* REV_ENDIAN_FS */
333 bap
[indirs
[i
- 1].in_off
] = nb
;
336 #endif /* REV_ENDIAN_FS */
338 * If required, write synchronously, otherwise use
341 if (flags
& B_SYNC
) {
348 * Get the data block, allocating if necessary.
351 pref
= ffs_blkpref(ip
, lbn
, indirs
[i
].in_off
, &bap
[0]);
352 if (error
= ffs_alloc(ip
,
353 lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
361 bap
[indirs
[i
].in_off
] = NXSwapLong(nb
);
363 #endif /* REV_ENDIAN_FS */
364 bap
[indirs
[i
].in_off
] = nb
;
367 #endif /* REV_ENDIAN_FS */
369 * If required, write synchronously, otherwise use
372 if ((flags
& B_SYNC
)) {
378 nbp
= getblk(vp
, lbn
, fs
->fs_bsize
, 0, 0, BLK_WRITE
);
379 nbp
->b_blkno
= fsbtodb(fs
, nb
);
380 if (flags
& B_CLRBUF
)
384 *blk_alloc
= fs
->fs_bsize
;
393 if (flags
& B_CLRBUF
) {
394 error
= bread(vp
, lbn
, (int)fs
->fs_bsize
, NOCRED
, &nbp
);
400 nbp
= getblk(vp
, lbn
, fs
->fs_bsize
, 0, 0, BLK_WRITE
);
401 nbp
->b_blkno
= fsbtodb(fs
, nb
);
408 * If we have failed part way through block allocation, we
409 * have to deallocate any indirect blocks that we have allocated.
411 for (deallocated
= 0, blkp
= allociblk
; blkp
< allocblk
; blkp
++) {
412 ffs_blkfree(ip
, *blkp
, fs
->fs_bsize
);
413 deallocated
+= fs
->fs_bsize
;
418 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
422 * Restore user's disk quota because allocation failed.
424 (void) chkdq(ip
, (long)-btodb(deallocated
, devBlockSize
), cred
, FORCE
);
426 ip
->i_blocks
-= btodb(deallocated
, devBlockSize
);
427 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
433 * ffs_blkalloc allocates a disk block for ffs_pageout(), as a consequence
434 * it does no breads (that could lead to deadblock as the page may be already
435 * marked busy as it is being paged out. Also important to note that we are not
436 * growing the file in pageouts. So ip->i_size cannot increase by this call
437 * due to the way UBC works.
438 * This code is derived from ffs_balloc and many cases of that are dealt
439 * in ffs_balloc are not applicable here
440 * Do not call with B_CLRBUF flags as this should only be called only
443 ffs_blkalloc(ip
, lbn
, size
, cred
, flags
)
444 register struct inode
*ip
;
450 register struct fs
*fs
;
451 register ufs_daddr_t nb
;
452 struct buf
*bp
, *nbp
;
453 struct vnode
*vp
= ITOV(ip
);
454 struct indir indirs
[NIADDR
+ 2];
455 ufs_daddr_t newb
, *bap
, pref
;
456 int deallocated
, osize
, nsize
, num
, i
, error
;
457 ufs_daddr_t
*allocib
, *blkp
, *allocblk
, allociblk
[NIADDR
+ 1];
460 struct mount
*mp
=vp
->v_mount
;
461 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
462 #endif /* REV_ENDIAN_FS */
466 if(size
> fs
->fs_bsize
)
467 panic("ffs_blkalloc: too large for allocation\n");
470 * If the next write will extend the file into a new block,
471 * and the file is currently composed of a fragment
472 * this fragment has to be extended to be a full block.
474 nb
= lblkno(fs
, ip
->i_size
);
475 if (nb
< NDADDR
&& nb
< lbn
) {
476 panic("ffs_blkalloc():cannot extend file: i_size %d, lbn %d\n", ip
->i_size
, lbn
);
479 * The first NDADDR blocks are direct blocks
483 if (nb
!= 0 && ip
->i_size
>= (lbn
+ 1) * fs
->fs_bsize
) {
484 /* TBD: trivial case; the block is already allocated */
489 * Consider need to reallocate a fragment.
491 osize
= fragroundup(fs
, blkoff(fs
, ip
->i_size
));
492 nsize
= fragroundup(fs
, size
);
494 panic("ffs_allocblk: trying to extend
499 if (ip
->i_size
< (lbn
+ 1) * fs
->fs_bsize
)
500 nsize
= fragroundup(fs
, size
);
502 nsize
= fs
->fs_bsize
;
503 error
= ffs_alloc(ip
, lbn
,
504 ffs_blkpref(ip
, lbn
, (int)lbn
, &ip
->i_db
[0]),
508 ip
->i_db
[lbn
] = newb
;
509 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
514 * Determine the number of levels of indirection.
517 if (error
= ufs_getlbns(vp
, lbn
, indirs
, &num
))
521 panic("ffs_blkalloc: file with direct blocks only\n");
525 * Fetch the first indirect block allocating if necessary.
528 nb
= ip
->i_ib
[indirs
[0].in_off
];
530 allocblk
= allociblk
;
532 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
533 if (error
= ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
,
538 bp
= getblk(vp
, indirs
[1].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
539 bp
->b_blkno
= fsbtodb(fs
, nb
);
542 * Write synchronously so that indirect blocks
543 * never point at garbage.
545 if (error
= bwrite(bp
))
547 allocib
= &ip
->i_ib
[indirs
[0].in_off
];
549 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
552 * Fetch through the indirect blocks, allocating as necessary.
555 error
= meta_bread(vp
,
556 indirs
[i
].in_lbn
, (int)fs
->fs_bsize
, NOCRED
, &bp
);
561 bap
= (ufs_daddr_t
*)bp
->b_data
;
564 nb
= NXSwapLong(bap
[indirs
[i
].in_off
]);
566 #endif /* REV_ENDIAN_FS */
567 nb
= bap
[indirs
[i
].in_off
];
570 #endif /* REV_ENDIAN_FS */
579 pref
= ffs_blkpref(ip
, lbn
, 0, (ufs_daddr_t
*)0);
581 ffs_alloc(ip
, lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
587 nbp
= getblk(vp
, indirs
[i
].in_lbn
, fs
->fs_bsize
, 0, 0, BLK_META
);
588 nbp
->b_blkno
= fsbtodb(fs
, nb
);
591 * Write synchronously so that indirect blocks
592 * never point at garbage.
594 if (error
= bwrite(nbp
)) {
600 bap
[indirs
[i
- 1].in_off
] = NXSwapLong(nb
);
602 #endif /* REV_ENDIAN_FS */
603 bap
[indirs
[i
- 1].in_off
] = nb
;
606 #endif /* REV_ENDIAN_FS */
608 * If required, write synchronously, otherwise use
611 if (flags
& B_SYNC
) {
618 * Get the data block, allocating if necessary.
621 pref
= ffs_blkpref(ip
, lbn
, indirs
[i
].in_off
, &bap
[0]);
622 if (error
= ffs_alloc(ip
,
623 lbn
, pref
, (int)fs
->fs_bsize
, cred
, &newb
)) {
631 bap
[indirs
[i
].in_off
] = NXSwapLong(nb
);
633 #endif /* REV_ENDIAN_FS */
634 bap
[indirs
[i
].in_off
] = nb
;
637 #endif /* REV_ENDIAN_FS */
639 * If required, write synchronously, otherwise use
642 if (flags
& B_SYNC
) {
653 * If we have failed part way through block allocation, we
654 * have to deallocate any indirect blocks that we have allocated.
656 for (deallocated
= 0, blkp
= allociblk
; blkp
< allocblk
; blkp
++) {
657 ffs_blkfree(ip
, *blkp
, fs
->fs_bsize
);
658 deallocated
+= fs
->fs_bsize
;
663 VOP_DEVBLOCKSIZE(ip
->i_devvp
,&devBlockSize
);
667 * Restore user's disk quota because allocation failed.
669 (void) chkdq(ip
, (long)-btodb(deallocated
, devBlockSize
), cred
, FORCE
);
671 ip
->i_blocks
-= btodb(deallocated
, devBlockSize
);
672 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;