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
xfs_iomap_enter_trace(int tag,xfs_iocore_t * io,xfs_off_t offset,ssize_t count)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
xfs_iomap_map_trace(int tag,xfs_iocore_t * io,xfs_off_t offset,ssize_t count,xfs_iomap_t * iomapp,xfs_bmbt_irec_t * imapp,int flags)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
xfs_imap_to_bmap(xfs_iocore_t * io,xfs_off_t offset,xfs_bmbt_irec_t * imap,xfs_iomap_t * iomapp,int imaps,int iomaps,int flags)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
xfs_iomap(xfs_iocore_t * io,xfs_off_t offset,ssize_t count,int flags,xfs_iomap_t * iomapp,int * niomaps)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 && (imap.br_startblock != HOLESTARTBLOCK)) {
282 			xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
283 					offset, count, iomapp, &imap, flags);
284 			break;
285 		}
286 
287 		if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
288 			error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
289 					count, flags, &imap, &nimaps, nimaps);
290 		} else {
291 			error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
292 					flags, &imap, &nimaps);
293 		}
294 		if (!error) {
295 			xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
296 					offset, count, iomapp, &imap, flags);
297 		}
298 		iomap_flags = IOMAP_NEW;
299 		break;
300 	case BMAPI_ALLOCATE:
301 		/* If we found an extent, return it */
302 		XFS_IUNLOCK(mp, io, lockmode);
303 		lockmode = 0;
304 
305 		if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
306 			xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
307 					offset, count, iomapp, &imap, flags);
308 			break;
309 		}
310 
311 		error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, &imap, &nimaps);
312 		break;
313 	case BMAPI_UNWRITTEN:
314 		lockmode = 0;
315 		error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
316 		nimaps = 0;
317 		break;
318 	}
319 
320 	if (nimaps) {
321 		*niomaps = xfs_imap_to_bmap(io, offset, &imap,
322 					    iomapp, nimaps, *niomaps, iomap_flags);
323 	} else if (niomaps) {
324 		*niomaps = 0;
325 	}
326 
327 out:
328 	if (lockmode)
329 		XFS_IUNLOCK(mp, io, lockmode);
330 	return XFS_ERROR(error);
331 }
332 
333 STATIC int
xfs_flush_space(xfs_inode_t * ip,int * fsynced,int * ioflags)334 xfs_flush_space(
335 	xfs_inode_t	*ip,
336 	int		*fsynced,
337 	int		*ioflags)
338 {
339 	switch (*fsynced) {
340 	case 0:
341 		if (ip->i_delayed_blks) {
342 			xfs_iunlock(ip, XFS_ILOCK_EXCL);
343 			xfs_flush_inode(ip);
344 			xfs_ilock(ip, XFS_ILOCK_EXCL);
345 			*fsynced = 1;
346 		} else {
347 			*ioflags |= BMAPI_SYNC;
348 			*fsynced = 2;
349 		}
350 		return 0;
351 	case 1:
352 		*fsynced = 2;
353 		*ioflags |= BMAPI_SYNC;
354 		return 0;
355 	case 2:
356 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
357 		xfs_flush_device(ip);
358 		xfs_ilock(ip, XFS_ILOCK_EXCL);
359 		*fsynced = 3;
360 		return 0;
361 	}
362 	return 1;
363 }
364 
365 int
xfs_iomap_write_direct(xfs_inode_t * ip,loff_t offset,size_t count,int flags,xfs_bmbt_irec_t * ret_imap,int * nmaps,int found)366 xfs_iomap_write_direct(
367 	xfs_inode_t	*ip,
368 	loff_t		offset,
369 	size_t		count,
370 	int		flags,
371 	xfs_bmbt_irec_t *ret_imap,
372 	int		*nmaps,
373 	int		found)
374 {
375 	xfs_mount_t	*mp = ip->i_mount;
376 	xfs_iocore_t	*io = &ip->i_iocore;
377 	xfs_fileoff_t	offset_fsb;
378 	xfs_fileoff_t	last_fsb;
379 	xfs_filblks_t	count_fsb;
380 	xfs_fsize_t	isize;
381 	xfs_fsblock_t	firstfsb;
382 	int		nimaps, maps;
383 	int		error;
384 	int		bmapi_flag;
385 	int		rt;
386 	xfs_trans_t	*tp;
387 	xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS], *imapp;
388 	xfs_bmap_free_t free_list;
389 	int		aeof;
390 	xfs_filblks_t	datablocks;
391 	int		committed;
392 	int		numrtextents;
393 	uint		resblks;
394 
395 	/*
396 	 * Make sure that the dquots are there. This doesn't hold
397 	 * the ilock across a disk read.
398 	 */
399 	error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
400 	if (error)
401 		return XFS_ERROR(error);
402 
403 	maps = min(XFS_WRITE_IMAPS, *nmaps);
404 	nimaps = maps;
405 
406 	isize = ip->i_d.di_size;
407 	aeof = (offset + count) > isize;
408 
409 	if (io->io_new_size > isize)
410 		isize = io->io_new_size;
411 
412 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
413 	last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
414 	count_fsb = last_fsb - offset_fsb;
415 	if (found && (ret_imap->br_startblock == HOLESTARTBLOCK)) {
416 		xfs_fileoff_t	map_last_fsb;
417 
418 		map_last_fsb = ret_imap->br_blockcount + ret_imap->br_startoff;
419 
420 		if (map_last_fsb < last_fsb) {
421 			last_fsb = map_last_fsb;
422 			count_fsb = last_fsb - offset_fsb;
423 		}
424 		ASSERT(count_fsb > 0);
425 	}
426 
427 	/*
428 	 * determine if reserving space on
429 	 * the data or realtime partition.
430 	 */
431 	if ((rt = XFS_IS_REALTIME_INODE(ip))) {
432 		int	sbrtextsize, iprtextsize;
433 
434 		sbrtextsize = mp->m_sb.sb_rextsize;
435 		iprtextsize =
436 			ip->i_d.di_extsize ? ip->i_d.di_extsize : sbrtextsize;
437 		numrtextents = (count_fsb + iprtextsize - 1);
438 		do_div(numrtextents, sbrtextsize);
439 		datablocks = 0;
440 	} else {
441 		datablocks = count_fsb;
442 		numrtextents = 0;
443 	}
444 
445 	/*
446 	 * allocate and setup the transaction
447 	 */
448 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
449 	tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
450 
451 	resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
452 
453 	error = xfs_trans_reserve(tp, resblks,
454 			XFS_WRITE_LOG_RES(mp), numrtextents,
455 			XFS_TRANS_PERM_LOG_RES,
456 			XFS_WRITE_LOG_COUNT);
457 
458 	/*
459 	 * check for running out of space
460 	 */
461 	if (error)
462 		/*
463 		 * Free the transaction structure.
464 		 */
465 		xfs_trans_cancel(tp, 0);
466 
467 	xfs_ilock(ip, XFS_ILOCK_EXCL);
468 
469 	if (error)
470 		goto error_out; /* Don't return in above if .. trans ..,
471 					need lock to return */
472 
473 	if (XFS_TRANS_RESERVE_BLKQUOTA(mp, tp, ip, resblks)) {
474 		error = (EDQUOT);
475 		goto error1;
476 	}
477 	nimaps = 1;
478 
479 	bmapi_flag = XFS_BMAPI_WRITE;
480 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
481 	xfs_trans_ihold(tp, ip);
482 
483 	if (!(flags & BMAPI_MMAP) && (offset < ip->i_d.di_size || rt))
484 		bmapi_flag |= XFS_BMAPI_PREALLOC;
485 
486 	/*
487 	 * issue the bmapi() call to allocate the blocks
488 	 */
489 	XFS_BMAP_INIT(&free_list, &firstfsb);
490 	imapp = &imap[0];
491 	error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
492 		bmapi_flag, &firstfsb, 0, imapp, &nimaps, &free_list);
493 	if (error) {
494 		goto error0;
495 	}
496 
497 	/*
498 	 * complete the transaction
499 	 */
500 
501 	error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
502 	if (error) {
503 		goto error0;
504 	}
505 
506 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
507 	if (error) {
508 		goto error_out;
509 	}
510 
511 	/* copy any maps to caller's array and return any error. */
512 	if (nimaps == 0) {
513 		error = (ENOSPC);
514 		goto error_out;
515 	}
516 
517 	*ret_imap = imap[0];
518 	*nmaps = 1;
519 	if ( !(io->io_flags & XFS_IOCORE_RT)  && !ret_imap->br_startblock) {
520                 cmn_err(CE_PANIC,"Access to block zero:  fs <%s> inode: %lld "
521                         "start_block : %llx start_off : %llx blkcnt : %llx "
522                         "extent-state : %x \n",
523                         (ip->i_mount)->m_fsname,
524                         (long long)ip->i_ino,
525                         ret_imap->br_startblock, ret_imap->br_startoff,
526                         ret_imap->br_blockcount,ret_imap->br_state);
527         }
528 	return 0;
529 
530  error0:	/* Cancel bmap, unlock inode, and cancel trans */
531 	xfs_bmap_cancel(&free_list);
532 
533  error1:	/* Just cancel transaction */
534 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
535 	*nmaps = 0;	/* nothing set-up here */
536 
537 error_out:
538 	return XFS_ERROR(error);
539 }
540 
541 int
xfs_iomap_write_delay(xfs_inode_t * ip,loff_t offset,size_t count,int ioflag,xfs_bmbt_irec_t * ret_imap,int * nmaps)542 xfs_iomap_write_delay(
543 	xfs_inode_t	*ip,
544 	loff_t		offset,
545 	size_t		count,
546 	int		ioflag,
547 	xfs_bmbt_irec_t *ret_imap,
548 	int		*nmaps)
549 {
550 	xfs_mount_t	*mp = ip->i_mount;
551 	xfs_iocore_t	*io = &ip->i_iocore;
552 	xfs_fileoff_t	offset_fsb;
553 	xfs_fileoff_t	last_fsb;
554 	xfs_fsize_t	isize;
555 	xfs_fsblock_t	firstblock;
556 	int		nimaps;
557 	int		error;
558 	xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
559 	int		aeof;
560 	int		fsynced = 0;
561 
562 	ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
563 
564 	/*
565 	 * Make sure that the dquots are there. This doesn't hold
566 	 * the ilock across a disk read.
567 	 */
568 
569 	error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
570 	if (error)
571 		return XFS_ERROR(error);
572 
573 retry:
574 	isize = ip->i_d.di_size;
575 	if (io->io_new_size > isize) {
576 		isize = io->io_new_size;
577 	}
578 
579 	aeof = 0;
580 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
581 	last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
582 	/*
583 	 * If the caller is doing a write at the end of the file,
584 	 * then extend the allocation (and the buffer used for the write)
585 	 * out to the file system's write iosize.  We clean up any extra
586 	 * space left over when the file is closed in xfs_inactive().
587 	 *
588 	 * For sync writes, we are flushing delayed allocate space to
589 	 * try to make additional space available for allocation near
590 	 * the filesystem full boundary - preallocation hurts in that
591 	 * situation, of course.
592 	 */
593 	if (!(ioflag & BMAPI_SYNC) && ((offset + count) > ip->i_d.di_size)) {
594 		xfs_off_t	aligned_offset;
595 		xfs_filblks_t   count_fsb;
596 		unsigned int	iosize;
597 		xfs_fileoff_t	ioalign;
598 		int		n;
599 		xfs_fileoff_t   start_fsb;
600 
601 		/*
602 		 * If there are any real blocks past eof, then don't
603 		 * do any speculative allocation.
604 		 */
605 		start_fsb = XFS_B_TO_FSBT(mp,
606 					((xfs_ufsize_t)(offset + count - 1)));
607 		count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
608 		while (count_fsb > 0) {
609 			nimaps = XFS_WRITE_IMAPS;
610 			error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb,
611 					0, &firstblock, 0, imap, &nimaps, NULL);
612 			if (error) {
613 				return error;
614 			}
615 			for (n = 0; n < nimaps; n++) {
616 				if ( !(io->io_flags & XFS_IOCORE_RT)  &&
617 					!imap[n].br_startblock) {
618 					cmn_err(CE_PANIC,"Access to block "
619 						"zero:  fs <%s> inode: %lld "
620 						"start_block : %llx start_off "
621 						": %llx blkcnt : %llx "
622 						"extent-state : %x \n",
623 						(ip->i_mount)->m_fsname,
624 						(long long)ip->i_ino,
625 						imap[n].br_startblock,
626 						imap[n].br_startoff,
627 						imap[n].br_blockcount,
628 						imap[n].br_state);
629         			}
630 				if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
631 				    (imap[n].br_startblock != DELAYSTARTBLOCK)) {
632 					goto write_map;
633 				}
634 				start_fsb += imap[n].br_blockcount;
635 				count_fsb -= imap[n].br_blockcount;
636 			}
637 		}
638 		iosize = mp->m_writeio_blocks;
639 		aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
640 		ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
641 		last_fsb = ioalign + iosize;
642 		aeof = 1;
643 	}
644 write_map:
645 	nimaps = XFS_WRITE_IMAPS;
646 	firstblock = NULLFSBLOCK;
647 
648 	/*
649 	 * If mounted with the "-o swalloc" option, roundup the allocation
650 	 * request to a stripe width boundary if the file size is >=
651 	 * stripe width and we are allocating past the allocation eof.
652 	 */
653 	if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_swidth
654 	    && (mp->m_flags & XFS_MOUNT_SWALLOC)
655 	    && (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)) && aeof) {
656 		int eof;
657 		xfs_fileoff_t new_last_fsb;
658 
659 		new_last_fsb = roundup_64(last_fsb, mp->m_swidth);
660 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
661 		if (error) {
662 			return error;
663 		}
664 		if (eof) {
665 			last_fsb = new_last_fsb;
666 		}
667 	/*
668 	 * Roundup the allocation request to a stripe unit (m_dalign) boundary
669 	 * if the file size is >= stripe unit size, and we are allocating past
670 	 * the allocation eof.
671 	 */
672 	} else if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_dalign &&
673 		   (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)) && aeof) {
674 		int eof;
675 		xfs_fileoff_t new_last_fsb;
676 		new_last_fsb = roundup_64(last_fsb, mp->m_dalign);
677 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
678 		if (error) {
679 			return error;
680 		}
681 		if (eof) {
682 			last_fsb = new_last_fsb;
683 		}
684 	/*
685 	 * Round up the allocation request to a real-time extent boundary
686 	 * if the file is on the real-time subvolume.
687 	 */
688 	} else if (io->io_flags & XFS_IOCORE_RT && aeof) {
689 		int eof;
690 		xfs_fileoff_t new_last_fsb;
691 
692 		new_last_fsb = roundup_64(last_fsb, mp->m_sb.sb_rextsize);
693 		error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
694 		if (error) {
695 			return error;
696 		}
697 		if (eof)
698 			last_fsb = new_last_fsb;
699 	}
700 	error = xfs_bmapi(NULL, ip, offset_fsb,
701 			  (xfs_filblks_t)(last_fsb - offset_fsb),
702 			  XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
703 			  XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
704 			  &nimaps, NULL);
705 	/*
706 	 * This can be EDQUOT, if nimaps == 0
707 	 */
708 	if (error && (error != ENOSPC)) {
709 		return XFS_ERROR(error);
710 	}
711 	/*
712 	 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
713 	 * then we must have run out of space.
714 	 */
715 	if (nimaps == 0) {
716 		xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
717 					io, offset, count);
718 		if (xfs_flush_space(ip, &fsynced, &ioflag))
719 			return XFS_ERROR(ENOSPC);
720 
721 		error = 0;
722 		goto retry;
723 	}
724 
725 	*ret_imap = imap[0];
726 	*nmaps = 1;
727 	if ( !(io->io_flags & XFS_IOCORE_RT)  && !ret_imap->br_startblock) {
728 		cmn_err(CE_PANIC,"Access to block zero:  fs <%s> inode: %lld "
729                         "start_block : %llx start_off : %llx blkcnt : %llx "
730                         "extent-state : %x \n",
731                         (ip->i_mount)->m_fsname,
732                         (long long)ip->i_ino,
733                         ret_imap->br_startblock, ret_imap->br_startoff,
734                         ret_imap->br_blockcount,ret_imap->br_state);
735 	}
736 	return 0;
737 }
738 
739 /*
740  * Pass in a delayed allocate extent, convert it to real extents;
741  * return to the caller the extent we create which maps on top of
742  * the originating callers request.
743  *
744  * Called without a lock on the inode.
745  */
746 int
xfs_iomap_write_allocate(xfs_inode_t * ip,xfs_bmbt_irec_t * map,int * retmap)747 xfs_iomap_write_allocate(
748 	xfs_inode_t	*ip,
749 	xfs_bmbt_irec_t *map,
750 	int		*retmap)
751 {
752 	xfs_mount_t	*mp = ip->i_mount;
753 	xfs_iocore_t    *io = &ip->i_iocore;
754 	xfs_fileoff_t	offset_fsb, last_block;
755 	xfs_fileoff_t	end_fsb, map_start_fsb;
756 	xfs_fsblock_t	first_block;
757 	xfs_bmap_free_t	free_list;
758 	xfs_filblks_t	count_fsb;
759 	xfs_bmbt_irec_t	imap[XFS_STRAT_WRITE_IMAPS];
760 	xfs_trans_t	*tp;
761 	int		i, nimaps, committed;
762 	int		error = 0;
763 	int		nres;
764 
765 	*retmap = 0;
766 
767 	/*
768 	 * Make sure that the dquots are there.
769 	 */
770 	if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
771 		return XFS_ERROR(error);
772 
773 	offset_fsb = map->br_startoff;
774 	count_fsb = map->br_blockcount;
775 	map_start_fsb = offset_fsb;
776 
777 	XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
778 
779 	while (count_fsb != 0) {
780 		/*
781 		 * Set up a transaction with which to allocate the
782 		 * backing store for the file.  Do allocations in a
783 		 * loop until we get some space in the range we are
784 		 * interested in.  The other space that might be allocated
785 		 * is in the delayed allocation extent on which we sit
786 		 * but before our buffer starts.
787 		 */
788 
789 		nimaps = 0;
790 		while (nimaps == 0) {
791 			tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
792 			nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
793 			error = xfs_trans_reserve(tp, nres,
794 					XFS_WRITE_LOG_RES(mp),
795 					0, XFS_TRANS_PERM_LOG_RES,
796 					XFS_WRITE_LOG_COUNT);
797 			if (error == ENOSPC) {
798 				error = xfs_trans_reserve(tp, 0,
799 						XFS_WRITE_LOG_RES(mp),
800 						0,
801 						XFS_TRANS_PERM_LOG_RES,
802 						XFS_WRITE_LOG_COUNT);
803 			}
804 			if (error) {
805 				xfs_trans_cancel(tp, 0);
806 				return XFS_ERROR(error);
807 			}
808 			xfs_ilock(ip, XFS_ILOCK_EXCL);
809 			xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
810 			xfs_trans_ihold(tp, ip);
811 
812 			XFS_BMAP_INIT(&free_list, &first_block);
813 
814 			nimaps = XFS_STRAT_WRITE_IMAPS;
815 			/*
816 			 * Ensure we don't go beyond eof - it is possible
817 			 * the extents changed since we did the read call,
818 			 * we dropped the ilock in the interim.
819 			 */
820 
821 			end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size);
822 			xfs_bmap_last_offset(NULL, ip, &last_block,
823 				XFS_DATA_FORK);
824 			last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
825 			if ((map_start_fsb + count_fsb) > last_block) {
826 				count_fsb = last_block - map_start_fsb;
827 				if (count_fsb == 0) {
828 					error = EAGAIN;
829 					goto trans_cancel;
830 				}
831 			}
832 
833 			/* Go get the actual blocks */
834 			error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
835 					XFS_BMAPI_WRITE, &first_block, 1,
836 					imap, &nimaps, &free_list);
837 			if (error)
838 				goto trans_cancel;
839 
840 			error = xfs_bmap_finish(&tp, &free_list,
841 					first_block, &committed);
842 			if (error)
843 				goto trans_cancel;
844 
845 			error = xfs_trans_commit(tp,
846 					XFS_TRANS_RELEASE_LOG_RES, NULL);
847 			if (error)
848 				goto error0;
849 
850 			xfs_iunlock(ip, XFS_ILOCK_EXCL);
851 		}
852 
853 		/*
854 		 * See if we were able to allocate an extent that
855 		 * covers at least part of the callers request
856 		 */
857 
858 		for (i = 0; i < nimaps; i++) {
859 			if ( !(io->io_flags & XFS_IOCORE_RT)  &&
860 				!imap[i].br_startblock) {
861 				cmn_err(CE_PANIC,"Access to block zero:  "
862 					"fs <%s> inode: %lld "
863 					"start_block : %llx start_off : %llx "
864 					"blkcnt : %llx extent-state : %x \n",
865 					(ip->i_mount)->m_fsname,
866 					(long long)ip->i_ino,
867 					imap[i].br_startblock,
868 					imap[i].br_startoff,
869 				        imap[i].br_blockcount,imap[i].br_state);
870                         }
871 			if ((map->br_startoff >= imap[i].br_startoff) &&
872 			    (map->br_startoff < (imap[i].br_startoff +
873 						 imap[i].br_blockcount))) {
874 				*map = imap[i];
875 				*retmap = 1;
876 				XFS_STATS_INC(xs_xstrat_quick);
877 				return 0;
878 			}
879 			count_fsb -= imap[i].br_blockcount;
880 		}
881 
882 		/* So far we have not mapped the requested part of the
883 		 * file, just surrounding data, try again.
884 		 */
885 		nimaps--;
886 		offset_fsb = imap[nimaps].br_startoff +
887 			     imap[nimaps].br_blockcount;
888 		map_start_fsb = offset_fsb;
889 	}
890 
891 trans_cancel:
892 	xfs_bmap_cancel(&free_list);
893 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
894 error0:
895 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
896 	return XFS_ERROR(error);
897 }
898 
899 int
xfs_iomap_write_unwritten(xfs_inode_t * ip,loff_t offset,size_t count)900 xfs_iomap_write_unwritten(
901 	xfs_inode_t	*ip,
902 	loff_t		offset,
903 	size_t		count)
904 {
905 	xfs_mount_t	*mp = ip->i_mount;
906 	xfs_iocore_t    *io = &ip->i_iocore;
907 	xfs_trans_t	*tp;
908 	xfs_fileoff_t	offset_fsb;
909 	xfs_filblks_t	count_fsb;
910 	xfs_filblks_t	numblks_fsb;
911 	xfs_bmbt_irec_t	imap;
912 	int		committed;
913 	int		error;
914 	int		nres;
915 	int		nimaps;
916 	xfs_fsblock_t	firstfsb;
917 	xfs_bmap_free_t	free_list;
918 
919 	xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
920 				&ip->i_iocore, offset, count);
921 
922 	offset_fsb = XFS_B_TO_FSBT(mp, offset);
923 	count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
924 	count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
925 
926 	do {
927 		nres = XFS_DIOSTRAT_SPACE_RES(mp, 0);
928 
929 		/*
930 		 * set up a transaction to convert the range of extents
931 		 * from unwritten to real. Do allocations in a loop until
932 		 * we have covered the range passed in.
933 		 */
934 
935 		tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
936 		error = xfs_trans_reserve(tp, nres,
937 				XFS_WRITE_LOG_RES(mp), 0,
938 				XFS_TRANS_PERM_LOG_RES,
939 				XFS_WRITE_LOG_COUNT);
940 		if (error) {
941 			xfs_trans_cancel(tp, 0);
942 			goto error0;
943 		}
944 
945 		xfs_ilock(ip, XFS_ILOCK_EXCL);
946 		xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
947 		xfs_trans_ihold(tp, ip);
948 
949 		/*
950 		 * Modify the unwritten extent state of the buffer.
951 		 */
952 		XFS_BMAP_INIT(&free_list, &firstfsb);
953 		nimaps = 1;
954 		error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
955 				  XFS_BMAPI_WRITE, &firstfsb,
956 				  1, &imap, &nimaps, &free_list);
957 		if (error)
958 			goto error_on_bmapi_transaction;
959 
960 		error = xfs_bmap_finish(&(tp), &(free_list),
961 				firstfsb, &committed);
962 		if (error)
963 			goto error_on_bmapi_transaction;
964 
965 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
966 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
967 		if (error)
968 			goto error0;
969 
970 		if ( !(io->io_flags & XFS_IOCORE_RT)  && !imap.br_startblock) {
971 			cmn_err(CE_PANIC,"Access to block zero:  fs <%s> "
972 				"inode: %lld start_block : %llx start_off : "
973 				"%llx blkcnt : %llx extent-state : %x \n",
974 				(ip->i_mount)->m_fsname,
975 				(long long)ip->i_ino,
976 				imap.br_startblock,imap.br_startoff,
977 				imap.br_blockcount,imap.br_state);
978         	}
979 
980 		if ((numblks_fsb = imap.br_blockcount) == 0) {
981 			/*
982 			 * The numblks_fsb value should always get
983 			 * smaller, otherwise the loop is stuck.
984 			 */
985 			ASSERT(imap.br_blockcount);
986 			break;
987 		}
988 		offset_fsb += numblks_fsb;
989 		count_fsb -= numblks_fsb;
990 	} while (count_fsb > 0);
991 
992 	return 0;
993 
994 error_on_bmapi_transaction:
995 	xfs_bmap_cancel(&free_list);
996 	xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
997 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
998 error0:
999 	return XFS_ERROR(error);
1000 }
1001