blob: 7d1e84c1b848d1f21185da0a8b645574328a3e2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Darrick J. Wong9749fee2016-08-03 11:14:35 +100025#include "xfs_defer.h"
Dave Chinner239880e2013-10-23 10:50:10 +110026#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_trans_priv.h"
28#include "xfs_extfree_item.h"
Brian Foster6bc43af2015-08-19 09:51:43 +100029#include "xfs_alloc.h"
Darrick J. Wong9749fee2016-08-03 11:14:35 +100030#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * This routine is called to allocate an "extent free done"
34 * log item that will hold nextents worth of extents. The
35 * caller must use all nextents extents, because we are not
36 * flexible about this at all.
37 */
Darrick J. Wongbba61cb2016-08-03 11:13:47 +100038struct xfs_efd_log_item *
39xfs_trans_get_efd(struct xfs_trans *tp,
40 struct xfs_efi_log_item *efip,
41 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Darrick J. Wongbba61cb2016-08-03 11:13:47 +100043 struct xfs_efd_log_item *efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 ASSERT(tp != NULL);
46 ASSERT(nextents > 0);
47
48 efdp = xfs_efd_init(tp->t_mountp, efip, nextents);
49 ASSERT(efdp != NULL);
50
51 /*
52 * Get a log_item_desc to point at the new item.
53 */
Christoph Hellwige98c4142010-06-23 18:11:15 +100054 xfs_trans_add_item(tp, &efdp->efd_item);
55 return efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58/*
Brian Foster6bc43af2015-08-19 09:51:43 +100059 * Free an extent and log it to the EFD. Note that the transaction is marked
60 * dirty regardless of whether the extent free succeeds or fails to support the
61 * EFI/EFD lifecycle rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Brian Foster6bc43af2015-08-19 09:51:43 +100063int
64xfs_trans_free_extent(
65 struct xfs_trans *tp,
66 struct xfs_efd_log_item *efdp,
67 xfs_fsblock_t start_block,
Darrick J. Wong340785c2016-08-03 11:33:42 +100068 xfs_extlen_t ext_len,
69 struct xfs_owner_info *oinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 uint next_extent;
Brian Foster6bc43af2015-08-19 09:51:43 +100072 struct xfs_extent *extp;
73 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Darrick J. Wong340785c2016-08-03 11:33:42 +100075 error = xfs_free_extent(tp, start_block, ext_len, oinfo);
Brian Foster6bc43af2015-08-19 09:51:43 +100076
77 /*
78 * Mark the transaction dirty, even on error. This ensures the
79 * transaction is aborted, which:
80 *
81 * 1.) releases the EFI and frees the EFD
82 * 2.) shuts down the filesystem
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 tp->t_flags |= XFS_TRANS_DIRTY;
Christoph Hellwige98c4142010-06-23 18:11:15 +100085 efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 next_extent = efdp->efd_next_extent;
88 ASSERT(next_extent < efdp->efd_format.efd_nextents);
89 extp = &(efdp->efd_format.efd_extents[next_extent]);
90 extp->ext_start = start_block;
91 extp->ext_len = ext_len;
92 efdp->efd_next_extent++;
Brian Foster6bc43af2015-08-19 09:51:43 +100093
94 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
Darrick J. Wong9749fee2016-08-03 11:14:35 +100096
97/* Sort bmap items by AG. */
98static int
99xfs_extent_free_diff_items(
100 void *priv,
101 struct list_head *a,
102 struct list_head *b)
103{
104 struct xfs_mount *mp = priv;
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000105 struct xfs_extent_free_item *ra;
106 struct xfs_extent_free_item *rb;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000107
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000108 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
109 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
110 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
111 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000112}
113
114/* Get an EFI. */
115STATIC void *
116xfs_extent_free_create_intent(
117 struct xfs_trans *tp,
118 unsigned int count)
119{
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000120 struct xfs_efi_log_item *efip;
121
122 ASSERT(tp != NULL);
123 ASSERT(count > 0);
124
125 efip = xfs_efi_init(tp->t_mountp, count);
126 ASSERT(efip != NULL);
127
128 /*
129 * Get a log_item_desc to point at the new item.
130 */
131 xfs_trans_add_item(tp, &efip->efi_item);
132 return efip;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000133}
134
135/* Log a free extent to the intent item. */
136STATIC void
137xfs_extent_free_log_item(
138 struct xfs_trans *tp,
139 void *intent,
140 struct list_head *item)
141{
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000142 struct xfs_efi_log_item *efip = intent;
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000143 struct xfs_extent_free_item *free;
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000144 uint next_extent;
145 struct xfs_extent *extp;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000146
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000147 free = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wong51ce9d02016-08-03 12:30:31 +1000148
149 tp->t_flags |= XFS_TRANS_DIRTY;
150 efip->efi_item.li_desc->lid_flags |= XFS_LID_DIRTY;
151
152 /*
153 * atomic_inc_return gives us the value after the increment;
154 * we want to use it as an array index so we need to subtract 1 from
155 * it.
156 */
157 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
158 ASSERT(next_extent < efip->efi_format.efi_nextents);
159 extp = &efip->efi_format.efi_extents[next_extent];
160 extp->ext_start = free->xefi_startblock;
161 extp->ext_len = free->xefi_blockcount;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000162}
163
164/* Get an EFD so we can process all the free extents. */
165STATIC void *
166xfs_extent_free_create_done(
167 struct xfs_trans *tp,
168 void *intent,
169 unsigned int count)
170{
171 return xfs_trans_get_efd(tp, intent, count);
172}
173
174/* Process a free extent. */
175STATIC int
176xfs_extent_free_finish_item(
177 struct xfs_trans *tp,
178 struct xfs_defer_ops *dop,
179 struct list_head *item,
180 void *done_item,
181 void **state)
182{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000183 struct xfs_extent_free_item *free;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000184 int error;
185
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000186 free = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000187 error = xfs_trans_free_extent(tp, done_item,
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000188 free->xefi_startblock,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000189 free->xefi_blockcount,
190 &free->xefi_oinfo);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000191 kmem_free(free);
192 return error;
193}
194
195/* Abort all pending EFIs. */
196STATIC void
197xfs_extent_free_abort_intent(
198 void *intent)
199{
200 xfs_efi_release(intent);
201}
202
203/* Cancel a free extent. */
204STATIC void
205xfs_extent_free_cancel_item(
206 struct list_head *item)
207{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000208 struct xfs_extent_free_item *free;
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000209
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000210 free = container_of(item, struct xfs_extent_free_item, xefi_list);
Darrick J. Wong9749fee2016-08-03 11:14:35 +1000211 kmem_free(free);
212}
213
214static const struct xfs_defer_op_type xfs_extent_free_defer_type = {
215 .type = XFS_DEFER_OPS_TYPE_FREE,
216 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
217 .diff_items = xfs_extent_free_diff_items,
218 .create_intent = xfs_extent_free_create_intent,
219 .abort_intent = xfs_extent_free_abort_intent,
220 .log_item = xfs_extent_free_log_item,
221 .create_done = xfs_extent_free_create_done,
222 .finish_item = xfs_extent_free_finish_item,
223 .cancel_item = xfs_extent_free_cancel_item,
224};
225
226/* Register the deferred op type. */
227void
228xfs_extent_free_init_defer_op(void)
229{
230 xfs_defer_init_op_type(&xfs_extent_free_defer_type);
231}