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