xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_extvnops.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/param.h>
31*0Sstevel@tonic-gate #include <sys/sysmacros.h>
32*0Sstevel@tonic-gate #include <sys/conf.h>
33*0Sstevel@tonic-gate #include <sys/fssnap_if.h>
34*0Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
35*0Sstevel@tonic-gate #include <sys/fs/ufs_lockfs.h>
36*0Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
37*0Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
38*0Sstevel@tonic-gate #include <sys/cmn_err.h>
39*0Sstevel@tonic-gate #include <vm/pvn.h>
40*0Sstevel@tonic-gate #include <vm/seg_map.h>
41*0Sstevel@tonic-gate #include <sys/fdbuffer.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #ifdef DEBUG
44*0Sstevel@tonic-gate int evn_ufs_debug = 0;
45*0Sstevel@tonic-gate #define	DEBUGF(args)	{ if (evn_ufs_debug) cmn_err args; }
46*0Sstevel@tonic-gate #else
47*0Sstevel@tonic-gate #define	DEBUGF(args)
48*0Sstevel@tonic-gate #endif
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * ufs_rdwr_data - supports reading or writing data when
52*0Sstevel@tonic-gate  * no changes are permitted in file size or space allocation.
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  * Inputs:
55*0Sstevel@tonic-gate  * fdb - The mandatory fdbuffer supports
56*0Sstevel@tonic-gate  *	the read or write operation.
57*0Sstevel@tonic-gate  * flags - defaults (zero value) to synchronous write
58*0Sstevel@tonic-gate  *	B_READ - indicates read operation
59*0Sstevel@tonic-gate  *	B_ASYNC - indicates perform operation asynchronously
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate /*ARGSUSED*/
62*0Sstevel@tonic-gate int
63*0Sstevel@tonic-gate ufs_rdwr_data(
64*0Sstevel@tonic-gate 	vnode_t		*vnodep,
65*0Sstevel@tonic-gate 	u_offset_t	offset,
66*0Sstevel@tonic-gate 	size_t		len,
67*0Sstevel@tonic-gate 	fdbuffer_t	*fdbp,
68*0Sstevel@tonic-gate 	int		flags,
69*0Sstevel@tonic-gate 	cred_t		*credp)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	struct inode	*ip = VTOI(vnodep);
72*0Sstevel@tonic-gate 	struct fs	*fs;
73*0Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
74*0Sstevel@tonic-gate 	struct buf	*bp;
75*0Sstevel@tonic-gate 	krw_t		rwtype = RW_READER;
76*0Sstevel@tonic-gate 	u_offset_t	offset1 = offset;	/* Initial offset */
77*0Sstevel@tonic-gate 	size_t		iolen;
78*0Sstevel@tonic-gate 	int		curlen = 0;
79*0Sstevel@tonic-gate 	int		pplen;
80*0Sstevel@tonic-gate 	daddr_t		bn;
81*0Sstevel@tonic-gate 	int		contig = 0;
82*0Sstevel@tonic-gate 	int		error = 0;
83*0Sstevel@tonic-gate 	int		nbytes;			/* Number bytes this IO */
84*0Sstevel@tonic-gate 	int		offsetn;		/* Start point this IO */
85*0Sstevel@tonic-gate 	int		iswrite = flags & B_WRITE;
86*0Sstevel@tonic-gate 	int		io_started = 0;		/* No IO started */
87*0Sstevel@tonic-gate 	struct ulockfs	*ulp;
88*0Sstevel@tonic-gate 	uint_t		protp = PROT_ALL;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	error = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, segkmap, !iswrite,
91*0Sstevel@tonic-gate 	    &protp);
92*0Sstevel@tonic-gate 	if (error) {
93*0Sstevel@tonic-gate 		if (flags & B_ASYNC) {
94*0Sstevel@tonic-gate 			fdb_ioerrdone(fdbp, error);
95*0Sstevel@tonic-gate 		}
96*0Sstevel@tonic-gate 		return (error);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
99*0Sstevel@tonic-gate 	iolen = len;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	DEBUGF((CE_CONT, "?ufs_rdwr: %s vp: %p pages:%p  off %llx len %lx"
102*0Sstevel@tonic-gate 	    " isize: %llx fdb: %p\n",
103*0Sstevel@tonic-gate 	    flags & B_READ ? "READ" : "WRITE", (void *)vnodep,
104*0Sstevel@tonic-gate 	    (void *)vnodep->v_pages, offset1, iolen, ip->i_size, (void *)fdbp));
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	rw_enter(&ip->i_ufsvfs->vfs_dqrwlock, RW_READER);
107*0Sstevel@tonic-gate 	rw_enter(&ip->i_contents, rwtype);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	ASSERT(offset1 < ip->i_size);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if ((offset1 + iolen) > ip->i_size) {
112*0Sstevel@tonic-gate 		iolen = ip->i_size - offset1;
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 	while (!error && curlen < iolen) {
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		contig = 0;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 		if ((error = bmap_read(ip, offset1, &bn, &contig)) != 0) {
119*0Sstevel@tonic-gate 			break;
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 		ASSERT(!(bn == UFS_HOLE && iswrite));
122*0Sstevel@tonic-gate 		if (bn == UFS_HOLE) {
123*0Sstevel@tonic-gate 			/*
124*0Sstevel@tonic-gate 			 * If the above assertion is true,
125*0Sstevel@tonic-gate 			 * then the following if statement can never be true.
126*0Sstevel@tonic-gate 			 */
127*0Sstevel@tonic-gate 			if (iswrite && (rwtype == RW_READER)) {
128*0Sstevel@tonic-gate 				rwtype = RW_WRITER;
129*0Sstevel@tonic-gate 				if (!rw_tryupgrade(&ip->i_contents)) {
130*0Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
131*0Sstevel@tonic-gate 					rw_enter(&ip->i_contents, rwtype);
132*0Sstevel@tonic-gate 					continue;
133*0Sstevel@tonic-gate 				}
134*0Sstevel@tonic-gate 			}
135*0Sstevel@tonic-gate 			offsetn = blkoff(fs, offset1);
136*0Sstevel@tonic-gate 			pplen = P2ROUNDUP(len, PAGESIZE);
137*0Sstevel@tonic-gate 			nbytes = MIN((pplen - curlen),
138*0Sstevel@tonic-gate 			    (fs->fs_bsize - offsetn));
139*0Sstevel@tonic-gate 			ASSERT(nbytes > 0);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 			/*
142*0Sstevel@tonic-gate 			 * We may be reading or writing.
143*0Sstevel@tonic-gate 			 */
144*0Sstevel@tonic-gate 			DEBUGF((CE_CONT, "?ufs_rdwr_data: hole %llx - %lx\n",
145*0Sstevel@tonic-gate 			    offset1, (iolen - curlen)));
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 			if (iswrite) {
148*0Sstevel@tonic-gate 				printf("**WARNING: ignoring hole in write\n");
149*0Sstevel@tonic-gate 				error = ENOSPC;
150*0Sstevel@tonic-gate 			} else {
151*0Sstevel@tonic-gate 				fdb_add_hole(fdbp, offset1 - offset, nbytes);
152*0Sstevel@tonic-gate 			}
153*0Sstevel@tonic-gate 			offset1 += nbytes;
154*0Sstevel@tonic-gate 			curlen += nbytes;
155*0Sstevel@tonic-gate 			continue;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 		ASSERT(contig > 0);
159*0Sstevel@tonic-gate 		pplen = P2ROUNDUP(len, PAGESIZE);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 		contig = MIN(contig, len - curlen);
162*0Sstevel@tonic-gate 		contig = P2ROUNDUP(contig, DEV_BSIZE);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		bp = fdb_iosetup(fdbp, offset1 - offset, contig, vnodep, flags);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
167*0Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
168*0Sstevel@tonic-gate 		bp->b_blkno = bn;
169*0Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
170*0Sstevel@tonic-gate 		bp->b_offset = (offset_t)offset1;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 		if (ufsvfsp->vfs_snapshot) {
173*0Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
174*0Sstevel@tonic-gate 		} else {
175*0Sstevel@tonic-gate 			(void) bdev_strategy(bp);
176*0Sstevel@tonic-gate 		}
177*0Sstevel@tonic-gate 		io_started = 1;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 		offset1 += contig;
180*0Sstevel@tonic-gate 		curlen += contig;
181*0Sstevel@tonic-gate 		if (iswrite)
182*0Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
183*0Sstevel@tonic-gate 		else
184*0Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		if ((flags & B_ASYNC) == 0) {
187*0Sstevel@tonic-gate 			error = biowait(bp);
188*0Sstevel@tonic-gate 			fdb_iodone(bp);
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		DEBUGF((CE_CONT, "?loop ufs_rdwr_data.. off %llx len %lx\n",
192*0Sstevel@tonic-gate 		    offset1, (iolen - curlen)));
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	DEBUGF((CE_CONT, "?ufs_rdwr_data: off %llx len %lx pages: %p ------\n",
196*0Sstevel@tonic-gate 	    offset1, (iolen - curlen), (void *)vnodep->v_pages));
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
199*0Sstevel@tonic-gate 	rw_exit(&ip->i_ufsvfs->vfs_dqrwlock);
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (flags & B_ASYNC) {
202*0Sstevel@tonic-gate 		/*
203*0Sstevel@tonic-gate 		 * Show that no more asynchronous IO will be added
204*0Sstevel@tonic-gate 		 */
205*0Sstevel@tonic-gate 		fdb_ioerrdone(fdbp, error);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 	if (ulp) {
208*0Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 	if (io_started && flags & B_ASYNC) {
211*0Sstevel@tonic-gate 		return (0);
212*0Sstevel@tonic-gate 	} else {
213*0Sstevel@tonic-gate 		return (error);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*
218*0Sstevel@tonic-gate  * ufs_alloc_data - supports allocating space and reads or writes
219*0Sstevel@tonic-gate  * that involve changes to file length or space allocation.
220*0Sstevel@tonic-gate  *
221*0Sstevel@tonic-gate  * This function is more expensive, because of the UFS log transaction,
222*0Sstevel@tonic-gate  * so ufs_rdwr_data() should be used when space or file length changes
223*0Sstevel@tonic-gate  * will not occur.
224*0Sstevel@tonic-gate  *
225*0Sstevel@tonic-gate  * Inputs:
226*0Sstevel@tonic-gate  * fdb - A null pointer instructs this function to only allocate
227*0Sstevel@tonic-gate  *	space for the specified offset and length.
228*0Sstevel@tonic-gate  *	An actual fdbuffer instructs this function to perform
229*0Sstevel@tonic-gate  *	the read or write operation.
230*0Sstevel@tonic-gate  * flags - defaults (zero value) to synchronous write
231*0Sstevel@tonic-gate  *	B_READ - indicates read operation
232*0Sstevel@tonic-gate  *	B_ASYNC - indicates perform operation asynchronously
233*0Sstevel@tonic-gate  */
234*0Sstevel@tonic-gate int
235*0Sstevel@tonic-gate ufs_alloc_data(
236*0Sstevel@tonic-gate 	vnode_t		*vnodep,
237*0Sstevel@tonic-gate 	u_offset_t	offset,
238*0Sstevel@tonic-gate 	size_t		*len,
239*0Sstevel@tonic-gate 	fdbuffer_t	*fdbp,
240*0Sstevel@tonic-gate 	int		flags,
241*0Sstevel@tonic-gate 	cred_t		*credp)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	struct inode	*ip = VTOI(vnodep);
244*0Sstevel@tonic-gate 	size_t		done_len, io_len;
245*0Sstevel@tonic-gate 	int		contig;
246*0Sstevel@tonic-gate 	u_offset_t	uoff, io_off;
247*0Sstevel@tonic-gate 	int		error = 0;		/* No error occured */
248*0Sstevel@tonic-gate 	int		offsetn;		/* Start point this IO */
249*0Sstevel@tonic-gate 	int		nbytes;			/* Number bytes in this IO */
250*0Sstevel@tonic-gate 	daddr_t		bn;
251*0Sstevel@tonic-gate 	struct fs	*fs;
252*0Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
253*0Sstevel@tonic-gate 	int		i_size_changed = 0;
254*0Sstevel@tonic-gate 	u_offset_t	old_i_size;
255*0Sstevel@tonic-gate 	struct ulockfs	*ulp;
256*0Sstevel@tonic-gate 	int		trans_size;
257*0Sstevel@tonic-gate 	int		issync;			/* UFS Log transaction */
258*0Sstevel@tonic-gate 						/* synchronous when non-zero */
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	int		io_started = 0;		/* No IO started */
261*0Sstevel@tonic-gate 	uint_t		protp = PROT_ALL;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	ASSERT((flags & B_WRITE) == 0);
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	/*
266*0Sstevel@tonic-gate 	 * Obey the lockfs protocol
267*0Sstevel@tonic-gate 	 */
268*0Sstevel@tonic-gate 	error = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, segkmap, 0, &protp);
269*0Sstevel@tonic-gate 	if (error) {
270*0Sstevel@tonic-gate 		if ((fdbp != NULL) && (flags & B_ASYNC)) {
271*0Sstevel@tonic-gate 			fdb_ioerrdone(fdbp, error);
272*0Sstevel@tonic-gate 		}
273*0Sstevel@tonic-gate 		return (error);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 	if (ulp) {
276*0Sstevel@tonic-gate 		/*
277*0Sstevel@tonic-gate 		 * Try to begin a UFS log transaction
278*0Sstevel@tonic-gate 		 */
279*0Sstevel@tonic-gate 		trans_size = TOP_GETPAGE_SIZE(ip);
280*0Sstevel@tonic-gate 		TRANS_TRY_BEGIN_CSYNC(ufsvfsp, issync, TOP_GETPAGE,
281*0Sstevel@tonic-gate 		    trans_size, error);
282*0Sstevel@tonic-gate 		if (error == EWOULDBLOCK) {
283*0Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
284*0Sstevel@tonic-gate 			if ((fdbp != NULL) && (flags & B_ASYNC)) {
285*0Sstevel@tonic-gate 				fdb_ioerrdone(fdbp, EDEADLK);
286*0Sstevel@tonic-gate 			}
287*0Sstevel@tonic-gate 			return (EDEADLK);
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	uoff = offset;
292*0Sstevel@tonic-gate 	io_off = offset;
293*0Sstevel@tonic-gate 	io_len = *len;
294*0Sstevel@tonic-gate 	done_len = 0;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	DEBUGF((CE_CONT, "?ufs_alloc: off %llx len %lx size %llx fdb: %p\n",
297*0Sstevel@tonic-gate 	    uoff, (io_len - done_len), ip->i_size, (void *)fdbp));
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	rw_enter(&ip->i_ufsvfs->vfs_dqrwlock, RW_READER);
300*0Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	ASSERT((ip->i_mode & IFMT) == IFREG);
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	fs = ip->i_fs;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	while (error == 0 && done_len < io_len) {
307*0Sstevel@tonic-gate 		uoff = (u_offset_t)(io_off + done_len);
308*0Sstevel@tonic-gate 		offsetn = (int)blkoff(fs, uoff);
309*0Sstevel@tonic-gate 		nbytes = (int)MIN(fs->fs_bsize - offsetn, io_len - done_len);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 		DEBUGF((CE_CONT, "?ufs_alloc_data: offset: %llx len %x\n",
312*0Sstevel@tonic-gate 		    uoff, nbytes));
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 		if (uoff + nbytes > ip->i_size) {
315*0Sstevel@tonic-gate 			/*
316*0Sstevel@tonic-gate 			 * We are extending the length of the file.
317*0Sstevel@tonic-gate 			 * bmap is used so that we are sure that
318*0Sstevel@tonic-gate 			 * if we need to allocate new blocks, that it
319*0Sstevel@tonic-gate 			 * is done here before we up the file size.
320*0Sstevel@tonic-gate 			 */
321*0Sstevel@tonic-gate 			DEBUGF((CE_CONT, "?ufs_alloc_data: grow %llx -> %llx\n",
322*0Sstevel@tonic-gate 			    ip->i_size, uoff + nbytes));
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 			error = bmap_write(ip, uoff, (offsetn + nbytes), 1,
325*0Sstevel@tonic-gate 			    credp);
326*0Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD))
327*0Sstevel@tonic-gate 				ip->i_seq++;
328*0Sstevel@tonic-gate 			if (error) {
329*0Sstevel@tonic-gate 				DEBUGF((CE_CONT, "?ufs_alloc_data: grow "
330*0Sstevel@tonic-gate 				    "failed err: %d\n", error));
331*0Sstevel@tonic-gate 				break;
332*0Sstevel@tonic-gate 			}
333*0Sstevel@tonic-gate 			if (fdbp != NULL) {
334*0Sstevel@tonic-gate 				if (uoff >= ip->i_size) {
335*0Sstevel@tonic-gate 					/*
336*0Sstevel@tonic-gate 					 * Desired offset is past end of bytes
337*0Sstevel@tonic-gate 					 * in file, so we have a hole.
338*0Sstevel@tonic-gate 					 */
339*0Sstevel@tonic-gate 					fdb_add_hole(fdbp, uoff - offset,
340*0Sstevel@tonic-gate 					    nbytes);
341*0Sstevel@tonic-gate 				} else {
342*0Sstevel@tonic-gate 					int contig;
343*0Sstevel@tonic-gate 					buf_t *bp;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 					error = bmap_read(ip, uoff, &bn,
346*0Sstevel@tonic-gate 					    &contig);
347*0Sstevel@tonic-gate 					if (error) {
348*0Sstevel@tonic-gate 						break;
349*0Sstevel@tonic-gate 					}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 					contig = ip->i_size - uoff;
352*0Sstevel@tonic-gate 					contig = P2ROUNDUP(contig, DEV_BSIZE);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 					bp = fdb_iosetup(fdbp, uoff - offset,
355*0Sstevel@tonic-gate 					    contig, vnodep, flags);
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 					bp->b_edev = ip->i_dev;
358*0Sstevel@tonic-gate 					bp->b_dev = cmpdev(ip->i_dev);
359*0Sstevel@tonic-gate 					bp->b_blkno = bn;
360*0Sstevel@tonic-gate 					bp->b_file = ip->i_vnode;
361*0Sstevel@tonic-gate 					bp->b_offset = (offset_t)uoff;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 					if (ufsvfsp->vfs_snapshot) {
364*0Sstevel@tonic-gate 						fssnap_strategy(
365*0Sstevel@tonic-gate 						    &ufsvfsp->vfs_snapshot, bp);
366*0Sstevel@tonic-gate 					} else {
367*0Sstevel@tonic-gate 						(void) bdev_strategy(bp);
368*0Sstevel@tonic-gate 					}
369*0Sstevel@tonic-gate 					io_started = 1;
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 					lwp_stat_update(LWP_STAT_OUBLK, 1);
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 					if ((flags & B_ASYNC) == 0) {
374*0Sstevel@tonic-gate 						error = biowait(bp);
375*0Sstevel@tonic-gate 						fdb_iodone(bp);
376*0Sstevel@tonic-gate 						if (error) {
377*0Sstevel@tonic-gate 							break;
378*0Sstevel@tonic-gate 						}
379*0Sstevel@tonic-gate 					}
380*0Sstevel@tonic-gate 					if (contig > (ip->i_size - uoff)) {
381*0Sstevel@tonic-gate 						contig -= ip->i_size - uoff;
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 						fdb_add_hole(fdbp,
384*0Sstevel@tonic-gate 						    ip->i_size - offset,
385*0Sstevel@tonic-gate 						    contig);
386*0Sstevel@tonic-gate 					}
387*0Sstevel@tonic-gate 				}
388*0Sstevel@tonic-gate 			}
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 			i_size_changed = 1;
391*0Sstevel@tonic-gate 			old_i_size = ip->i_size;
392*0Sstevel@tonic-gate 			UFS_SET_ISIZE(uoff + nbytes, ip);
393*0Sstevel@tonic-gate 			TRANS_INODE(ip->i_ufsvfs, ip);
394*0Sstevel@tonic-gate 			/*
395*0Sstevel@tonic-gate 			 * file has grown larger than 2GB. Set flag
396*0Sstevel@tonic-gate 			 * in superblock to indicate this, if it
397*0Sstevel@tonic-gate 			 * is not already set.
398*0Sstevel@tonic-gate 			 */
399*0Sstevel@tonic-gate 			if ((ip->i_size > MAXOFF32_T) &&
400*0Sstevel@tonic-gate 			    !(fs->fs_flags & FSLARGEFILES)) {
401*0Sstevel@tonic-gate 				ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
402*0Sstevel@tonic-gate 				mutex_enter(&ufsvfsp->vfs_lock);
403*0Sstevel@tonic-gate 				fs->fs_flags |= FSLARGEFILES;
404*0Sstevel@tonic-gate 				ufs_sbwrite(ufsvfsp);
405*0Sstevel@tonic-gate 				mutex_exit(&ufsvfsp->vfs_lock);
406*0Sstevel@tonic-gate 			}
407*0Sstevel@tonic-gate 		} else {
408*0Sstevel@tonic-gate 			/*
409*0Sstevel@tonic-gate 			 * The file length is not being extended.
410*0Sstevel@tonic-gate 			 */
411*0Sstevel@tonic-gate 			error = bmap_read(ip, uoff, &bn, &contig);
412*0Sstevel@tonic-gate 			if (error) {
413*0Sstevel@tonic-gate 				DEBUGF((CE_CONT, "?ufs_alloc_data: "
414*0Sstevel@tonic-gate 				    "bmap_read err: %d\n", error));
415*0Sstevel@tonic-gate 				break;
416*0Sstevel@tonic-gate 			}
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 			if (bn != UFS_HOLE) {
419*0Sstevel@tonic-gate 				/*
420*0Sstevel@tonic-gate 				 * Did not map a hole in the file
421*0Sstevel@tonic-gate 				 */
422*0Sstevel@tonic-gate 				int	contig = P2ROUNDUP(nbytes, DEV_BSIZE);
423*0Sstevel@tonic-gate 				buf_t	*bp;
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 				if (fdbp != NULL) {
426*0Sstevel@tonic-gate 					bp = fdb_iosetup(fdbp, uoff - offset,
427*0Sstevel@tonic-gate 					    contig, vnodep, flags);
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 					bp->b_edev = ip->i_dev;
430*0Sstevel@tonic-gate 					bp->b_dev = cmpdev(ip->i_dev);
431*0Sstevel@tonic-gate 					bp->b_blkno = bn;
432*0Sstevel@tonic-gate 					bp->b_file = ip->i_vnode;
433*0Sstevel@tonic-gate 					bp->b_offset = (offset_t)uoff;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 					if (ufsvfsp->vfs_snapshot) {
436*0Sstevel@tonic-gate 						fssnap_strategy(
437*0Sstevel@tonic-gate 						    &ufsvfsp->vfs_snapshot, bp);
438*0Sstevel@tonic-gate 					} else {
439*0Sstevel@tonic-gate 						(void) bdev_strategy(bp);
440*0Sstevel@tonic-gate 					}
441*0Sstevel@tonic-gate 					io_started = 1;
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 					lwp_stat_update(LWP_STAT_OUBLK, 1);
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 					if ((flags & B_ASYNC) == 0) {
446*0Sstevel@tonic-gate 						error = biowait(bp);
447*0Sstevel@tonic-gate 						fdb_iodone(bp);
448*0Sstevel@tonic-gate 						if (error) {
449*0Sstevel@tonic-gate 							break;
450*0Sstevel@tonic-gate 						}
451*0Sstevel@tonic-gate 					}
452*0Sstevel@tonic-gate 				}
453*0Sstevel@tonic-gate 			} else {
454*0Sstevel@tonic-gate 				/*
455*0Sstevel@tonic-gate 				 * We read a hole in the file.
456*0Sstevel@tonic-gate 				 * We have to allocate blocks for the hole.
457*0Sstevel@tonic-gate 				 */
458*0Sstevel@tonic-gate 				error = bmap_write(ip, uoff, (offsetn + nbytes),
459*0Sstevel@tonic-gate 				    1, credp);
460*0Sstevel@tonic-gate 				if (ip->i_flag & (ICHG|IUPD))
461*0Sstevel@tonic-gate 					ip->i_seq++;
462*0Sstevel@tonic-gate 				if (error) {
463*0Sstevel@tonic-gate 					DEBUGF((CE_CONT, "?ufs_alloc_data: fill"
464*0Sstevel@tonic-gate 					    " hole failed error: %d\n", error));
465*0Sstevel@tonic-gate 					break;
466*0Sstevel@tonic-gate 				}
467*0Sstevel@tonic-gate 				if (fdbp != NULL) {
468*0Sstevel@tonic-gate 					fdb_add_hole(fdbp, uoff - offset,
469*0Sstevel@tonic-gate 					    nbytes);
470*0Sstevel@tonic-gate 				}
471*0Sstevel@tonic-gate 			}
472*0Sstevel@tonic-gate 		}
473*0Sstevel@tonic-gate 		done_len += nbytes;
474*0Sstevel@tonic-gate 	}
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	if (error) {
477*0Sstevel@tonic-gate 		if (i_size_changed) {
478*0Sstevel@tonic-gate 			/*
479*0Sstevel@tonic-gate 			 * Allocation of the blocks for the file failed.
480*0Sstevel@tonic-gate 			 * So truncate the file size back to its original size.
481*0Sstevel@tonic-gate 			 */
482*0Sstevel@tonic-gate 			(void) ufs_itrunc(ip, old_i_size, 0, credp);
483*0Sstevel@tonic-gate 		}
484*0Sstevel@tonic-gate 	}
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	DEBUGF((CE_CONT, "?ufs_alloc: uoff %llx len %lx\n",
487*0Sstevel@tonic-gate 	    uoff, (io_len - done_len)));
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	if ((offset + *len) < (NDADDR * fs->fs_bsize)) {
490*0Sstevel@tonic-gate 		*len = (size_t)(roundup(offset + *len, fs->fs_fsize) - offset);
491*0Sstevel@tonic-gate 	} else {
492*0Sstevel@tonic-gate 		*len = (size_t)(roundup(offset + *len, fs->fs_bsize) - offset);
493*0Sstevel@tonic-gate 	}
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	/*
496*0Sstevel@tonic-gate 	 * Flush cached pages.
497*0Sstevel@tonic-gate 	 *
498*0Sstevel@tonic-gate 	 * XXX - There should be no pages involved, since the I/O was performed
499*0Sstevel@tonic-gate 	 * through the device strategy routine and the page cache was bypassed.
500*0Sstevel@tonic-gate 	 * However, testing has demonstrated that this VOP_PUTPAGE is
501*0Sstevel@tonic-gate 	 * necessary. Without this, data might not always be read back as it
502*0Sstevel@tonic-gate 	 * was written.
503*0Sstevel@tonic-gate 	 *
504*0Sstevel@tonic-gate 	 */
505*0Sstevel@tonic-gate 	(void) VOP_PUTPAGE(vnodep, 0, 0, B_INVAL, credp);
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
508*0Sstevel@tonic-gate 	rw_exit(&ip->i_ufsvfs->vfs_dqrwlock);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	if ((fdbp != NULL) && (flags & B_ASYNC)) {
511*0Sstevel@tonic-gate 		/*
512*0Sstevel@tonic-gate 		 * Show that no more asynchronous IO will be added
513*0Sstevel@tonic-gate 		 */
514*0Sstevel@tonic-gate 		fdb_ioerrdone(fdbp, error);
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate 	if (ulp) {
517*0Sstevel@tonic-gate 		/*
518*0Sstevel@tonic-gate 		 * End the UFS Log transaction
519*0Sstevel@tonic-gate 		 */
520*0Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_GETPAGE,
521*0Sstevel@tonic-gate 		    trans_size);
522*0Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
523*0Sstevel@tonic-gate 	}
524*0Sstevel@tonic-gate 	if (io_started && (flags & B_ASYNC)) {
525*0Sstevel@tonic-gate 		return (0);
526*0Sstevel@tonic-gate 	} else {
527*0Sstevel@tonic-gate 		return (error);
528*0Sstevel@tonic-gate 	}
529*0Sstevel@tonic-gate }
530