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