blob: ef14943829dabc38aa0f4f7d667ce0de6f9f2d05 [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
Christoph Hellwigb4ed4622010-04-28 12:29:01 +000058STATIC int xfs_iomap_write_direct(struct xfs_inode *, xfs_off_t, size_t,
59 int, struct xfs_bmbt_irec *, int *);
60STATIC int xfs_iomap_write_delay(struct xfs_inode *, xfs_off_t, size_t, int,
61 struct xfs_bmbt_irec *, int *);
62STATIC int xfs_iomap_write_allocate(struct xfs_inode *, xfs_off_t, size_t,
63 struct xfs_bmbt_irec *, int *);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065int
66xfs_iomap(
Christoph Hellwig207d0412010-04-28 12:28:56 +000067 struct xfs_inode *ip,
68 xfs_off_t offset,
69 ssize_t count,
70 int flags,
71 struct xfs_bmbt_irec *imap,
72 int *nimaps,
73 int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Christoph Hellwig207d0412010-04-28 12:28:56 +000075 struct xfs_mount *mp = ip->i_mount;
76 xfs_fileoff_t offset_fsb, end_fsb;
77 int error = 0;
78 int lockmode = 0;
79 int bmapi_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100081 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
Christoph Hellwig207d0412010-04-28 12:28:56 +000082
83 *new = 0;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (XFS_FORCED_SHUTDOWN(mp))
86 return XFS_ERROR(EIO);
87
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000088 trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
89
Christoph Hellwig76428612007-09-14 15:23:31 +100090 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 case BMAPI_READ:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100092 lockmode = xfs_ilock_map_shared(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 break;
95 case BMAPI_WRITE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +100096 lockmode = XFS_ILOCK_EXCL;
Nathan Scottc31e8872005-09-05 10:06:55 +100097 if (flags & BMAPI_IGNSTATE)
98 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100099 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 break;
101 case BMAPI_ALLOCATE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000102 lockmode = XFS_ILOCK_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 bmapi_flags = XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* Attempt non-blocking lock */
106 if (flags & BMAPI_TRYLOCK) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000107 if (!xfs_ilock_nowait(ip, lockmode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return XFS_ERROR(EAGAIN);
109 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000110 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 default:
114 BUG();
115 }
116
117 ASSERT(offset <= mp->m_maxioffset);
118 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
119 count = mp->m_maxioffset - offset;
120 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
121 offset_fsb = XFS_B_TO_FSBT(mp, offset);
122
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000123 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 (xfs_filblks_t)(end_fsb - offset_fsb),
Christoph Hellwig207d0412010-04-28 12:28:56 +0000125 bmapi_flags, NULL, 0, imap,
126 nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (error)
129 goto out;
130
Christoph Hellwig76428612007-09-14 15:23:31 +1000131 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 case BMAPI_WRITE:
133 /* If we found an extent, return it */
Christoph Hellwig207d0412010-04-28 12:28:56 +0000134 if (*nimaps &&
135 (imap->br_startblock != HOLESTARTBLOCK) &&
136 (imap->br_startblock != DELAYSTARTBLOCK)) {
137 trace_xfs_iomap_found(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 break;
139 }
140
141 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000142 error = xfs_iomap_write_direct(ip, offset, count, flags,
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000143 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000145 error = xfs_iomap_write_delay(ip, offset, count, flags,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000146 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 if (!error) {
Christoph Hellwig207d0412010-04-28 12:28:56 +0000149 trace_xfs_iomap_alloc(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Christoph Hellwig207d0412010-04-28 12:28:56 +0000151 *new = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 break;
153 case BMAPI_ALLOCATE:
154 /* If we found an extent, return it */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000155 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 lockmode = 0;
157
Christoph Hellwig207d0412010-04-28 12:28:56 +0000158 if (*nimaps && !isnullstartblock(imap->br_startblock)) {
159 trace_xfs_iomap_found(ip, offset, count, flags, imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161 }
162
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000163 error = xfs_iomap_write_allocate(ip, offset, count,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000164 imap, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167
Christoph Hellwig207d0412010-04-28 12:28:56 +0000168 ASSERT(*nimaps <= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170out:
171 if (lockmode)
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000172 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return XFS_ERROR(error);
174}
175
176STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100177xfs_iomap_eof_align_last_fsb(
178 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000179 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100180 xfs_extlen_t extsize,
181 xfs_fileoff_t *last_fsb)
182{
183 xfs_fileoff_t new_last_fsb = 0;
184 xfs_extlen_t align;
185 int eof, error;
186
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100187 if (XFS_IS_REALTIME_INODE(ip))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100188 ;
189 /*
190 * If mounted with the "-o swalloc" option, roundup the allocation
191 * request to a stripe width boundary if the file size is >=
192 * stripe width and we are allocating past the allocation eof.
193 */
194 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100195 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100196 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
197 /*
198 * Roundup the allocation request to a stripe unit (m_dalign) boundary
199 * if the file size is >= stripe unit size, and we are allocating past
200 * the allocation eof.
201 */
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100202 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100203 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
204
205 /*
206 * Always round up the allocation request to an extent boundary
207 * (when file on a real-time subvolume or has di_extsize hint).
208 */
209 if (extsize) {
210 if (new_last_fsb)
211 align = roundup_64(new_last_fsb, extsize);
212 else
213 align = extsize;
214 new_last_fsb = roundup_64(*last_fsb, align);
215 }
216
217 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000218 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100219 if (error)
220 return error;
221 if (eof)
222 *last_fsb = new_last_fsb;
223 }
224 return 0;
225}
226
227STATIC int
Nathan Scott572d95f2006-09-28 11:03:20 +1000228xfs_cmn_err_fsblock_zero(
229 xfs_inode_t *ip,
230 xfs_bmbt_irec_t *imap)
231{
232 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
233 "Access to block zero in inode %llu "
234 "start_block: %llx start_off: %llx "
235 "blkcnt: %llx extent-state: %x\n",
236 (unsigned long long)ip->i_ino,
237 (unsigned long long)imap->br_startblock,
238 (unsigned long long)imap->br_startoff,
239 (unsigned long long)imap->br_blockcount,
240 imap->br_state);
241 return EFSCORRUPTED;
242}
243
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000244STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245xfs_iomap_write_direct(
246 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700247 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 size_t count,
249 int flags,
250 xfs_bmbt_irec_t *ret_imap,
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000251 int *nmaps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 xfs_fileoff_t offset_fsb;
255 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100256 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100258 xfs_extlen_t extsz, temp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100259 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000261 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int rt;
263 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100264 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100266 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100268 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /*
271 * Make sure that the dquots are there. This doesn't hold
272 * the ilock across a disk read.
273 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200274 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (error)
276 return XFS_ERROR(error);
277
Nathan Scottdd9f4382006-01-11 15:28:28 +1100278 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000279 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David Chinner957d0eb2007-06-18 16:50:37 +1000281 offset_fsb = XFS_B_TO_FSBT(mp, offset);
282 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100283 if ((offset + count) > ip->i_size) {
284 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100285 if (error)
286 goto error_out;
287 } else {
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000288 if (*nmaps && (ret_imap->br_startblock == HOLESTARTBLOCK))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100289 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
290 ret_imap->br_blockcount +
291 ret_imap->br_startoff);
292 }
293 count_fsb = last_fsb - offset_fsb;
294 ASSERT(count_fsb > 0);
295
296 resaligned = count_fsb;
297 if (unlikely(extsz)) {
298 if ((temp = do_mod(offset_fsb, extsz)))
299 resaligned += temp;
300 if ((temp = do_mod(resaligned, extsz)))
301 resaligned += extsz - temp;
302 }
303
304 if (unlikely(rt)) {
305 resrtextents = qblocks = resaligned;
306 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000307 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
308 quota_flag = XFS_QMOPT_RES_RTBLKS;
309 } else {
310 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100311 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000312 quota_flag = XFS_QMOPT_RES_REGBLKS;
313 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000316 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
318 xfs_iunlock(ip, XFS_ILOCK_EXCL);
319 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000321 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 XFS_TRANS_PERM_LOG_RES,
323 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000325 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000331 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Christoph Hellwig7d095252009-06-08 15:33:32 +0200333 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100334 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
338 xfs_trans_ihold(tp, ip);
339
Nathan Scottdd9f4382006-01-11 15:28:28 +1100340 bmapi_flag = XFS_BMAPI_WRITE;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000341 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 bmapi_flag |= XFS_BMAPI_PREALLOC;
343
344 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100345 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600347 xfs_bmap_init(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000348 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000349 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000350 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000351 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000355 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100357 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000358 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000360 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Nathan Scott06d10dd2005-06-21 15:48:47 +1000364 /*
365 * Copy any maps to caller's array and return any error.
366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000368 error = ENOSPC;
369 goto error_out;
370 }
371
David Chinner86c4d622008-04-29 12:53:21 +1000372 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000373 error = xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto error_out;
375 }
376
Eric Sandeen0116d932005-11-02 15:00:01 +1100377 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
380
Nathan Scott06d10dd2005-06-21 15:48:47 +1000381error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200383 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Nathan Scott06d10dd2005-06-21 15:48:47 +1000385error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
387 *nmaps = 0; /* nothing set-up here */
388
389error_out:
390 return XFS_ERROR(error);
391}
392
Nathan Scottdd9f4382006-01-11 15:28:28 +1100393/*
Dave Chinner8de2bf92009-04-06 18:49:12 +0200394 * If the caller is doing a write at the end of the file, then extend the
395 * allocation out to the file system's write iosize. We clean up any extra
396 * space left over when the file is closed in xfs_inactive().
Nathan Scottdd9f4382006-01-11 15:28:28 +1100397 */
398STATIC int
399xfs_iomap_eof_want_preallocate(
400 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000401 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100402 xfs_off_t offset,
403 size_t count,
404 int ioflag,
405 xfs_bmbt_irec_t *imap,
406 int nimaps,
407 int *prealloc)
408{
409 xfs_fileoff_t start_fsb;
410 xfs_filblks_t count_fsb;
411 xfs_fsblock_t firstblock;
412 int n, error, imaps;
413
414 *prealloc = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200415 if ((offset + count) <= ip->i_size)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100416 return 0;
417
418 /*
419 * If there are any real blocks past eof, then don't
420 * do any speculative allocation.
421 */
422 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
423 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
424 while (count_fsb > 0) {
425 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100426 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000427 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000428 &firstblock, 0, imap, &imaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100429 if (error)
430 return error;
431 for (n = 0; n < imaps; n++) {
432 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
433 (imap[n].br_startblock != DELAYSTARTBLOCK))
434 return 0;
435 start_fsb += imap[n].br_blockcount;
436 count_fsb -= imap[n].br_blockcount;
437 }
438 }
439 *prealloc = 1;
440 return 0;
441}
442
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000443STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444xfs_iomap_write_delay(
445 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700446 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 size_t count,
448 int ioflag,
449 xfs_bmbt_irec_t *ret_imap,
450 int *nmaps)
451{
452 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 xfs_fileoff_t offset_fsb;
454 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100455 xfs_off_t aligned_offset;
456 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100458 xfs_extlen_t extsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Dave Chinner8de2bf92009-04-06 18:49:12 +0200461 int prealloc, flushed = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100462 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000464 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /*
467 * Make sure that the dquots are there. This doesn't hold
468 * the ilock across a disk read.
469 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200470 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (error)
472 return XFS_ERROR(error);
473
David Chinner957d0eb2007-06-18 16:50:37 +1000474 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100475 offset_fsb = XFS_B_TO_FSBT(mp, offset);
476
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100477 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100478 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
479 if (error)
480 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Dave Chinner8de2bf92009-04-06 18:49:12 +0200482retry:
Nathan Scottdd9f4382006-01-11 15:28:28 +1100483 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
485 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100486 last_fsb = ioalign + mp->m_writeio_blocks;
487 } else {
488 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100490
491 if (prealloc || extsz) {
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100492 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100493 if (error)
494 return error;
495 }
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 nimaps = XFS_WRITE_IMAPS;
498 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000499 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 (xfs_filblks_t)(last_fsb - offset_fsb),
501 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
502 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000503 &nimaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100504 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Dave Chinner8de2bf92009-04-06 18:49:12 +0200509 * then we must have run out of space - flush all other inodes with
510 * delalloc blocks and retry without EOF preallocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
512 if (nimaps == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000513 trace_xfs_delalloc_enospc(ip, offset, count);
Dave Chinner8de2bf92009-04-06 18:49:12 +0200514 if (flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return XFS_ERROR(ENOSPC);
516
Dave Chinner8de2bf92009-04-06 18:49:12 +0200517 xfs_iunlock(ip, XFS_ILOCK_EXCL);
518 xfs_flush_inodes(ip);
519 xfs_ilock(ip, XFS_ILOCK_EXCL);
520
521 flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 error = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200523 prealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto retry;
525 }
526
David Chinner86c4d622008-04-29 12:53:21 +1000527 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000528 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100529
530 *ret_imap = imap[0];
531 *nmaps = 1;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
534}
535
536/*
537 * Pass in a delayed allocate extent, convert it to real extents;
538 * return to the caller the extent we create which maps on top of
539 * the originating callers request.
540 *
541 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100542 *
543 * We no longer bother to look at the incoming map - all we have to
544 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Christoph Hellwigb4ed4622010-04-28 12:29:01 +0000546STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547xfs_iomap_write_allocate(
548 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700549 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700550 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 xfs_bmbt_irec_t *map,
552 int *retmap)
553{
554 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 xfs_fileoff_t offset_fsb, last_block;
556 xfs_fileoff_t end_fsb, map_start_fsb;
557 xfs_fsblock_t first_block;
558 xfs_bmap_free_t free_list;
559 xfs_filblks_t count_fsb;
David Chinnere4143a12007-11-23 16:29:11 +1100560 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xfs_trans_t *tp;
David Chinnere4143a12007-11-23 16:29:11 +1100562 int nimaps, committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int error = 0;
564 int nres;
565
566 *retmap = 0;
567
568 /*
569 * Make sure that the dquots are there.
570 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200571 error = xfs_qm_dqattach(ip, 0);
572 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return XFS_ERROR(error);
574
Nathan Scott24e17b52005-05-05 13:33:20 -0700575 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700577 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
580
581 while (count_fsb != 0) {
582 /*
583 * Set up a transaction with which to allocate the
584 * backing store for the file. Do allocations in a
585 * loop until we get some space in the range we are
586 * interested in. The other space that might be allocated
587 * is in the delayed allocation extent on which we sit
588 * but before our buffer starts.
589 */
590
591 nimaps = 0;
592 while (nimaps == 0) {
593 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000594 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
596 error = xfs_trans_reserve(tp, nres,
597 XFS_WRITE_LOG_RES(mp),
598 0, XFS_TRANS_PERM_LOG_RES,
599 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (error) {
601 xfs_trans_cancel(tp, 0);
602 return XFS_ERROR(error);
603 }
604 xfs_ilock(ip, XFS_ILOCK_EXCL);
605 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
606 xfs_trans_ihold(tp, ip);
607
Eric Sandeen9d87c312009-01-14 23:22:07 -0600608 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
David Chinnere4143a12007-11-23 16:29:11 +1100611 * it is possible that the extents have changed since
612 * we did the read call as we dropped the ilock for a
613 * while. We have to be careful about truncates or hole
614 * punchs here - we are not allowed to allocate
615 * non-delalloc blocks here.
616 *
617 * The only protection against truncation is the pages
618 * for the range we are being asked to convert are
619 * locked and hence a truncate will block on them
620 * first.
621 *
622 * As a result, if we go beyond the range we really
623 * need and hit an delalloc extent boundary followed by
624 * a hole while we have excess blocks in the map, we
625 * will fill the hole incorrectly and overrun the
626 * transaction reservation.
627 *
628 * Using a single map prevents this as we are forced to
629 * check each map we look for overlap with the desired
630 * range and abort as soon as we find it. Also, given
631 * that we only return a single map, having one beyond
632 * what we can return is probably a bit silly.
633 *
634 * We also need to check that we don't go beyond EOF;
635 * this is a truncate optimisation as a truncate sets
636 * the new file size before block on the pages we
637 * currently have locked under writeback. Because they
638 * are about to be tossed, we don't need to write them
639 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
David Chinnere4143a12007-11-23 16:29:11 +1100641 nimaps = 1;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000642 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
David Chinner7c9ef852008-04-10 12:21:59 +1000643 error = xfs_bmap_last_offset(NULL, ip, &last_block,
644 XFS_DATA_FORK);
645 if (error)
646 goto trans_cancel;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
649 if ((map_start_fsb + count_fsb) > last_block) {
650 count_fsb = last_block - map_start_fsb;
651 if (count_fsb == 0) {
652 error = EAGAIN;
653 goto trans_cancel;
654 }
655 }
656
657 /* Go get the actual blocks */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000658 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 XFS_BMAPI_WRITE, &first_block, 1,
David Chinnere4143a12007-11-23 16:29:11 +1100660 &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (error)
662 goto trans_cancel;
663
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100664 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (error)
666 goto trans_cancel;
667
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000668 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (error)
670 goto error0;
671
672 xfs_iunlock(ip, XFS_ILOCK_EXCL);
673 }
674
675 /*
676 * See if we were able to allocate an extent that
677 * covers at least part of the callers request
678 */
David Chinner86c4d622008-04-29 12:53:21 +1000679 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
David Chinnere4143a12007-11-23 16:29:11 +1100680 return xfs_cmn_err_fsblock_zero(ip, &imap);
David Chinner86c4d622008-04-29 12:53:21 +1000681
David Chinnere4143a12007-11-23 16:29:11 +1100682 if ((offset_fsb >= imap.br_startoff) &&
683 (offset_fsb < (imap.br_startoff +
684 imap.br_blockcount))) {
685 *map = imap;
686 *retmap = 1;
687 XFS_STATS_INC(xs_xstrat_quick);
688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
David Chinnere4143a12007-11-23 16:29:11 +1100691 /*
692 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * file, just surrounding data, try again.
694 */
David Chinnere4143a12007-11-23 16:29:11 +1100695 count_fsb -= imap.br_blockcount;
696 map_start_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699trans_cancel:
700 xfs_bmap_cancel(&free_list);
701 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
702error0:
703 xfs_iunlock(ip, XFS_ILOCK_EXCL);
704 return XFS_ERROR(error);
705}
706
707int
708xfs_iomap_write_unwritten(
709 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700710 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 size_t count)
712{
713 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 xfs_fileoff_t offset_fsb;
715 xfs_filblks_t count_fsb;
716 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100717 xfs_fsblock_t firstfsb;
718 int nimaps;
719 xfs_trans_t *tp;
720 xfs_bmbt_irec_t imap;
721 xfs_bmap_free_t free_list;
722 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int committed;
724 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000726 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 offset_fsb = XFS_B_TO_FSBT(mp, offset);
729 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
730 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
731
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000732 /*
733 * Reserve enough blocks in this transaction for two complete extent
734 * btree splits. We may be converting the middle part of an unwritten
735 * extent and in this case we will insert two new extents in the btree
736 * each of which could cause a full split.
737 *
738 * This reservation amount will be used in the first call to
739 * xfs_bmbt_split() to select an AG with enough space to satisfy the
740 * rest of the operation.
741 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100742 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Nathan Scottdd9f4382006-01-11 15:28:28 +1100744 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * set up a transaction to convert the range of extents
747 * from unwritten to real. Do allocations in a loop until
748 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000749 *
750 * Note that we open code the transaction allocation here
751 * to pass KM_NOFS--we can't risk to recursing back into
752 * the filesystem here as we might be asked to write out
753 * the same inode that we complete here and might deadlock
754 * on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 */
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000756 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
757 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
David Chinner84e1e992007-06-18 16:50:27 +1000758 tp->t_flags |= XFS_TRANS_RESERVE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100759 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 XFS_WRITE_LOG_RES(mp), 0,
761 XFS_TRANS_PERM_LOG_RES,
762 XFS_WRITE_LOG_COUNT);
763 if (error) {
764 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000765 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768 xfs_ilock(ip, XFS_ILOCK_EXCL);
769 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
770 xfs_trans_ihold(tp, ip);
771
772 /*
773 * Modify the unwritten extent state of the buffer.
774 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600775 xfs_bmap_init(&free_list, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000777 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100778 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000779 1, &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (error)
781 goto error_on_bmapi_transaction;
782
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100783 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (error)
785 goto error_on_bmapi_transaction;
786
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000787 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 xfs_iunlock(ip, XFS_ILOCK_EXCL);
789 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000790 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100791
David Chinner86c4d622008-04-29 12:53:21 +1000792 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000793 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if ((numblks_fsb = imap.br_blockcount) == 0) {
796 /*
797 * The numblks_fsb value should always get
798 * smaller, otherwise the loop is stuck.
799 */
800 ASSERT(imap.br_blockcount);
801 break;
802 }
803 offset_fsb += numblks_fsb;
804 count_fsb -= numblks_fsb;
805 } while (count_fsb > 0);
806
807 return 0;
808
809error_on_bmapi_transaction:
810 xfs_bmap_cancel(&free_list);
811 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
812 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return XFS_ERROR(error);
814}