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