1/*
2 * Copyright (c) 2000-2006 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_shared.h"
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
26#include "xfs_mount.h"
27#include "xfs_inode.h"
28#include "xfs_btree.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_bmap.h"
31#include "xfs_bmap_util.h"
32#include "xfs_error.h"
33#include "xfs_trans.h"
34#include "xfs_trans_space.h"
35#include "xfs_iomap.h"
36#include "xfs_trace.h"
37#include "xfs_icache.h"
38#include "xfs_quota.h"
39#include "xfs_dquot_item.h"
40#include "xfs_dquot.h"
41#include "xfs_dinode.h"
42
43
44#define XFS_WRITEIO_ALIGN(mp,off)	(((off) >> mp->m_writeio_log) \
45						<< mp->m_writeio_log)
46#define XFS_WRITE_IMAPS		XFS_BMAP_MAX_NMAP
47
48STATIC int
49xfs_iomap_eof_align_last_fsb(
50	xfs_mount_t	*mp,
51	xfs_inode_t	*ip,
52	xfs_extlen_t	extsize,
53	xfs_fileoff_t	*last_fsb)
54{
55	xfs_fileoff_t	new_last_fsb = 0;
56	xfs_extlen_t	align = 0;
57	int		eof, error;
58
59	if (!XFS_IS_REALTIME_INODE(ip)) {
60		/*
61		 * Round up the allocation request to a stripe unit
62		 * (m_dalign) boundary if the file size is >= stripe unit
63		 * size, and we are allocating past the allocation eof.
64		 *
65		 * If mounted with the "-o swalloc" option the alignment is
66		 * increased from the strip unit size to the stripe width.
67		 */
68		if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
69			align = mp->m_swidth;
70		else if (mp->m_dalign)
71			align = mp->m_dalign;
72
73		if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
74			new_last_fsb = roundup_64(*last_fsb, align);
75	}
76
77	/*
78	 * Always round up the allocation request to an extent boundary
79	 * (when file on a real-time subvolume or has di_extsize hint).
80	 */
81	if (extsize) {
82		if (new_last_fsb)
83			align = roundup_64(new_last_fsb, extsize);
84		else
85			align = extsize;
86		new_last_fsb = roundup_64(*last_fsb, align);
87	}
88
89	if (new_last_fsb) {
90		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
91		if (error)
92			return error;
93		if (eof)
94			*last_fsb = new_last_fsb;
95	}
96	return 0;
97}
98
99STATIC int
100xfs_alert_fsblock_zero(
101	xfs_inode_t	*ip,
102	xfs_bmbt_irec_t	*imap)
103{
104	xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
105			"Access to block zero in inode %llu "
106			"start_block: %llx start_off: %llx "
107			"blkcnt: %llx extent-state: %x",
108		(unsigned long long)ip->i_ino,
109		(unsigned long long)imap->br_startblock,
110		(unsigned long long)imap->br_startoff,
111		(unsigned long long)imap->br_blockcount,
112		imap->br_state);
113	return -EFSCORRUPTED;
114}
115
116int
117xfs_iomap_write_direct(
118	xfs_inode_t	*ip,
119	xfs_off_t	offset,
120	size_t		count,
121	xfs_bmbt_irec_t *imap,
122	int		nmaps)
123{
124	xfs_mount_t	*mp = ip->i_mount;
125	xfs_fileoff_t	offset_fsb;
126	xfs_fileoff_t	last_fsb;
127	xfs_filblks_t	count_fsb, resaligned;
128	xfs_fsblock_t	firstfsb;
129	xfs_extlen_t	extsz, temp;
130	int		nimaps;
131	int		quota_flag;
132	int		rt;
133	xfs_trans_t	*tp;
134	xfs_bmap_free_t free_list;
135	uint		qblocks, resblks, resrtextents;
136	int		committed;
137	int		error;
138
139	error = xfs_qm_dqattach(ip, 0);
140	if (error)
141		return error;
142
143	rt = XFS_IS_REALTIME_INODE(ip);
144	extsz = xfs_get_extsz_hint(ip);
145
146	offset_fsb = XFS_B_TO_FSBT(mp, offset);
147	last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
148	if ((offset + count) > XFS_ISIZE(ip)) {
149		error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
150		if (error)
151			return error;
152	} else {
153		if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
154			last_fsb = MIN(last_fsb, (xfs_fileoff_t)
155					imap->br_blockcount +
156					imap->br_startoff);
157	}
158	count_fsb = last_fsb - offset_fsb;
159	ASSERT(count_fsb > 0);
160
161	resaligned = count_fsb;
162	if (unlikely(extsz)) {
163		if ((temp = do_mod(offset_fsb, extsz)))
164			resaligned += temp;
165		if ((temp = do_mod(resaligned, extsz)))
166			resaligned += extsz - temp;
167	}
168
169	if (unlikely(rt)) {
170		resrtextents = qblocks = resaligned;
171		resrtextents /= mp->m_sb.sb_rextsize;
172		resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
173		quota_flag = XFS_QMOPT_RES_RTBLKS;
174	} else {
175		resrtextents = 0;
176		resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
177		quota_flag = XFS_QMOPT_RES_REGBLKS;
178	}
179
180	/*
181	 * Allocate and setup the transaction
182	 */
183	tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
184	error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
185				  resblks, resrtextents);
186	/*
187	 * Check for running out of space, note: need lock to return
188	 */
189	if (error) {
190		xfs_trans_cancel(tp, 0);
191		return error;
192	}
193
194	xfs_ilock(ip, XFS_ILOCK_EXCL);
195
196	error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
197	if (error)
198		goto out_trans_cancel;
199
200	xfs_trans_ijoin(tp, ip, 0);
201
202	/*
203	 * From this point onwards we overwrite the imap pointer that the
204	 * caller gave to us.
205	 */
206	xfs_bmap_init(&free_list, &firstfsb);
207	nimaps = 1;
208	error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
209				XFS_BMAPI_PREALLOC, &firstfsb, 0,
210				imap, &nimaps, &free_list);
211	if (error)
212		goto out_bmap_cancel;
213
214	/*
215	 * Complete the transaction
216	 */
217	error = xfs_bmap_finish(&tp, &free_list, &committed);
218	if (error)
219		goto out_bmap_cancel;
220	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
221	if (error)
222		goto out_unlock;
223
224	/*
225	 * Copy any maps to caller's array and return any error.
226	 */
227	if (nimaps == 0) {
228		error = -ENOSPC;
229		goto out_unlock;
230	}
231
232	if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
233		error = xfs_alert_fsblock_zero(ip, imap);
234
235out_unlock:
236	xfs_iunlock(ip, XFS_ILOCK_EXCL);
237	return error;
238
239out_bmap_cancel:
240	xfs_bmap_cancel(&free_list);
241	xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
242out_trans_cancel:
243	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
244	goto out_unlock;
245}
246
247/*
248 * If the caller is doing a write at the end of the file, then extend the
249 * allocation out to the file system's write iosize.  We clean up any extra
250 * space left over when the file is closed in xfs_inactive().
251 *
252 * If we find we already have delalloc preallocation beyond EOF, don't do more
253 * preallocation as it it not needed.
254 */
255STATIC int
256xfs_iomap_eof_want_preallocate(
257	xfs_mount_t	*mp,
258	xfs_inode_t	*ip,
259	xfs_off_t	offset,
260	size_t		count,
261	xfs_bmbt_irec_t *imap,
262	int		nimaps,
263	int		*prealloc)
264{
265	xfs_fileoff_t   start_fsb;
266	xfs_filblks_t   count_fsb;
267	xfs_fsblock_t	firstblock;
268	int		n, error, imaps;
269	int		found_delalloc = 0;
270
271	*prealloc = 0;
272	if (offset + count <= XFS_ISIZE(ip))
273		return 0;
274
275	/*
276	 * If the file is smaller than the minimum prealloc and we are using
277	 * dynamic preallocation, don't do any preallocation at all as it is
278	 * likely this is the only write to the file that is going to be done.
279	 */
280	if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
281	    XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks))
282		return 0;
283
284	/*
285	 * If there are any real blocks past eof, then don't
286	 * do any speculative allocation.
287	 */
288	start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
289	count_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
290	while (count_fsb > 0) {
291		imaps = nimaps;
292		firstblock = NULLFSBLOCK;
293		error = xfs_bmapi_read(ip, start_fsb, count_fsb, imap, &imaps,
294				       0);
295		if (error)
296			return error;
297		for (n = 0; n < imaps; n++) {
298			if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
299			    (imap[n].br_startblock != DELAYSTARTBLOCK))
300				return 0;
301			start_fsb += imap[n].br_blockcount;
302			count_fsb -= imap[n].br_blockcount;
303
304			if (imap[n].br_startblock == DELAYSTARTBLOCK)
305				found_delalloc = 1;
306		}
307	}
308	if (!found_delalloc)
309		*prealloc = 1;
310	return 0;
311}
312
313/*
314 * Determine the initial size of the preallocation. We are beyond the current
315 * EOF here, but we need to take into account whether this is a sparse write or
316 * an extending write when determining the preallocation size.  Hence we need to
317 * look up the extent that ends at the current write offset and use the result
318 * to determine the preallocation size.
319 *
320 * If the extent is a hole, then preallocation is essentially disabled.
321 * Otherwise we take the size of the preceeding data extent as the basis for the
322 * preallocation size. If the size of the extent is greater than half the
323 * maximum extent length, then use the current offset as the basis. This ensures
324 * that for large files the preallocation size always extends to MAXEXTLEN
325 * rather than falling short due to things like stripe unit/width alignment of
326 * real extents.
327 */
328STATIC xfs_fsblock_t
329xfs_iomap_eof_prealloc_initial_size(
330	struct xfs_mount	*mp,
331	struct xfs_inode	*ip,
332	xfs_off_t		offset,
333	xfs_bmbt_irec_t		*imap,
334	int			nimaps)
335{
336	xfs_fileoff_t   start_fsb;
337	int		imaps = 1;
338	int		error;
339
340	ASSERT(nimaps >= imaps);
341
342	/* if we are using a specific prealloc size, return now */
343	if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
344		return 0;
345
346	/* If the file is small, then use the minimum prealloc */
347	if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign))
348		return 0;
349
350	/*
351	 * As we write multiple pages, the offset will always align to the
352	 * start of a page and hence point to a hole at EOF. i.e. if the size is
353	 * 4096 bytes, we only have one block at FSB 0, but XFS_B_TO_FSB(4096)
354	 * will return FSB 1. Hence if there are blocks in the file, we want to
355	 * point to the block prior to the EOF block and not the hole that maps
356	 * directly at @offset.
357	 */
358	start_fsb = XFS_B_TO_FSB(mp, offset);
359	if (start_fsb)
360		start_fsb--;
361	error = xfs_bmapi_read(ip, start_fsb, 1, imap, &imaps, XFS_BMAPI_ENTIRE);
362	if (error)
363		return 0;
364
365	ASSERT(imaps == 1);
366	if (imap[0].br_startblock == HOLESTARTBLOCK)
367		return 0;
368	if (imap[0].br_blockcount <= (MAXEXTLEN >> 1))
369		return imap[0].br_blockcount << 1;
370	return XFS_B_TO_FSB(mp, offset);
371}
372
373STATIC bool
374xfs_quota_need_throttle(
375	struct xfs_inode *ip,
376	int type,
377	xfs_fsblock_t alloc_blocks)
378{
379	struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
380
381	if (!dq || !xfs_this_quota_on(ip->i_mount, type))
382		return false;
383
384	/* no hi watermark, no throttle */
385	if (!dq->q_prealloc_hi_wmark)
386		return false;
387
388	/* under the lo watermark, no throttle */
389	if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
390		return false;
391
392	return true;
393}
394
395STATIC void
396xfs_quota_calc_throttle(
397	struct xfs_inode *ip,
398	int type,
399	xfs_fsblock_t *qblocks,
400	int *qshift,
401	int64_t	*qfreesp)
402{
403	int64_t freesp;
404	int shift = 0;
405	struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
406
407	/* no dq, or over hi wmark, squash the prealloc completely */
408	if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
409		*qblocks = 0;
410		*qfreesp = 0;
411		return;
412	}
413
414	freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
415	if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
416		shift = 2;
417		if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
418			shift += 2;
419		if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
420			shift += 2;
421	}
422
423	if (freesp < *qfreesp)
424		*qfreesp = freesp;
425
426	/* only overwrite the throttle values if we are more aggressive */
427	if ((freesp >> shift) < (*qblocks >> *qshift)) {
428		*qblocks = freesp;
429		*qshift = shift;
430	}
431}
432
433/*
434 * If we don't have a user specified preallocation size, dynamically increase
435 * the preallocation size as the size of the file grows. Cap the maximum size
436 * at a single extent or less if the filesystem is near full. The closer the
437 * filesystem is to full, the smaller the maximum prealocation.
438 */
439STATIC xfs_fsblock_t
440xfs_iomap_prealloc_size(
441	struct xfs_mount	*mp,
442	struct xfs_inode	*ip,
443	xfs_off_t		offset,
444	struct xfs_bmbt_irec	*imap,
445	int			nimaps)
446{
447	xfs_fsblock_t		alloc_blocks = 0;
448	int			shift = 0;
449	int64_t			freesp;
450	xfs_fsblock_t		qblocks;
451	int			qshift = 0;
452
453	alloc_blocks = xfs_iomap_eof_prealloc_initial_size(mp, ip, offset,
454							   imap, nimaps);
455	if (!alloc_blocks)
456		goto check_writeio;
457	qblocks = alloc_blocks;
458
459	/*
460	 * MAXEXTLEN is not a power of two value but we round the prealloc down
461	 * to the nearest power of two value after throttling. To prevent the
462	 * round down from unconditionally reducing the maximum supported prealloc
463	 * size, we round up first, apply appropriate throttling, round down and
464	 * cap the value to MAXEXTLEN.
465	 */
466	alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
467				       alloc_blocks);
468
469	xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
470	freesp = mp->m_sb.sb_fdblocks;
471	if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
472		shift = 2;
473		if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
474			shift++;
475		if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
476			shift++;
477		if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
478			shift++;
479		if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
480			shift++;
481	}
482
483	/*
484	 * Check each quota to cap the prealloc size, provide a shift value to
485	 * throttle with and adjust amount of available space.
486	 */
487	if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
488		xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
489					&freesp);
490	if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
491		xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
492					&freesp);
493	if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
494		xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
495					&freesp);
496
497	/*
498	 * The final prealloc size is set to the minimum of free space available
499	 * in each of the quotas and the overall filesystem.
500	 *
501	 * The shift throttle value is set to the maximum value as determined by
502	 * the global low free space values and per-quota low free space values.
503	 */
504	alloc_blocks = MIN(alloc_blocks, qblocks);
505	shift = MAX(shift, qshift);
506
507	if (shift)
508		alloc_blocks >>= shift;
509	/*
510	 * rounddown_pow_of_two() returns an undefined result if we pass in
511	 * alloc_blocks = 0.
512	 */
513	if (alloc_blocks)
514		alloc_blocks = rounddown_pow_of_two(alloc_blocks);
515	if (alloc_blocks > MAXEXTLEN)
516		alloc_blocks = MAXEXTLEN;
517
518	/*
519	 * If we are still trying to allocate more space than is
520	 * available, squash the prealloc hard. This can happen if we
521	 * have a large file on a small filesystem and the above
522	 * lowspace thresholds are smaller than MAXEXTLEN.
523	 */
524	while (alloc_blocks && alloc_blocks >= freesp)
525		alloc_blocks >>= 4;
526
527check_writeio:
528	if (alloc_blocks < mp->m_writeio_blocks)
529		alloc_blocks = mp->m_writeio_blocks;
530
531	trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
532				      mp->m_writeio_blocks);
533
534	return alloc_blocks;
535}
536
537int
538xfs_iomap_write_delay(
539	xfs_inode_t	*ip,
540	xfs_off_t	offset,
541	size_t		count,
542	xfs_bmbt_irec_t *ret_imap)
543{
544	xfs_mount_t	*mp = ip->i_mount;
545	xfs_fileoff_t	offset_fsb;
546	xfs_fileoff_t	last_fsb;
547	xfs_off_t	aligned_offset;
548	xfs_fileoff_t	ioalign;
549	xfs_extlen_t	extsz;
550	int		nimaps;
551	xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
552	int		prealloc;
553	int		error;
554
555	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
556
557	/*
558	 * Make sure that the dquots are there. This doesn't hold
559	 * the ilock across a disk read.
560	 */
561	error = xfs_qm_dqattach_locked(ip, 0);
562	if (error)
563		return error;
564
565	extsz = xfs_get_extsz_hint(ip);
566	offset_fsb = XFS_B_TO_FSBT(mp, offset);
567
568	error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
569				imap, XFS_WRITE_IMAPS, &prealloc);
570	if (error)
571		return error;
572
573retry:
574	if (prealloc) {
575		xfs_fsblock_t	alloc_blocks;
576
577		alloc_blocks = xfs_iomap_prealloc_size(mp, ip, offset, imap,
578						       XFS_WRITE_IMAPS);
579
580		aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
581		ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
582		last_fsb = ioalign + alloc_blocks;
583	} else {
584		last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
585	}
586
587	if (prealloc || extsz) {
588		error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
589		if (error)
590			return error;
591	}
592
593	/*
594	 * Make sure preallocation does not create extents beyond the range we
595	 * actually support in this filesystem.
596	 */
597	if (last_fsb > XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes))
598		last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
599
600	ASSERT(last_fsb > offset_fsb);
601
602	nimaps = XFS_WRITE_IMAPS;
603	error = xfs_bmapi_delay(ip, offset_fsb, last_fsb - offset_fsb,
604				imap, &nimaps, XFS_BMAPI_ENTIRE);
605	switch (error) {
606	case 0:
607	case -ENOSPC:
608	case -EDQUOT:
609		break;
610	default:
611		return error;
612	}
613
614	/*
615	 * If bmapi returned us nothing, we got either ENOSPC or EDQUOT. Retry
616	 * without EOF preallocation.
617	 */
618	if (nimaps == 0) {
619		trace_xfs_delalloc_enospc(ip, offset, count);
620		if (prealloc) {
621			prealloc = 0;
622			error = 0;
623			goto retry;
624		}
625		return error ? error : -ENOSPC;
626	}
627
628	if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
629		return xfs_alert_fsblock_zero(ip, &imap[0]);
630
631	/*
632	 * Tag the inode as speculatively preallocated so we can reclaim this
633	 * space on demand, if necessary.
634	 */
635	if (prealloc)
636		xfs_inode_set_eofblocks_tag(ip);
637
638	*ret_imap = imap[0];
639	return 0;
640}
641
642/*
643 * Pass in a delayed allocate extent, convert it to real extents;
644 * return to the caller the extent we create which maps on top of
645 * the originating callers request.
646 *
647 * Called without a lock on the inode.
648 *
649 * We no longer bother to look at the incoming map - all we have to
650 * guarantee is that whatever we allocate fills the required range.
651 */
652int
653xfs_iomap_write_allocate(
654	xfs_inode_t	*ip,
655	xfs_off_t	offset,
656	xfs_bmbt_irec_t *imap)
657{
658	xfs_mount_t	*mp = ip->i_mount;
659	xfs_fileoff_t	offset_fsb, last_block;
660	xfs_fileoff_t	end_fsb, map_start_fsb;
661	xfs_fsblock_t	first_block;
662	xfs_bmap_free_t	free_list;
663	xfs_filblks_t	count_fsb;
664	xfs_trans_t	*tp;
665	int		nimaps, committed;
666	int		error = 0;
667	int		nres;
668
669	/*
670	 * Make sure that the dquots are there.
671	 */
672	error = xfs_qm_dqattach(ip, 0);
673	if (error)
674		return error;
675
676	offset_fsb = XFS_B_TO_FSBT(mp, offset);
677	count_fsb = imap->br_blockcount;
678	map_start_fsb = imap->br_startoff;
679
680	XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
681
682	while (count_fsb != 0) {
683		/*
684		 * Set up a transaction with which to allocate the
685		 * backing store for the file.  Do allocations in a
686		 * loop until we get some space in the range we are
687		 * interested in.  The other space that might be allocated
688		 * is in the delayed allocation extent on which we sit
689		 * but before our buffer starts.
690		 */
691
692		nimaps = 0;
693		while (nimaps == 0) {
694			tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
695			tp->t_flags |= XFS_TRANS_RESERVE;
696			nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
697			error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
698						  nres, 0);
699			if (error) {
700				xfs_trans_cancel(tp, 0);
701				return error;
702			}
703			xfs_ilock(ip, XFS_ILOCK_EXCL);
704			xfs_trans_ijoin(tp, ip, 0);
705
706			xfs_bmap_init(&free_list, &first_block);
707
708			/*
709			 * it is possible that the extents have changed since
710			 * we did the read call as we dropped the ilock for a
711			 * while. We have to be careful about truncates or hole
712			 * punchs here - we are not allowed to allocate
713			 * non-delalloc blocks here.
714			 *
715			 * The only protection against truncation is the pages
716			 * for the range we are being asked to convert are
717			 * locked and hence a truncate will block on them
718			 * first.
719			 *
720			 * As a result, if we go beyond the range we really
721			 * need and hit an delalloc extent boundary followed by
722			 * a hole while we have excess blocks in the map, we
723			 * will fill the hole incorrectly and overrun the
724			 * transaction reservation.
725			 *
726			 * Using a single map prevents this as we are forced to
727			 * check each map we look for overlap with the desired
728			 * range and abort as soon as we find it. Also, given
729			 * that we only return a single map, having one beyond
730			 * what we can return is probably a bit silly.
731			 *
732			 * We also need to check that we don't go beyond EOF;
733			 * this is a truncate optimisation as a truncate sets
734			 * the new file size before block on the pages we
735			 * currently have locked under writeback. Because they
736			 * are about to be tossed, we don't need to write them
737			 * back....
738			 */
739			nimaps = 1;
740			end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
741			error = xfs_bmap_last_offset(ip, &last_block,
742							XFS_DATA_FORK);
743			if (error)
744				goto trans_cancel;
745
746			last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
747			if ((map_start_fsb + count_fsb) > last_block) {
748				count_fsb = last_block - map_start_fsb;
749				if (count_fsb == 0) {
750					error = -EAGAIN;
751					goto trans_cancel;
752				}
753			}
754
755			/*
756			 * From this point onwards we overwrite the imap
757			 * pointer that the caller gave to us.
758			 */
759			error = xfs_bmapi_write(tp, ip, map_start_fsb,
760						count_fsb, 0,
761						&first_block, 1,
762						imap, &nimaps, &free_list);
763			if (error)
764				goto trans_cancel;
765
766			error = xfs_bmap_finish(&tp, &free_list, &committed);
767			if (error)
768				goto trans_cancel;
769
770			error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
771			if (error)
772				goto error0;
773
774			xfs_iunlock(ip, XFS_ILOCK_EXCL);
775		}
776
777		/*
778		 * See if we were able to allocate an extent that
779		 * covers at least part of the callers request
780		 */
781		if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
782			return xfs_alert_fsblock_zero(ip, imap);
783
784		if ((offset_fsb >= imap->br_startoff) &&
785		    (offset_fsb < (imap->br_startoff +
786				   imap->br_blockcount))) {
787			XFS_STATS_INC(xs_xstrat_quick);
788			return 0;
789		}
790
791		/*
792		 * So far we have not mapped the requested part of the
793		 * file, just surrounding data, try again.
794		 */
795		count_fsb -= imap->br_blockcount;
796		map_start_fsb = imap->br_startoff + imap->br_blockcount;
797	}
798
799trans_cancel:
800	xfs_bmap_cancel(&free_list);
801	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
802error0:
803	xfs_iunlock(ip, XFS_ILOCK_EXCL);
804	return error;
805}
806
807int
808xfs_iomap_write_unwritten(
809	xfs_inode_t	*ip,
810	xfs_off_t	offset,
811	size_t		count)
812{
813	xfs_mount_t	*mp = ip->i_mount;
814	xfs_fileoff_t	offset_fsb;
815	xfs_filblks_t	count_fsb;
816	xfs_filblks_t	numblks_fsb;
817	xfs_fsblock_t	firstfsb;
818	int		nimaps;
819	xfs_trans_t	*tp;
820	xfs_bmbt_irec_t imap;
821	xfs_bmap_free_t free_list;
822	xfs_fsize_t	i_size;
823	uint		resblks;
824	int		committed;
825	int		error;
826
827	trace_xfs_unwritten_convert(ip, offset, count);
828
829	offset_fsb = XFS_B_TO_FSBT(mp, offset);
830	count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
831	count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
832
833	/*
834	 * Reserve enough blocks in this transaction for two complete extent
835	 * btree splits.  We may be converting the middle part of an unwritten
836	 * extent and in this case we will insert two new extents in the btree
837	 * each of which could cause a full split.
838	 *
839	 * This reservation amount will be used in the first call to
840	 * xfs_bmbt_split() to select an AG with enough space to satisfy the
841	 * rest of the operation.
842	 */
843	resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
844
845	do {
846		/*
847		 * set up a transaction to convert the range of extents
848		 * from unwritten to real. Do allocations in a loop until
849		 * we have covered the range passed in.
850		 *
851		 * Note that we open code the transaction allocation here
852		 * to pass KM_NOFS--we can't risk to recursing back into
853		 * the filesystem here as we might be asked to write out
854		 * the same inode that we complete here and might deadlock
855		 * on the iolock.
856		 */
857		sb_start_intwrite(mp->m_super);
858		tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
859		tp->t_flags |= XFS_TRANS_RESERVE | XFS_TRANS_FREEZE_PROT;
860		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
861					  resblks, 0);
862		if (error) {
863			xfs_trans_cancel(tp, 0);
864			return error;
865		}
866
867		xfs_ilock(ip, XFS_ILOCK_EXCL);
868		xfs_trans_ijoin(tp, ip, 0);
869
870		/*
871		 * Modify the unwritten extent state of the buffer.
872		 */
873		xfs_bmap_init(&free_list, &firstfsb);
874		nimaps = 1;
875		error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
876				  XFS_BMAPI_CONVERT, &firstfsb,
877				  1, &imap, &nimaps, &free_list);
878		if (error)
879			goto error_on_bmapi_transaction;
880
881		/*
882		 * Log the updated inode size as we go.  We have to be careful
883		 * to only log it up to the actual write offset if it is
884		 * halfway into a block.
885		 */
886		i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
887		if (i_size > offset + count)
888			i_size = offset + count;
889
890		i_size = xfs_new_eof(ip, i_size);
891		if (i_size) {
892			ip->i_d.di_size = i_size;
893			xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
894		}
895
896		error = xfs_bmap_finish(&tp, &free_list, &committed);
897		if (error)
898			goto error_on_bmapi_transaction;
899
900		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
901		xfs_iunlock(ip, XFS_ILOCK_EXCL);
902		if (error)
903			return error;
904
905		if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
906			return xfs_alert_fsblock_zero(ip, &imap);
907
908		if ((numblks_fsb = imap.br_blockcount) == 0) {
909			/*
910			 * The numblks_fsb value should always get
911			 * smaller, otherwise the loop is stuck.
912			 */
913			ASSERT(imap.br_blockcount);
914			break;
915		}
916		offset_fsb += numblks_fsb;
917		count_fsb -= numblks_fsb;
918	} while (count_fsb > 0);
919
920	return 0;
921
922error_on_bmapi_transaction:
923	xfs_bmap_cancel(&free_list);
924	xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
925	xfs_iunlock(ip, XFS_ILOCK_EXCL);
926	return error;
927}
928