blob: 4821b8563c4120b0e488b9e803589a1b9c5f48a4 [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,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100056 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 xfs_off_t offset,
58 ssize_t count)
59{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100060 xfs_iocore_t *io = &ip->i_iocore;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
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,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100087 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 xfs_off_t offset,
89 ssize_t count,
90 xfs_iomap_t *iomapp,
91 xfs_bmbt_irec_t *imapp,
92 int flags)
93{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (!ip->i_rwtrace)
95 return;
96
97 ktrace_enter(ip->i_rwtrace,
98 (void *)((unsigned long)tag),
99 (void *)ip,
100 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
101 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
102 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
103 (void *)((unsigned long)(offset & 0xffffffff)),
104 (void *)((unsigned long)count),
105 (void *)((unsigned long)flags),
106 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
108 (void *)((unsigned long)(iomapp->iomap_delta)),
109 (void *)((unsigned long)(iomapp->iomap_bsize)),
110 (void *)((unsigned long)(iomapp->iomap_bn)),
111 (void *)(__psint_t)(imapp->br_startoff),
112 (void *)((unsigned long)(imapp->br_blockcount)),
113 (void *)(__psint_t)(imapp->br_startblock));
114}
115#else
116#define xfs_iomap_enter_trace(tag, io, offset, count)
117#define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
118#endif
119
120#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
121 << mp->m_writeio_log)
122#define XFS_STRAT_WRITE_IMAPS 2
123#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
124
125STATIC int
126xfs_imap_to_bmap(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000127 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 xfs_off_t offset,
129 xfs_bmbt_irec_t *imap,
130 xfs_iomap_t *iomapp,
131 int imaps, /* Number of imap entries */
132 int iomaps, /* Number of iomap entries */
133 int flags)
134{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000135 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int pbm;
137 xfs_fsblock_t start_block;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
141 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
142 iomapp->iomap_delta = offset - iomapp->iomap_offset;
143 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
144 iomapp->iomap_flags = flags;
145
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000146 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 iomapp->iomap_flags |= IOMAP_REALTIME;
148 iomapp->iomap_target = mp->m_rtdev_targp;
149 } else {
150 iomapp->iomap_target = mp->m_ddev_targp;
151 }
152 start_block = imap->br_startblock;
153 if (start_block == HOLESTARTBLOCK) {
154 iomapp->iomap_bn = IOMAP_DADDR_NULL;
155 iomapp->iomap_flags |= IOMAP_HOLE;
156 } else if (start_block == DELAYSTARTBLOCK) {
157 iomapp->iomap_bn = IOMAP_DADDR_NULL;
158 iomapp->iomap_flags |= IOMAP_DELAY;
159 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000160 iomapp->iomap_bn = XFS_FSB_TO_DB(ip, start_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (ISUNWRITTEN(imap))
162 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
163 }
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
166 }
167 return pbm; /* Return the number filled */
168}
169
170int
171xfs_iomap(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000172 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xfs_off_t offset,
174 ssize_t count,
175 int flags,
176 xfs_iomap_t *iomapp,
177 int *niomaps)
178{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000179 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 xfs_fileoff_t offset_fsb, end_fsb;
181 int error = 0;
182 int lockmode = 0;
183 xfs_bmbt_irec_t imap;
184 int nimaps = 1;
185 int bmapi_flags = 0;
186 int iomap_flags = 0;
187
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000188 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
189 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
190 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (XFS_FORCED_SHUTDOWN(mp))
193 return XFS_ERROR(EIO);
194
Christoph Hellwig76428612007-09-14 15:23:31 +1000195 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 case BMAPI_READ:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000197 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, ip, offset, count);
198 lockmode = xfs_ilock_map_shared(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201 case BMAPI_WRITE:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000202 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
Nathan Scottc31e8872005-09-05 10:06:55 +1000204 if (flags & BMAPI_IGNSTATE)
205 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000206 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208 case BMAPI_ALLOCATE:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000209 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
211 bmapi_flags = XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Attempt non-blocking lock */
214 if (flags & BMAPI_TRYLOCK) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000215 if (!xfs_ilock_nowait(ip, lockmode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return XFS_ERROR(EAGAIN);
217 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000218 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 default:
222 BUG();
223 }
224
225 ASSERT(offset <= mp->m_maxioffset);
226 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
227 count = mp->m_maxioffset - offset;
228 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
229 offset_fsb = XFS_B_TO_FSBT(mp, offset);
230
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000231 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 (xfs_filblks_t)(end_fsb - offset_fsb),
233 bmapi_flags, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000234 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (error)
237 goto out;
238
Christoph Hellwig76428612007-09-14 15:23:31 +1000239 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case BMAPI_WRITE:
241 /* If we found an extent, return it */
Russell Cattelan68d14982005-05-06 06:42:22 -0700242 if (nimaps &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100243 (imap.br_startblock != HOLESTARTBLOCK) &&
Russell Cattelan68d14982005-05-06 06:42:22 -0700244 (imap.br_startblock != DELAYSTARTBLOCK)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000245 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 offset, count, iomapp, &imap, flags);
247 break;
248 }
249
250 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000251 error = xfs_iomap_write_direct(ip, offset, count, flags,
252 &imap, &nimaps, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000254 error = xfs_iomap_write_delay(ip, offset, count, flags,
255 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 if (!error) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000258 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 offset, count, iomapp, &imap, flags);
260 }
261 iomap_flags = IOMAP_NEW;
262 break;
263 case BMAPI_ALLOCATE:
264 /* If we found an extent, return it */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000265 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 lockmode = 0;
267
268 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000269 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 offset, count, iomapp, &imap, flags);
271 break;
272 }
273
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000274 error = xfs_iomap_write_allocate(ip, offset, count,
Nathan Scott24e17b52005-05-05 13:33:20 -0700275 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 if (nimaps) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000280 *niomaps = xfs_imap_to_bmap(ip, offset, &imap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 iomapp, nimaps, *niomaps, iomap_flags);
282 } else if (niomaps) {
283 *niomaps = 0;
284 }
285
286out:
287 if (lockmode)
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000288 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return XFS_ERROR(error);
290}
291
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100294xfs_iomap_eof_align_last_fsb(
295 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000296 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100297 xfs_fsize_t isize,
298 xfs_extlen_t extsize,
299 xfs_fileoff_t *last_fsb)
300{
301 xfs_fileoff_t new_last_fsb = 0;
302 xfs_extlen_t align;
303 int eof, error;
304
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000305 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100306 ;
307 /*
308 * If mounted with the "-o swalloc" option, roundup the allocation
309 * request to a stripe width boundary if the file size is >=
310 * stripe width and we are allocating past the allocation eof.
311 */
312 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
313 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
314 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
315 /*
316 * Roundup the allocation request to a stripe unit (m_dalign) boundary
317 * if the file size is >= stripe unit size, and we are allocating past
318 * the allocation eof.
319 */
320 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
321 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
322
323 /*
324 * Always round up the allocation request to an extent boundary
325 * (when file on a real-time subvolume or has di_extsize hint).
326 */
327 if (extsize) {
328 if (new_last_fsb)
329 align = roundup_64(new_last_fsb, extsize);
330 else
331 align = extsize;
332 new_last_fsb = roundup_64(*last_fsb, align);
333 }
334
335 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000336 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100337 if (error)
338 return error;
339 if (eof)
340 *last_fsb = new_last_fsb;
341 }
342 return 0;
343}
344
345STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346xfs_flush_space(
347 xfs_inode_t *ip,
348 int *fsynced,
349 int *ioflags)
350{
351 switch (*fsynced) {
352 case 0:
353 if (ip->i_delayed_blks) {
354 xfs_iunlock(ip, XFS_ILOCK_EXCL);
355 xfs_flush_inode(ip);
356 xfs_ilock(ip, XFS_ILOCK_EXCL);
357 *fsynced = 1;
358 } else {
359 *ioflags |= BMAPI_SYNC;
360 *fsynced = 2;
361 }
362 return 0;
363 case 1:
364 *fsynced = 2;
365 *ioflags |= BMAPI_SYNC;
366 return 0;
367 case 2:
368 xfs_iunlock(ip, XFS_ILOCK_EXCL);
369 xfs_flush_device(ip);
370 xfs_ilock(ip, XFS_ILOCK_EXCL);
371 *fsynced = 3;
372 return 0;
373 }
374 return 1;
375}
376
Nathan Scott572d95f2006-09-28 11:03:20 +1000377STATIC int
378xfs_cmn_err_fsblock_zero(
379 xfs_inode_t *ip,
380 xfs_bmbt_irec_t *imap)
381{
382 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
383 "Access to block zero in inode %llu "
384 "start_block: %llx start_off: %llx "
385 "blkcnt: %llx extent-state: %x\n",
386 (unsigned long long)ip->i_ino,
387 (unsigned long long)imap->br_startblock,
388 (unsigned long long)imap->br_startoff,
389 (unsigned long long)imap->br_blockcount,
390 imap->br_state);
391 return EFSCORRUPTED;
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394int
395xfs_iomap_write_direct(
396 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700397 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 size_t count,
399 int flags,
400 xfs_bmbt_irec_t *ret_imap,
401 int *nmaps,
402 int found)
403{
404 xfs_mount_t *mp = ip->i_mount;
405 xfs_iocore_t *io = &ip->i_iocore;
406 xfs_fileoff_t offset_fsb;
407 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100408 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100410 xfs_extlen_t extsz, temp;
411 xfs_fsize_t isize;
Eric Sandeen0116d932005-11-02 15:00:01 +1100412 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000414 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int rt;
416 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100417 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100419 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100421 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /*
424 * Make sure that the dquots are there. This doesn't hold
425 * the ilock across a disk read.
426 */
427 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
428 if (error)
429 return XFS_ERROR(error);
430
Nathan Scottdd9f4382006-01-11 15:28:28 +1100431 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000432 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000434 isize = ip->i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100435 if (io->io_new_size > isize)
436 isize = io->io_new_size;
437
David Chinner957d0eb2007-06-18 16:50:37 +1000438 offset_fsb = XFS_B_TO_FSBT(mp, offset);
439 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Nathan Scottdd9f4382006-01-11 15:28:28 +1100440 if ((offset + count) > isize) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000441 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100442 &last_fsb);
443 if (error)
444 goto error_out;
445 } else {
446 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
447 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
448 ret_imap->br_blockcount +
449 ret_imap->br_startoff);
450 }
451 count_fsb = last_fsb - offset_fsb;
452 ASSERT(count_fsb > 0);
453
454 resaligned = count_fsb;
455 if (unlikely(extsz)) {
456 if ((temp = do_mod(offset_fsb, extsz)))
457 resaligned += temp;
458 if ((temp = do_mod(resaligned, extsz)))
459 resaligned += extsz - temp;
460 }
461
462 if (unlikely(rt)) {
463 resrtextents = qblocks = resaligned;
464 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000465 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
466 quota_flag = XFS_QMOPT_RES_RTBLKS;
467 } else {
468 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100469 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000470 quota_flag = XFS_QMOPT_RES_REGBLKS;
471 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000474 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
476 xfs_iunlock(ip, XFS_ILOCK_EXCL);
477 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000479 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 XFS_TRANS_PERM_LOG_RES,
481 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000483 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000489 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Nathan Scottdd9f4382006-01-11 15:28:28 +1100491 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
492 qblocks, 0, quota_flag);
493 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
497 xfs_trans_ihold(tp, ip);
498
Nathan Scottdd9f4382006-01-11 15:28:28 +1100499 bmapi_flag = XFS_BMAPI_WRITE;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000500 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 bmapi_flag |= XFS_BMAPI_PREALLOC;
502
503 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100504 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
506 XFS_BMAP_INIT(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000507 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000508 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000509 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000510 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000514 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100516 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000517 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000519 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000520 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Nathan Scott06d10dd2005-06-21 15:48:47 +1000523 /*
524 * Copy any maps to caller's array and return any error.
525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000527 error = ENOSPC;
528 goto error_out;
529 }
530
531 if (unlikely(!imap.br_startblock && !(io->io_flags & XFS_IOCORE_RT))) {
532 error = xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto error_out;
534 }
535
Eric Sandeen0116d932005-11-02 15:00:01 +1100536 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539
Nathan Scott06d10dd2005-06-21 15:48:47 +1000540error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 xfs_bmap_cancel(&free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000542 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Nathan Scott06d10dd2005-06-21 15:48:47 +1000544error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
546 *nmaps = 0; /* nothing set-up here */
547
548error_out:
549 return XFS_ERROR(error);
550}
551
Nathan Scottdd9f4382006-01-11 15:28:28 +1100552/*
553 * If the caller is doing a write at the end of the file,
554 * then extend the allocation out to the file system's write
555 * iosize. We clean up any extra space left over when the
556 * file is closed in xfs_inactive().
557 *
558 * For sync writes, we are flushing delayed allocate space to
559 * try to make additional space available for allocation near
560 * the filesystem full boundary - preallocation hurts in that
561 * situation, of course.
562 */
563STATIC int
564xfs_iomap_eof_want_preallocate(
565 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000566 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100567 xfs_fsize_t isize,
568 xfs_off_t offset,
569 size_t count,
570 int ioflag,
571 xfs_bmbt_irec_t *imap,
572 int nimaps,
573 int *prealloc)
574{
575 xfs_fileoff_t start_fsb;
576 xfs_filblks_t count_fsb;
577 xfs_fsblock_t firstblock;
578 int n, error, imaps;
579
580 *prealloc = 0;
581 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
582 return 0;
583
584 /*
585 * If there are any real blocks past eof, then don't
586 * do any speculative allocation.
587 */
588 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
589 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
590 while (count_fsb > 0) {
591 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100592 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000593 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000594 &firstblock, 0, imap, &imaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100595 if (error)
596 return error;
597 for (n = 0; n < imaps; n++) {
598 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
599 (imap[n].br_startblock != DELAYSTARTBLOCK))
600 return 0;
601 start_fsb += imap[n].br_blockcount;
602 count_fsb -= imap[n].br_blockcount;
603 }
604 }
605 *prealloc = 1;
606 return 0;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609int
610xfs_iomap_write_delay(
611 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700612 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 size_t count,
614 int ioflag,
615 xfs_bmbt_irec_t *ret_imap,
616 int *nmaps)
617{
618 xfs_mount_t *mp = ip->i_mount;
619 xfs_iocore_t *io = &ip->i_iocore;
620 xfs_fileoff_t offset_fsb;
621 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100622 xfs_off_t aligned_offset;
623 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100625 xfs_extlen_t extsz;
626 xfs_fsize_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Nathan Scottdd9f4382006-01-11 15:28:28 +1100629 int prealloc, fsynced = 0;
630 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
633
634 /*
635 * Make sure that the dquots are there. This doesn't hold
636 * the ilock across a disk read.
637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
639 if (error)
640 return XFS_ERROR(error);
641
David Chinner957d0eb2007-06-18 16:50:37 +1000642 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100643 offset_fsb = XFS_B_TO_FSBT(mp, offset);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645retry:
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000646 isize = ip->i_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100647 if (io->io_new_size > isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 isize = io->io_new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000650 error = xfs_iomap_eof_want_preallocate(mp, ip, isize, offset, count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100651 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
652 if (error)
653 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Nathan Scottdd9f4382006-01-11 15:28:28 +1100655 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
657 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100658 last_fsb = ioalign + mp->m_writeio_blocks;
659 } else {
660 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100662
663 if (prealloc || extsz) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000664 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100665 &last_fsb);
666 if (error)
667 return error;
668 }
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 nimaps = XFS_WRITE_IMAPS;
671 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000672 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 (xfs_filblks_t)(last_fsb - offset_fsb),
674 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
675 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000676 &nimaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100677 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
681 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100682 * then we must have run out of space - flush delalloc, and retry..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
684 if (nimaps == 0) {
685 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000686 ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (xfs_flush_space(ip, &fsynced, &ioflag))
688 return XFS_ERROR(ENOSPC);
689
690 error = 0;
691 goto retry;
692 }
693
Nathan Scott572d95f2006-09-28 11:03:20 +1000694 if (unlikely(!imap[0].br_startblock && !(io->io_flags & XFS_IOCORE_RT)))
695 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100696
697 *ret_imap = imap[0];
698 *nmaps = 1;
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
702
703/*
704 * Pass in a delayed allocate extent, convert it to real extents;
705 * return to the caller the extent we create which maps on top of
706 * the originating callers request.
707 *
708 * Called without a lock on the inode.
709 */
710int
711xfs_iomap_write_allocate(
712 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700713 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700714 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 xfs_bmbt_irec_t *map,
716 int *retmap)
717{
718 xfs_mount_t *mp = ip->i_mount;
719 xfs_iocore_t *io = &ip->i_iocore;
720 xfs_fileoff_t offset_fsb, last_block;
721 xfs_fileoff_t end_fsb, map_start_fsb;
722 xfs_fsblock_t first_block;
723 xfs_bmap_free_t free_list;
724 xfs_filblks_t count_fsb;
725 xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS];
726 xfs_trans_t *tp;
727 int i, nimaps, committed;
728 int error = 0;
729 int nres;
730
731 *retmap = 0;
732
733 /*
734 * Make sure that the dquots are there.
735 */
736 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
737 return XFS_ERROR(error);
738
Nathan Scott24e17b52005-05-05 13:33:20 -0700739 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700741 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
744
745 while (count_fsb != 0) {
746 /*
747 * Set up a transaction with which to allocate the
748 * backing store for the file. Do allocations in a
749 * loop until we get some space in the range we are
750 * interested in. The other space that might be allocated
751 * is in the delayed allocation extent on which we sit
752 * but before our buffer starts.
753 */
754
755 nimaps = 0;
756 while (nimaps == 0) {
757 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000758 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
760 error = xfs_trans_reserve(tp, nres,
761 XFS_WRITE_LOG_RES(mp),
762 0, XFS_TRANS_PERM_LOG_RES,
763 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (error) {
765 xfs_trans_cancel(tp, 0);
766 return XFS_ERROR(error);
767 }
768 xfs_ilock(ip, XFS_ILOCK_EXCL);
769 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
770 xfs_trans_ihold(tp, ip);
771
772 XFS_BMAP_INIT(&free_list, &first_block);
773
774 nimaps = XFS_STRAT_WRITE_IMAPS;
775 /*
776 * Ensure we don't go beyond eof - it is possible
777 * the extents changed since we did the read call,
778 * we dropped the ilock in the interim.
779 */
780
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000781 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 xfs_bmap_last_offset(NULL, ip, &last_block,
783 XFS_DATA_FORK);
784 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
785 if ((map_start_fsb + count_fsb) > last_block) {
786 count_fsb = last_block - map_start_fsb;
787 if (count_fsb == 0) {
788 error = EAGAIN;
789 goto trans_cancel;
790 }
791 }
792
793 /* Go get the actual blocks */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000794 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 XFS_BMAPI_WRITE, &first_block, 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000796 imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (error)
798 goto trans_cancel;
799
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100800 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (error)
802 goto trans_cancel;
803
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000804 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (error)
806 goto error0;
807
808 xfs_iunlock(ip, XFS_ILOCK_EXCL);
809 }
810
811 /*
812 * See if we were able to allocate an extent that
813 * covers at least part of the callers request
814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 for (i = 0; i < nimaps; i++) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000816 if (unlikely(!imap[i].br_startblock &&
817 !(io->io_flags & XFS_IOCORE_RT)))
818 return xfs_cmn_err_fsblock_zero(ip, &imap[i]);
Nathan Scott24e17b52005-05-05 13:33:20 -0700819 if ((offset_fsb >= imap[i].br_startoff) &&
820 (offset_fsb < (imap[i].br_startoff +
821 imap[i].br_blockcount))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *map = imap[i];
823 *retmap = 1;
824 XFS_STATS_INC(xs_xstrat_quick);
825 return 0;
826 }
827 count_fsb -= imap[i].br_blockcount;
828 }
829
830 /* So far we have not mapped the requested part of the
831 * file, just surrounding data, try again.
832 */
833 nimaps--;
Nathan Scott24e17b52005-05-05 13:33:20 -0700834 map_start_fsb = imap[nimaps].br_startoff +
835 imap[nimaps].br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
838trans_cancel:
839 xfs_bmap_cancel(&free_list);
840 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
841error0:
842 xfs_iunlock(ip, XFS_ILOCK_EXCL);
843 return XFS_ERROR(error);
844}
845
846int
847xfs_iomap_write_unwritten(
848 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700849 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 size_t count)
851{
852 xfs_mount_t *mp = ip->i_mount;
853 xfs_iocore_t *io = &ip->i_iocore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 xfs_fileoff_t offset_fsb;
855 xfs_filblks_t count_fsb;
856 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100857 xfs_fsblock_t firstfsb;
858 int nimaps;
859 xfs_trans_t *tp;
860 xfs_bmbt_irec_t imap;
861 xfs_bmap_free_t free_list;
862 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int committed;
864 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000866 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN, ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 offset_fsb = XFS_B_TO_FSBT(mp, offset);
869 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
870 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
871
Nathan Scottdd9f4382006-01-11 15:28:28 +1100872 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Nathan Scottdd9f4382006-01-11 15:28:28 +1100874 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /*
876 * set up a transaction to convert the range of extents
877 * from unwritten to real. Do allocations in a loop until
878 * we have covered the range passed in.
879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000881 tp->t_flags |= XFS_TRANS_RESERVE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100882 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 XFS_WRITE_LOG_RES(mp), 0,
884 XFS_TRANS_PERM_LOG_RES,
885 XFS_WRITE_LOG_COUNT);
886 if (error) {
887 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000888 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 xfs_ilock(ip, XFS_ILOCK_EXCL);
892 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
893 xfs_trans_ihold(tp, ip);
894
895 /*
896 * Modify the unwritten extent state of the buffer.
897 */
898 XFS_BMAP_INIT(&free_list, &firstfsb);
899 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000900 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100901 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000902 1, &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (error)
904 goto error_on_bmapi_transaction;
905
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100906 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (error)
908 goto error_on_bmapi_transaction;
909
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000910 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 xfs_iunlock(ip, XFS_ILOCK_EXCL);
912 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000913 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100914
Nathan Scott572d95f2006-09-28 11:03:20 +1000915 if (unlikely(!imap.br_startblock &&
916 !(io->io_flags & XFS_IOCORE_RT)))
917 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if ((numblks_fsb = imap.br_blockcount) == 0) {
920 /*
921 * The numblks_fsb value should always get
922 * smaller, otherwise the loop is stuck.
923 */
924 ASSERT(imap.br_blockcount);
925 break;
926 }
927 offset_fsb += numblks_fsb;
928 count_fsb -= numblks_fsb;
929 } while (count_fsb > 0);
930
931 return 0;
932
933error_on_bmapi_transaction:
934 xfs_bmap_cancel(&free_list);
935 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
936 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return XFS_ERROR(error);
938}