blob: 7545dcdaa8aaeb8b7e15b4cc1071cb8dd38e8d12 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
46#include "xfs_buf_item.h"
47#include "xfs_trans_space.h"
48#include "xfs_utils.h"
49#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
54 << mp->m_writeio_log)
55#define XFS_STRAT_WRITE_IMAPS 2
56#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058int
59xfs_iomap(
Christoph Hellwig207d0412010-04-28 12:28:56 +000060 struct xfs_inode *ip,
61 xfs_off_t offset,
62 ssize_t count,
63 int flags,
64 struct xfs_bmbt_irec *imap,
65 int *nimaps,
66 int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Christoph Hellwig207d0412010-04-28 12:28:56 +000068 struct xfs_mount *mp = ip->i_mount;
69 xfs_fileoff_t offset_fsb, end_fsb;
70 int error = 0;
71 int lockmode = 0;
72 int bmapi_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100074 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
Christoph Hellwig207d0412010-04-28 12:28:56 +000075
76 *new = 0;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (XFS_FORCED_SHUTDOWN(mp))
79 return XFS_ERROR(EIO);
80
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000081 trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
82
Christoph Hellwig76428612007-09-14 15:23:31 +100083 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 case BMAPI_READ:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100085 lockmode = xfs_ilock_map_shared(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 break;
88 case BMAPI_WRITE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100089 lockmode = XFS_ILOCK_EXCL;
Nathan Scottc31e8872005-09-05 10:06:55 +100090 if (flags & BMAPI_IGNSTATE)
91 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100092 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
94 case BMAPI_ALLOCATE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100095 lockmode = XFS_ILOCK_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 bmapi_flags = XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* Attempt non-blocking lock */
99 if (flags & BMAPI_TRYLOCK) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000100 if (!xfs_ilock_nowait(ip, lockmode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return XFS_ERROR(EAGAIN);
102 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000103 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 default:
107 BUG();
108 }
109
110 ASSERT(offset <= mp->m_maxioffset);
111 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
112 count = mp->m_maxioffset - offset;
113 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
114 offset_fsb = XFS_B_TO_FSBT(mp, offset);
115
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000116 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 (xfs_filblks_t)(end_fsb - offset_fsb),
Christoph Hellwig207d0412010-04-28 12:28:56 +0000118 bmapi_flags, NULL, 0, imap,
119 nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (error)
122 goto out;
123
Christoph Hellwig76428612007-09-14 15:23:31 +1000124 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 case BMAPI_WRITE:
126 /* If we found an extent, return it */
Christoph Hellwig207d0412010-04-28 12:28:56 +0000127 if (*nimaps &&
128 (imap->br_startblock != HOLESTARTBLOCK) &&
129 (imap->br_startblock != DELAYSTARTBLOCK)) {
130 trace_xfs_iomap_found(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
132 }
133
134 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000135 error = xfs_iomap_write_direct(ip, offset, count, flags,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000136 imap, nimaps, *nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000138 error = xfs_iomap_write_delay(ip, offset, count, flags,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000139 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 if (!error) {
Christoph Hellwig207d0412010-04-28 12:28:56 +0000142 trace_xfs_iomap_alloc(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Christoph Hellwig207d0412010-04-28 12:28:56 +0000144 *new = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 case BMAPI_ALLOCATE:
147 /* If we found an extent, return it */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000148 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 lockmode = 0;
150
Christoph Hellwig207d0412010-04-28 12:28:56 +0000151 if (*nimaps && !isnullstartblock(imap->br_startblock)) {
152 trace_xfs_iomap_found(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 }
155
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000156 error = xfs_iomap_write_allocate(ip, offset, count,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000157 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
Christoph Hellwig207d0412010-04-28 12:28:56 +0000161 ASSERT(*nimaps <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163out:
164 if (lockmode)
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000165 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return XFS_ERROR(error);
167}
168
169STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100170xfs_iomap_eof_align_last_fsb(
171 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000172 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100173 xfs_extlen_t extsize,
174 xfs_fileoff_t *last_fsb)
175{
176 xfs_fileoff_t new_last_fsb = 0;
177 xfs_extlen_t align;
178 int eof, error;
179
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100180 if (XFS_IS_REALTIME_INODE(ip))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100181 ;
182 /*
183 * If mounted with the "-o swalloc" option, roundup the allocation
184 * request to a stripe width boundary if the file size is >=
185 * stripe width and we are allocating past the allocation eof.
186 */
187 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100188 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100189 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
190 /*
191 * Roundup the allocation request to a stripe unit (m_dalign) boundary
192 * if the file size is >= stripe unit size, and we are allocating past
193 * the allocation eof.
194 */
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100195 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100196 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
197
198 /*
199 * Always round up the allocation request to an extent boundary
200 * (when file on a real-time subvolume or has di_extsize hint).
201 */
202 if (extsize) {
203 if (new_last_fsb)
204 align = roundup_64(new_last_fsb, extsize);
205 else
206 align = extsize;
207 new_last_fsb = roundup_64(*last_fsb, align);
208 }
209
210 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000211 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100212 if (error)
213 return error;
214 if (eof)
215 *last_fsb = new_last_fsb;
216 }
217 return 0;
218}
219
220STATIC int
Nathan Scott572d95f2006-09-28 11:03:20 +1000221xfs_cmn_err_fsblock_zero(
222 xfs_inode_t *ip,
223 xfs_bmbt_irec_t *imap)
224{
225 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
226 "Access to block zero in inode %llu "
227 "start_block: %llx start_off: %llx "
228 "blkcnt: %llx extent-state: %x\n",
229 (unsigned long long)ip->i_ino,
230 (unsigned long long)imap->br_startblock,
231 (unsigned long long)imap->br_startoff,
232 (unsigned long long)imap->br_blockcount,
233 imap->br_state);
234 return EFSCORRUPTED;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237int
238xfs_iomap_write_direct(
239 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700240 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 size_t count,
242 int flags,
243 xfs_bmbt_irec_t *ret_imap,
244 int *nmaps,
245 int found)
246{
247 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 xfs_fileoff_t offset_fsb;
249 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100250 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100252 xfs_extlen_t extsz, temp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100253 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000255 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int rt;
257 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100258 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100260 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100262 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * Make sure that the dquots are there. This doesn't hold
266 * the ilock across a disk read.
267 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200268 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (error)
270 return XFS_ERROR(error);
271
Nathan Scottdd9f4382006-01-11 15:28:28 +1100272 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000273 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
David Chinner957d0eb2007-06-18 16:50:37 +1000275 offset_fsb = XFS_B_TO_FSBT(mp, offset);
276 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100277 if ((offset + count) > ip->i_size) {
278 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100279 if (error)
280 goto error_out;
281 } else {
282 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
283 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
284 ret_imap->br_blockcount +
285 ret_imap->br_startoff);
286 }
287 count_fsb = last_fsb - offset_fsb;
288 ASSERT(count_fsb > 0);
289
290 resaligned = count_fsb;
291 if (unlikely(extsz)) {
292 if ((temp = do_mod(offset_fsb, extsz)))
293 resaligned += temp;
294 if ((temp = do_mod(resaligned, extsz)))
295 resaligned += extsz - temp;
296 }
297
298 if (unlikely(rt)) {
299 resrtextents = qblocks = resaligned;
300 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000301 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
302 quota_flag = XFS_QMOPT_RES_RTBLKS;
303 } else {
304 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100305 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000306 quota_flag = XFS_QMOPT_RES_REGBLKS;
307 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000310 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312 xfs_iunlock(ip, XFS_ILOCK_EXCL);
313 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000315 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 XFS_TRANS_PERM_LOG_RES,
317 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000319 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000325 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Christoph Hellwig7d095252009-06-08 15:33:32 +0200327 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100328 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
332 xfs_trans_ihold(tp, ip);
333
Nathan Scottdd9f4382006-01-11 15:28:28 +1100334 bmapi_flag = XFS_BMAPI_WRITE;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000335 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 bmapi_flag |= XFS_BMAPI_PREALLOC;
337
338 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100339 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600341 xfs_bmap_init(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000342 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000343 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000344 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000345 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000349 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100351 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000352 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000354 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000355 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Nathan Scott06d10dd2005-06-21 15:48:47 +1000358 /*
359 * Copy any maps to caller's array and return any error.
360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000362 error = ENOSPC;
363 goto error_out;
364 }
365
David Chinner86c4d622008-04-29 12:53:21 +1000366 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000367 error = xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto error_out;
369 }
370
Eric Sandeen0116d932005-11-02 15:00:01 +1100371 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374
Nathan Scott06d10dd2005-06-21 15:48:47 +1000375error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200377 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Nathan Scott06d10dd2005-06-21 15:48:47 +1000379error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
381 *nmaps = 0; /* nothing set-up here */
382
383error_out:
384 return XFS_ERROR(error);
385}
386
Nathan Scottdd9f4382006-01-11 15:28:28 +1100387/*
Dave Chinner8de2bf92009-04-06 18:49:12 +0200388 * If the caller is doing a write at the end of the file, then extend the
389 * allocation out to the file system's write iosize. We clean up any extra
390 * space left over when the file is closed in xfs_inactive().
Nathan Scottdd9f4382006-01-11 15:28:28 +1100391 */
392STATIC int
393xfs_iomap_eof_want_preallocate(
394 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000395 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100396 xfs_off_t offset,
397 size_t count,
398 int ioflag,
399 xfs_bmbt_irec_t *imap,
400 int nimaps,
401 int *prealloc)
402{
403 xfs_fileoff_t start_fsb;
404 xfs_filblks_t count_fsb;
405 xfs_fsblock_t firstblock;
406 int n, error, imaps;
407
408 *prealloc = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200409 if ((offset + count) <= ip->i_size)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100410 return 0;
411
412 /*
413 * If there are any real blocks past eof, then don't
414 * do any speculative allocation.
415 */
416 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
417 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
418 while (count_fsb > 0) {
419 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100420 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000421 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000422 &firstblock, 0, imap, &imaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100423 if (error)
424 return error;
425 for (n = 0; n < imaps; n++) {
426 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
427 (imap[n].br_startblock != DELAYSTARTBLOCK))
428 return 0;
429 start_fsb += imap[n].br_blockcount;
430 count_fsb -= imap[n].br_blockcount;
431 }
432 }
433 *prealloc = 1;
434 return 0;
435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437int
438xfs_iomap_write_delay(
439 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700440 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 size_t count,
442 int ioflag,
443 xfs_bmbt_irec_t *ret_imap,
444 int *nmaps)
445{
446 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xfs_fileoff_t offset_fsb;
448 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100449 xfs_off_t aligned_offset;
450 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100452 xfs_extlen_t extsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Dave Chinner8de2bf92009-04-06 18:49:12 +0200455 int prealloc, flushed = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100456 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000458 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * Make sure that the dquots are there. This doesn't hold
462 * the ilock across a disk read.
463 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200464 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (error)
466 return XFS_ERROR(error);
467
David Chinner957d0eb2007-06-18 16:50:37 +1000468 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100469 offset_fsb = XFS_B_TO_FSBT(mp, offset);
470
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100471 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100472 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
473 if (error)
474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dave Chinner8de2bf92009-04-06 18:49:12 +0200476retry:
Nathan Scottdd9f4382006-01-11 15:28:28 +1100477 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
479 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100480 last_fsb = ioalign + mp->m_writeio_blocks;
481 } else {
482 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100484
485 if (prealloc || extsz) {
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100486 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100487 if (error)
488 return error;
489 }
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 nimaps = XFS_WRITE_IMAPS;
492 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000493 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 (xfs_filblks_t)(last_fsb - offset_fsb),
495 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
496 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000497 &nimaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100498 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Dave Chinner8de2bf92009-04-06 18:49:12 +0200503 * then we must have run out of space - flush all other inodes with
504 * delalloc blocks and retry without EOF preallocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
506 if (nimaps == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000507 trace_xfs_delalloc_enospc(ip, offset, count);
Dave Chinner8de2bf92009-04-06 18:49:12 +0200508 if (flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return XFS_ERROR(ENOSPC);
510
Dave Chinner8de2bf92009-04-06 18:49:12 +0200511 xfs_iunlock(ip, XFS_ILOCK_EXCL);
512 xfs_flush_inodes(ip);
513 xfs_ilock(ip, XFS_ILOCK_EXCL);
514
515 flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 error = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200517 prealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto retry;
519 }
520
David Chinner86c4d622008-04-29 12:53:21 +1000521 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000522 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100523
524 *ret_imap = imap[0];
525 *nmaps = 1;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return 0;
528}
529
530/*
531 * Pass in a delayed allocate extent, convert it to real extents;
532 * return to the caller the extent we create which maps on top of
533 * the originating callers request.
534 *
535 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100536 *
537 * We no longer bother to look at the incoming map - all we have to
538 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540int
541xfs_iomap_write_allocate(
542 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700543 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700544 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 xfs_bmbt_irec_t *map,
546 int *retmap)
547{
548 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 xfs_fileoff_t offset_fsb, last_block;
550 xfs_fileoff_t end_fsb, map_start_fsb;
551 xfs_fsblock_t first_block;
552 xfs_bmap_free_t free_list;
553 xfs_filblks_t count_fsb;
David Chinnere4143a12007-11-23 16:29:11 +1100554 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 xfs_trans_t *tp;
David Chinnere4143a12007-11-23 16:29:11 +1100556 int nimaps, committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int error = 0;
558 int nres;
559
560 *retmap = 0;
561
562 /*
563 * Make sure that the dquots are there.
564 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200565 error = xfs_qm_dqattach(ip, 0);
566 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return XFS_ERROR(error);
568
Nathan Scott24e17b52005-05-05 13:33:20 -0700569 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700571 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
574
575 while (count_fsb != 0) {
576 /*
577 * Set up a transaction with which to allocate the
578 * backing store for the file. Do allocations in a
579 * loop until we get some space in the range we are
580 * interested in. The other space that might be allocated
581 * is in the delayed allocation extent on which we sit
582 * but before our buffer starts.
583 */
584
585 nimaps = 0;
586 while (nimaps == 0) {
587 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000588 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
590 error = xfs_trans_reserve(tp, nres,
591 XFS_WRITE_LOG_RES(mp),
592 0, XFS_TRANS_PERM_LOG_RES,
593 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (error) {
595 xfs_trans_cancel(tp, 0);
596 return XFS_ERROR(error);
597 }
598 xfs_ilock(ip, XFS_ILOCK_EXCL);
599 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
600 xfs_trans_ihold(tp, ip);
601
Eric Sandeen9d87c312009-01-14 23:22:07 -0600602 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
David Chinnere4143a12007-11-23 16:29:11 +1100605 * it is possible that the extents have changed since
606 * we did the read call as we dropped the ilock for a
607 * while. We have to be careful about truncates or hole
608 * punchs here - we are not allowed to allocate
609 * non-delalloc blocks here.
610 *
611 * The only protection against truncation is the pages
612 * for the range we are being asked to convert are
613 * locked and hence a truncate will block on them
614 * first.
615 *
616 * As a result, if we go beyond the range we really
617 * need and hit an delalloc extent boundary followed by
618 * a hole while we have excess blocks in the map, we
619 * will fill the hole incorrectly and overrun the
620 * transaction reservation.
621 *
622 * Using a single map prevents this as we are forced to
623 * check each map we look for overlap with the desired
624 * range and abort as soon as we find it. Also, given
625 * that we only return a single map, having one beyond
626 * what we can return is probably a bit silly.
627 *
628 * We also need to check that we don't go beyond EOF;
629 * this is a truncate optimisation as a truncate sets
630 * the new file size before block on the pages we
631 * currently have locked under writeback. Because they
632 * are about to be tossed, we don't need to write them
633 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
David Chinnere4143a12007-11-23 16:29:11 +1100635 nimaps = 1;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000636 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
David Chinner7c9ef852008-04-10 12:21:59 +1000637 error = xfs_bmap_last_offset(NULL, ip, &last_block,
638 XFS_DATA_FORK);
639 if (error)
640 goto trans_cancel;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
643 if ((map_start_fsb + count_fsb) > last_block) {
644 count_fsb = last_block - map_start_fsb;
645 if (count_fsb == 0) {
646 error = EAGAIN;
647 goto trans_cancel;
648 }
649 }
650
651 /* Go get the actual blocks */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000652 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 XFS_BMAPI_WRITE, &first_block, 1,
David Chinnere4143a12007-11-23 16:29:11 +1100654 &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (error)
656 goto trans_cancel;
657
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100658 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (error)
660 goto trans_cancel;
661
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000662 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (error)
664 goto error0;
665
666 xfs_iunlock(ip, XFS_ILOCK_EXCL);
667 }
668
669 /*
670 * See if we were able to allocate an extent that
671 * covers at least part of the callers request
672 */
David Chinner86c4d622008-04-29 12:53:21 +1000673 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
David Chinnere4143a12007-11-23 16:29:11 +1100674 return xfs_cmn_err_fsblock_zero(ip, &imap);
David Chinner86c4d622008-04-29 12:53:21 +1000675
David Chinnere4143a12007-11-23 16:29:11 +1100676 if ((offset_fsb >= imap.br_startoff) &&
677 (offset_fsb < (imap.br_startoff +
678 imap.br_blockcount))) {
679 *map = imap;
680 *retmap = 1;
681 XFS_STATS_INC(xs_xstrat_quick);
682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
David Chinnere4143a12007-11-23 16:29:11 +1100685 /*
686 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * file, just surrounding data, try again.
688 */
David Chinnere4143a12007-11-23 16:29:11 +1100689 count_fsb -= imap.br_blockcount;
690 map_start_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693trans_cancel:
694 xfs_bmap_cancel(&free_list);
695 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
696error0:
697 xfs_iunlock(ip, XFS_ILOCK_EXCL);
698 return XFS_ERROR(error);
699}
700
701int
702xfs_iomap_write_unwritten(
703 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700704 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 size_t count)
706{
707 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_fileoff_t offset_fsb;
709 xfs_filblks_t count_fsb;
710 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100711 xfs_fsblock_t firstfsb;
712 int nimaps;
713 xfs_trans_t *tp;
714 xfs_bmbt_irec_t imap;
715 xfs_bmap_free_t free_list;
716 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int committed;
718 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000720 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 offset_fsb = XFS_B_TO_FSBT(mp, offset);
723 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
724 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
725
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000726 /*
727 * Reserve enough blocks in this transaction for two complete extent
728 * btree splits. We may be converting the middle part of an unwritten
729 * extent and in this case we will insert two new extents in the btree
730 * each of which could cause a full split.
731 *
732 * This reservation amount will be used in the first call to
733 * xfs_bmbt_split() to select an AG with enough space to satisfy the
734 * rest of the operation.
735 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100736 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Nathan Scottdd9f4382006-01-11 15:28:28 +1100738 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
740 * set up a transaction to convert the range of extents
741 * from unwritten to real. Do allocations in a loop until
742 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000743 *
744 * Note that we open code the transaction allocation here
745 * to pass KM_NOFS--we can't risk to recursing back into
746 * the filesystem here as we might be asked to write out
747 * the same inode that we complete here and might deadlock
748 * on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000750 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
751 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
David Chinner84e1e992007-06-18 16:50:27 +1000752 tp->t_flags |= XFS_TRANS_RESERVE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100753 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 XFS_WRITE_LOG_RES(mp), 0,
755 XFS_TRANS_PERM_LOG_RES,
756 XFS_WRITE_LOG_COUNT);
757 if (error) {
758 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000759 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 xfs_ilock(ip, XFS_ILOCK_EXCL);
763 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
764 xfs_trans_ihold(tp, ip);
765
766 /*
767 * Modify the unwritten extent state of the buffer.
768 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600769 xfs_bmap_init(&free_list, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000771 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100772 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000773 1, &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (error)
775 goto error_on_bmapi_transaction;
776
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100777 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (error)
779 goto error_on_bmapi_transaction;
780
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000781 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 xfs_iunlock(ip, XFS_ILOCK_EXCL);
783 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000784 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100785
David Chinner86c4d622008-04-29 12:53:21 +1000786 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000787 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if ((numblks_fsb = imap.br_blockcount) == 0) {
790 /*
791 * The numblks_fsb value should always get
792 * smaller, otherwise the loop is stuck.
793 */
794 ASSERT(imap.br_blockcount);
795 break;
796 }
797 offset_fsb += numblks_fsb;
798 count_fsb -= numblks_fsb;
799 } while (count_fsb > 0);
800
801 return 0;
802
803error_on_bmapi_transaction:
804 xfs_bmap_cancel(&free_list);
805 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
806 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return XFS_ERROR(error);
808}