blob: 0b65039951a0cb926869eb334d57afea920fc43e [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
58STATIC int
59xfs_imap_to_bmap(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100060 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 xfs_off_t offset,
62 xfs_bmbt_irec_t *imap,
63 xfs_iomap_t *iomapp,
64 int imaps, /* Number of imap entries */
65 int iomaps, /* Number of iomap entries */
66 int flags)
67{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100068 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int pbm;
70 xfs_fsblock_t start_block;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
74 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
75 iomapp->iomap_delta = offset - iomapp->iomap_offset;
76 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
77 iomapp->iomap_flags = flags;
78
Eric Sandeen71ddabb2007-11-23 16:29:42 +110079 if (XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 iomapp->iomap_flags |= IOMAP_REALTIME;
81 iomapp->iomap_target = mp->m_rtdev_targp;
82 } else {
83 iomapp->iomap_target = mp->m_ddev_targp;
84 }
85 start_block = imap->br_startblock;
86 if (start_block == HOLESTARTBLOCK) {
87 iomapp->iomap_bn = IOMAP_DADDR_NULL;
88 iomapp->iomap_flags |= IOMAP_HOLE;
89 } else if (start_block == DELAYSTARTBLOCK) {
90 iomapp->iomap_bn = IOMAP_DADDR_NULL;
91 iomapp->iomap_flags |= IOMAP_DELAY;
92 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -060093 iomapp->iomap_bn = xfs_fsb_to_db(ip, start_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (ISUNWRITTEN(imap))
95 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
96 }
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
99 }
100 return pbm; /* Return the number filled */
101}
102
103int
104xfs_iomap(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000105 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 xfs_off_t offset,
107 ssize_t count,
108 int flags,
109 xfs_iomap_t *iomapp,
110 int *niomaps)
111{
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000112 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 xfs_fileoff_t offset_fsb, end_fsb;
114 int error = 0;
115 int lockmode = 0;
116 xfs_bmbt_irec_t imap;
117 int nimaps = 1;
118 int bmapi_flags = 0;
119 int iomap_flags = 0;
120
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000121 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (XFS_FORCED_SHUTDOWN(mp))
124 return XFS_ERROR(EIO);
125
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000126 trace_xfs_iomap_enter(ip, offset, count, flags, NULL);
127
Christoph Hellwig76428612007-09-14 15:23:31 +1000128 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 case BMAPI_READ:
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000130 lockmode = xfs_ilock_map_shared(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 bmapi_flags = XFS_BMAPI_ENTIRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 break;
133 case BMAPI_WRITE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000134 lockmode = XFS_ILOCK_EXCL;
Nathan Scottc31e8872005-09-05 10:06:55 +1000135 if (flags & BMAPI_IGNSTATE)
136 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000137 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 break;
139 case BMAPI_ALLOCATE:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000140 lockmode = XFS_ILOCK_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 bmapi_flags = XFS_BMAPI_ENTIRE;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* Attempt non-blocking lock */
144 if (flags & BMAPI_TRYLOCK) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000145 if (!xfs_ilock_nowait(ip, lockmode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return XFS_ERROR(EAGAIN);
147 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000148 xfs_ilock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 default:
152 BUG();
153 }
154
155 ASSERT(offset <= mp->m_maxioffset);
156 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
157 count = mp->m_maxioffset - offset;
158 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
159 offset_fsb = XFS_B_TO_FSBT(mp, offset);
160
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000161 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 (xfs_filblks_t)(end_fsb - offset_fsb),
163 bmapi_flags, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000164 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (error)
167 goto out;
168
Christoph Hellwig76428612007-09-14 15:23:31 +1000169 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 case BMAPI_WRITE:
171 /* If we found an extent, return it */
Russell Cattelan68d14982005-05-06 06:42:22 -0700172 if (nimaps &&
Nathan Scottdd9f4382006-01-11 15:28:28 +1100173 (imap.br_startblock != HOLESTARTBLOCK) &&
Russell Cattelan68d14982005-05-06 06:42:22 -0700174 (imap.br_startblock != DELAYSTARTBLOCK)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000175 trace_xfs_iomap_found(ip, offset, count, flags, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 }
178
179 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000180 error = xfs_iomap_write_direct(ip, offset, count, flags,
181 &imap, &nimaps, nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 } else {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000183 error = xfs_iomap_write_delay(ip, offset, count, flags,
184 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 if (!error) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000187 trace_xfs_iomap_alloc(ip, offset, count, flags, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 iomap_flags = IOMAP_NEW;
190 break;
191 case BMAPI_ALLOCATE:
192 /* If we found an extent, return it */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000193 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 lockmode = 0;
195
Eric Sandeen9d87c312009-01-14 23:22:07 -0600196 if (nimaps && !isnullstartblock(imap.br_startblock)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000197 trace_xfs_iomap_found(ip, offset, count, flags, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 }
200
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000201 error = xfs_iomap_write_allocate(ip, offset, count,
Nathan Scott24e17b52005-05-05 13:33:20 -0700202 &imap, &nimaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 if (nimaps) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000207 *niomaps = xfs_imap_to_bmap(ip, offset, &imap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 iomapp, nimaps, *niomaps, iomap_flags);
209 } else if (niomaps) {
210 *niomaps = 0;
211 }
212
213out:
214 if (lockmode)
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000215 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return XFS_ERROR(error);
217}
218
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220STATIC int
Nathan Scottdd9f4382006-01-11 15:28:28 +1100221xfs_iomap_eof_align_last_fsb(
222 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000223 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100224 xfs_extlen_t extsize,
225 xfs_fileoff_t *last_fsb)
226{
227 xfs_fileoff_t new_last_fsb = 0;
228 xfs_extlen_t align;
229 int eof, error;
230
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100231 if (XFS_IS_REALTIME_INODE(ip))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100232 ;
233 /*
234 * If mounted with the "-o swalloc" option, roundup the allocation
235 * request to a stripe width boundary if the file size is >=
236 * stripe width and we are allocating past the allocation eof.
237 */
238 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100239 (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_swidth)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100240 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
241 /*
242 * Roundup the allocation request to a stripe unit (m_dalign) boundary
243 * if the file size is >= stripe unit size, and we are allocating past
244 * the allocation eof.
245 */
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100246 else if (mp->m_dalign && (ip->i_size >= XFS_FSB_TO_B(mp, mp->m_dalign)))
Nathan Scottdd9f4382006-01-11 15:28:28 +1100247 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
248
249 /*
250 * Always round up the allocation request to an extent boundary
251 * (when file on a real-time subvolume or has di_extsize hint).
252 */
253 if (extsize) {
254 if (new_last_fsb)
255 align = roundup_64(new_last_fsb, extsize);
256 else
257 align = extsize;
258 new_last_fsb = roundup_64(*last_fsb, align);
259 }
260
261 if (new_last_fsb) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000262 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100263 if (error)
264 return error;
265 if (eof)
266 *last_fsb = new_last_fsb;
267 }
268 return 0;
269}
270
271STATIC int
Nathan Scott572d95f2006-09-28 11:03:20 +1000272xfs_cmn_err_fsblock_zero(
273 xfs_inode_t *ip,
274 xfs_bmbt_irec_t *imap)
275{
276 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
277 "Access to block zero in inode %llu "
278 "start_block: %llx start_off: %llx "
279 "blkcnt: %llx extent-state: %x\n",
280 (unsigned long long)ip->i_ino,
281 (unsigned long long)imap->br_startblock,
282 (unsigned long long)imap->br_startoff,
283 (unsigned long long)imap->br_blockcount,
284 imap->br_state);
285 return EFSCORRUPTED;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288int
289xfs_iomap_write_direct(
290 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700291 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 size_t count,
293 int flags,
294 xfs_bmbt_irec_t *ret_imap,
295 int *nmaps,
296 int found)
297{
298 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 xfs_fileoff_t offset_fsb;
300 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100301 xfs_filblks_t count_fsb, resaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_fsblock_t firstfsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100303 xfs_extlen_t extsz, temp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100304 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int bmapi_flag;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000306 int quota_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int rt;
308 xfs_trans_t *tp;
Eric Sandeen0116d932005-11-02 15:00:01 +1100309 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 xfs_bmap_free_t free_list;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100311 uint qblocks, resblks, resrtextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int committed;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100313 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /*
316 * Make sure that the dquots are there. This doesn't hold
317 * the ilock across a disk read.
318 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200319 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (error)
321 return XFS_ERROR(error);
322
Nathan Scottdd9f4382006-01-11 15:28:28 +1100323 rt = XFS_IS_REALTIME_INODE(ip);
David Chinner957d0eb2007-06-18 16:50:37 +1000324 extsz = xfs_get_extsz_hint(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David Chinner957d0eb2007-06-18 16:50:37 +1000326 offset_fsb = XFS_B_TO_FSBT(mp, offset);
327 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100328 if ((offset + count) > ip->i_size) {
329 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100330 if (error)
331 goto error_out;
332 } else {
333 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
334 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
335 ret_imap->br_blockcount +
336 ret_imap->br_startoff);
337 }
338 count_fsb = last_fsb - offset_fsb;
339 ASSERT(count_fsb > 0);
340
341 resaligned = count_fsb;
342 if (unlikely(extsz)) {
343 if ((temp = do_mod(offset_fsb, extsz)))
344 resaligned += temp;
345 if ((temp = do_mod(resaligned, extsz)))
346 resaligned += extsz - temp;
347 }
348
349 if (unlikely(rt)) {
350 resrtextents = qblocks = resaligned;
351 resrtextents /= mp->m_sb.sb_rextsize;
David Chinner84e1e992007-06-18 16:50:27 +1000352 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
353 quota_flag = XFS_QMOPT_RES_RTBLKS;
354 } else {
355 resrtextents = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100356 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
David Chinner84e1e992007-06-18 16:50:27 +1000357 quota_flag = XFS_QMOPT_RES_REGBLKS;
358 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000361 * Allocate and setup the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363 xfs_iunlock(ip, XFS_ILOCK_EXCL);
364 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 error = xfs_trans_reserve(tp, resblks,
Nathan Scottd52b44d2005-09-02 16:41:32 +1000366 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 XFS_TRANS_PERM_LOG_RES,
368 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000370 * Check for running out of space, note: need lock to return
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
372 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (error)
Nathan Scott06d10dd2005-06-21 15:48:47 +1000376 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Christoph Hellwig7d095252009-06-08 15:33:32 +0200378 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100379 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
383 xfs_trans_ihold(tp, ip);
384
Nathan Scottdd9f4382006-01-11 15:28:28 +1100385 bmapi_flag = XFS_BMAPI_WRITE;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000386 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 bmapi_flag |= XFS_BMAPI_PREALLOC;
388
389 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +1100390 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600392 xfs_bmap_init(&free_list, &firstfsb);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000393 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000394 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000395 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000396 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
Nathan Scott06d10dd2005-06-21 15:48:47 +1000400 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100402 error = xfs_bmap_finish(&tp, &free_list, &committed);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000403 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto error0;
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000405 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000406 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Nathan Scott06d10dd2005-06-21 15:48:47 +1000409 /*
410 * Copy any maps to caller's array and return any error.
411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (nimaps == 0) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000413 error = ENOSPC;
414 goto error_out;
415 }
416
David Chinner86c4d622008-04-29 12:53:21 +1000417 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip))) {
Nathan Scott572d95f2006-09-28 11:03:20 +1000418 error = xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 goto error_out;
420 }
421
Eric Sandeen0116d932005-11-02 15:00:01 +1100422 *ret_imap = imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425
Nathan Scott06d10dd2005-06-21 15:48:47 +1000426error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 xfs_bmap_cancel(&free_list);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200428 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Nathan Scott06d10dd2005-06-21 15:48:47 +1000430error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
432 *nmaps = 0; /* nothing set-up here */
433
434error_out:
435 return XFS_ERROR(error);
436}
437
Nathan Scottdd9f4382006-01-11 15:28:28 +1100438/*
Dave Chinner8de2bf92009-04-06 18:49:12 +0200439 * If the caller is doing a write at the end of the file, then extend the
440 * allocation out to the file system's write iosize. We clean up any extra
441 * space left over when the file is closed in xfs_inactive().
Nathan Scottdd9f4382006-01-11 15:28:28 +1100442 */
443STATIC int
444xfs_iomap_eof_want_preallocate(
445 xfs_mount_t *mp,
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000446 xfs_inode_t *ip,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100447 xfs_off_t offset,
448 size_t count,
449 int ioflag,
450 xfs_bmbt_irec_t *imap,
451 int nimaps,
452 int *prealloc)
453{
454 xfs_fileoff_t start_fsb;
455 xfs_filblks_t count_fsb;
456 xfs_fsblock_t firstblock;
457 int n, error, imaps;
458
459 *prealloc = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200460 if ((offset + count) <= ip->i_size)
Nathan Scottdd9f4382006-01-11 15:28:28 +1100461 return 0;
462
463 /*
464 * If there are any real blocks past eof, then don't
465 * do any speculative allocation.
466 */
467 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
468 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
469 while (count_fsb > 0) {
470 imaps = nimaps;
Nathan Scott3ddb8fa2006-01-11 15:33:02 +1100471 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000472 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000473 &firstblock, 0, imap, &imaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100474 if (error)
475 return error;
476 for (n = 0; n < imaps; n++) {
477 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
478 (imap[n].br_startblock != DELAYSTARTBLOCK))
479 return 0;
480 start_fsb += imap[n].br_blockcount;
481 count_fsb -= imap[n].br_blockcount;
482 }
483 }
484 *prealloc = 1;
485 return 0;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488int
489xfs_iomap_write_delay(
490 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700491 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 size_t count,
493 int ioflag,
494 xfs_bmbt_irec_t *ret_imap,
495 int *nmaps)
496{
497 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 xfs_fileoff_t offset_fsb;
499 xfs_fileoff_t last_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100500 xfs_off_t aligned_offset;
501 xfs_fileoff_t ioalign;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 xfs_fsblock_t firstblock;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100503 xfs_extlen_t extsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int nimaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
Dave Chinner8de2bf92009-04-06 18:49:12 +0200506 int prealloc, flushed = 0;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100507 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000509 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
512 * Make sure that the dquots are there. This doesn't hold
513 * the ilock across a disk read.
514 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200515 error = xfs_qm_dqattach_locked(ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (error)
517 return XFS_ERROR(error);
518
David Chinner957d0eb2007-06-18 16:50:37 +1000519 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100520 offset_fsb = XFS_B_TO_FSBT(mp, offset);
521
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100522 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100523 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
524 if (error)
525 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dave Chinner8de2bf92009-04-06 18:49:12 +0200527retry:
Nathan Scottdd9f4382006-01-11 15:28:28 +1100528 if (prealloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
530 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100531 last_fsb = ioalign + mp->m_writeio_blocks;
532 } else {
533 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Nathan Scottdd9f4382006-01-11 15:28:28 +1100535
536 if (prealloc || extsz) {
Lachlan McIlroy9f6c92b2008-12-22 17:56:49 +1100537 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100538 if (error)
539 return error;
540 }
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 nimaps = XFS_WRITE_IMAPS;
543 firstblock = NULLFSBLOCK;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000544 error = xfs_bmapi(NULL, ip, offset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 (xfs_filblks_t)(last_fsb - offset_fsb),
546 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
547 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000548 &nimaps, NULL, NULL);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100549 if (error && (error != ENOSPC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /*
553 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
Dave Chinner8de2bf92009-04-06 18:49:12 +0200554 * then we must have run out of space - flush all other inodes with
555 * delalloc blocks and retry without EOF preallocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
557 if (nimaps == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000558 trace_xfs_delalloc_enospc(ip, offset, count);
Dave Chinner8de2bf92009-04-06 18:49:12 +0200559 if (flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return XFS_ERROR(ENOSPC);
561
Dave Chinner8de2bf92009-04-06 18:49:12 +0200562 xfs_iunlock(ip, XFS_ILOCK_EXCL);
563 xfs_flush_inodes(ip);
564 xfs_ilock(ip, XFS_ILOCK_EXCL);
565
566 flushed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 error = 0;
Dave Chinner8de2bf92009-04-06 18:49:12 +0200568 prealloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto retry;
570 }
571
David Chinner86c4d622008-04-29 12:53:21 +1000572 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000573 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100574
575 *ret_imap = imap[0];
576 *nmaps = 1;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579}
580
581/*
582 * Pass in a delayed allocate extent, convert it to real extents;
583 * return to the caller the extent we create which maps on top of
584 * the originating callers request.
585 *
586 * Called without a lock on the inode.
David Chinnere4143a12007-11-23 16:29:11 +1100587 *
588 * We no longer bother to look at the incoming map - all we have to
589 * guarantee is that whatever we allocate fills the required range.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591int
592xfs_iomap_write_allocate(
593 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700594 xfs_off_t offset,
Nathan Scott24e17b52005-05-05 13:33:20 -0700595 size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 xfs_bmbt_irec_t *map,
597 int *retmap)
598{
599 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 xfs_fileoff_t offset_fsb, last_block;
601 xfs_fileoff_t end_fsb, map_start_fsb;
602 xfs_fsblock_t first_block;
603 xfs_bmap_free_t free_list;
604 xfs_filblks_t count_fsb;
David Chinnere4143a12007-11-23 16:29:11 +1100605 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 xfs_trans_t *tp;
David Chinnere4143a12007-11-23 16:29:11 +1100607 int nimaps, committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int error = 0;
609 int nres;
610
611 *retmap = 0;
612
613 /*
614 * Make sure that the dquots are there.
615 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200616 error = xfs_qm_dqattach(ip, 0);
617 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return XFS_ERROR(error);
619
Nathan Scott24e17b52005-05-05 13:33:20 -0700620 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 count_fsb = map->br_blockcount;
Nathan Scott24e17b52005-05-05 13:33:20 -0700622 map_start_fsb = map->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
625
626 while (count_fsb != 0) {
627 /*
628 * Set up a transaction with which to allocate the
629 * backing store for the file. Do allocations in a
630 * loop until we get some space in the range we are
631 * interested in. The other space that might be allocated
632 * is in the delayed allocation extent on which we sit
633 * but before our buffer starts.
634 */
635
636 nimaps = 0;
637 while (nimaps == 0) {
638 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
David Chinner84e1e992007-06-18 16:50:27 +1000639 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
641 error = xfs_trans_reserve(tp, nres,
642 XFS_WRITE_LOG_RES(mp),
643 0, XFS_TRANS_PERM_LOG_RES,
644 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (error) {
646 xfs_trans_cancel(tp, 0);
647 return XFS_ERROR(error);
648 }
649 xfs_ilock(ip, XFS_ILOCK_EXCL);
650 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
651 xfs_trans_ihold(tp, ip);
652
Eric Sandeen9d87c312009-01-14 23:22:07 -0600653 xfs_bmap_init(&free_list, &first_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /*
David Chinnere4143a12007-11-23 16:29:11 +1100656 * it is possible that the extents have changed since
657 * we did the read call as we dropped the ilock for a
658 * while. We have to be careful about truncates or hole
659 * punchs here - we are not allowed to allocate
660 * non-delalloc blocks here.
661 *
662 * The only protection against truncation is the pages
663 * for the range we are being asked to convert are
664 * locked and hence a truncate will block on them
665 * first.
666 *
667 * As a result, if we go beyond the range we really
668 * need and hit an delalloc extent boundary followed by
669 * a hole while we have excess blocks in the map, we
670 * will fill the hole incorrectly and overrun the
671 * transaction reservation.
672 *
673 * Using a single map prevents this as we are forced to
674 * check each map we look for overlap with the desired
675 * range and abort as soon as we find it. Also, given
676 * that we only return a single map, having one beyond
677 * what we can return is probably a bit silly.
678 *
679 * We also need to check that we don't go beyond EOF;
680 * this is a truncate optimisation as a truncate sets
681 * the new file size before block on the pages we
682 * currently have locked under writeback. Because they
683 * are about to be tossed, we don't need to write them
684 * back....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
David Chinnere4143a12007-11-23 16:29:11 +1100686 nimaps = 1;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000687 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
David Chinner7c9ef852008-04-10 12:21:59 +1000688 error = xfs_bmap_last_offset(NULL, ip, &last_block,
689 XFS_DATA_FORK);
690 if (error)
691 goto trans_cancel;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
694 if ((map_start_fsb + count_fsb) > last_block) {
695 count_fsb = last_block - map_start_fsb;
696 if (count_fsb == 0) {
697 error = EAGAIN;
698 goto trans_cancel;
699 }
700 }
701
702 /* Go get the actual blocks */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000703 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 XFS_BMAPI_WRITE, &first_block, 1,
David Chinnere4143a12007-11-23 16:29:11 +1100705 &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (error)
707 goto trans_cancel;
708
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100709 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (error)
711 goto trans_cancel;
712
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000713 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (error)
715 goto error0;
716
717 xfs_iunlock(ip, XFS_ILOCK_EXCL);
718 }
719
720 /*
721 * See if we were able to allocate an extent that
722 * covers at least part of the callers request
723 */
David Chinner86c4d622008-04-29 12:53:21 +1000724 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
David Chinnere4143a12007-11-23 16:29:11 +1100725 return xfs_cmn_err_fsblock_zero(ip, &imap);
David Chinner86c4d622008-04-29 12:53:21 +1000726
David Chinnere4143a12007-11-23 16:29:11 +1100727 if ((offset_fsb >= imap.br_startoff) &&
728 (offset_fsb < (imap.br_startoff +
729 imap.br_blockcount))) {
730 *map = imap;
731 *retmap = 1;
732 XFS_STATS_INC(xs_xstrat_quick);
733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
David Chinnere4143a12007-11-23 16:29:11 +1100736 /*
737 * So far we have not mapped the requested part of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * file, just surrounding data, try again.
739 */
David Chinnere4143a12007-11-23 16:29:11 +1100740 count_fsb -= imap.br_blockcount;
741 map_start_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744trans_cancel:
745 xfs_bmap_cancel(&free_list);
746 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
747error0:
748 xfs_iunlock(ip, XFS_ILOCK_EXCL);
749 return XFS_ERROR(error);
750}
751
752int
753xfs_iomap_write_unwritten(
754 xfs_inode_t *ip,
Nathan Scottf403b7f2005-05-05 13:33:40 -0700755 xfs_off_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 size_t count)
757{
758 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 xfs_fileoff_t offset_fsb;
760 xfs_filblks_t count_fsb;
761 xfs_filblks_t numblks_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100762 xfs_fsblock_t firstfsb;
763 int nimaps;
764 xfs_trans_t *tp;
765 xfs_bmbt_irec_t imap;
766 xfs_bmap_free_t free_list;
767 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int committed;
769 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000771 trace_xfs_unwritten_convert(ip, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 offset_fsb = XFS_B_TO_FSBT(mp, offset);
774 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
775 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
776
Lachlan McIlroy4ddd8bb2008-06-27 13:32:53 +1000777 /*
778 * Reserve enough blocks in this transaction for two complete extent
779 * btree splits. We may be converting the middle part of an unwritten
780 * extent and in this case we will insert two new extents in the btree
781 * each of which could cause a full split.
782 *
783 * This reservation amount will be used in the first call to
784 * xfs_bmbt_split() to select an AG with enough space to satisfy the
785 * rest of the operation.
786 */
Nathan Scottdd9f4382006-01-11 15:28:28 +1100787 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Nathan Scottdd9f4382006-01-11 15:28:28 +1100789 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * set up a transaction to convert the range of extents
792 * from unwritten to real. Do allocations in a loop until
793 * we have covered the range passed in.
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000794 *
795 * Note that we open code the transaction allocation here
796 * to pass KM_NOFS--we can't risk to recursing back into
797 * the filesystem here as we might be asked to write out
798 * the same inode that we complete here and might deadlock
799 * on the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000801 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
802 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
David Chinner84e1e992007-06-18 16:50:27 +1000803 tp->t_flags |= XFS_TRANS_RESERVE;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100804 error = xfs_trans_reserve(tp, resblks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 XFS_WRITE_LOG_RES(mp), 0,
806 XFS_TRANS_PERM_LOG_RES,
807 XFS_WRITE_LOG_COUNT);
808 if (error) {
809 xfs_trans_cancel(tp, 0);
Nathan Scott572d95f2006-09-28 11:03:20 +1000810 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 xfs_ilock(ip, XFS_ILOCK_EXCL);
814 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
815 xfs_trans_ihold(tp, ip);
816
817 /*
818 * Modify the unwritten extent state of the buffer.
819 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600820 xfs_bmap_init(&free_list, &firstfsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000822 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +1100823 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000824 1, &imap, &nimaps, &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (error)
826 goto error_on_bmapi_transaction;
827
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100828 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (error)
830 goto error_on_bmapi_transaction;
831
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000832 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 xfs_iunlock(ip, XFS_ILOCK_EXCL);
834 if (error)
Nathan Scott572d95f2006-09-28 11:03:20 +1000835 return XFS_ERROR(error);
Nathan Scottdd9f4382006-01-11 15:28:28 +1100836
David Chinner86c4d622008-04-29 12:53:21 +1000837 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
Nathan Scott572d95f2006-09-28 11:03:20 +1000838 return xfs_cmn_err_fsblock_zero(ip, &imap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if ((numblks_fsb = imap.br_blockcount) == 0) {
841 /*
842 * The numblks_fsb value should always get
843 * smaller, otherwise the loop is stuck.
844 */
845 ASSERT(imap.br_blockcount);
846 break;
847 }
848 offset_fsb += numblks_fsb;
849 count_fsb -= numblks_fsb;
850 } while (count_fsb > 0);
851
852 return 0;
853
854error_on_bmapi_transaction:
855 xfs_bmap_cancel(&free_list);
856 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
857 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return XFS_ERROR(error);
859}