blob: 991291068378d919896a30a0d7c10c54b1428bb7 [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,
Christoph Hellwig405f8042010-12-10 08:42:19 +000054 struct xfs_bmbt_irec *, int);
55STATIC int xfs_iomap_write_delay(struct xfs_inode *, xfs_off_t, size_t,
56 struct xfs_bmbt_irec *);
Christoph Hellwigb4ed4622010-04-28 12:29:01 +000057STATIC int xfs_iomap_write_allocate(struct xfs_inode *, xfs_off_t, size_t,
Christoph Hellwig405f8042010-12-10 08:42:19 +000058 struct xfs_bmbt_irec *);
Christoph Hellwigb4ed4622010-04-28 12:29:01 +000059
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 Hellwig579aa9ca2008-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 Hellwig579aa9ca2008-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,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000121 nimaps, 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
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +1000136 if (flags & BMAPI_DIRECT) {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000137 error = xfs_iomap_write_direct(ip, offset, count, imap,
138 *nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000140 error = xfs_iomap_write_delay(ip, offset, count, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Christoph Hellwig405f8042010-12-10 08:42:19 +0000142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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
Christoph Hellwig405f8042010-12-10 08:42:19 +0000158 error = xfs_iomap_write_allocate(ip, offset, count, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162out:
163 if (lockmode)
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000164 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return XFS_ERROR(error);
166}
167
168STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100169xfs_iomap_eof_align_last_fsb(
170 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000171 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100172 xfs_extlen_t extsize,
173 xfs_fileoff_t *last_fsb)
174{
175 xfs_fileoff_t new_last_fsb = 0;
176 xfs_extlen_t align;
177 int eof, error;
178
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100179 if (XFS_IS_REALTIME_INODE(ip))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100180 ;
181 /*
182 * If mounted with the "-o swalloc" option, roundup the allocation
183 * request to a stripe width boundary if the file size is >=
184 * stripe width and we are allocating past the allocation eof.
185 */
186 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100187 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100188 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
189 /*
190 * Roundup the allocation request to a stripe unit (m_dalign) boundary
191 * if the file size is >= stripe unit size, and we are allocating past
192 * the allocation eof.
193 */
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100194 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100195 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
196
197 /*
198 * Always round up the allocation request to an extent boundary
199 * (when file on a real-time subvolume or has di_extsize hint).
200 */
201 if (extsize) {
202 if (new_last_fsb)
203 align = roundup_64(new_last_fsb, extsize);
204 else
205 align = extsize;
206 new_last_fsb = roundup_64(*last_fsb, align);
207 }
208
209 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000210 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100211 if (error)
212 return error;
213 if (eof)
214 *last_fsb = new_last_fsb;
215 }
216 return 0;
217}
218
219STATIC int
Nathan Scott572d95f2006-09-28 11:03:20 +1000220xfs_cmn_err_fsblock_zero(
221 xfs_inode_t *ip,
222 xfs_bmbt_irec_t *imap)
223{
224 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
225 "Access to block zero in inode %llu "
226 "start_block: %llx start_off: %llx "
227 "blkcnt: %llx extent-state: %x\n",
228 (unsigned long long)ip->i_ino,
229 (unsigned long long)imap->br_startblock,
230 (unsigned long long)imap->br_startoff,
231 (unsigned long long)imap->br_blockcount,
232 imap->br_state);
233 return EFSCORRUPTED;
234}
235
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000236STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237xfs_iomap_write_direct(
238 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700239 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 size_t count,
Christoph Hellwig30704512010-06-24 11:42:19 +1000241 xfs_bmbt_irec_t *imap,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000242 int nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 xfs_fileoff_t offset_fsb;
246 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100247 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100249 xfs_extlen_t extsz, temp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100250 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000252 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int rt;
254 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100256 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100258 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * Make sure that the dquots are there. This doesn't hold
262 * the ilock across a disk read.
263 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200264 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (error)
266 return XFS_ERROR(error);
267
Nathan Scottdd9f4382006-01-11 15:28:28 +1100268 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000269 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David Chinner957d0eb2007-06-18 16:50:37 +1000271 offset_fsb = XFS_B_TO_FSBT(mp, offset);
272 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100273 if ((offset + count) > ip->i_size) {
274 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100275 if (error)
276 goto error_out;
277 } else {
Christoph Hellwig405f8042010-12-10 08:42:19 +0000278 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100279 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
Christoph Hellwig30704512010-06-24 11:42:19 +1000280 imap->br_blockcount +
281 imap->br_startoff);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100282 }
283 count_fsb = last_fsb - offset_fsb;
284 ASSERT(count_fsb > 0);
285
286 resaligned = count_fsb;
287 if (unlikely(extsz)) {
288 if ((temp = do_mod(offset_fsb, extsz)))
289 resaligned += temp;
290 if ((temp = do_mod(resaligned, extsz)))
291 resaligned += extsz - temp;
292 }
293
294 if (unlikely(rt)) {
295 resrtextents = qblocks = resaligned;
296 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000297 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
298 quota_flag = XFS_QMOPT_RES_RTBLKS;
299 } else {
300 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100301 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000302 quota_flag = XFS_QMOPT_RES_REGBLKS;
303 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000306 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
308 xfs_iunlock(ip, XFS_ILOCK_EXCL);
309 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000311 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 XFS_TRANS_PERM_LOG_RES,
313 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000315 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000321 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Christoph Hellwig7d095252009-06-08 15:33:32 +0200323 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Christoph Hellwig898621d2010-06-24 11:36:58 +1000327 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Nathan Scottdd9f4382006-01-11 15:28:28 +1100329 bmapi_flag = XFS_BMAPI_WRITE;
Christoph Hellwig405f8042010-12-10 08:42:19 +0000330 if (offset < ip->i_size || extsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 bmapi_flag |= XFS_BMAPI_PREALLOC;
332
333 /*
Christoph Hellwig30704512010-06-24 11:42:19 +1000334 * Issue the xfs_bmapi() call to allocate the blocks.
335 *
336 * From this point onwards we overwrite the imap pointer that the
337 * caller gave to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600339 xfs_bmap_init(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000340 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000341 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
Christoph Hellwig30704512010-06-24 11:42:19 +1000342 &firstfsb, 0, imap, &nimaps, &free_list);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000343 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000347 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100349 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000350 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000352 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Nathan Scott06d10dd2005-06-21 15:48:47 +1000356 /*
357 * Copy any maps to caller's array and return any error.
358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000360 error = ENOSPC;
361 goto error_out;
362 }
363
Christoph Hellwig30704512010-06-24 11:42:19 +1000364 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip))) {
365 error = xfs_cmn_err_fsblock_zero(ip, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto error_out;
367 }
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370
Nathan Scott06d10dd2005-06-21 15:48:47 +1000371error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200373 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Nathan Scott06d10dd2005-06-21 15:48:47 +1000375error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378error_out:
379 return XFS_ERROR(error);
380}
381
Nathan Scottdd9f4382006-01-11 15:28:28 +1100382/*
Dave Chinner8de2bf92009-04-06 18:49:12 +0200383 * If the caller is doing a write at the end of the file, then extend the
384 * allocation out to the file system's write iosize. We clean up any extra
385 * space left over when the file is closed in xfs_inactive().
Nathan Scottdd9f4382006-01-11 15:28:28 +1100386 */
387STATIC int
388xfs_iomap_eof_want_preallocate(
389 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000390 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100391 xfs_off_t offset,
392 size_t count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100393 xfs_bmbt_irec_t *imap,
394 int nimaps,
395 int *prealloc)
396{
397 xfs_fileoff_t start_fsb;
398 xfs_filblks_t count_fsb;
399 xfs_fsblock_t firstblock;
400 int n, error, imaps;
401
402 *prealloc = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200403 if ((offset + count) <= ip->i_size)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100404 return 0;
405
406 /*
407 * If there are any real blocks past eof, then don't
408 * do any speculative allocation.
409 */
410 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
411 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
412 while (count_fsb > 0) {
413 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100414 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000415 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000416 &firstblock, 0, imap, &imaps, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100417 if (error)
418 return error;
419 for (n = 0; n < imaps; n++) {
420 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
421 (imap[n].br_startblock != DELAYSTARTBLOCK))
422 return 0;
423 start_fsb += imap[n].br_blockcount;
424 count_fsb -= imap[n].br_blockcount;
425 }
426 }
427 *prealloc = 1;
428 return 0;
429}
430
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000431STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432xfs_iomap_write_delay(
433 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700434 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 size_t count,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000436 xfs_bmbt_irec_t *ret_imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 xfs_fileoff_t offset_fsb;
440 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100441 xfs_off_t aligned_offset;
442 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100444 xfs_extlen_t extsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Dave Chinner8de2bf92009-04-06 18:49:12 +0200447 int prealloc, flushed = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100448 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000450 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /*
453 * Make sure that the dquots are there. This doesn't hold
454 * the ilock across a disk read.
455 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200456 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (error)
458 return XFS_ERROR(error);
459
David Chinner957d0eb2007-06-18 16:50:37 +1000460 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100461 offset_fsb = XFS_B_TO_FSBT(mp, offset);
462
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100463 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000464 imap, XFS_WRITE_IMAPS, &prealloc);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100465 if (error)
466 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dave Chinner8de2bf92009-04-06 18:49:12 +0200468retry:
Nathan Scottdd9f4382006-01-11 15:28:28 +1100469 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
471 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100472 last_fsb = ioalign + mp->m_writeio_blocks;
473 } else {
474 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100476
477 if (prealloc || extsz) {
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100478 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100479 if (error)
480 return error;
481 }
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 nimaps = XFS_WRITE_IMAPS;
484 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000485 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 (xfs_filblks_t)(last_fsb - offset_fsb),
487 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
488 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000489 &nimaps, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100490 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Dave Chinner8de2bf92009-04-06 18:49:12 +0200495 * then we must have run out of space - flush all other inodes with
496 * delalloc blocks and retry without EOF preallocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
498 if (nimaps == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000499 trace_xfs_delalloc_enospc(ip, offset, count);
Dave Chinner8de2bf92009-04-06 18:49:12 +0200500 if (flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return XFS_ERROR(ENOSPC);
502
Dave Chinner8de2bf92009-04-06 18:49:12 +0200503 xfs_iunlock(ip, XFS_ILOCK_EXCL);
504 xfs_flush_inodes(ip);
505 xfs_ilock(ip, XFS_ILOCK_EXCL);
506
507 flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 error = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200509 prealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto retry;
511 }
512
David Chinner86c4d622008-04-29 12:53:21 +1000513 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000514 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100515
516 *ret_imap = imap[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return 0;
518}
519
520/*
521 * Pass in a delayed allocate extent, convert it to real extents;
522 * return to the caller the extent we create which maps on top of
523 * the originating callers request.
524 *
525 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100526 *
527 * We no longer bother to look at the incoming map - all we have to
528 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000530STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531xfs_iomap_write_allocate(
532 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700533 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700534 size_t count,
Christoph Hellwig405f8042010-12-10 08:42:19 +0000535 xfs_bmbt_irec_t *imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 xfs_fileoff_t offset_fsb, last_block;
539 xfs_fileoff_t end_fsb, map_start_fsb;
540 xfs_fsblock_t first_block;
541 xfs_bmap_free_t free_list;
542 xfs_filblks_t count_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 xfs_trans_t *tp;
David Chinnere4143a12007-11-23 16:29:11 +1100544 int nimaps, committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int error = 0;
546 int nres;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * Make sure that the dquots are there.
550 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200551 error = xfs_qm_dqattach(ip, 0);
552 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return XFS_ERROR(error);
554
Nathan Scott24e17b52005-05-05 13:33:20 -0700555 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Christoph Hellwig30704512010-06-24 11:42:19 +1000556 count_fsb = imap->br_blockcount;
557 map_start_fsb = imap->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
560
561 while (count_fsb != 0) {
562 /*
563 * Set up a transaction with which to allocate the
564 * backing store for the file. Do allocations in a
565 * loop until we get some space in the range we are
566 * interested in. The other space that might be allocated
567 * is in the delayed allocation extent on which we sit
568 * but before our buffer starts.
569 */
570
571 nimaps = 0;
572 while (nimaps == 0) {
573 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000574 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
576 error = xfs_trans_reserve(tp, nres,
577 XFS_WRITE_LOG_RES(mp),
578 0, XFS_TRANS_PERM_LOG_RES,
579 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (error) {
581 xfs_trans_cancel(tp, 0);
582 return XFS_ERROR(error);
583 }
584 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000585 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Eric Sandeen9d87c312009-01-14 23:22:07 -0600587 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
David Chinnere4143a12007-11-23 16:29:11 +1100590 * it is possible that the extents have changed since
591 * we did the read call as we dropped the ilock for a
592 * while. We have to be careful about truncates or hole
593 * punchs here - we are not allowed to allocate
594 * non-delalloc blocks here.
595 *
596 * The only protection against truncation is the pages
597 * for the range we are being asked to convert are
598 * locked and hence a truncate will block on them
599 * first.
600 *
601 * As a result, if we go beyond the range we really
602 * need and hit an delalloc extent boundary followed by
603 * a hole while we have excess blocks in the map, we
604 * will fill the hole incorrectly and overrun the
605 * transaction reservation.
606 *
607 * Using a single map prevents this as we are forced to
608 * check each map we look for overlap with the desired
609 * range and abort as soon as we find it. Also, given
610 * that we only return a single map, having one beyond
611 * what we can return is probably a bit silly.
612 *
613 * We also need to check that we don't go beyond EOF;
614 * this is a truncate optimisation as a truncate sets
615 * the new file size before block on the pages we
616 * currently have locked under writeback. Because they
617 * are about to be tossed, we don't need to write them
618 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
David Chinnere4143a12007-11-23 16:29:11 +1100620 nimaps = 1;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000621 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
David Chinner7c9ef852008-04-10 12:21:59 +1000622 error = xfs_bmap_last_offset(NULL, ip, &last_block,
623 XFS_DATA_FORK);
624 if (error)
625 goto trans_cancel;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
628 if ((map_start_fsb + count_fsb) > last_block) {
629 count_fsb = last_block - map_start_fsb;
630 if (count_fsb == 0) {
631 error = EAGAIN;
632 goto trans_cancel;
633 }
634 }
635
Christoph Hellwig30704512010-06-24 11:42:19 +1000636 /*
637 * Go get the actual blocks.
638 *
639 * From this point onwards we overwrite the imap
640 * pointer that the caller gave to us.
641 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000642 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 XFS_BMAPI_WRITE, &first_block, 1,
Christoph Hellwig30704512010-06-24 11:42:19 +1000644 imap, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (error)
646 goto trans_cancel;
647
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100648 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (error)
650 goto trans_cancel;
651
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000652 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (error)
654 goto error0;
655
656 xfs_iunlock(ip, XFS_ILOCK_EXCL);
657 }
658
659 /*
660 * See if we were able to allocate an extent that
661 * covers at least part of the callers request
662 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000663 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
664 return xfs_cmn_err_fsblock_zero(ip, imap);
David Chinner86c4d622008-04-29 12:53:21 +1000665
Christoph Hellwig30704512010-06-24 11:42:19 +1000666 if ((offset_fsb >= imap->br_startoff) &&
667 (offset_fsb < (imap->br_startoff +
668 imap->br_blockcount))) {
David Chinnere4143a12007-11-23 16:29:11 +1100669 XFS_STATS_INC(xs_xstrat_quick);
670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
David Chinnere4143a12007-11-23 16:29:11 +1100673 /*
674 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 * file, just surrounding data, try again.
676 */
Christoph Hellwig30704512010-06-24 11:42:19 +1000677 count_fsb -= imap->br_blockcount;
678 map_start_fsb = imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681trans_cancel:
682 xfs_bmap_cancel(&free_list);
683 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
684error0:
685 xfs_iunlock(ip, XFS_ILOCK_EXCL);
686 return XFS_ERROR(error);
687}
688
689int
690xfs_iomap_write_unwritten(
691 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700692 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 size_t count)
694{
695 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 xfs_fileoff_t offset_fsb;
697 xfs_filblks_t count_fsb;
698 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100699 xfs_fsblock_t firstfsb;
700 int nimaps;
701 xfs_trans_t *tp;
702 xfs_bmbt_irec_t imap;
703 xfs_bmap_free_t free_list;
704 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int committed;
706 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000708 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 offset_fsb = XFS_B_TO_FSBT(mp, offset);
711 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
712 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
713
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000714 /*
715 * Reserve enough blocks in this transaction for two complete extent
716 * btree splits. We may be converting the middle part of an unwritten
717 * extent and in this case we will insert two new extents in the btree
718 * each of which could cause a full split.
719 *
720 * This reservation amount will be used in the first call to
721 * xfs_bmbt_split() to select an AG with enough space to satisfy the
722 * rest of the operation.
723 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100724 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Nathan Scottdd9f4382006-01-11 15:28:28 +1100726 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
728 * set up a transaction to convert the range of extents
729 * from unwritten to real. Do allocations in a loop until
730 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000731 *
732 * Note that we open code the transaction allocation here
733 * to pass KM_NOFS--we can't risk to recursing back into
734 * the filesystem here as we might be asked to write out
735 * the same inode that we complete here and might deadlock
736 * on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000738 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
739 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
David Chinner84e1e992007-06-18 16:50:27 +1000740 tp->t_flags |= XFS_TRANS_RESERVE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100741 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 XFS_WRITE_LOG_RES(mp), 0,
743 XFS_TRANS_PERM_LOG_RES,
744 XFS_WRITE_LOG_COUNT);
745 if (error) {
746 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000747 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig898621d2010-06-24 11:36:58 +1000751 xfs_trans_ijoin(tp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /*
754 * Modify the unwritten extent state of the buffer.
755 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600756 xfs_bmap_init(&free_list, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000758 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100759 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000760 1, &imap, &nimaps, &free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (error)
762 goto error_on_bmapi_transaction;
763
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100764 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (error)
766 goto error_on_bmapi_transaction;
767
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000768 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 xfs_iunlock(ip, XFS_ILOCK_EXCL);
770 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000771 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100772
David Chinner86c4d622008-04-29 12:53:21 +1000773 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000774 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if ((numblks_fsb = imap.br_blockcount) == 0) {
777 /*
778 * The numblks_fsb value should always get
779 * smaller, otherwise the loop is stuck.
780 */
781 ASSERT(imap.br_blockcount);
782 break;
783 }
784 offset_fsb += numblks_fsb;
785 count_fsb -= numblks_fsb;
786 } while (count_fsb > 0);
787
788 return 0;
789
790error_on_bmapi_transaction:
791 xfs_bmap_cancel(&free_list);
792 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
793 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return XFS_ERROR(error);
795}