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