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