This source file includes following definitions.
- xfs_alert_fsblock_zero
- xfs_bmbt_to_iomap
- xfs_hole_to_iomap
- xfs_eof_alignment
- xfs_iomap_eof_align_last_fsb
- xfs_iomap_write_direct
- xfs_quota_need_throttle
- xfs_quota_calc_throttle
- xfs_iomap_prealloc_size
- xfs_file_iomap_begin_delay
- xfs_iomap_write_unwritten
- imap_needs_alloc
- needs_cow_for_zeroing
- xfs_ilock_for_iomap
- xfs_file_iomap_begin
- xfs_file_iomap_end_delalloc
- xfs_file_iomap_end
- xfs_seek_iomap_begin
- xfs_xattr_iomap_begin
1
2
3
4
5
6
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap_btree.h"
17 #include "xfs_bmap.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_errortag.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_inode_item.h"
24 #include "xfs_iomap.h"
25 #include "xfs_trace.h"
26 #include "xfs_quota.h"
27 #include "xfs_dquot_item.h"
28 #include "xfs_dquot.h"
29 #include "xfs_reflink.h"
30
31
32 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
33 << mp->m_writeio_log)
34
35 static int
36 xfs_alert_fsblock_zero(
37 xfs_inode_t *ip,
38 xfs_bmbt_irec_t *imap)
39 {
40 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
41 "Access to block zero in inode %llu "
42 "start_block: %llx start_off: %llx "
43 "blkcnt: %llx extent-state: %x",
44 (unsigned long long)ip->i_ino,
45 (unsigned long long)imap->br_startblock,
46 (unsigned long long)imap->br_startoff,
47 (unsigned long long)imap->br_blockcount,
48 imap->br_state);
49 return -EFSCORRUPTED;
50 }
51
52 int
53 xfs_bmbt_to_iomap(
54 struct xfs_inode *ip,
55 struct iomap *iomap,
56 struct xfs_bmbt_irec *imap,
57 bool shared)
58 {
59 struct xfs_mount *mp = ip->i_mount;
60
61 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
62 return xfs_alert_fsblock_zero(ip, imap);
63
64 if (imap->br_startblock == HOLESTARTBLOCK) {
65 iomap->addr = IOMAP_NULL_ADDR;
66 iomap->type = IOMAP_HOLE;
67 } else if (imap->br_startblock == DELAYSTARTBLOCK ||
68 isnullstartblock(imap->br_startblock)) {
69 iomap->addr = IOMAP_NULL_ADDR;
70 iomap->type = IOMAP_DELALLOC;
71 } else {
72 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
73 if (imap->br_state == XFS_EXT_UNWRITTEN)
74 iomap->type = IOMAP_UNWRITTEN;
75 else
76 iomap->type = IOMAP_MAPPED;
77 }
78 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
79 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
80 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
81 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
82
83 if (xfs_ipincount(ip) &&
84 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
85 iomap->flags |= IOMAP_F_DIRTY;
86 if (shared)
87 iomap->flags |= IOMAP_F_SHARED;
88 return 0;
89 }
90
91 static void
92 xfs_hole_to_iomap(
93 struct xfs_inode *ip,
94 struct iomap *iomap,
95 xfs_fileoff_t offset_fsb,
96 xfs_fileoff_t end_fsb)
97 {
98 iomap->addr = IOMAP_NULL_ADDR;
99 iomap->type = IOMAP_HOLE;
100 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
101 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
102 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
103 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
104 }
105
106 xfs_extlen_t
107 xfs_eof_alignment(
108 struct xfs_inode *ip,
109 xfs_extlen_t extsize)
110 {
111 struct xfs_mount *mp = ip->i_mount;
112 xfs_extlen_t align = 0;
113
114 if (!XFS_IS_REALTIME_INODE(ip)) {
115
116
117
118
119
120
121
122
123 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
124 align = mp->m_swidth;
125 else if (mp->m_dalign)
126 align = mp->m_dalign;
127
128 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
129 align = 0;
130 }
131
132
133
134
135
136 if (extsize) {
137 if (align)
138 align = roundup_64(align, extsize);
139 else
140 align = extsize;
141 }
142
143 return align;
144 }
145
146 STATIC int
147 xfs_iomap_eof_align_last_fsb(
148 struct xfs_inode *ip,
149 xfs_extlen_t extsize,
150 xfs_fileoff_t *last_fsb)
151 {
152 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
153
154 if (align) {
155 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
156 int eof, error;
157
158 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
159 if (error)
160 return error;
161 if (eof)
162 *last_fsb = new_last_fsb;
163 }
164 return 0;
165 }
166
167 int
168 xfs_iomap_write_direct(
169 xfs_inode_t *ip,
170 xfs_off_t offset,
171 size_t count,
172 xfs_bmbt_irec_t *imap,
173 int nmaps)
174 {
175 xfs_mount_t *mp = ip->i_mount;
176 xfs_fileoff_t offset_fsb;
177 xfs_fileoff_t last_fsb;
178 xfs_filblks_t count_fsb, resaligned;
179 xfs_extlen_t extsz;
180 int nimaps;
181 int quota_flag;
182 int rt;
183 xfs_trans_t *tp;
184 uint qblocks, resblks, resrtextents;
185 int error;
186 int lockmode;
187 int bmapi_flags = XFS_BMAPI_PREALLOC;
188 uint tflags = 0;
189
190 rt = XFS_IS_REALTIME_INODE(ip);
191 extsz = xfs_get_extsz_hint(ip);
192 lockmode = XFS_ILOCK_SHARED;
193
194 ASSERT(xfs_isilocked(ip, lockmode));
195
196 offset_fsb = XFS_B_TO_FSBT(mp, offset);
197 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
198 if ((offset + count) > XFS_ISIZE(ip)) {
199
200
201
202
203
204
205
206 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
207 XFS_IFEXTENTS);
208 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
209 if (error)
210 goto out_unlock;
211 } else {
212 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
213 last_fsb = min(last_fsb, (xfs_fileoff_t)
214 imap->br_blockcount +
215 imap->br_startoff);
216 }
217 count_fsb = last_fsb - offset_fsb;
218 ASSERT(count_fsb > 0);
219 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
220
221 if (unlikely(rt)) {
222 resrtextents = qblocks = resaligned;
223 resrtextents /= mp->m_sb.sb_rextsize;
224 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
225 quota_flag = XFS_QMOPT_RES_RTBLKS;
226 } else {
227 resrtextents = 0;
228 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
229 quota_flag = XFS_QMOPT_RES_REGBLKS;
230 }
231
232
233
234
235
236 xfs_iunlock(ip, lockmode);
237 error = xfs_qm_dqattach(ip);
238 if (error)
239 return error;
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 if (IS_DAX(VFS_I(ip))) {
255 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
256 if (imap->br_state == XFS_EXT_UNWRITTEN) {
257 tflags |= XFS_TRANS_RESERVE;
258 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
259 }
260 }
261 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
262 tflags, &tp);
263 if (error)
264 return error;
265
266 lockmode = XFS_ILOCK_EXCL;
267 xfs_ilock(ip, lockmode);
268
269 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
270 if (error)
271 goto out_trans_cancel;
272
273 xfs_trans_ijoin(tp, ip, 0);
274
275
276
277
278
279 nimaps = 1;
280 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
281 bmapi_flags, resblks, imap, &nimaps);
282 if (error)
283 goto out_res_cancel;
284
285
286
287
288 error = xfs_trans_commit(tp);
289 if (error)
290 goto out_unlock;
291
292
293
294
295 if (nimaps == 0) {
296 error = -ENOSPC;
297 goto out_unlock;
298 }
299
300 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
301 error = xfs_alert_fsblock_zero(ip, imap);
302
303 out_unlock:
304 xfs_iunlock(ip, lockmode);
305 return error;
306
307 out_res_cancel:
308 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
309 out_trans_cancel:
310 xfs_trans_cancel(tp);
311 goto out_unlock;
312 }
313
314 STATIC bool
315 xfs_quota_need_throttle(
316 struct xfs_inode *ip,
317 int type,
318 xfs_fsblock_t alloc_blocks)
319 {
320 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
321
322 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
323 return false;
324
325
326 if (!dq->q_prealloc_hi_wmark)
327 return false;
328
329
330 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
331 return false;
332
333 return true;
334 }
335
336 STATIC void
337 xfs_quota_calc_throttle(
338 struct xfs_inode *ip,
339 int type,
340 xfs_fsblock_t *qblocks,
341 int *qshift,
342 int64_t *qfreesp)
343 {
344 int64_t freesp;
345 int shift = 0;
346 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
347
348
349 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
350 *qblocks = 0;
351 *qfreesp = 0;
352 return;
353 }
354
355 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
356 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
357 shift = 2;
358 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
359 shift += 2;
360 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
361 shift += 2;
362 }
363
364 if (freesp < *qfreesp)
365 *qfreesp = freesp;
366
367
368 if ((freesp >> shift) < (*qblocks >> *qshift)) {
369 *qblocks = freesp;
370 *qshift = shift;
371 }
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392 STATIC xfs_fsblock_t
393 xfs_iomap_prealloc_size(
394 struct xfs_inode *ip,
395 int whichfork,
396 loff_t offset,
397 loff_t count,
398 struct xfs_iext_cursor *icur)
399 {
400 struct xfs_mount *mp = ip->i_mount;
401 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
402 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
403 struct xfs_bmbt_irec prev;
404 int shift = 0;
405 int64_t freesp;
406 xfs_fsblock_t qblocks;
407 int qshift = 0;
408 xfs_fsblock_t alloc_blocks = 0;
409
410 if (offset + count <= XFS_ISIZE(ip))
411 return 0;
412
413 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
414 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
415 return 0;
416
417
418
419
420
421 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
422 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
423 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
424 prev.br_startoff + prev.br_blockcount < offset_fsb)
425 return mp->m_writeio_blocks;
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
444 alloc_blocks = prev.br_blockcount << 1;
445 else
446 alloc_blocks = XFS_B_TO_FSB(mp, offset);
447 if (!alloc_blocks)
448 goto check_writeio;
449 qblocks = alloc_blocks;
450
451
452
453
454
455
456
457
458 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
459 alloc_blocks);
460
461 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
462 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
463 shift = 2;
464 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
465 shift++;
466 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
467 shift++;
468 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
469 shift++;
470 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
471 shift++;
472 }
473
474
475
476
477
478 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
479 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
480 &freesp);
481 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
482 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
483 &freesp);
484 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
485 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
486 &freesp);
487
488
489
490
491
492
493
494
495 alloc_blocks = min(alloc_blocks, qblocks);
496 shift = max(shift, qshift);
497
498 if (shift)
499 alloc_blocks >>= shift;
500
501
502
503
504 if (alloc_blocks)
505 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
506 if (alloc_blocks > MAXEXTLEN)
507 alloc_blocks = MAXEXTLEN;
508
509
510
511
512
513
514
515 while (alloc_blocks && alloc_blocks >= freesp)
516 alloc_blocks >>= 4;
517 check_writeio:
518 if (alloc_blocks < mp->m_writeio_blocks)
519 alloc_blocks = mp->m_writeio_blocks;
520 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
521 mp->m_writeio_blocks);
522 return alloc_blocks;
523 }
524
525 static int
526 xfs_file_iomap_begin_delay(
527 struct inode *inode,
528 loff_t offset,
529 loff_t count,
530 unsigned flags,
531 struct iomap *iomap)
532 {
533 struct xfs_inode *ip = XFS_I(inode);
534 struct xfs_mount *mp = ip->i_mount;
535 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
536 xfs_fileoff_t maxbytes_fsb =
537 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
538 xfs_fileoff_t end_fsb;
539 struct xfs_bmbt_irec imap, cmap;
540 struct xfs_iext_cursor icur, ccur;
541 xfs_fsblock_t prealloc_blocks = 0;
542 bool eof = false, cow_eof = false, shared = false;
543 int whichfork = XFS_DATA_FORK;
544 int error = 0;
545
546 ASSERT(!XFS_IS_REALTIME_INODE(ip));
547 ASSERT(!xfs_get_extsz_hint(ip));
548
549 xfs_ilock(ip, XFS_ILOCK_EXCL);
550
551 if (unlikely(XFS_TEST_ERROR(
552 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
553 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
554 mp, XFS_ERRTAG_BMAPIFORMAT))) {
555 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
556 error = -EFSCORRUPTED;
557 goto out_unlock;
558 }
559
560 XFS_STATS_INC(mp, xs_blk_mapw);
561
562 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
563 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
564 if (error)
565 goto out_unlock;
566 }
567
568 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
569
570
571
572
573
574
575
576 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
577 if (eof)
578 imap.br_startoff = end_fsb;
579
580
581 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
582 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
583 goto out_unlock;
584 }
585
586
587
588
589
590
591
592
593
594 if (xfs_is_cow_inode(ip)) {
595 if (!ip->i_cowfp) {
596 ASSERT(!xfs_is_reflink_inode(ip));
597 xfs_ifork_init_cow(ip);
598 }
599 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
600 &ccur, &cmap);
601 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
602 trace_xfs_reflink_cow_found(ip, &cmap);
603 whichfork = XFS_COW_FORK;
604 goto done;
605 }
606 }
607
608 if (imap.br_startoff <= offset_fsb) {
609
610
611
612
613
614 if (!xfs_is_cow_inode(ip) ||
615 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
616 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
617 &imap);
618 goto done;
619 }
620
621 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
622
623
624 error = xfs_inode_need_cow(ip, &imap, &shared);
625 if (error)
626 goto out_unlock;
627
628
629 if (!shared) {
630 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
631 &imap);
632 goto done;
633 }
634
635
636
637
638
639 whichfork = XFS_COW_FORK;
640 end_fsb = imap.br_startoff + imap.br_blockcount;
641 } else {
642
643
644
645
646
647
648
649
650
651 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
652 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
653
654 if (xfs_is_always_cow_inode(ip))
655 whichfork = XFS_COW_FORK;
656 }
657
658 error = xfs_qm_dqattach_locked(ip, false);
659 if (error)
660 goto out_unlock;
661
662 if (eof) {
663 prealloc_blocks = xfs_iomap_prealloc_size(ip, whichfork, offset,
664 count, &icur);
665 if (prealloc_blocks) {
666 xfs_extlen_t align;
667 xfs_off_t end_offset;
668 xfs_fileoff_t p_end_fsb;
669
670 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
671 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
672 prealloc_blocks;
673
674 align = xfs_eof_alignment(ip, 0);
675 if (align)
676 p_end_fsb = roundup_64(p_end_fsb, align);
677
678 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
679 ASSERT(p_end_fsb > offset_fsb);
680 prealloc_blocks = p_end_fsb - end_fsb;
681 }
682 }
683
684 retry:
685 error = xfs_bmapi_reserve_delalloc(ip, whichfork, offset_fsb,
686 end_fsb - offset_fsb, prealloc_blocks,
687 whichfork == XFS_DATA_FORK ? &imap : &cmap,
688 whichfork == XFS_DATA_FORK ? &icur : &ccur,
689 whichfork == XFS_DATA_FORK ? eof : cow_eof);
690 switch (error) {
691 case 0:
692 break;
693 case -ENOSPC:
694 case -EDQUOT:
695
696 trace_xfs_delalloc_enospc(ip, offset, count);
697 if (prealloc_blocks) {
698 prealloc_blocks = 0;
699 goto retry;
700 }
701
702 default:
703 goto out_unlock;
704 }
705
706
707
708
709
710 iomap->flags |= IOMAP_F_NEW;
711 trace_xfs_iomap_alloc(ip, offset, count, whichfork,
712 whichfork == XFS_DATA_FORK ? &imap : &cmap);
713 done:
714 if (whichfork == XFS_COW_FORK) {
715 if (imap.br_startoff > offset_fsb) {
716 xfs_trim_extent(&cmap, offset_fsb,
717 imap.br_startoff - offset_fsb);
718 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, true);
719 goto out_unlock;
720 }
721
722 xfs_trim_extent(&imap, cmap.br_startoff, cmap.br_blockcount);
723 shared = true;
724 }
725 error = xfs_bmbt_to_iomap(ip, iomap, &imap, shared);
726 out_unlock:
727 xfs_iunlock(ip, XFS_ILOCK_EXCL);
728 return error;
729 }
730
731 int
732 xfs_iomap_write_unwritten(
733 xfs_inode_t *ip,
734 xfs_off_t offset,
735 xfs_off_t count,
736 bool update_isize)
737 {
738 xfs_mount_t *mp = ip->i_mount;
739 xfs_fileoff_t offset_fsb;
740 xfs_filblks_t count_fsb;
741 xfs_filblks_t numblks_fsb;
742 int nimaps;
743 xfs_trans_t *tp;
744 xfs_bmbt_irec_t imap;
745 struct inode *inode = VFS_I(ip);
746 xfs_fsize_t i_size;
747 uint resblks;
748 int error;
749
750 trace_xfs_unwritten_convert(ip, offset, count);
751
752 offset_fsb = XFS_B_TO_FSBT(mp, offset);
753 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
754 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
755
756
757
758
759
760
761
762
763
764
765
766 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
767
768 do {
769
770
771
772
773
774
775
776
777
778 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
779 XFS_TRANS_RESERVE, &tp);
780 if (error)
781 return error;
782
783 xfs_ilock(ip, XFS_ILOCK_EXCL);
784 xfs_trans_ijoin(tp, ip, 0);
785
786
787
788
789 nimaps = 1;
790 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
791 XFS_BMAPI_CONVERT, resblks, &imap,
792 &nimaps);
793 if (error)
794 goto error_on_bmapi_transaction;
795
796
797
798
799
800
801 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
802 if (i_size > offset + count)
803 i_size = offset + count;
804 if (update_isize && i_size > i_size_read(inode))
805 i_size_write(inode, i_size);
806 i_size = xfs_new_eof(ip, i_size);
807 if (i_size) {
808 ip->i_d.di_size = i_size;
809 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
810 }
811
812 error = xfs_trans_commit(tp);
813 xfs_iunlock(ip, XFS_ILOCK_EXCL);
814 if (error)
815 return error;
816
817 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
818 return xfs_alert_fsblock_zero(ip, &imap);
819
820 if ((numblks_fsb = imap.br_blockcount) == 0) {
821
822
823
824
825 ASSERT(imap.br_blockcount);
826 break;
827 }
828 offset_fsb += numblks_fsb;
829 count_fsb -= numblks_fsb;
830 } while (count_fsb > 0);
831
832 return 0;
833
834 error_on_bmapi_transaction:
835 xfs_trans_cancel(tp);
836 xfs_iunlock(ip, XFS_ILOCK_EXCL);
837 return error;
838 }
839
840 static inline bool
841 imap_needs_alloc(
842 struct inode *inode,
843 struct xfs_bmbt_irec *imap,
844 int nimaps)
845 {
846 return !nimaps ||
847 imap->br_startblock == HOLESTARTBLOCK ||
848 imap->br_startblock == DELAYSTARTBLOCK ||
849 (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
850 }
851
852 static inline bool
853 needs_cow_for_zeroing(
854 struct xfs_bmbt_irec *imap,
855 int nimaps)
856 {
857 return nimaps &&
858 imap->br_startblock != HOLESTARTBLOCK &&
859 imap->br_state != XFS_EXT_UNWRITTEN;
860 }
861
862 static int
863 xfs_ilock_for_iomap(
864 struct xfs_inode *ip,
865 unsigned flags,
866 unsigned *lockmode)
867 {
868 unsigned mode = XFS_ILOCK_SHARED;
869 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
870
871
872
873
874
875 if (xfs_is_cow_inode(ip) && is_write) {
876
877
878
879
880 if (flags & IOMAP_NOWAIT)
881 return -EAGAIN;
882 mode = XFS_ILOCK_EXCL;
883 }
884
885
886
887
888
889
890 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
891 if (flags & IOMAP_NOWAIT)
892 return -EAGAIN;
893 mode = XFS_ILOCK_EXCL;
894 }
895
896 relock:
897 if (flags & IOMAP_NOWAIT) {
898 if (!xfs_ilock_nowait(ip, mode))
899 return -EAGAIN;
900 } else {
901 xfs_ilock(ip, mode);
902 }
903
904
905
906
907
908
909 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
910 xfs_iunlock(ip, mode);
911 mode = XFS_ILOCK_EXCL;
912 goto relock;
913 }
914
915 *lockmode = mode;
916 return 0;
917 }
918
919 static int
920 xfs_file_iomap_begin(
921 struct inode *inode,
922 loff_t offset,
923 loff_t length,
924 unsigned flags,
925 struct iomap *iomap)
926 {
927 struct xfs_inode *ip = XFS_I(inode);
928 struct xfs_mount *mp = ip->i_mount;
929 struct xfs_bmbt_irec imap;
930 xfs_fileoff_t offset_fsb, end_fsb;
931 int nimaps = 1, error = 0;
932 bool shared = false;
933 unsigned lockmode;
934
935 if (XFS_FORCED_SHUTDOWN(mp))
936 return -EIO;
937
938 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && !(flags & IOMAP_DIRECT) &&
939 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
940
941 return xfs_file_iomap_begin_delay(inode, offset, length, flags,
942 iomap);
943 }
944
945
946
947
948
949
950
951 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
952 if (error)
953 return error;
954
955 ASSERT(offset <= mp->m_super->s_maxbytes);
956 if (offset > mp->m_super->s_maxbytes - length)
957 length = mp->m_super->s_maxbytes - offset;
958 offset_fsb = XFS_B_TO_FSBT(mp, offset);
959 end_fsb = XFS_B_TO_FSB(mp, offset + length);
960
961 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
962 &nimaps, 0);
963 if (error)
964 goto out_unlock;
965
966 if (flags & IOMAP_REPORT) {
967
968 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
969 if (error)
970 goto out_unlock;
971 }
972
973
974 if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
975 goto out_found;
976
977
978
979
980
981 if (xfs_is_cow_inode(ip)) {
982 struct xfs_bmbt_irec cmap;
983 bool directio = (flags & IOMAP_DIRECT);
984
985
986 if ((flags & IOMAP_ZERO) &&
987 !needs_cow_for_zeroing(&imap, nimaps))
988 goto out_found;
989
990
991 cmap = imap;
992 error = xfs_reflink_allocate_cow(ip, &cmap, &shared, &lockmode,
993 directio);
994 if (error)
995 goto out_unlock;
996
997
998
999
1000
1001
1002
1003
1004
1005
1006 if (directio || imap.br_startblock == HOLESTARTBLOCK)
1007 imap = cmap;
1008
1009 end_fsb = imap.br_startoff + imap.br_blockcount;
1010 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
1011 }
1012
1013
1014 if (flags & IOMAP_ZERO)
1015 goto out_found;
1016
1017 if (!imap_needs_alloc(inode, &imap, nimaps))
1018 goto out_found;
1019
1020
1021 if (flags & IOMAP_NOWAIT) {
1022 error = -EAGAIN;
1023 goto out_unlock;
1024 }
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
1036
1037
1038
1039
1040
1041 if (lockmode == XFS_ILOCK_EXCL)
1042 xfs_ilock_demote(ip, lockmode);
1043 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1044 nimaps);
1045 if (error)
1046 return error;
1047
1048 iomap->flags |= IOMAP_F_NEW;
1049 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
1050
1051 out_finish:
1052 return xfs_bmbt_to_iomap(ip, iomap, &imap, shared);
1053
1054 out_found:
1055 ASSERT(nimaps);
1056 xfs_iunlock(ip, lockmode);
1057 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1058 goto out_finish;
1059
1060 out_unlock:
1061 xfs_iunlock(ip, lockmode);
1062 return error;
1063 }
1064
1065 static int
1066 xfs_file_iomap_end_delalloc(
1067 struct xfs_inode *ip,
1068 loff_t offset,
1069 loff_t length,
1070 ssize_t written,
1071 struct iomap *iomap)
1072 {
1073 struct xfs_mount *mp = ip->i_mount;
1074 xfs_fileoff_t start_fsb;
1075 xfs_fileoff_t end_fsb;
1076 int error = 0;
1077
1078
1079
1080
1081
1082 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1083 iomap->flags |= IOMAP_F_NEW;
1084 written = 0;
1085 }
1086
1087
1088
1089
1090
1091
1092 if (unlikely(!written))
1093 start_fsb = XFS_B_TO_FSBT(mp, offset);
1094 else
1095 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1096 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1107 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1108 XFS_FSB_TO_B(mp, end_fsb) - 1);
1109
1110 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1111 end_fsb - start_fsb);
1112 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1113 xfs_alert(mp, "%s: unable to clean up ino %lld",
1114 __func__, ip->i_ino);
1115 return error;
1116 }
1117 }
1118
1119 return 0;
1120 }
1121
1122 static int
1123 xfs_file_iomap_end(
1124 struct inode *inode,
1125 loff_t offset,
1126 loff_t length,
1127 ssize_t written,
1128 unsigned flags,
1129 struct iomap *iomap)
1130 {
1131 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1132 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
1133 length, written, iomap);
1134 return 0;
1135 }
1136
1137 const struct iomap_ops xfs_iomap_ops = {
1138 .iomap_begin = xfs_file_iomap_begin,
1139 .iomap_end = xfs_file_iomap_end,
1140 };
1141
1142 static int
1143 xfs_seek_iomap_begin(
1144 struct inode *inode,
1145 loff_t offset,
1146 loff_t length,
1147 unsigned flags,
1148 struct iomap *iomap)
1149 {
1150 struct xfs_inode *ip = XFS_I(inode);
1151 struct xfs_mount *mp = ip->i_mount;
1152 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1153 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1154 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1155 struct xfs_iext_cursor icur;
1156 struct xfs_bmbt_irec imap, cmap;
1157 int error = 0;
1158 unsigned lockmode;
1159
1160 if (XFS_FORCED_SHUTDOWN(mp))
1161 return -EIO;
1162
1163 lockmode = xfs_ilock_data_map_shared(ip);
1164 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
1165 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1166 if (error)
1167 goto out_unlock;
1168 }
1169
1170 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1171
1172
1173
1174 if (imap.br_startoff <= offset_fsb)
1175 goto done;
1176 data_fsb = imap.br_startoff;
1177 } else {
1178
1179
1180
1181 data_fsb = min(XFS_B_TO_FSB(mp, offset + length),
1182 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1183 }
1184
1185
1186
1187
1188
1189 if (xfs_inode_has_cow_data(ip) &&
1190 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1191 cow_fsb = cmap.br_startoff;
1192 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1193 if (data_fsb < cow_fsb + cmap.br_blockcount)
1194 end_fsb = min(end_fsb, data_fsb);
1195 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1196 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, true);
1197
1198
1199
1200
1201
1202 iomap->type = IOMAP_UNWRITTEN;
1203 goto out_unlock;
1204 }
1205
1206
1207
1208
1209 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1210 imap.br_blockcount = cow_fsb - offset_fsb;
1211 else
1212 imap.br_blockcount = data_fsb - offset_fsb;
1213 imap.br_startoff = offset_fsb;
1214 imap.br_startblock = HOLESTARTBLOCK;
1215 imap.br_state = XFS_EXT_NORM;
1216 done:
1217 xfs_trim_extent(&imap, offset_fsb, end_fsb);
1218 error = xfs_bmbt_to_iomap(ip, iomap, &imap, false);
1219 out_unlock:
1220 xfs_iunlock(ip, lockmode);
1221 return error;
1222 }
1223
1224 const struct iomap_ops xfs_seek_iomap_ops = {
1225 .iomap_begin = xfs_seek_iomap_begin,
1226 };
1227
1228 static int
1229 xfs_xattr_iomap_begin(
1230 struct inode *inode,
1231 loff_t offset,
1232 loff_t length,
1233 unsigned flags,
1234 struct iomap *iomap)
1235 {
1236 struct xfs_inode *ip = XFS_I(inode);
1237 struct xfs_mount *mp = ip->i_mount;
1238 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1239 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1240 struct xfs_bmbt_irec imap;
1241 int nimaps = 1, error = 0;
1242 unsigned lockmode;
1243
1244 if (XFS_FORCED_SHUTDOWN(mp))
1245 return -EIO;
1246
1247 lockmode = xfs_ilock_attr_map_shared(ip);
1248
1249
1250 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1251 error = -ENOENT;
1252 goto out_unlock;
1253 }
1254
1255 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1256 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1257 &nimaps, XFS_BMAPI_ATTRFORK);
1258 out_unlock:
1259 xfs_iunlock(ip, lockmode);
1260
1261 if (error)
1262 return error;
1263 ASSERT(nimaps);
1264 return xfs_bmbt_to_iomap(ip, iomap, &imap, false);
1265 }
1266
1267 const struct iomap_ops xfs_xattr_iomap_ops = {
1268 .iomap_begin = xfs_xattr_iomap_begin,
1269 };