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