xref: /linux/fs/xfs/xfs_iomap.c (revision 36ca1195ad7f760a6af3814cb002bd3a3d4b4db1)
1 /*
2  * Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.	 Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 
33 #include "xfs.h"
34 
35 #include "xfs_fs.h"
36 #include "xfs_inum.h"
37 #include "xfs_log.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_ag.h"
41 #include "xfs_dir.h"
42 #include "xfs_dir2.h"
43 #include "xfs_alloc.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_quota.h"
46 #include "xfs_mount.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_bmap_btree.h"
49 #include "xfs_ialloc_btree.h"
50 #include "xfs_btree.h"
51 #include "xfs_ialloc.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode.h"
57 #include "xfs_bmap.h"
58 #include "xfs_bit.h"
59 #include "xfs_rtalloc.h"
60 #include "xfs_error.h"
61 #include "xfs_itable.h"
62 #include "xfs_rw.h"
63 #include "xfs_acl.h"
64 #include "xfs_cap.h"
65 #include "xfs_mac.h"
66 #include "xfs_attr.h"
67 #include "xfs_buf_item.h"
68 #include "xfs_trans_space.h"
69 #include "xfs_utils.h"
70 #include "xfs_iomap.h"
71 
72 #if defined(XFS_RW_TRACE)
73 void
74 xfs_iomap_enter_trace(
75 	int		tag,
76 	xfs_iocore_t	*io,
77 	xfs_off_t	offset,
78 	ssize_t		count)
79 {
80 	xfs_inode_t	*ip = XFS_IO_INODE(io);
81 
82 	if (!ip->i_rwtrace)
83 		return;
84 
85 	ktrace_enter(ip->i_rwtrace,
86 		(void *)((unsigned long)tag),
87 		(void *)ip,
88 		(void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
89 		(void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
90 		(void *)((unsigned long)((offset >> 32) & 0xffffffff)),
91 		(void *)((unsigned long)(offset & 0xffffffff)),
92 		(void *)((unsigned long)count),
93 		(void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
94 		(void *)((unsigned long)(io->io_new_size & 0xffffffff)),
95 		(void *)NULL,
96 		(void *)NULL,
97 		(void *)NULL,
98 		(void *)NULL,
99 		(void *)NULL,
100 		(void *)NULL,
101 		(void *)NULL);
102 }
103 
104 void
105 xfs_iomap_map_trace(
106 	int		tag,
107 	xfs_iocore_t	*io,
108 	xfs_off_t	offset,
109 	ssize_t		count,
110 	xfs_iomap_t	*iomapp,
111 	xfs_bmbt_irec_t	*imapp,
112 	int		flags)
113 {
114 	xfs_inode_t	*ip = XFS_IO_INODE(io);
115 
116 	if (!ip->i_rwtrace)
117 		return;
118 
119 	ktrace_enter(ip->i_rwtrace,
120 		(void *)((unsigned long)tag),
121 		(void *)ip,
122 		(void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
123 		(void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
124 		(void *)((unsigned long)((offset >> 32) & 0xffffffff)),
125 		(void *)((unsigned long)(offset & 0xffffffff)),
126 		(void *)((unsigned long)count),
127 		(void *)((unsigned long)flags),
128 		(void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
129 		(void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
130 		(void *)((unsigned long)(iomapp->iomap_delta)),
131 		(void *)((unsigned long)(iomapp->iomap_bsize)),
132 		(void *)((unsigned long)(iomapp->iomap_bn)),
133 		(void *)(__psint_t)(imapp->br_startoff),
134 		(void *)((unsigned long)(imapp->br_blockcount)),
135 		(void *)(__psint_t)(imapp->br_startblock));
136 }
137 #else
138 #define xfs_iomap_enter_trace(tag, io, offset, count)
139 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
140 #endif
141 
142 #define XFS_WRITEIO_ALIGN(mp,off)	(((off) >> mp->m_writeio_log) \
143 						<< mp->m_writeio_log)
144 #define XFS_STRAT_WRITE_IMAPS	2
145 #define XFS_WRITE_IMAPS		XFS_BMAP_MAX_NMAP
146 
147 STATIC int
148 xfs_imap_to_bmap(
149 	xfs_iocore_t	*io,
150 	xfs_off_t	offset,
151 	xfs_bmbt_irec_t *imap,
152 	xfs_iomap_t	*iomapp,
153 	int		imaps,			/* Number of imap entries */
154 	int		iomaps,			/* Number of iomap entries */
155 	int		flags)
156 {
157 	xfs_mount_t	*mp;
158 	xfs_fsize_t	nisize;
159 	int		pbm;
160 	xfs_fsblock_t	start_block;
161 
162 	mp = io->io_mount;
163 	nisize = XFS_SIZE(mp, io);
164 	if (io->io_new_size > nisize)
165 		nisize = io->io_new_size;
166 
167 	for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
168 		iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
169 		iomapp->iomap_delta = offset - iomapp->iomap_offset;
170 		iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
171 		iomapp->iomap_flags = flags;
172 
173 		if (io->io_flags & XFS_IOCORE_RT) {
174 			iomapp->iomap_flags |= IOMAP_REALTIME;
175 			iomapp->iomap_target = mp->m_rtdev_targp;
176 		} else {
177 			iomapp->iomap_target = mp->m_ddev_targp;
178 		}
179 		start_block = imap->br_startblock;
180 		if (start_block == HOLESTARTBLOCK) {
181 			iomapp->iomap_bn = IOMAP_DADDR_NULL;
182 			iomapp->iomap_flags |= IOMAP_HOLE;
183 		} else if (start_block == DELAYSTARTBLOCK) {
184 			iomapp->iomap_bn = IOMAP_DADDR_NULL;
185 			iomapp->iomap_flags |= IOMAP_DELAY;
186 		} else {
187 			iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
188 			if (ISUNWRITTEN(imap))
189 				iomapp->iomap_flags |= IOMAP_UNWRITTEN;
190 		}
191 
192 		if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
193 			iomapp->iomap_flags |= IOMAP_EOF;
194 		}
195 
196 		offset += iomapp->iomap_bsize - iomapp->iomap_delta;
197 	}
198 	return pbm;	/* Return the number filled */
199 }
200 
201 int
202 xfs_iomap(
203 	xfs_iocore_t	*io,
204 	xfs_off_t	offset,
205 	ssize_t		count,
206 	int		flags,
207 	xfs_iomap_t	*iomapp,
208 	int		*niomaps)
209 {
210 	xfs_mount_t	*mp = io->io_mount;
211 	xfs_fileoff_t	offset_fsb, end_fsb;
212 	int		error = 0;
213 	int		lockmode = 0;
214 	xfs_bmbt_irec_t	imap;
215 	int		nimaps = 1;
216 	int		bmapi_flags = 0;
217 	int		iomap_flags = 0;
218 
219 	if (XFS_FORCED_SHUTDOWN(mp))
220 		return XFS_ERROR(EIO);
221 
222 	switch (flags &
223 		(BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
224 		 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
225 	case BMAPI_READ:
226 		xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
227 		lockmode = XFS_LCK_MAP_SHARED(mp, io);
228 		bmapi_flags = XFS_BMAPI_ENTIRE;
229 		if (flags & BMAPI_IGNSTATE)
230 			bmapi_flags |= XFS_BMAPI_IGSTATE;
231 		break;
232 	case BMAPI_WRITE:
233 		xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
234 		lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
235 		bmapi_flags = 0;
236 		XFS_ILOCK(mp, io, lockmode);
237 		break;
238 	case BMAPI_ALLOCATE:
239 		xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
240 		lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
241 		bmapi_flags = XFS_BMAPI_ENTIRE;
242 		/* Attempt non-blocking lock */
243 		if (flags & BMAPI_TRYLOCK) {
244 			if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
245 				return XFS_ERROR(EAGAIN);
246 		} else {
247 			XFS_ILOCK(mp, io, lockmode);
248 		}
249 		break;
250 	case BMAPI_UNWRITTEN:
251 		goto phase2;
252 	case BMAPI_DEVICE:
253 		lockmode = XFS_LCK_MAP_SHARED(mp, io);
254 		iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
255 			mp->m_rtdev_targp : mp->m_ddev_targp;
256 		error = 0;
257 		*niomaps = 1;
258 		goto out;
259 	default:
260 		BUG();
261 	}
262 
263 	ASSERT(offset <= mp->m_maxioffset);
264 	if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
265 		count = mp->m_maxioffset - offset;
266 	end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
267 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
268 
269 	error = XFS_BMAPI(mp, NULL, io, offset_fsb,
270 			(xfs_filblks_t)(end_fsb - offset_fsb),
271 			bmapi_flags,  NULL, 0, &imap,
272 			&nimaps, NULL);
273 
274 	if (error)
275 		goto out;
276 
277 phase2:
278 	switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
279 	case BMAPI_WRITE:
280 		/* If we found an extent, return it */
281 		if (nimaps &&
282 		    (imap.br_startblock != HOLESTARTBLOCK) &&
283 		    (imap.br_startblock != DELAYSTARTBLOCK)) {
284 			xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
285 					offset, count, iomapp, &imap, flags);
286 			break;
287 		}
288 
289 		if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
290 			error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
291 					count, flags, &imap, &nimaps, nimaps);
292 		} else {
293 			error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
294 					flags, &imap, &nimaps);
295 		}
296 		if (!error) {
297 			xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
298 					offset, count, iomapp, &imap, flags);
299 		}
300 		iomap_flags = IOMAP_NEW;
301 		break;
302 	case BMAPI_ALLOCATE:
303 		/* If we found an extent, return it */
304 		XFS_IUNLOCK(mp, io, lockmode);
305 		lockmode = 0;
306 
307 		if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
308 			xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
309 					offset, count, iomapp, &imap, flags);
310 			break;
311 		}
312 
313 		error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
314 						 &imap, &nimaps);
315 		break;
316 	case BMAPI_UNWRITTEN:
317 		lockmode = 0;
318 		error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
319 		nimaps = 0;
320 		break;
321 	}
322 
323 	if (nimaps) {
324 		*niomaps = xfs_imap_to_bmap(io, offset, &imap,
325 					    iomapp, nimaps, *niomaps, iomap_flags);
326 	} else if (niomaps) {
327 		*niomaps = 0;
328 	}
329 
330 out:
331 	if (lockmode)
332 		XFS_IUNLOCK(mp, io, lockmode);
333 	return XFS_ERROR(error);
334 }
335 
336 STATIC int
337 xfs_flush_space(
338 	xfs_inode_t	*ip,
339 	int		*fsynced,
340 	int		*ioflags)
341 {
342 	switch (*fsynced) {
343 	case 0:
344 		if (ip->i_delayed_blks) {
345 			xfs_iunlock(ip, XFS_ILOCK_EXCL);
346 			xfs_flush_inode(ip);
347 			xfs_ilock(ip, XFS_ILOCK_EXCL);
348 			*fsynced = 1;
349 		} else {
350 			*ioflags |= BMAPI_SYNC;
351 			*fsynced = 2;
352 		}
353 		return 0;
354 	case 1:
355 		*fsynced = 2;
356 		*ioflags |= BMAPI_SYNC;
357 		return 0;
358 	case 2:
359 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
360 		xfs_flush_device(ip);
361 		xfs_ilock(ip, XFS_ILOCK_EXCL);
362 		*fsynced = 3;
363 		return 0;
364 	}
365 	return 1;
366 }
367 
368 int
369 xfs_iomap_write_direct(
370 	xfs_inode_t	*ip,
371 	xfs_off_t	offset,
372 	size_t		count,
373 	int		flags,
374 	xfs_bmbt_irec_t *ret_imap,
375 	int		*nmaps,
376 	int		found)
377 {
378 	xfs_mount_t	*mp = ip->i_mount;
379 	xfs_iocore_t	*io = &ip->i_iocore;
380 	xfs_fileoff_t	offset_fsb;
381 	xfs_fileoff_t	last_fsb;
382 	xfs_filblks_t	count_fsb;
383 	xfs_fsize_t	isize;
384 	xfs_fsblock_t	firstfsb;
385 	int		nimaps, maps;
386 	int		error;
387 	int		bmapi_flag;
388 	int		rt;
389 	xfs_trans_t	*tp;
390 	xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS], *imapp;
391 	xfs_bmap_free_t free_list;
392 	int		aeof;
393 	xfs_filblks_t	datablocks;
394 	int		committed;
395 	int		numrtextents;
396 	uint		resblks;
397 
398 	/*
399 	 * Make sure that the dquots are there. This doesn't hold
400 	 * the ilock across a disk read.
401 	 */
402 	error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
403 	if (error)
404 		return XFS_ERROR(error);
405 
406 	maps = min(XFS_WRITE_IMAPS, *nmaps);
407 	nimaps = maps;
408 
409 	isize = ip->i_d.di_size;
410 	aeof = (offset + count) > isize;
411 
412 	if (io->io_new_size > isize)
413 		isize = io->io_new_size;
414 
415 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
416 	last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
417 	count_fsb = last_fsb - offset_fsb;
418 	if (found && (ret_imap->br_startblock == HOLESTARTBLOCK)) {
419 		xfs_fileoff_t	map_last_fsb;
420 
421 		map_last_fsb = ret_imap->br_blockcount + ret_imap->br_startoff;
422 
423 		if (map_last_fsb < last_fsb) {
424 			last_fsb = map_last_fsb;
425 			count_fsb = last_fsb - offset_fsb;
426 		}
427 		ASSERT(count_fsb > 0);
428 	}
429 
430 	/*
431 	 * determine if reserving space on
432 	 * the data or realtime partition.
433 	 */
434 	if ((rt = XFS_IS_REALTIME_INODE(ip))) {
435 		int	sbrtextsize, iprtextsize;
436 
437 		sbrtextsize = mp->m_sb.sb_rextsize;
438 		iprtextsize =
439 			ip->i_d.di_extsize ? ip->i_d.di_extsize : sbrtextsize;
440 		numrtextents = (count_fsb + iprtextsize - 1);
441 		do_div(numrtextents, sbrtextsize);
442 		datablocks = 0;
443 	} else {
444 		datablocks = count_fsb;
445 		numrtextents = 0;
446 	}
447 
448 	/*
449 	 * allocate and setup the transaction
450 	 */
451 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
452 	tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
453 
454 	resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
455 
456 	error = xfs_trans_reserve(tp, resblks,
457 			XFS_WRITE_LOG_RES(mp), numrtextents,
458 			XFS_TRANS_PERM_LOG_RES,
459 			XFS_WRITE_LOG_COUNT);
460 
461 	/*
462 	 * check for running out of space
463 	 */
464 	if (error)
465 		/*
466 		 * Free the transaction structure.
467 		 */
468 		xfs_trans_cancel(tp, 0);
469 
470 	xfs_ilock(ip, XFS_ILOCK_EXCL);
471 
472 	if (error)
473 		goto error_out; /* Don't return in above if .. trans ..,
474 					need lock to return */
475 
476 	if (XFS_TRANS_RESERVE_BLKQUOTA(mp, tp, ip, resblks)) {
477 		error = (EDQUOT);
478 		goto error1;
479 	}
480 	nimaps = 1;
481 
482 	bmapi_flag = XFS_BMAPI_WRITE;
483 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
484 	xfs_trans_ihold(tp, ip);
485 
486 	if (!(flags & BMAPI_MMAP) && (offset < ip->i_d.di_size || rt))
487 		bmapi_flag |= XFS_BMAPI_PREALLOC;
488 
489 	/*
490 	 * issue the bmapi() call to allocate the blocks
491 	 */
492 	XFS_BMAP_INIT(&free_list, &firstfsb);
493 	imapp = &imap[0];
494 	error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
495 		bmapi_flag, &firstfsb, 0, imapp, &nimaps, &free_list);
496 	if (error) {
497 		goto error0;
498 	}
499 
500 	/*
501 	 * complete the transaction
502 	 */
503 
504 	error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
505 	if (error) {
506 		goto error0;
507 	}
508 
509 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
510 	if (error) {
511 		goto error_out;
512 	}
513 
514 	/* copy any maps to caller's array and return any error. */
515 	if (nimaps == 0) {
516 		error = (ENOSPC);
517 		goto error_out;
518 	}
519 
520 	*ret_imap = imap[0];
521 	*nmaps = 1;
522 	if ( !(io->io_flags & XFS_IOCORE_RT)  && !ret_imap->br_startblock) {
523                 cmn_err(CE_PANIC,"Access to block zero:  fs <%s> inode: %lld "
524                         "start_block : %llx start_off : %llx blkcnt : %llx "
525                         "extent-state : %x \n",
526                         (ip->i_mount)->m_fsname,
527                         (long long)ip->i_ino,
528                         ret_imap->br_startblock, ret_imap->br_startoff,
529                         ret_imap->br_blockcount,ret_imap->br_state);
530         }
531 	return 0;
532 
533  error0:	/* Cancel bmap, unlock inode, and cancel trans */
534 	xfs_bmap_cancel(&free_list);
535 
536  error1:	/* Just cancel transaction */
537 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
538 	*nmaps = 0;	/* nothing set-up here */
539 
540 error_out:
541 	return XFS_ERROR(error);
542 }
543 
544 int
545 xfs_iomap_write_delay(
546 	xfs_inode_t	*ip,
547 	xfs_off_t	offset,
548 	size_t		count,
549 	int		ioflag,
550 	xfs_bmbt_irec_t *ret_imap,
551 	int		*nmaps)
552 {
553 	xfs_mount_t	*mp = ip->i_mount;
554 	xfs_iocore_t	*io = &ip->i_iocore;
555 	xfs_fileoff_t	offset_fsb;
556 	xfs_fileoff_t	last_fsb;
557 	xfs_fsize_t	isize;
558 	xfs_fsblock_t	firstblock;
559 	int		nimaps;
560 	int		error;
561 	xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
562 	int		aeof;
563 	int		fsynced = 0;
564 
565 	ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
566 
567 	/*
568 	 * Make sure that the dquots are there. This doesn't hold
569 	 * the ilock across a disk read.
570 	 */
571 
572 	error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
573 	if (error)
574 		return XFS_ERROR(error);
575 
576 retry:
577 	isize = ip->i_d.di_size;
578 	if (io->io_new_size > isize) {
579 		isize = io->io_new_size;
580 	}
581 
582 	aeof = 0;
583 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
584 	last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
585 	/*
586 	 * If the caller is doing a write at the end of the file,
587 	 * then extend the allocation (and the buffer used for the write)
588 	 * out to the file system's write iosize.  We clean up any extra
589 	 * space left over when the file is closed in xfs_inactive().
590 	 *
591 	 * For sync writes, we are flushing delayed allocate space to
592 	 * try to make additional space available for allocation near
593 	 * the filesystem full boundary - preallocation hurts in that
594 	 * situation, of course.
595 	 */
596 	if (!(ioflag & BMAPI_SYNC) && ((offset + count) > ip->i_d.di_size)) {
597 		xfs_off_t	aligned_offset;
598 		xfs_filblks_t   count_fsb;
599 		unsigned int	iosize;
600 		xfs_fileoff_t	ioalign;
601 		int		n;
602 		xfs_fileoff_t   start_fsb;
603 
604 		/*
605 		 * If there are any real blocks past eof, then don't
606 		 * do any speculative allocation.
607 		 */
608 		start_fsb = XFS_B_TO_FSBT(mp,
609 					((xfs_ufsize_t)(offset + count - 1)));
610 		count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
611 		while (count_fsb > 0) {
612 			nimaps = XFS_WRITE_IMAPS;
613 			error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb,
614 					0, &firstblock, 0, imap, &nimaps, NULL);
615 			if (error) {
616 				return error;
617 			}
618 			for (n = 0; n < nimaps; n++) {
619 				if ( !(io->io_flags & XFS_IOCORE_RT)  &&
620 					!imap[n].br_startblock) {
621 					cmn_err(CE_PANIC,"Access to block "
622 						"zero:  fs <%s> inode: %lld "
623 						"start_block : %llx start_off "
624 						": %llx blkcnt : %llx "
625 						"extent-state : %x \n",
626 						(ip->i_mount)->m_fsname,
627 						(long long)ip->i_ino,
628 						imap[n].br_startblock,
629 						imap[n].br_startoff,
630 						imap[n].br_blockcount,
631 						imap[n].br_state);
632         			}
633 				if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
634 				    (imap[n].br_startblock != DELAYSTARTBLOCK)) {
635 					goto write_map;
636 				}
637 				start_fsb += imap[n].br_blockcount;
638 				count_fsb -= imap[n].br_blockcount;
639 			}
640 		}
641 		iosize = mp->m_writeio_blocks;
642 		aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
643 		ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
644 		last_fsb = ioalign + iosize;
645 		aeof = 1;
646 	}
647 write_map:
648 	nimaps = XFS_WRITE_IMAPS;
649 	firstblock = NULLFSBLOCK;
650 
651 	/*
652 	 * If mounted with the "-o swalloc" option, roundup the allocation
653 	 * request to a stripe width boundary if the file size is >=
654 	 * stripe width and we are allocating past the allocation eof.
655 	 */
656 	if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_swidth
657 	    && (mp->m_flags & XFS_MOUNT_SWALLOC)
658 	    && (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)) && aeof) {
659 		int eof;
660 		xfs_fileoff_t new_last_fsb;
661 
662 		new_last_fsb = roundup_64(last_fsb, mp->m_swidth);
663 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
664 		if (error) {
665 			return error;
666 		}
667 		if (eof) {
668 			last_fsb = new_last_fsb;
669 		}
670 	/*
671 	 * Roundup the allocation request to a stripe unit (m_dalign) boundary
672 	 * if the file size is >= stripe unit size, and we are allocating past
673 	 * the allocation eof.
674 	 */
675 	} else if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_dalign &&
676 		   (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)) && aeof) {
677 		int eof;
678 		xfs_fileoff_t new_last_fsb;
679 		new_last_fsb = roundup_64(last_fsb, mp->m_dalign);
680 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
681 		if (error) {
682 			return error;
683 		}
684 		if (eof) {
685 			last_fsb = new_last_fsb;
686 		}
687 	/*
688 	 * Round up the allocation request to a real-time extent boundary
689 	 * if the file is on the real-time subvolume.
690 	 */
691 	} else if (io->io_flags & XFS_IOCORE_RT && aeof) {
692 		int eof;
693 		xfs_fileoff_t new_last_fsb;
694 
695 		new_last_fsb = roundup_64(last_fsb, mp->m_sb.sb_rextsize);
696 		error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
697 		if (error) {
698 			return error;
699 		}
700 		if (eof)
701 			last_fsb = new_last_fsb;
702 	}
703 	error = xfs_bmapi(NULL, ip, offset_fsb,
704 			  (xfs_filblks_t)(last_fsb - offset_fsb),
705 			  XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
706 			  XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
707 			  &nimaps, NULL);
708 	/*
709 	 * This can be EDQUOT, if nimaps == 0
710 	 */
711 	if (error && (error != ENOSPC)) {
712 		return XFS_ERROR(error);
713 	}
714 	/*
715 	 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
716 	 * then we must have run out of space.
717 	 */
718 	if (nimaps == 0) {
719 		xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
720 					io, offset, count);
721 		if (xfs_flush_space(ip, &fsynced, &ioflag))
722 			return XFS_ERROR(ENOSPC);
723 
724 		error = 0;
725 		goto retry;
726 	}
727 
728 	*ret_imap = imap[0];
729 	*nmaps = 1;
730 	if ( !(io->io_flags & XFS_IOCORE_RT)  && !ret_imap->br_startblock) {
731 		cmn_err(CE_PANIC,"Access to block zero:  fs <%s> inode: %lld "
732                         "start_block : %llx start_off : %llx blkcnt : %llx "
733                         "extent-state : %x \n",
734                         (ip->i_mount)->m_fsname,
735                         (long long)ip->i_ino,
736                         ret_imap->br_startblock, ret_imap->br_startoff,
737                         ret_imap->br_blockcount,ret_imap->br_state);
738 	}
739 	return 0;
740 }
741 
742 /*
743  * Pass in a delayed allocate extent, convert it to real extents;
744  * return to the caller the extent we create which maps on top of
745  * the originating callers request.
746  *
747  * Called without a lock on the inode.
748  */
749 int
750 xfs_iomap_write_allocate(
751 	xfs_inode_t	*ip,
752 	xfs_off_t	offset,
753 	size_t		count,
754 	xfs_bmbt_irec_t *map,
755 	int		*retmap)
756 {
757 	xfs_mount_t	*mp = ip->i_mount;
758 	xfs_iocore_t    *io = &ip->i_iocore;
759 	xfs_fileoff_t	offset_fsb, last_block;
760 	xfs_fileoff_t	end_fsb, map_start_fsb;
761 	xfs_fsblock_t	first_block;
762 	xfs_bmap_free_t	free_list;
763 	xfs_filblks_t	count_fsb;
764 	xfs_bmbt_irec_t	imap[XFS_STRAT_WRITE_IMAPS];
765 	xfs_trans_t	*tp;
766 	int		i, nimaps, committed;
767 	int		error = 0;
768 	int		nres;
769 
770 	*retmap = 0;
771 
772 	/*
773 	 * Make sure that the dquots are there.
774 	 */
775 	if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
776 		return XFS_ERROR(error);
777 
778 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
779 	count_fsb = map->br_blockcount;
780 	map_start_fsb = map->br_startoff;
781 
782 	XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
783 
784 	while (count_fsb != 0) {
785 		/*
786 		 * Set up a transaction with which to allocate the
787 		 * backing store for the file.  Do allocations in a
788 		 * loop until we get some space in the range we are
789 		 * interested in.  The other space that might be allocated
790 		 * is in the delayed allocation extent on which we sit
791 		 * but before our buffer starts.
792 		 */
793 
794 		nimaps = 0;
795 		while (nimaps == 0) {
796 			tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
797 			nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
798 			error = xfs_trans_reserve(tp, nres,
799 					XFS_WRITE_LOG_RES(mp),
800 					0, XFS_TRANS_PERM_LOG_RES,
801 					XFS_WRITE_LOG_COUNT);
802 			if (error == ENOSPC) {
803 				error = xfs_trans_reserve(tp, 0,
804 						XFS_WRITE_LOG_RES(mp),
805 						0,
806 						XFS_TRANS_PERM_LOG_RES,
807 						XFS_WRITE_LOG_COUNT);
808 			}
809 			if (error) {
810 				xfs_trans_cancel(tp, 0);
811 				return XFS_ERROR(error);
812 			}
813 			xfs_ilock(ip, XFS_ILOCK_EXCL);
814 			xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
815 			xfs_trans_ihold(tp, ip);
816 
817 			XFS_BMAP_INIT(&free_list, &first_block);
818 
819 			nimaps = XFS_STRAT_WRITE_IMAPS;
820 			/*
821 			 * Ensure we don't go beyond eof - it is possible
822 			 * the extents changed since we did the read call,
823 			 * we dropped the ilock in the interim.
824 			 */
825 
826 			end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size);
827 			xfs_bmap_last_offset(NULL, ip, &last_block,
828 				XFS_DATA_FORK);
829 			last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
830 			if ((map_start_fsb + count_fsb) > last_block) {
831 				count_fsb = last_block - map_start_fsb;
832 				if (count_fsb == 0) {
833 					error = EAGAIN;
834 					goto trans_cancel;
835 				}
836 			}
837 
838 			/* Go get the actual blocks */
839 			error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
840 					XFS_BMAPI_WRITE, &first_block, 1,
841 					imap, &nimaps, &free_list);
842 			if (error)
843 				goto trans_cancel;
844 
845 			error = xfs_bmap_finish(&tp, &free_list,
846 					first_block, &committed);
847 			if (error)
848 				goto trans_cancel;
849 
850 			error = xfs_trans_commit(tp,
851 					XFS_TRANS_RELEASE_LOG_RES, NULL);
852 			if (error)
853 				goto error0;
854 
855 			xfs_iunlock(ip, XFS_ILOCK_EXCL);
856 		}
857 
858 		/*
859 		 * See if we were able to allocate an extent that
860 		 * covers at least part of the callers request
861 		 */
862 
863 		for (i = 0; i < nimaps; i++) {
864 			if ( !(io->io_flags & XFS_IOCORE_RT)  &&
865 				!imap[i].br_startblock) {
866 				cmn_err(CE_PANIC,"Access to block zero:  "
867 					"fs <%s> inode: %lld "
868 					"start_block : %llx start_off : %llx "
869 					"blkcnt : %llx extent-state : %x \n",
870 					(ip->i_mount)->m_fsname,
871 					(long long)ip->i_ino,
872 					imap[i].br_startblock,
873 					imap[i].br_startoff,
874 				        imap[i].br_blockcount,imap[i].br_state);
875                         }
876 			if ((offset_fsb >= imap[i].br_startoff) &&
877 			    (offset_fsb < (imap[i].br_startoff +
878 					   imap[i].br_blockcount))) {
879 				*map = imap[i];
880 				*retmap = 1;
881 				XFS_STATS_INC(xs_xstrat_quick);
882 				return 0;
883 			}
884 			count_fsb -= imap[i].br_blockcount;
885 		}
886 
887 		/* So far we have not mapped the requested part of the
888 		 * file, just surrounding data, try again.
889 		 */
890 		nimaps--;
891 		map_start_fsb = imap[nimaps].br_startoff +
892 				imap[nimaps].br_blockcount;
893 	}
894 
895 trans_cancel:
896 	xfs_bmap_cancel(&free_list);
897 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
898 error0:
899 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
900 	return XFS_ERROR(error);
901 }
902 
903 int
904 xfs_iomap_write_unwritten(
905 	xfs_inode_t	*ip,
906 	xfs_off_t	offset,
907 	size_t		count)
908 {
909 	xfs_mount_t	*mp = ip->i_mount;
910 	xfs_iocore_t    *io = &ip->i_iocore;
911 	xfs_trans_t	*tp;
912 	xfs_fileoff_t	offset_fsb;
913 	xfs_filblks_t	count_fsb;
914 	xfs_filblks_t	numblks_fsb;
915 	xfs_bmbt_irec_t	imap;
916 	int		committed;
917 	int		error;
918 	int		nres;
919 	int		nimaps;
920 	xfs_fsblock_t	firstfsb;
921 	xfs_bmap_free_t	free_list;
922 
923 	xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
924 				&ip->i_iocore, offset, count);
925 
926 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
927 	count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
928 	count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
929 
930 	do {
931 		nres = XFS_DIOSTRAT_SPACE_RES(mp, 0);
932 
933 		/*
934 		 * set up a transaction to convert the range of extents
935 		 * from unwritten to real. Do allocations in a loop until
936 		 * we have covered the range passed in.
937 		 */
938 
939 		tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
940 		error = xfs_trans_reserve(tp, nres,
941 				XFS_WRITE_LOG_RES(mp), 0,
942 				XFS_TRANS_PERM_LOG_RES,
943 				XFS_WRITE_LOG_COUNT);
944 		if (error) {
945 			xfs_trans_cancel(tp, 0);
946 			goto error0;
947 		}
948 
949 		xfs_ilock(ip, XFS_ILOCK_EXCL);
950 		xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
951 		xfs_trans_ihold(tp, ip);
952 
953 		/*
954 		 * Modify the unwritten extent state of the buffer.
955 		 */
956 		XFS_BMAP_INIT(&free_list, &firstfsb);
957 		nimaps = 1;
958 		error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
959 				  XFS_BMAPI_WRITE, &firstfsb,
960 				  1, &imap, &nimaps, &free_list);
961 		if (error)
962 			goto error_on_bmapi_transaction;
963 
964 		error = xfs_bmap_finish(&(tp), &(free_list),
965 				firstfsb, &committed);
966 		if (error)
967 			goto error_on_bmapi_transaction;
968 
969 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
970 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
971 		if (error)
972 			goto error0;
973 
974 		if ( !(io->io_flags & XFS_IOCORE_RT)  && !imap.br_startblock) {
975 			cmn_err(CE_PANIC,"Access to block zero:  fs <%s> "
976 				"inode: %lld start_block : %llx start_off : "
977 				"%llx blkcnt : %llx extent-state : %x \n",
978 				(ip->i_mount)->m_fsname,
979 				(long long)ip->i_ino,
980 				imap.br_startblock,imap.br_startoff,
981 				imap.br_blockcount,imap.br_state);
982         	}
983 
984 		if ((numblks_fsb = imap.br_blockcount) == 0) {
985 			/*
986 			 * The numblks_fsb value should always get
987 			 * smaller, otherwise the loop is stuck.
988 			 */
989 			ASSERT(imap.br_blockcount);
990 			break;
991 		}
992 		offset_fsb += numblks_fsb;
993 		count_fsb -= numblks_fsb;
994 	} while (count_fsb > 0);
995 
996 	return 0;
997 
998 error_on_bmapi_transaction:
999 	xfs_bmap_cancel(&free_list);
1000 	xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
1001 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
1002 error0:
1003 	return XFS_ERROR(error);
1004 }
1005