[XFS] Move platform specific mount option parse out of core XFS code
[deliverable/linux.git] / fs / xfs / xfs_iomap.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
1da177e4 19#include "xfs_fs.h"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
1da177e4 31#include "xfs_bmap_btree.h"
a844f451 32#include "xfs_alloc_btree.h"
1da177e4 33#include "xfs_ialloc_btree.h"
1da177e4 34#include "xfs_dir2_sf.h"
a844f451 35#include "xfs_attr_sf.h"
1da177e4
LT
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
a844f451
NS
38#include "xfs_ialloc.h"
39#include "xfs_btree.h"
1da177e4 40#include "xfs_bmap.h"
1da177e4
LT
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
1da177e4
LT
46#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_trans_space.h"
49#include "xfs_utils.h"
50#include "xfs_iomap.h"
51
52#if defined(XFS_RW_TRACE)
53void
54xfs_iomap_enter_trace(
55 int tag,
541d7d3c 56 xfs_inode_t *ip,
1da177e4
LT
57 xfs_off_t offset,
58 ssize_t count)
59{
1da177e4
LT
60 if (!ip->i_rwtrace)
61 return;
62
63 ktrace_enter(ip->i_rwtrace,
64 (void *)((unsigned long)tag),
65 (void *)ip,
66 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
67 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
68 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
69 (void *)((unsigned long)(offset & 0xffffffff)),
70 (void *)((unsigned long)count),
613d7043
CH
71 (void *)((unsigned long)((ip->i_new_size >> 32) & 0xffffffff)),
72 (void *)((unsigned long)(ip->i_new_size & 0xffffffff)),
f1fdc848 73 (void *)((unsigned long)current_pid()),
1da177e4
LT
74 (void *)NULL,
75 (void *)NULL,
76 (void *)NULL,
77 (void *)NULL,
78 (void *)NULL,
79 (void *)NULL);
80}
81
82void
83xfs_iomap_map_trace(
84 int tag,
541d7d3c 85 xfs_inode_t *ip,
1da177e4
LT
86 xfs_off_t offset,
87 ssize_t count,
88 xfs_iomap_t *iomapp,
89 xfs_bmbt_irec_t *imapp,
90 int flags)
91{
1da177e4
LT
92 if (!ip->i_rwtrace)
93 return;
94
95 ktrace_enter(ip->i_rwtrace,
96 (void *)((unsigned long)tag),
97 (void *)ip,
98 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
99 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
100 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
101 (void *)((unsigned long)(offset & 0xffffffff)),
102 (void *)((unsigned long)count),
103 (void *)((unsigned long)flags),
104 (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)),
106 (void *)((unsigned long)(iomapp->iomap_delta)),
107 (void *)((unsigned long)(iomapp->iomap_bsize)),
108 (void *)((unsigned long)(iomapp->iomap_bn)),
109 (void *)(__psint_t)(imapp->br_startoff),
110 (void *)((unsigned long)(imapp->br_blockcount)),
111 (void *)(__psint_t)(imapp->br_startblock));
112}
113#else
114#define xfs_iomap_enter_trace(tag, io, offset, count)
115#define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags)
116#endif
117
118#define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
119 << mp->m_writeio_log)
120#define XFS_STRAT_WRITE_IMAPS 2
121#define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
122
123STATIC int
124xfs_imap_to_bmap(
541d7d3c 125 xfs_inode_t *ip,
1da177e4
LT
126 xfs_off_t offset,
127 xfs_bmbt_irec_t *imap,
128 xfs_iomap_t *iomapp,
129 int imaps, /* Number of imap entries */
130 int iomaps, /* Number of iomap entries */
131 int flags)
132{
541d7d3c 133 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
134 int pbm;
135 xfs_fsblock_t start_block;
136
1da177e4
LT
137
138 for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) {
139 iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
140 iomapp->iomap_delta = offset - iomapp->iomap_offset;
141 iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
142 iomapp->iomap_flags = flags;
143
541d7d3c 144 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
1da177e4
LT
145 iomapp->iomap_flags |= IOMAP_REALTIME;
146 iomapp->iomap_target = mp->m_rtdev_targp;
147 } else {
148 iomapp->iomap_target = mp->m_ddev_targp;
149 }
150 start_block = imap->br_startblock;
151 if (start_block == HOLESTARTBLOCK) {
152 iomapp->iomap_bn = IOMAP_DADDR_NULL;
153 iomapp->iomap_flags |= IOMAP_HOLE;
154 } else if (start_block == DELAYSTARTBLOCK) {
155 iomapp->iomap_bn = IOMAP_DADDR_NULL;
156 iomapp->iomap_flags |= IOMAP_DELAY;
157 } else {
541d7d3c 158 iomapp->iomap_bn = XFS_FSB_TO_DB(ip, start_block);
1da177e4
LT
159 if (ISUNWRITTEN(imap))
160 iomapp->iomap_flags |= IOMAP_UNWRITTEN;
161 }
162
1da177e4
LT
163 offset += iomapp->iomap_bsize - iomapp->iomap_delta;
164 }
165 return pbm; /* Return the number filled */
166}
167
168int
169xfs_iomap(
541d7d3c 170 xfs_inode_t *ip,
1da177e4
LT
171 xfs_off_t offset,
172 ssize_t count,
173 int flags,
174 xfs_iomap_t *iomapp,
175 int *niomaps)
176{
541d7d3c 177 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
178 xfs_fileoff_t offset_fsb, end_fsb;
179 int error = 0;
180 int lockmode = 0;
181 xfs_bmbt_irec_t imap;
182 int nimaps = 1;
183 int bmapi_flags = 0;
184 int iomap_flags = 0;
185
541d7d3c 186 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
541d7d3c 187
1da177e4
LT
188 if (XFS_FORCED_SHUTDOWN(mp))
189 return XFS_ERROR(EIO);
190
7642861b 191 switch (flags & (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE)) {
1da177e4 192 case BMAPI_READ:
541d7d3c
LM
193 xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, ip, offset, count);
194 lockmode = xfs_ilock_map_shared(ip);
1da177e4 195 bmapi_flags = XFS_BMAPI_ENTIRE;
1da177e4
LT
196 break;
197 case BMAPI_WRITE:
541d7d3c 198 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, ip, offset, count);
1da177e4 199 lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR;
c31e8878
NS
200 if (flags & BMAPI_IGNSTATE)
201 bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE;
541d7d3c 202 xfs_ilock(ip, lockmode);
1da177e4
LT
203 break;
204 case BMAPI_ALLOCATE:
541d7d3c 205 xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, ip, offset, count);
1da177e4
LT
206 lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD;
207 bmapi_flags = XFS_BMAPI_ENTIRE;
541d7d3c 208
1da177e4
LT
209 /* Attempt non-blocking lock */
210 if (flags & BMAPI_TRYLOCK) {
541d7d3c 211 if (!xfs_ilock_nowait(ip, lockmode))
1da177e4
LT
212 return XFS_ERROR(EAGAIN);
213 } else {
541d7d3c 214 xfs_ilock(ip, lockmode);
1da177e4
LT
215 }
216 break;
1da177e4
LT
217 default:
218 BUG();
219 }
220
221 ASSERT(offset <= mp->m_maxioffset);
222 if ((xfs_fsize_t)offset + count > mp->m_maxioffset)
223 count = mp->m_maxioffset - offset;
224 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
225 offset_fsb = XFS_B_TO_FSBT(mp, offset);
226
541d7d3c 227 error = xfs_bmapi(NULL, ip, offset_fsb,
1da177e4
LT
228 (xfs_filblks_t)(end_fsb - offset_fsb),
229 bmapi_flags, NULL, 0, &imap,
3e57ecf6 230 &nimaps, NULL, NULL);
1da177e4
LT
231
232 if (error)
233 goto out;
234
7642861b 235 switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)) {
1da177e4
LT
236 case BMAPI_WRITE:
237 /* If we found an extent, return it */
68d1498c 238 if (nimaps &&
dd9f438e 239 (imap.br_startblock != HOLESTARTBLOCK) &&
68d1498c 240 (imap.br_startblock != DELAYSTARTBLOCK)) {
541d7d3c 241 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
1da177e4
LT
242 offset, count, iomapp, &imap, flags);
243 break;
244 }
245
246 if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) {
541d7d3c
LM
247 error = xfs_iomap_write_direct(ip, offset, count, flags,
248 &imap, &nimaps, nimaps);
1da177e4 249 } else {
541d7d3c
LM
250 error = xfs_iomap_write_delay(ip, offset, count, flags,
251 &imap, &nimaps);
1da177e4
LT
252 }
253 if (!error) {
541d7d3c 254 xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, ip,
1da177e4
LT
255 offset, count, iomapp, &imap, flags);
256 }
257 iomap_flags = IOMAP_NEW;
258 break;
259 case BMAPI_ALLOCATE:
260 /* If we found an extent, return it */
541d7d3c 261 xfs_iunlock(ip, lockmode);
1da177e4
LT
262 lockmode = 0;
263
264 if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) {
541d7d3c 265 xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, ip,
1da177e4
LT
266 offset, count, iomapp, &imap, flags);
267 break;
268 }
269
541d7d3c 270 error = xfs_iomap_write_allocate(ip, offset, count,
24e17b5f 271 &imap, &nimaps);
1da177e4 272 break;
1da177e4
LT
273 }
274
275 if (nimaps) {
541d7d3c 276 *niomaps = xfs_imap_to_bmap(ip, offset, &imap,
1da177e4
LT
277 iomapp, nimaps, *niomaps, iomap_flags);
278 } else if (niomaps) {
279 *niomaps = 0;
280 }
281
282out:
283 if (lockmode)
541d7d3c 284 xfs_iunlock(ip, lockmode);
1da177e4
LT
285 return XFS_ERROR(error);
286}
287
541d7d3c 288
dd9f438e
NS
289STATIC int
290xfs_iomap_eof_align_last_fsb(
291 xfs_mount_t *mp,
541d7d3c 292 xfs_inode_t *ip,
dd9f438e
NS
293 xfs_fsize_t isize,
294 xfs_extlen_t extsize,
295 xfs_fileoff_t *last_fsb)
296{
297 xfs_fileoff_t new_last_fsb = 0;
298 xfs_extlen_t align;
299 int eof, error;
300
541d7d3c 301 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
dd9f438e
NS
302 ;
303 /*
304 * If mounted with the "-o swalloc" option, roundup the allocation
305 * request to a stripe width boundary if the file size is >=
306 * stripe width and we are allocating past the allocation eof.
307 */
308 else if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC) &&
309 (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)))
310 new_last_fsb = roundup_64(*last_fsb, mp->m_swidth);
311 /*
312 * Roundup the allocation request to a stripe unit (m_dalign) boundary
313 * if the file size is >= stripe unit size, and we are allocating past
314 * the allocation eof.
315 */
316 else if (mp->m_dalign && (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)))
317 new_last_fsb = roundup_64(*last_fsb, mp->m_dalign);
318
319 /*
320 * Always round up the allocation request to an extent boundary
321 * (when file on a real-time subvolume or has di_extsize hint).
322 */
323 if (extsize) {
324 if (new_last_fsb)
325 align = roundup_64(new_last_fsb, extsize);
326 else
327 align = extsize;
328 new_last_fsb = roundup_64(*last_fsb, align);
329 }
330
331 if (new_last_fsb) {
541d7d3c 332 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
dd9f438e
NS
333 if (error)
334 return error;
335 if (eof)
336 *last_fsb = new_last_fsb;
337 }
338 return 0;
339}
340
1da177e4
LT
341STATIC int
342xfs_flush_space(
343 xfs_inode_t *ip,
344 int *fsynced,
345 int *ioflags)
346{
347 switch (*fsynced) {
348 case 0:
349 if (ip->i_delayed_blks) {
350 xfs_iunlock(ip, XFS_ILOCK_EXCL);
351 xfs_flush_inode(ip);
352 xfs_ilock(ip, XFS_ILOCK_EXCL);
353 *fsynced = 1;
354 } else {
355 *ioflags |= BMAPI_SYNC;
356 *fsynced = 2;
357 }
358 return 0;
359 case 1:
360 *fsynced = 2;
361 *ioflags |= BMAPI_SYNC;
362 return 0;
363 case 2:
364 xfs_iunlock(ip, XFS_ILOCK_EXCL);
365 xfs_flush_device(ip);
366 xfs_ilock(ip, XFS_ILOCK_EXCL);
367 *fsynced = 3;
368 return 0;
369 }
370 return 1;
371}
372
572d95f4
NS
373STATIC int
374xfs_cmn_err_fsblock_zero(
375 xfs_inode_t *ip,
376 xfs_bmbt_irec_t *imap)
377{
378 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
379 "Access to block zero in inode %llu "
380 "start_block: %llx start_off: %llx "
381 "blkcnt: %llx extent-state: %x\n",
382 (unsigned long long)ip->i_ino,
383 (unsigned long long)imap->br_startblock,
384 (unsigned long long)imap->br_startoff,
385 (unsigned long long)imap->br_blockcount,
386 imap->br_state);
387 return EFSCORRUPTED;
388}
389
1da177e4
LT
390int
391xfs_iomap_write_direct(
392 xfs_inode_t *ip,
f403b7f4 393 xfs_off_t offset,
1da177e4
LT
394 size_t count,
395 int flags,
396 xfs_bmbt_irec_t *ret_imap,
397 int *nmaps,
398 int found)
399{
400 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
401 xfs_fileoff_t offset_fsb;
402 xfs_fileoff_t last_fsb;
dd9f438e 403 xfs_filblks_t count_fsb, resaligned;
1da177e4 404 xfs_fsblock_t firstfsb;
dd9f438e
NS
405 xfs_extlen_t extsz, temp;
406 xfs_fsize_t isize;
0116d935 407 int nimaps;
1da177e4 408 int bmapi_flag;
06d10dd9 409 int quota_flag;
1da177e4
LT
410 int rt;
411 xfs_trans_t *tp;
0116d935 412 xfs_bmbt_irec_t imap;
1da177e4 413 xfs_bmap_free_t free_list;
dd9f438e 414 uint qblocks, resblks, resrtextents;
1da177e4 415 int committed;
dd9f438e 416 int error;
1da177e4
LT
417
418 /*
419 * Make sure that the dquots are there. This doesn't hold
420 * the ilock across a disk read.
421 */
422 error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED);
423 if (error)
424 return XFS_ERROR(error);
425
dd9f438e 426 rt = XFS_IS_REALTIME_INODE(ip);
957d0ebe 427 extsz = xfs_get_extsz_hint(ip);
1da177e4 428
ba87ea69 429 isize = ip->i_size;
613d7043
CH
430 if (ip->i_new_size > isize)
431 isize = ip->i_new_size;
1da177e4 432
957d0ebe
DC
433 offset_fsb = XFS_B_TO_FSBT(mp, offset);
434 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
dd9f438e 435 if ((offset + count) > isize) {
541d7d3c 436 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
dd9f438e
NS
437 &last_fsb);
438 if (error)
439 goto error_out;
1da177e4 440 } else {
dd9f438e
NS
441 if (found && (ret_imap->br_startblock == HOLESTARTBLOCK))
442 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
443 ret_imap->br_blockcount +
444 ret_imap->br_startoff);
1da177e4 445 }
dd9f438e
NS
446 count_fsb = last_fsb - offset_fsb;
447 ASSERT(count_fsb > 0);
448
449 resaligned = count_fsb;
450 if (unlikely(extsz)) {
451 if ((temp = do_mod(offset_fsb, extsz)))
452 resaligned += temp;
453 if ((temp = do_mod(resaligned, extsz)))
454 resaligned += extsz - temp;
455 }
456
457 if (unlikely(rt)) {
458 resrtextents = qblocks = resaligned;
459 resrtextents /= mp->m_sb.sb_rextsize;
84e1e99f
DC
460 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
461 quota_flag = XFS_QMOPT_RES_RTBLKS;
462 } else {
463 resrtextents = 0;
dd9f438e 464 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
84e1e99f
DC
465 quota_flag = XFS_QMOPT_RES_REGBLKS;
466 }
1da177e4
LT
467
468 /*
06d10dd9 469 * Allocate and setup the transaction
1da177e4
LT
470 */
471 xfs_iunlock(ip, XFS_ILOCK_EXCL);
472 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
1da177e4 473 error = xfs_trans_reserve(tp, resblks,
d52b44d0 474 XFS_WRITE_LOG_RES(mp), resrtextents,
1da177e4
LT
475 XFS_TRANS_PERM_LOG_RES,
476 XFS_WRITE_LOG_COUNT);
1da177e4 477 /*
06d10dd9 478 * Check for running out of space, note: need lock to return
1da177e4
LT
479 */
480 if (error)
1da177e4 481 xfs_trans_cancel(tp, 0);
1da177e4 482 xfs_ilock(ip, XFS_ILOCK_EXCL);
1da177e4 483 if (error)
06d10dd9 484 goto error_out;
1da177e4 485
dd9f438e
NS
486 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
487 qblocks, 0, quota_flag);
488 if (error)
1da177e4 489 goto error1;
1da177e4 490
1da177e4
LT
491 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
492 xfs_trans_ihold(tp, ip);
493
dd9f438e 494 bmapi_flag = XFS_BMAPI_WRITE;
ba87ea69 495 if ((flags & BMAPI_DIRECT) && (offset < ip->i_size || extsz))
1da177e4
LT
496 bmapi_flag |= XFS_BMAPI_PREALLOC;
497
498 /*
dd9f438e 499 * Issue the xfs_bmapi() call to allocate the blocks
1da177e4
LT
500 */
501 XFS_BMAP_INIT(&free_list, &firstfsb);
06d10dd9 502 nimaps = 1;
541d7d3c 503 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, bmapi_flag,
3e57ecf6 504 &firstfsb, 0, &imap, &nimaps, &free_list, NULL);
06d10dd9 505 if (error)
1da177e4 506 goto error0;
1da177e4
LT
507
508 /*
06d10dd9 509 * Complete the transaction
1da177e4 510 */
f7c99b6f 511 error = xfs_bmap_finish(&tp, &free_list, &committed);
06d10dd9 512 if (error)
1da177e4 513 goto error0;
1c72bf90 514 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
06d10dd9 515 if (error)
1da177e4 516 goto error_out;
1da177e4 517
06d10dd9
NS
518 /*
519 * Copy any maps to caller's array and return any error.
520 */
1da177e4 521 if (nimaps == 0) {
572d95f4
NS
522 error = ENOSPC;
523 goto error_out;
524 }
525
613d7043
CH
526 if (unlikely(!imap.br_startblock &&
527 !(ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
572d95f4 528 error = xfs_cmn_err_fsblock_zero(ip, &imap);
1da177e4
LT
529 goto error_out;
530 }
531
0116d935 532 *ret_imap = imap;
1da177e4 533 *nmaps = 1;
1da177e4
LT
534 return 0;
535
06d10dd9 536error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1da177e4 537 xfs_bmap_cancel(&free_list);
06d10dd9 538 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
1da177e4 539
06d10dd9 540error1: /* Just cancel transaction */
1da177e4
LT
541 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
542 *nmaps = 0; /* nothing set-up here */
543
544error_out:
545 return XFS_ERROR(error);
546}
547
dd9f438e
NS
548/*
549 * If the caller is doing a write at the end of the file,
550 * then extend the allocation out to the file system's write
551 * iosize. We clean up any extra space left over when the
552 * file is closed in xfs_inactive().
553 *
554 * For sync writes, we are flushing delayed allocate space to
555 * try to make additional space available for allocation near
556 * the filesystem full boundary - preallocation hurts in that
557 * situation, of course.
558 */
559STATIC int
560xfs_iomap_eof_want_preallocate(
561 xfs_mount_t *mp,
541d7d3c 562 xfs_inode_t *ip,
dd9f438e
NS
563 xfs_fsize_t isize,
564 xfs_off_t offset,
565 size_t count,
566 int ioflag,
567 xfs_bmbt_irec_t *imap,
568 int nimaps,
569 int *prealloc)
570{
571 xfs_fileoff_t start_fsb;
572 xfs_filblks_t count_fsb;
573 xfs_fsblock_t firstblock;
574 int n, error, imaps;
575
576 *prealloc = 0;
577 if ((ioflag & BMAPI_SYNC) || (offset + count) <= isize)
578 return 0;
579
580 /*
581 * If there are any real blocks past eof, then don't
582 * do any speculative allocation.
583 */
584 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
585 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
586 while (count_fsb > 0) {
587 imaps = nimaps;
3ddb8fa9 588 firstblock = NULLFSBLOCK;
541d7d3c 589 error = xfs_bmapi(NULL, ip, start_fsb, count_fsb, 0,
3e57ecf6 590 &firstblock, 0, imap, &imaps, NULL, NULL);
dd9f438e
NS
591 if (error)
592 return error;
593 for (n = 0; n < imaps; n++) {
594 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
595 (imap[n].br_startblock != DELAYSTARTBLOCK))
596 return 0;
597 start_fsb += imap[n].br_blockcount;
598 count_fsb -= imap[n].br_blockcount;
599 }
600 }
601 *prealloc = 1;
602 return 0;
603}
604
1da177e4
LT
605int
606xfs_iomap_write_delay(
607 xfs_inode_t *ip,
f403b7f4 608 xfs_off_t offset,
1da177e4
LT
609 size_t count,
610 int ioflag,
611 xfs_bmbt_irec_t *ret_imap,
612 int *nmaps)
613{
614 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
615 xfs_fileoff_t offset_fsb;
616 xfs_fileoff_t last_fsb;
dd9f438e
NS
617 xfs_off_t aligned_offset;
618 xfs_fileoff_t ioalign;
1da177e4 619 xfs_fsblock_t firstblock;
dd9f438e
NS
620 xfs_extlen_t extsz;
621 xfs_fsize_t isize;
1da177e4 622 int nimaps;
1da177e4 623 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
dd9f438e
NS
624 int prealloc, fsynced = 0;
625 int error;
1da177e4
LT
626
627 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
628
629 /*
630 * Make sure that the dquots are there. This doesn't hold
631 * the ilock across a disk read.
632 */
1da177e4
LT
633 error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED);
634 if (error)
635 return XFS_ERROR(error);
636
957d0ebe 637 extsz = xfs_get_extsz_hint(ip);
dd9f438e
NS
638 offset_fsb = XFS_B_TO_FSBT(mp, offset);
639
1da177e4 640retry:
ba87ea69 641 isize = ip->i_size;
613d7043
CH
642 if (ip->i_new_size > isize)
643 isize = ip->i_new_size;
1da177e4 644
541d7d3c 645 error = xfs_iomap_eof_want_preallocate(mp, ip, isize, offset, count,
dd9f438e
NS
646 ioflag, imap, XFS_WRITE_IMAPS, &prealloc);
647 if (error)
648 return error;
1da177e4 649
dd9f438e 650 if (prealloc) {
1da177e4
LT
651 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
652 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
dd9f438e
NS
653 last_fsb = ioalign + mp->m_writeio_blocks;
654 } else {
655 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
1da177e4 656 }
1da177e4 657
dd9f438e 658 if (prealloc || extsz) {
541d7d3c 659 error = xfs_iomap_eof_align_last_fsb(mp, ip, isize, extsz,
dd9f438e
NS
660 &last_fsb);
661 if (error)
1da177e4 662 return error;
1da177e4 663 }
dd9f438e
NS
664
665 nimaps = XFS_WRITE_IMAPS;
666 firstblock = NULLFSBLOCK;
541d7d3c 667 error = xfs_bmapi(NULL, ip, offset_fsb,
1da177e4
LT
668 (xfs_filblks_t)(last_fsb - offset_fsb),
669 XFS_BMAPI_DELAY | XFS_BMAPI_WRITE |
670 XFS_BMAPI_ENTIRE, &firstblock, 1, imap,
3e57ecf6 671 &nimaps, NULL, NULL);
dd9f438e 672 if (error && (error != ENOSPC))
1da177e4 673 return XFS_ERROR(error);
dd9f438e 674
1da177e4
LT
675 /*
676 * If bmapi returned us nothing, and if we didn't get back EDQUOT,
dd9f438e 677 * then we must have run out of space - flush delalloc, and retry..
1da177e4
LT
678 */
679 if (nimaps == 0) {
680 xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE,
541d7d3c 681 ip, offset, count);
1da177e4
LT
682 if (xfs_flush_space(ip, &fsynced, &ioflag))
683 return XFS_ERROR(ENOSPC);
684
685 error = 0;
686 goto retry;
687 }
688
613d7043
CH
689 if (unlikely(!imap[0].br_startblock &&
690 !(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)))
572d95f4 691 return xfs_cmn_err_fsblock_zero(ip, &imap[0]);
dd9f438e
NS
692
693 *ret_imap = imap[0];
694 *nmaps = 1;
695
1da177e4
LT
696 return 0;
697}
698
699/*
700 * Pass in a delayed allocate extent, convert it to real extents;
701 * return to the caller the extent we create which maps on top of
702 * the originating callers request.
703 *
704 * Called without a lock on the inode.
e4143a1c
DC
705 *
706 * We no longer bother to look at the incoming map - all we have to
707 * guarantee is that whatever we allocate fills the required range.
1da177e4
LT
708 */
709int
710xfs_iomap_write_allocate(
711 xfs_inode_t *ip,
f403b7f4 712 xfs_off_t offset,
24e17b5f 713 size_t count,
1da177e4
LT
714 xfs_bmbt_irec_t *map,
715 int *retmap)
716{
717 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
718 xfs_fileoff_t offset_fsb, last_block;
719 xfs_fileoff_t end_fsb, map_start_fsb;
720 xfs_fsblock_t first_block;
721 xfs_bmap_free_t free_list;
722 xfs_filblks_t count_fsb;
e4143a1c 723 xfs_bmbt_irec_t imap;
1da177e4 724 xfs_trans_t *tp;
e4143a1c 725 int nimaps, committed;
1da177e4
LT
726 int error = 0;
727 int nres;
728
729 *retmap = 0;
730
731 /*
732 * Make sure that the dquots are there.
733 */
734 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
735 return XFS_ERROR(error);
736
24e17b5f 737 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1da177e4 738 count_fsb = map->br_blockcount;
24e17b5f 739 map_start_fsb = map->br_startoff;
1da177e4
LT
740
741 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
742
743 while (count_fsb != 0) {
744 /*
745 * Set up a transaction with which to allocate the
746 * backing store for the file. Do allocations in a
747 * loop until we get some space in the range we are
748 * interested in. The other space that might be allocated
749 * is in the delayed allocation extent on which we sit
750 * but before our buffer starts.
751 */
752
753 nimaps = 0;
754 while (nimaps == 0) {
755 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
84e1e99f 756 tp->t_flags |= XFS_TRANS_RESERVE;
1da177e4
LT
757 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
758 error = xfs_trans_reserve(tp, nres,
759 XFS_WRITE_LOG_RES(mp),
760 0, XFS_TRANS_PERM_LOG_RES,
761 XFS_WRITE_LOG_COUNT);
1da177e4
LT
762 if (error) {
763 xfs_trans_cancel(tp, 0);
764 return XFS_ERROR(error);
765 }
766 xfs_ilock(ip, XFS_ILOCK_EXCL);
767 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
768 xfs_trans_ihold(tp, ip);
769
770 XFS_BMAP_INIT(&free_list, &first_block);
771
1da177e4 772 /*
e4143a1c
DC
773 * it is possible that the extents have changed since
774 * we did the read call as we dropped the ilock for a
775 * while. We have to be careful about truncates or hole
776 * punchs here - we are not allowed to allocate
777 * non-delalloc blocks here.
778 *
779 * The only protection against truncation is the pages
780 * for the range we are being asked to convert are
781 * locked and hence a truncate will block on them
782 * first.
783 *
784 * As a result, if we go beyond the range we really
785 * need and hit an delalloc extent boundary followed by
786 * a hole while we have excess blocks in the map, we
787 * will fill the hole incorrectly and overrun the
788 * transaction reservation.
789 *
790 * Using a single map prevents this as we are forced to
791 * check each map we look for overlap with the desired
792 * range and abort as soon as we find it. Also, given
793 * that we only return a single map, having one beyond
794 * what we can return is probably a bit silly.
795 *
796 * We also need to check that we don't go beyond EOF;
797 * this is a truncate optimisation as a truncate sets
798 * the new file size before block on the pages we
799 * currently have locked under writeback. Because they
800 * are about to be tossed, we don't need to write them
801 * back....
1da177e4 802 */
e4143a1c 803 nimaps = 1;
ba87ea69 804 end_fsb = XFS_B_TO_FSB(mp, ip->i_size);
1da177e4
LT
805 xfs_bmap_last_offset(NULL, ip, &last_block,
806 XFS_DATA_FORK);
807 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
808 if ((map_start_fsb + count_fsb) > last_block) {
809 count_fsb = last_block - map_start_fsb;
810 if (count_fsb == 0) {
811 error = EAGAIN;
812 goto trans_cancel;
813 }
814 }
815
816 /* Go get the actual blocks */
541d7d3c 817 error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb,
1da177e4 818 XFS_BMAPI_WRITE, &first_block, 1,
e4143a1c 819 &imap, &nimaps, &free_list, NULL);
1da177e4
LT
820 if (error)
821 goto trans_cancel;
822
f7c99b6f 823 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
824 if (error)
825 goto trans_cancel;
826
1c72bf90 827 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
828 if (error)
829 goto error0;
830
831 xfs_iunlock(ip, XFS_ILOCK_EXCL);
832 }
833
834 /*
835 * See if we were able to allocate an extent that
836 * covers at least part of the callers request
837 */
e4143a1c
DC
838 if (unlikely(!imap.br_startblock &&
839 XFS_IS_REALTIME_INODE(ip)))
840 return xfs_cmn_err_fsblock_zero(ip, &imap);
841 if ((offset_fsb >= imap.br_startoff) &&
842 (offset_fsb < (imap.br_startoff +
843 imap.br_blockcount))) {
844 *map = imap;
845 *retmap = 1;
846 XFS_STATS_INC(xs_xstrat_quick);
847 return 0;
1da177e4
LT
848 }
849
e4143a1c
DC
850 /*
851 * So far we have not mapped the requested part of the
1da177e4
LT
852 * file, just surrounding data, try again.
853 */
e4143a1c
DC
854 count_fsb -= imap.br_blockcount;
855 map_start_fsb = imap.br_startoff + imap.br_blockcount;
1da177e4
LT
856 }
857
858trans_cancel:
859 xfs_bmap_cancel(&free_list);
860 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
861error0:
862 xfs_iunlock(ip, XFS_ILOCK_EXCL);
863 return XFS_ERROR(error);
864}
865
866int
867xfs_iomap_write_unwritten(
868 xfs_inode_t *ip,
f403b7f4 869 xfs_off_t offset,
1da177e4
LT
870 size_t count)
871{
872 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
873 xfs_fileoff_t offset_fsb;
874 xfs_filblks_t count_fsb;
875 xfs_filblks_t numblks_fsb;
dd9f438e
NS
876 xfs_fsblock_t firstfsb;
877 int nimaps;
878 xfs_trans_t *tp;
879 xfs_bmbt_irec_t imap;
880 xfs_bmap_free_t free_list;
881 uint resblks;
1da177e4
LT
882 int committed;
883 int error;
1da177e4 884
541d7d3c 885 xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN, ip, offset, count);
1da177e4
LT
886
887 offset_fsb = XFS_B_TO_FSBT(mp, offset);
888 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
889 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
890
dd9f438e 891 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
1da177e4 892
dd9f438e 893 do {
1da177e4
LT
894 /*
895 * set up a transaction to convert the range of extents
896 * from unwritten to real. Do allocations in a loop until
897 * we have covered the range passed in.
898 */
1da177e4 899 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
84e1e99f 900 tp->t_flags |= XFS_TRANS_RESERVE;
dd9f438e 901 error = xfs_trans_reserve(tp, resblks,
1da177e4
LT
902 XFS_WRITE_LOG_RES(mp), 0,
903 XFS_TRANS_PERM_LOG_RES,
904 XFS_WRITE_LOG_COUNT);
905 if (error) {
906 xfs_trans_cancel(tp, 0);
572d95f4 907 return XFS_ERROR(error);
1da177e4
LT
908 }
909
910 xfs_ilock(ip, XFS_ILOCK_EXCL);
911 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
912 xfs_trans_ihold(tp, ip);
913
914 /*
915 * Modify the unwritten extent state of the buffer.
916 */
917 XFS_BMAP_INIT(&free_list, &firstfsb);
918 nimaps = 1;
541d7d3c 919 error = xfs_bmapi(tp, ip, offset_fsb, count_fsb,
dd9f438e 920 XFS_BMAPI_WRITE|XFS_BMAPI_CONVERT, &firstfsb,
3e57ecf6 921 1, &imap, &nimaps, &free_list, NULL);
1da177e4
LT
922 if (error)
923 goto error_on_bmapi_transaction;
924
f7c99b6f 925 error = xfs_bmap_finish(&(tp), &(free_list), &committed);
1da177e4
LT
926 if (error)
927 goto error_on_bmapi_transaction;
928
1c72bf90 929 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
930 xfs_iunlock(ip, XFS_ILOCK_EXCL);
931 if (error)
572d95f4
NS
932 return XFS_ERROR(error);
933
934 if (unlikely(!imap.br_startblock &&
613d7043 935 !(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)))
572d95f4 936 return xfs_cmn_err_fsblock_zero(ip, &imap);
1da177e4
LT
937
938 if ((numblks_fsb = imap.br_blockcount) == 0) {
939 /*
940 * The numblks_fsb value should always get
941 * smaller, otherwise the loop is stuck.
942 */
943 ASSERT(imap.br_blockcount);
944 break;
945 }
946 offset_fsb += numblks_fsb;
947 count_fsb -= numblks_fsb;
948 } while (count_fsb > 0);
949
950 return 0;
951
952error_on_bmapi_transaction:
953 xfs_bmap_cancel(&free_list);
954 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
955 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1da177e4
LT
956 return XFS_ERROR(error);
957}
This page took 0.314618 seconds and 5 git commands to generate.