]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
68988114 DC |
2 | /* |
3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. | |
c24b5dfa | 4 | * Copyright (c) 2012 Red Hat, Inc. |
68988114 | 5 | * All Rights Reserved. |
68988114 DC |
6 | */ |
7 | #include "xfs.h" | |
8 | #include "xfs_fs.h" | |
70a9883c | 9 | #include "xfs_shared.h" |
239880ef DC |
10 | #include "xfs_format.h" |
11 | #include "xfs_log_format.h" | |
12 | #include "xfs_trans_resv.h" | |
68988114 | 13 | #include "xfs_bit.h" |
68988114 | 14 | #include "xfs_mount.h" |
3ab78df2 | 15 | #include "xfs_defer.h" |
68988114 DC |
16 | #include "xfs_inode.h" |
17 | #include "xfs_btree.h" | |
239880ef | 18 | #include "xfs_trans.h" |
68988114 DC |
19 | #include "xfs_alloc.h" |
20 | #include "xfs_bmap.h" | |
21 | #include "xfs_bmap_util.h" | |
a4fbe6ab | 22 | #include "xfs_bmap_btree.h" |
68988114 DC |
23 | #include "xfs_rtalloc.h" |
24 | #include "xfs_error.h" | |
25 | #include "xfs_quota.h" | |
26 | #include "xfs_trans_space.h" | |
27 | #include "xfs_trace.h" | |
c24b5dfa | 28 | #include "xfs_icache.h" |
f86f4037 DW |
29 | #include "xfs_iomap.h" |
30 | #include "xfs_reflink.h" | |
68988114 DC |
31 | |
32 | /* Kernel only BMAP related definitions and functions */ | |
33 | ||
34 | /* | |
35 | * Convert the given file system block to a disk block. We have to treat it | |
36 | * differently based on whether the file is a real time file or not, because the | |
37 | * bmap code does. | |
38 | */ | |
39 | xfs_daddr_t | |
40 | xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb) | |
41 | { | |
ecfc28a4 CH |
42 | if (XFS_IS_REALTIME_INODE(ip)) |
43 | return XFS_FSB_TO_BB(ip->i_mount, fsb); | |
44 | return XFS_FSB_TO_DADDR(ip->i_mount, fsb); | |
68988114 DC |
45 | } |
46 | ||
3fbbbea3 DC |
47 | /* |
48 | * Routine to zero an extent on disk allocated to the specific inode. | |
49 | * | |
50 | * The VFS functions take a linearised filesystem block offset, so we have to | |
51 | * convert the sparse xfs fsb to the right format first. | |
52 | * VFS types are real funky, too. | |
53 | */ | |
54 | int | |
55 | xfs_zero_extent( | |
30fa529e CH |
56 | struct xfs_inode *ip, |
57 | xfs_fsblock_t start_fsb, | |
58 | xfs_off_t count_fsb) | |
3fbbbea3 | 59 | { |
30fa529e CH |
60 | struct xfs_mount *mp = ip->i_mount; |
61 | struct xfs_buftarg *target = xfs_inode_buftarg(ip); | |
62 | xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb); | |
63 | sector_t block = XFS_BB_TO_FSBT(mp, sector); | |
3fbbbea3 | 64 | |
30fa529e | 65 | return blkdev_issue_zeroout(target->bt_bdev, |
3dc29161 MW |
66 | block << (mp->m_super->s_blocksize_bits - 9), |
67 | count_fsb << (mp->m_super->s_blocksize_bits - 9), | |
ee472d83 | 68 | GFP_NOFS, 0); |
3fbbbea3 DC |
69 | } |
70 | ||
bb9c2e54 | 71 | #ifdef CONFIG_XFS_RT |
68988114 DC |
72 | int |
73 | xfs_bmap_rtalloc( | |
74 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ | |
75 | { | |
68988114 DC |
76 | int error; /* error return value */ |
77 | xfs_mount_t *mp; /* mount point structure */ | |
78 | xfs_extlen_t prod = 0; /* product factor for allocators */ | |
0703a8e1 | 79 | xfs_extlen_t mod = 0; /* product factor for allocators */ |
68988114 DC |
80 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
81 | xfs_extlen_t align; /* minimum allocation alignment */ | |
82 | xfs_rtblock_t rtb; | |
83 | ||
84 | mp = ap->ip->i_mount; | |
85 | align = xfs_get_extsz_hint(ap->ip); | |
86 | prod = align / mp->m_sb.sb_rextsize; | |
87 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, | |
88 | align, 1, ap->eof, 0, | |
89 | ap->conv, &ap->offset, &ap->length); | |
90 | if (error) | |
91 | return error; | |
92 | ASSERT(ap->length); | |
93 | ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); | |
94 | ||
95 | /* | |
96 | * If the offset & length are not perfectly aligned | |
97 | * then kill prod, it will just get us in trouble. | |
98 | */ | |
0703a8e1 DC |
99 | div_u64_rem(ap->offset, align, &mod); |
100 | if (mod || ap->length % align) | |
68988114 DC |
101 | prod = 1; |
102 | /* | |
103 | * Set ralen to be the actual requested length in rtextents. | |
104 | */ | |
105 | ralen = ap->length / mp->m_sb.sb_rextsize; | |
106 | /* | |
107 | * If the old value was close enough to MAXEXTLEN that | |
108 | * we rounded up to it, cut it back so it's valid again. | |
109 | * Note that if it's a really large request (bigger than | |
110 | * MAXEXTLEN), we don't hear about that number, and can't | |
111 | * adjust the starting point to match it. | |
112 | */ | |
113 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) | |
114 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; | |
115 | ||
116 | /* | |
4b680afb | 117 | * Lock out modifications to both the RT bitmap and summary inodes |
68988114 | 118 | */ |
f4a0660d | 119 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
68988114 | 120 | xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
f4a0660d | 121 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
4b680afb | 122 | xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
68988114 DC |
123 | |
124 | /* | |
125 | * If it's an allocation to an empty file at offset 0, | |
126 | * pick an extent that will space things out in the rt area. | |
127 | */ | |
128 | if (ap->eof && ap->offset == 0) { | |
129 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ | |
130 | ||
131 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); | |
132 | if (error) | |
133 | return error; | |
134 | ap->blkno = rtx * mp->m_sb.sb_rextsize; | |
135 | } else { | |
136 | ap->blkno = 0; | |
137 | } | |
138 | ||
139 | xfs_bmap_adjacent(ap); | |
140 | ||
141 | /* | |
142 | * Realtime allocation, done through xfs_rtallocate_extent. | |
143 | */ | |
68988114 DC |
144 | do_div(ap->blkno, mp->m_sb.sb_rextsize); |
145 | rtb = ap->blkno; | |
146 | ap->length = ralen; | |
089ec2f8 CH |
147 | error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, |
148 | &ralen, ap->wasdel, prod, &rtb); | |
149 | if (error) | |
68988114 | 150 | return error; |
089ec2f8 | 151 | |
68988114 DC |
152 | ap->blkno = rtb; |
153 | if (ap->blkno != NULLFSBLOCK) { | |
154 | ap->blkno *= mp->m_sb.sb_rextsize; | |
155 | ralen *= mp->m_sb.sb_rextsize; | |
156 | ap->length = ralen; | |
157 | ap->ip->i_d.di_nblocks += ralen; | |
158 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); | |
159 | if (ap->wasdel) | |
160 | ap->ip->i_delayed_blks -= ralen; | |
161 | /* | |
162 | * Adjust the disk quota also. This was reserved | |
163 | * earlier. | |
164 | */ | |
165 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, | |
166 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : | |
167 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); | |
3fbbbea3 DC |
168 | |
169 | /* Zero the extent if we were asked to do so */ | |
292378ed | 170 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) { |
3fbbbea3 DC |
171 | error = xfs_zero_extent(ap->ip, ap->blkno, ap->length); |
172 | if (error) | |
173 | return error; | |
174 | } | |
68988114 DC |
175 | } else { |
176 | ap->length = 0; | |
177 | } | |
178 | return 0; | |
179 | } | |
bb9c2e54 | 180 | #endif /* CONFIG_XFS_RT */ |
68988114 | 181 | |
68988114 DC |
182 | /* |
183 | * Check if the endoff is outside the last extent. If so the caller will grow | |
184 | * the allocation to a stripe unit boundary. All offsets are considered outside | |
185 | * the end of file for an empty fork, so 1 is returned in *eof in that case. | |
186 | */ | |
187 | int | |
188 | xfs_bmap_eof( | |
189 | struct xfs_inode *ip, | |
190 | xfs_fileoff_t endoff, | |
191 | int whichfork, | |
192 | int *eof) | |
193 | { | |
194 | struct xfs_bmbt_irec rec; | |
195 | int error; | |
196 | ||
197 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); | |
198 | if (error || *eof) | |
199 | return error; | |
200 | ||
201 | *eof = endoff >= rec.br_startoff + rec.br_blockcount; | |
202 | return 0; | |
203 | } | |
204 | ||
205 | /* | |
206 | * Extent tree block counting routines. | |
207 | */ | |
208 | ||
209 | /* | |
d29cb3e4 DW |
210 | * Count leaf blocks given a range of extent records. Delayed allocation |
211 | * extents are not counted towards the totals. | |
68988114 | 212 | */ |
e17a5c6f | 213 | xfs_extnum_t |
68988114 | 214 | xfs_bmap_count_leaves( |
d29cb3e4 | 215 | struct xfs_ifork *ifp, |
e7f5d5ca | 216 | xfs_filblks_t *count) |
68988114 | 217 | { |
b2b1712a | 218 | struct xfs_iext_cursor icur; |
e17a5c6f | 219 | struct xfs_bmbt_irec got; |
b2b1712a | 220 | xfs_extnum_t numrecs = 0; |
68988114 | 221 | |
b2b1712a | 222 | for_each_xfs_iext(ifp, &icur, &got) { |
e17a5c6f CH |
223 | if (!isnullstartblock(got.br_startblock)) { |
224 | *count += got.br_blockcount; | |
225 | numrecs++; | |
d29cb3e4 | 226 | } |
68988114 | 227 | } |
b2b1712a | 228 | |
e17a5c6f | 229 | return numrecs; |
68988114 DC |
230 | } |
231 | ||
232 | /* | |
233 | * Count leaf blocks given a range of extent records originally | |
234 | * in btree format. | |
235 | */ | |
236 | STATIC void | |
237 | xfs_bmap_disk_count_leaves( | |
238 | struct xfs_mount *mp, | |
239 | struct xfs_btree_block *block, | |
240 | int numrecs, | |
e7f5d5ca | 241 | xfs_filblks_t *count) |
68988114 DC |
242 | { |
243 | int b; | |
244 | xfs_bmbt_rec_t *frp; | |
245 | ||
246 | for (b = 1; b <= numrecs; b++) { | |
247 | frp = XFS_BMBT_REC_ADDR(mp, block, b); | |
248 | *count += xfs_bmbt_disk_get_blockcount(frp); | |
249 | } | |
250 | } | |
251 | ||
252 | /* | |
253 | * Recursively walks each level of a btree | |
8be11e92 | 254 | * to count total fsblocks in use. |
68988114 | 255 | */ |
e7f5d5ca | 256 | STATIC int |
68988114 | 257 | xfs_bmap_count_tree( |
e7f5d5ca DW |
258 | struct xfs_mount *mp, |
259 | struct xfs_trans *tp, | |
260 | struct xfs_ifork *ifp, | |
261 | xfs_fsblock_t blockno, | |
262 | int levelin, | |
263 | xfs_extnum_t *nextents, | |
264 | xfs_filblks_t *count) | |
68988114 DC |
265 | { |
266 | int error; | |
e7f5d5ca | 267 | struct xfs_buf *bp, *nbp; |
68988114 DC |
268 | int level = levelin; |
269 | __be64 *pp; | |
270 | xfs_fsblock_t bno = blockno; | |
271 | xfs_fsblock_t nextbno; | |
272 | struct xfs_btree_block *block, *nextblock; | |
273 | int numrecs; | |
274 | ||
f5b999c0 | 275 | error = xfs_btree_read_bufl(mp, tp, bno, &bp, XFS_BMAP_BTREE_REF, |
68988114 DC |
276 | &xfs_bmbt_buf_ops); |
277 | if (error) | |
278 | return error; | |
279 | *count += 1; | |
280 | block = XFS_BUF_TO_BLOCK(bp); | |
281 | ||
282 | if (--level) { | |
283 | /* Not at node above leaves, count this level of nodes */ | |
284 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | |
285 | while (nextbno != NULLFSBLOCK) { | |
f5b999c0 | 286 | error = xfs_btree_read_bufl(mp, tp, nextbno, &nbp, |
68988114 DC |
287 | XFS_BMAP_BTREE_REF, |
288 | &xfs_bmbt_buf_ops); | |
289 | if (error) | |
290 | return error; | |
291 | *count += 1; | |
292 | nextblock = XFS_BUF_TO_BLOCK(nbp); | |
293 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); | |
294 | xfs_trans_brelse(tp, nbp); | |
295 | } | |
296 | ||
297 | /* Dive to the next level */ | |
298 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | |
299 | bno = be64_to_cpu(*pp); | |
e7f5d5ca DW |
300 | error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, nextents, |
301 | count); | |
302 | if (error) { | |
68988114 DC |
303 | xfs_trans_brelse(tp, bp); |
304 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", | |
305 | XFS_ERRLEVEL_LOW, mp); | |
2451337d | 306 | return -EFSCORRUPTED; |
68988114 DC |
307 | } |
308 | xfs_trans_brelse(tp, bp); | |
309 | } else { | |
310 | /* count all level 1 nodes and their leaves */ | |
311 | for (;;) { | |
312 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | |
313 | numrecs = be16_to_cpu(block->bb_numrecs); | |
e7f5d5ca | 314 | (*nextents) += numrecs; |
68988114 DC |
315 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
316 | xfs_trans_brelse(tp, bp); | |
317 | if (nextbno == NULLFSBLOCK) | |
318 | break; | |
319 | bno = nextbno; | |
f5b999c0 | 320 | error = xfs_btree_read_bufl(mp, tp, bno, &bp, |
68988114 DC |
321 | XFS_BMAP_BTREE_REF, |
322 | &xfs_bmbt_buf_ops); | |
323 | if (error) | |
324 | return error; | |
325 | *count += 1; | |
326 | block = XFS_BUF_TO_BLOCK(bp); | |
327 | } | |
328 | } | |
329 | return 0; | |
330 | } | |
331 | ||
332 | /* | |
d29cb3e4 DW |
333 | * Count fsblocks of the given fork. Delayed allocation extents are |
334 | * not counted towards the totals. | |
68988114 | 335 | */ |
e7f5d5ca | 336 | int |
68988114 | 337 | xfs_bmap_count_blocks( |
e7f5d5ca DW |
338 | struct xfs_trans *tp, |
339 | struct xfs_inode *ip, | |
340 | int whichfork, | |
341 | xfs_extnum_t *nextents, | |
342 | xfs_filblks_t *count) | |
68988114 | 343 | { |
e7f5d5ca DW |
344 | struct xfs_mount *mp; /* file system mount structure */ |
345 | __be64 *pp; /* pointer to block address */ | |
68988114 | 346 | struct xfs_btree_block *block; /* current btree block */ |
e7f5d5ca | 347 | struct xfs_ifork *ifp; /* fork structure */ |
68988114 | 348 | xfs_fsblock_t bno; /* block # of "block" */ |
68988114 | 349 | int level; /* btree level, for checking */ |
e7f5d5ca | 350 | int error; |
68988114 DC |
351 | |
352 | bno = NULLFSBLOCK; | |
353 | mp = ip->i_mount; | |
e7f5d5ca DW |
354 | *nextents = 0; |
355 | *count = 0; | |
68988114 | 356 | ifp = XFS_IFORK_PTR(ip, whichfork); |
e7f5d5ca | 357 | if (!ifp) |
68988114 | 358 | return 0; |
68988114 | 359 | |
e7f5d5ca DW |
360 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
361 | case XFS_DINODE_FMT_EXTENTS: | |
e17a5c6f | 362 | *nextents = xfs_bmap_count_leaves(ifp, count); |
e7f5d5ca DW |
363 | return 0; |
364 | case XFS_DINODE_FMT_BTREE: | |
365 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
366 | error = xfs_iread_extents(tp, ip, whichfork); | |
367 | if (error) | |
368 | return error; | |
369 | } | |
370 | ||
371 | /* | |
372 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. | |
373 | */ | |
374 | block = ifp->if_broot; | |
375 | level = be16_to_cpu(block->bb_level); | |
376 | ASSERT(level > 0); | |
377 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | |
378 | bno = be64_to_cpu(*pp); | |
379 | ASSERT(bno != NULLFSBLOCK); | |
380 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); | |
381 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | |
382 | ||
383 | error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, | |
384 | nextents, count); | |
385 | if (error) { | |
386 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", | |
387 | XFS_ERRLEVEL_LOW, mp); | |
388 | return -EFSCORRUPTED; | |
389 | } | |
390 | return 0; | |
68988114 DC |
391 | } |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
abbf9e8a CH |
396 | static int |
397 | xfs_getbmap_report_one( | |
398 | struct xfs_inode *ip, | |
399 | struct getbmapx *bmv, | |
232b5194 | 400 | struct kgetbmap *out, |
abbf9e8a CH |
401 | int64_t bmv_end, |
402 | struct xfs_bmbt_irec *got) | |
f86f4037 | 403 | { |
232b5194 | 404 | struct kgetbmap *p = out + bmv->bmv_entries; |
d392bc81 | 405 | bool shared = false; |
abbf9e8a | 406 | int error; |
f86f4037 | 407 | |
d392bc81 | 408 | error = xfs_reflink_trim_around_shared(ip, got, &shared); |
f86f4037 DW |
409 | if (error) |
410 | return error; | |
411 | ||
abbf9e8a CH |
412 | if (isnullstartblock(got->br_startblock) || |
413 | got->br_startblock == DELAYSTARTBLOCK) { | |
f86f4037 | 414 | /* |
abbf9e8a CH |
415 | * Delalloc extents that start beyond EOF can occur due to |
416 | * speculative EOF allocation when the delalloc extent is larger | |
417 | * than the largest freespace extent at conversion time. These | |
418 | * extents cannot be converted by data writeback, so can exist | |
419 | * here even if we are not supposed to be finding delalloc | |
420 | * extents. | |
f86f4037 | 421 | */ |
abbf9e8a CH |
422 | if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip))) |
423 | ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0); | |
424 | ||
425 | p->bmv_oflags |= BMV_OF_DELALLOC; | |
426 | p->bmv_block = -2; | |
f86f4037 | 427 | } else { |
abbf9e8a | 428 | p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock); |
f86f4037 DW |
429 | } |
430 | ||
abbf9e8a CH |
431 | if (got->br_state == XFS_EXT_UNWRITTEN && |
432 | (bmv->bmv_iflags & BMV_IF_PREALLOC)) | |
433 | p->bmv_oflags |= BMV_OF_PREALLOC; | |
434 | ||
435 | if (shared) | |
436 | p->bmv_oflags |= BMV_OF_SHARED; | |
437 | ||
438 | p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff); | |
439 | p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount); | |
440 | ||
441 | bmv->bmv_offset = p->bmv_offset + p->bmv_length; | |
442 | bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset); | |
443 | bmv->bmv_entries++; | |
f86f4037 DW |
444 | return 0; |
445 | } | |
446 | ||
abbf9e8a CH |
447 | static void |
448 | xfs_getbmap_report_hole( | |
449 | struct xfs_inode *ip, | |
450 | struct getbmapx *bmv, | |
232b5194 | 451 | struct kgetbmap *out, |
abbf9e8a CH |
452 | int64_t bmv_end, |
453 | xfs_fileoff_t bno, | |
454 | xfs_fileoff_t end) | |
455 | { | |
232b5194 | 456 | struct kgetbmap *p = out + bmv->bmv_entries; |
abbf9e8a CH |
457 | |
458 | if (bmv->bmv_iflags & BMV_IF_NO_HOLES) | |
459 | return; | |
460 | ||
461 | p->bmv_block = -1; | |
462 | p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno); | |
463 | p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno); | |
464 | ||
465 | bmv->bmv_offset = p->bmv_offset + p->bmv_length; | |
466 | bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset); | |
467 | bmv->bmv_entries++; | |
468 | } | |
469 | ||
470 | static inline bool | |
471 | xfs_getbmap_full( | |
472 | struct getbmapx *bmv) | |
473 | { | |
474 | return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1; | |
475 | } | |
476 | ||
477 | static bool | |
478 | xfs_getbmap_next_rec( | |
479 | struct xfs_bmbt_irec *rec, | |
480 | xfs_fileoff_t total_end) | |
481 | { | |
482 | xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount; | |
483 | ||
484 | if (end == total_end) | |
485 | return false; | |
486 | ||
487 | rec->br_startoff += rec->br_blockcount; | |
488 | if (!isnullstartblock(rec->br_startblock) && | |
489 | rec->br_startblock != DELAYSTARTBLOCK) | |
490 | rec->br_startblock += rec->br_blockcount; | |
491 | rec->br_blockcount = total_end - end; | |
492 | return true; | |
493 | } | |
494 | ||
68988114 DC |
495 | /* |
496 | * Get inode's extents as described in bmv, and format for output. | |
497 | * Calls formatter to fill the user's buffer until all extents | |
498 | * are mapped, until the passed-in bmv->bmv_count slots have | |
499 | * been filled, or until the formatter short-circuits the loop, | |
500 | * if it is tracking filled-in extents on its own. | |
501 | */ | |
502 | int /* error code */ | |
503 | xfs_getbmap( | |
232b5194 | 504 | struct xfs_inode *ip, |
68988114 | 505 | struct getbmapx *bmv, /* user bmap structure */ |
232b5194 | 506 | struct kgetbmap *out) |
68988114 | 507 | { |
abbf9e8a CH |
508 | struct xfs_mount *mp = ip->i_mount; |
509 | int iflags = bmv->bmv_iflags; | |
232b5194 | 510 | int whichfork, lock, error = 0; |
abbf9e8a CH |
511 | int64_t bmv_end, max_len; |
512 | xfs_fileoff_t bno, first_bno; | |
513 | struct xfs_ifork *ifp; | |
abbf9e8a CH |
514 | struct xfs_bmbt_irec got, rec; |
515 | xfs_filblks_t len; | |
b2b1712a | 516 | struct xfs_iext_cursor icur; |
68988114 | 517 | |
232b5194 CH |
518 | if (bmv->bmv_iflags & ~BMV_IF_VALID) |
519 | return -EINVAL; | |
f86f4037 DW |
520 | #ifndef DEBUG |
521 | /* Only allow CoW fork queries if we're debugging. */ | |
522 | if (iflags & BMV_IF_COWFORK) | |
523 | return -EINVAL; | |
524 | #endif | |
525 | if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK)) | |
526 | return -EINVAL; | |
527 | ||
abbf9e8a CH |
528 | if (bmv->bmv_length < -1) |
529 | return -EINVAL; | |
abbf9e8a CH |
530 | bmv->bmv_entries = 0; |
531 | if (bmv->bmv_length == 0) | |
532 | return 0; | |
533 | ||
f86f4037 DW |
534 | if (iflags & BMV_IF_ATTRFORK) |
535 | whichfork = XFS_ATTR_FORK; | |
536 | else if (iflags & BMV_IF_COWFORK) | |
537 | whichfork = XFS_COW_FORK; | |
538 | else | |
539 | whichfork = XFS_DATA_FORK; | |
abbf9e8a | 540 | ifp = XFS_IFORK_PTR(ip, whichfork); |
f86f4037 | 541 | |
abbf9e8a | 542 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
f86f4037 DW |
543 | switch (whichfork) { |
544 | case XFS_ATTR_FORK: | |
abbf9e8a CH |
545 | if (!XFS_IFORK_Q(ip)) |
546 | goto out_unlock_iolock; | |
68988114 | 547 | |
abbf9e8a CH |
548 | max_len = 1LL << 32; |
549 | lock = xfs_ilock_attr_map_shared(ip); | |
f86f4037 DW |
550 | break; |
551 | case XFS_COW_FORK: | |
abbf9e8a CH |
552 | /* No CoW fork? Just return */ |
553 | if (!ifp) | |
554 | goto out_unlock_iolock; | |
68988114 | 555 | |
abbf9e8a CH |
556 | if (xfs_get_cowextsz_hint(ip)) |
557 | max_len = mp->m_super->s_maxbytes; | |
558 | else | |
559 | max_len = XFS_ISIZE(ip); | |
68988114 | 560 | |
abbf9e8a CH |
561 | lock = XFS_ILOCK_SHARED; |
562 | xfs_ilock(ip, lock); | |
563 | break; | |
f86f4037 | 564 | case XFS_DATA_FORK: |
efa70be1 CH |
565 | if (!(iflags & BMV_IF_DELALLOC) && |
566 | (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) { | |
2451337d | 567 | error = filemap_write_and_wait(VFS_I(ip)->i_mapping); |
68988114 DC |
568 | if (error) |
569 | goto out_unlock_iolock; | |
efa70be1 CH |
570 | |
571 | /* | |
572 | * Even after flushing the inode, there can still be | |
573 | * delalloc blocks on the inode beyond EOF due to | |
574 | * speculative preallocation. These are not removed | |
575 | * until the release function is called or the inode | |
576 | * is inactivated. Hence we cannot assert here that | |
577 | * ip->i_delayed_blks == 0. | |
578 | */ | |
68988114 | 579 | } |
68988114 | 580 | |
abbf9e8a CH |
581 | if (xfs_get_extsz_hint(ip) || |
582 | (ip->i_d.di_flags & | |
583 | (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))) | |
584 | max_len = mp->m_super->s_maxbytes; | |
585 | else | |
586 | max_len = XFS_ISIZE(ip); | |
587 | ||
efa70be1 | 588 | lock = xfs_ilock_data_map_shared(ip); |
f86f4037 | 589 | break; |
efa70be1 | 590 | } |
68988114 | 591 | |
abbf9e8a CH |
592 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
593 | case XFS_DINODE_FMT_EXTENTS: | |
594 | case XFS_DINODE_FMT_BTREE: | |
595 | break; | |
596 | case XFS_DINODE_FMT_LOCAL: | |
597 | /* Local format inode forks report no extents. */ | |
68988114 | 598 | goto out_unlock_ilock; |
abbf9e8a CH |
599 | default: |
600 | error = -EINVAL; | |
601 | goto out_unlock_ilock; | |
602 | } | |
68988114 | 603 | |
abbf9e8a CH |
604 | if (bmv->bmv_length == -1) { |
605 | max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len)); | |
606 | bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset); | |
68988114 DC |
607 | } |
608 | ||
abbf9e8a | 609 | bmv_end = bmv->bmv_offset + bmv->bmv_length; |
68988114 | 610 | |
abbf9e8a CH |
611 | first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset); |
612 | len = XFS_BB_TO_FSB(mp, bmv->bmv_length); | |
68988114 | 613 | |
abbf9e8a CH |
614 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
615 | error = xfs_iread_extents(NULL, ip, whichfork); | |
616 | if (error) | |
617 | goto out_unlock_ilock; | |
618 | } | |
f86f4037 | 619 | |
b2b1712a | 620 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
abbf9e8a CH |
621 | /* |
622 | * Report a whole-file hole if the delalloc flag is set to | |
623 | * stay compatible with the old implementation. | |
624 | */ | |
625 | if (iflags & BMV_IF_DELALLOC) | |
626 | xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno, | |
627 | XFS_B_TO_FSB(mp, XFS_ISIZE(ip))); | |
628 | goto out_unlock_ilock; | |
629 | } | |
68988114 | 630 | |
abbf9e8a CH |
631 | while (!xfs_getbmap_full(bmv)) { |
632 | xfs_trim_extent(&got, first_bno, len); | |
68988114 | 633 | |
abbf9e8a CH |
634 | /* |
635 | * Report an entry for a hole if this extent doesn't directly | |
636 | * follow the previous one. | |
637 | */ | |
638 | if (got.br_startoff > bno) { | |
639 | xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno, | |
640 | got.br_startoff); | |
641 | if (xfs_getbmap_full(bmv)) | |
642 | break; | |
643 | } | |
68988114 | 644 | |
abbf9e8a CH |
645 | /* |
646 | * In order to report shared extents accurately, we report each | |
647 | * distinct shared / unshared part of a single bmbt record with | |
648 | * an individual getbmapx record. | |
649 | */ | |
650 | bno = got.br_startoff + got.br_blockcount; | |
651 | rec = got; | |
652 | do { | |
653 | error = xfs_getbmap_report_one(ip, bmv, out, bmv_end, | |
654 | &rec); | |
655 | if (error || xfs_getbmap_full(bmv)) | |
656 | goto out_unlock_ilock; | |
657 | } while (xfs_getbmap_next_rec(&rec, bno)); | |
658 | ||
b2b1712a | 659 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
abbf9e8a CH |
660 | xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip)); |
661 | ||
662 | out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST; | |
663 | ||
664 | if (whichfork != XFS_ATTR_FORK && bno < end && | |
665 | !xfs_getbmap_full(bmv)) { | |
666 | xfs_getbmap_report_hole(ip, bmv, out, bmv_end, | |
667 | bno, end); | |
c364b6d0 | 668 | } |
abbf9e8a | 669 | break; |
68988114 | 670 | } |
68988114 | 671 | |
abbf9e8a CH |
672 | if (bno >= first_bno + len) |
673 | break; | |
674 | } | |
675 | ||
676 | out_unlock_ilock: | |
01f4f327 | 677 | xfs_iunlock(ip, lock); |
abbf9e8a | 678 | out_unlock_iolock: |
68988114 | 679 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
68988114 DC |
680 | return error; |
681 | } | |
682 | ||
683 | /* | |
e2ac8363 CH |
684 | * Dead simple method of punching delalyed allocation blocks from a range in |
685 | * the inode. This will always punch out both the start and end blocks, even | |
686 | * if the ranges only partially overlap them, so it is up to the caller to | |
687 | * ensure that partial blocks are not passed in. | |
68988114 DC |
688 | */ |
689 | int | |
690 | xfs_bmap_punch_delalloc_range( | |
691 | struct xfs_inode *ip, | |
692 | xfs_fileoff_t start_fsb, | |
693 | xfs_fileoff_t length) | |
694 | { | |
e2ac8363 CH |
695 | struct xfs_ifork *ifp = &ip->i_df; |
696 | xfs_fileoff_t end_fsb = start_fsb + length; | |
697 | struct xfs_bmbt_irec got, del; | |
698 | struct xfs_iext_cursor icur; | |
68988114 DC |
699 | int error = 0; |
700 | ||
0065b541 | 701 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
68988114 | 702 | |
0065b541 | 703 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
e2ac8363 | 704 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got)) |
d4380177 | 705 | goto out_unlock; |
68988114 | 706 | |
e2ac8363 CH |
707 | while (got.br_startoff + got.br_blockcount > start_fsb) { |
708 | del = got; | |
709 | xfs_trim_extent(&del, start_fsb, length); | |
68988114 DC |
710 | |
711 | /* | |
e2ac8363 CH |
712 | * A delete can push the cursor forward. Step back to the |
713 | * previous extent on non-delalloc or extents outside the | |
714 | * target range. | |
68988114 | 715 | */ |
e2ac8363 CH |
716 | if (!del.br_blockcount || |
717 | !isnullstartblock(del.br_startblock)) { | |
718 | if (!xfs_iext_prev_extent(ifp, &icur, &got)) | |
719 | break; | |
720 | continue; | |
721 | } | |
68988114 | 722 | |
e2ac8363 CH |
723 | error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur, |
724 | &got, &del); | |
725 | if (error || !xfs_iext_get_extent(ifp, &icur, &got)) | |
726 | break; | |
727 | } | |
68988114 | 728 | |
d4380177 CH |
729 | out_unlock: |
730 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
68988114 DC |
731 | return error; |
732 | } | |
c24b5dfa DC |
733 | |
734 | /* | |
735 | * Test whether it is appropriate to check an inode for and free post EOF | |
736 | * blocks. The 'force' parameter determines whether we should also consider | |
737 | * regular files that are marked preallocated or append-only. | |
738 | */ | |
739 | bool | |
740 | xfs_can_free_eofblocks(struct xfs_inode *ip, bool force) | |
741 | { | |
742 | /* prealloc/delalloc exists only on regular files */ | |
c19b3b05 | 743 | if (!S_ISREG(VFS_I(ip)->i_mode)) |
c24b5dfa DC |
744 | return false; |
745 | ||
746 | /* | |
747 | * Zero sized files with no cached pages and delalloc blocks will not | |
748 | * have speculative prealloc/delalloc blocks to remove. | |
749 | */ | |
750 | if (VFS_I(ip)->i_size == 0 && | |
2667c6f9 | 751 | VFS_I(ip)->i_mapping->nrpages == 0 && |
c24b5dfa DC |
752 | ip->i_delayed_blks == 0) |
753 | return false; | |
754 | ||
755 | /* If we haven't read in the extent list, then don't do it now. */ | |
756 | if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) | |
757 | return false; | |
758 | ||
759 | /* | |
760 | * Do not free real preallocated or append-only files unless the file | |
761 | * has delalloc blocks and we are forced to remove them. | |
762 | */ | |
763 | if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) | |
764 | if (!force || ip->i_delayed_blks == 0) | |
765 | return false; | |
766 | ||
767 | return true; | |
768 | } | |
769 | ||
770 | /* | |
3b4683c2 BF |
771 | * This is called to free any blocks beyond eof. The caller must hold |
772 | * IOLOCK_EXCL unless we are in the inode reclaim path and have the only | |
773 | * reference to the inode. | |
c24b5dfa DC |
774 | */ |
775 | int | |
776 | xfs_free_eofblocks( | |
a36b9261 | 777 | struct xfs_inode *ip) |
c24b5dfa | 778 | { |
a36b9261 BF |
779 | struct xfs_trans *tp; |
780 | int error; | |
781 | xfs_fileoff_t end_fsb; | |
782 | xfs_fileoff_t last_fsb; | |
783 | xfs_filblks_t map_len; | |
784 | int nimaps; | |
785 | struct xfs_bmbt_irec imap; | |
786 | struct xfs_mount *mp = ip->i_mount; | |
787 | ||
c24b5dfa DC |
788 | /* |
789 | * Figure out if there are any blocks beyond the end | |
790 | * of the file. If not, then there is nothing to do. | |
791 | */ | |
792 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip)); | |
793 | last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes); | |
794 | if (last_fsb <= end_fsb) | |
795 | return 0; | |
796 | map_len = last_fsb - end_fsb; | |
797 | ||
798 | nimaps = 1; | |
799 | xfs_ilock(ip, XFS_ILOCK_SHARED); | |
800 | error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0); | |
801 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | |
802 | ||
a36b9261 BF |
803 | /* |
804 | * If there are blocks after the end of file, truncate the file to its | |
805 | * current size to free them up. | |
806 | */ | |
c24b5dfa DC |
807 | if (!error && (nimaps != 0) && |
808 | (imap.br_startblock != HOLESTARTBLOCK || | |
809 | ip->i_delayed_blks)) { | |
810 | /* | |
811 | * Attach the dquots to the inode up front. | |
812 | */ | |
c14cfcca | 813 | error = xfs_qm_dqattach(ip); |
c24b5dfa DC |
814 | if (error) |
815 | return error; | |
816 | ||
e4229d6b BF |
817 | /* wait on dio to ensure i_size has settled */ |
818 | inode_dio_wait(VFS_I(ip)); | |
819 | ||
253f4911 CH |
820 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, |
821 | &tp); | |
c24b5dfa DC |
822 | if (error) { |
823 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); | |
c24b5dfa DC |
824 | return error; |
825 | } | |
826 | ||
827 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
828 | xfs_trans_ijoin(tp, ip, 0); | |
829 | ||
830 | /* | |
831 | * Do not update the on-disk file size. If we update the | |
832 | * on-disk file size and then the system crashes before the | |
833 | * contents of the file are flushed to disk then the files | |
834 | * may be full of holes (ie NULL files bug). | |
835 | */ | |
4e529339 BF |
836 | error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK, |
837 | XFS_ISIZE(ip), XFS_BMAPI_NODISCARD); | |
c24b5dfa DC |
838 | if (error) { |
839 | /* | |
840 | * If we get an error at this point we simply don't | |
841 | * bother truncating the file. | |
842 | */ | |
4906e215 | 843 | xfs_trans_cancel(tp); |
c24b5dfa | 844 | } else { |
70393313 | 845 | error = xfs_trans_commit(tp); |
c24b5dfa DC |
846 | if (!error) |
847 | xfs_inode_clear_eofblocks_tag(ip); | |
848 | } | |
849 | ||
850 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
c24b5dfa DC |
851 | } |
852 | return error; | |
853 | } | |
854 | ||
83aee9e4 | 855 | int |
c24b5dfa | 856 | xfs_alloc_file_space( |
83aee9e4 | 857 | struct xfs_inode *ip, |
c24b5dfa DC |
858 | xfs_off_t offset, |
859 | xfs_off_t len, | |
5f8aca8b | 860 | int alloc_type) |
c24b5dfa DC |
861 | { |
862 | xfs_mount_t *mp = ip->i_mount; | |
863 | xfs_off_t count; | |
864 | xfs_filblks_t allocated_fsb; | |
865 | xfs_filblks_t allocatesize_fsb; | |
866 | xfs_extlen_t extsz, temp; | |
867 | xfs_fileoff_t startoffset_fsb; | |
e093c4be | 868 | xfs_fileoff_t endoffset_fsb; |
c24b5dfa DC |
869 | int nimaps; |
870 | int quota_flag; | |
871 | int rt; | |
872 | xfs_trans_t *tp; | |
873 | xfs_bmbt_irec_t imaps[1], *imapp; | |
c24b5dfa | 874 | uint qblocks, resblks, resrtextents; |
c24b5dfa DC |
875 | int error; |
876 | ||
877 | trace_xfs_alloc_file_space(ip); | |
878 | ||
879 | if (XFS_FORCED_SHUTDOWN(mp)) | |
2451337d | 880 | return -EIO; |
c24b5dfa | 881 | |
c14cfcca | 882 | error = xfs_qm_dqattach(ip); |
c24b5dfa DC |
883 | if (error) |
884 | return error; | |
885 | ||
886 | if (len <= 0) | |
2451337d | 887 | return -EINVAL; |
c24b5dfa DC |
888 | |
889 | rt = XFS_IS_REALTIME_INODE(ip); | |
890 | extsz = xfs_get_extsz_hint(ip); | |
891 | ||
892 | count = len; | |
893 | imapp = &imaps[0]; | |
894 | nimaps = 1; | |
895 | startoffset_fsb = XFS_B_TO_FSBT(mp, offset); | |
e093c4be MR |
896 | endoffset_fsb = XFS_B_TO_FSB(mp, offset + count); |
897 | allocatesize_fsb = endoffset_fsb - startoffset_fsb; | |
c24b5dfa DC |
898 | |
899 | /* | |
900 | * Allocate file space until done or until there is an error | |
901 | */ | |
902 | while (allocatesize_fsb && !error) { | |
903 | xfs_fileoff_t s, e; | |
904 | ||
905 | /* | |
906 | * Determine space reservations for data/realtime. | |
907 | */ | |
908 | if (unlikely(extsz)) { | |
909 | s = startoffset_fsb; | |
910 | do_div(s, extsz); | |
911 | s *= extsz; | |
912 | e = startoffset_fsb + allocatesize_fsb; | |
0703a8e1 DC |
913 | div_u64_rem(startoffset_fsb, extsz, &temp); |
914 | if (temp) | |
c24b5dfa | 915 | e += temp; |
0703a8e1 DC |
916 | div_u64_rem(e, extsz, &temp); |
917 | if (temp) | |
c24b5dfa DC |
918 | e += extsz - temp; |
919 | } else { | |
920 | s = 0; | |
921 | e = allocatesize_fsb; | |
922 | } | |
923 | ||
924 | /* | |
925 | * The transaction reservation is limited to a 32-bit block | |
926 | * count, hence we need to limit the number of blocks we are | |
927 | * trying to reserve to avoid an overflow. We can't allocate | |
928 | * more than @nimaps extents, and an extent is limited on disk | |
929 | * to MAXEXTLEN (21 bits), so use that to enforce the limit. | |
930 | */ | |
931 | resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps)); | |
932 | if (unlikely(rt)) { | |
933 | resrtextents = qblocks = resblks; | |
934 | resrtextents /= mp->m_sb.sb_rextsize; | |
935 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); | |
936 | quota_flag = XFS_QMOPT_RES_RTBLKS; | |
937 | } else { | |
938 | resrtextents = 0; | |
939 | resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks); | |
940 | quota_flag = XFS_QMOPT_RES_REGBLKS; | |
941 | } | |
942 | ||
943 | /* | |
944 | * Allocate and setup the transaction. | |
945 | */ | |
253f4911 CH |
946 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, |
947 | resrtextents, 0, &tp); | |
948 | ||
c24b5dfa DC |
949 | /* |
950 | * Check for running out of space | |
951 | */ | |
952 | if (error) { | |
953 | /* | |
954 | * Free the transaction structure. | |
955 | */ | |
2451337d | 956 | ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp)); |
c24b5dfa DC |
957 | break; |
958 | } | |
959 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
960 | error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, | |
961 | 0, quota_flag); | |
962 | if (error) | |
963 | goto error1; | |
964 | ||
965 | xfs_trans_ijoin(tp, ip, 0); | |
966 | ||
c24b5dfa | 967 | error = xfs_bmapi_write(tp, ip, startoffset_fsb, |
da781e64 BF |
968 | allocatesize_fsb, alloc_type, 0, imapp, |
969 | &nimaps); | |
f6106efa | 970 | if (error) |
c24b5dfa | 971 | goto error0; |
c24b5dfa DC |
972 | |
973 | /* | |
974 | * Complete the transaction | |
975 | */ | |
70393313 | 976 | error = xfs_trans_commit(tp); |
c24b5dfa | 977 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
f6106efa | 978 | if (error) |
c24b5dfa | 979 | break; |
c24b5dfa DC |
980 | |
981 | allocated_fsb = imapp->br_blockcount; | |
982 | ||
983 | if (nimaps == 0) { | |
2451337d | 984 | error = -ENOSPC; |
c24b5dfa DC |
985 | break; |
986 | } | |
987 | ||
988 | startoffset_fsb += allocated_fsb; | |
989 | allocatesize_fsb -= allocated_fsb; | |
990 | } | |
991 | ||
992 | return error; | |
993 | ||
c8eac49e | 994 | error0: /* unlock inode, unreserve quota blocks, cancel trans */ |
c24b5dfa DC |
995 | xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag); |
996 | ||
997 | error1: /* Just cancel transaction */ | |
4906e215 | 998 | xfs_trans_cancel(tp); |
c24b5dfa DC |
999 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
1000 | return error; | |
1001 | } | |
1002 | ||
bdb0d04f CH |
1003 | static int |
1004 | xfs_unmap_extent( | |
1005 | struct xfs_inode *ip, | |
1006 | xfs_fileoff_t startoffset_fsb, | |
1007 | xfs_filblks_t len_fsb, | |
1008 | int *done) | |
c24b5dfa | 1009 | { |
bdb0d04f CH |
1010 | struct xfs_mount *mp = ip->i_mount; |
1011 | struct xfs_trans *tp; | |
bdb0d04f CH |
1012 | uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
1013 | int error; | |
c24b5dfa | 1014 | |
bdb0d04f CH |
1015 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); |
1016 | if (error) { | |
1017 | ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp)); | |
1018 | return error; | |
1019 | } | |
c24b5dfa | 1020 | |
bdb0d04f CH |
1021 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
1022 | error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot, | |
1023 | ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS); | |
1024 | if (error) | |
1025 | goto out_trans_cancel; | |
c24b5dfa | 1026 | |
bdb0d04f | 1027 | xfs_trans_ijoin(tp, ip, 0); |
4f317369 | 1028 | |
2af52842 | 1029 | error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done); |
bdb0d04f | 1030 | if (error) |
c8eac49e | 1031 | goto out_trans_cancel; |
4f317369 | 1032 | |
bdb0d04f CH |
1033 | error = xfs_trans_commit(tp); |
1034 | out_unlock: | |
1035 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1036 | return error; | |
4f69f578 | 1037 | |
bdb0d04f CH |
1038 | out_trans_cancel: |
1039 | xfs_trans_cancel(tp); | |
1040 | goto out_unlock; | |
1041 | } | |
4f69f578 | 1042 | |
2c307174 | 1043 | int |
bdb0d04f CH |
1044 | xfs_flush_unmap_range( |
1045 | struct xfs_inode *ip, | |
1046 | xfs_off_t offset, | |
1047 | xfs_off_t len) | |
1048 | { | |
1049 | struct xfs_mount *mp = ip->i_mount; | |
1050 | struct inode *inode = VFS_I(ip); | |
1051 | xfs_off_t rounding, start, end; | |
1052 | int error; | |
1053 | ||
1054 | /* wait for the completion of any pending DIOs */ | |
1055 | inode_dio_wait(inode); | |
1056 | ||
1057 | rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE); | |
1058 | start = round_down(offset, rounding); | |
1059 | end = round_up(offset + len, rounding) - 1; | |
1060 | ||
1061 | error = filemap_write_and_wait_range(inode->i_mapping, start, end); | |
1062 | if (error) | |
1063 | return error; | |
1064 | truncate_pagecache_range(inode, start, end); | |
1065 | return 0; | |
c24b5dfa DC |
1066 | } |
1067 | ||
83aee9e4 | 1068 | int |
c24b5dfa | 1069 | xfs_free_file_space( |
83aee9e4 | 1070 | struct xfs_inode *ip, |
c24b5dfa | 1071 | xfs_off_t offset, |
5f8aca8b | 1072 | xfs_off_t len) |
c24b5dfa | 1073 | { |
bdb0d04f | 1074 | struct xfs_mount *mp = ip->i_mount; |
c24b5dfa | 1075 | xfs_fileoff_t startoffset_fsb; |
bdb0d04f | 1076 | xfs_fileoff_t endoffset_fsb; |
3c2bdc91 | 1077 | int done = 0, error; |
c24b5dfa DC |
1078 | |
1079 | trace_xfs_free_file_space(ip); | |
1080 | ||
c14cfcca | 1081 | error = xfs_qm_dqattach(ip); |
c24b5dfa DC |
1082 | if (error) |
1083 | return error; | |
1084 | ||
c24b5dfa | 1085 | if (len <= 0) /* if nothing being freed */ |
bdb0d04f | 1086 | return 0; |
c24b5dfa | 1087 | |
bdb0d04f | 1088 | error = xfs_flush_unmap_range(ip, offset, len); |
c24b5dfa | 1089 | if (error) |
bdb0d04f CH |
1090 | return error; |
1091 | ||
1092 | startoffset_fsb = XFS_B_TO_FSB(mp, offset); | |
1093 | endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len); | |
c24b5dfa DC |
1094 | |
1095 | /* | |
daa79bae | 1096 | * Need to zero the stuff we're not freeing, on disk. |
c24b5dfa | 1097 | */ |
3c2bdc91 CH |
1098 | if (endoffset_fsb > startoffset_fsb) { |
1099 | while (!done) { | |
1100 | error = xfs_unmap_extent(ip, startoffset_fsb, | |
1101 | endoffset_fsb - startoffset_fsb, &done); | |
1102 | if (error) | |
1103 | return error; | |
c24b5dfa | 1104 | } |
c24b5dfa DC |
1105 | } |
1106 | ||
3c2bdc91 CH |
1107 | /* |
1108 | * Now that we've unmap all full blocks we'll have to zero out any | |
f5c54717 CH |
1109 | * partial block at the beginning and/or end. iomap_zero_range is smart |
1110 | * enough to skip any holes, including those we just created, but we | |
1111 | * must take care not to zero beyond EOF and enlarge i_size. | |
3c2bdc91 | 1112 | */ |
3dd09d5a CO |
1113 | if (offset >= XFS_ISIZE(ip)) |
1114 | return 0; | |
3dd09d5a CO |
1115 | if (offset + len > XFS_ISIZE(ip)) |
1116 | len = XFS_ISIZE(ip) - offset; | |
f150b423 CH |
1117 | error = iomap_zero_range(VFS_I(ip), offset, len, NULL, |
1118 | &xfs_buffered_write_iomap_ops); | |
e53c4b59 DW |
1119 | if (error) |
1120 | return error; | |
1121 | ||
1122 | /* | |
1123 | * If we zeroed right up to EOF and EOF straddles a page boundary we | |
1124 | * must make sure that the post-EOF area is also zeroed because the | |
1125 | * page could be mmap'd and iomap_zero_range doesn't do that for us. | |
1126 | * Writeback of the eof page will do this, albeit clumsily. | |
1127 | */ | |
a579121f | 1128 | if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) { |
e53c4b59 | 1129 | error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, |
a579121f | 1130 | round_down(offset + len, PAGE_SIZE), LLONG_MAX); |
e53c4b59 DW |
1131 | } |
1132 | ||
1133 | return error; | |
c24b5dfa DC |
1134 | } |
1135 | ||
72c1a739 | 1136 | static int |
4ed36c6b CH |
1137 | xfs_prepare_shift( |
1138 | struct xfs_inode *ip, | |
1139 | loff_t offset) | |
e1d8fb88 | 1140 | { |
e1d8fb88 | 1141 | int error; |
e1d8fb88 | 1142 | |
f71721d0 BF |
1143 | /* |
1144 | * Trim eofblocks to avoid shifting uninitialized post-eof preallocation | |
1145 | * into the accessible region of the file. | |
1146 | */ | |
41b9d726 | 1147 | if (xfs_can_free_eofblocks(ip, true)) { |
a36b9261 | 1148 | error = xfs_free_eofblocks(ip); |
41b9d726 BF |
1149 | if (error) |
1150 | return error; | |
1151 | } | |
1669a8ca | 1152 | |
f71721d0 BF |
1153 | /* |
1154 | * Writeback and invalidate cache for the remainder of the file as we're | |
a904b1ca | 1155 | * about to shift down every extent from offset to EOF. |
f71721d0 | 1156 | */ |
7f9f71be | 1157 | error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip)); |
1749d1ea BF |
1158 | if (error) |
1159 | return error; | |
e1d8fb88 | 1160 | |
a904b1ca | 1161 | /* |
3af423b0 DW |
1162 | * Clean out anything hanging around in the cow fork now that |
1163 | * we've flushed all the dirty data out to disk to avoid having | |
1164 | * CoW extents at the wrong offsets. | |
1165 | */ | |
51d62690 | 1166 | if (xfs_inode_has_cow_data(ip)) { |
3af423b0 DW |
1167 | error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF, |
1168 | true); | |
1169 | if (error) | |
1170 | return error; | |
1171 | } | |
1172 | ||
4ed36c6b CH |
1173 | return 0; |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * xfs_collapse_file_space() | |
1178 | * This routine frees disk space and shift extent for the given file. | |
1179 | * The first thing we do is to free data blocks in the specified range | |
1180 | * by calling xfs_free_file_space(). It would also sync dirty data | |
1181 | * and invalidate page cache over the region on which collapse range | |
1182 | * is working. And Shift extent records to the left to cover a hole. | |
1183 | * RETURNS: | |
1184 | * 0 on success | |
1185 | * errno on error | |
1186 | * | |
1187 | */ | |
1188 | int | |
1189 | xfs_collapse_file_space( | |
1190 | struct xfs_inode *ip, | |
1191 | xfs_off_t offset, | |
1192 | xfs_off_t len) | |
1193 | { | |
4ed36c6b CH |
1194 | struct xfs_mount *mp = ip->i_mount; |
1195 | struct xfs_trans *tp; | |
1196 | int error; | |
4ed36c6b CH |
1197 | xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len); |
1198 | xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len); | |
1199 | uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); | |
ecfea3f0 | 1200 | bool done = false; |
4ed36c6b CH |
1201 | |
1202 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); | |
9ad1a23a CH |
1203 | ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); |
1204 | ||
4ed36c6b CH |
1205 | trace_xfs_collapse_file_space(ip); |
1206 | ||
1207 | error = xfs_free_file_space(ip, offset, len); | |
1208 | if (error) | |
1209 | return error; | |
1210 | ||
1211 | error = xfs_prepare_shift(ip, offset); | |
1212 | if (error) | |
1213 | return error; | |
a904b1ca | 1214 | |
e1d8fb88 | 1215 | while (!error && !done) { |
48af96ab BF |
1216 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, |
1217 | &tp); | |
253f4911 | 1218 | if (error) |
e1d8fb88 | 1219 | break; |
e1d8fb88 NJ |
1220 | |
1221 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
1222 | error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, | |
48af96ab | 1223 | ip->i_gdquot, ip->i_pdquot, resblks, 0, |
e1d8fb88 NJ |
1224 | XFS_QMOPT_RES_REGBLKS); |
1225 | if (error) | |
d4a97a04 | 1226 | goto out_trans_cancel; |
a904b1ca | 1227 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
e1d8fb88 | 1228 | |
ecfea3f0 | 1229 | error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb, |
333f950c | 1230 | &done); |
e1d8fb88 | 1231 | if (error) |
c8eac49e | 1232 | goto out_trans_cancel; |
e1d8fb88 | 1233 | |
70393313 | 1234 | error = xfs_trans_commit(tp); |
e1d8fb88 NJ |
1235 | } |
1236 | ||
1237 | return error; | |
1238 | ||
d4a97a04 | 1239 | out_trans_cancel: |
4906e215 | 1240 | xfs_trans_cancel(tp); |
e1d8fb88 NJ |
1241 | return error; |
1242 | } | |
1243 | ||
a904b1ca NJ |
1244 | /* |
1245 | * xfs_insert_file_space() | |
1246 | * This routine create hole space by shifting extents for the given file. | |
1247 | * The first thing we do is to sync dirty data and invalidate page cache | |
1248 | * over the region on which insert range is working. And split an extent | |
1249 | * to two extents at given offset by calling xfs_bmap_split_extent. | |
1250 | * And shift all extent records which are laying between [offset, | |
1251 | * last allocated extent] to the right to reserve hole range. | |
1252 | * RETURNS: | |
1253 | * 0 on success | |
1254 | * errno on error | |
1255 | */ | |
1256 | int | |
1257 | xfs_insert_file_space( | |
1258 | struct xfs_inode *ip, | |
1259 | loff_t offset, | |
1260 | loff_t len) | |
1261 | { | |
4ed36c6b CH |
1262 | struct xfs_mount *mp = ip->i_mount; |
1263 | struct xfs_trans *tp; | |
1264 | int error; | |
4ed36c6b CH |
1265 | xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset); |
1266 | xfs_fileoff_t next_fsb = NULLFSBLOCK; | |
1267 | xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len); | |
ecfea3f0 | 1268 | bool done = false; |
4ed36c6b | 1269 | |
a904b1ca | 1270 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
9ad1a23a CH |
1271 | ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL)); |
1272 | ||
a904b1ca NJ |
1273 | trace_xfs_insert_file_space(ip); |
1274 | ||
f62cb48e DW |
1275 | error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb); |
1276 | if (error) | |
1277 | return error; | |
1278 | ||
4ed36c6b CH |
1279 | error = xfs_prepare_shift(ip, offset); |
1280 | if (error) | |
1281 | return error; | |
1282 | ||
1283 | /* | |
1284 | * The extent shifting code works on extent granularity. So, if stop_fsb | |
1285 | * is not the starting block of extent, we need to split the extent at | |
1286 | * stop_fsb. | |
1287 | */ | |
1288 | error = xfs_bmap_split_extent(ip, stop_fsb); | |
1289 | if (error) | |
1290 | return error; | |
1291 | ||
1292 | while (!error && !done) { | |
1293 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, | |
1294 | &tp); | |
1295 | if (error) | |
1296 | break; | |
1297 | ||
1298 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
1299 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
ecfea3f0 | 1300 | error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb, |
333f950c | 1301 | &done, stop_fsb); |
4ed36c6b | 1302 | if (error) |
c8eac49e | 1303 | goto out_trans_cancel; |
4ed36c6b | 1304 | |
4ed36c6b CH |
1305 | error = xfs_trans_commit(tp); |
1306 | } | |
1307 | ||
1308 | return error; | |
1309 | ||
c8eac49e | 1310 | out_trans_cancel: |
4ed36c6b CH |
1311 | xfs_trans_cancel(tp); |
1312 | return error; | |
a904b1ca NJ |
1313 | } |
1314 | ||
a133d952 DC |
1315 | /* |
1316 | * We need to check that the format of the data fork in the temporary inode is | |
1317 | * valid for the target inode before doing the swap. This is not a problem with | |
1318 | * attr1 because of the fixed fork offset, but attr2 has a dynamically sized | |
1319 | * data fork depending on the space the attribute fork is taking so we can get | |
1320 | * invalid formats on the target inode. | |
1321 | * | |
1322 | * E.g. target has space for 7 extents in extent format, temp inode only has | |
1323 | * space for 6. If we defragment down to 7 extents, then the tmp format is a | |
1324 | * btree, but when swapped it needs to be in extent format. Hence we can't just | |
1325 | * blindly swap data forks on attr2 filesystems. | |
1326 | * | |
1327 | * Note that we check the swap in both directions so that we don't end up with | |
1328 | * a corrupt temporary inode, either. | |
1329 | * | |
1330 | * Note that fixing the way xfs_fsr sets up the attribute fork in the source | |
1331 | * inode will prevent this situation from occurring, so all we do here is | |
1332 | * reject and log the attempt. basically we are putting the responsibility on | |
1333 | * userspace to get this right. | |
1334 | */ | |
1335 | static int | |
1336 | xfs_swap_extents_check_format( | |
e06259aa DW |
1337 | struct xfs_inode *ip, /* target inode */ |
1338 | struct xfs_inode *tip) /* tmp inode */ | |
a133d952 DC |
1339 | { |
1340 | ||
1341 | /* Should never get a local format */ | |
1342 | if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL || | |
1343 | tip->i_d.di_format == XFS_DINODE_FMT_LOCAL) | |
2451337d | 1344 | return -EINVAL; |
a133d952 DC |
1345 | |
1346 | /* | |
1347 | * if the target inode has less extents that then temporary inode then | |
1348 | * why did userspace call us? | |
1349 | */ | |
1350 | if (ip->i_d.di_nextents < tip->i_d.di_nextents) | |
2451337d | 1351 | return -EINVAL; |
a133d952 | 1352 | |
1f08af52 DW |
1353 | /* |
1354 | * If we have to use the (expensive) rmap swap method, we can | |
1355 | * handle any number of extents and any format. | |
1356 | */ | |
1357 | if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb)) | |
1358 | return 0; | |
1359 | ||
a133d952 DC |
1360 | /* |
1361 | * if the target inode is in extent form and the temp inode is in btree | |
1362 | * form then we will end up with the target inode in the wrong format | |
1363 | * as we already know there are less extents in the temp inode. | |
1364 | */ | |
1365 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && | |
1366 | tip->i_d.di_format == XFS_DINODE_FMT_BTREE) | |
2451337d | 1367 | return -EINVAL; |
a133d952 DC |
1368 | |
1369 | /* Check temp in extent form to max in target */ | |
1370 | if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && | |
1371 | XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) > | |
1372 | XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) | |
2451337d | 1373 | return -EINVAL; |
a133d952 DC |
1374 | |
1375 | /* Check target in extent form to max in temp */ | |
1376 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && | |
1377 | XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > | |
1378 | XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) | |
2451337d | 1379 | return -EINVAL; |
a133d952 DC |
1380 | |
1381 | /* | |
1382 | * If we are in a btree format, check that the temp root block will fit | |
1383 | * in the target and that it has enough extents to be in btree format | |
1384 | * in the target. | |
1385 | * | |
1386 | * Note that we have to be careful to allow btree->extent conversions | |
1387 | * (a common defrag case) which will occur when the temp inode is in | |
1388 | * extent format... | |
1389 | */ | |
1390 | if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) { | |
0cbe48cc | 1391 | if (XFS_IFORK_Q(ip) && |
a133d952 | 1392 | XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip)) |
2451337d | 1393 | return -EINVAL; |
a133d952 DC |
1394 | if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <= |
1395 | XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)) | |
2451337d | 1396 | return -EINVAL; |
a133d952 DC |
1397 | } |
1398 | ||
1399 | /* Reciprocal target->temp btree format checks */ | |
1400 | if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) { | |
0cbe48cc | 1401 | if (XFS_IFORK_Q(tip) && |
a133d952 | 1402 | XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip)) |
2451337d | 1403 | return -EINVAL; |
a133d952 DC |
1404 | if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <= |
1405 | XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK)) | |
2451337d | 1406 | return -EINVAL; |
a133d952 DC |
1407 | } |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
7abbb8f9 | 1412 | static int |
4ef897a2 DC |
1413 | xfs_swap_extent_flush( |
1414 | struct xfs_inode *ip) | |
1415 | { | |
1416 | int error; | |
1417 | ||
1418 | error = filemap_write_and_wait(VFS_I(ip)->i_mapping); | |
1419 | if (error) | |
1420 | return error; | |
1421 | truncate_pagecache_range(VFS_I(ip), 0, -1); | |
1422 | ||
1423 | /* Verify O_DIRECT for ftmp */ | |
1424 | if (VFS_I(ip)->i_mapping->nrpages) | |
1425 | return -EINVAL; | |
4ef897a2 DC |
1426 | return 0; |
1427 | } | |
1428 | ||
1f08af52 DW |
1429 | /* |
1430 | * Move extents from one file to another, when rmap is enabled. | |
1431 | */ | |
1432 | STATIC int | |
1433 | xfs_swap_extent_rmap( | |
1434 | struct xfs_trans **tpp, | |
1435 | struct xfs_inode *ip, | |
1436 | struct xfs_inode *tip) | |
1437 | { | |
7a7943c7 | 1438 | struct xfs_trans *tp = *tpp; |
1f08af52 DW |
1439 | struct xfs_bmbt_irec irec; |
1440 | struct xfs_bmbt_irec uirec; | |
1441 | struct xfs_bmbt_irec tirec; | |
1442 | xfs_fileoff_t offset_fsb; | |
1443 | xfs_fileoff_t end_fsb; | |
1444 | xfs_filblks_t count_fsb; | |
1f08af52 DW |
1445 | int error; |
1446 | xfs_filblks_t ilen; | |
1447 | xfs_filblks_t rlen; | |
1448 | int nimaps; | |
c8ce540d | 1449 | uint64_t tip_flags2; |
1f08af52 DW |
1450 | |
1451 | /* | |
1452 | * If the source file has shared blocks, we must flag the donor | |
1453 | * file as having shared blocks so that we get the shared-block | |
1454 | * rmap functions when we go to fix up the rmaps. The flags | |
1455 | * will be switch for reals later. | |
1456 | */ | |
1457 | tip_flags2 = tip->i_d.di_flags2; | |
1458 | if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) | |
1459 | tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK; | |
1460 | ||
1461 | offset_fsb = 0; | |
1462 | end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip))); | |
1463 | count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb); | |
1464 | ||
1465 | while (count_fsb) { | |
1466 | /* Read extent from the donor file */ | |
1467 | nimaps = 1; | |
1468 | error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec, | |
1469 | &nimaps, 0); | |
1470 | if (error) | |
1471 | goto out; | |
1472 | ASSERT(nimaps == 1); | |
1473 | ASSERT(tirec.br_startblock != DELAYSTARTBLOCK); | |
1474 | ||
1475 | trace_xfs_swap_extent_rmap_remap(tip, &tirec); | |
1476 | ilen = tirec.br_blockcount; | |
1477 | ||
1478 | /* Unmap the old blocks in the source file. */ | |
1479 | while (tirec.br_blockcount) { | |
c8eac49e | 1480 | ASSERT(tp->t_firstblock == NULLFSBLOCK); |
1f08af52 DW |
1481 | trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec); |
1482 | ||
1483 | /* Read extent from the source file */ | |
1484 | nimaps = 1; | |
1485 | error = xfs_bmapi_read(ip, tirec.br_startoff, | |
1486 | tirec.br_blockcount, &irec, | |
1487 | &nimaps, 0); | |
1488 | if (error) | |
d5a2e289 | 1489 | goto out; |
1f08af52 DW |
1490 | ASSERT(nimaps == 1); |
1491 | ASSERT(tirec.br_startoff == irec.br_startoff); | |
1492 | trace_xfs_swap_extent_rmap_remap_piece(ip, &irec); | |
1493 | ||
1494 | /* Trim the extent. */ | |
1495 | uirec = tirec; | |
1496 | uirec.br_blockcount = rlen = min_t(xfs_filblks_t, | |
1497 | tirec.br_blockcount, | |
1498 | irec.br_blockcount); | |
1499 | trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec); | |
1500 | ||
1501 | /* Remove the mapping from the donor file. */ | |
3e08f42a | 1502 | xfs_bmap_unmap_extent(tp, tip, &uirec); |
1f08af52 DW |
1503 | |
1504 | /* Remove the mapping from the source file. */ | |
3e08f42a | 1505 | xfs_bmap_unmap_extent(tp, ip, &irec); |
1f08af52 DW |
1506 | |
1507 | /* Map the donor file's blocks into the source file. */ | |
3e08f42a | 1508 | xfs_bmap_map_extent(tp, ip, &uirec); |
1f08af52 DW |
1509 | |
1510 | /* Map the source file's blocks into the donor file. */ | |
3e08f42a | 1511 | xfs_bmap_map_extent(tp, tip, &irec); |
1f08af52 | 1512 | |
9e28a242 | 1513 | error = xfs_defer_finish(tpp); |
7a7943c7 | 1514 | tp = *tpp; |
1f08af52 | 1515 | if (error) |
9b1f4e98 | 1516 | goto out; |
1f08af52 DW |
1517 | |
1518 | tirec.br_startoff += rlen; | |
1519 | if (tirec.br_startblock != HOLESTARTBLOCK && | |
1520 | tirec.br_startblock != DELAYSTARTBLOCK) | |
1521 | tirec.br_startblock += rlen; | |
1522 | tirec.br_blockcount -= rlen; | |
1523 | } | |
1524 | ||
1525 | /* Roll on... */ | |
1526 | count_fsb -= ilen; | |
1527 | offset_fsb += ilen; | |
1528 | } | |
1529 | ||
1530 | tip->i_d.di_flags2 = tip_flags2; | |
1531 | return 0; | |
1532 | ||
1f08af52 DW |
1533 | out: |
1534 | trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_); | |
1535 | tip->i_d.di_flags2 = tip_flags2; | |
1536 | return error; | |
1537 | } | |
1538 | ||
39aff5fd DW |
1539 | /* Swap the extents of two files by swapping data forks. */ |
1540 | STATIC int | |
1541 | xfs_swap_extent_forks( | |
1542 | struct xfs_trans *tp, | |
1543 | struct xfs_inode *ip, | |
1544 | struct xfs_inode *tip, | |
1545 | int *src_log_flags, | |
1546 | int *target_log_flags) | |
a133d952 | 1547 | { |
e7f5d5ca DW |
1548 | xfs_filblks_t aforkblks = 0; |
1549 | xfs_filblks_t taforkblks = 0; | |
1550 | xfs_extnum_t junk; | |
c8ce540d | 1551 | uint64_t tmp; |
39aff5fd | 1552 | int error; |
a133d952 | 1553 | |
a133d952 DC |
1554 | /* |
1555 | * Count the number of extended attribute blocks | |
1556 | */ | |
1557 | if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) && | |
1558 | (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { | |
e7f5d5ca | 1559 | error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk, |
39aff5fd | 1560 | &aforkblks); |
a133d952 | 1561 | if (error) |
39aff5fd | 1562 | return error; |
a133d952 DC |
1563 | } |
1564 | if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) && | |
1565 | (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) { | |
e7f5d5ca | 1566 | error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk, |
39aff5fd | 1567 | &taforkblks); |
a133d952 | 1568 | if (error) |
39aff5fd | 1569 | return error; |
a133d952 DC |
1570 | } |
1571 | ||
21b5c978 | 1572 | /* |
6fb10d6d BF |
1573 | * Btree format (v3) inodes have the inode number stamped in the bmbt |
1574 | * block headers. We can't start changing the bmbt blocks until the | |
1575 | * inode owner change is logged so recovery does the right thing in the | |
1576 | * event of a crash. Set the owner change log flags now and leave the | |
1577 | * bmbt scan as the last step. | |
21b5c978 | 1578 | */ |
21b5c978 | 1579 | if (ip->i_d.di_version == 3 && |
6fb10d6d | 1580 | ip->i_d.di_format == XFS_DINODE_FMT_BTREE) |
39aff5fd | 1581 | (*target_log_flags) |= XFS_ILOG_DOWNER; |
21b5c978 | 1582 | if (tip->i_d.di_version == 3 && |
6fb10d6d | 1583 | tip->i_d.di_format == XFS_DINODE_FMT_BTREE) |
39aff5fd | 1584 | (*src_log_flags) |= XFS_ILOG_DOWNER; |
21b5c978 | 1585 | |
a133d952 DC |
1586 | /* |
1587 | * Swap the data forks of the inodes | |
1588 | */ | |
897992b7 | 1589 | swap(ip->i_df, tip->i_df); |
a133d952 DC |
1590 | |
1591 | /* | |
1592 | * Fix the on-disk inode values | |
1593 | */ | |
c8ce540d | 1594 | tmp = (uint64_t)ip->i_d.di_nblocks; |
a133d952 DC |
1595 | ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks; |
1596 | tip->i_d.di_nblocks = tmp + taforkblks - aforkblks; | |
1597 | ||
897992b7 GS |
1598 | swap(ip->i_d.di_nextents, tip->i_d.di_nextents); |
1599 | swap(ip->i_d.di_format, tip->i_d.di_format); | |
a133d952 DC |
1600 | |
1601 | /* | |
1602 | * The extents in the source inode could still contain speculative | |
1603 | * preallocation beyond EOF (e.g. the file is open but not modified | |
1604 | * while defrag is in progress). In that case, we need to copy over the | |
1605 | * number of delalloc blocks the data fork in the source inode is | |
1606 | * tracking beyond EOF so that when the fork is truncated away when the | |
1607 | * temporary inode is unlinked we don't underrun the i_delayed_blks | |
1608 | * counter on that inode. | |
1609 | */ | |
1610 | ASSERT(tip->i_delayed_blks == 0); | |
1611 | tip->i_delayed_blks = ip->i_delayed_blks; | |
1612 | ip->i_delayed_blks = 0; | |
1613 | ||
a133d952 DC |
1614 | switch (ip->i_d.di_format) { |
1615 | case XFS_DINODE_FMT_EXTENTS: | |
39aff5fd | 1616 | (*src_log_flags) |= XFS_ILOG_DEXT; |
a133d952 DC |
1617 | break; |
1618 | case XFS_DINODE_FMT_BTREE: | |
21b5c978 | 1619 | ASSERT(ip->i_d.di_version < 3 || |
39aff5fd DW |
1620 | (*src_log_flags & XFS_ILOG_DOWNER)); |
1621 | (*src_log_flags) |= XFS_ILOG_DBROOT; | |
a133d952 DC |
1622 | break; |
1623 | } | |
1624 | ||
a133d952 DC |
1625 | switch (tip->i_d.di_format) { |
1626 | case XFS_DINODE_FMT_EXTENTS: | |
39aff5fd | 1627 | (*target_log_flags) |= XFS_ILOG_DEXT; |
a133d952 DC |
1628 | break; |
1629 | case XFS_DINODE_FMT_BTREE: | |
39aff5fd | 1630 | (*target_log_flags) |= XFS_ILOG_DBROOT; |
21b5c978 | 1631 | ASSERT(tip->i_d.di_version < 3 || |
39aff5fd | 1632 | (*target_log_flags & XFS_ILOG_DOWNER)); |
a133d952 DC |
1633 | break; |
1634 | } | |
1635 | ||
39aff5fd DW |
1636 | return 0; |
1637 | } | |
1638 | ||
2dd3d709 BF |
1639 | /* |
1640 | * Fix up the owners of the bmbt blocks to refer to the current inode. The | |
1641 | * change owner scan attempts to order all modified buffers in the current | |
1642 | * transaction. In the event of ordered buffer failure, the offending buffer is | |
1643 | * physically logged as a fallback and the scan returns -EAGAIN. We must roll | |
1644 | * the transaction in this case to replenish the fallback log reservation and | |
1645 | * restart the scan. This process repeats until the scan completes. | |
1646 | */ | |
1647 | static int | |
1648 | xfs_swap_change_owner( | |
1649 | struct xfs_trans **tpp, | |
1650 | struct xfs_inode *ip, | |
1651 | struct xfs_inode *tmpip) | |
1652 | { | |
1653 | int error; | |
1654 | struct xfs_trans *tp = *tpp; | |
1655 | ||
1656 | do { | |
1657 | error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino, | |
1658 | NULL); | |
1659 | /* success or fatal error */ | |
1660 | if (error != -EAGAIN) | |
1661 | break; | |
1662 | ||
1663 | error = xfs_trans_roll(tpp); | |
1664 | if (error) | |
1665 | break; | |
1666 | tp = *tpp; | |
1667 | ||
1668 | /* | |
1669 | * Redirty both inodes so they can relog and keep the log tail | |
1670 | * moving forward. | |
1671 | */ | |
1672 | xfs_trans_ijoin(tp, ip, 0); | |
1673 | xfs_trans_ijoin(tp, tmpip, 0); | |
1674 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
1675 | xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE); | |
1676 | } while (true); | |
1677 | ||
1678 | return error; | |
1679 | } | |
1680 | ||
39aff5fd DW |
1681 | int |
1682 | xfs_swap_extents( | |
1683 | struct xfs_inode *ip, /* target inode */ | |
1684 | struct xfs_inode *tip, /* tmp inode */ | |
1685 | struct xfs_swapext *sxp) | |
1686 | { | |
1687 | struct xfs_mount *mp = ip->i_mount; | |
1688 | struct xfs_trans *tp; | |
1689 | struct xfs_bstat *sbp = &sxp->sx_stat; | |
1690 | int src_log_flags, target_log_flags; | |
1691 | int error = 0; | |
1692 | int lock_flags; | |
c8ce540d | 1693 | uint64_t f; |
2dd3d709 | 1694 | int resblks = 0; |
39aff5fd DW |
1695 | |
1696 | /* | |
1697 | * Lock the inodes against other IO, page faults and truncate to | |
1698 | * begin with. Then we can ensure the inodes are flushed and have no | |
1699 | * page cache safely. Once we have done this we can take the ilocks and | |
1700 | * do the rest of the checks. | |
1701 | */ | |
65523218 CH |
1702 | lock_two_nondirectories(VFS_I(ip), VFS_I(tip)); |
1703 | lock_flags = XFS_MMAPLOCK_EXCL; | |
7c2d238a | 1704 | xfs_lock_two_inodes(ip, XFS_MMAPLOCK_EXCL, tip, XFS_MMAPLOCK_EXCL); |
39aff5fd DW |
1705 | |
1706 | /* Verify that both files have the same format */ | |
1707 | if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) { | |
1708 | error = -EINVAL; | |
1709 | goto out_unlock; | |
1710 | } | |
1711 | ||
1712 | /* Verify both files are either real-time or non-realtime */ | |
1713 | if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) { | |
1714 | error = -EINVAL; | |
1715 | goto out_unlock; | |
1716 | } | |
1717 | ||
1718 | error = xfs_swap_extent_flush(ip); | |
1719 | if (error) | |
1720 | goto out_unlock; | |
1721 | error = xfs_swap_extent_flush(tip); | |
1722 | if (error) | |
1723 | goto out_unlock; | |
1724 | ||
96987eea CH |
1725 | if (xfs_inode_has_cow_data(tip)) { |
1726 | error = xfs_reflink_cancel_cow_range(tip, 0, NULLFILEOFF, true); | |
1727 | if (error) | |
1728 | return error; | |
1729 | } | |
1730 | ||
1f08af52 DW |
1731 | /* |
1732 | * Extent "swapping" with rmap requires a permanent reservation and | |
1733 | * a block reservation because it's really just a remap operation | |
1734 | * performed with log redo items! | |
1735 | */ | |
1736 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) { | |
b3fed434 BF |
1737 | int w = XFS_DATA_FORK; |
1738 | uint32_t ipnext = XFS_IFORK_NEXTENTS(ip, w); | |
1739 | uint32_t tipnext = XFS_IFORK_NEXTENTS(tip, w); | |
1740 | ||
1741 | /* | |
1742 | * Conceptually this shouldn't affect the shape of either bmbt, | |
1743 | * but since we atomically move extents one by one, we reserve | |
1744 | * enough space to rebuild both trees. | |
1745 | */ | |
1746 | resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w); | |
1747 | resblks += XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w); | |
1748 | ||
1f08af52 | 1749 | /* |
b3fed434 BF |
1750 | * Handle the corner case where either inode might straddle the |
1751 | * btree format boundary. If so, the inode could bounce between | |
1752 | * btree <-> extent format on unmap -> remap cycles, freeing and | |
1753 | * allocating a bmapbt block each time. | |
1f08af52 | 1754 | */ |
b3fed434 BF |
1755 | if (ipnext == (XFS_IFORK_MAXEXT(ip, w) + 1)) |
1756 | resblks += XFS_IFORK_MAXEXT(ip, w); | |
1757 | if (tipnext == (XFS_IFORK_MAXEXT(tip, w) + 1)) | |
1758 | resblks += XFS_IFORK_MAXEXT(tip, w); | |
2dd3d709 BF |
1759 | } |
1760 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp); | |
39aff5fd DW |
1761 | if (error) |
1762 | goto out_unlock; | |
1763 | ||
1764 | /* | |
1765 | * Lock and join the inodes to the tansaction so that transaction commit | |
1766 | * or cancel will unlock the inodes from this point onwards. | |
1767 | */ | |
7c2d238a | 1768 | xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL); |
39aff5fd DW |
1769 | lock_flags |= XFS_ILOCK_EXCL; |
1770 | xfs_trans_ijoin(tp, ip, 0); | |
1771 | xfs_trans_ijoin(tp, tip, 0); | |
1772 | ||
1773 | ||
1774 | /* Verify all data are being swapped */ | |
1775 | if (sxp->sx_offset != 0 || | |
1776 | sxp->sx_length != ip->i_d.di_size || | |
1777 | sxp->sx_length != tip->i_d.di_size) { | |
1778 | error = -EFAULT; | |
1779 | goto out_trans_cancel; | |
1780 | } | |
1781 | ||
1782 | trace_xfs_swap_extent_before(ip, 0); | |
1783 | trace_xfs_swap_extent_before(tip, 1); | |
1784 | ||
1785 | /* check inode formats now that data is flushed */ | |
1786 | error = xfs_swap_extents_check_format(ip, tip); | |
1787 | if (error) { | |
1788 | xfs_notice(mp, | |
1789 | "%s: inode 0x%llx format is incompatible for exchanging.", | |
1790 | __func__, ip->i_ino); | |
1791 | goto out_trans_cancel; | |
1792 | } | |
1793 | ||
1794 | /* | |
1795 | * Compare the current change & modify times with that | |
1796 | * passed in. If they differ, we abort this swap. | |
1797 | * This is the mechanism used to ensure the calling | |
1798 | * process that the file was not changed out from | |
1799 | * under it. | |
1800 | */ | |
1801 | if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) || | |
1802 | (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) || | |
1803 | (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) || | |
1804 | (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) { | |
1805 | error = -EBUSY; | |
1806 | goto out_trans_cancel; | |
1807 | } | |
1808 | ||
1809 | /* | |
1810 | * Note the trickiness in setting the log flags - we set the owner log | |
1811 | * flag on the opposite inode (i.e. the inode we are setting the new | |
1812 | * owner to be) because once we swap the forks and log that, log | |
1813 | * recovery is going to see the fork as owned by the swapped inode, | |
1814 | * not the pre-swapped inodes. | |
1815 | */ | |
1816 | src_log_flags = XFS_ILOG_CORE; | |
1817 | target_log_flags = XFS_ILOG_CORE; | |
1818 | ||
1f08af52 DW |
1819 | if (xfs_sb_version_hasrmapbt(&mp->m_sb)) |
1820 | error = xfs_swap_extent_rmap(&tp, ip, tip); | |
1821 | else | |
1822 | error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags, | |
1823 | &target_log_flags); | |
39aff5fd DW |
1824 | if (error) |
1825 | goto out_trans_cancel; | |
1826 | ||
f0bc4d13 DW |
1827 | /* Do we have to swap reflink flags? */ |
1828 | if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^ | |
1829 | (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) { | |
1830 | f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK; | |
1831 | ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; | |
1832 | ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK; | |
1833 | tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK; | |
1834 | tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK; | |
52bfcdd7 DW |
1835 | } |
1836 | ||
1837 | /* Swap the cow forks. */ | |
1838 | if (xfs_sb_version_hasreflink(&mp->m_sb)) { | |
52bfcdd7 DW |
1839 | ASSERT(ip->i_cformat == XFS_DINODE_FMT_EXTENTS); |
1840 | ASSERT(tip->i_cformat == XFS_DINODE_FMT_EXTENTS); | |
1841 | ||
897992b7 GS |
1842 | swap(ip->i_cnextents, tip->i_cnextents); |
1843 | swap(ip->i_cowfp, tip->i_cowfp); | |
52bfcdd7 | 1844 | |
5bcffe30 | 1845 | if (ip->i_cowfp && ip->i_cowfp->if_bytes) |
52bfcdd7 DW |
1846 | xfs_inode_set_cowblocks_tag(ip); |
1847 | else | |
1848 | xfs_inode_clear_cowblocks_tag(ip); | |
5bcffe30 | 1849 | if (tip->i_cowfp && tip->i_cowfp->if_bytes) |
52bfcdd7 DW |
1850 | xfs_inode_set_cowblocks_tag(tip); |
1851 | else | |
1852 | xfs_inode_clear_cowblocks_tag(tip); | |
f0bc4d13 DW |
1853 | } |
1854 | ||
a133d952 DC |
1855 | xfs_trans_log_inode(tp, ip, src_log_flags); |
1856 | xfs_trans_log_inode(tp, tip, target_log_flags); | |
1857 | ||
6fb10d6d BF |
1858 | /* |
1859 | * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems | |
1860 | * have inode number owner values in the bmbt blocks that still refer to | |
1861 | * the old inode. Scan each bmbt to fix up the owner values with the | |
1862 | * inode number of the current inode. | |
1863 | */ | |
1864 | if (src_log_flags & XFS_ILOG_DOWNER) { | |
2dd3d709 | 1865 | error = xfs_swap_change_owner(&tp, ip, tip); |
6fb10d6d BF |
1866 | if (error) |
1867 | goto out_trans_cancel; | |
1868 | } | |
1869 | if (target_log_flags & XFS_ILOG_DOWNER) { | |
2dd3d709 | 1870 | error = xfs_swap_change_owner(&tp, tip, ip); |
6fb10d6d BF |
1871 | if (error) |
1872 | goto out_trans_cancel; | |
1873 | } | |
1874 | ||
a133d952 DC |
1875 | /* |
1876 | * If this is a synchronous mount, make sure that the | |
1877 | * transaction goes to disk before returning to the user. | |
1878 | */ | |
1879 | if (mp->m_flags & XFS_MOUNT_WSYNC) | |
1880 | xfs_trans_set_sync(tp); | |
1881 | ||
70393313 | 1882 | error = xfs_trans_commit(tp); |
a133d952 DC |
1883 | |
1884 | trace_xfs_swap_extent_after(ip, 0); | |
1885 | trace_xfs_swap_extent_after(tip, 1); | |
a133d952 | 1886 | |
65523218 | 1887 | out_unlock: |
81217683 DC |
1888 | xfs_iunlock(ip, lock_flags); |
1889 | xfs_iunlock(tip, lock_flags); | |
65523218 | 1890 | unlock_two_nondirectories(VFS_I(ip), VFS_I(tip)); |
39aff5fd | 1891 | return error; |
a133d952 DC |
1892 | |
1893 | out_trans_cancel: | |
4906e215 | 1894 | xfs_trans_cancel(tp); |
65523218 | 1895 | goto out_unlock; |
a133d952 | 1896 | } |