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