blob: 772f3e791ebed1ea8cf1f334dd4de87051d2e3eb [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_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
33#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_rtalloc.h"
37#include "xfs_error.h"
38#include "xfs_itable.h"
39#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_attr.h"
41#include "xfs_buf_item.h"
42#include "xfs_trans_space.h"
43#include "xfs_utils.h"
44#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
49 << mp->m_writeio_log)
50#define XFS_STRAT_WRITE_IMAPS 2
51#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
52
Christoph Hellwigb4ed4622010-04-28 12:29:01 +000053STATIC int xfs_iomap_write_direct(struct xfs_inode *, xfs_off_t, size_t,
54 int, struct xfs_bmbt_irec *, int *);
55STATIC int xfs_iomap_write_delay(struct xfs_inode *, xfs_off_t, size_t, int,
56 struct xfs_bmbt_irec *, int *);
57STATIC int xfs_iomap_write_allocate(struct xfs_inode *, xfs_off_t, size_t,
58 struct xfs_bmbt_irec *, int *);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060int
61xfs_iomap(
Christoph Hellwig207d0412010-04-28 12:28:56 +000062 struct xfs_inode *ip,
63 xfs_off_t offset,
64 ssize_t count,
65 int flags,
66 struct xfs_bmbt_irec *imap,
67 int *nimaps,
68 int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Christoph Hellwig207d0412010-04-28 12:28:56 +000070 struct xfs_mount *mp = ip->i_mount;
71 xfs_fileoff_t offset_fsb, end_fsb;
72 int error = 0;
73 int lockmode = 0;
74 int bmapi_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100076 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
Christoph Hellwig207d0412010-04-28 12:28:56 +000077
78 *new = 0;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (XFS_FORCED_SHUTDOWN(mp))
81 return XFS_ERROR(EIO);
82
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000083 trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
84
Christoph Hellwig76428612007-09-14 15:23:31 +100085 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 case BMAPI_READ:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100087 lockmode = xfs_ilock_map_shared(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 break;
90 case BMAPI_WRITE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100091 lockmode = XFS_ILOCK_EXCL;
Nathan Scottc31e8872005-09-05 10:06:55 +100092 if (flags & BMAPI_IGNSTATE)
93 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100094 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
96 case BMAPI_ALLOCATE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100097 lockmode = XFS_ILOCK_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 bmapi_flags = XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* Attempt non-blocking lock */
101 if (flags & BMAPI_TRYLOCK) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000102 if (!xfs_ilock_nowait(ip, lockmode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return XFS_ERROR(EAGAIN);
104 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000105 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 default:
109 BUG();
110 }
111
112 ASSERT(offset <= mp->m_maxioffset);
113 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
114 count = mp->m_maxioffset - offset;
115 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
116 offset_fsb = XFS_B_TO_FSBT(mp, offset);
117
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000118 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 (xfs_filblks_t)(end_fsb - offset_fsb),
Christoph Hellwig207d0412010-04-28 12:28:56 +0000120 bmapi_flags, NULL, 0, imap,
121 nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (error)
124 goto out;
125
Christoph Hellwig76428612007-09-14 15:23:31 +1000126 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 case BMAPI_WRITE:
128 /* If we found an extent, return it */
Christoph Hellwig207d0412010-04-28 12:28:56 +0000129 if (*nimaps &&
130 (imap->br_startblock != HOLESTARTBLOCK) &&
131 (imap->br_startblock != DELAYSTARTBLOCK)) {
132 trace_xfs_iomap_found(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 break;
134 }
135
136 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000137 error = xfs_iomap_write_direct(ip, offset, count, flags,
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000138 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000140 error = xfs_iomap_write_delay(ip, offset, count, flags,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000141 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 if (!error) {
Christoph Hellwig207d0412010-04-28 12:28:56 +0000144 trace_xfs_iomap_alloc(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Christoph Hellwig207d0412010-04-28 12:28:56 +0000146 *new = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148 case BMAPI_ALLOCATE:
149 /* If we found an extent, return it */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000150 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 lockmode = 0;
152
Christoph Hellwig207d0412010-04-28 12:28:56 +0000153 if (*nimaps && !isnullstartblock(imap->br_startblock)) {
154 trace_xfs_iomap_found(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
156 }
157
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000158 error = xfs_iomap_write_allocate(ip, offset, count,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000159 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
Christoph Hellwig207d0412010-04-28 12:28:56 +0000163 ASSERT(*nimaps <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165out:
166 if (lockmode)
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000167 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return XFS_ERROR(error);
169}
170
171STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100172xfs_iomap_eof_align_last_fsb(
173 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000174 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100175 xfs_extlen_t extsize,
176 xfs_fileoff_t *last_fsb)
177{
178 xfs_fileoff_t new_last_fsb = 0;
179 xfs_extlen_t align;
180 int eof, error;
181
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100182 if (XFS_IS_REALTIME_INODE(ip))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100183 ;
184 /*
185 * If mounted with the "-o swalloc" option, roundup the allocation
186 * request to a stripe width boundary if the file size is >=
187 * stripe width and we are allocating past the allocation eof.
188 */
189 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100190 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100191 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
192 /*
193 * Roundup the allocation request to a stripe unit (m_dalign) boundary
194 * if the file size is >= stripe unit size, and we are allocating past
195 * the allocation eof.
196 */
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100197 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100198 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
199
200 /*
201 * Always round up the allocation request to an extent boundary
202 * (when file on a real-time subvolume or has di_extsize hint).
203 */
204 if (extsize) {
205 if (new_last_fsb)
206 align = roundup_64(new_last_fsb, extsize);
207 else
208 align = extsize;
209 new_last_fsb = roundup_64(*last_fsb, align);
210 }
211
212 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000213 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100214 if (error)
215 return error;
216 if (eof)
217 *last_fsb = new_last_fsb;
218 }
219 return 0;
220}
221
222STATIC int
Nathan Scott572d95f2006-09-28 11:03:20 +1000223xfs_cmn_err_fsblock_zero(
224 xfs_inode_t *ip,
225 xfs_bmbt_irec_t *imap)
226{
227 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
228 "Access to block zero in inode %llu "
229 "start_block: %llx start_off: %llx "
230 "blkcnt: %llx extent-state: %x\n",
231 (unsigned long long)ip->i_ino,
232 (unsigned long long)imap->br_startblock,
233 (unsigned long long)imap->br_startoff,
234 (unsigned long long)imap->br_blockcount,
235 imap->br_state);
236 return EFSCORRUPTED;
237}
238
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000239STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240xfs_iomap_write_direct(
241 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700242 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 size_t count,
244 int flags,
245 xfs_bmbt_irec_t *ret_imap,
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000246 int *nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 xfs_fileoff_t offset_fsb;
250 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100251 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100253 xfs_extlen_t extsz, temp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100254 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000256 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int rt;
258 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100259 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100261 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100263 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
266 * Make sure that the dquots are there. This doesn't hold
267 * the ilock across a disk read.
268 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200269 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (error)
271 return XFS_ERROR(error);
272
Nathan Scottdd9f4382006-01-11 15:28:28 +1100273 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000274 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David Chinner957d0eb2007-06-18 16:50:37 +1000276 offset_fsb = XFS_B_TO_FSBT(mp, offset);
277 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100278 if ((offset + count) > ip->i_size) {
279 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100280 if (error)
281 goto error_out;
282 } else {
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000283 if (*nmaps && (ret_imap->br_startblock == HOLESTARTBLOCK))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100284 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
285 ret_imap->br_blockcount +
286 ret_imap->br_startoff);
287 }
288 count_fsb = last_fsb - offset_fsb;
289 ASSERT(count_fsb > 0);
290
291 resaligned = count_fsb;
292 if (unlikely(extsz)) {
293 if ((temp = do_mod(offset_fsb, extsz)))
294 resaligned += temp;
295 if ((temp = do_mod(resaligned, extsz)))
296 resaligned += extsz - temp;
297 }
298
299 if (unlikely(rt)) {
300 resrtextents = qblocks = resaligned;
301 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000302 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
303 quota_flag = XFS_QMOPT_RES_RTBLKS;
304 } else {
305 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100306 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000307 quota_flag = XFS_QMOPT_RES_REGBLKS;
308 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000311 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313 xfs_iunlock(ip, XFS_ILOCK_EXCL);
314 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000316 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 XFS_TRANS_PERM_LOG_RES,
318 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000320 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
322 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000326 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Christoph Hellwig7d095252009-06-08 15:33:32 +0200328 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100329 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
333 xfs_trans_ihold(tp, ip);
334
Nathan Scottdd9f4382006-01-11 15:28:28 +1100335 bmapi_flag = XFS_BMAPI_WRITE;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000336 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 bmapi_flag |= XFS_BMAPI_PREALLOC;
338
339 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100340 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600342 xfs_bmap_init(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000343 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000344 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000345 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000346 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000350 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100352 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000355 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000356 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Nathan Scott06d10dd2005-06-21 15:48:47 +1000359 /*
360 * Copy any maps to caller's array and return any error.
361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000363 error = ENOSPC;
364 goto error_out;
365 }
366
David Chinner86c4d622008-04-29 12:53:21 +1000367 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000368 error = xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto error_out;
370 }
371
Eric Sandeen0116d932005-11-02 15:00:01 +1100372 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375
Nathan Scott06d10dd2005-06-21 15:48:47 +1000376error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200378 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Nathan Scott06d10dd2005-06-21 15:48:47 +1000380error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
382 *nmaps = 0; /* nothing set-up here */
383
384error_out:
385 return XFS_ERROR(error);
386}
387
Nathan Scottdd9f4382006-01-11 15:28:28 +1100388/*
Dave Chinner8de2bf92009-04-06 18:49:12 +0200389 * If the caller is doing a write at the end of the file, then extend the
390 * allocation out to the file system's write iosize. We clean up any extra
391 * space left over when the file is closed in xfs_inactive().
Nathan Scottdd9f4382006-01-11 15:28:28 +1100392 */
393STATIC int
394xfs_iomap_eof_want_preallocate(
395 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000396 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100397 xfs_off_t offset,
398 size_t count,
399 int ioflag,
400 xfs_bmbt_irec_t *imap,
401 int nimaps,
402 int *prealloc)
403{
404 xfs_fileoff_t start_fsb;
405 xfs_filblks_t count_fsb;
406 xfs_fsblock_t firstblock;
407 int n, error, imaps;
408
409 *prealloc = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200410 if ((offset + count) <= ip->i_size)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100411 return 0;
412
413 /*
414 * If there are any real blocks past eof, then don't
415 * do any speculative allocation.
416 */
417 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
418 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
419 while (count_fsb > 0) {
420 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100421 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000422 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000423 &firstblock, 0, imap, &imaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100424 if (error)
425 return error;
426 for (n = 0; n < imaps; n++) {
427 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
428 (imap[n].br_startblock != DELAYSTARTBLOCK))
429 return 0;
430 start_fsb += imap[n].br_blockcount;
431 count_fsb -= imap[n].br_blockcount;
432 }
433 }
434 *prealloc = 1;
435 return 0;
436}
437
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000438STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439xfs_iomap_write_delay(
440 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700441 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 size_t count,
443 int ioflag,
444 xfs_bmbt_irec_t *ret_imap,
445 int *nmaps)
446{
447 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 xfs_fileoff_t offset_fsb;
449 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100450 xfs_off_t aligned_offset;
451 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100453 xfs_extlen_t extsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Dave Chinner8de2bf92009-04-06 18:49:12 +0200456 int prealloc, flushed = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100457 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000459 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /*
462 * Make sure that the dquots are there. This doesn't hold
463 * the ilock across a disk read.
464 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200465 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (error)
467 return XFS_ERROR(error);
468
David Chinner957d0eb2007-06-18 16:50:37 +1000469 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100470 offset_fsb = XFS_B_TO_FSBT(mp, offset);
471
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100472 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100473 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
474 if (error)
475 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Dave Chinner8de2bf92009-04-06 18:49:12 +0200477retry:
Nathan Scottdd9f4382006-01-11 15:28:28 +1100478 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
480 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100481 last_fsb = ioalign + mp->m_writeio_blocks;
482 } else {
483 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100485
486 if (prealloc || extsz) {
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100487 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100488 if (error)
489 return error;
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 nimaps = XFS_WRITE_IMAPS;
493 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000494 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 (xfs_filblks_t)(last_fsb - offset_fsb),
496 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
497 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000498 &nimaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100499 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Dave Chinner8de2bf92009-04-06 18:49:12 +0200504 * then we must have run out of space - flush all other inodes with
505 * delalloc blocks and retry without EOF preallocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
507 if (nimaps == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000508 trace_xfs_delalloc_enospc(ip, offset, count);
Dave Chinner8de2bf92009-04-06 18:49:12 +0200509 if (flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return XFS_ERROR(ENOSPC);
511
Dave Chinner8de2bf92009-04-06 18:49:12 +0200512 xfs_iunlock(ip, XFS_ILOCK_EXCL);
513 xfs_flush_inodes(ip);
514 xfs_ilock(ip, XFS_ILOCK_EXCL);
515
516 flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 error = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200518 prealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto retry;
520 }
521
David Chinner86c4d622008-04-29 12:53:21 +1000522 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000523 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100524
525 *ret_imap = imap[0];
526 *nmaps = 1;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529}
530
531/*
532 * Pass in a delayed allocate extent, convert it to real extents;
533 * return to the caller the extent we create which maps on top of
534 * the originating callers request.
535 *
536 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100537 *
538 * We no longer bother to look at the incoming map - all we have to
539 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000541STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542xfs_iomap_write_allocate(
543 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700544 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700545 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 xfs_bmbt_irec_t *map,
547 int *retmap)
548{
549 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 xfs_fileoff_t offset_fsb, last_block;
551 xfs_fileoff_t end_fsb, map_start_fsb;
552 xfs_fsblock_t first_block;
553 xfs_bmap_free_t free_list;
554 xfs_filblks_t count_fsb;
David Chinnere4143a12007-11-23 16:29:11 +1100555 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xfs_trans_t *tp;
David Chinnere4143a12007-11-23 16:29:11 +1100557 int nimaps, committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int error = 0;
559 int nres;
560
561 *retmap = 0;
562
563 /*
564 * Make sure that the dquots are there.
565 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200566 error = xfs_qm_dqattach(ip, 0);
567 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return XFS_ERROR(error);
569
Nathan Scott24e17b52005-05-05 13:33:20 -0700570 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700572 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
575
576 while (count_fsb != 0) {
577 /*
578 * Set up a transaction with which to allocate the
579 * backing store for the file. Do allocations in a
580 * loop until we get some space in the range we are
581 * interested in. The other space that might be allocated
582 * is in the delayed allocation extent on which we sit
583 * but before our buffer starts.
584 */
585
586 nimaps = 0;
587 while (nimaps == 0) {
588 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000589 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
591 error = xfs_trans_reserve(tp, nres,
592 XFS_WRITE_LOG_RES(mp),
593 0, XFS_TRANS_PERM_LOG_RES,
594 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (error) {
596 xfs_trans_cancel(tp, 0);
597 return XFS_ERROR(error);
598 }
599 xfs_ilock(ip, XFS_ILOCK_EXCL);
600 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
601 xfs_trans_ihold(tp, ip);
602
Eric Sandeen9d87c312009-01-14 23:22:07 -0600603 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
David Chinnere4143a12007-11-23 16:29:11 +1100606 * it is possible that the extents have changed since
607 * we did the read call as we dropped the ilock for a
608 * while. We have to be careful about truncates or hole
609 * punchs here - we are not allowed to allocate
610 * non-delalloc blocks here.
611 *
612 * The only protection against truncation is the pages
613 * for the range we are being asked to convert are
614 * locked and hence a truncate will block on them
615 * first.
616 *
617 * As a result, if we go beyond the range we really
618 * need and hit an delalloc extent boundary followed by
619 * a hole while we have excess blocks in the map, we
620 * will fill the hole incorrectly and overrun the
621 * transaction reservation.
622 *
623 * Using a single map prevents this as we are forced to
624 * check each map we look for overlap with the desired
625 * range and abort as soon as we find it. Also, given
626 * that we only return a single map, having one beyond
627 * what we can return is probably a bit silly.
628 *
629 * We also need to check that we don't go beyond EOF;
630 * this is a truncate optimisation as a truncate sets
631 * the new file size before block on the pages we
632 * currently have locked under writeback. Because they
633 * are about to be tossed, we don't need to write them
634 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
David Chinnere4143a12007-11-23 16:29:11 +1100636 nimaps = 1;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000637 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
David Chinner7c9ef852008-04-10 12:21:59 +1000638 error = xfs_bmap_last_offset(NULL, ip, &last_block,
639 XFS_DATA_FORK);
640 if (error)
641 goto trans_cancel;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
644 if ((map_start_fsb + count_fsb) > last_block) {
645 count_fsb = last_block - map_start_fsb;
646 if (count_fsb == 0) {
647 error = EAGAIN;
648 goto trans_cancel;
649 }
650 }
651
652 /* Go get the actual blocks */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000653 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 XFS_BMAPI_WRITE, &first_block, 1,
David Chinnere4143a12007-11-23 16:29:11 +1100655 &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (error)
657 goto trans_cancel;
658
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100659 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (error)
661 goto trans_cancel;
662
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000663 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (error)
665 goto error0;
666
667 xfs_iunlock(ip, XFS_ILOCK_EXCL);
668 }
669
670 /*
671 * See if we were able to allocate an extent that
672 * covers at least part of the callers request
673 */
David Chinner86c4d622008-04-29 12:53:21 +1000674 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
David Chinnere4143a12007-11-23 16:29:11 +1100675 return xfs_cmn_err_fsblock_zero(ip, &imap);
David Chinner86c4d622008-04-29 12:53:21 +1000676
David Chinnere4143a12007-11-23 16:29:11 +1100677 if ((offset_fsb >= imap.br_startoff) &&
678 (offset_fsb < (imap.br_startoff +
679 imap.br_blockcount))) {
680 *map = imap;
681 *retmap = 1;
682 XFS_STATS_INC(xs_xstrat_quick);
683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
David Chinnere4143a12007-11-23 16:29:11 +1100686 /*
687 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * file, just surrounding data, try again.
689 */
David Chinnere4143a12007-11-23 16:29:11 +1100690 count_fsb -= imap.br_blockcount;
691 map_start_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
694trans_cancel:
695 xfs_bmap_cancel(&free_list);
696 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
697error0:
698 xfs_iunlock(ip, XFS_ILOCK_EXCL);
699 return XFS_ERROR(error);
700}
701
702int
703xfs_iomap_write_unwritten(
704 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700705 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 size_t count)
707{
708 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 xfs_fileoff_t offset_fsb;
710 xfs_filblks_t count_fsb;
711 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100712 xfs_fsblock_t firstfsb;
713 int nimaps;
714 xfs_trans_t *tp;
715 xfs_bmbt_irec_t imap;
716 xfs_bmap_free_t free_list;
717 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int committed;
719 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000721 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 offset_fsb = XFS_B_TO_FSBT(mp, offset);
724 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
725 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
726
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000727 /*
728 * Reserve enough blocks in this transaction for two complete extent
729 * btree splits. We may be converting the middle part of an unwritten
730 * extent and in this case we will insert two new extents in the btree
731 * each of which could cause a full split.
732 *
733 * This reservation amount will be used in the first call to
734 * xfs_bmbt_split() to select an AG with enough space to satisfy the
735 * rest of the operation.
736 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100737 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Nathan Scottdd9f4382006-01-11 15:28:28 +1100739 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /*
741 * set up a transaction to convert the range of extents
742 * from unwritten to real. Do allocations in a loop until
743 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000744 *
745 * Note that we open code the transaction allocation here
746 * to pass KM_NOFS--we can't risk to recursing back into
747 * the filesystem here as we might be asked to write out
748 * the same inode that we complete here and might deadlock
749 * on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000751 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
752 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
David Chinner84e1e992007-06-18 16:50:27 +1000753 tp->t_flags |= XFS_TRANS_RESERVE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100754 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 XFS_WRITE_LOG_RES(mp), 0,
756 XFS_TRANS_PERM_LOG_RES,
757 XFS_WRITE_LOG_COUNT);
758 if (error) {
759 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000760 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 xfs_ilock(ip, XFS_ILOCK_EXCL);
764 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
765 xfs_trans_ihold(tp, ip);
766
767 /*
768 * Modify the unwritten extent state of the buffer.
769 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600770 xfs_bmap_init(&free_list, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000772 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100773 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000774 1, &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (error)
776 goto error_on_bmapi_transaction;
777
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100778 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (error)
780 goto error_on_bmapi_transaction;
781
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000782 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 xfs_iunlock(ip, XFS_ILOCK_EXCL);
784 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000785 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100786
David Chinner86c4d622008-04-29 12:53:21 +1000787 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000788 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if ((numblks_fsb = imap.br_blockcount) == 0) {
791 /*
792 * The numblks_fsb value should always get
793 * smaller, otherwise the loop is stuck.
794 */
795 ASSERT(imap.br_blockcount);
796 break;
797 }
798 offset_fsb += numblks_fsb;
799 count_fsb -= numblks_fsb;
800 } while (count_fsb > 0);
801
802 return 0;
803
804error_on_bmapi_transaction:
805 xfs_bmap_cancel(&free_list);
806 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
807 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return XFS_ERROR(error);
809}