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