]>
Commit | Line | Data |
---|---|---|
631ac87a DW |
1 | /* |
2 | * Copyright (c) 2014 Red Hat, Inc. | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it would be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | #include "libxfs_priv.h" | |
19 | #include "xfs_fs.h" | |
20 | #include "xfs_shared.h" | |
21 | #include "xfs_format.h" | |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
24 | #include "xfs_bit.h" | |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_mount.h" | |
27 | #include "xfs_defer.h" | |
28 | #include "xfs_da_format.h" | |
29 | #include "xfs_da_btree.h" | |
30 | #include "xfs_btree.h" | |
31 | #include "xfs_trans.h" | |
32 | #include "xfs_alloc.h" | |
33 | #include "xfs_rmap.h" | |
631bda23 | 34 | #include "xfs_rmap_btree.h" |
631ac87a DW |
35 | #include "xfs_trans_space.h" |
36 | #include "xfs_trace.h" | |
d7f80320 DW |
37 | #include "xfs_bmap.h" |
38 | #include "xfs_inode.h" | |
631ac87a | 39 | |
936ca687 DW |
40 | /* |
41 | * Lookup the first record less than or equal to [bno, len, owner, offset] | |
42 | * in the btree given by cur. | |
43 | */ | |
44 | int | |
45 | xfs_rmap_lookup_le( | |
46 | struct xfs_btree_cur *cur, | |
47 | xfs_agblock_t bno, | |
48 | xfs_extlen_t len, | |
49 | uint64_t owner, | |
50 | uint64_t offset, | |
51 | unsigned int flags, | |
52 | int *stat) | |
53 | { | |
54 | cur->bc_rec.r.rm_startblock = bno; | |
55 | cur->bc_rec.r.rm_blockcount = len; | |
56 | cur->bc_rec.r.rm_owner = owner; | |
57 | cur->bc_rec.r.rm_offset = offset; | |
58 | cur->bc_rec.r.rm_flags = flags; | |
59 | return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat); | |
60 | } | |
61 | ||
62 | /* | |
63 | * Lookup the record exactly matching [bno, len, owner, offset] | |
64 | * in the btree given by cur. | |
65 | */ | |
66 | int | |
67 | xfs_rmap_lookup_eq( | |
68 | struct xfs_btree_cur *cur, | |
69 | xfs_agblock_t bno, | |
70 | xfs_extlen_t len, | |
71 | uint64_t owner, | |
72 | uint64_t offset, | |
73 | unsigned int flags, | |
74 | int *stat) | |
75 | { | |
76 | cur->bc_rec.r.rm_startblock = bno; | |
77 | cur->bc_rec.r.rm_blockcount = len; | |
78 | cur->bc_rec.r.rm_owner = owner; | |
79 | cur->bc_rec.r.rm_offset = offset; | |
80 | cur->bc_rec.r.rm_flags = flags; | |
81 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); | |
82 | } | |
83 | ||
84 | /* | |
85 | * Update the record referred to by cur to the value given | |
86 | * by [bno, len, owner, offset]. | |
87 | * This either works (return 0) or gets an EFSCORRUPTED error. | |
88 | */ | |
89 | STATIC int | |
90 | xfs_rmap_update( | |
91 | struct xfs_btree_cur *cur, | |
92 | struct xfs_rmap_irec *irec) | |
93 | { | |
94 | union xfs_btree_rec rec; | |
b26675c4 DW |
95 | int error; |
96 | ||
97 | trace_xfs_rmap_update(cur->bc_mp, cur->bc_private.a.agno, | |
98 | irec->rm_startblock, irec->rm_blockcount, | |
99 | irec->rm_owner, irec->rm_offset, irec->rm_flags); | |
936ca687 DW |
100 | |
101 | rec.rmap.rm_startblock = cpu_to_be32(irec->rm_startblock); | |
102 | rec.rmap.rm_blockcount = cpu_to_be32(irec->rm_blockcount); | |
103 | rec.rmap.rm_owner = cpu_to_be64(irec->rm_owner); | |
104 | rec.rmap.rm_offset = cpu_to_be64( | |
105 | xfs_rmap_irec_offset_pack(irec)); | |
b26675c4 DW |
106 | error = xfs_btree_update(cur, &rec); |
107 | if (error) | |
108 | trace_xfs_rmap_update_error(cur->bc_mp, | |
109 | cur->bc_private.a.agno, error, _RET_IP_); | |
110 | return error; | |
111 | } | |
112 | ||
113 | int | |
114 | xfs_rmap_insert( | |
115 | struct xfs_btree_cur *rcur, | |
116 | xfs_agblock_t agbno, | |
117 | xfs_extlen_t len, | |
118 | uint64_t owner, | |
119 | uint64_t offset, | |
120 | unsigned int flags) | |
121 | { | |
122 | int i; | |
123 | int error; | |
124 | ||
125 | trace_xfs_rmap_insert(rcur->bc_mp, rcur->bc_private.a.agno, agbno, | |
126 | len, owner, offset, flags); | |
127 | ||
128 | error = xfs_rmap_lookup_eq(rcur, agbno, len, owner, offset, flags, &i); | |
129 | if (error) | |
130 | goto done; | |
131 | XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 0, done); | |
132 | ||
133 | rcur->bc_rec.r.rm_startblock = agbno; | |
134 | rcur->bc_rec.r.rm_blockcount = len; | |
135 | rcur->bc_rec.r.rm_owner = owner; | |
136 | rcur->bc_rec.r.rm_offset = offset; | |
137 | rcur->bc_rec.r.rm_flags = flags; | |
138 | error = xfs_btree_insert(rcur, &i); | |
139 | if (error) | |
140 | goto done; | |
141 | XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 1, done); | |
142 | done: | |
143 | if (error) | |
144 | trace_xfs_rmap_insert_error(rcur->bc_mp, | |
145 | rcur->bc_private.a.agno, error, _RET_IP_); | |
146 | return error; | |
936ca687 DW |
147 | } |
148 | ||
6c6bdf6f DW |
149 | STATIC int |
150 | xfs_rmap_delete( | |
151 | struct xfs_btree_cur *rcur, | |
152 | xfs_agblock_t agbno, | |
153 | xfs_extlen_t len, | |
154 | uint64_t owner, | |
155 | uint64_t offset, | |
156 | unsigned int flags) | |
157 | { | |
158 | int i; | |
159 | int error; | |
160 | ||
161 | trace_xfs_rmap_delete(rcur->bc_mp, rcur->bc_private.a.agno, agbno, | |
162 | len, owner, offset, flags); | |
163 | ||
164 | error = xfs_rmap_lookup_eq(rcur, agbno, len, owner, offset, flags, &i); | |
165 | if (error) | |
166 | goto done; | |
167 | XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 1, done); | |
168 | ||
169 | error = xfs_btree_delete(rcur, &i); | |
170 | if (error) | |
171 | goto done; | |
172 | XFS_WANT_CORRUPTED_GOTO(rcur->bc_mp, i == 1, done); | |
173 | done: | |
174 | if (error) | |
175 | trace_xfs_rmap_delete_error(rcur->bc_mp, | |
176 | rcur->bc_private.a.agno, error, _RET_IP_); | |
177 | return error; | |
178 | } | |
179 | ||
936ca687 DW |
180 | static int |
181 | xfs_rmap_btrec_to_irec( | |
182 | union xfs_btree_rec *rec, | |
183 | struct xfs_rmap_irec *irec) | |
184 | { | |
185 | irec->rm_flags = 0; | |
186 | irec->rm_startblock = be32_to_cpu(rec->rmap.rm_startblock); | |
187 | irec->rm_blockcount = be32_to_cpu(rec->rmap.rm_blockcount); | |
188 | irec->rm_owner = be64_to_cpu(rec->rmap.rm_owner); | |
189 | return xfs_rmap_irec_offset_unpack(be64_to_cpu(rec->rmap.rm_offset), | |
190 | irec); | |
191 | } | |
192 | ||
193 | /* | |
194 | * Get the data from the pointed-to record. | |
195 | */ | |
196 | int | |
197 | xfs_rmap_get_rec( | |
198 | struct xfs_btree_cur *cur, | |
199 | struct xfs_rmap_irec *irec, | |
200 | int *stat) | |
201 | { | |
202 | union xfs_btree_rec *rec; | |
203 | int error; | |
204 | ||
205 | error = xfs_btree_get_rec(cur, &rec, stat); | |
206 | if (error || !*stat) | |
207 | return error; | |
208 | ||
209 | return xfs_rmap_btrec_to_irec(rec, irec); | |
210 | } | |
211 | ||
6c6bdf6f DW |
212 | struct xfs_find_left_neighbor_info { |
213 | struct xfs_rmap_irec high; | |
214 | struct xfs_rmap_irec *irec; | |
215 | int *stat; | |
216 | }; | |
217 | ||
218 | /* For each rmap given, figure out if it matches the key we want. */ | |
219 | STATIC int | |
220 | xfs_rmap_find_left_neighbor_helper( | |
221 | struct xfs_btree_cur *cur, | |
222 | struct xfs_rmap_irec *rec, | |
223 | void *priv) | |
224 | { | |
225 | struct xfs_find_left_neighbor_info *info = priv; | |
226 | ||
227 | trace_xfs_rmap_find_left_neighbor_candidate(cur->bc_mp, | |
228 | cur->bc_private.a.agno, rec->rm_startblock, | |
229 | rec->rm_blockcount, rec->rm_owner, rec->rm_offset, | |
230 | rec->rm_flags); | |
231 | ||
232 | if (rec->rm_owner != info->high.rm_owner) | |
233 | return XFS_BTREE_QUERY_RANGE_CONTINUE; | |
234 | if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) && | |
235 | !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK) && | |
236 | rec->rm_offset + rec->rm_blockcount - 1 != info->high.rm_offset) | |
237 | return XFS_BTREE_QUERY_RANGE_CONTINUE; | |
238 | ||
239 | *info->irec = *rec; | |
240 | *info->stat = 1; | |
241 | return XFS_BTREE_QUERY_RANGE_ABORT; | |
242 | } | |
243 | ||
244 | /* | |
245 | * Find the record to the left of the given extent, being careful only to | |
246 | * return a match with the same owner and adjacent physical and logical | |
247 | * block ranges. | |
248 | */ | |
249 | int | |
250 | xfs_rmap_find_left_neighbor( | |
251 | struct xfs_btree_cur *cur, | |
252 | xfs_agblock_t bno, | |
253 | uint64_t owner, | |
254 | uint64_t offset, | |
255 | unsigned int flags, | |
256 | struct xfs_rmap_irec *irec, | |
257 | int *stat) | |
258 | { | |
259 | struct xfs_find_left_neighbor_info info; | |
260 | int error; | |
261 | ||
262 | *stat = 0; | |
263 | if (bno == 0) | |
264 | return 0; | |
265 | info.high.rm_startblock = bno - 1; | |
266 | info.high.rm_owner = owner; | |
267 | if (!XFS_RMAP_NON_INODE_OWNER(owner) && | |
268 | !(flags & XFS_RMAP_BMBT_BLOCK)) { | |
269 | if (offset == 0) | |
270 | return 0; | |
271 | info.high.rm_offset = offset - 1; | |
272 | } else | |
273 | info.high.rm_offset = 0; | |
274 | info.high.rm_flags = flags; | |
275 | info.high.rm_blockcount = 0; | |
276 | info.irec = irec; | |
277 | info.stat = stat; | |
278 | ||
279 | trace_xfs_rmap_find_left_neighbor_query(cur->bc_mp, | |
280 | cur->bc_private.a.agno, bno, 0, owner, offset, flags); | |
281 | ||
282 | error = xfs_rmap_query_range(cur, &info.high, &info.high, | |
283 | xfs_rmap_find_left_neighbor_helper, &info); | |
284 | if (error == XFS_BTREE_QUERY_RANGE_ABORT) | |
285 | error = 0; | |
286 | if (*stat) | |
287 | trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp, | |
288 | cur->bc_private.a.agno, irec->rm_startblock, | |
289 | irec->rm_blockcount, irec->rm_owner, | |
290 | irec->rm_offset, irec->rm_flags); | |
291 | return error; | |
292 | } | |
293 | ||
294 | /* For each rmap given, figure out if it matches the key we want. */ | |
295 | STATIC int | |
296 | xfs_rmap_lookup_le_range_helper( | |
297 | struct xfs_btree_cur *cur, | |
298 | struct xfs_rmap_irec *rec, | |
299 | void *priv) | |
300 | { | |
301 | struct xfs_find_left_neighbor_info *info = priv; | |
302 | ||
303 | trace_xfs_rmap_lookup_le_range_candidate(cur->bc_mp, | |
304 | cur->bc_private.a.agno, rec->rm_startblock, | |
305 | rec->rm_blockcount, rec->rm_owner, rec->rm_offset, | |
306 | rec->rm_flags); | |
307 | ||
308 | if (rec->rm_owner != info->high.rm_owner) | |
309 | return XFS_BTREE_QUERY_RANGE_CONTINUE; | |
310 | if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) && | |
311 | !(rec->rm_flags & XFS_RMAP_BMBT_BLOCK) && | |
312 | (rec->rm_offset > info->high.rm_offset || | |
313 | rec->rm_offset + rec->rm_blockcount <= info->high.rm_offset)) | |
314 | return XFS_BTREE_QUERY_RANGE_CONTINUE; | |
315 | ||
316 | *info->irec = *rec; | |
317 | *info->stat = 1; | |
318 | return XFS_BTREE_QUERY_RANGE_ABORT; | |
319 | } | |
320 | ||
321 | /* | |
322 | * Find the record to the left of the given extent, being careful only to | |
323 | * return a match with the same owner and overlapping physical and logical | |
324 | * block ranges. This is the overlapping-interval version of | |
325 | * xfs_rmap_lookup_le. | |
326 | */ | |
327 | int | |
328 | xfs_rmap_lookup_le_range( | |
329 | struct xfs_btree_cur *cur, | |
330 | xfs_agblock_t bno, | |
331 | uint64_t owner, | |
332 | uint64_t offset, | |
333 | unsigned int flags, | |
334 | struct xfs_rmap_irec *irec, | |
335 | int *stat) | |
336 | { | |
337 | struct xfs_find_left_neighbor_info info; | |
338 | int error; | |
339 | ||
340 | info.high.rm_startblock = bno; | |
341 | info.high.rm_owner = owner; | |
342 | if (!XFS_RMAP_NON_INODE_OWNER(owner) && !(flags & XFS_RMAP_BMBT_BLOCK)) | |
343 | info.high.rm_offset = offset; | |
344 | else | |
345 | info.high.rm_offset = 0; | |
346 | info.high.rm_flags = flags; | |
347 | info.high.rm_blockcount = 0; | |
348 | *stat = 0; | |
349 | info.irec = irec; | |
350 | info.stat = stat; | |
351 | ||
352 | trace_xfs_rmap_lookup_le_range(cur->bc_mp, | |
353 | cur->bc_private.a.agno, bno, 0, owner, offset, flags); | |
354 | error = xfs_rmap_query_range(cur, &info.high, &info.high, | |
355 | xfs_rmap_lookup_le_range_helper, &info); | |
356 | if (error == XFS_BTREE_QUERY_RANGE_ABORT) | |
357 | error = 0; | |
358 | if (*stat) | |
359 | trace_xfs_rmap_lookup_le_range_result(cur->bc_mp, | |
360 | cur->bc_private.a.agno, irec->rm_startblock, | |
361 | irec->rm_blockcount, irec->rm_owner, | |
362 | irec->rm_offset, irec->rm_flags); | |
363 | return error; | |
364 | } | |
365 | ||
ae81ebf2 DW |
366 | /* |
367 | * Find the extent in the rmap btree and remove it. | |
368 | * | |
369 | * The record we find should always be an exact match for the extent that we're | |
370 | * looking for, since we insert them into the btree without modification. | |
371 | * | |
372 | * Special Case #1: when growing the filesystem, we "free" an extent when | |
373 | * growing the last AG. This extent is new space and so it is not tracked as | |
374 | * used space in the btree. The growfs code will pass in an owner of | |
375 | * XFS_RMAP_OWN_NULL to indicate that it expected that there is no owner of this | |
376 | * extent. We verify that - the extent lookup result in a record that does not | |
377 | * overlap. | |
378 | * | |
379 | * Special Case #2: EFIs do not record the owner of the extent, so when | |
380 | * recovering EFIs from the log we pass in XFS_RMAP_OWN_UNKNOWN to tell the rmap | |
381 | * btree to ignore the owner (i.e. wildcard match) so we don't trigger | |
382 | * corruption checks during log recovery. | |
383 | */ | |
384 | STATIC int | |
385 | xfs_rmap_unmap( | |
386 | struct xfs_btree_cur *cur, | |
387 | xfs_agblock_t bno, | |
388 | xfs_extlen_t len, | |
389 | bool unwritten, | |
390 | struct xfs_owner_info *oinfo) | |
391 | { | |
392 | struct xfs_mount *mp = cur->bc_mp; | |
393 | struct xfs_rmap_irec ltrec; | |
394 | uint64_t ltoff; | |
395 | int error = 0; | |
396 | int i; | |
397 | uint64_t owner; | |
398 | uint64_t offset; | |
399 | unsigned int flags; | |
400 | bool ignore_off; | |
401 | ||
402 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); | |
403 | ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) || | |
404 | (flags & XFS_RMAP_BMBT_BLOCK); | |
405 | if (unwritten) | |
406 | flags |= XFS_RMAP_UNWRITTEN; | |
407 | trace_xfs_rmap_unmap(mp, cur->bc_private.a.agno, bno, len, | |
408 | unwritten, oinfo); | |
409 | ||
410 | /* | |
411 | * We should always have a left record because there's a static record | |
412 | * for the AG headers at rm_startblock == 0 created by mkfs/growfs that | |
413 | * will not ever be removed from the tree. | |
414 | */ | |
415 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags, &i); | |
416 | if (error) | |
417 | goto out_error; | |
418 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
419 | ||
420 | error = xfs_rmap_get_rec(cur, <rec, &i); | |
421 | if (error) | |
422 | goto out_error; | |
423 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
424 | trace_xfs_rmap_lookup_le_range_result(cur->bc_mp, | |
425 | cur->bc_private.a.agno, ltrec.rm_startblock, | |
426 | ltrec.rm_blockcount, ltrec.rm_owner, | |
427 | ltrec.rm_offset, ltrec.rm_flags); | |
428 | ltoff = ltrec.rm_offset; | |
429 | ||
430 | /* | |
431 | * For growfs, the incoming extent must be beyond the left record we | |
432 | * just found as it is new space and won't be used by anyone. This is | |
433 | * just a corruption check as we don't actually do anything with this | |
434 | * extent. Note that we need to use >= instead of > because it might | |
435 | * be the case that the "left" extent goes all the way to EOFS. | |
436 | */ | |
437 | if (owner == XFS_RMAP_OWN_NULL) { | |
438 | XFS_WANT_CORRUPTED_GOTO(mp, bno >= ltrec.rm_startblock + | |
439 | ltrec.rm_blockcount, out_error); | |
440 | goto out_done; | |
441 | } | |
442 | ||
443 | /* Make sure the unwritten flag matches. */ | |
444 | XFS_WANT_CORRUPTED_GOTO(mp, (flags & XFS_RMAP_UNWRITTEN) == | |
445 | (ltrec.rm_flags & XFS_RMAP_UNWRITTEN), out_error); | |
446 | ||
447 | /* Make sure the extent we found covers the entire freeing range. */ | |
448 | XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_startblock <= bno && | |
449 | ltrec.rm_startblock + ltrec.rm_blockcount >= | |
450 | bno + len, out_error); | |
451 | ||
452 | /* Make sure the owner matches what we expect to find in the tree. */ | |
453 | XFS_WANT_CORRUPTED_GOTO(mp, owner == ltrec.rm_owner || | |
454 | XFS_RMAP_NON_INODE_OWNER(owner), out_error); | |
455 | ||
456 | /* Check the offset, if necessary. */ | |
457 | if (!XFS_RMAP_NON_INODE_OWNER(owner)) { | |
458 | if (flags & XFS_RMAP_BMBT_BLOCK) { | |
459 | XFS_WANT_CORRUPTED_GOTO(mp, | |
460 | ltrec.rm_flags & XFS_RMAP_BMBT_BLOCK, | |
461 | out_error); | |
462 | } else { | |
463 | XFS_WANT_CORRUPTED_GOTO(mp, | |
464 | ltrec.rm_offset <= offset, out_error); | |
465 | XFS_WANT_CORRUPTED_GOTO(mp, | |
466 | ltoff + ltrec.rm_blockcount >= offset + len, | |
467 | out_error); | |
468 | } | |
469 | } | |
470 | ||
471 | if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) { | |
472 | /* exact match, simply remove the record from rmap tree */ | |
473 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, | |
474 | ltrec.rm_startblock, ltrec.rm_blockcount, | |
475 | ltrec.rm_owner, ltrec.rm_offset, | |
476 | ltrec.rm_flags); | |
477 | error = xfs_btree_delete(cur, &i); | |
478 | if (error) | |
479 | goto out_error; | |
480 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
481 | } else if (ltrec.rm_startblock == bno) { | |
482 | /* | |
483 | * overlap left hand side of extent: move the start, trim the | |
484 | * length and update the current record. | |
485 | * | |
486 | * ltbno ltlen | |
487 | * Orig: |oooooooooooooooooooo| | |
488 | * Freeing: |fffffffff| | |
489 | * Result: |rrrrrrrrrr| | |
490 | * bno len | |
491 | */ | |
492 | ltrec.rm_startblock += len; | |
493 | ltrec.rm_blockcount -= len; | |
494 | if (!ignore_off) | |
495 | ltrec.rm_offset += len; | |
496 | error = xfs_rmap_update(cur, <rec); | |
497 | if (error) | |
498 | goto out_error; | |
499 | } else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) { | |
500 | /* | |
501 | * overlap right hand side of extent: trim the length and update | |
502 | * the current record. | |
503 | * | |
504 | * ltbno ltlen | |
505 | * Orig: |oooooooooooooooooooo| | |
506 | * Freeing: |fffffffff| | |
507 | * Result: |rrrrrrrrrr| | |
508 | * bno len | |
509 | */ | |
510 | ltrec.rm_blockcount -= len; | |
511 | error = xfs_rmap_update(cur, <rec); | |
512 | if (error) | |
513 | goto out_error; | |
514 | } else { | |
515 | ||
516 | /* | |
517 | * overlap middle of extent: trim the length of the existing | |
518 | * record to the length of the new left-extent size, increment | |
519 | * the insertion position so we can insert a new record | |
520 | * containing the remaining right-extent space. | |
521 | * | |
522 | * ltbno ltlen | |
523 | * Orig: |oooooooooooooooooooo| | |
524 | * Freeing: |fffffffff| | |
525 | * Result: |rrrrr| |rrrr| | |
526 | * bno len | |
527 | */ | |
528 | xfs_extlen_t orig_len = ltrec.rm_blockcount; | |
529 | ||
530 | ltrec.rm_blockcount = bno - ltrec.rm_startblock; | |
531 | error = xfs_rmap_update(cur, <rec); | |
532 | if (error) | |
533 | goto out_error; | |
534 | ||
535 | error = xfs_btree_increment(cur, 0, &i); | |
536 | if (error) | |
537 | goto out_error; | |
538 | ||
539 | cur->bc_rec.r.rm_startblock = bno + len; | |
540 | cur->bc_rec.r.rm_blockcount = orig_len - len - | |
541 | ltrec.rm_blockcount; | |
542 | cur->bc_rec.r.rm_owner = ltrec.rm_owner; | |
543 | if (ignore_off) | |
544 | cur->bc_rec.r.rm_offset = 0; | |
545 | else | |
546 | cur->bc_rec.r.rm_offset = offset + len; | |
547 | cur->bc_rec.r.rm_flags = flags; | |
548 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, | |
549 | cur->bc_rec.r.rm_startblock, | |
550 | cur->bc_rec.r.rm_blockcount, | |
551 | cur->bc_rec.r.rm_owner, | |
552 | cur->bc_rec.r.rm_offset, | |
553 | cur->bc_rec.r.rm_flags); | |
554 | error = xfs_btree_insert(cur, &i); | |
555 | if (error) | |
556 | goto out_error; | |
557 | } | |
558 | ||
559 | out_done: | |
560 | trace_xfs_rmap_unmap_done(mp, cur->bc_private.a.agno, bno, len, | |
561 | unwritten, oinfo); | |
562 | out_error: | |
563 | if (error) | |
564 | trace_xfs_rmap_unmap_error(mp, cur->bc_private.a.agno, | |
565 | error, _RET_IP_); | |
566 | return error; | |
567 | } | |
568 | ||
569 | /* | |
570 | * Remove a reference to an extent in the rmap btree. | |
571 | */ | |
631ac87a DW |
572 | int |
573 | xfs_rmap_free( | |
574 | struct xfs_trans *tp, | |
575 | struct xfs_buf *agbp, | |
576 | xfs_agnumber_t agno, | |
577 | xfs_agblock_t bno, | |
578 | xfs_extlen_t len, | |
579 | struct xfs_owner_info *oinfo) | |
580 | { | |
581 | struct xfs_mount *mp = tp->t_mountp; | |
ae81ebf2 DW |
582 | struct xfs_btree_cur *cur; |
583 | int error; | |
631ac87a DW |
584 | |
585 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) | |
586 | return 0; | |
587 | ||
ae81ebf2 DW |
588 | cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno); |
589 | ||
590 | error = xfs_rmap_unmap(cur, bno, len, false, oinfo); | |
591 | if (error) | |
631ac87a | 592 | goto out_error; |
ae81ebf2 DW |
593 | |
594 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
631ac87a DW |
595 | return 0; |
596 | ||
597 | out_error: | |
ae81ebf2 | 598 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
631ac87a DW |
599 | return error; |
600 | } | |
601 | ||
631bda23 DW |
602 | /* |
603 | * A mergeable rmap must have the same owner and the same values for | |
604 | * the unwritten, attr_fork, and bmbt flags. The startblock and | |
605 | * offset are checked separately. | |
606 | */ | |
607 | static bool | |
608 | xfs_rmap_is_mergeable( | |
609 | struct xfs_rmap_irec *irec, | |
610 | uint64_t owner, | |
611 | unsigned int flags) | |
612 | { | |
613 | if (irec->rm_owner == XFS_RMAP_OWN_NULL) | |
614 | return false; | |
615 | if (irec->rm_owner != owner) | |
616 | return false; | |
617 | if ((flags & XFS_RMAP_UNWRITTEN) ^ | |
618 | (irec->rm_flags & XFS_RMAP_UNWRITTEN)) | |
619 | return false; | |
620 | if ((flags & XFS_RMAP_ATTR_FORK) ^ | |
621 | (irec->rm_flags & XFS_RMAP_ATTR_FORK)) | |
622 | return false; | |
623 | if ((flags & XFS_RMAP_BMBT_BLOCK) ^ | |
624 | (irec->rm_flags & XFS_RMAP_BMBT_BLOCK)) | |
625 | return false; | |
626 | return true; | |
627 | } | |
628 | ||
629 | /* | |
630 | * When we allocate a new block, the first thing we do is add a reference to | |
631 | * the extent in the rmap btree. This takes the form of a [agbno, length, | |
632 | * owner, offset] record. Flags are encoded in the high bits of the offset | |
633 | * field. | |
634 | */ | |
635 | STATIC int | |
636 | xfs_rmap_map( | |
637 | struct xfs_btree_cur *cur, | |
638 | xfs_agblock_t bno, | |
639 | xfs_extlen_t len, | |
640 | bool unwritten, | |
641 | struct xfs_owner_info *oinfo) | |
642 | { | |
643 | struct xfs_mount *mp = cur->bc_mp; | |
644 | struct xfs_rmap_irec ltrec; | |
645 | struct xfs_rmap_irec gtrec; | |
646 | int have_gt; | |
647 | int have_lt; | |
648 | int error = 0; | |
649 | int i; | |
650 | uint64_t owner; | |
651 | uint64_t offset; | |
652 | unsigned int flags = 0; | |
653 | bool ignore_off; | |
654 | ||
655 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); | |
656 | ASSERT(owner != 0); | |
657 | ignore_off = XFS_RMAP_NON_INODE_OWNER(owner) || | |
658 | (flags & XFS_RMAP_BMBT_BLOCK); | |
659 | if (unwritten) | |
660 | flags |= XFS_RMAP_UNWRITTEN; | |
661 | trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len, | |
662 | unwritten, oinfo); | |
663 | ||
664 | /* | |
665 | * For the initial lookup, look for an exact match or the left-adjacent | |
666 | * record for our insertion point. This will also give us the record for | |
667 | * start block contiguity tests. | |
668 | */ | |
669 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, flags, | |
670 | &have_lt); | |
671 | if (error) | |
672 | goto out_error; | |
673 | XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error); | |
674 | ||
675 | error = xfs_rmap_get_rec(cur, <rec, &have_lt); | |
676 | if (error) | |
677 | goto out_error; | |
678 | XFS_WANT_CORRUPTED_GOTO(mp, have_lt == 1, out_error); | |
679 | trace_xfs_rmap_lookup_le_range_result(cur->bc_mp, | |
680 | cur->bc_private.a.agno, ltrec.rm_startblock, | |
681 | ltrec.rm_blockcount, ltrec.rm_owner, | |
682 | ltrec.rm_offset, ltrec.rm_flags); | |
683 | ||
684 | if (!xfs_rmap_is_mergeable(<rec, owner, flags)) | |
685 | have_lt = 0; | |
686 | ||
687 | XFS_WANT_CORRUPTED_GOTO(mp, | |
688 | have_lt == 0 || | |
689 | ltrec.rm_startblock + ltrec.rm_blockcount <= bno, out_error); | |
690 | ||
691 | /* | |
692 | * Increment the cursor to see if we have a right-adjacent record to our | |
693 | * insertion point. This will give us the record for end block | |
694 | * contiguity tests. | |
695 | */ | |
696 | error = xfs_btree_increment(cur, 0, &have_gt); | |
697 | if (error) | |
698 | goto out_error; | |
699 | if (have_gt) { | |
700 | error = xfs_rmap_get_rec(cur, >rec, &have_gt); | |
701 | if (error) | |
702 | goto out_error; | |
703 | XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error); | |
704 | XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= gtrec.rm_startblock, | |
705 | out_error); | |
706 | trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp, | |
707 | cur->bc_private.a.agno, gtrec.rm_startblock, | |
708 | gtrec.rm_blockcount, gtrec.rm_owner, | |
709 | gtrec.rm_offset, gtrec.rm_flags); | |
710 | if (!xfs_rmap_is_mergeable(>rec, owner, flags)) | |
711 | have_gt = 0; | |
712 | } | |
713 | ||
714 | /* | |
715 | * Note: cursor currently points one record to the right of ltrec, even | |
716 | * if there is no record in the tree to the right. | |
717 | */ | |
718 | if (have_lt && | |
719 | ltrec.rm_startblock + ltrec.rm_blockcount == bno && | |
720 | (ignore_off || ltrec.rm_offset + ltrec.rm_blockcount == offset)) { | |
721 | /* | |
722 | * left edge contiguous, merge into left record. | |
723 | * | |
724 | * ltbno ltlen | |
725 | * orig: |ooooooooo| | |
726 | * adding: |aaaaaaaaa| | |
727 | * result: |rrrrrrrrrrrrrrrrrrr| | |
728 | * bno len | |
729 | */ | |
730 | ltrec.rm_blockcount += len; | |
731 | if (have_gt && | |
732 | bno + len == gtrec.rm_startblock && | |
733 | (ignore_off || offset + len == gtrec.rm_offset) && | |
734 | (unsigned long)ltrec.rm_blockcount + len + | |
735 | gtrec.rm_blockcount <= XFS_RMAP_LEN_MAX) { | |
736 | /* | |
737 | * right edge also contiguous, delete right record | |
738 | * and merge into left record. | |
739 | * | |
740 | * ltbno ltlen gtbno gtlen | |
741 | * orig: |ooooooooo| |ooooooooo| | |
742 | * adding: |aaaaaaaaa| | |
743 | * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr| | |
744 | */ | |
745 | ltrec.rm_blockcount += gtrec.rm_blockcount; | |
746 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, | |
747 | gtrec.rm_startblock, | |
748 | gtrec.rm_blockcount, | |
749 | gtrec.rm_owner, | |
750 | gtrec.rm_offset, | |
751 | gtrec.rm_flags); | |
752 | error = xfs_btree_delete(cur, &i); | |
753 | if (error) | |
754 | goto out_error; | |
755 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
756 | } | |
757 | ||
758 | /* point the cursor back to the left record and update */ | |
759 | error = xfs_btree_decrement(cur, 0, &have_gt); | |
760 | if (error) | |
761 | goto out_error; | |
762 | error = xfs_rmap_update(cur, <rec); | |
763 | if (error) | |
764 | goto out_error; | |
765 | } else if (have_gt && | |
766 | bno + len == gtrec.rm_startblock && | |
767 | (ignore_off || offset + len == gtrec.rm_offset)) { | |
768 | /* | |
769 | * right edge contiguous, merge into right record. | |
770 | * | |
771 | * gtbno gtlen | |
772 | * Orig: |ooooooooo| | |
773 | * adding: |aaaaaaaaa| | |
774 | * Result: |rrrrrrrrrrrrrrrrrrr| | |
775 | * bno len | |
776 | */ | |
777 | gtrec.rm_startblock = bno; | |
778 | gtrec.rm_blockcount += len; | |
779 | if (!ignore_off) | |
780 | gtrec.rm_offset = offset; | |
781 | error = xfs_rmap_update(cur, >rec); | |
782 | if (error) | |
783 | goto out_error; | |
784 | } else { | |
785 | /* | |
786 | * no contiguous edge with identical owner, insert | |
787 | * new record at current cursor position. | |
788 | */ | |
789 | cur->bc_rec.r.rm_startblock = bno; | |
790 | cur->bc_rec.r.rm_blockcount = len; | |
791 | cur->bc_rec.r.rm_owner = owner; | |
792 | cur->bc_rec.r.rm_offset = offset; | |
793 | cur->bc_rec.r.rm_flags = flags; | |
794 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len, | |
795 | owner, offset, flags); | |
796 | error = xfs_btree_insert(cur, &i); | |
797 | if (error) | |
798 | goto out_error; | |
799 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
800 | } | |
801 | ||
802 | trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len, | |
803 | unwritten, oinfo); | |
804 | out_error: | |
805 | if (error) | |
806 | trace_xfs_rmap_map_error(mp, cur->bc_private.a.agno, | |
807 | error, _RET_IP_); | |
808 | return error; | |
809 | } | |
810 | ||
811 | /* | |
812 | * Add a reference to an extent in the rmap btree. | |
813 | */ | |
631ac87a DW |
814 | int |
815 | xfs_rmap_alloc( | |
816 | struct xfs_trans *tp, | |
817 | struct xfs_buf *agbp, | |
818 | xfs_agnumber_t agno, | |
819 | xfs_agblock_t bno, | |
820 | xfs_extlen_t len, | |
821 | struct xfs_owner_info *oinfo) | |
822 | { | |
823 | struct xfs_mount *mp = tp->t_mountp; | |
631bda23 DW |
824 | struct xfs_btree_cur *cur; |
825 | int error; | |
631ac87a DW |
826 | |
827 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) | |
828 | return 0; | |
829 | ||
631bda23 DW |
830 | cur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno); |
831 | error = xfs_rmap_map(cur, bno, len, false, oinfo); | |
832 | if (error) | |
631ac87a | 833 | goto out_error; |
631bda23 DW |
834 | |
835 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
631ac87a DW |
836 | return 0; |
837 | ||
838 | out_error: | |
631bda23 | 839 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
631ac87a DW |
840 | return error; |
841 | } | |
890e1174 | 842 | |
7faf209c DW |
843 | #define RMAP_LEFT_CONTIG (1 << 0) |
844 | #define RMAP_RIGHT_CONTIG (1 << 1) | |
845 | #define RMAP_LEFT_FILLING (1 << 2) | |
846 | #define RMAP_RIGHT_FILLING (1 << 3) | |
847 | #define RMAP_LEFT_VALID (1 << 6) | |
848 | #define RMAP_RIGHT_VALID (1 << 7) | |
849 | ||
850 | #define LEFT r[0] | |
851 | #define RIGHT r[1] | |
852 | #define PREV r[2] | |
853 | #define NEW r[3] | |
854 | ||
855 | /* | |
856 | * Convert an unwritten extent to a real extent or vice versa. | |
857 | * Does not handle overlapping extents. | |
858 | */ | |
859 | STATIC int | |
860 | xfs_rmap_convert( | |
861 | struct xfs_btree_cur *cur, | |
862 | xfs_agblock_t bno, | |
863 | xfs_extlen_t len, | |
864 | bool unwritten, | |
865 | struct xfs_owner_info *oinfo) | |
866 | { | |
867 | struct xfs_mount *mp = cur->bc_mp; | |
868 | struct xfs_rmap_irec r[4]; /* neighbor extent entries */ | |
869 | /* left is 0, right is 1, prev is 2 */ | |
870 | /* new is 3 */ | |
871 | uint64_t owner; | |
872 | uint64_t offset; | |
873 | uint64_t new_endoff; | |
874 | unsigned int oldext; | |
875 | unsigned int newext; | |
876 | unsigned int flags = 0; | |
877 | int i; | |
878 | int state = 0; | |
879 | int error; | |
880 | ||
881 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); | |
882 | ASSERT(!(XFS_RMAP_NON_INODE_OWNER(owner) || | |
883 | (flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK)))); | |
884 | oldext = unwritten ? XFS_RMAP_UNWRITTEN : 0; | |
885 | new_endoff = offset + len; | |
886 | trace_xfs_rmap_convert(mp, cur->bc_private.a.agno, bno, len, | |
887 | unwritten, oinfo); | |
888 | ||
889 | /* | |
890 | * For the initial lookup, look for an exact match or the left-adjacent | |
891 | * record for our insertion point. This will also give us the record for | |
892 | * start block contiguity tests. | |
893 | */ | |
894 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i); | |
895 | if (error) | |
896 | goto done; | |
897 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
898 | ||
899 | error = xfs_rmap_get_rec(cur, &PREV, &i); | |
900 | if (error) | |
901 | goto done; | |
902 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
903 | trace_xfs_rmap_lookup_le_range_result(cur->bc_mp, | |
904 | cur->bc_private.a.agno, PREV.rm_startblock, | |
905 | PREV.rm_blockcount, PREV.rm_owner, | |
906 | PREV.rm_offset, PREV.rm_flags); | |
907 | ||
908 | ASSERT(PREV.rm_offset <= offset); | |
909 | ASSERT(PREV.rm_offset + PREV.rm_blockcount >= new_endoff); | |
910 | ASSERT((PREV.rm_flags & XFS_RMAP_UNWRITTEN) == oldext); | |
911 | newext = ~oldext & XFS_RMAP_UNWRITTEN; | |
912 | ||
913 | /* | |
914 | * Set flags determining what part of the previous oldext allocation | |
915 | * extent is being replaced by a newext allocation. | |
916 | */ | |
917 | if (PREV.rm_offset == offset) | |
918 | state |= RMAP_LEFT_FILLING; | |
919 | if (PREV.rm_offset + PREV.rm_blockcount == new_endoff) | |
920 | state |= RMAP_RIGHT_FILLING; | |
921 | ||
922 | /* | |
923 | * Decrement the cursor to see if we have a left-adjacent record to our | |
924 | * insertion point. This will give us the record for end block | |
925 | * contiguity tests. | |
926 | */ | |
927 | error = xfs_btree_decrement(cur, 0, &i); | |
928 | if (error) | |
929 | goto done; | |
930 | if (i) { | |
931 | state |= RMAP_LEFT_VALID; | |
932 | error = xfs_rmap_get_rec(cur, &LEFT, &i); | |
933 | if (error) | |
934 | goto done; | |
935 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
936 | XFS_WANT_CORRUPTED_GOTO(mp, | |
937 | LEFT.rm_startblock + LEFT.rm_blockcount <= bno, | |
938 | done); | |
939 | trace_xfs_rmap_find_left_neighbor_result(cur->bc_mp, | |
940 | cur->bc_private.a.agno, LEFT.rm_startblock, | |
941 | LEFT.rm_blockcount, LEFT.rm_owner, | |
942 | LEFT.rm_offset, LEFT.rm_flags); | |
943 | if (LEFT.rm_startblock + LEFT.rm_blockcount == bno && | |
944 | LEFT.rm_offset + LEFT.rm_blockcount == offset && | |
945 | xfs_rmap_is_mergeable(&LEFT, owner, newext)) | |
946 | state |= RMAP_LEFT_CONTIG; | |
947 | } | |
948 | ||
949 | /* | |
950 | * Increment the cursor to see if we have a right-adjacent record to our | |
951 | * insertion point. This will give us the record for end block | |
952 | * contiguity tests. | |
953 | */ | |
954 | error = xfs_btree_increment(cur, 0, &i); | |
955 | if (error) | |
956 | goto done; | |
957 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
958 | error = xfs_btree_increment(cur, 0, &i); | |
959 | if (error) | |
960 | goto done; | |
961 | if (i) { | |
962 | state |= RMAP_RIGHT_VALID; | |
963 | error = xfs_rmap_get_rec(cur, &RIGHT, &i); | |
964 | if (error) | |
965 | goto done; | |
966 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
967 | XFS_WANT_CORRUPTED_GOTO(mp, bno + len <= RIGHT.rm_startblock, | |
968 | done); | |
969 | trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp, | |
970 | cur->bc_private.a.agno, RIGHT.rm_startblock, | |
971 | RIGHT.rm_blockcount, RIGHT.rm_owner, | |
972 | RIGHT.rm_offset, RIGHT.rm_flags); | |
973 | if (bno + len == RIGHT.rm_startblock && | |
974 | offset + len == RIGHT.rm_offset && | |
975 | xfs_rmap_is_mergeable(&RIGHT, owner, newext)) | |
976 | state |= RMAP_RIGHT_CONTIG; | |
977 | } | |
978 | ||
979 | /* check that left + prev + right is not too long */ | |
980 | if ((state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | | |
981 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) == | |
982 | (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | | |
983 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG) && | |
984 | (unsigned long)LEFT.rm_blockcount + len + | |
985 | RIGHT.rm_blockcount > XFS_RMAP_LEN_MAX) | |
986 | state &= ~RMAP_RIGHT_CONTIG; | |
987 | ||
988 | trace_xfs_rmap_convert_state(mp, cur->bc_private.a.agno, state, | |
989 | _RET_IP_); | |
990 | ||
991 | /* reset the cursor back to PREV */ | |
992 | error = xfs_rmap_lookup_le(cur, bno, len, owner, offset, oldext, &i); | |
993 | if (error) | |
994 | goto done; | |
995 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
996 | ||
997 | /* | |
998 | * Switch out based on the FILLING and CONTIG state bits. | |
999 | */ | |
1000 | switch (state & (RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | | |
1001 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG)) { | |
1002 | case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | | |
1003 | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG: | |
1004 | /* | |
1005 | * Setting all of a previous oldext extent to newext. | |
1006 | * The left and right neighbors are both contiguous with new. | |
1007 | */ | |
1008 | error = xfs_btree_increment(cur, 0, &i); | |
1009 | if (error) | |
1010 | goto done; | |
1011 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1012 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, | |
1013 | RIGHT.rm_startblock, RIGHT.rm_blockcount, | |
1014 | RIGHT.rm_owner, RIGHT.rm_offset, | |
1015 | RIGHT.rm_flags); | |
1016 | error = xfs_btree_delete(cur, &i); | |
1017 | if (error) | |
1018 | goto done; | |
1019 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1020 | error = xfs_btree_decrement(cur, 0, &i); | |
1021 | if (error) | |
1022 | goto done; | |
1023 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1024 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, | |
1025 | PREV.rm_startblock, PREV.rm_blockcount, | |
1026 | PREV.rm_owner, PREV.rm_offset, | |
1027 | PREV.rm_flags); | |
1028 | error = xfs_btree_delete(cur, &i); | |
1029 | if (error) | |
1030 | goto done; | |
1031 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1032 | error = xfs_btree_decrement(cur, 0, &i); | |
1033 | if (error) | |
1034 | goto done; | |
1035 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1036 | NEW = LEFT; | |
1037 | NEW.rm_blockcount += PREV.rm_blockcount + RIGHT.rm_blockcount; | |
1038 | error = xfs_rmap_update(cur, &NEW); | |
1039 | if (error) | |
1040 | goto done; | |
1041 | break; | |
1042 | ||
1043 | case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG: | |
1044 | /* | |
1045 | * Setting all of a previous oldext extent to newext. | |
1046 | * The left neighbor is contiguous, the right is not. | |
1047 | */ | |
1048 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, | |
1049 | PREV.rm_startblock, PREV.rm_blockcount, | |
1050 | PREV.rm_owner, PREV.rm_offset, | |
1051 | PREV.rm_flags); | |
1052 | error = xfs_btree_delete(cur, &i); | |
1053 | if (error) | |
1054 | goto done; | |
1055 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1056 | error = xfs_btree_decrement(cur, 0, &i); | |
1057 | if (error) | |
1058 | goto done; | |
1059 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1060 | NEW = LEFT; | |
1061 | NEW.rm_blockcount += PREV.rm_blockcount; | |
1062 | error = xfs_rmap_update(cur, &NEW); | |
1063 | if (error) | |
1064 | goto done; | |
1065 | break; | |
1066 | ||
1067 | case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG: | |
1068 | /* | |
1069 | * Setting all of a previous oldext extent to newext. | |
1070 | * The right neighbor is contiguous, the left is not. | |
1071 | */ | |
1072 | error = xfs_btree_increment(cur, 0, &i); | |
1073 | if (error) | |
1074 | goto done; | |
1075 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1076 | trace_xfs_rmap_delete(mp, cur->bc_private.a.agno, | |
1077 | RIGHT.rm_startblock, RIGHT.rm_blockcount, | |
1078 | RIGHT.rm_owner, RIGHT.rm_offset, | |
1079 | RIGHT.rm_flags); | |
1080 | error = xfs_btree_delete(cur, &i); | |
1081 | if (error) | |
1082 | goto done; | |
1083 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1084 | error = xfs_btree_decrement(cur, 0, &i); | |
1085 | if (error) | |
1086 | goto done; | |
1087 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1088 | NEW = PREV; | |
1089 | NEW.rm_blockcount = len + RIGHT.rm_blockcount; | |
1090 | NEW.rm_flags = newext; | |
1091 | error = xfs_rmap_update(cur, &NEW); | |
1092 | if (error) | |
1093 | goto done; | |
1094 | break; | |
1095 | ||
1096 | case RMAP_LEFT_FILLING | RMAP_RIGHT_FILLING: | |
1097 | /* | |
1098 | * Setting all of a previous oldext extent to newext. | |
1099 | * Neither the left nor right neighbors are contiguous with | |
1100 | * the new one. | |
1101 | */ | |
1102 | NEW = PREV; | |
1103 | NEW.rm_flags = newext; | |
1104 | error = xfs_rmap_update(cur, &NEW); | |
1105 | if (error) | |
1106 | goto done; | |
1107 | break; | |
1108 | ||
1109 | case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG: | |
1110 | /* | |
1111 | * Setting the first part of a previous oldext extent to newext. | |
1112 | * The left neighbor is contiguous. | |
1113 | */ | |
1114 | NEW = PREV; | |
1115 | NEW.rm_offset += len; | |
1116 | NEW.rm_startblock += len; | |
1117 | NEW.rm_blockcount -= len; | |
1118 | error = xfs_rmap_update(cur, &NEW); | |
1119 | if (error) | |
1120 | goto done; | |
1121 | error = xfs_btree_decrement(cur, 0, &i); | |
1122 | if (error) | |
1123 | goto done; | |
1124 | NEW = LEFT; | |
1125 | NEW.rm_blockcount += len; | |
1126 | error = xfs_rmap_update(cur, &NEW); | |
1127 | if (error) | |
1128 | goto done; | |
1129 | break; | |
1130 | ||
1131 | case RMAP_LEFT_FILLING: | |
1132 | /* | |
1133 | * Setting the first part of a previous oldext extent to newext. | |
1134 | * The left neighbor is not contiguous. | |
1135 | */ | |
1136 | NEW = PREV; | |
1137 | NEW.rm_startblock += len; | |
1138 | NEW.rm_offset += len; | |
1139 | NEW.rm_blockcount -= len; | |
1140 | error = xfs_rmap_update(cur, &NEW); | |
1141 | if (error) | |
1142 | goto done; | |
1143 | NEW.rm_startblock = bno; | |
1144 | NEW.rm_owner = owner; | |
1145 | NEW.rm_offset = offset; | |
1146 | NEW.rm_blockcount = len; | |
1147 | NEW.rm_flags = newext; | |
1148 | cur->bc_rec.r = NEW; | |
1149 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, | |
1150 | len, owner, offset, newext); | |
1151 | error = xfs_btree_insert(cur, &i); | |
1152 | if (error) | |
1153 | goto done; | |
1154 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1155 | break; | |
1156 | ||
1157 | case RMAP_RIGHT_FILLING | RMAP_RIGHT_CONTIG: | |
1158 | /* | |
1159 | * Setting the last part of a previous oldext extent to newext. | |
1160 | * The right neighbor is contiguous with the new allocation. | |
1161 | */ | |
1162 | NEW = PREV; | |
1163 | NEW.rm_blockcount -= len; | |
1164 | error = xfs_rmap_update(cur, &NEW); | |
1165 | if (error) | |
1166 | goto done; | |
1167 | error = xfs_btree_increment(cur, 0, &i); | |
1168 | if (error) | |
1169 | goto done; | |
1170 | NEW = RIGHT; | |
1171 | NEW.rm_offset = offset; | |
1172 | NEW.rm_startblock = bno; | |
1173 | NEW.rm_blockcount += len; | |
1174 | error = xfs_rmap_update(cur, &NEW); | |
1175 | if (error) | |
1176 | goto done; | |
1177 | break; | |
1178 | ||
1179 | case RMAP_RIGHT_FILLING: | |
1180 | /* | |
1181 | * Setting the last part of a previous oldext extent to newext. | |
1182 | * The right neighbor is not contiguous. | |
1183 | */ | |
1184 | NEW = PREV; | |
1185 | NEW.rm_blockcount -= len; | |
1186 | error = xfs_rmap_update(cur, &NEW); | |
1187 | if (error) | |
1188 | goto done; | |
1189 | error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset, | |
1190 | oldext, &i); | |
1191 | if (error) | |
1192 | goto done; | |
1193 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); | |
1194 | NEW.rm_startblock = bno; | |
1195 | NEW.rm_owner = owner; | |
1196 | NEW.rm_offset = offset; | |
1197 | NEW.rm_blockcount = len; | |
1198 | NEW.rm_flags = newext; | |
1199 | cur->bc_rec.r = NEW; | |
1200 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, | |
1201 | len, owner, offset, newext); | |
1202 | error = xfs_btree_insert(cur, &i); | |
1203 | if (error) | |
1204 | goto done; | |
1205 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1206 | break; | |
1207 | ||
1208 | case 0: | |
1209 | /* | |
1210 | * Setting the middle part of a previous oldext extent to | |
1211 | * newext. Contiguity is impossible here. | |
1212 | * One extent becomes three extents. | |
1213 | */ | |
1214 | /* new right extent - oldext */ | |
1215 | NEW.rm_startblock = bno + len; | |
1216 | NEW.rm_owner = owner; | |
1217 | NEW.rm_offset = new_endoff; | |
1218 | NEW.rm_blockcount = PREV.rm_offset + PREV.rm_blockcount - | |
1219 | new_endoff; | |
1220 | NEW.rm_flags = PREV.rm_flags; | |
1221 | error = xfs_rmap_update(cur, &NEW); | |
1222 | if (error) | |
1223 | goto done; | |
1224 | /* new left extent - oldext */ | |
1225 | NEW = PREV; | |
1226 | NEW.rm_blockcount = offset - PREV.rm_offset; | |
1227 | cur->bc_rec.r = NEW; | |
1228 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, | |
1229 | NEW.rm_startblock, NEW.rm_blockcount, | |
1230 | NEW.rm_owner, NEW.rm_offset, | |
1231 | NEW.rm_flags); | |
1232 | error = xfs_btree_insert(cur, &i); | |
1233 | if (error) | |
1234 | goto done; | |
1235 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1236 | /* | |
1237 | * Reset the cursor to the position of the new extent | |
1238 | * we are about to insert as we can't trust it after | |
1239 | * the previous insert. | |
1240 | */ | |
1241 | error = xfs_rmap_lookup_eq(cur, bno, len, owner, offset, | |
1242 | oldext, &i); | |
1243 | if (error) | |
1244 | goto done; | |
1245 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); | |
1246 | /* new middle extent - newext */ | |
1247 | cur->bc_rec.r.rm_flags &= ~XFS_RMAP_UNWRITTEN; | |
1248 | cur->bc_rec.r.rm_flags |= newext; | |
1249 | trace_xfs_rmap_insert(mp, cur->bc_private.a.agno, bno, len, | |
1250 | owner, offset, newext); | |
1251 | error = xfs_btree_insert(cur, &i); | |
1252 | if (error) | |
1253 | goto done; | |
1254 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); | |
1255 | break; | |
1256 | ||
1257 | case RMAP_LEFT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG: | |
1258 | case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG: | |
1259 | case RMAP_LEFT_FILLING | RMAP_RIGHT_CONTIG: | |
1260 | case RMAP_RIGHT_FILLING | RMAP_LEFT_CONTIG: | |
1261 | case RMAP_LEFT_CONTIG | RMAP_RIGHT_CONTIG: | |
1262 | case RMAP_LEFT_CONTIG: | |
1263 | case RMAP_RIGHT_CONTIG: | |
1264 | /* | |
1265 | * These cases are all impossible. | |
1266 | */ | |
1267 | ASSERT(0); | |
1268 | } | |
1269 | ||
1270 | trace_xfs_rmap_convert_done(mp, cur->bc_private.a.agno, bno, len, | |
1271 | unwritten, oinfo); | |
1272 | done: | |
1273 | if (error) | |
1274 | trace_xfs_rmap_convert_error(cur->bc_mp, | |
1275 | cur->bc_private.a.agno, error, _RET_IP_); | |
1276 | return error; | |
1277 | } | |
1278 | ||
1279 | #undef NEW | |
1280 | #undef LEFT | |
1281 | #undef RIGHT | |
1282 | #undef PREV | |
1283 | ||
6c6bdf6f DW |
1284 | /* |
1285 | * Find an extent in the rmap btree and unmap it. For rmap extent types that | |
1286 | * can overlap (data fork rmaps on reflink filesystems) we must be careful | |
1287 | * that the prev/next records in the btree might belong to another owner. | |
1288 | * Therefore we must use delete+insert to alter any of the key fields. | |
1289 | * | |
1290 | * For every other situation there can only be one owner for a given extent, | |
1291 | * so we can call the regular _free function. | |
1292 | */ | |
1293 | STATIC int | |
1294 | xfs_rmap_unmap_shared( | |
1295 | struct xfs_btree_cur *cur, | |
1296 | xfs_agblock_t bno, | |
1297 | xfs_extlen_t len, | |
1298 | bool unwritten, | |
1299 | struct xfs_owner_info *oinfo) | |
1300 | { | |
1301 | struct xfs_mount *mp = cur->bc_mp; | |
1302 | struct xfs_rmap_irec ltrec; | |
1303 | uint64_t ltoff; | |
1304 | int error = 0; | |
1305 | int i; | |
1306 | uint64_t owner; | |
1307 | uint64_t offset; | |
1308 | unsigned int flags; | |
1309 | ||
1310 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); | |
1311 | if (unwritten) | |
1312 | flags |= XFS_RMAP_UNWRITTEN; | |
1313 | trace_xfs_rmap_unmap(mp, cur->bc_private.a.agno, bno, len, | |
1314 | unwritten, oinfo); | |
1315 | ||
1316 | /* | |
1317 | * We should always have a left record because there's a static record | |
1318 | * for the AG headers at rm_startblock == 0 created by mkfs/growfs that | |
1319 | * will not ever be removed from the tree. | |
1320 | */ | |
1321 | error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, flags, | |
1322 | <rec, &i); | |
1323 | if (error) | |
1324 | goto out_error; | |
1325 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
1326 | ltoff = ltrec.rm_offset; | |
1327 | ||
1328 | /* Make sure the extent we found covers the entire freeing range. */ | |
1329 | XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_startblock <= bno && | |
1330 | ltrec.rm_startblock + ltrec.rm_blockcount >= | |
1331 | bno + len, out_error); | |
1332 | ||
1333 | /* Make sure the owner matches what we expect to find in the tree. */ | |
1334 | XFS_WANT_CORRUPTED_GOTO(mp, owner == ltrec.rm_owner, out_error); | |
1335 | ||
1336 | /* Make sure the unwritten flag matches. */ | |
1337 | XFS_WANT_CORRUPTED_GOTO(mp, (flags & XFS_RMAP_UNWRITTEN) == | |
1338 | (ltrec.rm_flags & XFS_RMAP_UNWRITTEN), out_error); | |
1339 | ||
1340 | /* Check the offset. */ | |
1341 | XFS_WANT_CORRUPTED_GOTO(mp, ltrec.rm_offset <= offset, out_error); | |
1342 | XFS_WANT_CORRUPTED_GOTO(mp, offset <= ltoff + ltrec.rm_blockcount, | |
1343 | out_error); | |
1344 | ||
1345 | if (ltrec.rm_startblock == bno && ltrec.rm_blockcount == len) { | |
1346 | /* Exact match, simply remove the record from rmap tree. */ | |
1347 | error = xfs_rmap_delete(cur, ltrec.rm_startblock, | |
1348 | ltrec.rm_blockcount, ltrec.rm_owner, | |
1349 | ltrec.rm_offset, ltrec.rm_flags); | |
1350 | if (error) | |
1351 | goto out_error; | |
1352 | } else if (ltrec.rm_startblock == bno) { | |
1353 | /* | |
1354 | * Overlap left hand side of extent: move the start, trim the | |
1355 | * length and update the current record. | |
1356 | * | |
1357 | * ltbno ltlen | |
1358 | * Orig: |oooooooooooooooooooo| | |
1359 | * Freeing: |fffffffff| | |
1360 | * Result: |rrrrrrrrrr| | |
1361 | * bno len | |
1362 | */ | |
1363 | ||
1364 | /* Delete prev rmap. */ | |
1365 | error = xfs_rmap_delete(cur, ltrec.rm_startblock, | |
1366 | ltrec.rm_blockcount, ltrec.rm_owner, | |
1367 | ltrec.rm_offset, ltrec.rm_flags); | |
1368 | if (error) | |
1369 | goto out_error; | |
1370 | ||
1371 | /* Add an rmap at the new offset. */ | |
1372 | ltrec.rm_startblock += len; | |
1373 | ltrec.rm_blockcount -= len; | |
1374 | ltrec.rm_offset += len; | |
1375 | error = xfs_rmap_insert(cur, ltrec.rm_startblock, | |
1376 | ltrec.rm_blockcount, ltrec.rm_owner, | |
1377 | ltrec.rm_offset, ltrec.rm_flags); | |
1378 | if (error) | |
1379 | goto out_error; | |
1380 | } else if (ltrec.rm_startblock + ltrec.rm_blockcount == bno + len) { | |
1381 | /* | |
1382 | * Overlap right hand side of extent: trim the length and | |
1383 | * update the current record. | |
1384 | * | |
1385 | * ltbno ltlen | |
1386 | * Orig: |oooooooooooooooooooo| | |
1387 | * Freeing: |fffffffff| | |
1388 | * Result: |rrrrrrrrrr| | |
1389 | * bno len | |
1390 | */ | |
1391 | error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock, | |
1392 | ltrec.rm_blockcount, ltrec.rm_owner, | |
1393 | ltrec.rm_offset, ltrec.rm_flags, &i); | |
1394 | if (error) | |
1395 | goto out_error; | |
1396 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
1397 | ltrec.rm_blockcount -= len; | |
1398 | error = xfs_rmap_update(cur, <rec); | |
1399 | if (error) | |
1400 | goto out_error; | |
1401 | } else { | |
1402 | /* | |
1403 | * Overlap middle of extent: trim the length of the existing | |
1404 | * record to the length of the new left-extent size, increment | |
1405 | * the insertion position so we can insert a new record | |
1406 | * containing the remaining right-extent space. | |
1407 | * | |
1408 | * ltbno ltlen | |
1409 | * Orig: |oooooooooooooooooooo| | |
1410 | * Freeing: |fffffffff| | |
1411 | * Result: |rrrrr| |rrrr| | |
1412 | * bno len | |
1413 | */ | |
1414 | xfs_extlen_t orig_len = ltrec.rm_blockcount; | |
1415 | ||
1416 | /* Shrink the left side of the rmap */ | |
1417 | error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock, | |
1418 | ltrec.rm_blockcount, ltrec.rm_owner, | |
1419 | ltrec.rm_offset, ltrec.rm_flags, &i); | |
1420 | if (error) | |
1421 | goto out_error; | |
1422 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
1423 | ltrec.rm_blockcount = bno - ltrec.rm_startblock; | |
1424 | error = xfs_rmap_update(cur, <rec); | |
1425 | if (error) | |
1426 | goto out_error; | |
1427 | ||
1428 | /* Add an rmap at the new offset */ | |
1429 | error = xfs_rmap_insert(cur, bno + len, | |
1430 | orig_len - len - ltrec.rm_blockcount, | |
1431 | ltrec.rm_owner, offset + len, | |
1432 | ltrec.rm_flags); | |
1433 | if (error) | |
1434 | goto out_error; | |
1435 | } | |
1436 | ||
1437 | trace_xfs_rmap_unmap_done(mp, cur->bc_private.a.agno, bno, len, | |
1438 | unwritten, oinfo); | |
1439 | out_error: | |
1440 | if (error) | |
1441 | trace_xfs_rmap_unmap_error(cur->bc_mp, | |
1442 | cur->bc_private.a.agno, error, _RET_IP_); | |
1443 | return error; | |
1444 | } | |
1445 | ||
1446 | /* | |
1447 | * Find an extent in the rmap btree and map it. For rmap extent types that | |
1448 | * can overlap (data fork rmaps on reflink filesystems) we must be careful | |
1449 | * that the prev/next records in the btree might belong to another owner. | |
1450 | * Therefore we must use delete+insert to alter any of the key fields. | |
1451 | * | |
1452 | * For every other situation there can only be one owner for a given extent, | |
1453 | * so we can call the regular _alloc function. | |
1454 | */ | |
1455 | STATIC int | |
1456 | xfs_rmap_map_shared( | |
1457 | struct xfs_btree_cur *cur, | |
1458 | xfs_agblock_t bno, | |
1459 | xfs_extlen_t len, | |
1460 | bool unwritten, | |
1461 | struct xfs_owner_info *oinfo) | |
1462 | { | |
1463 | struct xfs_mount *mp = cur->bc_mp; | |
1464 | struct xfs_rmap_irec ltrec; | |
1465 | struct xfs_rmap_irec gtrec; | |
1466 | int have_gt; | |
1467 | int have_lt; | |
1468 | int error = 0; | |
1469 | int i; | |
1470 | uint64_t owner; | |
1471 | uint64_t offset; | |
1472 | unsigned int flags = 0; | |
1473 | ||
1474 | xfs_owner_info_unpack(oinfo, &owner, &offset, &flags); | |
1475 | if (unwritten) | |
1476 | flags |= XFS_RMAP_UNWRITTEN; | |
1477 | trace_xfs_rmap_map(mp, cur->bc_private.a.agno, bno, len, | |
1478 | unwritten, oinfo); | |
1479 | ||
1480 | /* Is there a left record that abuts our range? */ | |
1481 | error = xfs_rmap_find_left_neighbor(cur, bno, owner, offset, flags, | |
1482 | <rec, &have_lt); | |
1483 | if (error) | |
1484 | goto out_error; | |
1485 | if (have_lt && | |
1486 | !xfs_rmap_is_mergeable(<rec, owner, flags)) | |
1487 | have_lt = 0; | |
1488 | ||
1489 | /* Is there a right record that abuts our range? */ | |
1490 | error = xfs_rmap_lookup_eq(cur, bno + len, len, owner, offset + len, | |
1491 | flags, &have_gt); | |
1492 | if (error) | |
1493 | goto out_error; | |
1494 | if (have_gt) { | |
1495 | error = xfs_rmap_get_rec(cur, >rec, &have_gt); | |
1496 | if (error) | |
1497 | goto out_error; | |
1498 | XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error); | |
1499 | trace_xfs_rmap_find_right_neighbor_result(cur->bc_mp, | |
1500 | cur->bc_private.a.agno, gtrec.rm_startblock, | |
1501 | gtrec.rm_blockcount, gtrec.rm_owner, | |
1502 | gtrec.rm_offset, gtrec.rm_flags); | |
1503 | ||
1504 | if (!xfs_rmap_is_mergeable(>rec, owner, flags)) | |
1505 | have_gt = 0; | |
1506 | } | |
1507 | ||
1508 | if (have_lt && | |
1509 | ltrec.rm_startblock + ltrec.rm_blockcount == bno && | |
1510 | ltrec.rm_offset + ltrec.rm_blockcount == offset) { | |
1511 | /* | |
1512 | * Left edge contiguous, merge into left record. | |
1513 | * | |
1514 | * ltbno ltlen | |
1515 | * orig: |ooooooooo| | |
1516 | * adding: |aaaaaaaaa| | |
1517 | * result: |rrrrrrrrrrrrrrrrrrr| | |
1518 | * bno len | |
1519 | */ | |
1520 | ltrec.rm_blockcount += len; | |
1521 | if (have_gt && | |
1522 | bno + len == gtrec.rm_startblock && | |
1523 | offset + len == gtrec.rm_offset) { | |
1524 | /* | |
1525 | * Right edge also contiguous, delete right record | |
1526 | * and merge into left record. | |
1527 | * | |
1528 | * ltbno ltlen gtbno gtlen | |
1529 | * orig: |ooooooooo| |ooooooooo| | |
1530 | * adding: |aaaaaaaaa| | |
1531 | * result: |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr| | |
1532 | */ | |
1533 | ltrec.rm_blockcount += gtrec.rm_blockcount; | |
1534 | error = xfs_rmap_delete(cur, gtrec.rm_startblock, | |
1535 | gtrec.rm_blockcount, gtrec.rm_owner, | |
1536 | gtrec.rm_offset, gtrec.rm_flags); | |
1537 | if (error) | |
1538 | goto out_error; | |
1539 | } | |
1540 | ||
1541 | /* Point the cursor back to the left record and update. */ | |
1542 | error = xfs_rmap_lookup_eq(cur, ltrec.rm_startblock, | |
1543 | ltrec.rm_blockcount, ltrec.rm_owner, | |
1544 | ltrec.rm_offset, ltrec.rm_flags, &i); | |
1545 | if (error) | |
1546 | goto out_error; | |
1547 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, out_error); | |
1548 | ||
1549 | error = xfs_rmap_update(cur, <rec); | |
1550 | if (error) | |
1551 | goto out_error; | |
1552 | } else if (have_gt && | |
1553 | bno + len == gtrec.rm_startblock && | |
1554 | offset + len == gtrec.rm_offset) { | |
1555 | /* | |
1556 | * Right edge contiguous, merge into right record. | |
1557 | * | |
1558 | * gtbno gtlen | |
1559 | * Orig: |ooooooooo| | |
1560 | * adding: |aaaaaaaaa| | |
1561 | * Result: |rrrrrrrrrrrrrrrrrrr| | |
1562 | * bno len | |
1563 | */ | |
1564 | /* Delete the old record. */ | |
1565 | error = xfs_rmap_delete(cur, gtrec.rm_startblock, | |
1566 | gtrec.rm_blockcount, gtrec.rm_owner, | |
1567 | gtrec.rm_offset, gtrec.rm_flags); | |
1568 | if (error) | |
1569 | goto out_error; | |
1570 | ||
1571 | /* Move the start and re-add it. */ | |
1572 | gtrec.rm_startblock = bno; | |
1573 | gtrec.rm_blockcount += len; | |
1574 | gtrec.rm_offset = offset; | |
1575 | error = xfs_rmap_insert(cur, gtrec.rm_startblock, | |
1576 | gtrec.rm_blockcount, gtrec.rm_owner, | |
1577 | gtrec.rm_offset, gtrec.rm_flags); | |
1578 | if (error) | |
1579 | goto out_error; | |
1580 | } else { | |
1581 | /* | |
1582 | * No contiguous edge with identical owner, insert | |
1583 | * new record at current cursor position. | |
1584 | */ | |
1585 | error = xfs_rmap_insert(cur, bno, len, owner, offset, flags); | |
1586 | if (error) | |
1587 | goto out_error; | |
1588 | } | |
1589 | ||
1590 | trace_xfs_rmap_map_done(mp, cur->bc_private.a.agno, bno, len, | |
1591 | unwritten, oinfo); | |
1592 | out_error: | |
1593 | if (error) | |
1594 | trace_xfs_rmap_map_error(cur->bc_mp, | |
1595 | cur->bc_private.a.agno, error, _RET_IP_); | |
1596 | return error; | |
1597 | } | |
1598 | ||
890e1174 DW |
1599 | struct xfs_rmap_query_range_info { |
1600 | xfs_rmap_query_range_fn fn; | |
1601 | void *priv; | |
1602 | }; | |
1603 | ||
1604 | /* Format btree record and pass to our callback. */ | |
1605 | STATIC int | |
1606 | xfs_rmap_query_range_helper( | |
1607 | struct xfs_btree_cur *cur, | |
1608 | union xfs_btree_rec *rec, | |
1609 | void *priv) | |
1610 | { | |
1611 | struct xfs_rmap_query_range_info *query = priv; | |
1612 | struct xfs_rmap_irec irec; | |
1613 | int error; | |
1614 | ||
1615 | error = xfs_rmap_btrec_to_irec(rec, &irec); | |
1616 | if (error) | |
1617 | return error; | |
1618 | return query->fn(cur, &irec, query->priv); | |
1619 | } | |
1620 | ||
1621 | /* Find all rmaps between two keys. */ | |
1622 | int | |
1623 | xfs_rmap_query_range( | |
1624 | struct xfs_btree_cur *cur, | |
1625 | struct xfs_rmap_irec *low_rec, | |
1626 | struct xfs_rmap_irec *high_rec, | |
1627 | xfs_rmap_query_range_fn fn, | |
1628 | void *priv) | |
1629 | { | |
1630 | union xfs_btree_irec low_brec; | |
1631 | union xfs_btree_irec high_brec; | |
1632 | struct xfs_rmap_query_range_info query; | |
1633 | ||
1634 | low_brec.r = *low_rec; | |
1635 | high_brec.r = *high_rec; | |
1636 | query.priv = priv; | |
1637 | query.fn = fn; | |
1638 | return xfs_btree_query_range(cur, &low_brec, &high_brec, | |
1639 | xfs_rmap_query_range_helper, &query); | |
1640 | } | |
d7f80320 DW |
1641 | |
1642 | /* Clean up after calling xfs_rmap_finish_one. */ | |
1643 | void | |
1644 | xfs_rmap_finish_one_cleanup( | |
1645 | struct xfs_trans *tp, | |
1646 | struct xfs_btree_cur *rcur, | |
1647 | int error) | |
1648 | { | |
1649 | struct xfs_buf *agbp; | |
1650 | ||
1651 | if (rcur == NULL) | |
1652 | return; | |
1653 | agbp = rcur->bc_private.a.agbp; | |
1654 | xfs_btree_del_cursor(rcur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
1655 | if (error) | |
1656 | xfs_trans_brelse(tp, agbp); | |
1657 | } | |
1658 | ||
1659 | /* | |
1660 | * Process one of the deferred rmap operations. We pass back the | |
1661 | * btree cursor to maintain our lock on the rmapbt between calls. | |
1662 | * This saves time and eliminates a buffer deadlock between the | |
1663 | * superblock and the AGF because we'll always grab them in the same | |
1664 | * order. | |
1665 | */ | |
1666 | int | |
1667 | xfs_rmap_finish_one( | |
1668 | struct xfs_trans *tp, | |
1669 | enum xfs_rmap_intent_type type, | |
1670 | __uint64_t owner, | |
1671 | int whichfork, | |
1672 | xfs_fileoff_t startoff, | |
1673 | xfs_fsblock_t startblock, | |
1674 | xfs_filblks_t blockcount, | |
1675 | xfs_exntst_t state, | |
1676 | struct xfs_btree_cur **pcur) | |
1677 | { | |
1678 | struct xfs_mount *mp = tp->t_mountp; | |
1679 | struct xfs_btree_cur *rcur; | |
1680 | struct xfs_buf *agbp = NULL; | |
1681 | int error = 0; | |
1682 | xfs_agnumber_t agno; | |
1683 | struct xfs_owner_info oinfo; | |
1684 | xfs_agblock_t bno; | |
1685 | bool unwritten; | |
1686 | ||
1687 | agno = XFS_FSB_TO_AGNO(mp, startblock); | |
1688 | ASSERT(agno != NULLAGNUMBER); | |
1689 | bno = XFS_FSB_TO_AGBNO(mp, startblock); | |
1690 | ||
1691 | trace_xfs_rmap_deferred(mp, agno, type, bno, owner, whichfork, | |
1692 | startoff, blockcount, state); | |
1693 | ||
1694 | if (XFS_TEST_ERROR(false, mp, | |
1695 | XFS_ERRTAG_RMAP_FINISH_ONE, | |
1696 | XFS_RANDOM_RMAP_FINISH_ONE)) | |
1697 | return -EIO; | |
1698 | ||
1699 | /* | |
1700 | * If we haven't gotten a cursor or the cursor AG doesn't match | |
1701 | * the startblock, get one now. | |
1702 | */ | |
1703 | rcur = *pcur; | |
1704 | if (rcur != NULL && rcur->bc_private.a.agno != agno) { | |
1705 | xfs_rmap_finish_one_cleanup(tp, rcur, 0); | |
1706 | rcur = NULL; | |
1707 | *pcur = NULL; | |
1708 | } | |
1709 | if (rcur == NULL) { | |
1710 | /* | |
1711 | * Refresh the freelist before we start changing the | |
1712 | * rmapbt, because a shape change could cause us to | |
1713 | * allocate blocks. | |
1714 | */ | |
1715 | error = xfs_free_extent_fix_freelist(tp, agno, &agbp); | |
1716 | if (error) | |
1717 | return error; | |
1718 | if (!agbp) | |
1719 | return -EFSCORRUPTED; | |
1720 | ||
1721 | rcur = xfs_rmapbt_init_cursor(mp, tp, agbp, agno); | |
1722 | if (!rcur) { | |
1723 | error = -ENOMEM; | |
1724 | goto out_cur; | |
1725 | } | |
1726 | } | |
1727 | *pcur = rcur; | |
1728 | ||
1729 | xfs_rmap_ino_owner(&oinfo, owner, whichfork, startoff); | |
1730 | unwritten = state == XFS_EXT_UNWRITTEN; | |
1731 | bno = XFS_FSB_TO_AGBNO(rcur->bc_mp, startblock); | |
1732 | ||
1733 | switch (type) { | |
1734 | case XFS_RMAP_ALLOC: | |
1735 | case XFS_RMAP_MAP: | |
1736 | error = xfs_rmap_map(rcur, bno, blockcount, unwritten, &oinfo); | |
1737 | break; | |
6c6bdf6f DW |
1738 | case XFS_RMAP_MAP_SHARED: |
1739 | error = xfs_rmap_map_shared(rcur, bno, blockcount, unwritten, | |
1740 | &oinfo); | |
1741 | break; | |
d7f80320 DW |
1742 | case XFS_RMAP_FREE: |
1743 | case XFS_RMAP_UNMAP: | |
1744 | error = xfs_rmap_unmap(rcur, bno, blockcount, unwritten, | |
1745 | &oinfo); | |
1746 | break; | |
6c6bdf6f DW |
1747 | case XFS_RMAP_UNMAP_SHARED: |
1748 | error = xfs_rmap_unmap_shared(rcur, bno, blockcount, unwritten, | |
1749 | &oinfo); | |
1750 | break; | |
d7f80320 DW |
1751 | case XFS_RMAP_CONVERT: |
1752 | error = xfs_rmap_convert(rcur, bno, blockcount, !unwritten, | |
1753 | &oinfo); | |
1754 | break; | |
1755 | default: | |
1756 | ASSERT(0); | |
1757 | error = -EFSCORRUPTED; | |
1758 | } | |
1759 | return error; | |
1760 | ||
1761 | out_cur: | |
1762 | xfs_trans_brelse(tp, agbp); | |
1763 | ||
1764 | return error; | |
1765 | } | |
1766 | ||
1767 | /* | |
1768 | * Don't defer an rmap if we aren't an rmap filesystem. | |
1769 | */ | |
1770 | static bool | |
1771 | xfs_rmap_update_is_needed( | |
cb8a004a DW |
1772 | struct xfs_mount *mp, |
1773 | int whichfork) | |
d7f80320 | 1774 | { |
cb8a004a | 1775 | return xfs_sb_version_hasrmapbt(&mp->m_sb) && whichfork != XFS_COW_FORK; |
d7f80320 DW |
1776 | } |
1777 | ||
1778 | /* | |
1779 | * Record a rmap intent; the list is kept sorted first by AG and then by | |
1780 | * increasing age. | |
1781 | */ | |
1782 | static int | |
1783 | __xfs_rmap_add( | |
1784 | struct xfs_mount *mp, | |
1785 | struct xfs_defer_ops *dfops, | |
1786 | enum xfs_rmap_intent_type type, | |
1787 | __uint64_t owner, | |
1788 | int whichfork, | |
1789 | struct xfs_bmbt_irec *bmap) | |
1790 | { | |
1791 | struct xfs_rmap_intent *ri; | |
1792 | ||
1793 | trace_xfs_rmap_defer(mp, XFS_FSB_TO_AGNO(mp, bmap->br_startblock), | |
1794 | type, | |
1795 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), | |
1796 | owner, whichfork, | |
1797 | bmap->br_startoff, | |
1798 | bmap->br_blockcount, | |
1799 | bmap->br_state); | |
1800 | ||
1801 | ri = kmem_alloc(sizeof(struct xfs_rmap_intent), KM_SLEEP | KM_NOFS); | |
1802 | INIT_LIST_HEAD(&ri->ri_list); | |
1803 | ri->ri_type = type; | |
1804 | ri->ri_owner = owner; | |
1805 | ri->ri_whichfork = whichfork; | |
1806 | ri->ri_bmap = *bmap; | |
1807 | ||
1808 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_RMAP, &ri->ri_list); | |
1809 | return 0; | |
1810 | } | |
1811 | ||
1812 | /* Map an extent into a file. */ | |
1813 | int | |
1814 | xfs_rmap_map_extent( | |
1815 | struct xfs_mount *mp, | |
1816 | struct xfs_defer_ops *dfops, | |
1817 | struct xfs_inode *ip, | |
1818 | int whichfork, | |
1819 | struct xfs_bmbt_irec *PREV) | |
1820 | { | |
cb8a004a | 1821 | if (!xfs_rmap_update_is_needed(mp, whichfork)) |
d7f80320 DW |
1822 | return 0; |
1823 | ||
6c6bdf6f DW |
1824 | return __xfs_rmap_add(mp, dfops, xfs_is_reflink_inode(ip) ? |
1825 | XFS_RMAP_MAP_SHARED : XFS_RMAP_MAP, ip->i_ino, | |
d7f80320 DW |
1826 | whichfork, PREV); |
1827 | } | |
1828 | ||
1829 | /* Unmap an extent out of a file. */ | |
1830 | int | |
1831 | xfs_rmap_unmap_extent( | |
1832 | struct xfs_mount *mp, | |
1833 | struct xfs_defer_ops *dfops, | |
1834 | struct xfs_inode *ip, | |
1835 | int whichfork, | |
1836 | struct xfs_bmbt_irec *PREV) | |
1837 | { | |
cb8a004a | 1838 | if (!xfs_rmap_update_is_needed(mp, whichfork)) |
d7f80320 DW |
1839 | return 0; |
1840 | ||
6c6bdf6f DW |
1841 | return __xfs_rmap_add(mp, dfops, xfs_is_reflink_inode(ip) ? |
1842 | XFS_RMAP_UNMAP_SHARED : XFS_RMAP_UNMAP, ip->i_ino, | |
d7f80320 DW |
1843 | whichfork, PREV); |
1844 | } | |
1845 | ||
1846 | /* Convert a data fork extent from unwritten to real or vice versa. */ | |
1847 | int | |
1848 | xfs_rmap_convert_extent( | |
1849 | struct xfs_mount *mp, | |
1850 | struct xfs_defer_ops *dfops, | |
1851 | struct xfs_inode *ip, | |
1852 | int whichfork, | |
1853 | struct xfs_bmbt_irec *PREV) | |
1854 | { | |
cb8a004a | 1855 | if (!xfs_rmap_update_is_needed(mp, whichfork)) |
d7f80320 DW |
1856 | return 0; |
1857 | ||
1858 | return __xfs_rmap_add(mp, dfops, XFS_RMAP_CONVERT, ip->i_ino, | |
1859 | whichfork, PREV); | |
1860 | } | |
1861 | ||
1862 | /* Schedule the creation of an rmap for non-file data. */ | |
1863 | int | |
1864 | xfs_rmap_alloc_extent( | |
1865 | struct xfs_mount *mp, | |
1866 | struct xfs_defer_ops *dfops, | |
1867 | xfs_agnumber_t agno, | |
1868 | xfs_agblock_t bno, | |
1869 | xfs_extlen_t len, | |
1870 | __uint64_t owner) | |
1871 | { | |
1872 | struct xfs_bmbt_irec bmap; | |
1873 | ||
cb8a004a | 1874 | if (!xfs_rmap_update_is_needed(mp, XFS_DATA_FORK)) |
d7f80320 DW |
1875 | return 0; |
1876 | ||
1877 | bmap.br_startblock = XFS_AGB_TO_FSB(mp, agno, bno); | |
1878 | bmap.br_blockcount = len; | |
1879 | bmap.br_startoff = 0; | |
1880 | bmap.br_state = XFS_EXT_NORM; | |
1881 | ||
1882 | return __xfs_rmap_add(mp, dfops, XFS_RMAP_ALLOC, owner, | |
1883 | XFS_DATA_FORK, &bmap); | |
1884 | } | |
1885 | ||
1886 | /* Schedule the deletion of an rmap for non-file data. */ | |
1887 | int | |
1888 | xfs_rmap_free_extent( | |
1889 | struct xfs_mount *mp, | |
1890 | struct xfs_defer_ops *dfops, | |
1891 | xfs_agnumber_t agno, | |
1892 | xfs_agblock_t bno, | |
1893 | xfs_extlen_t len, | |
1894 | __uint64_t owner) | |
1895 | { | |
1896 | struct xfs_bmbt_irec bmap; | |
1897 | ||
cb8a004a | 1898 | if (!xfs_rmap_update_is_needed(mp, XFS_DATA_FORK)) |
d7f80320 DW |
1899 | return 0; |
1900 | ||
1901 | bmap.br_startblock = XFS_AGB_TO_FSB(mp, agno, bno); | |
1902 | bmap.br_blockcount = len; | |
1903 | bmap.br_startoff = 0; | |
1904 | bmap.br_state = XFS_EXT_NORM; | |
1905 | ||
1906 | return __xfs_rmap_add(mp, dfops, XFS_RMAP_FREE, owner, | |
1907 | XFS_DATA_FORK, &bmap); | |
1908 | } |