blob: 6638bc725b232a2a457e535fcebd1b44c66754b2 [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"
34#include "xfs_trans_space.h"
35#include "xfs_trace.h"
36#include "xfs_error.h"
37#include "xfs_extent_busy.h"
38
Darrick J. Wong4b8ed672016-08-03 11:39:05 +100039/*
40 * Lookup the first record less than or equal to [bno, len, owner, offset]
41 * in the btree given by cur.
42 */
43int
44xfs_rmap_lookup_le(
45 struct xfs_btree_cur *cur,
46 xfs_agblock_t bno,
47 xfs_extlen_t len,
48 uint64_t owner,
49 uint64_t offset,
50 unsigned int flags,
51 int *stat)
52{
53 cur->bc_rec.r.rm_startblock = bno;
54 cur->bc_rec.r.rm_blockcount = len;
55 cur->bc_rec.r.rm_owner = owner;
56 cur->bc_rec.r.rm_offset = offset;
57 cur->bc_rec.r.rm_flags = flags;
58 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
59}
60
61/*
62 * Lookup the record exactly matching [bno, len, owner, offset]
63 * in the btree given by cur.
64 */
65int
66xfs_rmap_lookup_eq(
67 struct xfs_btree_cur *cur,
68 xfs_agblock_t bno,
69 xfs_extlen_t len,
70 uint64_t owner,
71 uint64_t offset,
72 unsigned int flags,
73 int *stat)
74{
75 cur->bc_rec.r.rm_startblock = bno;
76 cur->bc_rec.r.rm_blockcount = len;
77 cur->bc_rec.r.rm_owner = owner;
78 cur->bc_rec.r.rm_offset = offset;
79 cur->bc_rec.r.rm_flags = flags;
80 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
81}
82
83/*
84 * Update the record referred to by cur to the value given
85 * by [bno, len, owner, offset].
86 * This either works (return 0) or gets an EFSCORRUPTED error.
87 */
88STATIC int
89xfs_rmap_update(
90 struct xfs_btree_cur *cur,
91 struct xfs_rmap_irec *irec)
92{
93 union xfs_btree_rec rec;
94
95 rec.rmap.rm_startblock = cpu_to_be32(irec->rm_startblock);
96 rec.rmap.rm_blockcount = cpu_to_be32(irec->rm_blockcount);
97 rec.rmap.rm_owner = cpu_to_be64(irec->rm_owner);
98 rec.rmap.rm_offset = cpu_to_be64(
99 xfs_rmap_irec_offset_pack(irec));
100 return xfs_btree_update(cur, &rec);
101}
102
103static int
104xfs_rmap_btrec_to_irec(
105 union xfs_btree_rec *rec,
106 struct xfs_rmap_irec *irec)
107{
108 irec->rm_flags = 0;
109 irec->rm_startblock = be32_to_cpu(rec->rmap.rm_startblock);
110 irec->rm_blockcount = be32_to_cpu(rec->rmap.rm_blockcount);
111 irec->rm_owner = be64_to_cpu(rec->rmap.rm_owner);
112 return xfs_rmap_irec_offset_unpack(be64_to_cpu(rec->rmap.rm_offset),
113 irec);
114}
115
116/*
117 * Get the data from the pointed-to record.
118 */
119int
120xfs_rmap_get_rec(
121 struct xfs_btree_cur *cur,
122 struct xfs_rmap_irec *irec,
123 int *stat)
124{
125 union xfs_btree_rec *rec;
126 int error;
127
128 error = xfs_btree_get_rec(cur, &rec, stat);
129 if (error || !*stat)
130 return error;
131
132 return xfs_rmap_btrec_to_irec(rec, irec);
133}
134
Darrick J. Wong673930c2016-08-03 11:33:43 +1000135int
136xfs_rmap_free(
137 struct xfs_trans *tp,
138 struct xfs_buf *agbp,
139 xfs_agnumber_t agno,
140 xfs_agblock_t bno,
141 xfs_extlen_t len,
142 struct xfs_owner_info *oinfo)
143{
144 struct xfs_mount *mp = tp->t_mountp;
145 int error = 0;
146
147 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
148 return 0;
149
150 trace_xfs_rmap_unmap(mp, agno, bno, len, false, oinfo);
151 if (1)
152 goto out_error;
153 trace_xfs_rmap_unmap_done(mp, agno, bno, len, false, oinfo);
154 return 0;
155
156out_error:
157 trace_xfs_rmap_unmap_error(mp, agno, error, _RET_IP_);
158 return error;
159}
160
161int
162xfs_rmap_alloc(
163 struct xfs_trans *tp,
164 struct xfs_buf *agbp,
165 xfs_agnumber_t agno,
166 xfs_agblock_t bno,
167 xfs_extlen_t len,
168 struct xfs_owner_info *oinfo)
169{
170 struct xfs_mount *mp = tp->t_mountp;
171 int error = 0;
172
173 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
174 return 0;
175
176 trace_xfs_rmap_map(mp, agno, bno, len, false, oinfo);
177 if (1)
178 goto out_error;
179 trace_xfs_rmap_map_done(mp, agno, bno, len, false, oinfo);
180 return 0;
181
182out_error:
183 trace_xfs_rmap_map_error(mp, agno, error, _RET_IP_);
184 return error;
185}
Darrick J. Wongc5438382016-08-03 11:42:39 +1000186
187struct xfs_rmap_query_range_info {
188 xfs_rmap_query_range_fn fn;
189 void *priv;
190};
191
192/* Format btree record and pass to our callback. */
193STATIC int
194xfs_rmap_query_range_helper(
195 struct xfs_btree_cur *cur,
196 union xfs_btree_rec *rec,
197 void *priv)
198{
199 struct xfs_rmap_query_range_info *query = priv;
200 struct xfs_rmap_irec irec;
201 int error;
202
203 error = xfs_rmap_btrec_to_irec(rec, &irec);
204 if (error)
205 return error;
206 return query->fn(cur, &irec, query->priv);
207}
208
209/* Find all rmaps between two keys. */
210int
211xfs_rmap_query_range(
212 struct xfs_btree_cur *cur,
213 struct xfs_rmap_irec *low_rec,
214 struct xfs_rmap_irec *high_rec,
215 xfs_rmap_query_range_fn fn,
216 void *priv)
217{
218 union xfs_btree_irec low_brec;
219 union xfs_btree_irec high_brec;
220 struct xfs_rmap_query_range_info query;
221
222 low_brec.r = *low_rec;
223 high_brec.r = *high_rec;
224 query.priv = priv;
225 query.fn = fn;
226 return xfs_btree_query_range(cur, &low_brec, &high_brec,
227 xfs_rmap_query_range_helper, &query);
228}