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