10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*5331Samw * Common Development and Distribution License (the "License").
6*5331Samw * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*5331Samw * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/fssnap_if.h>
330Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
340Sstevel@tonic-gate #include <sys/fs/ufs_lockfs.h>
350Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
360Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
380Sstevel@tonic-gate #include <vm/pvn.h>
390Sstevel@tonic-gate #include <vm/seg_map.h>
400Sstevel@tonic-gate #include <sys/fdbuffer.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #ifdef DEBUG
430Sstevel@tonic-gate int evn_ufs_debug = 0;
440Sstevel@tonic-gate #define DEBUGF(args) { if (evn_ufs_debug) cmn_err args; }
450Sstevel@tonic-gate #else
460Sstevel@tonic-gate #define DEBUGF(args)
470Sstevel@tonic-gate #endif
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * ufs_rdwr_data - supports reading or writing data when
510Sstevel@tonic-gate * no changes are permitted in file size or space allocation.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * Inputs:
540Sstevel@tonic-gate * fdb - The mandatory fdbuffer supports
550Sstevel@tonic-gate * the read or write operation.
560Sstevel@tonic-gate * flags - defaults (zero value) to synchronous write
570Sstevel@tonic-gate * B_READ - indicates read operation
580Sstevel@tonic-gate * B_ASYNC - indicates perform operation asynchronously
590Sstevel@tonic-gate */
600Sstevel@tonic-gate /*ARGSUSED*/
610Sstevel@tonic-gate int
ufs_rdwr_data(vnode_t * vnodep,u_offset_t offset,size_t len,fdbuffer_t * fdbp,int flags,cred_t * credp)620Sstevel@tonic-gate ufs_rdwr_data(
630Sstevel@tonic-gate vnode_t *vnodep,
640Sstevel@tonic-gate u_offset_t offset,
650Sstevel@tonic-gate size_t len,
660Sstevel@tonic-gate fdbuffer_t *fdbp,
670Sstevel@tonic-gate int flags,
680Sstevel@tonic-gate cred_t *credp)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate struct inode *ip = VTOI(vnodep);
710Sstevel@tonic-gate struct fs *fs;
720Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
730Sstevel@tonic-gate struct buf *bp;
740Sstevel@tonic-gate krw_t rwtype = RW_READER;
750Sstevel@tonic-gate u_offset_t offset1 = offset; /* Initial offset */
760Sstevel@tonic-gate size_t iolen;
770Sstevel@tonic-gate int curlen = 0;
780Sstevel@tonic-gate int pplen;
790Sstevel@tonic-gate daddr_t bn;
800Sstevel@tonic-gate int contig = 0;
810Sstevel@tonic-gate int error = 0;
820Sstevel@tonic-gate int nbytes; /* Number bytes this IO */
830Sstevel@tonic-gate int offsetn; /* Start point this IO */
840Sstevel@tonic-gate int iswrite = flags & B_WRITE;
850Sstevel@tonic-gate int io_started = 0; /* No IO started */
860Sstevel@tonic-gate struct ulockfs *ulp;
870Sstevel@tonic-gate uint_t protp = PROT_ALL;
880Sstevel@tonic-gate
890Sstevel@tonic-gate error = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, segkmap, !iswrite,
900Sstevel@tonic-gate &protp);
910Sstevel@tonic-gate if (error) {
920Sstevel@tonic-gate if (flags & B_ASYNC) {
930Sstevel@tonic-gate fdb_ioerrdone(fdbp, error);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate return (error);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate fs = ufsvfsp->vfs_fs;
980Sstevel@tonic-gate iolen = len;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_rdwr: %s vp: %p pages:%p off %llx len %lx"
1010Sstevel@tonic-gate " isize: %llx fdb: %p\n",
1020Sstevel@tonic-gate flags & B_READ ? "READ" : "WRITE", (void *)vnodep,
1030Sstevel@tonic-gate (void *)vnodep->v_pages, offset1, iolen, ip->i_size, (void *)fdbp));
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate rw_enter(&ip->i_ufsvfs->vfs_dqrwlock, RW_READER);
1060Sstevel@tonic-gate rw_enter(&ip->i_contents, rwtype);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate ASSERT(offset1 < ip->i_size);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if ((offset1 + iolen) > ip->i_size) {
1110Sstevel@tonic-gate iolen = ip->i_size - offset1;
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate while (!error && curlen < iolen) {
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate contig = 0;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate if ((error = bmap_read(ip, offset1, &bn, &contig)) != 0) {
1180Sstevel@tonic-gate break;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate ASSERT(!(bn == UFS_HOLE && iswrite));
1210Sstevel@tonic-gate if (bn == UFS_HOLE) {
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate * If the above assertion is true,
1240Sstevel@tonic-gate * then the following if statement can never be true.
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate if (iswrite && (rwtype == RW_READER)) {
1270Sstevel@tonic-gate rwtype = RW_WRITER;
1280Sstevel@tonic-gate if (!rw_tryupgrade(&ip->i_contents)) {
1290Sstevel@tonic-gate rw_exit(&ip->i_contents);
1300Sstevel@tonic-gate rw_enter(&ip->i_contents, rwtype);
1310Sstevel@tonic-gate continue;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate offsetn = blkoff(fs, offset1);
1350Sstevel@tonic-gate pplen = P2ROUNDUP(len, PAGESIZE);
1360Sstevel@tonic-gate nbytes = MIN((pplen - curlen),
1370Sstevel@tonic-gate (fs->fs_bsize - offsetn));
1380Sstevel@tonic-gate ASSERT(nbytes > 0);
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * We may be reading or writing.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_rdwr_data: hole %llx - %lx\n",
1440Sstevel@tonic-gate offset1, (iolen - curlen)));
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if (iswrite) {
1470Sstevel@tonic-gate printf("**WARNING: ignoring hole in write\n");
1480Sstevel@tonic-gate error = ENOSPC;
1490Sstevel@tonic-gate } else {
1500Sstevel@tonic-gate fdb_add_hole(fdbp, offset1 - offset, nbytes);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate offset1 += nbytes;
1530Sstevel@tonic-gate curlen += nbytes;
1540Sstevel@tonic-gate continue;
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate ASSERT(contig > 0);
1580Sstevel@tonic-gate pplen = P2ROUNDUP(len, PAGESIZE);
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate contig = MIN(contig, len - curlen);
1610Sstevel@tonic-gate contig = P2ROUNDUP(contig, DEV_BSIZE);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate bp = fdb_iosetup(fdbp, offset1 - offset, contig, vnodep, flags);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate bp->b_edev = ip->i_dev;
1660Sstevel@tonic-gate bp->b_dev = cmpdev(ip->i_dev);
1670Sstevel@tonic-gate bp->b_blkno = bn;
1680Sstevel@tonic-gate bp->b_file = ip->i_vnode;
1690Sstevel@tonic-gate bp->b_offset = (offset_t)offset1;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (ufsvfsp->vfs_snapshot) {
1720Sstevel@tonic-gate fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
1730Sstevel@tonic-gate } else {
1740Sstevel@tonic-gate (void) bdev_strategy(bp);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate io_started = 1;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate offset1 += contig;
1790Sstevel@tonic-gate curlen += contig;
1800Sstevel@tonic-gate if (iswrite)
1810Sstevel@tonic-gate lwp_stat_update(LWP_STAT_OUBLK, 1);
1820Sstevel@tonic-gate else
1830Sstevel@tonic-gate lwp_stat_update(LWP_STAT_INBLK, 1);
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate if ((flags & B_ASYNC) == 0) {
1860Sstevel@tonic-gate error = biowait(bp);
1870Sstevel@tonic-gate fdb_iodone(bp);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate DEBUGF((CE_CONT, "?loop ufs_rdwr_data.. off %llx len %lx\n",
1910Sstevel@tonic-gate offset1, (iolen - curlen)));
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_rdwr_data: off %llx len %lx pages: %p ------\n",
1950Sstevel@tonic-gate offset1, (iolen - curlen), (void *)vnodep->v_pages));
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate rw_exit(&ip->i_contents);
1980Sstevel@tonic-gate rw_exit(&ip->i_ufsvfs->vfs_dqrwlock);
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (flags & B_ASYNC) {
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * Show that no more asynchronous IO will be added
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate fdb_ioerrdone(fdbp, error);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate if (ulp) {
2070Sstevel@tonic-gate ufs_lockfs_end(ulp);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate if (io_started && flags & B_ASYNC) {
2100Sstevel@tonic-gate return (0);
2110Sstevel@tonic-gate } else {
2120Sstevel@tonic-gate return (error);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * ufs_alloc_data - supports allocating space and reads or writes
2180Sstevel@tonic-gate * that involve changes to file length or space allocation.
2190Sstevel@tonic-gate *
2200Sstevel@tonic-gate * This function is more expensive, because of the UFS log transaction,
2210Sstevel@tonic-gate * so ufs_rdwr_data() should be used when space or file length changes
2220Sstevel@tonic-gate * will not occur.
2230Sstevel@tonic-gate *
2240Sstevel@tonic-gate * Inputs:
2250Sstevel@tonic-gate * fdb - A null pointer instructs this function to only allocate
2260Sstevel@tonic-gate * space for the specified offset and length.
2270Sstevel@tonic-gate * An actual fdbuffer instructs this function to perform
2280Sstevel@tonic-gate * the read or write operation.
2290Sstevel@tonic-gate * flags - defaults (zero value) to synchronous write
2300Sstevel@tonic-gate * B_READ - indicates read operation
2310Sstevel@tonic-gate * B_ASYNC - indicates perform operation asynchronously
2320Sstevel@tonic-gate */
2330Sstevel@tonic-gate int
ufs_alloc_data(vnode_t * vnodep,u_offset_t offset,size_t * len,fdbuffer_t * fdbp,int flags,cred_t * credp)2340Sstevel@tonic-gate ufs_alloc_data(
2350Sstevel@tonic-gate vnode_t *vnodep,
2360Sstevel@tonic-gate u_offset_t offset,
2370Sstevel@tonic-gate size_t *len,
2380Sstevel@tonic-gate fdbuffer_t *fdbp,
2390Sstevel@tonic-gate int flags,
2400Sstevel@tonic-gate cred_t *credp)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate struct inode *ip = VTOI(vnodep);
2430Sstevel@tonic-gate size_t done_len, io_len;
2440Sstevel@tonic-gate int contig;
2450Sstevel@tonic-gate u_offset_t uoff, io_off;
246*5331Samw int error = 0; /* No error occurred */
2470Sstevel@tonic-gate int offsetn; /* Start point this IO */
2480Sstevel@tonic-gate int nbytes; /* Number bytes in this IO */
2490Sstevel@tonic-gate daddr_t bn;
2500Sstevel@tonic-gate struct fs *fs;
2510Sstevel@tonic-gate struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
2520Sstevel@tonic-gate int i_size_changed = 0;
2530Sstevel@tonic-gate u_offset_t old_i_size;
2540Sstevel@tonic-gate struct ulockfs *ulp;
2550Sstevel@tonic-gate int trans_size;
2560Sstevel@tonic-gate int issync; /* UFS Log transaction */
2570Sstevel@tonic-gate /* synchronous when non-zero */
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate int io_started = 0; /* No IO started */
2600Sstevel@tonic-gate uint_t protp = PROT_ALL;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate ASSERT((flags & B_WRITE) == 0);
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate * Obey the lockfs protocol
2660Sstevel@tonic-gate */
2670Sstevel@tonic-gate error = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, segkmap, 0, &protp);
2680Sstevel@tonic-gate if (error) {
2690Sstevel@tonic-gate if ((fdbp != NULL) && (flags & B_ASYNC)) {
2700Sstevel@tonic-gate fdb_ioerrdone(fdbp, error);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate return (error);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate if (ulp) {
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate * Try to begin a UFS log transaction
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate trans_size = TOP_GETPAGE_SIZE(ip);
2790Sstevel@tonic-gate TRANS_TRY_BEGIN_CSYNC(ufsvfsp, issync, TOP_GETPAGE,
2800Sstevel@tonic-gate trans_size, error);
2810Sstevel@tonic-gate if (error == EWOULDBLOCK) {
2820Sstevel@tonic-gate ufs_lockfs_end(ulp);
2830Sstevel@tonic-gate if ((fdbp != NULL) && (flags & B_ASYNC)) {
2840Sstevel@tonic-gate fdb_ioerrdone(fdbp, EDEADLK);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate return (EDEADLK);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate uoff = offset;
2910Sstevel@tonic-gate io_off = offset;
2920Sstevel@tonic-gate io_len = *len;
2930Sstevel@tonic-gate done_len = 0;
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_alloc: off %llx len %lx size %llx fdb: %p\n",
2960Sstevel@tonic-gate uoff, (io_len - done_len), ip->i_size, (void *)fdbp));
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate rw_enter(&ip->i_ufsvfs->vfs_dqrwlock, RW_READER);
2990Sstevel@tonic-gate rw_enter(&ip->i_contents, RW_WRITER);
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate ASSERT((ip->i_mode & IFMT) == IFREG);
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate fs = ip->i_fs;
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate while (error == 0 && done_len < io_len) {
3060Sstevel@tonic-gate uoff = (u_offset_t)(io_off + done_len);
3070Sstevel@tonic-gate offsetn = (int)blkoff(fs, uoff);
3080Sstevel@tonic-gate nbytes = (int)MIN(fs->fs_bsize - offsetn, io_len - done_len);
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_alloc_data: offset: %llx len %x\n",
3110Sstevel@tonic-gate uoff, nbytes));
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate if (uoff + nbytes > ip->i_size) {
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * We are extending the length of the file.
3160Sstevel@tonic-gate * bmap is used so that we are sure that
3170Sstevel@tonic-gate * if we need to allocate new blocks, that it
3180Sstevel@tonic-gate * is done here before we up the file size.
3190Sstevel@tonic-gate */
3200Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_alloc_data: grow %llx -> %llx\n",
3210Sstevel@tonic-gate ip->i_size, uoff + nbytes));
3220Sstevel@tonic-gate
323923Ssdebnath error = bmap_write(ip, uoff, (offsetn + nbytes),
324923Ssdebnath BI_ALLOC_ONLY, NULL, credp);
3250Sstevel@tonic-gate if (ip->i_flag & (ICHG|IUPD))
3260Sstevel@tonic-gate ip->i_seq++;
3270Sstevel@tonic-gate if (error) {
3280Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_alloc_data: grow "
3290Sstevel@tonic-gate "failed err: %d\n", error));
3300Sstevel@tonic-gate break;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate if (fdbp != NULL) {
3330Sstevel@tonic-gate if (uoff >= ip->i_size) {
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate * Desired offset is past end of bytes
3360Sstevel@tonic-gate * in file, so we have a hole.
3370Sstevel@tonic-gate */
3380Sstevel@tonic-gate fdb_add_hole(fdbp, uoff - offset,
3390Sstevel@tonic-gate nbytes);
3400Sstevel@tonic-gate } else {
3410Sstevel@tonic-gate int contig;
3420Sstevel@tonic-gate buf_t *bp;
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate error = bmap_read(ip, uoff, &bn,
3450Sstevel@tonic-gate &contig);
3460Sstevel@tonic-gate if (error) {
3470Sstevel@tonic-gate break;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate contig = ip->i_size - uoff;
3510Sstevel@tonic-gate contig = P2ROUNDUP(contig, DEV_BSIZE);
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate bp = fdb_iosetup(fdbp, uoff - offset,
3540Sstevel@tonic-gate contig, vnodep, flags);
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate bp->b_edev = ip->i_dev;
3570Sstevel@tonic-gate bp->b_dev = cmpdev(ip->i_dev);
3580Sstevel@tonic-gate bp->b_blkno = bn;
3590Sstevel@tonic-gate bp->b_file = ip->i_vnode;
3600Sstevel@tonic-gate bp->b_offset = (offset_t)uoff;
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate if (ufsvfsp->vfs_snapshot) {
3630Sstevel@tonic-gate fssnap_strategy(
3640Sstevel@tonic-gate &ufsvfsp->vfs_snapshot, bp);
3650Sstevel@tonic-gate } else {
3660Sstevel@tonic-gate (void) bdev_strategy(bp);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate io_started = 1;
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate lwp_stat_update(LWP_STAT_OUBLK, 1);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate if ((flags & B_ASYNC) == 0) {
3730Sstevel@tonic-gate error = biowait(bp);
3740Sstevel@tonic-gate fdb_iodone(bp);
3750Sstevel@tonic-gate if (error) {
3760Sstevel@tonic-gate break;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate if (contig > (ip->i_size - uoff)) {
3800Sstevel@tonic-gate contig -= ip->i_size - uoff;
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate fdb_add_hole(fdbp,
3830Sstevel@tonic-gate ip->i_size - offset,
3840Sstevel@tonic-gate contig);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate i_size_changed = 1;
3900Sstevel@tonic-gate old_i_size = ip->i_size;
3910Sstevel@tonic-gate UFS_SET_ISIZE(uoff + nbytes, ip);
3920Sstevel@tonic-gate TRANS_INODE(ip->i_ufsvfs, ip);
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate * file has grown larger than 2GB. Set flag
3950Sstevel@tonic-gate * in superblock to indicate this, if it
3960Sstevel@tonic-gate * is not already set.
3970Sstevel@tonic-gate */
3980Sstevel@tonic-gate if ((ip->i_size > MAXOFF32_T) &&
3990Sstevel@tonic-gate !(fs->fs_flags & FSLARGEFILES)) {
4000Sstevel@tonic-gate ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
4010Sstevel@tonic-gate mutex_enter(&ufsvfsp->vfs_lock);
4020Sstevel@tonic-gate fs->fs_flags |= FSLARGEFILES;
4030Sstevel@tonic-gate ufs_sbwrite(ufsvfsp);
4040Sstevel@tonic-gate mutex_exit(&ufsvfsp->vfs_lock);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate } else {
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate * The file length is not being extended.
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate error = bmap_read(ip, uoff, &bn, &contig);
4110Sstevel@tonic-gate if (error) {
4120Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_alloc_data: "
4130Sstevel@tonic-gate "bmap_read err: %d\n", error));
4140Sstevel@tonic-gate break;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate if (bn != UFS_HOLE) {
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate * Did not map a hole in the file
4200Sstevel@tonic-gate */
4210Sstevel@tonic-gate int contig = P2ROUNDUP(nbytes, DEV_BSIZE);
4220Sstevel@tonic-gate buf_t *bp;
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate if (fdbp != NULL) {
4250Sstevel@tonic-gate bp = fdb_iosetup(fdbp, uoff - offset,
4260Sstevel@tonic-gate contig, vnodep, flags);
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate bp->b_edev = ip->i_dev;
4290Sstevel@tonic-gate bp->b_dev = cmpdev(ip->i_dev);
4300Sstevel@tonic-gate bp->b_blkno = bn;
4310Sstevel@tonic-gate bp->b_file = ip->i_vnode;
4320Sstevel@tonic-gate bp->b_offset = (offset_t)uoff;
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate if (ufsvfsp->vfs_snapshot) {
4350Sstevel@tonic-gate fssnap_strategy(
4360Sstevel@tonic-gate &ufsvfsp->vfs_snapshot, bp);
4370Sstevel@tonic-gate } else {
4380Sstevel@tonic-gate (void) bdev_strategy(bp);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate io_started = 1;
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate lwp_stat_update(LWP_STAT_OUBLK, 1);
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate if ((flags & B_ASYNC) == 0) {
4450Sstevel@tonic-gate error = biowait(bp);
4460Sstevel@tonic-gate fdb_iodone(bp);
4470Sstevel@tonic-gate if (error) {
4480Sstevel@tonic-gate break;
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate } else {
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate * We read a hole in the file.
4550Sstevel@tonic-gate * We have to allocate blocks for the hole.
4560Sstevel@tonic-gate */
4570Sstevel@tonic-gate error = bmap_write(ip, uoff, (offsetn + nbytes),
458923Ssdebnath BI_ALLOC_ONLY, NULL, credp);
4590Sstevel@tonic-gate if (ip->i_flag & (ICHG|IUPD))
4600Sstevel@tonic-gate ip->i_seq++;
4610Sstevel@tonic-gate if (error) {
4620Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_alloc_data: fill"
4630Sstevel@tonic-gate " hole failed error: %d\n", error));
4640Sstevel@tonic-gate break;
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate if (fdbp != NULL) {
4670Sstevel@tonic-gate fdb_add_hole(fdbp, uoff - offset,
4680Sstevel@tonic-gate nbytes);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate done_len += nbytes;
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate if (error) {
4760Sstevel@tonic-gate if (i_size_changed) {
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * Allocation of the blocks for the file failed.
4790Sstevel@tonic-gate * So truncate the file size back to its original size.
4800Sstevel@tonic-gate */
4810Sstevel@tonic-gate (void) ufs_itrunc(ip, old_i_size, 0, credp);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate DEBUGF((CE_CONT, "?ufs_alloc: uoff %llx len %lx\n",
4860Sstevel@tonic-gate uoff, (io_len - done_len)));
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate if ((offset + *len) < (NDADDR * fs->fs_bsize)) {
4890Sstevel@tonic-gate *len = (size_t)(roundup(offset + *len, fs->fs_fsize) - offset);
4900Sstevel@tonic-gate } else {
4910Sstevel@tonic-gate *len = (size_t)(roundup(offset + *len, fs->fs_bsize) - offset);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate * Flush cached pages.
4960Sstevel@tonic-gate *
4970Sstevel@tonic-gate * XXX - There should be no pages involved, since the I/O was performed
4980Sstevel@tonic-gate * through the device strategy routine and the page cache was bypassed.
4990Sstevel@tonic-gate * However, testing has demonstrated that this VOP_PUTPAGE is
5000Sstevel@tonic-gate * necessary. Without this, data might not always be read back as it
5010Sstevel@tonic-gate * was written.
5020Sstevel@tonic-gate *
5030Sstevel@tonic-gate */
504*5331Samw (void) VOP_PUTPAGE(vnodep, 0, 0, B_INVAL, credp, NULL);
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate rw_exit(&ip->i_contents);
5070Sstevel@tonic-gate rw_exit(&ip->i_ufsvfs->vfs_dqrwlock);
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate if ((fdbp != NULL) && (flags & B_ASYNC)) {
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate * Show that no more asynchronous IO will be added
5120Sstevel@tonic-gate */
5130Sstevel@tonic-gate fdb_ioerrdone(fdbp, error);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate if (ulp) {
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate * End the UFS Log transaction
5180Sstevel@tonic-gate */
5190Sstevel@tonic-gate TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_GETPAGE,
5200Sstevel@tonic-gate trans_size);
5210Sstevel@tonic-gate ufs_lockfs_end(ulp);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate if (io_started && (flags & B_ASYNC)) {
5240Sstevel@tonic-gate return (0);
5250Sstevel@tonic-gate } else {
5260Sstevel@tonic-gate return (error);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate }
529