blob: 5c6d873e292ca4110ffe24cfcfad0caeeaa523cf [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"
43#include "xfs_bit.h"
44#include "xfs_rtalloc.h"
45#include "xfs_error.h"
46#include "xfs_itable.h"
47#include "xfs_rw.h"
48#include "xfs_acl.h"
49#include "xfs_cap.h"
50#include "xfs_mac.h"
51#include "xfs_attr.h"
52#include "xfs_buf_item.h"
53#include "xfs_trans_space.h"
54#include "xfs_utils.h"
55#include "xfs_iomap.h"
56
57#if defined(XFS_RW_TRACE)
58void
59xfs_iomap_enter_trace(
60 int tag,
61 xfs_iocore_t *io,
62 xfs_off_t offset,
63 ssize_t count)
64{
65 xfs_inode_t *ip = XFS_IO_INODE(io);
66
67 if (!ip->i_rwtrace)
68 return;
69
70 ktrace_enter(ip->i_rwtrace,
71 (void *)((unsigned long)tag),
72 (void *)ip,
73 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
74 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
75 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
76 (void *)((unsigned long)(offset & 0xffffffff)),
77 (void *)((unsigned long)count),
78 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
79 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
80 (void *)NULL,
81 (void *)NULL,
82 (void *)NULL,
83 (void *)NULL,
84 (void *)NULL,
85 (void *)NULL,
86 (void *)NULL);
87}
88
89void
90xfs_iomap_map_trace(
91 int tag,
92 xfs_iocore_t *io,
93 xfs_off_t offset,
94 ssize_t count,
95 xfs_iomap_t *iomapp,
96 xfs_bmbt_irec_t *imapp,
97 int flags)
98{
99 xfs_inode_t *ip = XFS_IO_INODE(io);
100
101 if (!ip->i_rwtrace)
102 return;
103
104 ktrace_enter(ip->i_rwtrace,
105 (void *)((unsigned long)tag),
106 (void *)ip,
107 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
108 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
109 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
110 (void *)((unsigned long)(offset & 0xffffffff)),
111 (void *)((unsigned long)count),
112 (void *)((unsigned long)flags),
113 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
114 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
115 (void *)((unsigned long)(iomapp->iomap_delta)),
116 (void *)((unsigned long)(iomapp->iomap_bsize)),
117 (void *)((unsigned long)(iomapp->iomap_bn)),
118 (void *)(__psint_t)(imapp->br_startoff),
119 (void *)((unsigned long)(imapp->br_blockcount)),
120 (void *)(__psint_t)(imapp->br_startblock));
121}
122#else
123#define xfs_iomap_enter_trace(tag, io, offset, count)
124#define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
125#endif
126
127#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
128 << mp->m_writeio_log)
129#define XFS_STRAT_WRITE_IMAPS 2
130#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
131
132STATIC int
133xfs_imap_to_bmap(
134 xfs_iocore_t *io,
135 xfs_off_t offset,
136 xfs_bmbt_irec_t *imap,
137 xfs_iomap_t *iomapp,
138 int imaps, /* Number of imap entries */
139 int iomaps, /* Number of iomap entries */
140 int flags)
141{
142 xfs_mount_t *mp;
143 xfs_fsize_t nisize;
144 int pbm;
145 xfs_fsblock_t start_block;
146
147 mp = io->io_mount;
148 nisize = XFS_SIZE(mp, io);
149 if (io->io_new_size > nisize)
150 nisize = io->io_new_size;
151
152 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
153 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
154 iomapp->iomap_delta = offset - iomapp->iomap_offset;
155 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
156 iomapp->iomap_flags = flags;
157
158 if (io->io_flags & XFS_IOCORE_RT) {
159 iomapp->iomap_flags |= IOMAP_REALTIME;
160 iomapp->iomap_target = mp->m_rtdev_targp;
161 } else {
162 iomapp->iomap_target = mp->m_ddev_targp;
163 }
164 start_block = imap->br_startblock;
165 if (start_block == HOLESTARTBLOCK) {
166 iomapp->iomap_bn = IOMAP_DADDR_NULL;
167 iomapp->iomap_flags |= IOMAP_HOLE;
168 } else if (start_block == DELAYSTARTBLOCK) {
169 iomapp->iomap_bn = IOMAP_DADDR_NULL;
170 iomapp->iomap_flags |= IOMAP_DELAY;
171 } else {
172 iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block);
173 if (ISUNWRITTEN(imap))
174 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
175 }
176
177 if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) {
178 iomapp->iomap_flags |= IOMAP_EOF;
179 }
180
181 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
182 }
183 return pbm; /* Return the number filled */
184}
185
186int
187xfs_iomap(
188 xfs_iocore_t *io,
189 xfs_off_t offset,
190 ssize_t count,
191 int flags,
192 xfs_iomap_t *iomapp,
193 int *niomaps)
194{
195 xfs_mount_t *mp = io->io_mount;
196 xfs_fileoff_t offset_fsb, end_fsb;
197 int error = 0;
198 int lockmode = 0;
199 xfs_bmbt_irec_t imap;
200 int nimaps = 1;
201 int bmapi_flags = 0;
202 int iomap_flags = 0;
203
204 if (XFS_FORCED_SHUTDOWN(mp))
205 return XFS_ERROR(EIO);
206
207 switch (flags &
208 (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE |
209 BMAPI_UNWRITTEN | BMAPI_DEVICE)) {
210 case BMAPI_READ:
211 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count);
212 lockmode = XFS_LCK_MAP_SHARED(mp, io);
213 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 break;
215 case BMAPI_WRITE:
216 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count);
217 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
Nathan Scottc31e8872005-09-05 10:06:55 +1000218 if (flags & BMAPI_IGNSTATE)
219 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 XFS_ILOCK(mp, io, lockmode);
221 break;
222 case BMAPI_ALLOCATE:
223 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count);
224 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
225 bmapi_flags = XFS_BMAPI_ENTIRE;
226 /* Attempt non-blocking lock */
227 if (flags & BMAPI_TRYLOCK) {
228 if (!XFS_ILOCK_NOWAIT(mp, io, lockmode))
229 return XFS_ERROR(EAGAIN);
230 } else {
231 XFS_ILOCK(mp, io, lockmode);
232 }
233 break;
234 case BMAPI_UNWRITTEN:
235 goto phase2;
236 case BMAPI_DEVICE:
237 lockmode = XFS_LCK_MAP_SHARED(mp, io);
238 iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ?
239 mp->m_rtdev_targp : mp->m_ddev_targp;
240 error = 0;
241 *niomaps = 1;
242 goto out;
243 default:
244 BUG();
245 }
246
247 ASSERT(offset <= mp->m_maxioffset);
248 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
249 count = mp->m_maxioffset - offset;
250 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
251 offset_fsb = XFS_B_TO_FSBT(mp, offset);
252
253 error = XFS_BMAPI(mp, NULL, io, offset_fsb,
254 (xfs_filblks_t)(end_fsb - offset_fsb),
255 bmapi_flags, NULL, 0, &imap,
256 &nimaps, NULL);
257
258 if (error)
259 goto out;
260
261phase2:
262 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) {
263 case BMAPI_WRITE:
264 /* If we found an extent, return it */
Russell Cattelan68d14982005-05-06 06:42:22 -0700265 if (nimaps &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100266 (imap.br_startblock != HOLESTARTBLOCK) &&
Russell Cattelan68d14982005-05-06 06:42:22 -0700267 (imap.br_startblock != DELAYSTARTBLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
269 offset, count, iomapp, &imap, flags);
270 break;
271 }
272
273 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
274 error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset,
275 count, flags, &imap, &nimaps, nimaps);
276 } else {
277 error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count,
278 flags, &imap, &nimaps);
279 }
280 if (!error) {
281 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io,
282 offset, count, iomapp, &imap, flags);
283 }
284 iomap_flags = IOMAP_NEW;
285 break;
286 case BMAPI_ALLOCATE:
287 /* If we found an extent, return it */
288 XFS_IUNLOCK(mp, io, lockmode);
289 lockmode = 0;
290
291 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
292 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io,
293 offset, count, iomapp, &imap, flags);
294 break;
295 }
296
Nathan Scott24e17b52005-05-05 13:33:20 -0700297 error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count,
298 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 break;
300 case BMAPI_UNWRITTEN:
301 lockmode = 0;
302 error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count);
303 nimaps = 0;
304 break;
305 }
306
307 if (nimaps) {
308 *niomaps = xfs_imap_to_bmap(io, offset, &imap,
309 iomapp, nimaps, *niomaps, iomap_flags);
310 } else if (niomaps) {
311 *niomaps = 0;
312 }
313
314out:
315 if (lockmode)
316 XFS_IUNLOCK(mp, io, lockmode);
317 return XFS_ERROR(error);
318}
319
320STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100321xfs_iomap_eof_align_last_fsb(
322 xfs_mount_t *mp,
323 xfs_iocore_t *io,
324 xfs_fsize_t isize,
325 xfs_extlen_t extsize,
326 xfs_fileoff_t *last_fsb)
327{
328 xfs_fileoff_t new_last_fsb = 0;
329 xfs_extlen_t align;
330 int eof, error;
331
332 if (io->io_flags & XFS_IOCORE_RT)
333 ;
334 /*
335 * If mounted with the "-o swalloc" option, roundup the allocation
336 * request to a stripe width boundary if the file size is >=
337 * stripe width and we are allocating past the allocation eof.
338 */
339 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
340 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
341 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
342 /*
343 * Roundup the allocation request to a stripe unit (m_dalign) boundary
344 * if the file size is >= stripe unit size, and we are allocating past
345 * the allocation eof.
346 */
347 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
348 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
349
350 /*
351 * Always round up the allocation request to an extent boundary
352 * (when file on a real-time subvolume or has di_extsize hint).
353 */
354 if (extsize) {
355 if (new_last_fsb)
356 align = roundup_64(new_last_fsb, extsize);
357 else
358 align = extsize;
359 new_last_fsb = roundup_64(*last_fsb, align);
360 }
361
362 if (new_last_fsb) {
363 error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof);
364 if (error)
365 return error;
366 if (eof)
367 *last_fsb = new_last_fsb;
368 }
369 return 0;
370}
371
372STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373xfs_flush_space(
374 xfs_inode_t *ip,
375 int *fsynced,
376 int *ioflags)
377{
378 switch (*fsynced) {
379 case 0:
380 if (ip->i_delayed_blks) {
381 xfs_iunlock(ip, XFS_ILOCK_EXCL);
382 xfs_flush_inode(ip);
383 xfs_ilock(ip, XFS_ILOCK_EXCL);
384 *fsynced = 1;
385 } else {
386 *ioflags |= BMAPI_SYNC;
387 *fsynced = 2;
388 }
389 return 0;
390 case 1:
391 *fsynced = 2;
392 *ioflags |= BMAPI_SYNC;
393 return 0;
394 case 2:
395 xfs_iunlock(ip, XFS_ILOCK_EXCL);
396 xfs_flush_device(ip);
397 xfs_ilock(ip, XFS_ILOCK_EXCL);
398 *fsynced = 3;
399 return 0;
400 }
401 return 1;
402}
403
404int
405xfs_iomap_write_direct(
406 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700407 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 size_t count,
409 int flags,
410 xfs_bmbt_irec_t *ret_imap,
411 int *nmaps,
412 int found)
413{
414 xfs_mount_t *mp = ip->i_mount;
415 xfs_iocore_t *io = &ip->i_iocore;
416 xfs_fileoff_t offset_fsb;
417 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100418 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100420 xfs_extlen_t extsz, temp;
421 xfs_fsize_t isize;
Eric Sandeen0116d932005-11-02 15:00:01 +1100422 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000424 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int rt;
426 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100427 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100429 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100431 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /*
434 * Make sure that the dquots are there. This doesn't hold
435 * the ilock across a disk read.
436 */
437 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
438 if (error)
439 return XFS_ERROR(error);
440
Nathan Scottdd9f4382006-01-11 15:28:28 +1100441 rt = XFS_IS_REALTIME_INODE(ip);
442 if (unlikely(rt)) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000443 if (!(extsz = ip->i_d.di_extsize))
444 extsz = mp->m_sb.sb_rextsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +1100446 extsz = ip->i_d.di_extsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
Nathan Scottdd9f4382006-01-11 15:28:28 +1100449 isize = ip->i_d.di_size;
450 if (io->io_new_size > isize)
451 isize = io->io_new_size;
452
453 offset_fsb = XFS_B_TO_FSBT(mp, offset);
454 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
455 if ((offset + count) > isize) {
456 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
457 &last_fsb);
458 if (error)
459 goto error_out;
460 } else {
461 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
462 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
463 ret_imap->br_blockcount +
464 ret_imap->br_startoff);
465 }
466 count_fsb = last_fsb - offset_fsb;
467 ASSERT(count_fsb > 0);
468
469 resaligned = count_fsb;
470 if (unlikely(extsz)) {
471 if ((temp = do_mod(offset_fsb, extsz)))
472 resaligned += temp;
473 if ((temp = do_mod(resaligned, extsz)))
474 resaligned += extsz - temp;
475 }
476
477 if (unlikely(rt)) {
478 resrtextents = qblocks = resaligned;
479 resrtextents /= mp->m_sb.sb_rextsize;
480 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
481 quota_flag = XFS_QMOPT_RES_RTBLKS;
482 } else {
483 resrtextents = 0;
484 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
485 quota_flag = XFS_QMOPT_RES_REGBLKS;
486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000489 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
491 xfs_iunlock(ip, XFS_ILOCK_EXCL);
492 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000494 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 XFS_TRANS_PERM_LOG_RES,
496 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000498 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
500 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000504 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Nathan Scottdd9f4382006-01-11 15:28:28 +1100506 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
507 qblocks, 0, quota_flag);
508 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
512 xfs_trans_ihold(tp, ip);
513
Nathan Scottdd9f4382006-01-11 15:28:28 +1100514 bmapi_flag = XFS_BMAPI_WRITE;
515 if ((flags & BMAPI_DIRECT) && (offset < ip->i_d.di_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 bmapi_flag |= XFS_BMAPI_PREALLOC;
517
518 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100519 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
521 XFS_BMAP_INIT(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000522 nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Eric Sandeen0116d932005-11-02 15:00:01 +1100524 bmapi_flag, &firstfsb, 0, &imap, &nimaps, &free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000525 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000529 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000532 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000535 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Nathan Scott06d10dd2005-06-21 15:48:47 +1000538 /*
539 * Copy any maps to caller's array and return any error.
540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (nimaps == 0) {
542 error = (ENOSPC);
543 goto error_out;
544 }
545
Eric Sandeen0116d932005-11-02 15:00:01 +1100546 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *nmaps = 1;
548 if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) {
549 cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld "
550 "start_block : %llx start_off : %llx blkcnt : %llx "
551 "extent-state : %x \n",
552 (ip->i_mount)->m_fsname,
553 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100554 (unsigned long long)ret_imap->br_startblock,
555 (unsigned long long)ret_imap->br_startoff,
556 (unsigned long long)ret_imap->br_blockcount,
557 ret_imap->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 return 0;
560
Nathan Scott06d10dd2005-06-21 15:48:47 +1000561error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 xfs_bmap_cancel(&free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000563 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Nathan Scott06d10dd2005-06-21 15:48:47 +1000565error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
567 *nmaps = 0; /* nothing set-up here */
568
569error_out:
570 return XFS_ERROR(error);
571}
572
Nathan Scottdd9f4382006-01-11 15:28:28 +1100573/*
574 * If the caller is doing a write at the end of the file,
575 * then extend the allocation out to the file system's write
576 * iosize. We clean up any extra space left over when the
577 * file is closed in xfs_inactive().
578 *
579 * For sync writes, we are flushing delayed allocate space to
580 * try to make additional space available for allocation near
581 * the filesystem full boundary - preallocation hurts in that
582 * situation, of course.
583 */
584STATIC int
585xfs_iomap_eof_want_preallocate(
586 xfs_mount_t *mp,
587 xfs_iocore_t *io,
588 xfs_fsize_t isize,
589 xfs_off_t offset,
590 size_t count,
591 int ioflag,
592 xfs_bmbt_irec_t *imap,
593 int nimaps,
594 int *prealloc)
595{
596 xfs_fileoff_t start_fsb;
597 xfs_filblks_t count_fsb;
598 xfs_fsblock_t firstblock;
599 int n, error, imaps;
600
601 *prealloc = 0;
602 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
603 return 0;
604
605 /*
606 * If there are any real blocks past eof, then don't
607 * do any speculative allocation.
608 */
609 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
610 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
611 while (count_fsb > 0) {
612 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100613 firstblock = NULLFSBLOCK;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100614 error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb,
615 0, &firstblock, 0, imap, &imaps, NULL);
616 if (error)
617 return error;
618 for (n = 0; n < imaps; n++) {
619 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
620 (imap[n].br_startblock != DELAYSTARTBLOCK))
621 return 0;
622 start_fsb += imap[n].br_blockcount;
623 count_fsb -= imap[n].br_blockcount;
624 }
625 }
626 *prealloc = 1;
627 return 0;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630int
631xfs_iomap_write_delay(
632 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700633 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 size_t count,
635 int ioflag,
636 xfs_bmbt_irec_t *ret_imap,
637 int *nmaps)
638{
639 xfs_mount_t *mp = ip->i_mount;
640 xfs_iocore_t *io = &ip->i_iocore;
641 xfs_fileoff_t offset_fsb;
642 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100643 xfs_off_t aligned_offset;
644 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100646 xfs_extlen_t extsz;
647 xfs_fsize_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Nathan Scottdd9f4382006-01-11 15:28:28 +1100650 int prealloc, fsynced = 0;
651 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
654
655 /*
656 * Make sure that the dquots are there. This doesn't hold
657 * the ilock across a disk read.
658 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
660 if (error)
661 return XFS_ERROR(error);
662
Nathan Scottdd9f4382006-01-11 15:28:28 +1100663 if (XFS_IS_REALTIME_INODE(ip)) {
664 if (!(extsz = ip->i_d.di_extsize))
665 extsz = mp->m_sb.sb_rextsize;
666 } else {
667 extsz = ip->i_d.di_extsize;
668 }
669
670 offset_fsb = XFS_B_TO_FSBT(mp, offset);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672retry:
673 isize = ip->i_d.di_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100674 if (io->io_new_size > isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 isize = io->io_new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Nathan Scottdd9f4382006-01-11 15:28:28 +1100677 error = xfs_iomap_eof_want_preallocate(mp, io, isize, offset, count,
678 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
679 if (error)
680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Nathan Scottdd9f4382006-01-11 15:28:28 +1100682 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
684 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100685 last_fsb = ioalign + mp->m_writeio_blocks;
686 } else {
687 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100689
690 if (prealloc || extsz) {
691 error = xfs_iomap_eof_align_last_fsb(mp, io, isize, extsz,
692 &last_fsb);
693 if (error)
694 return error;
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 nimaps = XFS_WRITE_IMAPS;
698 firstblock = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 error = xfs_bmapi(NULL, ip, offset_fsb,
700 (xfs_filblks_t)(last_fsb - offset_fsb),
701 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
702 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
703 &nimaps, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100704 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
708 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100709 * then we must have run out of space - flush delalloc, and retry..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
711 if (nimaps == 0) {
712 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
713 io, offset, count);
714 if (xfs_flush_space(ip, &fsynced, &ioflag))
715 return XFS_ERROR(ENOSPC);
716
717 error = 0;
718 goto retry;
719 }
720
Nathan Scottdd9f4382006-01-11 15:28:28 +1100721 if (!(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld "
723 "start_block : %llx start_off : %llx blkcnt : %llx "
724 "extent-state : %x \n",
725 (ip->i_mount)->m_fsname,
726 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100727 (unsigned long long)ret_imap->br_startblock,
728 (unsigned long long)ret_imap->br_startoff,
729 (unsigned long long)ret_imap->br_blockcount,
730 ret_imap->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100732
733 *ret_imap = imap[0];
734 *nmaps = 1;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 0;
737}
738
739/*
740 * Pass in a delayed allocate extent, convert it to real extents;
741 * return to the caller the extent we create which maps on top of
742 * the originating callers request.
743 *
744 * Called without a lock on the inode.
745 */
746int
747xfs_iomap_write_allocate(
748 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700749 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700750 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 xfs_bmbt_irec_t *map,
752 int *retmap)
753{
754 xfs_mount_t *mp = ip->i_mount;
755 xfs_iocore_t *io = &ip->i_iocore;
756 xfs_fileoff_t offset_fsb, last_block;
757 xfs_fileoff_t end_fsb, map_start_fsb;
758 xfs_fsblock_t first_block;
759 xfs_bmap_free_t free_list;
760 xfs_filblks_t count_fsb;
761 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
762 xfs_trans_t *tp;
763 int i, nimaps, committed;
764 int error = 0;
765 int nres;
766
767 *retmap = 0;
768
769 /*
770 * Make sure that the dquots are there.
771 */
772 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
773 return XFS_ERROR(error);
774
Nathan Scott24e17b52005-05-05 13:33:20 -0700775 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700777 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
780
781 while (count_fsb != 0) {
782 /*
783 * Set up a transaction with which to allocate the
784 * backing store for the file. Do allocations in a
785 * loop until we get some space in the range we are
786 * interested in. The other space that might be allocated
787 * is in the delayed allocation extent on which we sit
788 * but before our buffer starts.
789 */
790
791 nimaps = 0;
792 while (nimaps == 0) {
793 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
794 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
795 error = xfs_trans_reserve(tp, nres,
796 XFS_WRITE_LOG_RES(mp),
797 0, XFS_TRANS_PERM_LOG_RES,
798 XFS_WRITE_LOG_COUNT);
799 if (error == ENOSPC) {
800 error = xfs_trans_reserve(tp, 0,
801 XFS_WRITE_LOG_RES(mp),
802 0,
803 XFS_TRANS_PERM_LOG_RES,
804 XFS_WRITE_LOG_COUNT);
805 }
806 if (error) {
807 xfs_trans_cancel(tp, 0);
808 return XFS_ERROR(error);
809 }
810 xfs_ilock(ip, XFS_ILOCK_EXCL);
811 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
812 xfs_trans_ihold(tp, ip);
813
814 XFS_BMAP_INIT(&free_list, &first_block);
815
816 nimaps = XFS_STRAT_WRITE_IMAPS;
817 /*
818 * Ensure we don't go beyond eof - it is possible
819 * the extents changed since we did the read call,
820 * we dropped the ilock in the interim.
821 */
822
823 end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size);
824 xfs_bmap_last_offset(NULL, ip, &last_block,
825 XFS_DATA_FORK);
826 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
827 if ((map_start_fsb + count_fsb) > last_block) {
828 count_fsb = last_block - map_start_fsb;
829 if (count_fsb == 0) {
830 error = EAGAIN;
831 goto trans_cancel;
832 }
833 }
834
835 /* Go get the actual blocks */
836 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
837 XFS_BMAPI_WRITE, &first_block, 1,
838 imap, &nimaps, &free_list);
839 if (error)
840 goto trans_cancel;
841
842 error = xfs_bmap_finish(&tp, &free_list,
843 first_block, &committed);
844 if (error)
845 goto trans_cancel;
846
847 error = xfs_trans_commit(tp,
848 XFS_TRANS_RELEASE_LOG_RES, NULL);
849 if (error)
850 goto error0;
851
852 xfs_iunlock(ip, XFS_ILOCK_EXCL);
853 }
854
855 /*
856 * See if we were able to allocate an extent that
857 * covers at least part of the callers request
858 */
859
860 for (i = 0; i < nimaps; i++) {
Nathan Scott6ab65422006-01-11 21:03:28 +1100861 if (!(io->io_flags & XFS_IOCORE_RT) &&
862 !imap[i].br_startblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 cmn_err(CE_PANIC,"Access to block zero: "
864 "fs <%s> inode: %lld "
Nathan Scott6ab65422006-01-11 21:03:28 +1100865 "start_block : %llx start_off : %llx "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 "blkcnt : %llx extent-state : %x \n",
867 (ip->i_mount)->m_fsname,
868 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100869 (unsigned long long)
870 imap[i].br_startblock,
871 (unsigned long long)
872 imap[i].br_startoff,
873 (unsigned long long)
874 imap[i].br_blockcount,
875 imap[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Nathan Scott24e17b52005-05-05 13:33:20 -0700877 if ((offset_fsb >= imap[i].br_startoff) &&
878 (offset_fsb < (imap[i].br_startoff +
879 imap[i].br_blockcount))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *map = imap[i];
881 *retmap = 1;
882 XFS_STATS_INC(xs_xstrat_quick);
883 return 0;
884 }
885 count_fsb -= imap[i].br_blockcount;
886 }
887
888 /* So far we have not mapped the requested part of the
889 * file, just surrounding data, try again.
890 */
891 nimaps--;
Nathan Scott24e17b52005-05-05 13:33:20 -0700892 map_start_fsb = imap[nimaps].br_startoff +
893 imap[nimaps].br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
896trans_cancel:
897 xfs_bmap_cancel(&free_list);
898 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
899error0:
900 xfs_iunlock(ip, XFS_ILOCK_EXCL);
901 return XFS_ERROR(error);
902}
903
904int
905xfs_iomap_write_unwritten(
906 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700907 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 size_t count)
909{
910 xfs_mount_t *mp = ip->i_mount;
911 xfs_iocore_t *io = &ip->i_iocore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 xfs_fileoff_t offset_fsb;
913 xfs_filblks_t count_fsb;
914 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100915 xfs_fsblock_t firstfsb;
916 int nimaps;
917 xfs_trans_t *tp;
918 xfs_bmbt_irec_t imap;
919 xfs_bmap_free_t free_list;
920 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int committed;
922 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN,
925 &ip->i_iocore, offset, count);
926
927 offset_fsb = XFS_B_TO_FSBT(mp, offset);
928 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
929 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
930
Nathan Scottdd9f4382006-01-11 15:28:28 +1100931 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Nathan Scottdd9f4382006-01-11 15:28:28 +1100933 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /*
935 * set up a transaction to convert the range of extents
936 * from unwritten to real. Do allocations in a loop until
937 * we have covered the range passed in.
938 */
939
940 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100941 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 XFS_WRITE_LOG_RES(mp), 0,
943 XFS_TRANS_PERM_LOG_RES,
944 XFS_WRITE_LOG_COUNT);
945 if (error) {
946 xfs_trans_cancel(tp, 0);
947 goto error0;
948 }
949
950 xfs_ilock(ip, XFS_ILOCK_EXCL);
951 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
952 xfs_trans_ihold(tp, ip);
953
954 /*
955 * Modify the unwritten extent state of the buffer.
956 */
957 XFS_BMAP_INIT(&free_list, &firstfsb);
958 nimaps = 1;
959 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100960 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 1, &imap, &nimaps, &free_list);
962 if (error)
963 goto error_on_bmapi_transaction;
964
965 error = xfs_bmap_finish(&(tp), &(free_list),
966 firstfsb, &committed);
967 if (error)
968 goto error_on_bmapi_transaction;
969
970 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
971 xfs_iunlock(ip, XFS_ILOCK_EXCL);
972 if (error)
973 goto error0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if ( !(io->io_flags & XFS_IOCORE_RT) && !imap.br_startblock) {
976 cmn_err(CE_PANIC,"Access to block zero: fs <%s> "
977 "inode: %lld start_block : %llx start_off : "
978 "%llx blkcnt : %llx extent-state : %x \n",
979 (ip->i_mount)->m_fsname,
980 (long long)ip->i_ino,
Nathan Scott6ab65422006-01-11 21:03:28 +1100981 (unsigned long long)imap.br_startblock,
982 (unsigned long long)imap.br_startoff,
983 (unsigned long long)imap.br_blockcount,
984 imap.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
987 if ((numblks_fsb = imap.br_blockcount) == 0) {
988 /*
989 * The numblks_fsb value should always get
990 * smaller, otherwise the loop is stuck.
991 */
992 ASSERT(imap.br_blockcount);
993 break;
994 }
995 offset_fsb += numblks_fsb;
996 count_fsb -= numblks_fsb;
997 } while (count_fsb > 0);
998
999 return 0;
1000
1001error_on_bmapi_transaction:
1002 xfs_bmap_cancel(&free_list);
1003 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
1004 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1005error0:
1006 return XFS_ERROR(error);
1007}