blob: d5dfedcb8922a2b506b772f65ff1e5d1cd848f80 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * 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"
26#include "xfs_dir.h"
27#include "xfs_dir2.h"
28#include "xfs_alloc.h"
29#include "xfs_dmapi.h"
30#include "xfs_quota.h"
31#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir_sf.h"
36#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_dinode.h"
39#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_ialloc.h"
41#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_rtalloc.h"
44#include "xfs_error.h"
45#include "xfs_itable.h"
46#include "xfs_rw.h"
47#include "xfs_acl.h"
48#include "xfs_cap.h"
49#include "xfs_mac.h"
50#include "xfs_attr.h"
51#include "xfs_buf_item.h"
52#include "xfs_trans_space.h"
53#include "xfs_utils.h"
54#include "xfs_iomap.h"
55
56#if defined(XFS_RW_TRACE)
57void
58xfs_iomap_enter_trace(
59 int tag,
60 xfs_iocore_t *io,
61 xfs_off_t offset,
62 ssize_t count)
63{
64 xfs_inode_t *ip = XFS_IO_INODE(io);
65
66 if (!ip->i_rwtrace)
67 return;
68
69 ktrace_enter(ip->i_rwtrace,
70 (void *)((unsigned long)tag),
71 (void *)ip,
72 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
73 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
74 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
75 (void *)((unsigned long)(offset & 0xffffffff)),
76 (void *)((unsigned long)count),
77 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110079 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 (void *)NULL,
81 (void *)NULL,
82 (void *)NULL,
83 (void *)NULL,
84 (void *)NULL,
85 (void *)NULL);
86}
87
88void
89xfs_iomap_map_trace(
90 int tag,
91 xfs_iocore_t *io,
92 xfs_off_t offset,
93 ssize_t count,
94 xfs_iomap_t *iomapp,
95 xfs_bmbt_irec_t *imapp,
96 int flags)
97{
98 xfs_inode_t *ip = XFS_IO_INODE(io);
99
100 if (!ip->i_rwtrace)
101 return;
102
103 ktrace_enter(ip->i_rwtrace,
104 (void *)((unsigned long)tag),
105 (void *)ip,
106 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
108 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(offset & 0xffffffff)),
110 (void *)((unsigned long)count),
111 (void *)((unsigned long)flags),
112 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
113 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
114 (void *)((unsigned long)(iomapp->iomap_delta)),
115 (void *)((unsigned long)(iomapp->iomap_bsize)),
116 (void *)((unsigned long)(iomapp->iomap_bn)),
117 (void *)(__psint_t)(imapp->br_startoff),
118 (void *)((unsigned long)(imapp->br_blockcount)),
119 (void *)(__psint_t)(imapp->br_startblock));
120}
121#else
122#define xfs_iomap_enter_trace(tag, io, offset, count)
123#define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
124#endif
125
126#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
127 << mp->m_writeio_log)
128#define XFS_STRAT_WRITE_IMAPS 2
129#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
130
131STATIC int
132xfs_imap_to_bmap(
133 xfs_iocore_t *io,
134 xfs_off_t offset,
135 xfs_bmbt_irec_t *imap,
136 xfs_iomap_t *iomapp,
137 int imaps, /* Number of imap entries */
138 int iomaps, /* Number of iomap entries */
139 int flags)
140{
141 xfs_mount_t *mp;
142 xfs_fsize_t nisize;
143 int pbm;
144 xfs_fsblock_t start_block;
145
146 mp = io->io_mount;
147 nisize = XFS_SIZE(mp, io);
148 if (io->io_new_size > nisize)
149 nisize = io->io_new_size;
150
151 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
152 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
153 iomapp->iomap_delta = offset - iomapp->iomap_offset;
154 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
155 iomapp->iomap_flags = flags;
156
157 if (io->io_flags & XFS_IOCORE_RT) {
158 iomapp->iomap_flags |= IOMAP_REALTIME;
159 iomapp->iomap_target = mp->m_rtdev_targp;
160 } else {
161 iomapp->iomap_target = mp->m_ddev_targp;
162 }
163 start_block = imap->br_startblock;
164 if (start_block == HOLESTARTBLOCK) {
165 iomapp->iomap_bn = IOMAP_DADDR_NULL;
166 iomapp->iomap_flags |= IOMAP_HOLE;
167 } else if (start_block == DELAYSTARTBLOCK) {
168 iomapp->iomap_bn = IOMAP_DADDR_NULL;
169 iomapp->iomap_flags |= IOMAP_DELAY;
170 } else {
171 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
172 if (ISUNWRITTEN(imap))
173 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
174 }
175
176 if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
177 iomapp->iomap_flags |= IOMAP_EOF;
178 }
179
180 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
181 }
182 return pbm; /* Return the number filled */
183}
184
185int
186xfs_iomap(
187 xfs_iocore_t *io,
188 xfs_off_t offset,
189 ssize_t count,
190 int flags,
191 xfs_iomap_t *iomapp,
192 int *niomaps)
193{
194 xfs_mount_t *mp = io->io_mount;
195 xfs_fileoff_t offset_fsb, end_fsb;
196 int error = 0;
197 int lockmode = 0;
198 xfs_bmbt_irec_t imap;
199 int nimaps = 1;
200 int bmapi_flags = 0;
201 int iomap_flags = 0;
202
203 if (XFS_FORCED_SHUTDOWN(mp))
204 return XFS_ERROR(EIO);
205
206 switch (flags &
207 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
208 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
209 case BMAPI_READ:
210 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
211 lockmode = XFS_LCK_MAP_SHARED(mp, io);
212 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 case BMAPI_WRITE:
215 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
216 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
Nathan Scottc31e8872005-09-05 10:06:55 +1000217 if (flags & BMAPI_IGNSTATE)
218 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 XFS_ILOCK(mp, io, lockmode);
220 break;
221 case BMAPI_ALLOCATE:
222 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
223 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
224 bmapi_flags = XFS_BMAPI_ENTIRE;
225 /* Attempt non-blocking lock */
226 if (flags & BMAPI_TRYLOCK) {
227 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
228 return XFS_ERROR(EAGAIN);
229 } else {
230 XFS_ILOCK(mp, io, lockmode);
231 }
232 break;
233 case BMAPI_UNWRITTEN:
234 goto phase2;
235 case BMAPI_DEVICE:
236 lockmode = XFS_LCK_MAP_SHARED(mp, io);
237 iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
238 mp->m_rtdev_targp : mp->m_ddev_targp;
239 error = 0;
240 *niomaps = 1;
241 goto out;
242 default:
243 BUG();
244 }
245
246 ASSERT(offset <= mp->m_maxioffset);
247 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
248 count = mp->m_maxioffset - offset;
249 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
250 offset_fsb = XFS_B_TO_FSBT(mp, offset);
251
252 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
253 (xfs_filblks_t)(end_fsb - offset_fsb),
254 bmapi_flags, NULL, 0, &imap,
255 &nimaps, NULL);
256
257 if (error)
258 goto out;
259
260phase2:
261 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
262 case BMAPI_WRITE:
263 /* If we found an extent, return it */
Russell Cattelan68d14982005-05-06 06:42:22 -0700264 if (nimaps &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100265 (imap.br_startblock != HOLESTARTBLOCK) &&
Russell Cattelan68d14982005-05-06 06:42:22 -0700266 (imap.br_startblock != DELAYSTARTBLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
268 offset, count, iomapp, &imap, flags);
269 break;
270 }
271
272 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
273 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
274 count, flags, &imap, &nimaps, nimaps);
275 } else {
276 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
277 flags, &imap, &nimaps);
278 }
279 if (!error) {
280 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
281 offset, count, iomapp, &imap, flags);
282 }
283 iomap_flags = IOMAP_NEW;
284 break;
285 case BMAPI_ALLOCATE:
286 /* If we found an extent, return it */
287 XFS_IUNLOCK(mp, io, lockmode);
288 lockmode = 0;
289
290 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
291 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
292 offset, count, iomapp, &imap, flags);
293 break;
294 }
295
Nathan Scott24e17b52005-05-05 13:33:20 -0700296 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
297 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
299 case BMAPI_UNWRITTEN:
300 lockmode = 0;
301 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
302 nimaps = 0;
303 break;
304 }
305
306 if (nimaps) {
307 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
308 iomapp, nimaps, *niomaps, iomap_flags);
309 } else if (niomaps) {
310 *niomaps = 0;
311 }
312
313out:
314 if (lockmode)
315 XFS_IUNLOCK(mp, io, lockmode);
316 return XFS_ERROR(error);
317}
318
319STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100320xfs_iomap_eof_align_last_fsb(
321 xfs_mount_t *mp,
322 xfs_iocore_t *io,
323 xfs_fsize_t isize,
324 xfs_extlen_t extsize,
325 xfs_fileoff_t *last_fsb)
326{
327 xfs_fileoff_t new_last_fsb = 0;
328 xfs_extlen_t align;
329 int eof, error;
330
331 if (io->io_flags & XFS_IOCORE_RT)
332 ;
333 /*
334 * If mounted with the "-o swalloc" option, roundup the allocation
335 * request to a stripe width boundary if the file size is >=
336 * stripe width and we are allocating past the allocation eof.
337 */
338 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
339 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
340 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
341 /*
342 * Roundup the allocation request to a stripe unit (m_dalign) boundary
343 * if the file size is >= stripe unit size, and we are allocating past
344 * the allocation eof.
345 */
346 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
347 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
348
349 /*
350 * Always round up the allocation request to an extent boundary
351 * (when file on a real-time subvolume or has di_extsize hint).
352 */
353 if (extsize) {
354 if (new_last_fsb)
355 align = roundup_64(new_last_fsb, extsize);
356 else
357 align = extsize;
358 new_last_fsb = roundup_64(*last_fsb, align);
359 }
360
361 if (new_last_fsb) {
362 error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
363 if (error)
364 return error;
365 if (eof)
366 *last_fsb = new_last_fsb;
367 }
368 return 0;
369}
370
371STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372xfs_flush_space(
373 xfs_inode_t *ip,
374 int *fsynced,
375 int *ioflags)
376{
377 switch (*fsynced) {
378 case 0:
379 if (ip->i_delayed_blks) {
380 xfs_iunlock(ip, XFS_ILOCK_EXCL);
381 xfs_flush_inode(ip);
382 xfs_ilock(ip, XFS_ILOCK_EXCL);
383 *fsynced = 1;
384 } else {
385 *ioflags |= BMAPI_SYNC;
386 *fsynced = 2;
387 }
388 return 0;
389 case 1:
390 *fsynced = 2;
391 *ioflags |= BMAPI_SYNC;
392 return 0;
393 case 2:
394 xfs_iunlock(ip, XFS_ILOCK_EXCL);
395 xfs_flush_device(ip);
396 xfs_ilock(ip, XFS_ILOCK_EXCL);
397 *fsynced = 3;
398 return 0;
399 }
400 return 1;
401}
402
403int
404xfs_iomap_write_direct(
405 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700406 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 size_t count,
408 int flags,
409 xfs_bmbt_irec_t *ret_imap,
410 int *nmaps,
411 int found)
412{
413 xfs_mount_t *mp = ip->i_mount;
414 xfs_iocore_t *io = &ip->i_iocore;
415 xfs_fileoff_t offset_fsb;
416 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100417 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100419 xfs_extlen_t extsz, temp;
420 xfs_fsize_t isize;
Eric Sandeen0116d932005-11-02 15:00:01 +1100421 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000423 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int rt;
425 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100426 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100428 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100430 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * Make sure that the dquots are there. This doesn't hold
434 * the ilock across a disk read.
435 */
436 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
437 if (error)
438 return XFS_ERROR(error);
439
Nathan Scottdd9f4382006-01-11 15:28:28 +1100440 rt = XFS_IS_REALTIME_INODE(ip);
441 if (unlikely(rt)) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000442 if (!(extsz = ip->i_d.di_extsize))
443 extsz = mp->m_sb.sb_rextsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +1100445 extsz = ip->i_d.di_extsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
Nathan Scottdd9f4382006-01-11 15:28:28 +1100448 isize = ip->i_d.di_size;
449 if (io->io_new_size > isize)
450 isize = io->io_new_size;
451
452 offset_fsb = XFS_B_TO_FSBT(mp, offset);
453 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
454 if ((offset + count) > isize) {
455 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
456 &last_fsb);
457 if (error)
458 goto error_out;
459 } else {
460 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
461 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
462 ret_imap->br_blockcount +
463 ret_imap->br_startoff);
464 }
465 count_fsb = last_fsb - offset_fsb;
466 ASSERT(count_fsb > 0);
467
468 resaligned = count_fsb;
469 if (unlikely(extsz)) {
470 if ((temp = do_mod(offset_fsb, extsz)))
471 resaligned += temp;
472 if ((temp = do_mod(resaligned, extsz)))
473 resaligned += extsz - temp;
474 }
475
476 if (unlikely(rt)) {
477 resrtextents = qblocks = resaligned;
478 resrtextents /= mp->m_sb.sb_rextsize;
479 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
480 quota_flag = XFS_QMOPT_RES_RTBLKS;
481 } else {
482 resrtextents = 0;
483 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
484 quota_flag = XFS_QMOPT_RES_REGBLKS;
485 }
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000488 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
490 xfs_iunlock(ip, XFS_ILOCK_EXCL);
491 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000493 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 XFS_TRANS_PERM_LOG_RES,
495 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000497 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
499 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000503 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Nathan Scottdd9f4382006-01-11 15:28:28 +1100505 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
506 qblocks, 0, quota_flag);
507 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
511 xfs_trans_ihold(tp, ip);
512
Nathan Scottdd9f4382006-01-11 15:28:28 +1100513 bmapi_flag = XFS_BMAPI_WRITE;
514 if ((flags & BMAPI_DIRECT) && (offset < ip->i_d.di_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 bmapi_flag |= XFS_BMAPI_PREALLOC;
516
517 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100518 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
520 XFS_BMAP_INIT(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000521 nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Eric Sandeen0116d932005-11-02 15:00:01 +1100523 bmapi_flag, &firstfsb, 0, &imap, &nimaps, &free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000524 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000528 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000531 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000534 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Nathan Scott06d10dd2005-06-21 15:48:47 +1000537 /*
538 * Copy any maps to caller's array and return any error.
539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (nimaps == 0) {
541 error = (ENOSPC);
542 goto error_out;
543 }
544
Eric Sandeen0116d932005-11-02 15:00:01 +1100545 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *nmaps = 1;
547 if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) {
548 cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld "
549 "start_block : %llx start_off : %llx blkcnt : %llx "
550 "extent-state : %x \n",
551 (ip->i_mount)->m_fsname,
552 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100553 (unsigned long long)ret_imap->br_startblock,
554 (unsigned long long)ret_imap->br_startoff,
555 (unsigned long long)ret_imap->br_blockcount,
556 ret_imap->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 return 0;
559
Nathan Scott06d10dd2005-06-21 15:48:47 +1000560error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xfs_bmap_cancel(&free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000562 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Nathan Scott06d10dd2005-06-21 15:48:47 +1000564error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
566 *nmaps = 0; /* nothing set-up here */
567
568error_out:
569 return XFS_ERROR(error);
570}
571
Nathan Scottdd9f4382006-01-11 15:28:28 +1100572/*
573 * If the caller is doing a write at the end of the file,
574 * then extend the allocation out to the file system's write
575 * iosize. We clean up any extra space left over when the
576 * file is closed in xfs_inactive().
577 *
578 * For sync writes, we are flushing delayed allocate space to
579 * try to make additional space available for allocation near
580 * the filesystem full boundary - preallocation hurts in that
581 * situation, of course.
582 */
583STATIC int
584xfs_iomap_eof_want_preallocate(
585 xfs_mount_t *mp,
586 xfs_iocore_t *io,
587 xfs_fsize_t isize,
588 xfs_off_t offset,
589 size_t count,
590 int ioflag,
591 xfs_bmbt_irec_t *imap,
592 int nimaps,
593 int *prealloc)
594{
595 xfs_fileoff_t start_fsb;
596 xfs_filblks_t count_fsb;
597 xfs_fsblock_t firstblock;
598 int n, error, imaps;
599
600 *prealloc = 0;
601 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
602 return 0;
603
604 /*
605 * If there are any real blocks past eof, then don't
606 * do any speculative allocation.
607 */
608 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
609 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
610 while (count_fsb > 0) {
611 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100612 firstblock = NULLFSBLOCK;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100613 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb,
614 0, &firstblock, 0, imap, &imaps, NULL);
615 if (error)
616 return error;
617 for (n = 0; n < imaps; n++) {
618 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
619 (imap[n].br_startblock != DELAYSTARTBLOCK))
620 return 0;
621 start_fsb += imap[n].br_blockcount;
622 count_fsb -= imap[n].br_blockcount;
623 }
624 }
625 *prealloc = 1;
626 return 0;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629int
630xfs_iomap_write_delay(
631 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700632 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 size_t count,
634 int ioflag,
635 xfs_bmbt_irec_t *ret_imap,
636 int *nmaps)
637{
638 xfs_mount_t *mp = ip->i_mount;
639 xfs_iocore_t *io = &ip->i_iocore;
640 xfs_fileoff_t offset_fsb;
641 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100642 xfs_off_t aligned_offset;
643 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100645 xfs_extlen_t extsz;
646 xfs_fsize_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Nathan Scottdd9f4382006-01-11 15:28:28 +1100649 int prealloc, fsynced = 0;
650 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
653
654 /*
655 * Make sure that the dquots are there. This doesn't hold
656 * the ilock across a disk read.
657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
659 if (error)
660 return XFS_ERROR(error);
661
Nathan Scottdd9f4382006-01-11 15:28:28 +1100662 if (XFS_IS_REALTIME_INODE(ip)) {
663 if (!(extsz = ip->i_d.di_extsize))
664 extsz = mp->m_sb.sb_rextsize;
665 } else {
666 extsz = ip->i_d.di_extsize;
667 }
668
669 offset_fsb = XFS_B_TO_FSBT(mp, offset);
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671retry:
672 isize = ip->i_d.di_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100673 if (io->io_new_size > isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 isize = io->io_new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Nathan Scottdd9f4382006-01-11 15:28:28 +1100676 error = xfs_iomap_eof_want_preallocate(mp, io, isize, offset, count,
677 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
678 if (error)
679 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Nathan Scottdd9f4382006-01-11 15:28:28 +1100681 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
683 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100684 last_fsb = ioalign + mp->m_writeio_blocks;
685 } else {
686 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100688
689 if (prealloc || extsz) {
690 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
691 &last_fsb);
692 if (error)
693 return error;
694 }
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 nimaps = XFS_WRITE_IMAPS;
697 firstblock = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 error = xfs_bmapi(NULL, ip, offset_fsb,
699 (xfs_filblks_t)(last_fsb - offset_fsb),
700 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
701 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
702 &nimaps, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100703 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /*
707 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100708 * then we must have run out of space - flush delalloc, and retry..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
710 if (nimaps == 0) {
711 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
712 io, offset, count);
713 if (xfs_flush_space(ip, &fsynced, &ioflag))
714 return XFS_ERROR(ENOSPC);
715
716 error = 0;
717 goto retry;
718 }
719
Nathan Scottdd9f4382006-01-11 15:28:28 +1100720 if (!(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld "
722 "start_block : %llx start_off : %llx blkcnt : %llx "
723 "extent-state : %x \n",
724 (ip->i_mount)->m_fsname,
725 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100726 (unsigned long long)ret_imap->br_startblock,
727 (unsigned long long)ret_imap->br_startoff,
728 (unsigned long long)ret_imap->br_blockcount,
729 ret_imap->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100731
732 *ret_imap = imap[0];
733 *nmaps = 1;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736}
737
738/*
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.
742 *
743 * Called without a lock on the inode.
744 */
745int
746xfs_iomap_write_allocate(
747 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700748 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700749 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 xfs_bmbt_irec_t *map,
751 int *retmap)
752{
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;
765
766 *retmap = 0;
767
768 /*
769 * Make sure that the dquots are there.
770 */
771 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
772 return XFS_ERROR(error);
773
Nathan Scott24e17b52005-05-05 13:33:20 -0700774 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700776 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
779
780 while (count_fsb != 0) {
781 /*
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.
788 */
789
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),
801 0,
802 XFS_TRANS_PERM_LOG_RES,
803 XFS_WRITE_LOG_COUNT);
804 }
805 if (error) {
806 xfs_trans_cancel(tp, 0);
807 return XFS_ERROR(error);
808 }
809 xfs_ilock(ip, XFS_ILOCK_EXCL);
810 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
811 xfs_trans_ihold(tp, ip);
812
813 XFS_BMAP_INIT(&free_list, &first_block);
814
815 nimaps = XFS_STRAT_WRITE_IMAPS;
816 /*
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.
820 */
821
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;
831 }
832 }
833
834 /* Go get the actual blocks */
835 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
836 XFS_BMAPI_WRITE, &first_block, 1,
837 imap, &nimaps, &free_list);
838 if (error)
839 goto trans_cancel;
840
841 error = xfs_bmap_finish(&tp, &free_list,
842 first_block, &committed);
843 if (error)
844 goto trans_cancel;
845
846 error = xfs_trans_commit(tp,
847 XFS_TRANS_RELEASE_LOG_RES, NULL);
848 if (error)
849 goto error0;
850
851 xfs_iunlock(ip, XFS_ILOCK_EXCL);
852 }
853
854 /*
855 * See if we were able to allocate an extent that
856 * covers at least part of the callers request
857 */
858
859 for (i = 0; i < nimaps; i++) {
Nathan Scott6ab65422006-01-11 21:03:28 +1100860 if (!(io->io_flags & XFS_IOCORE_RT) &&
861 !imap[i].br_startblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 cmn_err(CE_PANIC,"Access to block zero: "
863 "fs <%s> inode: %lld "
Nathan Scott6ab65422006-01-11 21:03:28 +1100864 "start_block : %llx start_off : %llx "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 "blkcnt : %llx extent-state : %x \n",
866 (ip->i_mount)->m_fsname,
867 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100868 (unsigned long long)
869 imap[i].br_startblock,
870 (unsigned long long)
871 imap[i].br_startoff,
872 (unsigned long long)
873 imap[i].br_blockcount,
874 imap[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Nathan Scott24e17b52005-05-05 13:33:20 -0700876 if ((offset_fsb >= imap[i].br_startoff) &&
877 (offset_fsb < (imap[i].br_startoff +
878 imap[i].br_blockcount))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *map = imap[i];
880 *retmap = 1;
881 XFS_STATS_INC(xs_xstrat_quick);
882 return 0;
883 }
884 count_fsb -= imap[i].br_blockcount;
885 }
886
887 /* So far we have not mapped the requested part of the
888 * file, just surrounding data, try again.
889 */
890 nimaps--;
Nathan Scott24e17b52005-05-05 13:33:20 -0700891 map_start_fsb = imap[nimaps].br_startoff +
892 imap[nimaps].br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
895trans_cancel:
896 xfs_bmap_cancel(&free_list);
897 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
898error0:
899 xfs_iunlock(ip, XFS_ILOCK_EXCL);
900 return XFS_ERROR(error);
901}
902
903int
904xfs_iomap_write_unwritten(
905 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700906 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 size_t count)
908{
909 xfs_mount_t *mp = ip->i_mount;
910 xfs_iocore_t *io = &ip->i_iocore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 xfs_fileoff_t offset_fsb;
912 xfs_filblks_t count_fsb;
913 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100914 xfs_fsblock_t firstfsb;
915 int nimaps;
916 xfs_trans_t *tp;
917 xfs_bmbt_irec_t imap;
918 xfs_bmap_free_t free_list;
919 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int committed;
921 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
924 &ip->i_iocore, offset, count);
925
926 offset_fsb = XFS_B_TO_FSBT(mp, offset);
927 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
928 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
929
Nathan Scottdd9f4382006-01-11 15:28:28 +1100930 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Nathan Scottdd9f4382006-01-11 15:28:28 +1100932 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * set up a transaction to convert the range of extents
935 * from unwritten to real. Do allocations in a loop until
936 * we have covered the range passed in.
937 */
938
939 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100940 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 XFS_WRITE_LOG_RES(mp), 0,
942 XFS_TRANS_PERM_LOG_RES,
943 XFS_WRITE_LOG_COUNT);
944 if (error) {
945 xfs_trans_cancel(tp, 0);
946 goto error0;
947 }
948
949 xfs_ilock(ip, XFS_ILOCK_EXCL);
950 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
951 xfs_trans_ihold(tp, ip);
952
953 /*
954 * Modify the unwritten extent state of the buffer.
955 */
956 XFS_BMAP_INIT(&free_list, &firstfsb);
957 nimaps = 1;
958 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100959 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 1, &imap, &nimaps, &free_list);
961 if (error)
962 goto error_on_bmapi_transaction;
963
964 error = xfs_bmap_finish(&(tp), &(free_list),
965 firstfsb, &committed);
966 if (error)
967 goto error_on_bmapi_transaction;
968
969 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
970 xfs_iunlock(ip, XFS_ILOCK_EXCL);
971 if (error)
972 goto error0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if ( !(io->io_flags & XFS_IOCORE_RT) && !imap.br_startblock) {
975 cmn_err(CE_PANIC,"Access to block zero: fs <%s> "
976 "inode: %lld start_block : %llx start_off : "
977 "%llx blkcnt : %llx extent-state : %x \n",
978 (ip->i_mount)->m_fsname,
979 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100980 (unsigned long long)imap.br_startblock,
981 (unsigned long long)imap.br_startoff,
982 (unsigned long long)imap.br_blockcount,
983 imap.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 if ((numblks_fsb = imap.br_blockcount) == 0) {
987 /*
988 * The numblks_fsb value should always get
989 * smaller, otherwise the loop is stuck.
990 */
991 ASSERT(imap.br_blockcount);
992 break;
993 }
994 offset_fsb += numblks_fsb;
995 count_fsb -= numblks_fsb;
996 } while (count_fsb > 0);
997
998 return 0;
999
1000error_on_bmapi_transaction:
1001 xfs_bmap_cancel(&free_list);
1002 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
1003 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1004error0:
1005 return XFS_ERROR(error);
1006}