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