blob: 67f22b2b44b3c41c51ce3ac6c1f911f5db47b181 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (!ip->i_rwtrace)
61 return;
62
63 ktrace_enter(ip->i_rwtrace,
64 (void *)((unsigned long)tag),
65 (void *)ip,
66 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
67 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
68 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
69 (void *)((unsigned long)(offset & 0xffffffff)),
70 (void *)((unsigned long)count),
Christoph Hellwig613d7042007-10-11 17:44:08 +100071 (void *)((unsigned long)((ip->i_new_size >> 32) & 0xffffffff)),
72 (void *)((unsigned long)(ip->i_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110073 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 (void *)NULL,
75 (void *)NULL,
76 (void *)NULL,
77 (void *)NULL,
78 (void *)NULL,
79 (void *)NULL);
80}
81
82void
83xfs_iomap_map_trace(
84 int tag,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100085 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 xfs_off_t offset,
87 ssize_t count,
88 xfs_iomap_t *iomapp,
89 xfs_bmbt_irec_t *imapp,
90 int flags)
91{
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!ip->i_rwtrace)
93 return;
94
95 ktrace_enter(ip->i_rwtrace,
96 (void *)((unsigned long)tag),
97 (void *)ip,
98 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
99 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
100 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
101 (void *)((unsigned long)(offset & 0xffffffff)),
102 (void *)((unsigned long)count),
103 (void *)((unsigned long)flags),
104 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
106 (void *)((unsigned long)(iomapp->iomap_delta)),
107 (void *)((unsigned long)(iomapp->iomap_bsize)),
108 (void *)((unsigned long)(iomapp->iomap_bn)),
109 (void *)(__psint_t)(imapp->br_startoff),
110 (void *)((unsigned long)(imapp->br_blockcount)),
111 (void *)(__psint_t)(imapp->br_startblock));
112}
113#else
114#define xfs_iomap_enter_trace(tag, io, offset, count)
115#define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
116#endif
117
118#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
119 << mp->m_writeio_log)
120#define XFS_STRAT_WRITE_IMAPS 2
121#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
122
123STATIC int
124xfs_imap_to_bmap(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000125 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 xfs_off_t offset,
127 xfs_bmbt_irec_t *imap,
128 xfs_iomap_t *iomapp,
129 int imaps, /* Number of imap entries */
130 int iomaps, /* Number of iomap entries */
131 int flags)
132{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000133 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int pbm;
135 xfs_fsblock_t start_block;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
139 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
140 iomapp->iomap_delta = offset - iomapp->iomap_offset;
141 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
142 iomapp->iomap_flags = flags;
143
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100144 if (XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 iomapp->iomap_flags |= IOMAP_REALTIME;
146 iomapp->iomap_target = mp->m_rtdev_targp;
147 } else {
148 iomapp->iomap_target = mp->m_ddev_targp;
149 }
150 start_block = imap->br_startblock;
151 if (start_block == HOLESTARTBLOCK) {
152 iomapp->iomap_bn = IOMAP_DADDR_NULL;
153 iomapp->iomap_flags |= IOMAP_HOLE;
154 } else if (start_block == DELAYSTARTBLOCK) {
155 iomapp->iomap_bn = IOMAP_DADDR_NULL;
156 iomapp->iomap_flags |= IOMAP_DELAY;
157 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000158 iomapp->iomap_bn = XFS_FSB_TO_DB(ip, start_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (ISUNWRITTEN(imap))
160 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
161 }
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
164 }
165 return pbm; /* Return the number filled */
166}
167
168int
169xfs_iomap(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000170 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 xfs_off_t offset,
172 ssize_t count,
173 int flags,
174 xfs_iomap_t *iomapp,
175 int *niomaps)
176{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000177 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 xfs_fileoff_t offset_fsb, end_fsb;
179 int error = 0;
180 int lockmode = 0;
181 xfs_bmbt_irec_t imap;
182 int nimaps = 1;
183 int bmapi_flags = 0;
184 int iomap_flags = 0;
185
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000186 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (XFS_FORCED_SHUTDOWN(mp))
189 return XFS_ERROR(EIO);
190
Christoph Hellwig76428612007-09-14 15:23:31 +1000191 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 case BMAPI_READ:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000193 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, ip, offset, count);
194 lockmode = xfs_ilock_map_shared(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 case BMAPI_WRITE:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000198 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, ip, offset, count);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000199 lockmode = XFS_ILOCK_EXCL;
Nathan Scottc31e8872005-09-05 10:06:55 +1000200 if (flags & BMAPI_IGNSTATE)
201 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000202 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 case BMAPI_ALLOCATE:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000205 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, ip, offset, count);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000206 lockmode = XFS_ILOCK_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 bmapi_flags = XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Attempt non-blocking lock */
210 if (flags & BMAPI_TRYLOCK) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000211 if (!xfs_ilock_nowait(ip, lockmode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return XFS_ERROR(EAGAIN);
213 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000214 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 default:
218 BUG();
219 }
220
221 ASSERT(offset <= mp->m_maxioffset);
222 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
223 count = mp->m_maxioffset - offset;
224 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
225 offset_fsb = XFS_B_TO_FSBT(mp, offset);
226
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000227 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 (xfs_filblks_t)(end_fsb - offset_fsb),
229 bmapi_flags, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000230 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (error)
233 goto out;
234
Christoph Hellwig76428612007-09-14 15:23:31 +1000235 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 case BMAPI_WRITE:
237 /* If we found an extent, return it */
Russell Cattelan68d14982005-05-06 06:42:22 -0700238 if (nimaps &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100239 (imap.br_startblock != HOLESTARTBLOCK) &&
Russell Cattelan68d14982005-05-06 06:42:22 -0700240 (imap.br_startblock != DELAYSTARTBLOCK)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000241 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 offset, count, iomapp, &imap, flags);
243 break;
244 }
245
246 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000247 error = xfs_iomap_write_direct(ip, offset, count, flags,
248 &imap, &nimaps, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000250 error = xfs_iomap_write_delay(ip, offset, count, flags,
251 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 if (!error) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000254 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 offset, count, iomapp, &imap, flags);
256 }
257 iomap_flags = IOMAP_NEW;
258 break;
259 case BMAPI_ALLOCATE:
260 /* If we found an extent, return it */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000261 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 lockmode = 0;
263
264 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000265 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 offset, count, iomapp, &imap, flags);
267 break;
268 }
269
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000270 error = xfs_iomap_write_allocate(ip, offset, count,
Nathan Scott24e17b52005-05-05 13:33:20 -0700271 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
275 if (nimaps) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000276 *niomaps = xfs_imap_to_bmap(ip, offset, &imap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 iomapp, nimaps, *niomaps, iomap_flags);
278 } else if (niomaps) {
279 *niomaps = 0;
280 }
281
282out:
283 if (lockmode)
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000284 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return XFS_ERROR(error);
286}
287
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100290xfs_iomap_eof_align_last_fsb(
291 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000292 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100293 xfs_fsize_t isize,
294 xfs_extlen_t extsize,
295 xfs_fileoff_t *last_fsb)
296{
297 xfs_fileoff_t new_last_fsb = 0;
298 xfs_extlen_t align;
299 int eof, error;
300
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100301 if (XFS_IS_REALTIME_INODE(ip))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100302 ;
303 /*
304 * If mounted with the "-o swalloc" option, roundup the allocation
305 * request to a stripe width boundary if the file size is >=
306 * stripe width and we are allocating past the allocation eof.
307 */
308 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
309 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
310 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
311 /*
312 * Roundup the allocation request to a stripe unit (m_dalign) boundary
313 * if the file size is >= stripe unit size, and we are allocating past
314 * the allocation eof.
315 */
316 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
317 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
318
319 /*
320 * Always round up the allocation request to an extent boundary
321 * (when file on a real-time subvolume or has di_extsize hint).
322 */
323 if (extsize) {
324 if (new_last_fsb)
325 align = roundup_64(new_last_fsb, extsize);
326 else
327 align = extsize;
328 new_last_fsb = roundup_64(*last_fsb, align);
329 }
330
331 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000332 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100333 if (error)
334 return error;
335 if (eof)
336 *last_fsb = new_last_fsb;
337 }
338 return 0;
339}
340
341STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342xfs_flush_space(
343 xfs_inode_t *ip,
344 int *fsynced,
345 int *ioflags)
346{
347 switch (*fsynced) {
348 case 0:
349 if (ip->i_delayed_blks) {
350 xfs_iunlock(ip, XFS_ILOCK_EXCL);
351 xfs_flush_inode(ip);
352 xfs_ilock(ip, XFS_ILOCK_EXCL);
353 *fsynced = 1;
354 } else {
355 *ioflags |= BMAPI_SYNC;
356 *fsynced = 2;
357 }
358 return 0;
359 case 1:
360 *fsynced = 2;
361 *ioflags |= BMAPI_SYNC;
362 return 0;
363 case 2:
364 xfs_iunlock(ip, XFS_ILOCK_EXCL);
365 xfs_flush_device(ip);
366 xfs_ilock(ip, XFS_ILOCK_EXCL);
367 *fsynced = 3;
368 return 0;
369 }
370 return 1;
371}
372
Nathan Scott572d95f2006-09-28 11:03:20 +1000373STATIC int
374xfs_cmn_err_fsblock_zero(
375 xfs_inode_t *ip,
376 xfs_bmbt_irec_t *imap)
377{
378 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
379 "Access to block zero in inode %llu "
380 "start_block: %llx start_off: %llx "
381 "blkcnt: %llx extent-state: %x\n",
382 (unsigned long long)ip->i_ino,
383 (unsigned long long)imap->br_startblock,
384 (unsigned long long)imap->br_startoff,
385 (unsigned long long)imap->br_blockcount,
386 imap->br_state);
387 return EFSCORRUPTED;
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390int
391xfs_iomap_write_direct(
392 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700393 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 size_t count,
395 int flags,
396 xfs_bmbt_irec_t *ret_imap,
397 int *nmaps,
398 int found)
399{
400 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 xfs_fileoff_t offset_fsb;
402 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100403 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100405 xfs_extlen_t extsz, temp;
406 xfs_fsize_t isize;
Eric Sandeen0116d932005-11-02 15:00:01 +1100407 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000409 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int rt;
411 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100412 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100414 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100416 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /*
419 * Make sure that the dquots are there. This doesn't hold
420 * the ilock across a disk read.
421 */
422 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
423 if (error)
424 return XFS_ERROR(error);
425
Nathan Scottdd9f4382006-01-11 15:28:28 +1100426 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000427 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000429 isize = ip->i_size;
Christoph Hellwig613d7042007-10-11 17:44:08 +1000430 if (ip->i_new_size > isize)
431 isize = ip->i_new_size;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100432
David Chinner957d0eb2007-06-18 16:50:37 +1000433 offset_fsb = XFS_B_TO_FSBT(mp, offset);
434 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Nathan Scottdd9f4382006-01-11 15:28:28 +1100435 if ((offset + count) > isize) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000436 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100437 &last_fsb);
438 if (error)
439 goto error_out;
440 } else {
441 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
442 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
443 ret_imap->br_blockcount +
444 ret_imap->br_startoff);
445 }
446 count_fsb = last_fsb - offset_fsb;
447 ASSERT(count_fsb > 0);
448
449 resaligned = count_fsb;
450 if (unlikely(extsz)) {
451 if ((temp = do_mod(offset_fsb, extsz)))
452 resaligned += temp;
453 if ((temp = do_mod(resaligned, extsz)))
454 resaligned += extsz - temp;
455 }
456
457 if (unlikely(rt)) {
458 resrtextents = qblocks = resaligned;
459 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000460 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
461 quota_flag = XFS_QMOPT_RES_RTBLKS;
462 } else {
463 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100464 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000465 quota_flag = XFS_QMOPT_RES_REGBLKS;
466 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000469 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
471 xfs_iunlock(ip, XFS_ILOCK_EXCL);
472 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000474 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 XFS_TRANS_PERM_LOG_RES,
476 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000478 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
480 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000484 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Nathan Scottdd9f4382006-01-11 15:28:28 +1100486 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
487 qblocks, 0, quota_flag);
488 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
492 xfs_trans_ihold(tp, ip);
493
Nathan Scottdd9f4382006-01-11 15:28:28 +1100494 bmapi_flag = XFS_BMAPI_WRITE;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000495 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 bmapi_flag |= XFS_BMAPI_PREALLOC;
497
498 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100499 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
501 XFS_BMAP_INIT(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000502 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000503 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000504 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000505 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000509 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100511 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000512 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000514 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000515 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Nathan Scott06d10dd2005-06-21 15:48:47 +1000518 /*
519 * Copy any maps to caller's array and return any error.
520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000522 error = ENOSPC;
523 goto error_out;
524 }
525
David Chinner86c4d622008-04-29 12:53:21 +1000526 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000527 error = xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto error_out;
529 }
530
Eric Sandeen0116d932005-11-02 15:00:01 +1100531 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
534
Nathan Scott06d10dd2005-06-21 15:48:47 +1000535error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xfs_bmap_cancel(&free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000537 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Nathan Scott06d10dd2005-06-21 15:48:47 +1000539error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
541 *nmaps = 0; /* nothing set-up here */
542
543error_out:
544 return XFS_ERROR(error);
545}
546
Nathan Scottdd9f4382006-01-11 15:28:28 +1100547/*
548 * If the caller is doing a write at the end of the file,
549 * then extend the allocation out to the file system's write
550 * iosize. We clean up any extra space left over when the
551 * file is closed in xfs_inactive().
552 *
553 * For sync writes, we are flushing delayed allocate space to
554 * try to make additional space available for allocation near
555 * the filesystem full boundary - preallocation hurts in that
556 * situation, of course.
557 */
558STATIC int
559xfs_iomap_eof_want_preallocate(
560 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000561 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100562 xfs_fsize_t isize,
563 xfs_off_t offset,
564 size_t count,
565 int ioflag,
566 xfs_bmbt_irec_t *imap,
567 int nimaps,
568 int *prealloc)
569{
570 xfs_fileoff_t start_fsb;
571 xfs_filblks_t count_fsb;
572 xfs_fsblock_t firstblock;
573 int n, error, imaps;
574
575 *prealloc = 0;
576 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
577 return 0;
578
579 /*
580 * If there are any real blocks past eof, then don't
581 * do any speculative allocation.
582 */
583 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
584 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
585 while (count_fsb > 0) {
586 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100587 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000588 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000589 &firstblock, 0, imap, &imaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100590 if (error)
591 return error;
592 for (n = 0; n < imaps; n++) {
593 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
594 (imap[n].br_startblock != DELAYSTARTBLOCK))
595 return 0;
596 start_fsb += imap[n].br_blockcount;
597 count_fsb -= imap[n].br_blockcount;
598 }
599 }
600 *prealloc = 1;
601 return 0;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604int
605xfs_iomap_write_delay(
606 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700607 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 size_t count,
609 int ioflag,
610 xfs_bmbt_irec_t *ret_imap,
611 int *nmaps)
612{
613 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 xfs_fileoff_t offset_fsb;
615 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100616 xfs_off_t aligned_offset;
617 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100619 xfs_extlen_t extsz;
620 xfs_fsize_t isize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Nathan Scottdd9f4382006-01-11 15:28:28 +1100623 int prealloc, fsynced = 0;
624 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000626 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /*
629 * Make sure that the dquots are there. This doesn't hold
630 * the ilock across a disk read.
631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
633 if (error)
634 return XFS_ERROR(error);
635
David Chinner957d0eb2007-06-18 16:50:37 +1000636 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100637 offset_fsb = XFS_B_TO_FSBT(mp, offset);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639retry:
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000640 isize = ip->i_size;
Christoph Hellwig613d7042007-10-11 17:44:08 +1000641 if (ip->i_new_size > isize)
642 isize = ip->i_new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000644 error = xfs_iomap_eof_want_preallocate(mp, ip, isize, offset, count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100645 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
646 if (error)
647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Nathan Scottdd9f4382006-01-11 15:28:28 +1100649 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
651 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100652 last_fsb = ioalign + mp->m_writeio_blocks;
653 } else {
654 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100656
657 if (prealloc || extsz) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000658 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100659 &last_fsb);
660 if (error)
661 return error;
662 }
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 nimaps = XFS_WRITE_IMAPS;
665 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000666 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 (xfs_filblks_t)(last_fsb - offset_fsb),
668 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
669 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000670 &nimaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100671 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100676 * then we must have run out of space - flush delalloc, and retry..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
678 if (nimaps == 0) {
679 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000680 ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (xfs_flush_space(ip, &fsynced, &ioflag))
682 return XFS_ERROR(ENOSPC);
683
684 error = 0;
685 goto retry;
686 }
687
David Chinner86c4d622008-04-29 12:53:21 +1000688 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000689 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100690
691 *ret_imap = imap[0];
692 *nmaps = 1;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
695}
696
697/*
698 * Pass in a delayed allocate extent, convert it to real extents;
699 * return to the caller the extent we create which maps on top of
700 * the originating callers request.
701 *
702 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100703 *
704 * We no longer bother to look at the incoming map - all we have to
705 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707int
708xfs_iomap_write_allocate(
709 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700710 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700711 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 xfs_bmbt_irec_t *map,
713 int *retmap)
714{
715 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 xfs_fileoff_t offset_fsb, last_block;
717 xfs_fileoff_t end_fsb, map_start_fsb;
718 xfs_fsblock_t first_block;
719 xfs_bmap_free_t free_list;
720 xfs_filblks_t count_fsb;
David Chinnere4143a12007-11-23 16:29:11 +1100721 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 xfs_trans_t *tp;
David Chinnere4143a12007-11-23 16:29:11 +1100723 int nimaps, committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int error = 0;
725 int nres;
726
727 *retmap = 0;
728
729 /*
730 * Make sure that the dquots are there.
731 */
732 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
733 return XFS_ERROR(error);
734
Nathan Scott24e17b52005-05-05 13:33:20 -0700735 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700737 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
740
741 while (count_fsb != 0) {
742 /*
743 * Set up a transaction with which to allocate the
744 * backing store for the file. Do allocations in a
745 * loop until we get some space in the range we are
746 * interested in. The other space that might be allocated
747 * is in the delayed allocation extent on which we sit
748 * but before our buffer starts.
749 */
750
751 nimaps = 0;
752 while (nimaps == 0) {
753 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000754 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
756 error = xfs_trans_reserve(tp, nres,
757 XFS_WRITE_LOG_RES(mp),
758 0, XFS_TRANS_PERM_LOG_RES,
759 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (error) {
761 xfs_trans_cancel(tp, 0);
762 return XFS_ERROR(error);
763 }
764 xfs_ilock(ip, XFS_ILOCK_EXCL);
765 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
766 xfs_trans_ihold(tp, ip);
767
768 XFS_BMAP_INIT(&free_list, &first_block);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /*
David Chinnere4143a12007-11-23 16:29:11 +1100771 * it is possible that the extents have changed since
772 * we did the read call as we dropped the ilock for a
773 * while. We have to be careful about truncates or hole
774 * punchs here - we are not allowed to allocate
775 * non-delalloc blocks here.
776 *
777 * The only protection against truncation is the pages
778 * for the range we are being asked to convert are
779 * locked and hence a truncate will block on them
780 * first.
781 *
782 * As a result, if we go beyond the range we really
783 * need and hit an delalloc extent boundary followed by
784 * a hole while we have excess blocks in the map, we
785 * will fill the hole incorrectly and overrun the
786 * transaction reservation.
787 *
788 * Using a single map prevents this as we are forced to
789 * check each map we look for overlap with the desired
790 * range and abort as soon as we find it. Also, given
791 * that we only return a single map, having one beyond
792 * what we can return is probably a bit silly.
793 *
794 * We also need to check that we don't go beyond EOF;
795 * this is a truncate optimisation as a truncate sets
796 * the new file size before block on the pages we
797 * currently have locked under writeback. Because they
798 * are about to be tossed, we don't need to write them
799 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
David Chinnere4143a12007-11-23 16:29:11 +1100801 nimaps = 1;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000802 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
David Chinner7c9ef852008-04-10 12:21:59 +1000803 error = xfs_bmap_last_offset(NULL, ip, &last_block,
804 XFS_DATA_FORK);
805 if (error)
806 goto trans_cancel;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
809 if ((map_start_fsb + count_fsb) > last_block) {
810 count_fsb = last_block - map_start_fsb;
811 if (count_fsb == 0) {
812 error = EAGAIN;
813 goto trans_cancel;
814 }
815 }
816
817 /* Go get the actual blocks */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000818 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 XFS_BMAPI_WRITE, &first_block, 1,
David Chinnere4143a12007-11-23 16:29:11 +1100820 &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (error)
822 goto trans_cancel;
823
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100824 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (error)
826 goto trans_cancel;
827
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000828 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (error)
830 goto error0;
831
832 xfs_iunlock(ip, XFS_ILOCK_EXCL);
833 }
834
835 /*
836 * See if we were able to allocate an extent that
837 * covers at least part of the callers request
838 */
David Chinner86c4d622008-04-29 12:53:21 +1000839 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
David Chinnere4143a12007-11-23 16:29:11 +1100840 return xfs_cmn_err_fsblock_zero(ip, &imap);
David Chinner86c4d622008-04-29 12:53:21 +1000841
David Chinnere4143a12007-11-23 16:29:11 +1100842 if ((offset_fsb >= imap.br_startoff) &&
843 (offset_fsb < (imap.br_startoff +
844 imap.br_blockcount))) {
845 *map = imap;
846 *retmap = 1;
847 XFS_STATS_INC(xs_xstrat_quick);
848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
David Chinnere4143a12007-11-23 16:29:11 +1100851 /*
852 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * file, just surrounding data, try again.
854 */
David Chinnere4143a12007-11-23 16:29:11 +1100855 count_fsb -= imap.br_blockcount;
856 map_start_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859trans_cancel:
860 xfs_bmap_cancel(&free_list);
861 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
862error0:
863 xfs_iunlock(ip, XFS_ILOCK_EXCL);
864 return XFS_ERROR(error);
865}
866
867int
868xfs_iomap_write_unwritten(
869 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700870 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 size_t count)
872{
873 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 xfs_fileoff_t offset_fsb;
875 xfs_filblks_t count_fsb;
876 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100877 xfs_fsblock_t firstfsb;
878 int nimaps;
879 xfs_trans_t *tp;
880 xfs_bmbt_irec_t imap;
881 xfs_bmap_free_t free_list;
882 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int committed;
884 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000886 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN, ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 offset_fsb = XFS_B_TO_FSBT(mp, offset);
889 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
890 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
891
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000892 /*
893 * Reserve enough blocks in this transaction for two complete extent
894 * btree splits. We may be converting the middle part of an unwritten
895 * extent and in this case we will insert two new extents in the btree
896 * each of which could cause a full split.
897 *
898 * This reservation amount will be used in the first call to
899 * xfs_bmbt_split() to select an AG with enough space to satisfy the
900 * rest of the operation.
901 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100902 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Nathan Scottdd9f4382006-01-11 15:28:28 +1100904 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /*
906 * set up a transaction to convert the range of extents
907 * from unwritten to real. Do allocations in a loop until
908 * we have covered the range passed in.
909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000911 tp->t_flags |= XFS_TRANS_RESERVE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100912 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 XFS_WRITE_LOG_RES(mp), 0,
914 XFS_TRANS_PERM_LOG_RES,
915 XFS_WRITE_LOG_COUNT);
916 if (error) {
917 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000918 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921 xfs_ilock(ip, XFS_ILOCK_EXCL);
922 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
923 xfs_trans_ihold(tp, ip);
924
925 /*
926 * Modify the unwritten extent state of the buffer.
927 */
928 XFS_BMAP_INIT(&free_list, &firstfsb);
929 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000930 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100931 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000932 1, &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (error)
934 goto error_on_bmapi_transaction;
935
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100936 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (error)
938 goto error_on_bmapi_transaction;
939
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000940 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 xfs_iunlock(ip, XFS_ILOCK_EXCL);
942 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000943 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100944
David Chinner86c4d622008-04-29 12:53:21 +1000945 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000946 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 if ((numblks_fsb = imap.br_blockcount) == 0) {
949 /*
950 * The numblks_fsb value should always get
951 * smaller, otherwise the loop is stuck.
952 */
953 ASSERT(imap.br_blockcount);
954 break;
955 }
956 offset_fsb += numblks_fsb;
957 count_fsb -= numblks_fsb;
958 } while (count_fsb > 0);
959
960 return 0;
961
962error_on_bmapi_transaction:
963 xfs_bmap_cancel(&free_list);
964 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
965 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return XFS_ERROR(error);
967}