blob: 3f2b9f2a7b949d01e0f01f74b21e9b42fd76ae35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_ialloc.h"
39#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#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"
51
52#if defined(XFS_RW_TRACE)
53void
54xfs_iomap_enter_trace(
55 int tag,
56 xfs_iocore_t *io,
57 xfs_off_t offset,
58 ssize_t count)
59{
60 xfs_inode_t *ip = XFS_IO_INODE(io);
61
62 if (!ip->i_rwtrace)
63 return;
64
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)),
Yingping Luf1fdc842006-03-22 12:44:15 +110075 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 (void *)NULL,
77 (void *)NULL,
78 (void *)NULL,
79 (void *)NULL,
80 (void *)NULL,
81 (void *)NULL);
82}
83
84void
85xfs_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)
93{
94 xfs_inode_t *ip = XFS_IO_INODE(io);
95
96 if (!ip->i_rwtrace)
97 return;
98
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));
116}
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
121
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
126
127STATIC int
128xfs_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)
136{
137 xfs_mount_t *mp;
138 xfs_fsize_t nisize;
139 int pbm;
140 xfs_fsblock_t start_block;
141
142 mp = io->io_mount;
143 nisize = XFS_SIZE(mp, io);
144 if (io->io_new_size > nisize)
145 nisize = io->io_new_size;
146
147 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
148 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
149 iomapp->iomap_delta = offset - iomapp->iomap_offset;
150 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
151 iomapp->iomap_flags = flags;
152
153 if (io->io_flags & XFS_IOCORE_RT) {
154 iomapp->iomap_flags |= IOMAP_REALTIME;
155 iomapp->iomap_target = mp->m_rtdev_targp;
156 } else {
157 iomapp->iomap_target = mp->m_ddev_targp;
158 }
159 start_block = imap->br_startblock;
160 if (start_block == HOLESTARTBLOCK) {
161 iomapp->iomap_bn = IOMAP_DADDR_NULL;
162 iomapp->iomap_flags |= IOMAP_HOLE;
163 } else if (start_block == DELAYSTARTBLOCK) {
164 iomapp->iomap_bn = IOMAP_DADDR_NULL;
165 iomapp->iomap_flags |= IOMAP_DELAY;
166 } else {
167 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
168 if (ISUNWRITTEN(imap))
169 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
170 }
171
172 if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
173 iomapp->iomap_flags |= IOMAP_EOF;
174 }
175
176 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
177 }
178 return pbm; /* Return the number filled */
179}
180
181int
182xfs_iomap(
183 xfs_iocore_t *io,
184 xfs_off_t offset,
185 ssize_t count,
186 int flags,
187 xfs_iomap_t *iomapp,
188 int *niomaps)
189{
190 xfs_mount_t *mp = io->io_mount;
191 xfs_fileoff_t offset_fsb, end_fsb;
192 int error = 0;
193 int lockmode = 0;
194 xfs_bmbt_irec_t imap;
195 int nimaps = 1;
196 int bmapi_flags = 0;
197 int iomap_flags = 0;
198
199 if (XFS_FORCED_SHUTDOWN(mp))
200 return XFS_ERROR(EIO);
201
202 switch (flags &
203 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
204 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
205 case BMAPI_READ:
206 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
207 lockmode = XFS_LCK_MAP_SHARED(mp, io);
208 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 break;
210 case BMAPI_WRITE:
211 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
212 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
Nathan Scottc31e8872005-09-05 10:06:55 +1000213 if (flags & BMAPI_IGNSTATE)
214 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 XFS_ILOCK(mp, io, lockmode);
216 break;
217 case BMAPI_ALLOCATE:
218 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
219 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
220 bmapi_flags = XFS_BMAPI_ENTIRE;
221 /* Attempt non-blocking lock */
222 if (flags & BMAPI_TRYLOCK) {
223 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
224 return XFS_ERROR(EAGAIN);
225 } else {
226 XFS_ILOCK(mp, io, lockmode);
227 }
228 break;
229 case BMAPI_UNWRITTEN:
230 goto phase2;
231 case BMAPI_DEVICE:
232 lockmode = XFS_LCK_MAP_SHARED(mp, io);
233 iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
234 mp->m_rtdev_targp : mp->m_ddev_targp;
235 error = 0;
236 *niomaps = 1;
237 goto out;
238 default:
239 BUG();
240 }
241
242 ASSERT(offset <= mp->m_maxioffset);
243 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
244 count = mp->m_maxioffset - offset;
245 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
246 offset_fsb = XFS_B_TO_FSBT(mp, offset);
247
248 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
249 (xfs_filblks_t)(end_fsb - offset_fsb),
250 bmapi_flags, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000251 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (error)
254 goto out;
255
256phase2:
257 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
258 case BMAPI_WRITE:
259 /* If we found an extent, return it */
Russell Cattelan68d14982005-05-06 06:42:22 -0700260 if (nimaps &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100261 (imap.br_startblock != HOLESTARTBLOCK) &&
Russell Cattelan68d14982005-05-06 06:42:22 -0700262 (imap.br_startblock != DELAYSTARTBLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
264 offset, count, iomapp, &imap, flags);
265 break;
266 }
267
268 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
269 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
270 count, flags, &imap, &nimaps, nimaps);
271 } else {
272 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
273 flags, &imap, &nimaps);
274 }
275 if (!error) {
276 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
277 offset, count, iomapp, &imap, flags);
278 }
279 iomap_flags = IOMAP_NEW;
280 break;
281 case BMAPI_ALLOCATE:
282 /* If we found an extent, return it */
283 XFS_IUNLOCK(mp, io, lockmode);
284 lockmode = 0;
285
286 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
287 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
288 offset, count, iomapp, &imap, flags);
289 break;
290 }
291
Nathan Scott24e17b52005-05-05 13:33:20 -0700292 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
293 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295 case BMAPI_UNWRITTEN:
296 lockmode = 0;
297 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
298 nimaps = 0;
299 break;
300 }
301
302 if (nimaps) {
303 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
304 iomapp, nimaps, *niomaps, iomap_flags);
305 } else if (niomaps) {
306 *niomaps = 0;
307 }
308
309out:
310 if (lockmode)
311 XFS_IUNLOCK(mp, io, lockmode);
312 return XFS_ERROR(error);
313}
314
315STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100316xfs_iomap_eof_align_last_fsb(
317 xfs_mount_t *mp,
318 xfs_iocore_t *io,
319 xfs_fsize_t isize,
320 xfs_extlen_t extsize,
321 xfs_fileoff_t *last_fsb)
322{
323 xfs_fileoff_t new_last_fsb = 0;
324 xfs_extlen_t align;
325 int eof, error;
326
327 if (io->io_flags & XFS_IOCORE_RT)
328 ;
329 /*
330 * If mounted with the "-o swalloc" option, roundup the allocation
331 * request to a stripe width boundary if the file size is >=
332 * stripe width and we are allocating past the allocation eof.
333 */
334 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
335 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
336 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
337 /*
338 * Roundup the allocation request to a stripe unit (m_dalign) boundary
339 * if the file size is >= stripe unit size, and we are allocating past
340 * the allocation eof.
341 */
342 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
343 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
344
345 /*
346 * Always round up the allocation request to an extent boundary
347 * (when file on a real-time subvolume or has di_extsize hint).
348 */
349 if (extsize) {
350 if (new_last_fsb)
351 align = roundup_64(new_last_fsb, extsize);
352 else
353 align = extsize;
354 new_last_fsb = roundup_64(*last_fsb, align);
355 }
356
357 if (new_last_fsb) {
358 error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
359 if (error)
360 return error;
361 if (eof)
362 *last_fsb = new_last_fsb;
363 }
364 return 0;
365}
366
367STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368xfs_flush_space(
369 xfs_inode_t *ip,
370 int *fsynced,
371 int *ioflags)
372{
373 switch (*fsynced) {
374 case 0:
375 if (ip->i_delayed_blks) {
376 xfs_iunlock(ip, XFS_ILOCK_EXCL);
377 xfs_flush_inode(ip);
378 xfs_ilock(ip, XFS_ILOCK_EXCL);
379 *fsynced = 1;
380 } else {
381 *ioflags |= BMAPI_SYNC;
382 *fsynced = 2;
383 }
384 return 0;
385 case 1:
386 *fsynced = 2;
387 *ioflags |= BMAPI_SYNC;
388 return 0;
389 case 2:
390 xfs_iunlock(ip, XFS_ILOCK_EXCL);
391 xfs_flush_device(ip);
392 xfs_ilock(ip, XFS_ILOCK_EXCL);
393 *fsynced = 3;
394 return 0;
395 }
396 return 1;
397}
398
Nathan Scott572d95f2006-09-28 11:03:20 +1000399STATIC int
400xfs_cmn_err_fsblock_zero(
401 xfs_inode_t *ip,
402 xfs_bmbt_irec_t *imap)
403{
404 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
405 "Access to block zero in inode %llu "
406 "start_block: %llx start_off: %llx "
407 "blkcnt: %llx extent-state: %x\n",
408 (unsigned long long)ip->i_ino,
409 (unsigned long long)imap->br_startblock,
410 (unsigned long long)imap->br_startoff,
411 (unsigned long long)imap->br_blockcount,
412 imap->br_state);
413 return EFSCORRUPTED;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416int
417xfs_iomap_write_direct(
418 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700419 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 size_t count,
421 int flags,
422 xfs_bmbt_irec_t *ret_imap,
423 int *nmaps,
424 int found)
425{
426 xfs_mount_t *mp = ip->i_mount;
427 xfs_iocore_t *io = &ip->i_iocore;
428 xfs_fileoff_t offset_fsb;
429 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100430 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100432 xfs_extlen_t extsz, temp;
433 xfs_fsize_t isize;
Eric Sandeen0116d932005-11-02 15:00:01 +1100434 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000436 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int rt;
438 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100439 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100441 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100443 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /*
446 * Make sure that the dquots are there. This doesn't hold
447 * the ilock across a disk read.
448 */
449 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
450 if (error)
451 return XFS_ERROR(error);
452
Nathan Scottdd9f4382006-01-11 15:28:28 +1100453 rt = XFS_IS_REALTIME_INODE(ip);
454 if (unlikely(rt)) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000455 if (!(extsz = ip->i_d.di_extsize))
456 extsz = mp->m_sb.sb_rextsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +1100458 extsz = ip->i_d.di_extsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000461 isize = ip->i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100462 if (io->io_new_size > isize)
463 isize = io->io_new_size;
464
465 offset_fsb = XFS_B_TO_FSBT(mp, offset);
466 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
467 if ((offset + count) > isize) {
468 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
469 &last_fsb);
470 if (error)
471 goto error_out;
472 } else {
473 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
474 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
475 ret_imap->br_blockcount +
476 ret_imap->br_startoff);
477 }
478 count_fsb = last_fsb - offset_fsb;
479 ASSERT(count_fsb > 0);
480
481 resaligned = count_fsb;
482 if (unlikely(extsz)) {
483 if ((temp = do_mod(offset_fsb, extsz)))
484 resaligned += temp;
485 if ((temp = do_mod(resaligned, extsz)))
486 resaligned += extsz - temp;
487 }
488
489 if (unlikely(rt)) {
490 resrtextents = qblocks = resaligned;
491 resrtextents /= mp->m_sb.sb_rextsize;
492 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
493 quota_flag = XFS_QMOPT_RES_RTBLKS;
494 } else {
495 resrtextents = 0;
496 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
497 quota_flag = XFS_QMOPT_RES_REGBLKS;
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000501 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
503 xfs_iunlock(ip, XFS_ILOCK_EXCL);
504 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000506 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 XFS_TRANS_PERM_LOG_RES,
508 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000510 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
512 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000516 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Nathan Scottdd9f4382006-01-11 15:28:28 +1100518 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
519 qblocks, 0, quota_flag);
520 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
524 xfs_trans_ihold(tp, ip);
525
Nathan Scottdd9f4382006-01-11 15:28:28 +1100526 bmapi_flag = XFS_BMAPI_WRITE;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000527 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 bmapi_flag |= XFS_BMAPI_PREALLOC;
529
530 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100531 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
533 XFS_BMAP_INIT(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000534 nimaps = 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000535 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb, bmapi_flag,
536 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000537 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000541 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100543 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000544 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000546 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000547 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Nathan Scott06d10dd2005-06-21 15:48:47 +1000550 /*
551 * Copy any maps to caller's array and return any error.
552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000554 error = ENOSPC;
555 goto error_out;
556 }
557
558 if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
559 error = xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto error_out;
561 }
562
Eric Sandeen0116d932005-11-02 15:00:01 +1100563 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 *nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return 0;
566
Nathan Scott06d10dd2005-06-21 15:48:47 +1000567error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 xfs_bmap_cancel(&free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000569 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Nathan Scott06d10dd2005-06-21 15:48:47 +1000571error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
573 *nmaps = 0; /* nothing set-up here */
574
575error_out:
576 return XFS_ERROR(error);
577}
578
Nathan Scottdd9f4382006-01-11 15:28:28 +1100579/*
580 * If the caller is doing a write at the end of the file,
581 * then extend the allocation out to the file system's write
582 * iosize. We clean up any extra space left over when the
583 * file is closed in xfs_inactive().
584 *
585 * For sync writes, we are flushing delayed allocate space to
586 * try to make additional space available for allocation near
587 * the filesystem full boundary - preallocation hurts in that
588 * situation, of course.
589 */
590STATIC int
591xfs_iomap_eof_want_preallocate(
592 xfs_mount_t *mp,
593 xfs_iocore_t *io,
594 xfs_fsize_t isize,
595 xfs_off_t offset,
596 size_t count,
597 int ioflag,
598 xfs_bmbt_irec_t *imap,
599 int nimaps,
600 int *prealloc)
601{
602 xfs_fileoff_t start_fsb;
603 xfs_filblks_t count_fsb;
604 xfs_fsblock_t firstblock;
605 int n, error, imaps;
606
607 *prealloc = 0;
608 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
609 return 0;
610
611 /*
612 * If there are any real blocks past eof, then don't
613 * do any speculative allocation.
614 */
615 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
616 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
617 while (count_fsb > 0) {
618 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100619 firstblock = NULLFSBLOCK;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000620 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb, 0,
621 &firstblock, 0, imap, &imaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100622 if (error)
623 return error;
624 for (n = 0; n < imaps; n++) {
625 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
626 (imap[n].br_startblock != DELAYSTARTBLOCK))
627 return 0;
628 start_fsb += imap[n].br_blockcount;
629 count_fsb -= imap[n].br_blockcount;
630 }
631 }
632 *prealloc = 1;
633 return 0;
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636int
637xfs_iomap_write_delay(
638 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700639 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 size_t count,
641 int ioflag,
642 xfs_bmbt_irec_t *ret_imap,
643 int *nmaps)
644{
645 xfs_mount_t *mp = ip->i_mount;
646 xfs_iocore_t *io = &ip->i_iocore;
647 xfs_fileoff_t offset_fsb;
648 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100649 xfs_off_t aligned_offset;
650 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100652 xfs_extlen_t extsz;
653 xfs_fsize_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Nathan Scottdd9f4382006-01-11 15:28:28 +1100656 int prealloc, fsynced = 0;
657 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
660
661 /*
662 * Make sure that the dquots are there. This doesn't hold
663 * the ilock across a disk read.
664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
666 if (error)
667 return XFS_ERROR(error);
668
Nathan Scottdd9f4382006-01-11 15:28:28 +1100669 if (XFS_IS_REALTIME_INODE(ip)) {
670 if (!(extsz = ip->i_d.di_extsize))
671 extsz = mp->m_sb.sb_rextsize;
672 } else {
673 extsz = ip->i_d.di_extsize;
674 }
675
676 offset_fsb = XFS_B_TO_FSBT(mp, offset);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678retry:
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000679 isize = ip->i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100680 if (io->io_new_size > isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 isize = io->io_new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Nathan Scottdd9f4382006-01-11 15:28:28 +1100683 error = xfs_iomap_eof_want_preallocate(mp, io, isize, offset, count,
684 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
685 if (error)
686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Nathan Scottdd9f4382006-01-11 15:28:28 +1100688 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
690 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100691 last_fsb = ioalign + mp->m_writeio_blocks;
692 } else {
693 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100695
696 if (prealloc || extsz) {
697 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
698 &last_fsb);
699 if (error)
700 return error;
701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 nimaps = XFS_WRITE_IMAPS;
704 firstblock = NULLFSBLOCK;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000705 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 (xfs_filblks_t)(last_fsb - offset_fsb),
707 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
708 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000709 &nimaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100710 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
714 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100715 * then we must have run out of space - flush delalloc, and retry..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
717 if (nimaps == 0) {
718 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
719 io, offset, count);
720 if (xfs_flush_space(ip, &fsynced, &ioflag))
721 return XFS_ERROR(ENOSPC);
722
723 error = 0;
724 goto retry;
725 }
726
Nathan Scott572d95f2006-09-28 11:03:20 +1000727 if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
728 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100729
730 *ret_imap = imap[0];
731 *nmaps = 1;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734}
735
736/*
737 * Pass in a delayed allocate extent, convert it to real extents;
738 * return to the caller the extent we create which maps on top of
739 * the originating callers request.
740 *
741 * Called without a lock on the inode.
742 */
743int
744xfs_iomap_write_allocate(
745 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700746 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700747 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfs_bmbt_irec_t *map,
749 int *retmap)
750{
751 xfs_mount_t *mp = ip->i_mount;
752 xfs_iocore_t *io = &ip->i_iocore;
753 xfs_fileoff_t offset_fsb, last_block;
754 xfs_fileoff_t end_fsb, map_start_fsb;
755 xfs_fsblock_t first_block;
756 xfs_bmap_free_t free_list;
757 xfs_filblks_t count_fsb;
758 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
759 xfs_trans_t *tp;
760 int i, nimaps, committed;
761 int error = 0;
762 int nres;
763
764 *retmap = 0;
765
766 /*
767 * Make sure that the dquots are there.
768 */
769 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
770 return XFS_ERROR(error);
771
Nathan Scott24e17b52005-05-05 13:33:20 -0700772 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700774 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
777
778 while (count_fsb != 0) {
779 /*
780 * Set up a transaction with which to allocate the
781 * backing store for the file. Do allocations in a
782 * loop until we get some space in the range we are
783 * interested in. The other space that might be allocated
784 * is in the delayed allocation extent on which we sit
785 * but before our buffer starts.
786 */
787
788 nimaps = 0;
789 while (nimaps == 0) {
790 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
791 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
792 error = xfs_trans_reserve(tp, nres,
793 XFS_WRITE_LOG_RES(mp),
794 0, XFS_TRANS_PERM_LOG_RES,
795 XFS_WRITE_LOG_COUNT);
796 if (error == ENOSPC) {
797 error = xfs_trans_reserve(tp, 0,
798 XFS_WRITE_LOG_RES(mp),
799 0,
800 XFS_TRANS_PERM_LOG_RES,
801 XFS_WRITE_LOG_COUNT);
802 }
803 if (error) {
804 xfs_trans_cancel(tp, 0);
805 return XFS_ERROR(error);
806 }
807 xfs_ilock(ip, XFS_ILOCK_EXCL);
808 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
809 xfs_trans_ihold(tp, ip);
810
811 XFS_BMAP_INIT(&free_list, &first_block);
812
813 nimaps = XFS_STRAT_WRITE_IMAPS;
814 /*
815 * Ensure we don't go beyond eof - it is possible
816 * the extents changed since we did the read call,
817 * we dropped the ilock in the interim.
818 */
819
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000820 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 xfs_bmap_last_offset(NULL, ip, &last_block,
822 XFS_DATA_FORK);
823 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
824 if ((map_start_fsb + count_fsb) > last_block) {
825 count_fsb = last_block - map_start_fsb;
826 if (count_fsb == 0) {
827 error = EAGAIN;
828 goto trans_cancel;
829 }
830 }
831
832 /* Go get the actual blocks */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000833 error = XFS_BMAPI(mp, tp, io, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 XFS_BMAPI_WRITE, &first_block, 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000835 imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (error)
837 goto trans_cancel;
838
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100839 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (error)
841 goto trans_cancel;
842
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000843 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (error)
845 goto error0;
846
847 xfs_iunlock(ip, XFS_ILOCK_EXCL);
848 }
849
850 /*
851 * See if we were able to allocate an extent that
852 * covers at least part of the callers request
853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 for (i = 0; i < nimaps; i++) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000855 if (unlikely(!imap[i].br_startblock &&
856 !(io->io_flags & XFS_IOCORE_RT)))
857 return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
Nathan Scott24e17b52005-05-05 13:33:20 -0700858 if ((offset_fsb >= imap[i].br_startoff) &&
859 (offset_fsb < (imap[i].br_startoff +
860 imap[i].br_blockcount))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 *map = imap[i];
862 *retmap = 1;
863 XFS_STATS_INC(xs_xstrat_quick);
864 return 0;
865 }
866 count_fsb -= imap[i].br_blockcount;
867 }
868
869 /* So far we have not mapped the requested part of the
870 * file, just surrounding data, try again.
871 */
872 nimaps--;
Nathan Scott24e17b52005-05-05 13:33:20 -0700873 map_start_fsb = imap[nimaps].br_startoff +
874 imap[nimaps].br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
877trans_cancel:
878 xfs_bmap_cancel(&free_list);
879 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
880error0:
881 xfs_iunlock(ip, XFS_ILOCK_EXCL);
882 return XFS_ERROR(error);
883}
884
885int
886xfs_iomap_write_unwritten(
887 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700888 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 size_t count)
890{
891 xfs_mount_t *mp = ip->i_mount;
892 xfs_iocore_t *io = &ip->i_iocore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 xfs_fileoff_t offset_fsb;
894 xfs_filblks_t count_fsb;
895 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100896 xfs_fsblock_t firstfsb;
897 int nimaps;
898 xfs_trans_t *tp;
899 xfs_bmbt_irec_t imap;
900 xfs_bmap_free_t free_list;
901 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int committed;
903 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
906 &ip->i_iocore, offset, count);
907
908 offset_fsb = XFS_B_TO_FSBT(mp, offset);
909 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
910 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
911
Nathan Scottdd9f4382006-01-11 15:28:28 +1100912 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Nathan Scottdd9f4382006-01-11 15:28:28 +1100914 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
916 * set up a transaction to convert the range of extents
917 * from unwritten to real. Do allocations in a loop until
918 * we have covered the range passed in.
919 */
920
921 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100922 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 XFS_WRITE_LOG_RES(mp), 0,
924 XFS_TRANS_PERM_LOG_RES,
925 XFS_WRITE_LOG_COUNT);
926 if (error) {
927 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000928 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
931 xfs_ilock(ip, XFS_ILOCK_EXCL);
932 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
933 xfs_trans_ihold(tp, ip);
934
935 /*
936 * Modify the unwritten extent state of the buffer.
937 */
938 XFS_BMAP_INIT(&free_list, &firstfsb);
939 nimaps = 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000940 error = XFS_BMAPI(mp, tp, io, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100941 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000942 1, &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (error)
944 goto error_on_bmapi_transaction;
945
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100946 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (error)
948 goto error_on_bmapi_transaction;
949
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000950 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 xfs_iunlock(ip, XFS_ILOCK_EXCL);
952 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000953 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100954
Nathan Scott572d95f2006-09-28 11:03:20 +1000955 if (unlikely(!imap.br_startblock &&
956 !(io->io_flags & XFS_IOCORE_RT)))
957 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if ((numblks_fsb = imap.br_blockcount) == 0) {
960 /*
961 * The numblks_fsb value should always get
962 * smaller, otherwise the loop is stuck.
963 */
964 ASSERT(imap.br_blockcount);
965 break;
966 }
967 offset_fsb += numblks_fsb;
968 count_fsb -= numblks_fsb;
969 } while (count_fsb > 0);
970
971 return 0;
972
973error_on_bmapi_transaction:
974 xfs_bmap_cancel(&free_list);
975 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
976 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return XFS_ERROR(error);
978}