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