Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 | 6 | #include "xfs.h" |
a844f451 | 7 | #include "xfs_fs.h" |
4fb6e8ad | 8 | #include "xfs_format.h" |
239880ef DC |
9 | #include "xfs_log_format.h" |
10 | #include "xfs_trans_resv.h" | |
dc42375d | 11 | #include "xfs_bit.h" |
5467b34b | 12 | #include "xfs_shared.h" |
1da177e4 | 13 | #include "xfs_mount.h" |
239880ef | 14 | #include "xfs_trans.h" |
1da177e4 | 15 | #include "xfs_trans_priv.h" |
239880ef | 16 | #include "xfs_buf_item.h" |
1da177e4 | 17 | #include "xfs_extfree_item.h" |
1234351c | 18 | #include "xfs_log.h" |
340785cc DW |
19 | #include "xfs_btree.h" |
20 | #include "xfs_rmap.h" | |
1da177e4 LT |
21 | |
22 | ||
23 | kmem_zone_t *xfs_efi_zone; | |
24 | kmem_zone_t *xfs_efd_zone; | |
25 | ||
7bfa31d8 CH |
26 | static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip) |
27 | { | |
28 | return container_of(lip, struct xfs_efi_log_item, efi_item); | |
29 | } | |
1da177e4 | 30 | |
7d795ca3 | 31 | void |
7bfa31d8 CH |
32 | xfs_efi_item_free( |
33 | struct xfs_efi_log_item *efip) | |
7d795ca3 | 34 | { |
b1c5ebb2 | 35 | kmem_free(efip->efi_item.li_lv_shadow); |
7bfa31d8 | 36 | if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS) |
f0e2d93c | 37 | kmem_free(efip); |
7bfa31d8 | 38 | else |
7d795ca3 | 39 | kmem_zone_free(xfs_efi_zone, efip); |
7d795ca3 | 40 | } |
1da177e4 | 41 | |
0612d116 DC |
42 | /* |
43 | * Freeing the efi requires that we remove it from the AIL if it has already | |
44 | * been placed there. However, the EFI may not yet have been placed in the AIL | |
45 | * when called by xfs_efi_release() from EFD processing due to the ordering of | |
46 | * committed vs unpin operations in bulk insert operations. Hence the reference | |
47 | * count to ensure only the last caller frees the EFI. | |
48 | */ | |
49 | void | |
50 | xfs_efi_release( | |
51 | struct xfs_efi_log_item *efip) | |
52 | { | |
53 | ASSERT(atomic_read(&efip->efi_refcount) > 0); | |
54 | if (atomic_dec_and_test(&efip->efi_refcount)) { | |
55 | xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR); | |
56 | xfs_efi_item_free(efip); | |
57 | } | |
58 | } | |
59 | ||
1da177e4 LT |
60 | /* |
61 | * This returns the number of iovecs needed to log the given efi item. | |
62 | * We only need 1 iovec for an efi item. It just logs the efi_log_format | |
63 | * structure. | |
64 | */ | |
166d1368 DC |
65 | static inline int |
66 | xfs_efi_item_sizeof( | |
67 | struct xfs_efi_log_item *efip) | |
68 | { | |
69 | return sizeof(struct xfs_efi_log_format) + | |
70 | (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t); | |
71 | } | |
72 | ||
73 | STATIC void | |
7bfa31d8 | 74 | xfs_efi_item_size( |
166d1368 DC |
75 | struct xfs_log_item *lip, |
76 | int *nvecs, | |
77 | int *nbytes) | |
1da177e4 | 78 | { |
166d1368 DC |
79 | *nvecs += 1; |
80 | *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip)); | |
1da177e4 LT |
81 | } |
82 | ||
83 | /* | |
84 | * This is called to fill in the vector of log iovecs for the | |
85 | * given efi log item. We use only 1 iovec, and we point that | |
86 | * at the efi_log_format structure embedded in the efi item. | |
87 | * It is at this point that we assert that all of the extent | |
88 | * slots in the efi item have been filled. | |
89 | */ | |
90 | STATIC void | |
7bfa31d8 CH |
91 | xfs_efi_item_format( |
92 | struct xfs_log_item *lip, | |
bde7cff6 | 93 | struct xfs_log_vec *lv) |
1da177e4 | 94 | { |
7bfa31d8 | 95 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
bde7cff6 | 96 | struct xfs_log_iovec *vecp = NULL; |
1da177e4 | 97 | |
b199c8a4 DC |
98 | ASSERT(atomic_read(&efip->efi_next_extent) == |
99 | efip->efi_format.efi_nextents); | |
1da177e4 LT |
100 | |
101 | efip->efi_format.efi_type = XFS_LI_EFI; | |
1da177e4 LT |
102 | efip->efi_format.efi_size = 1; |
103 | ||
bde7cff6 | 104 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, |
1234351c CH |
105 | &efip->efi_format, |
106 | xfs_efi_item_sizeof(efip)); | |
1da177e4 LT |
107 | } |
108 | ||
109 | ||
1da177e4 | 110 | /* |
8d99fe92 BF |
111 | * The unpin operation is the last place an EFI is manipulated in the log. It is |
112 | * either inserted in the AIL or aborted in the event of a log I/O error. In | |
113 | * either case, the EFI transaction has been successfully committed to make it | |
114 | * this far. Therefore, we expect whoever committed the EFI to either construct | |
115 | * and commit the EFD or drop the EFD's reference in the event of error. Simply | |
116 | * drop the log's EFI reference now that the log is done with it. | |
1da177e4 | 117 | */ |
1da177e4 | 118 | STATIC void |
7bfa31d8 CH |
119 | xfs_efi_item_unpin( |
120 | struct xfs_log_item *lip, | |
121 | int remove) | |
1da177e4 | 122 | { |
7bfa31d8 | 123 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
5e4b5386 | 124 | xfs_efi_release(efip); |
1da177e4 LT |
125 | } |
126 | ||
8d99fe92 BF |
127 | /* |
128 | * The EFI has been either committed or aborted if the transaction has been | |
129 | * cancelled. If the transaction was cancelled, an EFD isn't going to be | |
130 | * constructed and thus we free the EFI here directly. | |
131 | */ | |
1da177e4 | 132 | STATIC void |
ddf92053 | 133 | xfs_efi_item_release( |
7bfa31d8 | 134 | struct xfs_log_item *lip) |
1da177e4 | 135 | { |
ddf92053 | 136 | xfs_efi_release(EFI_ITEM(lip)); |
1da177e4 LT |
137 | } |
138 | ||
1da177e4 LT |
139 | /* |
140 | * This is the ops vector shared by all efi log items. | |
141 | */ | |
272e42b2 | 142 | static const struct xfs_item_ops xfs_efi_item_ops = { |
7bfa31d8 CH |
143 | .iop_size = xfs_efi_item_size, |
144 | .iop_format = xfs_efi_item_format, | |
7bfa31d8 | 145 | .iop_unpin = xfs_efi_item_unpin, |
ddf92053 | 146 | .iop_release = xfs_efi_item_release, |
1da177e4 LT |
147 | }; |
148 | ||
149 | ||
150 | /* | |
151 | * Allocate and initialize an efi item with the given number of extents. | |
152 | */ | |
7bfa31d8 CH |
153 | struct xfs_efi_log_item * |
154 | xfs_efi_init( | |
155 | struct xfs_mount *mp, | |
156 | uint nextents) | |
1da177e4 LT |
157 | |
158 | { | |
7bfa31d8 | 159 | struct xfs_efi_log_item *efip; |
1da177e4 LT |
160 | uint size; |
161 | ||
162 | ASSERT(nextents > 0); | |
163 | if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { | |
164 | size = (uint)(sizeof(xfs_efi_log_item_t) + | |
165 | ((nextents - 1) * sizeof(xfs_extent_t))); | |
7bfa31d8 | 166 | efip = kmem_zalloc(size, KM_SLEEP); |
1da177e4 | 167 | } else { |
7bfa31d8 | 168 | efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP); |
1da177e4 LT |
169 | } |
170 | ||
43f5efc5 | 171 | xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops); |
1da177e4 | 172 | efip->efi_format.efi_nextents = nextents; |
db9d67d6 | 173 | efip->efi_format.efi_id = (uintptr_t)(void *)efip; |
b199c8a4 | 174 | atomic_set(&efip->efi_next_extent, 0); |
666d644c | 175 | atomic_set(&efip->efi_refcount, 2); |
1da177e4 | 176 | |
7bfa31d8 | 177 | return efip; |
1da177e4 LT |
178 | } |
179 | ||
6d192a9b TS |
180 | /* |
181 | * Copy an EFI format buffer from the given buf, and into the destination | |
182 | * EFI format structure. | |
183 | * The given buffer can be in 32 bit or 64 bit form (which has different padding), | |
184 | * one of which will be the native format for this kernel. | |
185 | * It will handle the conversion of formats if necessary. | |
186 | */ | |
187 | int | |
188 | xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) | |
189 | { | |
4e0d5f92 | 190 | xfs_efi_log_format_t *src_efi_fmt = buf->i_addr; |
6d192a9b TS |
191 | uint i; |
192 | uint len = sizeof(xfs_efi_log_format_t) + | |
193 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t); | |
194 | uint len32 = sizeof(xfs_efi_log_format_32_t) + | |
195 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t); | |
196 | uint len64 = sizeof(xfs_efi_log_format_64_t) + | |
197 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t); | |
198 | ||
199 | if (buf->i_len == len) { | |
200 | memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len); | |
201 | return 0; | |
202 | } else if (buf->i_len == len32) { | |
4e0d5f92 | 203 | xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr; |
6d192a9b TS |
204 | |
205 | dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type; | |
206 | dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size; | |
207 | dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents; | |
208 | dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id; | |
209 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { | |
210 | dst_efi_fmt->efi_extents[i].ext_start = | |
211 | src_efi_fmt_32->efi_extents[i].ext_start; | |
212 | dst_efi_fmt->efi_extents[i].ext_len = | |
213 | src_efi_fmt_32->efi_extents[i].ext_len; | |
214 | } | |
215 | return 0; | |
216 | } else if (buf->i_len == len64) { | |
4e0d5f92 | 217 | xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr; |
6d192a9b TS |
218 | |
219 | dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type; | |
220 | dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size; | |
221 | dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents; | |
222 | dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id; | |
223 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { | |
224 | dst_efi_fmt->efi_extents[i].ext_start = | |
225 | src_efi_fmt_64->efi_extents[i].ext_start; | |
226 | dst_efi_fmt->efi_extents[i].ext_len = | |
227 | src_efi_fmt_64->efi_extents[i].ext_len; | |
228 | } | |
229 | return 0; | |
230 | } | |
2451337d | 231 | return -EFSCORRUPTED; |
6d192a9b TS |
232 | } |
233 | ||
7bfa31d8 | 234 | static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip) |
7d795ca3 | 235 | { |
7bfa31d8 CH |
236 | return container_of(lip, struct xfs_efd_log_item, efd_item); |
237 | } | |
1da177e4 | 238 | |
7bfa31d8 CH |
239 | STATIC void |
240 | xfs_efd_item_free(struct xfs_efd_log_item *efdp) | |
241 | { | |
b1c5ebb2 | 242 | kmem_free(efdp->efd_item.li_lv_shadow); |
7bfa31d8 | 243 | if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS) |
f0e2d93c | 244 | kmem_free(efdp); |
7bfa31d8 | 245 | else |
7d795ca3 | 246 | kmem_zone_free(xfs_efd_zone, efdp); |
7d795ca3 | 247 | } |
1da177e4 LT |
248 | |
249 | /* | |
250 | * This returns the number of iovecs needed to log the given efd item. | |
251 | * We only need 1 iovec for an efd item. It just logs the efd_log_format | |
252 | * structure. | |
253 | */ | |
166d1368 DC |
254 | static inline int |
255 | xfs_efd_item_sizeof( | |
256 | struct xfs_efd_log_item *efdp) | |
257 | { | |
258 | return sizeof(xfs_efd_log_format_t) + | |
259 | (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t); | |
260 | } | |
261 | ||
262 | STATIC void | |
7bfa31d8 | 263 | xfs_efd_item_size( |
166d1368 DC |
264 | struct xfs_log_item *lip, |
265 | int *nvecs, | |
266 | int *nbytes) | |
1da177e4 | 267 | { |
166d1368 DC |
268 | *nvecs += 1; |
269 | *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip)); | |
1da177e4 LT |
270 | } |
271 | ||
272 | /* | |
273 | * This is called to fill in the vector of log iovecs for the | |
274 | * given efd log item. We use only 1 iovec, and we point that | |
275 | * at the efd_log_format structure embedded in the efd item. | |
276 | * It is at this point that we assert that all of the extent | |
277 | * slots in the efd item have been filled. | |
278 | */ | |
279 | STATIC void | |
7bfa31d8 CH |
280 | xfs_efd_item_format( |
281 | struct xfs_log_item *lip, | |
bde7cff6 | 282 | struct xfs_log_vec *lv) |
1da177e4 | 283 | { |
7bfa31d8 | 284 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
bde7cff6 | 285 | struct xfs_log_iovec *vecp = NULL; |
1da177e4 LT |
286 | |
287 | ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); | |
288 | ||
289 | efdp->efd_format.efd_type = XFS_LI_EFD; | |
1da177e4 LT |
290 | efdp->efd_format.efd_size = 1; |
291 | ||
bde7cff6 | 292 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, |
1234351c CH |
293 | &efdp->efd_format, |
294 | xfs_efd_item_sizeof(efdp)); | |
1da177e4 LT |
295 | } |
296 | ||
8d99fe92 BF |
297 | /* |
298 | * The EFD is either committed or aborted if the transaction is cancelled. If | |
299 | * the transaction is cancelled, drop our reference to the EFI and free the EFD. | |
300 | */ | |
1da177e4 | 301 | STATIC void |
ddf92053 | 302 | xfs_efd_item_release( |
7bfa31d8 | 303 | struct xfs_log_item *lip) |
1da177e4 | 304 | { |
8d99fe92 BF |
305 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
306 | ||
ddf92053 CH |
307 | xfs_efi_release(efdp->efd_efip); |
308 | xfs_efd_item_free(efdp); | |
1da177e4 LT |
309 | } |
310 | ||
1da177e4 LT |
311 | /* |
312 | * This is the ops vector shared by all efd log items. | |
313 | */ | |
272e42b2 | 314 | static const struct xfs_item_ops xfs_efd_item_ops = { |
9ce632a2 | 315 | .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED, |
7bfa31d8 CH |
316 | .iop_size = xfs_efd_item_size, |
317 | .iop_format = xfs_efd_item_format, | |
ddf92053 | 318 | .iop_release = xfs_efd_item_release, |
1da177e4 LT |
319 | }; |
320 | ||
1da177e4 LT |
321 | /* |
322 | * Allocate and initialize an efd item with the given number of extents. | |
323 | */ | |
7bfa31d8 CH |
324 | struct xfs_efd_log_item * |
325 | xfs_efd_init( | |
326 | struct xfs_mount *mp, | |
327 | struct xfs_efi_log_item *efip, | |
328 | uint nextents) | |
1da177e4 LT |
329 | |
330 | { | |
7bfa31d8 | 331 | struct xfs_efd_log_item *efdp; |
1da177e4 LT |
332 | uint size; |
333 | ||
334 | ASSERT(nextents > 0); | |
335 | if (nextents > XFS_EFD_MAX_FAST_EXTENTS) { | |
336 | size = (uint)(sizeof(xfs_efd_log_item_t) + | |
337 | ((nextents - 1) * sizeof(xfs_extent_t))); | |
7bfa31d8 | 338 | efdp = kmem_zalloc(size, KM_SLEEP); |
1da177e4 | 339 | } else { |
7bfa31d8 | 340 | efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP); |
1da177e4 LT |
341 | } |
342 | ||
43f5efc5 | 343 | xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops); |
1da177e4 LT |
344 | efdp->efd_efip = efip; |
345 | efdp->efd_format.efd_nextents = nextents; | |
346 | efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; | |
347 | ||
7bfa31d8 | 348 | return efdp; |
1da177e4 | 349 | } |
dc42375d DW |
350 | |
351 | /* | |
352 | * Process an extent free intent item that was recovered from | |
353 | * the log. We need to free the extents that it describes. | |
354 | */ | |
355 | int | |
356 | xfs_efi_recover( | |
357 | struct xfs_mount *mp, | |
358 | struct xfs_efi_log_item *efip) | |
359 | { | |
360 | struct xfs_efd_log_item *efdp; | |
361 | struct xfs_trans *tp; | |
362 | int i; | |
363 | int error = 0; | |
364 | xfs_extent_t *extp; | |
365 | xfs_fsblock_t startblock_fsb; | |
366 | ||
367 | ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)); | |
368 | ||
369 | /* | |
370 | * First check the validity of the extents described by the | |
371 | * EFI. If any are bad, then assume that all are bad and | |
372 | * just toss the EFI. | |
373 | */ | |
374 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { | |
e127fafd | 375 | extp = &efip->efi_format.efi_extents[i]; |
dc42375d DW |
376 | startblock_fsb = XFS_BB_TO_FSB(mp, |
377 | XFS_FSB_TO_DADDR(mp, extp->ext_start)); | |
e127fafd DW |
378 | if (startblock_fsb == 0 || |
379 | extp->ext_len == 0 || | |
380 | startblock_fsb >= mp->m_sb.sb_dblocks || | |
381 | extp->ext_len >= mp->m_sb.sb_agblocks) { | |
dc42375d DW |
382 | /* |
383 | * This will pull the EFI from the AIL and | |
384 | * free the memory associated with it. | |
385 | */ | |
386 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); | |
387 | xfs_efi_release(efip); | |
388 | return -EIO; | |
389 | } | |
390 | } | |
391 | ||
392 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); | |
393 | if (error) | |
394 | return error; | |
395 | efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents); | |
396 | ||
397 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { | |
e127fafd | 398 | extp = &efip->efi_format.efi_extents[i]; |
dc42375d | 399 | error = xfs_trans_free_extent(tp, efdp, extp->ext_start, |
7280feda DW |
400 | extp->ext_len, |
401 | &XFS_RMAP_OINFO_ANY_OWNER, false); | |
dc42375d DW |
402 | if (error) |
403 | goto abort_error; | |
404 | ||
405 | } | |
406 | ||
407 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); | |
408 | error = xfs_trans_commit(tp); | |
409 | return error; | |
410 | ||
411 | abort_error: | |
412 | xfs_trans_cancel(tp); | |
413 | return error; | |
414 | } |