811923231bb7ad1125cff4db4a58776d51342f98
[linux-block.git] / fs / xfs / xfs_extfree_item.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_bit.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_trans.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_buf_item.h"
17 #include "xfs_extfree_item.h"
18 #include "xfs_log.h"
19 #include "xfs_btree.h"
20 #include "xfs_rmap.h"
21
22
23 kmem_zone_t     *xfs_efi_zone;
24 kmem_zone_t     *xfs_efd_zone;
25
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 }
30
31 void
32 xfs_efi_item_free(
33         struct xfs_efi_log_item *efip)
34 {
35         kmem_free(efip->efi_item.li_lv_shadow);
36         if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
37                 kmem_free(efip);
38         else
39                 kmem_zone_free(xfs_efi_zone, efip);
40 }
41
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
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  */
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
74 xfs_efi_item_size(
75         struct xfs_log_item     *lip,
76         int                     *nvecs,
77         int                     *nbytes)
78 {
79         *nvecs += 1;
80         *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
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
91 xfs_efi_item_format(
92         struct xfs_log_item     *lip,
93         struct xfs_log_vec      *lv)
94 {
95         struct xfs_efi_log_item *efip = EFI_ITEM(lip);
96         struct xfs_log_iovec    *vecp = NULL;
97
98         ASSERT(atomic_read(&efip->efi_next_extent) ==
99                                 efip->efi_format.efi_nextents);
100
101         efip->efi_format.efi_type = XFS_LI_EFI;
102         efip->efi_format.efi_size = 1;
103
104         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
105                         &efip->efi_format,
106                         xfs_efi_item_sizeof(efip));
107 }
108
109
110 /*
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.
117  */
118 STATIC void
119 xfs_efi_item_unpin(
120         struct xfs_log_item     *lip,
121         int                     remove)
122 {
123         struct xfs_efi_log_item *efip = EFI_ITEM(lip);
124         xfs_efi_release(efip);
125 }
126
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  */
132 STATIC void
133 xfs_efi_item_release(
134         struct xfs_log_item     *lip)
135 {
136         xfs_efi_release(EFI_ITEM(lip));
137 }
138
139 /*
140  * This is the ops vector shared by all efi log items.
141  */
142 static const struct xfs_item_ops xfs_efi_item_ops = {
143         .iop_size       = xfs_efi_item_size,
144         .iop_format     = xfs_efi_item_format,
145         .iop_unpin      = xfs_efi_item_unpin,
146         .iop_release    = xfs_efi_item_release,
147 };
148
149
150 /*
151  * Allocate and initialize an efi item with the given number of extents.
152  */
153 struct xfs_efi_log_item *
154 xfs_efi_init(
155         struct xfs_mount        *mp,
156         uint                    nextents)
157
158 {
159         struct xfs_efi_log_item *efip;
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)));
166                 efip = kmem_zalloc(size, KM_SLEEP);
167         } else {
168                 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
169         }
170
171         xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
172         efip->efi_format.efi_nextents = nextents;
173         efip->efi_format.efi_id = (uintptr_t)(void *)efip;
174         atomic_set(&efip->efi_next_extent, 0);
175         atomic_set(&efip->efi_refcount, 2);
176
177         return efip;
178 }
179
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 {
190         xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
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) {
203                 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
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) {
217                 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
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         }
231         return -EFSCORRUPTED;
232 }
233
234 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
235 {
236         return container_of(lip, struct xfs_efd_log_item, efd_item);
237 }
238
239 STATIC void
240 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
241 {
242         kmem_free(efdp->efd_item.li_lv_shadow);
243         if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
244                 kmem_free(efdp);
245         else
246                 kmem_zone_free(xfs_efd_zone, efdp);
247 }
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  */
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
263 xfs_efd_item_size(
264         struct xfs_log_item     *lip,
265         int                     *nvecs,
266         int                     *nbytes)
267 {
268         *nvecs += 1;
269         *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
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
280 xfs_efd_item_format(
281         struct xfs_log_item     *lip,
282         struct xfs_log_vec      *lv)
283 {
284         struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
285         struct xfs_log_iovec    *vecp = NULL;
286
287         ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
288
289         efdp->efd_format.efd_type = XFS_LI_EFD;
290         efdp->efd_format.efd_size = 1;
291
292         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
293                         &efdp->efd_format,
294                         xfs_efd_item_sizeof(efdp));
295 }
296
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  */
301 STATIC void
302 xfs_efd_item_release(
303         struct xfs_log_item     *lip)
304 {
305         struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
306
307         xfs_efi_release(efdp->efd_efip);
308         xfs_efd_item_free(efdp);
309 }
310
311 /*
312  * This is the ops vector shared by all efd log items.
313  */
314 static const struct xfs_item_ops xfs_efd_item_ops = {
315         .flags          = XFS_ITEM_RELEASE_WHEN_COMMITTED,
316         .iop_size       = xfs_efd_item_size,
317         .iop_format     = xfs_efd_item_format,
318         .iop_release    = xfs_efd_item_release,
319 };
320
321 /*
322  * Allocate and initialize an efd item with the given number of extents.
323  */
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)
329
330 {
331         struct xfs_efd_log_item *efdp;
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)));
338                 efdp = kmem_zalloc(size, KM_SLEEP);
339         } else {
340                 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
341         }
342
343         xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
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
348         return efdp;
349 }
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++) {
375                 extp = &efip->efi_format.efi_extents[i];
376                 startblock_fsb = XFS_BB_TO_FSB(mp,
377                                    XFS_FSB_TO_DADDR(mp, extp->ext_start));
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) {
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++) {
398                 extp = &efip->efi_format.efi_extents[i];
399                 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
400                                               extp->ext_len,
401                                               &XFS_RMAP_OINFO_ANY_OWNER, false);
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 }