blob: 855c0b651fd40a3db5ad78d1740cbfb283fe0281 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Darrick J. Wong3481b682016-08-03 12:31:07 +100012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Darrick J. Wong9749fee2016-08-03 11:14:35 +100014#include "xfs_defer.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_trans_priv.h"
17#include "xfs_extfree_item.h"
Brian Foster6bc43af2015-08-19 09:51:43 +100018#include "xfs_alloc.h"
Darrick J. Wong9749fee2016-08-03 11:14:35 +100019#include "xfs_bmap.h"
Darrick J. Wong3481b682016-08-03 12:31:07 +100020#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This routine is called to allocate an "extent free done"
24 * log item that will hold nextents worth of extents. The
25 * caller must use all nextents extents, because we are not
26 * flexible about this at all.
27 */
Darrick J. Wongbba61cb2016-08-03 11:13:47 +100028struct xfs_efd_log_item *
29xfs_trans_get_efd(struct xfs_trans *tp,
30 struct xfs_efi_log_item *efip,
31 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Darrick J. Wongbba61cb2016-08-03 11:13:47 +100033 struct xfs_efd_log_item *efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 ASSERT(tp != NULL);
36 ASSERT(nextents > 0);
37
38 efdp = xfs_efd_init(tp->t_mountp, efip, nextents);
39 ASSERT(efdp != NULL);
40
41 /*
42 * Get a log_item_desc to point at the new item.
43 */
Christoph Hellwige98c4142010-06-23 18:11:15 +100044 xfs_trans_add_item(tp, &efdp->efd_item);
45 return efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48/*
Brian Foster6bc43af2015-08-19 09:51:43 +100049 * Free an extent and log it to the EFD. Note that the transaction is marked
50 * dirty regardless of whether the extent free succeeds or fails to support the
51 * EFI/EFD lifecycle rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Brian Foster6bc43af2015-08-19 09:51:43 +100053int
54xfs_trans_free_extent(
55 struct xfs_trans *tp,
56 struct xfs_efd_log_item *efdp,
57 xfs_fsblock_t start_block,
Darrick J. Wong340785c2016-08-03 11:33:42 +100058 xfs_extlen_t ext_len,
Brian Fosterfcb762f2018-05-09 08:45:04 -070059 struct xfs_owner_info *oinfo,
60 bool skip_discard)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Darrick J. Wong3481b682016-08-03 12:31:07 +100062 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 uint next_extent;
Darrick J. Wong3481b682016-08-03 12:31:07 +100064 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
65 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, start_block);
Brian Foster6bc43af2015-08-19 09:51:43 +100066 struct xfs_extent *extp;
67 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Darrick J. Wong3481b682016-08-03 12:31:07 +100069 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
70
Brian Foster4e529332018-05-10 09:35:42 -070071 error = __xfs_free_extent(tp, start_block, ext_len,
72 oinfo, XFS_AG_RESV_NONE, skip_discard);
Brian Foster6bc43af2015-08-19 09:51:43 +100073 /*
74 * Mark the transaction dirty, even on error. This ensures the
75 * transaction is aborted, which:
76 *
77 * 1.) releases the EFI and frees the EFD
78 * 2.) shuts down the filesystem
79 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 tp->t_flags |= XFS_TRANS_DIRTY;
Dave Chinnere6631f82018-05-09 07:49:37 -070081 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 next_extent = efdp->efd_next_extent;
84 ASSERT(next_extent < efdp->efd_format.efd_nextents);
85 extp = &(efdp->efd_format.efd_extents[next_extent]);
86 extp->ext_start = start_block;
87 extp->ext_len = ext_len;
88 efdp->efd_next_extent++;
Brian Foster6bc43af2015-08-19 09:51:43 +100089
90 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
Darrick J. Wong9749fee2016-08-03 11:14:35 +100092
93/* Sort bmap items by AG. */
94static int
95xfs_extent_free_diff_items(
96 void *priv,
97 struct list_head *a,
98 struct list_head *b)
99{
100 struct xfs_mount *mp = priv;
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000101 struct xfs_extent_free_item *ra;
102 struct xfs_extent_free_item *rb;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000103
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000104 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
105 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
106 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
107 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000108}
109
110/* Get an EFI. */
111STATIC void *
112xfs_extent_free_create_intent(
113 struct xfs_trans *tp,
114 unsigned int count)
115{
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000116 struct xfs_efi_log_item *efip;
117
118 ASSERT(tp != NULL);
119 ASSERT(count > 0);
120
121 efip = xfs_efi_init(tp->t_mountp, count);
122 ASSERT(efip != NULL);
123
124 /*
125 * Get a log_item_desc to point at the new item.
126 */
127 xfs_trans_add_item(tp, &efip->efi_item);
128 return efip;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000129}
130
131/* Log a free extent to the intent item. */
132STATIC void
133xfs_extent_free_log_item(
134 struct xfs_trans *tp,
135 void *intent,
136 struct list_head *item)
137{
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000138 struct xfs_efi_log_item *efip = intent;
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000139 struct xfs_extent_free_item *free;
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000140 uint next_extent;
141 struct xfs_extent *extp;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000142
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000143 free = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000144
145 tp->t_flags |= XFS_TRANS_DIRTY;
Dave Chinnere6631f82018-05-09 07:49:37 -0700146 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000147
148 /*
149 * atomic_inc_return gives us the value after the increment;
150 * we want to use it as an array index so we need to subtract 1 from
151 * it.
152 */
153 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
154 ASSERT(next_extent < efip->efi_format.efi_nextents);
155 extp = &efip->efi_format.efi_extents[next_extent];
156 extp->ext_start = free->xefi_startblock;
157 extp->ext_len = free->xefi_blockcount;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000158}
159
160/* Get an EFD so we can process all the free extents. */
161STATIC void *
162xfs_extent_free_create_done(
163 struct xfs_trans *tp,
164 void *intent,
165 unsigned int count)
166{
167 return xfs_trans_get_efd(tp, intent, count);
168}
169
170/* Process a free extent. */
171STATIC int
172xfs_extent_free_finish_item(
173 struct xfs_trans *tp,
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000174 struct list_head *item,
175 void *done_item,
176 void **state)
177{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000178 struct xfs_extent_free_item *free;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000179 int error;
180
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000181 free = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000182 error = xfs_trans_free_extent(tp, done_item,
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000183 free->xefi_startblock,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000184 free->xefi_blockcount,
Brian Fosterfcb762f2018-05-09 08:45:04 -0700185 &free->xefi_oinfo, free->xefi_skip_discard);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000186 kmem_free(free);
187 return error;
188}
189
190/* Abort all pending EFIs. */
191STATIC void
192xfs_extent_free_abort_intent(
193 void *intent)
194{
195 xfs_efi_release(intent);
196}
197
198/* Cancel a free extent. */
199STATIC void
200xfs_extent_free_cancel_item(
201 struct list_head *item)
202{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000203 struct xfs_extent_free_item *free;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000204
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000205 free = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000206 kmem_free(free);
207}
208
209static const struct xfs_defer_op_type xfs_extent_free_defer_type = {
210 .type = XFS_DEFER_OPS_TYPE_FREE,
211 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
212 .diff_items = xfs_extent_free_diff_items,
213 .create_intent = xfs_extent_free_create_intent,
214 .abort_intent = xfs_extent_free_abort_intent,
215 .log_item = xfs_extent_free_log_item,
216 .create_done = xfs_extent_free_create_done,
217 .finish_item = xfs_extent_free_finish_item,
218 .cancel_item = xfs_extent_free_cancel_item,
219};
220
Brian Fosterf8f28352018-05-07 17:38:47 -0700221/*
222 * AGFL blocks are accounted differently in the reserve pools and are not
223 * inserted into the busy extent list.
224 */
225STATIC int
226xfs_agfl_free_finish_item(
227 struct xfs_trans *tp,
Brian Fosterf8f28352018-05-07 17:38:47 -0700228 struct list_head *item,
229 void *done_item,
230 void **state)
231{
232 struct xfs_mount *mp = tp->t_mountp;
233 struct xfs_efd_log_item *efdp = done_item;
234 struct xfs_extent_free_item *free;
235 struct xfs_extent *extp;
236 struct xfs_buf *agbp;
237 int error;
238 xfs_agnumber_t agno;
239 xfs_agblock_t agbno;
240 uint next_extent;
241
242 free = container_of(item, struct xfs_extent_free_item, xefi_list);
243 ASSERT(free->xefi_blockcount == 1);
244 agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
245 agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
246
247 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
248
249 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
250 if (!error)
251 error = xfs_free_agfl_block(tp, agno, agbno, agbp,
252 &free->xefi_oinfo);
253
254 /*
255 * Mark the transaction dirty, even on error. This ensures the
256 * transaction is aborted, which:
257 *
258 * 1.) releases the EFI and frees the EFD
259 * 2.) shuts down the filesystem
260 */
261 tp->t_flags |= XFS_TRANS_DIRTY;
Dave Chinnere6631f82018-05-09 07:49:37 -0700262 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
Brian Fosterf8f28352018-05-07 17:38:47 -0700263
264 next_extent = efdp->efd_next_extent;
265 ASSERT(next_extent < efdp->efd_format.efd_nextents);
266 extp = &(efdp->efd_format.efd_extents[next_extent]);
267 extp->ext_start = free->xefi_startblock;
268 extp->ext_len = free->xefi_blockcount;
269 efdp->efd_next_extent++;
270
271 kmem_free(free);
272 return error;
273}
274
275
276/* sub-type with special handling for AGFL deferred frees */
277static const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
278 .type = XFS_DEFER_OPS_TYPE_AGFL_FREE,
279 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
280 .diff_items = xfs_extent_free_diff_items,
281 .create_intent = xfs_extent_free_create_intent,
282 .abort_intent = xfs_extent_free_abort_intent,
283 .log_item = xfs_extent_free_log_item,
284 .create_done = xfs_extent_free_create_done,
285 .finish_item = xfs_agfl_free_finish_item,
286 .cancel_item = xfs_extent_free_cancel_item,
287};
288
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000289/* Register the deferred op type. */
290void
291xfs_extent_free_init_defer_op(void)
292{
293 xfs_defer_init_op_type(&xfs_extent_free_defer_type);
Brian Fosterf8f28352018-05-07 17:38:47 -0700294 xfs_defer_init_op_type(&xfs_agfl_free_defer_type);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000295}