[PATCH] FRV: Permit large kmalloc allocations [try #2]
[linux-2.6/mini2440.git] / fs / xfs / xfs_iomap.c
blob19655124da782299a409eee8bb78ed26f676ceae
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_cap.h"
47 #include "xfs_mac.h"
48 #include "xfs_attr.h"
49 #include "xfs_buf_item.h"
50 #include "xfs_trans_space.h"
51 #include "xfs_utils.h"
52 #include "xfs_iomap.h"
54 #if defined(XFS_RW_TRACE)
55 void
56 xfs_iomap_enter_trace(
57 int tag,
58 xfs_iocore_t *io,
59 xfs_off_t offset,
60 ssize_t count)
62 xfs_inode_t *ip = XFS_IO_INODE(io);
64 if (!ip->i_rwtrace)
65 return;
67 ktrace_enter(ip->i_rwtrace,
68 (void *)((unsigned long)tag),
69 (void *)ip,
70 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
71 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
72 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
73 (void *)((unsigned long)(offset & 0xffffffff)),
74 (void *)((unsigned long)count),
75 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
76 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
77 (void *)((unsigned long)current_pid()),
78 (void *)NULL,
79 (void *)NULL,
80 (void *)NULL,
81 (void *)NULL,
82 (void *)NULL,
83 (void *)NULL);
86 void
87 xfs_iomap_map_trace(
88 int tag,
89 xfs_iocore_t *io,
90 xfs_off_t offset,
91 ssize_t count,
92 xfs_iomap_t *iomapp,
93 xfs_bmbt_irec_t *imapp,
94 int flags)
96 xfs_inode_t *ip = XFS_IO_INODE(io);
98 if (!ip->i_rwtrace)
99 return;
101 ktrace_enter(ip->i_rwtrace,
102 (void *)((unsigned long)tag),
103 (void *)ip,
104 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
106 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(offset & 0xffffffff)),
108 (void *)((unsigned long)count),
109 (void *)((unsigned long)flags),
110 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
111 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
112 (void *)((unsigned long)(iomapp->iomap_delta)),
113 (void *)((unsigned long)(iomapp->iomap_bsize)),
114 (void *)((unsigned long)(iomapp->iomap_bn)),
115 (void *)(__psint_t)(imapp->br_startoff),
116 (void *)((unsigned long)(imapp->br_blockcount)),
117 (void *)(__psint_t)(imapp->br_startblock));
119 #else
120 #define xfs_iomap_enter_trace(tag, io, offset, count)
121 #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
122 #endif
124 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
125 << mp->m_writeio_log)
126 #define XFS_STRAT_WRITE_IMAPS 2
127 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
129 STATIC int
130 xfs_imap_to_bmap(
131 xfs_iocore_t *io,
132 xfs_off_t offset,
133 xfs_bmbt_irec_t *imap,
134 xfs_iomap_t *iomapp,
135 int imaps, /* Number of imap entries */
136 int iomaps, /* Number of iomap entries */
137 int flags)
139 xfs_mount_t *mp;
140 xfs_fsize_t nisize;
141 int pbm;
142 xfs_fsblock_t start_block;
144 mp = io->io_mount;
145 nisize = XFS_SIZE(mp, io);
146 if (io->io_new_size > nisize)
147 nisize = io->io_new_size;
149 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
150 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
151 iomapp->iomap_delta = offset - iomapp->iomap_offset;
152 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
153 iomapp->iomap_flags = flags;
155 if (io->io_flags & XFS_IOCORE_RT) {
156 iomapp->iomap_flags |= IOMAP_REALTIME;
157 iomapp->iomap_target = mp->m_rtdev_targp;
158 } else {
159 iomapp->iomap_target = mp->m_ddev_targp;
161 start_block = imap->br_startblock;
162 if (start_block == HOLESTARTBLOCK) {
163 iomapp->iomap_bn = IOMAP_DADDR_NULL;
164 iomapp->iomap_flags |= IOMAP_HOLE;
165 } else if (start_block == DELAYSTARTBLOCK) {
166 iomapp->iomap_bn = IOMAP_DADDR_NULL;
167 iomapp->iomap_flags |= IOMAP_DELAY;
168 } else {
169 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
170 if (ISUNWRITTEN(imap))
171 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
174 if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
175 iomapp->iomap_flags |= IOMAP_EOF;
178 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
180 return pbm; /* Return the number filled */
184 xfs_iomap(
185 xfs_iocore_t *io,
186 xfs_off_t offset,
187 ssize_t count,
188 int flags,
189 xfs_iomap_t *iomapp,
190 int *niomaps)
192 xfs_mount_t *mp = io->io_mount;
193 xfs_fileoff_t offset_fsb, end_fsb;
194 int error = 0;
195 int lockmode = 0;
196 xfs_bmbt_irec_t imap;
197 int nimaps = 1;
198 int bmapi_flags = 0;
199 int iomap_flags = 0;
201 if (XFS_FORCED_SHUTDOWN(mp))
202 return XFS_ERROR(EIO);
204 switch (flags &
205 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
206 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
207 case BMAPI_READ:
208 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
209 lockmode = XFS_LCK_MAP_SHARED(mp, io);
210 bmapi_flags = XFS_BMAPI_ENTIRE;
211 break;
212 case BMAPI_WRITE:
213 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
214 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
215 if (flags & BMAPI_IGNSTATE)
216 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
217 XFS_ILOCK(mp, io, lockmode);
218 break;
219 case BMAPI_ALLOCATE:
220 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
221 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
222 bmapi_flags = XFS_BMAPI_ENTIRE;
223 /* Attempt non-blocking lock */
224 if (flags & BMAPI_TRYLOCK) {
225 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
226 return XFS_ERROR(EAGAIN);
227 } else {
228 XFS_ILOCK(mp, io, lockmode);
230 break;
231 case BMAPI_UNWRITTEN:
232 goto phase2;
233 case BMAPI_DEVICE:
234 lockmode = XFS_LCK_MAP_SHARED(mp, io);
235 iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
236 mp->m_rtdev_targp : mp->m_ddev_targp;
237 error = 0;
238 *niomaps = 1;
239 goto out;
240 default:
241 BUG();
244 ASSERT(offset <= mp->m_maxioffset);
245 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
246 count = mp->m_maxioffset - offset;
247 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
248 offset_fsb = XFS_B_TO_FSBT(mp, offset);
250 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
251 (xfs_filblks_t)(end_fsb - offset_fsb),
252 bmapi_flags, NULL, 0, &imap,
253 &nimaps, NULL, NULL);
255 if (error)
256 goto out;
258 phase2:
259 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
260 case BMAPI_WRITE:
261 /* If we found an extent, return it */
262 if (nimaps &&
263 (imap.br_startblock != HOLESTARTBLOCK) &&
264 (imap.br_startblock != DELAYSTARTBLOCK)) {
265 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
266 offset, count, iomapp, &imap, flags);
267 break;
270 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
271 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
272 count, flags, &imap, &nimaps, nimaps);
273 } else {
274 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
275 flags, &imap, &nimaps);
277 if (!error) {
278 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
279 offset, count, iomapp, &imap, flags);
281 iomap_flags = IOMAP_NEW;
282 break;
283 case BMAPI_ALLOCATE:
284 /* If we found an extent, return it */
285 XFS_IUNLOCK(mp, io, lockmode);
286 lockmode = 0;
288 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
289 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
290 offset, count, iomapp, &imap, flags);
291 break;
294 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
295 &imap, &nimaps);
296 break;
297 case BMAPI_UNWRITTEN:
298 lockmode = 0;
299 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
300 nimaps = 0;
301 break;
304 if (nimaps) {
305 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
306 iomapp, nimaps, *niomaps, iomap_flags);
307 } else if (niomaps) {
308 *niomaps = 0;
311 out:
312 if (lockmode)
313 XFS_IUNLOCK(mp, io, lockmode);
314 return XFS_ERROR(error);
317 STATIC int
318 xfs_iomap_eof_align_last_fsb(
319 xfs_mount_t *mp,
320 xfs_iocore_t *io,
321 xfs_fsize_t isize,
322 xfs_extlen_t extsize,
323 xfs_fileoff_t *last_fsb)
325 xfs_fileoff_t new_last_fsb = 0;
326 xfs_extlen_t align;
327 int eof, error;
329 if (io->io_flags & XFS_IOCORE_RT)
332 * If mounted with the "-o swalloc" option, roundup the allocation
333 * request to a stripe width boundary if the file size is >=
334 * stripe width and we are allocating past the allocation eof.
336 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
337 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
338 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
340 * Roundup the allocation request to a stripe unit (m_dalign) boundary
341 * if the file size is >= stripe unit size, and we are allocating past
342 * the allocation eof.
344 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
345 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
348 * Always round up the allocation request to an extent boundary
349 * (when file on a real-time subvolume or has di_extsize hint).
351 if (extsize) {
352 if (new_last_fsb)
353 align = roundup_64(new_last_fsb, extsize);
354 else
355 align = extsize;
356 new_last_fsb = roundup_64(*last_fsb, align);
359 if (new_last_fsb) {
360 error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
361 if (error)
362 return error;
363 if (eof)
364 *last_fsb = new_last_fsb;
366 return 0;
369 STATIC int
370 xfs_flush_space(
371 xfs_inode_t *ip,
372 int *fsynced,
373 int *ioflags)
375 switch (*fsynced) {
376 case 0:
377 if (ip->i_delayed_blks) {
378 xfs_iunlock(ip, XFS_ILOCK_EXCL);
379 xfs_flush_inode(ip);
380 xfs_ilock(ip, XFS_ILOCK_EXCL);
381 *fsynced = 1;
382 } else {
383 *ioflags |= BMAPI_SYNC;
384 *fsynced = 2;
386 return 0;
387 case 1:
388 *fsynced = 2;
389 *ioflags |= BMAPI_SYNC;
390 return 0;
391 case 2:
392 xfs_iunlock(ip, XFS_ILOCK_EXCL);
393 xfs_flush_device(ip);
394 xfs_ilock(ip, XFS_ILOCK_EXCL);
395 *fsynced = 3;
396 return 0;
398 return 1;
401 STATIC int
402 xfs_cmn_err_fsblock_zero(
403 xfs_inode_t *ip,
404 xfs_bmbt_irec_t *imap)
406 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
407 "Access to block zero in inode %llu "
408 "start_block: %llx start_off: %llx "
409 "blkcnt: %llx extent-state: %x\n",
410 (unsigned long long)ip->i_ino,
411 (unsigned long long)imap->br_startblock,
412 (unsigned long long)imap->br_startoff,
413 (unsigned long long)imap->br_blockcount,
414 imap->br_state);
415 return EFSCORRUPTED;
419 xfs_iomap_write_direct(
420 xfs_inode_t *ip,
421 xfs_off_t offset,
422 size_t count,
423 int flags,
424 xfs_bmbt_irec_t *ret_imap,
425 int *nmaps,
426 int found)
428 xfs_mount_t *mp = ip->i_mount;
429 xfs_iocore_t *io = &ip->i_iocore;
430 xfs_fileoff_t offset_fsb;
431 xfs_fileoff_t last_fsb;
432 xfs_filblks_t count_fsb, resaligned;
433 xfs_fsblock_t firstfsb;
434 xfs_extlen_t extsz, temp;
435 xfs_fsize_t isize;
436 int nimaps;
437 int bmapi_flag;
438 int quota_flag;
439 int rt;
440 xfs_trans_t *tp;
441 xfs_bmbt_irec_t imap;
442 xfs_bmap_free_t free_list;
443 uint qblocks, resblks, resrtextents;
444 int committed;
445 int error;
448 * Make sure that the dquots are there. This doesn't hold
449 * the ilock across a disk read.
451 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
452 if (error)
453 return XFS_ERROR(error);
455 rt = XFS_IS_REALTIME_INODE(ip);
456 if (unlikely(rt)) {
457 if (!(extsz = ip->i_d.di_extsize))
458 extsz = mp->m_sb.sb_rextsize;
459 } else {
460 extsz = ip->i_d.di_extsize;
463 isize = ip->i_d.di_size;
464 if (io->io_new_size > isize)
465 isize = io->io_new_size;
467 offset_fsb = XFS_B_TO_FSBT(mp, offset);
468 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
469 if ((offset + count) > isize) {
470 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
471 &last_fsb);
472 if (error)
473 goto error_out;
474 } else {
475 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
476 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
477 ret_imap->br_blockcount +
478 ret_imap->br_startoff);
480 count_fsb = last_fsb - offset_fsb;
481 ASSERT(count_fsb > 0);
483 resaligned = count_fsb;
484 if (unlikely(extsz)) {
485 if ((temp = do_mod(offset_fsb, extsz)))
486 resaligned += temp;
487 if ((temp = do_mod(resaligned, extsz)))
488 resaligned += extsz - temp;
491 if (unlikely(rt)) {
492 resrtextents = qblocks = resaligned;
493 resrtextents /= mp->m_sb.sb_rextsize;
494 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
495 quota_flag = XFS_QMOPT_RES_RTBLKS;
496 } else {
497 resrtextents = 0;
498 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
499 quota_flag = XFS_QMOPT_RES_REGBLKS;
503 * Allocate and setup the transaction
505 xfs_iunlock(ip, XFS_ILOCK_EXCL);
506 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
507 error = xfs_trans_reserve(tp, resblks,
508 XFS_WRITE_LOG_RES(mp), resrtextents,
509 XFS_TRANS_PERM_LOG_RES,
510 XFS_WRITE_LOG_COUNT);
512 * Check for running out of space, note: need lock to return
514 if (error)
515 xfs_trans_cancel(tp, 0);
516 xfs_ilock(ip, XFS_ILOCK_EXCL);
517 if (error)
518 goto error_out;
520 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
521 qblocks, 0, quota_flag);
522 if (error)
523 goto error1;
525 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
526 xfs_trans_ihold(tp, ip);
528 bmapi_flag = XFS_BMAPI_WRITE;
529 if ((flags & BMAPI_DIRECT) && (offset < ip->i_d.di_size || extsz))
530 bmapi_flag |= XFS_BMAPI_PREALLOC;
533 * Issue the xfs_bmapi() call to allocate the blocks
535 XFS_BMAP_INIT(&free_list, &firstfsb);
536 nimaps = 1;
537 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb, bmapi_flag,
538 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
539 if (error)
540 goto error0;
543 * Complete the transaction
545 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
546 if (error)
547 goto error0;
548 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
549 if (error)
550 goto error_out;
553 * Copy any maps to caller's array and return any error.
555 if (nimaps == 0) {
556 error = ENOSPC;
557 goto error_out;
560 if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
561 error = xfs_cmn_err_fsblock_zero(ip, &imap);
562 goto error_out;
565 *ret_imap = imap;
566 *nmaps = 1;
567 return 0;
569 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
570 xfs_bmap_cancel(&free_list);
571 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
573 error1: /* Just cancel transaction */
574 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
575 *nmaps = 0; /* nothing set-up here */
577 error_out:
578 return XFS_ERROR(error);
582 * If the caller is doing a write at the end of the file,
583 * then extend the allocation out to the file system's write
584 * iosize. We clean up any extra space left over when the
585 * file is closed in xfs_inactive().
587 * For sync writes, we are flushing delayed allocate space to
588 * try to make additional space available for allocation near
589 * the filesystem full boundary - preallocation hurts in that
590 * situation, of course.
592 STATIC int
593 xfs_iomap_eof_want_preallocate(
594 xfs_mount_t *mp,
595 xfs_iocore_t *io,
596 xfs_fsize_t isize,
597 xfs_off_t offset,
598 size_t count,
599 int ioflag,
600 xfs_bmbt_irec_t *imap,
601 int nimaps,
602 int *prealloc)
604 xfs_fileoff_t start_fsb;
605 xfs_filblks_t count_fsb;
606 xfs_fsblock_t firstblock;
607 int n, error, imaps;
609 *prealloc = 0;
610 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
611 return 0;
614 * If there are any real blocks past eof, then don't
615 * do any speculative allocation.
617 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
618 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
619 while (count_fsb > 0) {
620 imaps = nimaps;
621 firstblock = NULLFSBLOCK;
622 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb, 0,
623 &firstblock, 0, imap, &imaps, NULL, NULL);
624 if (error)
625 return error;
626 for (n = 0; n < imaps; n++) {
627 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
628 (imap[n].br_startblock != DELAYSTARTBLOCK))
629 return 0;
630 start_fsb += imap[n].br_blockcount;
631 count_fsb -= imap[n].br_blockcount;
634 *prealloc = 1;
635 return 0;
639 xfs_iomap_write_delay(
640 xfs_inode_t *ip,
641 xfs_off_t offset,
642 size_t count,
643 int ioflag,
644 xfs_bmbt_irec_t *ret_imap,
645 int *nmaps)
647 xfs_mount_t *mp = ip->i_mount;
648 xfs_iocore_t *io = &ip->i_iocore;
649 xfs_fileoff_t offset_fsb;
650 xfs_fileoff_t last_fsb;
651 xfs_off_t aligned_offset;
652 xfs_fileoff_t ioalign;
653 xfs_fsblock_t firstblock;
654 xfs_extlen_t extsz;
655 xfs_fsize_t isize;
656 int nimaps;
657 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
658 int prealloc, fsynced = 0;
659 int error;
661 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
664 * Make sure that the dquots are there. This doesn't hold
665 * the ilock across a disk read.
667 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
668 if (error)
669 return XFS_ERROR(error);
671 if (XFS_IS_REALTIME_INODE(ip)) {
672 if (!(extsz = ip->i_d.di_extsize))
673 extsz = mp->m_sb.sb_rextsize;
674 } else {
675 extsz = ip->i_d.di_extsize;
678 offset_fsb = XFS_B_TO_FSBT(mp, offset);
680 retry:
681 isize = ip->i_d.di_size;
682 if (io->io_new_size > isize)
683 isize = io->io_new_size;
685 error = xfs_iomap_eof_want_preallocate(mp, io, isize, offset, count,
686 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
687 if (error)
688 return error;
690 if (prealloc) {
691 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
692 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
693 last_fsb = ioalign + mp->m_writeio_blocks;
694 } else {
695 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
698 if (prealloc || extsz) {
699 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
700 &last_fsb);
701 if (error)
702 return error;
705 nimaps = XFS_WRITE_IMAPS;
706 firstblock = NULLFSBLOCK;
707 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
708 (xfs_filblks_t)(last_fsb - offset_fsb),
709 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
710 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
711 &nimaps, NULL, NULL);
712 if (error && (error != ENOSPC))
713 return XFS_ERROR(error);
716 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
717 * then we must have run out of space - flush delalloc, and retry..
719 if (nimaps == 0) {
720 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
721 io, offset, count);
722 if (xfs_flush_space(ip, &fsynced, &ioflag))
723 return XFS_ERROR(ENOSPC);
725 error = 0;
726 goto retry;
729 if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
730 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
732 *ret_imap = imap[0];
733 *nmaps = 1;
735 return 0;
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.
743 * Called without a lock on the inode.
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)
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;
766 *retmap = 0;
769 * Make sure that the dquots are there.
771 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
772 return XFS_ERROR(error);
774 offset_fsb = XFS_B_TO_FSBT(mp, offset);
775 count_fsb = map->br_blockcount;
776 map_start_fsb = map->br_startoff;
778 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
780 while (count_fsb != 0) {
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.
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),
802 XFS_TRANS_PERM_LOG_RES,
803 XFS_WRITE_LOG_COUNT);
805 if (error) {
806 xfs_trans_cancel(tp, 0);
807 return XFS_ERROR(error);
809 xfs_ilock(ip, XFS_ILOCK_EXCL);
810 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
811 xfs_trans_ihold(tp, ip);
813 XFS_BMAP_INIT(&free_list, &first_block);
815 nimaps = XFS_STRAT_WRITE_IMAPS;
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.
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;
834 /* Go get the actual blocks */
835 error = XFS_BMAPI(mp, tp, io, map_start_fsb, count_fsb,
836 XFS_BMAPI_WRITE, &first_block, 1,
837 imap, &nimaps, &free_list, NULL);
838 if (error)
839 goto trans_cancel;
841 error = xfs_bmap_finish(&tp, &free_list,
842 first_block, &committed);
843 if (error)
844 goto trans_cancel;
846 error = xfs_trans_commit(tp,
847 XFS_TRANS_RELEASE_LOG_RES, NULL);
848 if (error)
849 goto error0;
851 xfs_iunlock(ip, XFS_ILOCK_EXCL);
855 * See if we were able to allocate an extent that
856 * covers at least part of the callers request
858 for (i = 0; i < nimaps; i++) {
859 if (unlikely(!imap[i].br_startblock &&
860 !(io->io_flags & XFS_IOCORE_RT)))
861 return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
862 if ((offset_fsb >= imap[i].br_startoff) &&
863 (offset_fsb < (imap[i].br_startoff +
864 imap[i].br_blockcount))) {
865 *map = imap[i];
866 *retmap = 1;
867 XFS_STATS_INC(xs_xstrat_quick);
868 return 0;
870 count_fsb -= imap[i].br_blockcount;
873 /* So far we have not mapped the requested part of the
874 * file, just surrounding data, try again.
876 nimaps--;
877 map_start_fsb = imap[nimaps].br_startoff +
878 imap[nimaps].br_blockcount;
881 trans_cancel:
882 xfs_bmap_cancel(&free_list);
883 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
884 error0:
885 xfs_iunlock(ip, XFS_ILOCK_EXCL);
886 return XFS_ERROR(error);
890 xfs_iomap_write_unwritten(
891 xfs_inode_t *ip,
892 xfs_off_t offset,
893 size_t count)
895 xfs_mount_t *mp = ip->i_mount;
896 xfs_iocore_t *io = &ip->i_iocore;
897 xfs_fileoff_t offset_fsb;
898 xfs_filblks_t count_fsb;
899 xfs_filblks_t numblks_fsb;
900 xfs_fsblock_t firstfsb;
901 int nimaps;
902 xfs_trans_t *tp;
903 xfs_bmbt_irec_t imap;
904 xfs_bmap_free_t free_list;
905 uint resblks;
906 int committed;
907 int error;
909 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
910 &ip->i_iocore, offset, count);
912 offset_fsb = XFS_B_TO_FSBT(mp, offset);
913 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
914 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
916 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
918 do {
920 * set up a transaction to convert the range of extents
921 * from unwritten to real. Do allocations in a loop until
922 * we have covered the range passed in.
925 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
926 error = xfs_trans_reserve(tp, resblks,
927 XFS_WRITE_LOG_RES(mp), 0,
928 XFS_TRANS_PERM_LOG_RES,
929 XFS_WRITE_LOG_COUNT);
930 if (error) {
931 xfs_trans_cancel(tp, 0);
932 return XFS_ERROR(error);
935 xfs_ilock(ip, XFS_ILOCK_EXCL);
936 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
937 xfs_trans_ihold(tp, ip);
940 * Modify the unwritten extent state of the buffer.
942 XFS_BMAP_INIT(&free_list, &firstfsb);
943 nimaps = 1;
944 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb,
945 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
946 1, &imap, &nimaps, &free_list, NULL);
947 if (error)
948 goto error_on_bmapi_transaction;
950 error = xfs_bmap_finish(&(tp), &(free_list),
951 firstfsb, &committed);
952 if (error)
953 goto error_on_bmapi_transaction;
955 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
956 xfs_iunlock(ip, XFS_ILOCK_EXCL);
957 if (error)
958 return XFS_ERROR(error);
960 if (unlikely(!imap.br_startblock &&
961 !(io->io_flags & XFS_IOCORE_RT)))
962 return xfs_cmn_err_fsblock_zero(ip, &imap);
964 if ((numblks_fsb = imap.br_blockcount) == 0) {
966 * The numblks_fsb value should always get
967 * smaller, otherwise the loop is stuck.
969 ASSERT(imap.br_blockcount);
970 break;
972 offset_fsb += numblks_fsb;
973 count_fsb -= numblks_fsb;
974 } while (count_fsb > 0);
976 return 0;
978 error_on_bmapi_transaction:
979 xfs_bmap_cancel(&free_list);
980 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
981 xfs_iunlock(ip, XFS_ILOCK_EXCL);
982 return XFS_ERROR(error);