[XFS] kill BMAPI_DEVICE
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_iomap.c
blob58cda06a18467235ec93afe6b81ea8af5590645f
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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.
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.
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
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_dir2.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_btree.h"
40 #include "xfs_bmap.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
44 #include "xfs_rw.h"
45 #include "xfs_acl.h"
46 #include "xfs_attr.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_trans_space.h"
49 #include "xfs_utils.h"
50 #include "xfs_iomap.h"
52 #if defined(XFS_RW_TRACE)
53 void
54 xfs_iomap_enter_trace(
55 int tag,
56 xfs_iocore_t *io,
57 xfs_off_t offset,
58 ssize_t count)
60 xfs_inode_t *ip = XFS_IO_INODE(io);
62 if (!ip->i_rwtrace)
63 return;
65 ktrace_enter(ip->i_rwtrace,
66 (void *)((unsigned long)tag),
67 (void *)ip,
68 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
69 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
70 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
71 (void *)((unsigned long)(offset & 0xffffffff)),
72 (void *)((unsigned long)count),
73 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
74 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
75 (void *)((unsigned long)current_pid()),
76 (void *)NULL,
77 (void *)NULL,
78 (void *)NULL,
79 (void *)NULL,
80 (void *)NULL,
81 (void *)NULL);
84 void
85 xfs_iomap_map_trace(
86 int tag,
87 xfs_iocore_t *io,
88 xfs_off_t offset,
89 ssize_t count,
90 xfs_iomap_t *iomapp,
91 xfs_bmbt_irec_t *imapp,
92 int flags)
94 xfs_inode_t *ip = XFS_IO_INODE(io);
96 if (!ip->i_rwtrace)
97 return;
99 ktrace_enter(ip->i_rwtrace,
100 (void *)((unsigned long)tag),
101 (void *)ip,
102 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
103 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
104 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(offset & 0xffffffff)),
106 (void *)((unsigned long)count),
107 (void *)((unsigned long)flags),
108 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
110 (void *)((unsigned long)(iomapp->iomap_delta)),
111 (void *)((unsigned long)(iomapp->iomap_bsize)),
112 (void *)((unsigned long)(iomapp->iomap_bn)),
113 (void *)(__psint_t)(imapp->br_startoff),
114 (void *)((unsigned long)(imapp->br_blockcount)),
115 (void *)(__psint_t)(imapp->br_startblock));
117 #else
118 #define xfs_iomap_enter_trace(tag, io, offset, count)
119 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
120 #endif
122 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
123 << mp->m_writeio_log)
124 #define XFS_STRAT_WRITE_IMAPS 2
125 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
127 STATIC int
128 xfs_imap_to_bmap(
129 xfs_iocore_t *io,
130 xfs_off_t offset,
131 xfs_bmbt_irec_t *imap,
132 xfs_iomap_t *iomapp,
133 int imaps, /* Number of imap entries */
134 int iomaps, /* Number of iomap entries */
135 int flags)
137 xfs_mount_t *mp;
138 int pbm;
139 xfs_fsblock_t start_block;
141 mp = io->io_mount;
143 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
144 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
145 iomapp->iomap_delta = offset - iomapp->iomap_offset;
146 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
147 iomapp->iomap_flags = flags;
149 if (io->io_flags & XFS_IOCORE_RT) {
150 iomapp->iomap_flags |= IOMAP_REALTIME;
151 iomapp->iomap_target = mp->m_rtdev_targp;
152 } else {
153 iomapp->iomap_target = mp->m_ddev_targp;
155 start_block = imap->br_startblock;
156 if (start_block == HOLESTARTBLOCK) {
157 iomapp->iomap_bn = IOMAP_DADDR_NULL;
158 iomapp->iomap_flags |= IOMAP_HOLE;
159 } else if (start_block == DELAYSTARTBLOCK) {
160 iomapp->iomap_bn = IOMAP_DADDR_NULL;
161 iomapp->iomap_flags |= IOMAP_DELAY;
162 } else {
163 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
164 if (ISUNWRITTEN(imap))
165 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
168 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
170 return pbm; /* Return the number filled */
174 xfs_iomap(
175 xfs_iocore_t *io,
176 xfs_off_t offset,
177 ssize_t count,
178 int flags,
179 xfs_iomap_t *iomapp,
180 int *niomaps)
182 xfs_mount_t *mp = io->io_mount;
183 xfs_fileoff_t offset_fsb, end_fsb;
184 int error = 0;
185 int lockmode = 0;
186 xfs_bmbt_irec_t imap;
187 int nimaps = 1;
188 int bmapi_flags = 0;
189 int iomap_flags = 0;
191 if (XFS_FORCED_SHUTDOWN(mp))
192 return XFS_ERROR(EIO);
194 switch (flags &
195 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
196 BMAPI_UNWRITTEN)) {
197 case BMAPI_READ:
198 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
199 lockmode = XFS_LCK_MAP_SHARED(mp, io);
200 bmapi_flags = XFS_BMAPI_ENTIRE;
201 break;
202 case BMAPI_WRITE:
203 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
204 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
205 if (flags & BMAPI_IGNSTATE)
206 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
207 XFS_ILOCK(mp, io, lockmode);
208 break;
209 case BMAPI_ALLOCATE:
210 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
211 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
212 bmapi_flags = XFS_BMAPI_ENTIRE;
213 /* Attempt non-blocking lock */
214 if (flags & BMAPI_TRYLOCK) {
215 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
216 return XFS_ERROR(EAGAIN);
217 } else {
218 XFS_ILOCK(mp, io, lockmode);
220 break;
221 case BMAPI_UNWRITTEN:
222 goto phase2;
223 default:
224 BUG();
227 ASSERT(offset <= mp->m_maxioffset);
228 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
229 count = mp->m_maxioffset - offset;
230 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
231 offset_fsb = XFS_B_TO_FSBT(mp, offset);
233 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
234 (xfs_filblks_t)(end_fsb - offset_fsb),
235 bmapi_flags, NULL, 0, &imap,
236 &nimaps, NULL, NULL);
238 if (error)
239 goto out;
241 phase2:
242 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
243 case BMAPI_WRITE:
244 /* If we found an extent, return it */
245 if (nimaps &&
246 (imap.br_startblock != HOLESTARTBLOCK) &&
247 (imap.br_startblock != DELAYSTARTBLOCK)) {
248 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
249 offset, count, iomapp, &imap, flags);
250 break;
253 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
254 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
255 count, flags, &imap, &nimaps, nimaps);
256 } else {
257 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
258 flags, &imap, &nimaps);
260 if (!error) {
261 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
262 offset, count, iomapp, &imap, flags);
264 iomap_flags = IOMAP_NEW;
265 break;
266 case BMAPI_ALLOCATE:
267 /* If we found an extent, return it */
268 XFS_IUNLOCK(mp, io, lockmode);
269 lockmode = 0;
271 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
272 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
273 offset, count, iomapp, &imap, flags);
274 break;
277 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
278 &imap, &nimaps);
279 break;
280 case BMAPI_UNWRITTEN:
281 lockmode = 0;
282 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
283 nimaps = 0;
284 break;
287 if (nimaps) {
288 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
289 iomapp, nimaps, *niomaps, iomap_flags);
290 } else if (niomaps) {
291 *niomaps = 0;
294 out:
295 if (lockmode)
296 XFS_IUNLOCK(mp, io, lockmode);
297 return XFS_ERROR(error);
300 STATIC int
301 xfs_iomap_eof_align_last_fsb(
302 xfs_mount_t *mp,
303 xfs_iocore_t *io,
304 xfs_fsize_t isize,
305 xfs_extlen_t extsize,
306 xfs_fileoff_t *last_fsb)
308 xfs_fileoff_t new_last_fsb = 0;
309 xfs_extlen_t align;
310 int eof, error;
312 if (io->io_flags & XFS_IOCORE_RT)
315 * If mounted with the "-o swalloc" option, roundup the allocation
316 * request to a stripe width boundary if the file size is >=
317 * stripe width and we are allocating past the allocation eof.
319 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
320 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
321 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
323 * Roundup the allocation request to a stripe unit (m_dalign) boundary
324 * if the file size is >= stripe unit size, and we are allocating past
325 * the allocation eof.
327 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
328 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
331 * Always round up the allocation request to an extent boundary
332 * (when file on a real-time subvolume or has di_extsize hint).
334 if (extsize) {
335 if (new_last_fsb)
336 align = roundup_64(new_last_fsb, extsize);
337 else
338 align = extsize;
339 new_last_fsb = roundup_64(*last_fsb, align);
342 if (new_last_fsb) {
343 error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
344 if (error)
345 return error;
346 if (eof)
347 *last_fsb = new_last_fsb;
349 return 0;
352 STATIC int
353 xfs_flush_space(
354 xfs_inode_t *ip,
355 int *fsynced,
356 int *ioflags)
358 switch (*fsynced) {
359 case 0:
360 if (ip->i_delayed_blks) {
361 xfs_iunlock(ip, XFS_ILOCK_EXCL);
362 xfs_flush_inode(ip);
363 xfs_ilock(ip, XFS_ILOCK_EXCL);
364 *fsynced = 1;
365 } else {
366 *ioflags |= BMAPI_SYNC;
367 *fsynced = 2;
369 return 0;
370 case 1:
371 *fsynced = 2;
372 *ioflags |= BMAPI_SYNC;
373 return 0;
374 case 2:
375 xfs_iunlock(ip, XFS_ILOCK_EXCL);
376 xfs_flush_device(ip);
377 xfs_ilock(ip, XFS_ILOCK_EXCL);
378 *fsynced = 3;
379 return 0;
381 return 1;
384 STATIC int
385 xfs_cmn_err_fsblock_zero(
386 xfs_inode_t *ip,
387 xfs_bmbt_irec_t *imap)
389 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
390 "Access to block zero in inode %llu "
391 "start_block: %llx start_off: %llx "
392 "blkcnt: %llx extent-state: %x\n",
393 (unsigned long long)ip->i_ino,
394 (unsigned long long)imap->br_startblock,
395 (unsigned long long)imap->br_startoff,
396 (unsigned long long)imap->br_blockcount,
397 imap->br_state);
398 return EFSCORRUPTED;
402 xfs_iomap_write_direct(
403 xfs_inode_t *ip,
404 xfs_off_t offset,
405 size_t count,
406 int flags,
407 xfs_bmbt_irec_t *ret_imap,
408 int *nmaps,
409 int found)
411 xfs_mount_t *mp = ip->i_mount;
412 xfs_iocore_t *io = &ip->i_iocore;
413 xfs_fileoff_t offset_fsb;
414 xfs_fileoff_t last_fsb;
415 xfs_filblks_t count_fsb, resaligned;
416 xfs_fsblock_t firstfsb;
417 xfs_extlen_t extsz, temp;
418 xfs_fsize_t isize;
419 int nimaps;
420 int bmapi_flag;
421 int quota_flag;
422 int rt;
423 xfs_trans_t *tp;
424 xfs_bmbt_irec_t imap;
425 xfs_bmap_free_t free_list;
426 uint qblocks, resblks, resrtextents;
427 int committed;
428 int error;
431 * Make sure that the dquots are there. This doesn't hold
432 * the ilock across a disk read.
434 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
435 if (error)
436 return XFS_ERROR(error);
438 rt = XFS_IS_REALTIME_INODE(ip);
439 extsz = xfs_get_extsz_hint(ip);
441 isize = ip->i_size;
442 if (io->io_new_size > isize)
443 isize = io->io_new_size;
445 offset_fsb = XFS_B_TO_FSBT(mp, offset);
446 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
447 if ((offset + count) > isize) {
448 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
449 &last_fsb);
450 if (error)
451 goto error_out;
452 } else {
453 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
454 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
455 ret_imap->br_blockcount +
456 ret_imap->br_startoff);
458 count_fsb = last_fsb - offset_fsb;
459 ASSERT(count_fsb > 0);
461 resaligned = count_fsb;
462 if (unlikely(extsz)) {
463 if ((temp = do_mod(offset_fsb, extsz)))
464 resaligned += temp;
465 if ((temp = do_mod(resaligned, extsz)))
466 resaligned += extsz - temp;
469 if (unlikely(rt)) {
470 resrtextents = qblocks = resaligned;
471 resrtextents /= mp->m_sb.sb_rextsize;
472 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
473 quota_flag = XFS_QMOPT_RES_RTBLKS;
474 } else {
475 resrtextents = 0;
476 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
477 quota_flag = XFS_QMOPT_RES_REGBLKS;
481 * Allocate and setup the transaction
483 xfs_iunlock(ip, XFS_ILOCK_EXCL);
484 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
485 error = xfs_trans_reserve(tp, resblks,
486 XFS_WRITE_LOG_RES(mp), resrtextents,
487 XFS_TRANS_PERM_LOG_RES,
488 XFS_WRITE_LOG_COUNT);
490 * Check for running out of space, note: need lock to return
492 if (error)
493 xfs_trans_cancel(tp, 0);
494 xfs_ilock(ip, XFS_ILOCK_EXCL);
495 if (error)
496 goto error_out;
498 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
499 qblocks, 0, quota_flag);
500 if (error)
501 goto error1;
503 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
504 xfs_trans_ihold(tp, ip);
506 bmapi_flag = XFS_BMAPI_WRITE;
507 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
508 bmapi_flag |= XFS_BMAPI_PREALLOC;
511 * Issue the xfs_bmapi() call to allocate the blocks
513 XFS_BMAP_INIT(&free_list, &firstfsb);
514 nimaps = 1;
515 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb, bmapi_flag,
516 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
517 if (error)
518 goto error0;
521 * Complete the transaction
523 error = xfs_bmap_finish(&tp, &free_list, &committed);
524 if (error)
525 goto error0;
526 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
527 if (error)
528 goto error_out;
531 * Copy any maps to caller's array and return any error.
533 if (nimaps == 0) {
534 error = ENOSPC;
535 goto error_out;
538 if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
539 error = xfs_cmn_err_fsblock_zero(ip, &imap);
540 goto error_out;
543 *ret_imap = imap;
544 *nmaps = 1;
545 return 0;
547 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
548 xfs_bmap_cancel(&free_list);
549 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
551 error1: /* Just cancel transaction */
552 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
553 *nmaps = 0; /* nothing set-up here */
555 error_out:
556 return XFS_ERROR(error);
560 * If the caller is doing a write at the end of the file,
561 * then extend the allocation out to the file system's write
562 * iosize. We clean up any extra space left over when the
563 * file is closed in xfs_inactive().
565 * For sync writes, we are flushing delayed allocate space to
566 * try to make additional space available for allocation near
567 * the filesystem full boundary - preallocation hurts in that
568 * situation, of course.
570 STATIC int
571 xfs_iomap_eof_want_preallocate(
572 xfs_mount_t *mp,
573 xfs_iocore_t *io,
574 xfs_fsize_t isize,
575 xfs_off_t offset,
576 size_t count,
577 int ioflag,
578 xfs_bmbt_irec_t *imap,
579 int nimaps,
580 int *prealloc)
582 xfs_fileoff_t start_fsb;
583 xfs_filblks_t count_fsb;
584 xfs_fsblock_t firstblock;
585 int n, error, imaps;
587 *prealloc = 0;
588 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
589 return 0;
592 * If there are any real blocks past eof, then don't
593 * do any speculative allocation.
595 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
596 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
597 while (count_fsb > 0) {
598 imaps = nimaps;
599 firstblock = NULLFSBLOCK;
600 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb, 0,
601 &firstblock, 0, imap, &imaps, NULL, NULL);
602 if (error)
603 return error;
604 for (n = 0; n < imaps; n++) {
605 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
606 (imap[n].br_startblock != DELAYSTARTBLOCK))
607 return 0;
608 start_fsb += imap[n].br_blockcount;
609 count_fsb -= imap[n].br_blockcount;
612 *prealloc = 1;
613 return 0;
617 xfs_iomap_write_delay(
618 xfs_inode_t *ip,
619 xfs_off_t offset,
620 size_t count,
621 int ioflag,
622 xfs_bmbt_irec_t *ret_imap,
623 int *nmaps)
625 xfs_mount_t *mp = ip->i_mount;
626 xfs_iocore_t *io = &ip->i_iocore;
627 xfs_fileoff_t offset_fsb;
628 xfs_fileoff_t last_fsb;
629 xfs_off_t aligned_offset;
630 xfs_fileoff_t ioalign;
631 xfs_fsblock_t firstblock;
632 xfs_extlen_t extsz;
633 xfs_fsize_t isize;
634 int nimaps;
635 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
636 int prealloc, fsynced = 0;
637 int error;
639 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
642 * Make sure that the dquots are there. This doesn't hold
643 * the ilock across a disk read.
645 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
646 if (error)
647 return XFS_ERROR(error);
649 extsz = xfs_get_extsz_hint(ip);
650 offset_fsb = XFS_B_TO_FSBT(mp, offset);
652 retry:
653 isize = ip->i_size;
654 if (io->io_new_size > isize)
655 isize = io->io_new_size;
657 error = xfs_iomap_eof_want_preallocate(mp, io, isize, offset, count,
658 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
659 if (error)
660 return error;
662 if (prealloc) {
663 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
664 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
665 last_fsb = ioalign + mp->m_writeio_blocks;
666 } else {
667 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
670 if (prealloc || extsz) {
671 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
672 &last_fsb);
673 if (error)
674 return error;
677 nimaps = XFS_WRITE_IMAPS;
678 firstblock = NULLFSBLOCK;
679 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
680 (xfs_filblks_t)(last_fsb - offset_fsb),
681 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
682 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
683 &nimaps, NULL, NULL);
684 if (error && (error != ENOSPC))
685 return XFS_ERROR(error);
688 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
689 * then we must have run out of space - flush delalloc, and retry..
691 if (nimaps == 0) {
692 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
693 io, offset, count);
694 if (xfs_flush_space(ip, &fsynced, &ioflag))
695 return XFS_ERROR(ENOSPC);
697 error = 0;
698 goto retry;
701 if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
702 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
704 *ret_imap = imap[0];
705 *nmaps = 1;
707 return 0;
711 * Pass in a delayed allocate extent, convert it to real extents;
712 * return to the caller the extent we create which maps on top of
713 * the originating callers request.
715 * Called without a lock on the inode.
718 xfs_iomap_write_allocate(
719 xfs_inode_t *ip,
720 xfs_off_t offset,
721 size_t count,
722 xfs_bmbt_irec_t *map,
723 int *retmap)
725 xfs_mount_t *mp = ip->i_mount;
726 xfs_iocore_t *io = &ip->i_iocore;
727 xfs_fileoff_t offset_fsb, last_block;
728 xfs_fileoff_t end_fsb, map_start_fsb;
729 xfs_fsblock_t first_block;
730 xfs_bmap_free_t free_list;
731 xfs_filblks_t count_fsb;
732 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
733 xfs_trans_t *tp;
734 int i, nimaps, committed;
735 int error = 0;
736 int nres;
738 *retmap = 0;
741 * Make sure that the dquots are there.
743 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
744 return XFS_ERROR(error);
746 offset_fsb = XFS_B_TO_FSBT(mp, offset);
747 count_fsb = map->br_blockcount;
748 map_start_fsb = map->br_startoff;
750 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
752 while (count_fsb != 0) {
754 * Set up a transaction with which to allocate the
755 * backing store for the file. Do allocations in a
756 * loop until we get some space in the range we are
757 * interested in. The other space that might be allocated
758 * is in the delayed allocation extent on which we sit
759 * but before our buffer starts.
762 nimaps = 0;
763 while (nimaps == 0) {
764 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
765 tp->t_flags |= XFS_TRANS_RESERVE;
766 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
767 error = xfs_trans_reserve(tp, nres,
768 XFS_WRITE_LOG_RES(mp),
769 0, XFS_TRANS_PERM_LOG_RES,
770 XFS_WRITE_LOG_COUNT);
771 if (error) {
772 xfs_trans_cancel(tp, 0);
773 return XFS_ERROR(error);
775 xfs_ilock(ip, XFS_ILOCK_EXCL);
776 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
777 xfs_trans_ihold(tp, ip);
779 XFS_BMAP_INIT(&free_list, &first_block);
781 nimaps = XFS_STRAT_WRITE_IMAPS;
783 * Ensure we don't go beyond eof - it is possible
784 * the extents changed since we did the read call,
785 * we dropped the ilock in the interim.
788 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
789 xfs_bmap_last_offset(NULL, ip, &last_block,
790 XFS_DATA_FORK);
791 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
792 if ((map_start_fsb + count_fsb) > last_block) {
793 count_fsb = last_block - map_start_fsb;
794 if (count_fsb == 0) {
795 error = EAGAIN;
796 goto trans_cancel;
800 /* Go get the actual blocks */
801 error = XFS_BMAPI(mp, tp, io, map_start_fsb, count_fsb,
802 XFS_BMAPI_WRITE, &first_block, 1,
803 imap, &nimaps, &free_list, NULL);
804 if (error)
805 goto trans_cancel;
807 error = xfs_bmap_finish(&tp, &free_list, &committed);
808 if (error)
809 goto trans_cancel;
811 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
812 if (error)
813 goto error0;
815 xfs_iunlock(ip, XFS_ILOCK_EXCL);
819 * See if we were able to allocate an extent that
820 * covers at least part of the callers request
822 for (i = 0; i < nimaps; i++) {
823 if (unlikely(!imap[i].br_startblock &&
824 !(io->io_flags & XFS_IOCORE_RT)))
825 return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
826 if ((offset_fsb >= imap[i].br_startoff) &&
827 (offset_fsb < (imap[i].br_startoff +
828 imap[i].br_blockcount))) {
829 *map = imap[i];
830 *retmap = 1;
831 XFS_STATS_INC(xs_xstrat_quick);
832 return 0;
834 count_fsb -= imap[i].br_blockcount;
837 /* So far we have not mapped the requested part of the
838 * file, just surrounding data, try again.
840 nimaps--;
841 map_start_fsb = imap[nimaps].br_startoff +
842 imap[nimaps].br_blockcount;
845 trans_cancel:
846 xfs_bmap_cancel(&free_list);
847 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
848 error0:
849 xfs_iunlock(ip, XFS_ILOCK_EXCL);
850 return XFS_ERROR(error);
854 xfs_iomap_write_unwritten(
855 xfs_inode_t *ip,
856 xfs_off_t offset,
857 size_t count)
859 xfs_mount_t *mp = ip->i_mount;
860 xfs_iocore_t *io = &ip->i_iocore;
861 xfs_fileoff_t offset_fsb;
862 xfs_filblks_t count_fsb;
863 xfs_filblks_t numblks_fsb;
864 xfs_fsblock_t firstfsb;
865 int nimaps;
866 xfs_trans_t *tp;
867 xfs_bmbt_irec_t imap;
868 xfs_bmap_free_t free_list;
869 uint resblks;
870 int committed;
871 int error;
873 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
874 &ip->i_iocore, offset, count);
876 offset_fsb = XFS_B_TO_FSBT(mp, offset);
877 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
878 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
880 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
882 do {
884 * set up a transaction to convert the range of extents
885 * from unwritten to real. Do allocations in a loop until
886 * we have covered the range passed in.
888 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
889 tp->t_flags |= XFS_TRANS_RESERVE;
890 error = xfs_trans_reserve(tp, resblks,
891 XFS_WRITE_LOG_RES(mp), 0,
892 XFS_TRANS_PERM_LOG_RES,
893 XFS_WRITE_LOG_COUNT);
894 if (error) {
895 xfs_trans_cancel(tp, 0);
896 return XFS_ERROR(error);
899 xfs_ilock(ip, XFS_ILOCK_EXCL);
900 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
901 xfs_trans_ihold(tp, ip);
904 * Modify the unwritten extent state of the buffer.
906 XFS_BMAP_INIT(&free_list, &firstfsb);
907 nimaps = 1;
908 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb,
909 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
910 1, &imap, &nimaps, &free_list, NULL);
911 if (error)
912 goto error_on_bmapi_transaction;
914 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
915 if (error)
916 goto error_on_bmapi_transaction;
918 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
919 xfs_iunlock(ip, XFS_ILOCK_EXCL);
920 if (error)
921 return XFS_ERROR(error);
923 if (unlikely(!imap.br_startblock &&
924 !(io->io_flags & XFS_IOCORE_RT)))
925 return xfs_cmn_err_fsblock_zero(ip, &imap);
927 if ((numblks_fsb = imap.br_blockcount) == 0) {
929 * The numblks_fsb value should always get
930 * smaller, otherwise the loop is stuck.
932 ASSERT(imap.br_blockcount);
933 break;
935 offset_fsb += numblks_fsb;
936 count_fsb -= numblks_fsb;
937 } while (count_fsb > 0);
939 return 0;
941 error_on_bmapi_transaction:
942 xfs_bmap_cancel(&free_list);
943 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
944 xfs_iunlock(ip, XFS_ILOCK_EXCL);
945 return XFS_ERROR(error);