xref: /openbmc/linux/fs/xfs/xfs_extfree_item.c (revision 9ac8d3fb)
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_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_buf_item.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_extfree_item.h"
31 
32 
33 kmem_zone_t	*xfs_efi_zone;
34 kmem_zone_t	*xfs_efd_zone;
35 
36 STATIC void	xfs_efi_item_unlock(xfs_efi_log_item_t *);
37 
38 void
39 xfs_efi_item_free(xfs_efi_log_item_t *efip)
40 {
41 	int nexts = efip->efi_format.efi_nextents;
42 
43 	if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
44 		kmem_free(efip);
45 	} else {
46 		kmem_zone_free(xfs_efi_zone, efip);
47 	}
48 }
49 
50 /*
51  * This returns the number of iovecs needed to log the given efi item.
52  * We only need 1 iovec for an efi item.  It just logs the efi_log_format
53  * structure.
54  */
55 /*ARGSUSED*/
56 STATIC uint
57 xfs_efi_item_size(xfs_efi_log_item_t *efip)
58 {
59 	return 1;
60 }
61 
62 /*
63  * This is called to fill in the vector of log iovecs for the
64  * given efi log item. We use only 1 iovec, and we point that
65  * at the efi_log_format structure embedded in the efi item.
66  * It is at this point that we assert that all of the extent
67  * slots in the efi item have been filled.
68  */
69 STATIC void
70 xfs_efi_item_format(xfs_efi_log_item_t	*efip,
71 		    xfs_log_iovec_t	*log_vector)
72 {
73 	uint	size;
74 
75 	ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
76 
77 	efip->efi_format.efi_type = XFS_LI_EFI;
78 
79 	size = sizeof(xfs_efi_log_format_t);
80 	size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
81 	efip->efi_format.efi_size = 1;
82 
83 	log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
84 	log_vector->i_len = size;
85 	XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT);
86 	ASSERT(size >= sizeof(xfs_efi_log_format_t));
87 }
88 
89 
90 /*
91  * Pinning has no meaning for an efi item, so just return.
92  */
93 /*ARGSUSED*/
94 STATIC void
95 xfs_efi_item_pin(xfs_efi_log_item_t *efip)
96 {
97 	return;
98 }
99 
100 
101 /*
102  * While EFIs cannot really be pinned, the unpin operation is the
103  * last place at which the EFI is manipulated during a transaction.
104  * Here we coordinate with xfs_efi_cancel() to determine who gets to
105  * free the EFI.
106  */
107 /*ARGSUSED*/
108 STATIC void
109 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
110 {
111 	xfs_mount_t	*mp;
112 
113 	mp = efip->efi_item.li_mountp;
114 	spin_lock(&mp->m_ail_lock);
115 	if (efip->efi_flags & XFS_EFI_CANCELED) {
116 		/*
117 		 * xfs_trans_delete_ail() drops the AIL lock.
118 		 */
119 		xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip);
120 		xfs_efi_item_free(efip);
121 	} else {
122 		efip->efi_flags |= XFS_EFI_COMMITTED;
123 		spin_unlock(&mp->m_ail_lock);
124 	}
125 }
126 
127 /*
128  * like unpin only we have to also clear the xaction descriptor
129  * pointing the log item if we free the item.  This routine duplicates
130  * unpin because efi_flags is protected by the AIL lock.  Freeing
131  * the descriptor and then calling unpin would force us to drop the AIL
132  * lock which would open up a race condition.
133  */
134 STATIC void
135 xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
136 {
137 	xfs_mount_t	*mp;
138 	xfs_log_item_desc_t	*lidp;
139 
140 	mp = efip->efi_item.li_mountp;
141 	spin_lock(&mp->m_ail_lock);
142 	if (efip->efi_flags & XFS_EFI_CANCELED) {
143 		/*
144 		 * free the xaction descriptor pointing to this item
145 		 */
146 		lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
147 		xfs_trans_free_item(tp, lidp);
148 		/*
149 		 * pull the item off the AIL.
150 		 * xfs_trans_delete_ail() drops the AIL lock.
151 		 */
152 		xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip);
153 		xfs_efi_item_free(efip);
154 	} else {
155 		efip->efi_flags |= XFS_EFI_COMMITTED;
156 		spin_unlock(&mp->m_ail_lock);
157 	}
158 }
159 
160 /*
161  * Efi items have no locking or pushing.  However, since EFIs are
162  * pulled from the AIL when their corresponding EFDs are committed
163  * to disk, their situation is very similar to being pinned.  Return
164  * XFS_ITEM_PINNED so that the caller will eventually flush the log.
165  * This should help in getting the EFI out of the AIL.
166  */
167 /*ARGSUSED*/
168 STATIC uint
169 xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
170 {
171 	return XFS_ITEM_PINNED;
172 }
173 
174 /*
175  * Efi items have no locking, so just return.
176  */
177 /*ARGSUSED*/
178 STATIC void
179 xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
180 {
181 	if (efip->efi_item.li_flags & XFS_LI_ABORTED)
182 		xfs_efi_item_free(efip);
183 	return;
184 }
185 
186 /*
187  * The EFI is logged only once and cannot be moved in the log, so
188  * simply return the lsn at which it's been logged.  The canceled
189  * flag is not paid any attention here.  Checking for that is delayed
190  * until the EFI is unpinned.
191  */
192 /*ARGSUSED*/
193 STATIC xfs_lsn_t
194 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
195 {
196 	return lsn;
197 }
198 
199 /*
200  * There isn't much you can do to push on an efi item.  It is simply
201  * stuck waiting for all of its corresponding efd items to be
202  * committed to disk.
203  */
204 /*ARGSUSED*/
205 STATIC void
206 xfs_efi_item_push(xfs_efi_log_item_t *efip)
207 {
208 	return;
209 }
210 
211 /*
212  * The EFI dependency tracking op doesn't do squat.  It can't because
213  * it doesn't know where the free extent is coming from.  The dependency
214  * tracking has to be handled by the "enclosing" metadata object.  For
215  * example, for inodes, the inode is locked throughout the extent freeing
216  * so the dependency should be recorded there.
217  */
218 /*ARGSUSED*/
219 STATIC void
220 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
221 {
222 	return;
223 }
224 
225 /*
226  * This is the ops vector shared by all efi log items.
227  */
228 static struct xfs_item_ops xfs_efi_item_ops = {
229 	.iop_size	= (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
230 	.iop_format	= (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
231 					xfs_efi_item_format,
232 	.iop_pin	= (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
233 	.iop_unpin	= (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
234 	.iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
235 					xfs_efi_item_unpin_remove,
236 	.iop_trylock	= (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
237 	.iop_unlock	= (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
238 	.iop_committed	= (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
239 					xfs_efi_item_committed,
240 	.iop_push	= (void(*)(xfs_log_item_t*))xfs_efi_item_push,
241 	.iop_pushbuf	= NULL,
242 	.iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
243 					xfs_efi_item_committing
244 };
245 
246 
247 /*
248  * Allocate and initialize an efi item with the given number of extents.
249  */
250 xfs_efi_log_item_t *
251 xfs_efi_init(xfs_mount_t	*mp,
252 	     uint		nextents)
253 
254 {
255 	xfs_efi_log_item_t	*efip;
256 	uint			size;
257 
258 	ASSERT(nextents > 0);
259 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
260 		size = (uint)(sizeof(xfs_efi_log_item_t) +
261 			((nextents - 1) * sizeof(xfs_extent_t)));
262 		efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
263 	} else {
264 		efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
265 							     KM_SLEEP);
266 	}
267 
268 	efip->efi_item.li_type = XFS_LI_EFI;
269 	efip->efi_item.li_ops = &xfs_efi_item_ops;
270 	efip->efi_item.li_mountp = mp;
271 	efip->efi_format.efi_nextents = nextents;
272 	efip->efi_format.efi_id = (__psint_t)(void*)efip;
273 
274 	return (efip);
275 }
276 
277 /*
278  * Copy an EFI format buffer from the given buf, and into the destination
279  * EFI format structure.
280  * The given buffer can be in 32 bit or 64 bit form (which has different padding),
281  * one of which will be the native format for this kernel.
282  * It will handle the conversion of formats if necessary.
283  */
284 int
285 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
286 {
287 	xfs_efi_log_format_t *src_efi_fmt = (xfs_efi_log_format_t *)buf->i_addr;
288 	uint i;
289 	uint len = sizeof(xfs_efi_log_format_t) +
290 		(src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
291 	uint len32 = sizeof(xfs_efi_log_format_32_t) +
292 		(src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
293 	uint len64 = sizeof(xfs_efi_log_format_64_t) +
294 		(src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
295 
296 	if (buf->i_len == len) {
297 		memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
298 		return 0;
299 	} else if (buf->i_len == len32) {
300 		xfs_efi_log_format_32_t *src_efi_fmt_32 =
301 			(xfs_efi_log_format_32_t *)buf->i_addr;
302 
303 		dst_efi_fmt->efi_type     = src_efi_fmt_32->efi_type;
304 		dst_efi_fmt->efi_size     = src_efi_fmt_32->efi_size;
305 		dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
306 		dst_efi_fmt->efi_id       = src_efi_fmt_32->efi_id;
307 		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
308 			dst_efi_fmt->efi_extents[i].ext_start =
309 				src_efi_fmt_32->efi_extents[i].ext_start;
310 			dst_efi_fmt->efi_extents[i].ext_len =
311 				src_efi_fmt_32->efi_extents[i].ext_len;
312 		}
313 		return 0;
314 	} else if (buf->i_len == len64) {
315 		xfs_efi_log_format_64_t *src_efi_fmt_64 =
316 			(xfs_efi_log_format_64_t *)buf->i_addr;
317 
318 		dst_efi_fmt->efi_type     = src_efi_fmt_64->efi_type;
319 		dst_efi_fmt->efi_size     = src_efi_fmt_64->efi_size;
320 		dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
321 		dst_efi_fmt->efi_id       = src_efi_fmt_64->efi_id;
322 		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
323 			dst_efi_fmt->efi_extents[i].ext_start =
324 				src_efi_fmt_64->efi_extents[i].ext_start;
325 			dst_efi_fmt->efi_extents[i].ext_len =
326 				src_efi_fmt_64->efi_extents[i].ext_len;
327 		}
328 		return 0;
329 	}
330 	return EFSCORRUPTED;
331 }
332 
333 /*
334  * This is called by the efd item code below to release references to
335  * the given efi item.  Each efd calls this with the number of
336  * extents that it has logged, and when the sum of these reaches
337  * the total number of extents logged by this efi item we can free
338  * the efi item.
339  *
340  * Freeing the efi item requires that we remove it from the AIL.
341  * We'll use the AIL lock to protect our counters as well as
342  * the removal from the AIL.
343  */
344 void
345 xfs_efi_release(xfs_efi_log_item_t	*efip,
346 		uint			nextents)
347 {
348 	xfs_mount_t	*mp;
349 	int		extents_left;
350 
351 	mp = efip->efi_item.li_mountp;
352 	ASSERT(efip->efi_next_extent > 0);
353 	ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
354 
355 	spin_lock(&mp->m_ail_lock);
356 	ASSERT(efip->efi_next_extent >= nextents);
357 	efip->efi_next_extent -= nextents;
358 	extents_left = efip->efi_next_extent;
359 	if (extents_left == 0) {
360 		/*
361 		 * xfs_trans_delete_ail() drops the AIL lock.
362 		 */
363 		xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip);
364 		xfs_efi_item_free(efip);
365 	} else {
366 		spin_unlock(&mp->m_ail_lock);
367 	}
368 }
369 
370 STATIC void
371 xfs_efd_item_free(xfs_efd_log_item_t *efdp)
372 {
373 	int nexts = efdp->efd_format.efd_nextents;
374 
375 	if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
376 		kmem_free(efdp);
377 	} else {
378 		kmem_zone_free(xfs_efd_zone, efdp);
379 	}
380 }
381 
382 /*
383  * This returns the number of iovecs needed to log the given efd item.
384  * We only need 1 iovec for an efd item.  It just logs the efd_log_format
385  * structure.
386  */
387 /*ARGSUSED*/
388 STATIC uint
389 xfs_efd_item_size(xfs_efd_log_item_t *efdp)
390 {
391 	return 1;
392 }
393 
394 /*
395  * This is called to fill in the vector of log iovecs for the
396  * given efd log item. We use only 1 iovec, and we point that
397  * at the efd_log_format structure embedded in the efd item.
398  * It is at this point that we assert that all of the extent
399  * slots in the efd item have been filled.
400  */
401 STATIC void
402 xfs_efd_item_format(xfs_efd_log_item_t	*efdp,
403 		    xfs_log_iovec_t	*log_vector)
404 {
405 	uint	size;
406 
407 	ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
408 
409 	efdp->efd_format.efd_type = XFS_LI_EFD;
410 
411 	size = sizeof(xfs_efd_log_format_t);
412 	size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
413 	efdp->efd_format.efd_size = 1;
414 
415 	log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
416 	log_vector->i_len = size;
417 	XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT);
418 	ASSERT(size >= sizeof(xfs_efd_log_format_t));
419 }
420 
421 
422 /*
423  * Pinning has no meaning for an efd item, so just return.
424  */
425 /*ARGSUSED*/
426 STATIC void
427 xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
428 {
429 	return;
430 }
431 
432 
433 /*
434  * Since pinning has no meaning for an efd item, unpinning does
435  * not either.
436  */
437 /*ARGSUSED*/
438 STATIC void
439 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
440 {
441 	return;
442 }
443 
444 /*ARGSUSED*/
445 STATIC void
446 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
447 {
448 	return;
449 }
450 
451 /*
452  * Efd items have no locking, so just return success.
453  */
454 /*ARGSUSED*/
455 STATIC uint
456 xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
457 {
458 	return XFS_ITEM_LOCKED;
459 }
460 
461 /*
462  * Efd items have no locking or pushing, so return failure
463  * so that the caller doesn't bother with us.
464  */
465 /*ARGSUSED*/
466 STATIC void
467 xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
468 {
469 	if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
470 		xfs_efd_item_free(efdp);
471 	return;
472 }
473 
474 /*
475  * When the efd item is committed to disk, all we need to do
476  * is delete our reference to our partner efi item and then
477  * free ourselves.  Since we're freeing ourselves we must
478  * return -1 to keep the transaction code from further referencing
479  * this item.
480  */
481 /*ARGSUSED*/
482 STATIC xfs_lsn_t
483 xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
484 {
485 	/*
486 	 * If we got a log I/O error, it's always the case that the LR with the
487 	 * EFI got unpinned and freed before the EFD got aborted.
488 	 */
489 	if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
490 		xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
491 
492 	xfs_efd_item_free(efdp);
493 	return (xfs_lsn_t)-1;
494 }
495 
496 /*
497  * There isn't much you can do to push on an efd item.  It is simply
498  * stuck waiting for the log to be flushed to disk.
499  */
500 /*ARGSUSED*/
501 STATIC void
502 xfs_efd_item_push(xfs_efd_log_item_t *efdp)
503 {
504 	return;
505 }
506 
507 /*
508  * The EFD dependency tracking op doesn't do squat.  It can't because
509  * it doesn't know where the free extent is coming from.  The dependency
510  * tracking has to be handled by the "enclosing" metadata object.  For
511  * example, for inodes, the inode is locked throughout the extent freeing
512  * so the dependency should be recorded there.
513  */
514 /*ARGSUSED*/
515 STATIC void
516 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
517 {
518 	return;
519 }
520 
521 /*
522  * This is the ops vector shared by all efd log items.
523  */
524 static struct xfs_item_ops xfs_efd_item_ops = {
525 	.iop_size	= (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
526 	.iop_format	= (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
527 					xfs_efd_item_format,
528 	.iop_pin	= (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
529 	.iop_unpin	= (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
530 	.iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
531 					xfs_efd_item_unpin_remove,
532 	.iop_trylock	= (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
533 	.iop_unlock	= (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
534 	.iop_committed	= (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
535 					xfs_efd_item_committed,
536 	.iop_push	= (void(*)(xfs_log_item_t*))xfs_efd_item_push,
537 	.iop_pushbuf	= NULL,
538 	.iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
539 					xfs_efd_item_committing
540 };
541 
542 
543 /*
544  * Allocate and initialize an efd item with the given number of extents.
545  */
546 xfs_efd_log_item_t *
547 xfs_efd_init(xfs_mount_t	*mp,
548 	     xfs_efi_log_item_t	*efip,
549 	     uint		nextents)
550 
551 {
552 	xfs_efd_log_item_t	*efdp;
553 	uint			size;
554 
555 	ASSERT(nextents > 0);
556 	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
557 		size = (uint)(sizeof(xfs_efd_log_item_t) +
558 			((nextents - 1) * sizeof(xfs_extent_t)));
559 		efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
560 	} else {
561 		efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
562 							     KM_SLEEP);
563 	}
564 
565 	efdp->efd_item.li_type = XFS_LI_EFD;
566 	efdp->efd_item.li_ops = &xfs_efd_item_ops;
567 	efdp->efd_item.li_mountp = mp;
568 	efdp->efd_efip = efip;
569 	efdp->efd_format.efd_nextents = nextents;
570 	efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
571 
572 	return (efdp);
573 }
574