xfs: merge xfs_ag.h into xfs_format.h
[deliverable/linux.git] / fs / xfs / xfs_iomap.c
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sb.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_bmap.h"
30 #include "xfs_bmap_util.h"
31 #include "xfs_error.h"
32 #include "xfs_trans.h"
33 #include "xfs_trans_space.h"
34 #include "xfs_iomap.h"
35 #include "xfs_trace.h"
36 #include "xfs_icache.h"
37 #include "xfs_quota.h"
38 #include "xfs_dquot_item.h"
39 #include "xfs_dquot.h"
40
41
42 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
43 << mp->m_writeio_log)
44 #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP
45
46 STATIC int
47 xfs_iomap_eof_align_last_fsb(
48 xfs_mount_t *mp,
49 xfs_inode_t *ip,
50 xfs_extlen_t extsize,
51 xfs_fileoff_t *last_fsb)
52 {
53 xfs_fileoff_t new_last_fsb = 0;
54 xfs_extlen_t align = 0;
55 int eof, error;
56
57 if (!XFS_IS_REALTIME_INODE(ip)) {
58 /*
59 * Round up the allocation request to a stripe unit
60 * (m_dalign) boundary if the file size is >= stripe unit
61 * size, and we are allocating past the allocation eof.
62 *
63 * If mounted with the "-o swalloc" option the alignment is
64 * increased from the strip unit size to the stripe width.
65 */
66 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
67 align = mp->m_swidth;
68 else if (mp->m_dalign)
69 align = mp->m_dalign;
70
71 if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
72 new_last_fsb = roundup_64(*last_fsb, align);
73 }
74
75 /*
76 * Always round up the allocation request to an extent boundary
77 * (when file on a real-time subvolume or has di_extsize hint).
78 */
79 if (extsize) {
80 if (new_last_fsb)
81 align = roundup_64(new_last_fsb, extsize);
82 else
83 align = extsize;
84 new_last_fsb = roundup_64(*last_fsb, align);
85 }
86
87 if (new_last_fsb) {
88 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
89 if (error)
90 return error;
91 if (eof)
92 *last_fsb = new_last_fsb;
93 }
94 return 0;
95 }
96
97 STATIC int
98 xfs_alert_fsblock_zero(
99 xfs_inode_t *ip,
100 xfs_bmbt_irec_t *imap)
101 {
102 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
103 "Access to block zero in inode %llu "
104 "start_block: %llx start_off: %llx "
105 "blkcnt: %llx extent-state: %x",
106 (unsigned long long)ip->i_ino,
107 (unsigned long long)imap->br_startblock,
108 (unsigned long long)imap->br_startoff,
109 (unsigned long long)imap->br_blockcount,
110 imap->br_state);
111 return -EFSCORRUPTED;
112 }
113
114 int
115 xfs_iomap_write_direct(
116 xfs_inode_t *ip,
117 xfs_off_t offset,
118 size_t count,
119 xfs_bmbt_irec_t *imap,
120 int nmaps)
121 {
122 xfs_mount_t *mp = ip->i_mount;
123 xfs_fileoff_t offset_fsb;
124 xfs_fileoff_t last_fsb;
125 xfs_filblks_t count_fsb, resaligned;
126 xfs_fsblock_t firstfsb;
127 xfs_extlen_t extsz, temp;
128 int nimaps;
129 int quota_flag;
130 int rt;
131 xfs_trans_t *tp;
132 xfs_bmap_free_t free_list;
133 uint qblocks, resblks, resrtextents;
134 int committed;
135 int error;
136
137 error = xfs_qm_dqattach(ip, 0);
138 if (error)
139 return error;
140
141 rt = XFS_IS_REALTIME_INODE(ip);
142 extsz = xfs_get_extsz_hint(ip);
143
144 offset_fsb = XFS_B_TO_FSBT(mp, offset);
145 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
146 if ((offset + count) > XFS_ISIZE(ip)) {
147 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
148 if (error)
149 return error;
150 } else {
151 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
152 last_fsb = MIN(last_fsb, (xfs_fileoff_t)
153 imap->br_blockcount +
154 imap->br_startoff);
155 }
156 count_fsb = last_fsb - offset_fsb;
157 ASSERT(count_fsb > 0);
158
159 resaligned = count_fsb;
160 if (unlikely(extsz)) {
161 if ((temp = do_mod(offset_fsb, extsz)))
162 resaligned += temp;
163 if ((temp = do_mod(resaligned, extsz)))
164 resaligned += extsz - temp;
165 }
166
167 if (unlikely(rt)) {
168 resrtextents = qblocks = resaligned;
169 resrtextents /= mp->m_sb.sb_rextsize;
170 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
171 quota_flag = XFS_QMOPT_RES_RTBLKS;
172 } else {
173 resrtextents = 0;
174 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
175 quota_flag = XFS_QMOPT_RES_REGBLKS;
176 }
177
178 /*
179 * Allocate and setup the transaction
180 */
181 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
182 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
183 resblks, resrtextents);
184 /*
185 * Check for running out of space, note: need lock to return
186 */
187 if (error) {
188 xfs_trans_cancel(tp, 0);
189 return error;
190 }
191
192 xfs_ilock(ip, XFS_ILOCK_EXCL);
193
194 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
195 if (error)
196 goto out_trans_cancel;
197
198 xfs_trans_ijoin(tp, ip, 0);
199
200 /*
201 * From this point onwards we overwrite the imap pointer that the
202 * caller gave to us.
203 */
204 xfs_bmap_init(&free_list, &firstfsb);
205 nimaps = 1;
206 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
207 XFS_BMAPI_PREALLOC, &firstfsb, 0,
208 imap, &nimaps, &free_list);
209 if (error)
210 goto out_bmap_cancel;
211
212 /*
213 * Complete the transaction
214 */
215 error = xfs_bmap_finish(&tp, &free_list, &committed);
216 if (error)
217 goto out_bmap_cancel;
218 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
219 if (error)
220 goto out_unlock;
221
222 /*
223 * Copy any maps to caller's array and return any error.
224 */
225 if (nimaps == 0) {
226 error = -ENOSPC;
227 goto out_unlock;
228 }
229
230 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
231 error = xfs_alert_fsblock_zero(ip, imap);
232
233 out_unlock:
234 xfs_iunlock(ip, XFS_ILOCK_EXCL);
235 return error;
236
237 out_bmap_cancel:
238 xfs_bmap_cancel(&free_list);
239 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
240 out_trans_cancel:
241 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
242 goto out_unlock;
243 }
244
245 /*
246 * If the caller is doing a write at the end of the file, then extend the
247 * allocation out to the file system's write iosize. We clean up any extra
248 * space left over when the file is closed in xfs_inactive().
249 *
250 * If we find we already have delalloc preallocation beyond EOF, don't do more
251 * preallocation as it it not needed.
252 */
253 STATIC int
254 xfs_iomap_eof_want_preallocate(
255 xfs_mount_t *mp,
256 xfs_inode_t *ip,
257 xfs_off_t offset,
258 size_t count,
259 xfs_bmbt_irec_t *imap,
260 int nimaps,
261 int *prealloc)
262 {
263 xfs_fileoff_t start_fsb;
264 xfs_filblks_t count_fsb;
265 xfs_fsblock_t firstblock;
266 int n, error, imaps;
267 int found_delalloc = 0;
268
269 *prealloc = 0;
270 if (offset + count <= XFS_ISIZE(ip))
271 return 0;
272
273 /*
274 * If the file is smaller than the minimum prealloc and we are using
275 * dynamic preallocation, don't do any preallocation at all as it is
276 * likely this is the only write to the file that is going to be done.
277 */
278 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
279 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks))
280 return 0;
281
282 /*
283 * If there are any real blocks past eof, then don't
284 * do any speculative allocation.
285 */
286 start_fsb = XFS_B_TO_FSBT(mp, ((xfs_ufsize_t)(offset + count - 1)));
287 count_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
288 while (count_fsb > 0) {
289 imaps = nimaps;
290 firstblock = NULLFSBLOCK;
291 error = xfs_bmapi_read(ip, start_fsb, count_fsb, imap, &imaps,
292 0);
293 if (error)
294 return error;
295 for (n = 0; n < imaps; n++) {
296 if ((imap[n].br_startblock != HOLESTARTBLOCK) &&
297 (imap[n].br_startblock != DELAYSTARTBLOCK))
298 return 0;
299 start_fsb += imap[n].br_blockcount;
300 count_fsb -= imap[n].br_blockcount;
301
302 if (imap[n].br_startblock == DELAYSTARTBLOCK)
303 found_delalloc = 1;
304 }
305 }
306 if (!found_delalloc)
307 *prealloc = 1;
308 return 0;
309 }
310
311 /*
312 * Determine the initial size of the preallocation. We are beyond the current
313 * EOF here, but we need to take into account whether this is a sparse write or
314 * an extending write when determining the preallocation size. Hence we need to
315 * look up the extent that ends at the current write offset and use the result
316 * to determine the preallocation size.
317 *
318 * If the extent is a hole, then preallocation is essentially disabled.
319 * Otherwise we take the size of the preceeding data extent as the basis for the
320 * preallocation size. If the size of the extent is greater than half the
321 * maximum extent length, then use the current offset as the basis. This ensures
322 * that for large files the preallocation size always extends to MAXEXTLEN
323 * rather than falling short due to things like stripe unit/width alignment of
324 * real extents.
325 */
326 STATIC xfs_fsblock_t
327 xfs_iomap_eof_prealloc_initial_size(
328 struct xfs_mount *mp,
329 struct xfs_inode *ip,
330 xfs_off_t offset,
331 xfs_bmbt_irec_t *imap,
332 int nimaps)
333 {
334 xfs_fileoff_t start_fsb;
335 int imaps = 1;
336 int error;
337
338 ASSERT(nimaps >= imaps);
339
340 /* if we are using a specific prealloc size, return now */
341 if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
342 return 0;
343
344 /* If the file is small, then use the minimum prealloc */
345 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign))
346 return 0;
347
348 /*
349 * As we write multiple pages, the offset will always align to the
350 * start of a page and hence point to a hole at EOF. i.e. if the size is
351 * 4096 bytes, we only have one block at FSB 0, but XFS_B_TO_FSB(4096)
352 * will return FSB 1. Hence if there are blocks in the file, we want to
353 * point to the block prior to the EOF block and not the hole that maps
354 * directly at @offset.
355 */
356 start_fsb = XFS_B_TO_FSB(mp, offset);
357 if (start_fsb)
358 start_fsb--;
359 error = xfs_bmapi_read(ip, start_fsb, 1, imap, &imaps, XFS_BMAPI_ENTIRE);
360 if (error)
361 return 0;
362
363 ASSERT(imaps == 1);
364 if (imap[0].br_startblock == HOLESTARTBLOCK)
365 return 0;
366 if (imap[0].br_blockcount <= (MAXEXTLEN >> 1))
367 return imap[0].br_blockcount << 1;
368 return XFS_B_TO_FSB(mp, offset);
369 }
370
371 STATIC bool
372 xfs_quota_need_throttle(
373 struct xfs_inode *ip,
374 int type,
375 xfs_fsblock_t alloc_blocks)
376 {
377 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
378
379 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
380 return false;
381
382 /* no hi watermark, no throttle */
383 if (!dq->q_prealloc_hi_wmark)
384 return false;
385
386 /* under the lo watermark, no throttle */
387 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
388 return false;
389
390 return true;
391 }
392
393 STATIC void
394 xfs_quota_calc_throttle(
395 struct xfs_inode *ip,
396 int type,
397 xfs_fsblock_t *qblocks,
398 int *qshift,
399 int64_t *qfreesp)
400 {
401 int64_t freesp;
402 int shift = 0;
403 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
404
405 /* no dq, or over hi wmark, squash the prealloc completely */
406 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
407 *qblocks = 0;
408 *qfreesp = 0;
409 return;
410 }
411
412 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
413 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
414 shift = 2;
415 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
416 shift += 2;
417 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
418 shift += 2;
419 }
420
421 if (freesp < *qfreesp)
422 *qfreesp = freesp;
423
424 /* only overwrite the throttle values if we are more aggressive */
425 if ((freesp >> shift) < (*qblocks >> *qshift)) {
426 *qblocks = freesp;
427 *qshift = shift;
428 }
429 }
430
431 /*
432 * If we don't have a user specified preallocation size, dynamically increase
433 * the preallocation size as the size of the file grows. Cap the maximum size
434 * at a single extent or less if the filesystem is near full. The closer the
435 * filesystem is to full, the smaller the maximum prealocation.
436 */
437 STATIC xfs_fsblock_t
438 xfs_iomap_prealloc_size(
439 struct xfs_mount *mp,
440 struct xfs_inode *ip,
441 xfs_off_t offset,
442 struct xfs_bmbt_irec *imap,
443 int nimaps)
444 {
445 xfs_fsblock_t alloc_blocks = 0;
446 int shift = 0;
447 int64_t freesp;
448 xfs_fsblock_t qblocks;
449 int qshift = 0;
450
451 alloc_blocks = xfs_iomap_eof_prealloc_initial_size(mp, ip, offset,
452 imap, nimaps);
453 if (!alloc_blocks)
454 goto check_writeio;
455 qblocks = alloc_blocks;
456
457 /*
458 * MAXEXTLEN is not a power of two value but we round the prealloc down
459 * to the nearest power of two value after throttling. To prevent the
460 * round down from unconditionally reducing the maximum supported prealloc
461 * size, we round up first, apply appropriate throttling, round down and
462 * cap the value to MAXEXTLEN.
463 */
464 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
465 alloc_blocks);
466
467 xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
468 freesp = mp->m_sb.sb_fdblocks;
469 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
470 shift = 2;
471 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
472 shift++;
473 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
474 shift++;
475 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
476 shift++;
477 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
478 shift++;
479 }
480
481 /*
482 * Check each quota to cap the prealloc size, provide a shift value to
483 * throttle with and adjust amount of available space.
484 */
485 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
486 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
487 &freesp);
488 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
489 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
490 &freesp);
491 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
492 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
493 &freesp);
494
495 /*
496 * The final prealloc size is set to the minimum of free space available
497 * in each of the quotas and the overall filesystem.
498 *
499 * The shift throttle value is set to the maximum value as determined by
500 * the global low free space values and per-quota low free space values.
501 */
502 alloc_blocks = MIN(alloc_blocks, qblocks);
503 shift = MAX(shift, qshift);
504
505 if (shift)
506 alloc_blocks >>= shift;
507 /*
508 * rounddown_pow_of_two() returns an undefined result if we pass in
509 * alloc_blocks = 0.
510 */
511 if (alloc_blocks)
512 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
513 if (alloc_blocks > MAXEXTLEN)
514 alloc_blocks = MAXEXTLEN;
515
516 /*
517 * If we are still trying to allocate more space than is
518 * available, squash the prealloc hard. This can happen if we
519 * have a large file on a small filesystem and the above
520 * lowspace thresholds are smaller than MAXEXTLEN.
521 */
522 while (alloc_blocks && alloc_blocks >= freesp)
523 alloc_blocks >>= 4;
524
525 check_writeio:
526 if (alloc_blocks < mp->m_writeio_blocks)
527 alloc_blocks = mp->m_writeio_blocks;
528
529 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
530 mp->m_writeio_blocks);
531
532 return alloc_blocks;
533 }
534
535 int
536 xfs_iomap_write_delay(
537 xfs_inode_t *ip,
538 xfs_off_t offset,
539 size_t count,
540 xfs_bmbt_irec_t *ret_imap)
541 {
542 xfs_mount_t *mp = ip->i_mount;
543 xfs_fileoff_t offset_fsb;
544 xfs_fileoff_t last_fsb;
545 xfs_off_t aligned_offset;
546 xfs_fileoff_t ioalign;
547 xfs_extlen_t extsz;
548 int nimaps;
549 xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS];
550 int prealloc;
551 int error;
552
553 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
554
555 /*
556 * Make sure that the dquots are there. This doesn't hold
557 * the ilock across a disk read.
558 */
559 error = xfs_qm_dqattach_locked(ip, 0);
560 if (error)
561 return error;
562
563 extsz = xfs_get_extsz_hint(ip);
564 offset_fsb = XFS_B_TO_FSBT(mp, offset);
565
566 error = xfs_iomap_eof_want_preallocate(mp, ip, offset, count,
567 imap, XFS_WRITE_IMAPS, &prealloc);
568 if (error)
569 return error;
570
571 retry:
572 if (prealloc) {
573 xfs_fsblock_t alloc_blocks;
574
575 alloc_blocks = xfs_iomap_prealloc_size(mp, ip, offset, imap,
576 XFS_WRITE_IMAPS);
577
578 aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1));
579 ioalign = XFS_B_TO_FSBT(mp, aligned_offset);
580 last_fsb = ioalign + alloc_blocks;
581 } else {
582 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
583 }
584
585 if (prealloc || extsz) {
586 error = xfs_iomap_eof_align_last_fsb(mp, ip, extsz, &last_fsb);
587 if (error)
588 return error;
589 }
590
591 /*
592 * Make sure preallocation does not create extents beyond the range we
593 * actually support in this filesystem.
594 */
595 if (last_fsb > XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes))
596 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
597
598 ASSERT(last_fsb > offset_fsb);
599
600 nimaps = XFS_WRITE_IMAPS;
601 error = xfs_bmapi_delay(ip, offset_fsb, last_fsb - offset_fsb,
602 imap, &nimaps, XFS_BMAPI_ENTIRE);
603 switch (error) {
604 case 0:
605 case -ENOSPC:
606 case -EDQUOT:
607 break;
608 default:
609 return error;
610 }
611
612 /*
613 * If bmapi returned us nothing, we got either ENOSPC or EDQUOT. Retry
614 * without EOF preallocation.
615 */
616 if (nimaps == 0) {
617 trace_xfs_delalloc_enospc(ip, offset, count);
618 if (prealloc) {
619 prealloc = 0;
620 error = 0;
621 goto retry;
622 }
623 return error ? error : -ENOSPC;
624 }
625
626 if (!(imap[0].br_startblock || XFS_IS_REALTIME_INODE(ip)))
627 return xfs_alert_fsblock_zero(ip, &imap[0]);
628
629 /*
630 * Tag the inode as speculatively preallocated so we can reclaim this
631 * space on demand, if necessary.
632 */
633 if (prealloc)
634 xfs_inode_set_eofblocks_tag(ip);
635
636 *ret_imap = imap[0];
637 return 0;
638 }
639
640 /*
641 * Pass in a delayed allocate extent, convert it to real extents;
642 * return to the caller the extent we create which maps on top of
643 * the originating callers request.
644 *
645 * Called without a lock on the inode.
646 *
647 * We no longer bother to look at the incoming map - all we have to
648 * guarantee is that whatever we allocate fills the required range.
649 */
650 int
651 xfs_iomap_write_allocate(
652 xfs_inode_t *ip,
653 xfs_off_t offset,
654 xfs_bmbt_irec_t *imap)
655 {
656 xfs_mount_t *mp = ip->i_mount;
657 xfs_fileoff_t offset_fsb, last_block;
658 xfs_fileoff_t end_fsb, map_start_fsb;
659 xfs_fsblock_t first_block;
660 xfs_bmap_free_t free_list;
661 xfs_filblks_t count_fsb;
662 xfs_trans_t *tp;
663 int nimaps, committed;
664 int error = 0;
665 int nres;
666
667 /*
668 * Make sure that the dquots are there.
669 */
670 error = xfs_qm_dqattach(ip, 0);
671 if (error)
672 return error;
673
674 offset_fsb = XFS_B_TO_FSBT(mp, offset);
675 count_fsb = imap->br_blockcount;
676 map_start_fsb = imap->br_startoff;
677
678 XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb));
679
680 while (count_fsb != 0) {
681 /*
682 * Set up a transaction with which to allocate the
683 * backing store for the file. Do allocations in a
684 * loop until we get some space in the range we are
685 * interested in. The other space that might be allocated
686 * is in the delayed allocation extent on which we sit
687 * but before our buffer starts.
688 */
689
690 nimaps = 0;
691 while (nimaps == 0) {
692 tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE);
693 tp->t_flags |= XFS_TRANS_RESERVE;
694 nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
695 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
696 nres, 0);
697 if (error) {
698 xfs_trans_cancel(tp, 0);
699 return error;
700 }
701 xfs_ilock(ip, XFS_ILOCK_EXCL);
702 xfs_trans_ijoin(tp, ip, 0);
703
704 xfs_bmap_init(&free_list, &first_block);
705
706 /*
707 * it is possible that the extents have changed since
708 * we did the read call as we dropped the ilock for a
709 * while. We have to be careful about truncates or hole
710 * punchs here - we are not allowed to allocate
711 * non-delalloc blocks here.
712 *
713 * The only protection against truncation is the pages
714 * for the range we are being asked to convert are
715 * locked and hence a truncate will block on them
716 * first.
717 *
718 * As a result, if we go beyond the range we really
719 * need and hit an delalloc extent boundary followed by
720 * a hole while we have excess blocks in the map, we
721 * will fill the hole incorrectly and overrun the
722 * transaction reservation.
723 *
724 * Using a single map prevents this as we are forced to
725 * check each map we look for overlap with the desired
726 * range and abort as soon as we find it. Also, given
727 * that we only return a single map, having one beyond
728 * what we can return is probably a bit silly.
729 *
730 * We also need to check that we don't go beyond EOF;
731 * this is a truncate optimisation as a truncate sets
732 * the new file size before block on the pages we
733 * currently have locked under writeback. Because they
734 * are about to be tossed, we don't need to write them
735 * back....
736 */
737 nimaps = 1;
738 end_fsb = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
739 error = xfs_bmap_last_offset(ip, &last_block,
740 XFS_DATA_FORK);
741 if (error)
742 goto trans_cancel;
743
744 last_block = XFS_FILEOFF_MAX(last_block, end_fsb);
745 if ((map_start_fsb + count_fsb) > last_block) {
746 count_fsb = last_block - map_start_fsb;
747 if (count_fsb == 0) {
748 error = -EAGAIN;
749 goto trans_cancel;
750 }
751 }
752
753 /*
754 * From this point onwards we overwrite the imap
755 * pointer that the caller gave to us.
756 */
757 error = xfs_bmapi_write(tp, ip, map_start_fsb,
758 count_fsb, 0,
759 &first_block, 1,
760 imap, &nimaps, &free_list);
761 if (error)
762 goto trans_cancel;
763
764 error = xfs_bmap_finish(&tp, &free_list, &committed);
765 if (error)
766 goto trans_cancel;
767
768 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
769 if (error)
770 goto error0;
771
772 xfs_iunlock(ip, XFS_ILOCK_EXCL);
773 }
774
775 /*
776 * See if we were able to allocate an extent that
777 * covers at least part of the callers request
778 */
779 if (!(imap->br_startblock || XFS_IS_REALTIME_INODE(ip)))
780 return xfs_alert_fsblock_zero(ip, imap);
781
782 if ((offset_fsb >= imap->br_startoff) &&
783 (offset_fsb < (imap->br_startoff +
784 imap->br_blockcount))) {
785 XFS_STATS_INC(xs_xstrat_quick);
786 return 0;
787 }
788
789 /*
790 * So far we have not mapped the requested part of the
791 * file, just surrounding data, try again.
792 */
793 count_fsb -= imap->br_blockcount;
794 map_start_fsb = imap->br_startoff + imap->br_blockcount;
795 }
796
797 trans_cancel:
798 xfs_bmap_cancel(&free_list);
799 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
800 error0:
801 xfs_iunlock(ip, XFS_ILOCK_EXCL);
802 return error;
803 }
804
805 int
806 xfs_iomap_write_unwritten(
807 xfs_inode_t *ip,
808 xfs_off_t offset,
809 size_t count)
810 {
811 xfs_mount_t *mp = ip->i_mount;
812 xfs_fileoff_t offset_fsb;
813 xfs_filblks_t count_fsb;
814 xfs_filblks_t numblks_fsb;
815 xfs_fsblock_t firstfsb;
816 int nimaps;
817 xfs_trans_t *tp;
818 xfs_bmbt_irec_t imap;
819 xfs_bmap_free_t free_list;
820 xfs_fsize_t i_size;
821 uint resblks;
822 int committed;
823 int error;
824
825 trace_xfs_unwritten_convert(ip, offset, count);
826
827 offset_fsb = XFS_B_TO_FSBT(mp, offset);
828 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
829 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
830
831 /*
832 * Reserve enough blocks in this transaction for two complete extent
833 * btree splits. We may be converting the middle part of an unwritten
834 * extent and in this case we will insert two new extents in the btree
835 * each of which could cause a full split.
836 *
837 * This reservation amount will be used in the first call to
838 * xfs_bmbt_split() to select an AG with enough space to satisfy the
839 * rest of the operation.
840 */
841 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
842
843 do {
844 /*
845 * set up a transaction to convert the range of extents
846 * from unwritten to real. Do allocations in a loop until
847 * we have covered the range passed in.
848 *
849 * Note that we open code the transaction allocation here
850 * to pass KM_NOFS--we can't risk to recursing back into
851 * the filesystem here as we might be asked to write out
852 * the same inode that we complete here and might deadlock
853 * on the iolock.
854 */
855 sb_start_intwrite(mp->m_super);
856 tp = _xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE, KM_NOFS);
857 tp->t_flags |= XFS_TRANS_RESERVE | XFS_TRANS_FREEZE_PROT;
858 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
859 resblks, 0);
860 if (error) {
861 xfs_trans_cancel(tp, 0);
862 return error;
863 }
864
865 xfs_ilock(ip, XFS_ILOCK_EXCL);
866 xfs_trans_ijoin(tp, ip, 0);
867
868 /*
869 * Modify the unwritten extent state of the buffer.
870 */
871 xfs_bmap_init(&free_list, &firstfsb);
872 nimaps = 1;
873 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
874 XFS_BMAPI_CONVERT, &firstfsb,
875 1, &imap, &nimaps, &free_list);
876 if (error)
877 goto error_on_bmapi_transaction;
878
879 /*
880 * Log the updated inode size as we go. We have to be careful
881 * to only log it up to the actual write offset if it is
882 * halfway into a block.
883 */
884 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
885 if (i_size > offset + count)
886 i_size = offset + count;
887
888 i_size = xfs_new_eof(ip, i_size);
889 if (i_size) {
890 ip->i_d.di_size = i_size;
891 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
892 }
893
894 error = xfs_bmap_finish(&tp, &free_list, &committed);
895 if (error)
896 goto error_on_bmapi_transaction;
897
898 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
899 xfs_iunlock(ip, XFS_ILOCK_EXCL);
900 if (error)
901 return error;
902
903 if (!(imap.br_startblock || XFS_IS_REALTIME_INODE(ip)))
904 return xfs_alert_fsblock_zero(ip, &imap);
905
906 if ((numblks_fsb = imap.br_blockcount) == 0) {
907 /*
908 * The numblks_fsb value should always get
909 * smaller, otherwise the loop is stuck.
910 */
911 ASSERT(imap.br_blockcount);
912 break;
913 }
914 offset_fsb += numblks_fsb;
915 count_fsb -= numblks_fsb;
916 } while (count_fsb > 0);
917
918 return 0;
919
920 error_on_bmapi_transaction:
921 xfs_bmap_cancel(&free_list);
922 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT));
923 xfs_iunlock(ip, XFS_ILOCK_EXCL);
924 return error;
925 }
This page took 0.068149 seconds and 5 git commands to generate.