]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
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 | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
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. | |
1da177e4 | 13 | * |
7b718769 NS |
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 | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
4fb6e8ad | 20 | #include "xfs_format.h" |
239880ef DC |
21 | #include "xfs_log_format.h" |
22 | #include "xfs_trans_resv.h" | |
dc42375d | 23 | #include "xfs_bit.h" |
1da177e4 | 24 | #include "xfs_mount.h" |
239880ef | 25 | #include "xfs_trans.h" |
1da177e4 | 26 | #include "xfs_trans_priv.h" |
239880ef | 27 | #include "xfs_buf_item.h" |
1da177e4 | 28 | #include "xfs_extfree_item.h" |
1234351c | 29 | #include "xfs_log.h" |
340785cc DW |
30 | #include "xfs_btree.h" |
31 | #include "xfs_rmap.h" | |
1da177e4 LT |
32 | |
33 | ||
34 | kmem_zone_t *xfs_efi_zone; | |
35 | kmem_zone_t *xfs_efd_zone; | |
36 | ||
7bfa31d8 CH |
37 | static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip) |
38 | { | |
39 | return container_of(lip, struct xfs_efi_log_item, efi_item); | |
40 | } | |
1da177e4 | 41 | |
7d795ca3 | 42 | void |
7bfa31d8 CH |
43 | xfs_efi_item_free( |
44 | struct xfs_efi_log_item *efip) | |
7d795ca3 | 45 | { |
b1c5ebb2 | 46 | kmem_free(efip->efi_item.li_lv_shadow); |
7bfa31d8 | 47 | if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS) |
f0e2d93c | 48 | kmem_free(efip); |
7bfa31d8 | 49 | else |
7d795ca3 | 50 | kmem_zone_free(xfs_efi_zone, efip); |
7d795ca3 | 51 | } |
1da177e4 LT |
52 | |
53 | /* | |
54 | * This returns the number of iovecs needed to log the given efi item. | |
55 | * We only need 1 iovec for an efi item. It just logs the efi_log_format | |
56 | * structure. | |
57 | */ | |
166d1368 DC |
58 | static inline int |
59 | xfs_efi_item_sizeof( | |
60 | struct xfs_efi_log_item *efip) | |
61 | { | |
62 | return sizeof(struct xfs_efi_log_format) + | |
63 | (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t); | |
64 | } | |
65 | ||
66 | STATIC void | |
7bfa31d8 | 67 | xfs_efi_item_size( |
166d1368 DC |
68 | struct xfs_log_item *lip, |
69 | int *nvecs, | |
70 | int *nbytes) | |
1da177e4 | 71 | { |
166d1368 DC |
72 | *nvecs += 1; |
73 | *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip)); | |
1da177e4 LT |
74 | } |
75 | ||
76 | /* | |
77 | * This is called to fill in the vector of log iovecs for the | |
78 | * given efi log item. We use only 1 iovec, and we point that | |
79 | * at the efi_log_format structure embedded in the efi item. | |
80 | * It is at this point that we assert that all of the extent | |
81 | * slots in the efi item have been filled. | |
82 | */ | |
83 | STATIC void | |
7bfa31d8 CH |
84 | xfs_efi_item_format( |
85 | struct xfs_log_item *lip, | |
bde7cff6 | 86 | struct xfs_log_vec *lv) |
1da177e4 | 87 | { |
7bfa31d8 | 88 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
bde7cff6 | 89 | struct xfs_log_iovec *vecp = NULL; |
1da177e4 | 90 | |
b199c8a4 DC |
91 | ASSERT(atomic_read(&efip->efi_next_extent) == |
92 | efip->efi_format.efi_nextents); | |
1da177e4 LT |
93 | |
94 | efip->efi_format.efi_type = XFS_LI_EFI; | |
1da177e4 LT |
95 | efip->efi_format.efi_size = 1; |
96 | ||
bde7cff6 | 97 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, |
1234351c CH |
98 | &efip->efi_format, |
99 | xfs_efi_item_sizeof(efip)); | |
1da177e4 LT |
100 | } |
101 | ||
102 | ||
103 | /* | |
104 | * Pinning has no meaning for an efi item, so just return. | |
105 | */ | |
1da177e4 | 106 | STATIC void |
7bfa31d8 CH |
107 | xfs_efi_item_pin( |
108 | struct xfs_log_item *lip) | |
1da177e4 | 109 | { |
1da177e4 LT |
110 | } |
111 | ||
1da177e4 | 112 | /* |
8d99fe92 BF |
113 | * The unpin operation is the last place an EFI is manipulated in the log. It is |
114 | * either inserted in the AIL or aborted in the event of a log I/O error. In | |
115 | * either case, the EFI transaction has been successfully committed to make it | |
116 | * this far. Therefore, we expect whoever committed the EFI to either construct | |
117 | * and commit the EFD or drop the EFD's reference in the event of error. Simply | |
118 | * drop the log's EFI reference now that the log is done with it. | |
1da177e4 | 119 | */ |
1da177e4 | 120 | STATIC void |
7bfa31d8 CH |
121 | xfs_efi_item_unpin( |
122 | struct xfs_log_item *lip, | |
123 | int remove) | |
1da177e4 | 124 | { |
7bfa31d8 | 125 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
5e4b5386 | 126 | xfs_efi_release(efip); |
1da177e4 LT |
127 | } |
128 | ||
129 | /* | |
43ff2122 CH |
130 | * Efi items have no locking or pushing. However, since EFIs are pulled from |
131 | * the AIL when their corresponding EFDs are committed to disk, their situation | |
132 | * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller | |
133 | * will eventually flush the log. This should help in getting the EFI out of | |
134 | * the AIL. | |
1da177e4 | 135 | */ |
1da177e4 | 136 | STATIC uint |
43ff2122 CH |
137 | xfs_efi_item_push( |
138 | struct xfs_log_item *lip, | |
139 | struct list_head *buffer_list) | |
1da177e4 LT |
140 | { |
141 | return XFS_ITEM_PINNED; | |
142 | } | |
143 | ||
8d99fe92 BF |
144 | /* |
145 | * The EFI has been either committed or aborted if the transaction has been | |
146 | * cancelled. If the transaction was cancelled, an EFD isn't going to be | |
147 | * constructed and thus we free the EFI here directly. | |
148 | */ | |
1da177e4 | 149 | STATIC void |
7bfa31d8 CH |
150 | xfs_efi_item_unlock( |
151 | struct xfs_log_item *lip) | |
1da177e4 | 152 | { |
7bfa31d8 CH |
153 | if (lip->li_flags & XFS_LI_ABORTED) |
154 | xfs_efi_item_free(EFI_ITEM(lip)); | |
1da177e4 LT |
155 | } |
156 | ||
157 | /* | |
b199c8a4 | 158 | * The EFI is logged only once and cannot be moved in the log, so simply return |
666d644c | 159 | * the lsn at which it's been logged. |
1da177e4 | 160 | */ |
1da177e4 | 161 | STATIC xfs_lsn_t |
7bfa31d8 CH |
162 | xfs_efi_item_committed( |
163 | struct xfs_log_item *lip, | |
164 | xfs_lsn_t lsn) | |
1da177e4 LT |
165 | { |
166 | return lsn; | |
167 | } | |
168 | ||
1da177e4 LT |
169 | /* |
170 | * The EFI dependency tracking op doesn't do squat. It can't because | |
171 | * it doesn't know where the free extent is coming from. The dependency | |
172 | * tracking has to be handled by the "enclosing" metadata object. For | |
173 | * example, for inodes, the inode is locked throughout the extent freeing | |
174 | * so the dependency should be recorded there. | |
175 | */ | |
1da177e4 | 176 | STATIC void |
7bfa31d8 CH |
177 | xfs_efi_item_committing( |
178 | struct xfs_log_item *lip, | |
179 | xfs_lsn_t lsn) | |
1da177e4 | 180 | { |
1da177e4 LT |
181 | } |
182 | ||
183 | /* | |
184 | * This is the ops vector shared by all efi log items. | |
185 | */ | |
272e42b2 | 186 | static const struct xfs_item_ops xfs_efi_item_ops = { |
7bfa31d8 CH |
187 | .iop_size = xfs_efi_item_size, |
188 | .iop_format = xfs_efi_item_format, | |
189 | .iop_pin = xfs_efi_item_pin, | |
190 | .iop_unpin = xfs_efi_item_unpin, | |
7bfa31d8 CH |
191 | .iop_unlock = xfs_efi_item_unlock, |
192 | .iop_committed = xfs_efi_item_committed, | |
193 | .iop_push = xfs_efi_item_push, | |
194 | .iop_committing = xfs_efi_item_committing | |
1da177e4 LT |
195 | }; |
196 | ||
197 | ||
198 | /* | |
199 | * Allocate and initialize an efi item with the given number of extents. | |
200 | */ | |
7bfa31d8 CH |
201 | struct xfs_efi_log_item * |
202 | xfs_efi_init( | |
203 | struct xfs_mount *mp, | |
204 | uint nextents) | |
1da177e4 LT |
205 | |
206 | { | |
7bfa31d8 | 207 | struct xfs_efi_log_item *efip; |
1da177e4 LT |
208 | uint size; |
209 | ||
210 | ASSERT(nextents > 0); | |
211 | if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { | |
212 | size = (uint)(sizeof(xfs_efi_log_item_t) + | |
213 | ((nextents - 1) * sizeof(xfs_extent_t))); | |
7bfa31d8 | 214 | efip = kmem_zalloc(size, KM_SLEEP); |
1da177e4 | 215 | } else { |
7bfa31d8 | 216 | efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP); |
1da177e4 LT |
217 | } |
218 | ||
43f5efc5 | 219 | xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops); |
1da177e4 | 220 | efip->efi_format.efi_nextents = nextents; |
db9d67d6 | 221 | efip->efi_format.efi_id = (uintptr_t)(void *)efip; |
b199c8a4 | 222 | atomic_set(&efip->efi_next_extent, 0); |
666d644c | 223 | atomic_set(&efip->efi_refcount, 2); |
1da177e4 | 224 | |
7bfa31d8 | 225 | return efip; |
1da177e4 LT |
226 | } |
227 | ||
6d192a9b TS |
228 | /* |
229 | * Copy an EFI format buffer from the given buf, and into the destination | |
230 | * EFI format structure. | |
231 | * The given buffer can be in 32 bit or 64 bit form (which has different padding), | |
232 | * one of which will be the native format for this kernel. | |
233 | * It will handle the conversion of formats if necessary. | |
234 | */ | |
235 | int | |
236 | xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) | |
237 | { | |
4e0d5f92 | 238 | xfs_efi_log_format_t *src_efi_fmt = buf->i_addr; |
6d192a9b TS |
239 | uint i; |
240 | uint len = sizeof(xfs_efi_log_format_t) + | |
241 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t); | |
242 | uint len32 = sizeof(xfs_efi_log_format_32_t) + | |
243 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t); | |
244 | uint len64 = sizeof(xfs_efi_log_format_64_t) + | |
245 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t); | |
246 | ||
247 | if (buf->i_len == len) { | |
248 | memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len); | |
249 | return 0; | |
250 | } else if (buf->i_len == len32) { | |
4e0d5f92 | 251 | xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr; |
6d192a9b TS |
252 | |
253 | dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type; | |
254 | dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size; | |
255 | dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents; | |
256 | dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id; | |
257 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { | |
258 | dst_efi_fmt->efi_extents[i].ext_start = | |
259 | src_efi_fmt_32->efi_extents[i].ext_start; | |
260 | dst_efi_fmt->efi_extents[i].ext_len = | |
261 | src_efi_fmt_32->efi_extents[i].ext_len; | |
262 | } | |
263 | return 0; | |
264 | } else if (buf->i_len == len64) { | |
4e0d5f92 | 265 | xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr; |
6d192a9b TS |
266 | |
267 | dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type; | |
268 | dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size; | |
269 | dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents; | |
270 | dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id; | |
271 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { | |
272 | dst_efi_fmt->efi_extents[i].ext_start = | |
273 | src_efi_fmt_64->efi_extents[i].ext_start; | |
274 | dst_efi_fmt->efi_extents[i].ext_len = | |
275 | src_efi_fmt_64->efi_extents[i].ext_len; | |
276 | } | |
277 | return 0; | |
278 | } | |
2451337d | 279 | return -EFSCORRUPTED; |
6d192a9b TS |
280 | } |
281 | ||
1da177e4 | 282 | /* |
e32a1d1f BF |
283 | * Freeing the efi requires that we remove it from the AIL if it has already |
284 | * been placed there. However, the EFI may not yet have been placed in the AIL | |
285 | * when called by xfs_efi_release() from EFD processing due to the ordering of | |
286 | * committed vs unpin operations in bulk insert operations. Hence the reference | |
287 | * count to ensure only the last caller frees the EFI. | |
1da177e4 LT |
288 | */ |
289 | void | |
5e4b5386 BF |
290 | xfs_efi_release( |
291 | struct xfs_efi_log_item *efip) | |
1da177e4 | 292 | { |
e32a1d1f | 293 | if (atomic_dec_and_test(&efip->efi_refcount)) { |
146e54b7 | 294 | xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR); |
e32a1d1f BF |
295 | xfs_efi_item_free(efip); |
296 | } | |
1da177e4 LT |
297 | } |
298 | ||
7bfa31d8 | 299 | static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip) |
7d795ca3 | 300 | { |
7bfa31d8 CH |
301 | return container_of(lip, struct xfs_efd_log_item, efd_item); |
302 | } | |
1da177e4 | 303 | |
7bfa31d8 CH |
304 | STATIC void |
305 | xfs_efd_item_free(struct xfs_efd_log_item *efdp) | |
306 | { | |
b1c5ebb2 | 307 | kmem_free(efdp->efd_item.li_lv_shadow); |
7bfa31d8 | 308 | if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS) |
f0e2d93c | 309 | kmem_free(efdp); |
7bfa31d8 | 310 | else |
7d795ca3 | 311 | kmem_zone_free(xfs_efd_zone, efdp); |
7d795ca3 | 312 | } |
1da177e4 LT |
313 | |
314 | /* | |
315 | * This returns the number of iovecs needed to log the given efd item. | |
316 | * We only need 1 iovec for an efd item. It just logs the efd_log_format | |
317 | * structure. | |
318 | */ | |
166d1368 DC |
319 | static inline int |
320 | xfs_efd_item_sizeof( | |
321 | struct xfs_efd_log_item *efdp) | |
322 | { | |
323 | return sizeof(xfs_efd_log_format_t) + | |
324 | (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t); | |
325 | } | |
326 | ||
327 | STATIC void | |
7bfa31d8 | 328 | xfs_efd_item_size( |
166d1368 DC |
329 | struct xfs_log_item *lip, |
330 | int *nvecs, | |
331 | int *nbytes) | |
1da177e4 | 332 | { |
166d1368 DC |
333 | *nvecs += 1; |
334 | *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip)); | |
1da177e4 LT |
335 | } |
336 | ||
337 | /* | |
338 | * This is called to fill in the vector of log iovecs for the | |
339 | * given efd log item. We use only 1 iovec, and we point that | |
340 | * at the efd_log_format structure embedded in the efd item. | |
341 | * It is at this point that we assert that all of the extent | |
342 | * slots in the efd item have been filled. | |
343 | */ | |
344 | STATIC void | |
7bfa31d8 CH |
345 | xfs_efd_item_format( |
346 | struct xfs_log_item *lip, | |
bde7cff6 | 347 | struct xfs_log_vec *lv) |
1da177e4 | 348 | { |
7bfa31d8 | 349 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
bde7cff6 | 350 | struct xfs_log_iovec *vecp = NULL; |
1da177e4 LT |
351 | |
352 | ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); | |
353 | ||
354 | efdp->efd_format.efd_type = XFS_LI_EFD; | |
1da177e4 LT |
355 | efdp->efd_format.efd_size = 1; |
356 | ||
bde7cff6 | 357 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, |
1234351c CH |
358 | &efdp->efd_format, |
359 | xfs_efd_item_sizeof(efdp)); | |
1da177e4 LT |
360 | } |
361 | ||
1da177e4 LT |
362 | /* |
363 | * Pinning has no meaning for an efd item, so just return. | |
364 | */ | |
1da177e4 | 365 | STATIC void |
7bfa31d8 CH |
366 | xfs_efd_item_pin( |
367 | struct xfs_log_item *lip) | |
1da177e4 | 368 | { |
1da177e4 LT |
369 | } |
370 | ||
1da177e4 LT |
371 | /* |
372 | * Since pinning has no meaning for an efd item, unpinning does | |
373 | * not either. | |
374 | */ | |
1da177e4 | 375 | STATIC void |
7bfa31d8 CH |
376 | xfs_efd_item_unpin( |
377 | struct xfs_log_item *lip, | |
378 | int remove) | |
1da177e4 | 379 | { |
1da177e4 LT |
380 | } |
381 | ||
382 | /* | |
43ff2122 CH |
383 | * There isn't much you can do to push on an efd item. It is simply stuck |
384 | * waiting for the log to be flushed to disk. | |
1da177e4 | 385 | */ |
1da177e4 | 386 | STATIC uint |
43ff2122 CH |
387 | xfs_efd_item_push( |
388 | struct xfs_log_item *lip, | |
389 | struct list_head *buffer_list) | |
1da177e4 | 390 | { |
43ff2122 | 391 | return XFS_ITEM_PINNED; |
1da177e4 LT |
392 | } |
393 | ||
8d99fe92 BF |
394 | /* |
395 | * The EFD is either committed or aborted if the transaction is cancelled. If | |
396 | * the transaction is cancelled, drop our reference to the EFI and free the EFD. | |
397 | */ | |
1da177e4 | 398 | STATIC void |
7bfa31d8 CH |
399 | xfs_efd_item_unlock( |
400 | struct xfs_log_item *lip) | |
1da177e4 | 401 | { |
8d99fe92 BF |
402 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
403 | ||
404 | if (lip->li_flags & XFS_LI_ABORTED) { | |
405 | xfs_efi_release(efdp->efd_efip); | |
406 | xfs_efd_item_free(efdp); | |
407 | } | |
1da177e4 LT |
408 | } |
409 | ||
410 | /* | |
8d99fe92 BF |
411 | * When the efd item is committed to disk, all we need to do is delete our |
412 | * reference to our partner efi item and then free ourselves. Since we're | |
413 | * freeing ourselves we must return -1 to keep the transaction code from further | |
414 | * referencing this item. | |
1da177e4 | 415 | */ |
1da177e4 | 416 | STATIC xfs_lsn_t |
7bfa31d8 CH |
417 | xfs_efd_item_committed( |
418 | struct xfs_log_item *lip, | |
419 | xfs_lsn_t lsn) | |
1da177e4 | 420 | { |
7bfa31d8 CH |
421 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
422 | ||
1da177e4 | 423 | /* |
8d99fe92 BF |
424 | * Drop the EFI reference regardless of whether the EFD has been |
425 | * aborted. Once the EFD transaction is constructed, it is the sole | |
426 | * responsibility of the EFD to release the EFI (even if the EFI is | |
427 | * aborted due to log I/O error). | |
1da177e4 | 428 | */ |
8d99fe92 | 429 | xfs_efi_release(efdp->efd_efip); |
7d795ca3 | 430 | xfs_efd_item_free(efdp); |
8d99fe92 | 431 | |
1da177e4 LT |
432 | return (xfs_lsn_t)-1; |
433 | } | |
434 | ||
1da177e4 LT |
435 | /* |
436 | * The EFD dependency tracking op doesn't do squat. It can't because | |
437 | * it doesn't know where the free extent is coming from. The dependency | |
438 | * tracking has to be handled by the "enclosing" metadata object. For | |
439 | * example, for inodes, the inode is locked throughout the extent freeing | |
440 | * so the dependency should be recorded there. | |
441 | */ | |
1da177e4 | 442 | STATIC void |
7bfa31d8 CH |
443 | xfs_efd_item_committing( |
444 | struct xfs_log_item *lip, | |
445 | xfs_lsn_t lsn) | |
1da177e4 | 446 | { |
1da177e4 LT |
447 | } |
448 | ||
449 | /* | |
450 | * This is the ops vector shared by all efd log items. | |
451 | */ | |
272e42b2 | 452 | static const struct xfs_item_ops xfs_efd_item_ops = { |
7bfa31d8 CH |
453 | .iop_size = xfs_efd_item_size, |
454 | .iop_format = xfs_efd_item_format, | |
455 | .iop_pin = xfs_efd_item_pin, | |
456 | .iop_unpin = xfs_efd_item_unpin, | |
7bfa31d8 CH |
457 | .iop_unlock = xfs_efd_item_unlock, |
458 | .iop_committed = xfs_efd_item_committed, | |
459 | .iop_push = xfs_efd_item_push, | |
460 | .iop_committing = xfs_efd_item_committing | |
1da177e4 LT |
461 | }; |
462 | ||
1da177e4 LT |
463 | /* |
464 | * Allocate and initialize an efd item with the given number of extents. | |
465 | */ | |
7bfa31d8 CH |
466 | struct xfs_efd_log_item * |
467 | xfs_efd_init( | |
468 | struct xfs_mount *mp, | |
469 | struct xfs_efi_log_item *efip, | |
470 | uint nextents) | |
1da177e4 LT |
471 | |
472 | { | |
7bfa31d8 | 473 | struct xfs_efd_log_item *efdp; |
1da177e4 LT |
474 | uint size; |
475 | ||
476 | ASSERT(nextents > 0); | |
477 | if (nextents > XFS_EFD_MAX_FAST_EXTENTS) { | |
478 | size = (uint)(sizeof(xfs_efd_log_item_t) + | |
479 | ((nextents - 1) * sizeof(xfs_extent_t))); | |
7bfa31d8 | 480 | efdp = kmem_zalloc(size, KM_SLEEP); |
1da177e4 | 481 | } else { |
7bfa31d8 | 482 | efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP); |
1da177e4 LT |
483 | } |
484 | ||
43f5efc5 | 485 | xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops); |
1da177e4 LT |
486 | efdp->efd_efip = efip; |
487 | efdp->efd_format.efd_nextents = nextents; | |
488 | efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; | |
489 | ||
7bfa31d8 | 490 | return efdp; |
1da177e4 | 491 | } |
dc42375d DW |
492 | |
493 | /* | |
494 | * Process an extent free intent item that was recovered from | |
495 | * the log. We need to free the extents that it describes. | |
496 | */ | |
497 | int | |
498 | xfs_efi_recover( | |
499 | struct xfs_mount *mp, | |
500 | struct xfs_efi_log_item *efip) | |
501 | { | |
502 | struct xfs_efd_log_item *efdp; | |
503 | struct xfs_trans *tp; | |
504 | int i; | |
505 | int error = 0; | |
506 | xfs_extent_t *extp; | |
507 | xfs_fsblock_t startblock_fsb; | |
340785cc | 508 | struct xfs_owner_info oinfo; |
dc42375d DW |
509 | |
510 | ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)); | |
511 | ||
512 | /* | |
513 | * First check the validity of the extents described by the | |
514 | * EFI. If any are bad, then assume that all are bad and | |
515 | * just toss the EFI. | |
516 | */ | |
517 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { | |
e127fafd | 518 | extp = &efip->efi_format.efi_extents[i]; |
dc42375d DW |
519 | startblock_fsb = XFS_BB_TO_FSB(mp, |
520 | XFS_FSB_TO_DADDR(mp, extp->ext_start)); | |
e127fafd DW |
521 | if (startblock_fsb == 0 || |
522 | extp->ext_len == 0 || | |
523 | startblock_fsb >= mp->m_sb.sb_dblocks || | |
524 | extp->ext_len >= mp->m_sb.sb_agblocks) { | |
dc42375d DW |
525 | /* |
526 | * This will pull the EFI from the AIL and | |
527 | * free the memory associated with it. | |
528 | */ | |
529 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); | |
530 | xfs_efi_release(efip); | |
531 | return -EIO; | |
532 | } | |
533 | } | |
534 | ||
535 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); | |
536 | if (error) | |
537 | return error; | |
538 | efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents); | |
539 | ||
340785cc | 540 | xfs_rmap_skip_owner_update(&oinfo); |
dc42375d | 541 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { |
e127fafd | 542 | extp = &efip->efi_format.efi_extents[i]; |
dc42375d | 543 | error = xfs_trans_free_extent(tp, efdp, extp->ext_start, |
340785cc | 544 | extp->ext_len, &oinfo); |
dc42375d DW |
545 | if (error) |
546 | goto abort_error; | |
547 | ||
548 | } | |
549 | ||
550 | set_bit(XFS_EFI_RECOVERED, &efip->efi_flags); | |
551 | error = xfs_trans_commit(tp); | |
552 | return error; | |
553 | ||
554 | abort_error: | |
555 | xfs_trans_cancel(tp); | |
556 | return error; | |
557 | } |