/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#include <sys/sysmacros.h>
/*
* This structure is used to track blocks as we allocate them, so that
* we can free them if we encounter an error during allocation. We
* keep track of five pieces of information for each allocated block:
* - The number of the newly allocated block
* - The size of the block (lets us deal with fragments if we want)
* - The number of the block containing a pointer to it; or whether
* the pointer is in the inode
* - The offset within the block (or inode) containing a pointer to it.
* - A flag indicating the usage of the block. (Logging needs to know
* this to avoid overwriting a data block if it was previously used
* for metadata.)
*/
enum ufs_owner_type {
};
struct ufs_allocated_block {
};
int maxtrans);
/*
* Find the extent and the matching block number.
*
* bsize > PAGESIZE
* boff indicates that we want a page in the middle
* min expression is supposed to make sure no extra page[s] after EOF
* PAGESIZE >= bsize
* we assume that a page is a multiple of bsize, i.e.,
* boff always == 0
*
* We always return a length that is suitable for a disk transfer.
*/
\
if (*dp == 0) { \
} else { \
register int len; \
\
if (_chkfrag) { \
register u_offset_t tmp; \
\
} \
if (len <= 0) { \
} else { \
} \
} \
}
/*
* The maximum supported file size is actually somewhat less that 1
* terabyte. This is because the total number of blocks used for the
* file and its metadata must fit into the ic_blocks field of the
* inode, which is a signed 32-bit quantity. The metadata allocated
* for a file (that is, the single, double, and triple indirect blocks
* used to reference the file blocks) is actually quite small,
* but just to make sure, we check for overflow in the ic_blocks
* ic_blocks fields for all files whose total block count is
* within 1 GB of a terabyte. VERYLARGEFILESIZE below is the number of
* 512-byte blocks in a terabyte (2^31), minus the number of 512-byte blocks
* in a gigabyte (2^21). We only check for overflow in the ic_blocks
* field if the number of blocks currently allocated to the file is
* greater than VERYLARGEFILESIZE.
*
* Note that file "size" is the not the same as file "length". A
* file's "size" is the number of blocks allocated to it. A file's
* "length" is the maximum offset in the file. A UFS FILE can have a
* length of a terabyte, but the size is limited to somewhat less than
* a terabyte, as described above.
*/
/*
* bmap{read,write} define the structure of file system storage by mapping
* a logical offset in a file to a physical block number on the device.
* It should be called with a locked inode when allocation is to be
* done (bmap_write). Note this strangeness: bmap_write is always called from
* getpage(), not putpage(), since getpage() is where all the allocation
* is done.
*
* S_READ, S_OTHER -> bmap_read; S_WRITE -> bmap_write.
*
* NOTICE: the block number returned is the disk block number, not the
* file system block number. All the worries about block offsets and
* unfortunately. It's impossible to find one place to hide all this
* mess. There are 3 cases:
*
* PAGESIZE < bsize
* In this case, the {get,put}page routines will attempt to align to
* a file system block boundry (XXX - maybe this is a mistake?). Since
* the kluster routines may be out of memory, we don't always get all
* the pages we wanted. If we called bmap first, to find out how much
* to kluster, we handed in the block aligned offset. If we didn't get
* all the pages, we have to chop off the amount we didn't get from the
* amount handed back by bmap.
*
* PAGESIZE == bsize
* Life is quite pleasant here, no extra work needed, mainly because we
* (probably?) won't kluster backwards, just forwards.
*
* PAGESIZE > bsize
* This one has a different set of problems, specifically, we may have to
* do N reads to fill one page. Let us hope that Sun will stay with small
* pages.
*
* Returns 0 on success, or a non-zero errno if an error occurs.
*
* TODO
* LMXXX - add a bmap cache. This could be a couple of extents in the
* inode. Two is nice for PAGESIZE > bsize.
*/
int
{
int i, j, boff;
if (lbn < 0)
return (EFBIG);
/*
* The first NDADDR blocks are direct blocks.
*/
return (0);
}
/*
* Determine how many levels of indirection.
*/
shft = 0; /* sh = 1 */
for (j = NIADDR; j > 0; j--) {
break;
}
if (j == 0)
return (EFBIG);
/*
* Fetch the first indirect block.
*/
if (nb == 0) {
return (0);
}
/*
* Fetch through the indirect blocks.
*/
for (; j <= NIADDR; j++) {
return (EIO);
}
if (nb == 0) {
return (0);
}
if (j != NIADDR)
}
0, ufsvfsp->vfs_iotransz);
return (0);
}
/*
* See bmap_read for general notes.
*
* The block must be at least size bytes and will be extended or
* allocated as needed. If alloc_type is of type BI_ALLOC_ONLY, then bmap
* will not create any in-core pages that correspond to the new disk allocation.
* If alloc_type is of BI_FALLOCATE, blocks will be stored as (-1) * block addr
* and security is maintained b/c upon reading a negative block number pages
* are zeroed. For all other allocation types (BI_NORMAL) the in-core pages will
* be created and initialized as needed.
*
* Returns 0 on success, or a non-zero errno if an error occurs.
*/
int
{
int i;
int j;
int err;
int nindirshift;
int nindiroffset;
int verylargefile = 0;
if (allocblk)
*allocblk = 0;
if (lbn < 0)
return (EFBIG);
verylargefile = 1;
metaflag = isdirquota = 0;
if (isdirquota || issync) {
}
/*
* If the next write will extend the file into a new block,
* and the file is currently composed of a fragment
* this fragment has to be extended to be a full block.
*/
/*
* Check to see if doing this will make the file too
* big. Only check if we are dealing with a very
* large file.
*/
if (verylargefile == 1) {
return (EFBIG);
}
}
/*
* Make sure we have all needed pages setup correctly.
*
* We pass S_OTHER to fbread here because we want
* an exclusive lock on the page in question
* (see ufs_getpage). I/O to the old block location
* may still be in progress and we are about to free
* the old block. We don't want anyone else to get
* a hold of the old block once we free it until
* the I/O is complete.
*/
err =
if (err)
return (err);
if (err) {
if (fbp)
return (err);
}
/*
* Update the inode before releasing the
* lock on the page. If we released the page
* lock first, the data could be written to it's
* old address and then destroyed.
*/
ip);
/* Caller is responsible for updating i_seq */
/*
* Don't check metaflag here, directories won't do this
*
*/
if (issync) {
} else {
}
}
}
}
/*
* The first NDADDR blocks are direct blocks.
*/
if (nb == 0 ||
if (nb != 0) {
/* consider need to reallocate a frag */
goto gotit;
/*
* Check to see if doing this will make the
* file too big. Only check if we are dealing
* with a very large file.
*/
if (verylargefile == 1) {
return (EFBIG);
}
}
/*
* need to re-allocate a block or frag
*/
if (err)
return (err);
if (allocblk)
} else {
/*
* need to allocate a block or frag
*/
osize = 0;
else
/*
* Check to see if doing this will make the
* file too big. Only check if we are dealing
* with a very large file.
*/
if (verylargefile == 1) {
return (EFBIG);
}
}
if (err)
return (err);
if (allocblk)
}
/*
*/
if (osize == 0) {
/*
* mmap S_WRITE faults always enter here
*/
/*
* We zero it if its also BI_FALLOCATE, but
* only for direct blocks!
*/
if (alloc_type == BI_NORMAL ||
alloc_type == BI_FALLOCATE ||
/* fbzero doesn't cause a pagefault */
}
} else {
if (err) {
} else {
metaflag);
}
return (err);
}
}
/* Caller is responsible for updating i_seq */
/*
* Write directory and shadow blocks synchronously so
* that they never appear with garbage in them on the
* disk.
*
*/
TRANS_ISTRANS(ufsvfsp))) {
/*
* XXX man not be necessary with harpy trans
* bug id 1130055
*/
} else if (fbp) {
}
}
return (0);
}
/*
* Determine how many levels of indirection.
*/
pref = 0;
shft = 0; /* sh = 1 */
for (j = NIADDR; j > 0; j--) {
break;
}
if (j == 0)
return (EFBIG);
/*
* Fetch the first indirect block.
*/
if (nb == 0) {
/*
* Check to see if doing this will make the
* file too big. Only check if we are dealing
* with a very large file.
*/
if (verylargefile == 1) {
> INT_MAX) {
return (EFBIG);
}
}
/*
* Need to allocate an indirect block.
*/
if (err)
return (err);
/*
* Keep track of this allocation so we can undo it if we
* get an error later.
*/
/*
* Write zero block synchronously so that
* indirect blocks never point at garbage.
*/
/* XXX Maybe special-case this? */
return (err);
}
/* Caller is responsible for updating i_seq */
/*
* Update the 'undo table' now that we've linked this block
* to an inode.
*/
/*
* In the ISYNC case, wrip will notice that the block
* count on the inode has changed and will be sure to
* ufs_iupdat the inode at the end of wrip.
*/
}
/*
* Fetch through the indirect blocks.
*/
for (; j <= NIADDR; j++) {
/*
* Return any partial allocations.
*
* It is possible that we have not yet made any
* allocations at this point (if this is the first
* pass through the loop and we didn't have to
* allocate the first indirect block, above).
* In this case, alloced_blocks and added_sectors will
* be zero, and ufs_undo_allocation will do nothing.
*/
return (err);
}
if (nb == 0) {
/*
* Check to see if doing this will make the
* file too big. Only check if we are dealing
* with a very large file.
*/
if (verylargefile == 1) {
> INT_MAX) {
return (EFBIG);
}
}
if (pref == 0) {
if (j < NIADDR) {
/* Indirect block */
(daddr32_t *)0);
} else {
/* Data block */
}
}
/*
* release "bp" buf to avoid deadlock (re-bread later)
*/
if (err) {
/*
* Return any partial allocations.
*/
return (err);
}
if (allocblk)
if (j < NIADDR) {
/*
* Write synchronously so indirect
* blocks never point at garbage.
*/
nbp = UFS_GETBLK(
/* XXX Maybe special-case this? */
/*
* Return any partial
* allocations.
*/
return (err);
}
} else if (alloc_type == BI_NORMAL ||
/*
* Cases which we need to do a synchronous
* write of the zeroed data pages:
*
* 1) If we are writing a directory then we
* want to write synchronously so blocks in
* directories never contain garbage.
*
* 2) If we are filling in a hole and the
* indirect block is going to be synchronously
* written back below we need to make sure
* that the zeroes are written here before
* the indirect block is updated so that if
* we crash before the real data is pushed
* we will not end up with random data is
* the middle of the file.
*
* 3) If the size of the request rounded up
* to the system page size is smaller than
* the file system block size, we want to
* write out all the pages now so that
* they are not aborted before they actually
* make it to ufs_putpage since the length
* of the inode will not include the pages.
*/
if (isdirquota || (issync &&
else
}
/*
* re-acquire "bp" buf
*/
/*
* Return any partial allocations.
*/
return (err);
}
/*
* The magic explained: j will be equal to NIADDR
* when we are at the lowest level, this is where the
* array entries point directly to data blocks. Since
* we will be 'fallocate'ing we will go ahead and negate
* the addresses.
*/
/* Caller is responsible for updating i_seq */
if (issync) {
/*
* Return any partial
* allocations.
*/
return (err);
}
} else {
}
} else {
}
}
return (0);
}
/*
* Return 1 if inode has unmapped blocks (UFS holes) or if another thread
* is in the critical region of wrip().
*/
int
{
int nindirshift;
int nindiroffset;
int n, j, shft;
/*
* Check for writer in critical region, if found then we
* cannot trust the values of i_size and i_blocks
* simply return true.
*/
return (1);
}
/*
* File has only direct blocks.
*/
shft = 0;
/*
* Determine how many levels of indirection.
*/
for (j = NIADDR; j > 0; j--) {
break;
}
/* LINTED: warning: logical expression always true: op "||" */
if (j == NIADDR) /* single level indirection */
}
}
/*
* find some contig blocks starting at *sbp and going for min(n, max_contig)
* return the number of blocks (not frags) found.
* The array passed in must be at least [0..n-1].
*/
static int
{
register int diff;
int maxtransblk;
if (n <= 0)
return (0);
if (bn == 0)
return (0);
if (*lenp) {
} else {
/*
* If the user has set the value for maxcontig lower than
* the drive transfer size, then assume they want this
* to be the maximum value for the size of the data transfer.
*/
} else {
n = MIN(n, maxtransblk);
}
}
while (--n > 0) {
break;
bp++;
}
}
/*
* Free any blocks which had been successfully allocated. Always called
* as a result of an error, so we don't bother returning an error code
* from here.
*
* If block_count and inode_sector_adjust are both zero, we'll do nothing.
* Thus it is safe to call this as part of error handling, whether or not
* any blocks have been allocated.
*
* The ufs_inode_direct case is currently unused.
*/
static void
int block_count,
struct ufs_allocated_block table[],
int inode_sector_adjust)
{
int i;
int inode_changed;
inode_changed = 0;
/*
* Update pointers on disk before freeing blocks. If we fail,
* some blocks may remain busy; but they will be reclaimed by
* an fsck. (This is better than letting a block wind up with
* two owners if we successfully freed it but could not remove
* the pointer to it.)
*/
for (i = 0; i < block_count; i++) {
case ufs_no_owner:
/* Nothing to do here, nobody points to us */
break;
case ufs_inode_direct:
inode_changed = 1;
break;
case ufs_inode_indirect:
inode_changed = 1;
break;
case ufs_indirect_block: {
/* Couldn't read this block; give up. */
break; /* out of SWITCH */
}
/* Write a log entry which includes the zero. */
/* It might be possible to optimize this by using */
/* TRANS_BUF directly and zeroing only the four */
/* bytes involved, but an attempt to do that led */
/* to panics in the logging code. The attempt was */
/* TRANS_BUF(ufsvfsp, */
/* table[i].owner_offset * sizeof (daddr32_t), */
/* sizeof (daddr32_t), */
/* bp, */
/* DT_ABZERO); */
/* Now we can write the buffer itself. */
}
break;
}
default:
"ufs_undo_allocation failure\n");
break;
}
}
/*
* If the inode changed, or if we need to update its block count,
* then do that now. We update the inode synchronously on disk
* to ensure that it won't transiently point at a block we've
* freed (only necessary if we're not logging).
*
* NOTE: Currently ufs_iupdat() does not check for errors. When
* it is fixed, we should verify that we successfully updated the
* inode before freeing blocks below.
*/
if (inode_changed || (inode_sector_adjust != 0)) {
if (!TRANS_ISTRANS(ufsvfsp))
}
/*
* Now we go through and actually free the blocks, but only if we
* successfully removed the pointers to them.
*/
if (!error_updating_pointers) {
for (i = 0; i < block_count; i++) {
table[i].usage_flags);
}
}
}
/*
* Find the next hole or data block in file starting at *off
* Return found offset in *off, which can be less than the
* starting offset if not block aligned.
* This code is based on bmap_read().
* Errors: ENXIO for end of file
* EIO for block read error.
*/
int
{
int i, j;
int error = 0;
for (i = 0; i < NIADDR; i++)
/*
* The first NDADDR blocks are direct blocks.
*/
goto out;
}
}
goto out;
}
/* Set up limits array */
loop:
/*
* Determine how many levels of indirection.
*/
shft = 0; /* sh = 1 */
for (j = NIADDR; j > 0; j--) {
break;
}
if (j == 0) {
/* must have passed end of file */
goto out;
}
/*
* Fetch the first indirect block.
*/
if (nb == 0) {
if (hole) {
goto out;
} else {
goto out;
goto loop;
}
}
/*
* Fetch through the indirect blocks.
*/
/*
* if there's a different block at this level then release
* the old one and in with the new.
*/
goto out;
}
}
}
/*
* Scan through the blocks in this array.
*/
goto out;
if (skip == 1) {
/* we're at the lowest level */
goto out;
} else {
goto loop;
}
}
}
goto loop;
out:
for (i = 0; i < NIADDR; i++) {
if (bp[i])
}
if (error == 0) {
} else {
/* success */
}
}
return (error);
}
/*
* Set a particular offset in the inode list to be a certain block.
* User is responsible for calling TRANS* functions
*/
int
{
int i, j;
int err;
if (lbn < 0)
return (EFBIG);
/*
* Take care of direct block assignment
*/
return (0);
}
/*
* Determine how many levels of indirection.
*/
shft = 0; /* sh = 1 */
for (j = NIADDR; j > 0; j--) {
break;
}
if (j == 0)
return (EFBIG);
/*
* Fetch the first indirect block.
*/
if (nb == 0) {
return (err);
}
/*
* Fetch through the indirect blocks.
*/
for (; j <= NIADDR; j++) {
return (err);
}
if (nb == 0) {
return (err);
}
if (j == NIADDR) {
return (0);
}
}
return (0);
}