1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_format.h"
21#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
23#include "xfs_bit.h"
24#include "xfs_mount.h"
25#include "xfs_trans.h"
26#include "xfs_trans_priv.h"
27#include "xfs_buf_item.h"
28#include "xfs_extfree_item.h"
29#include "xfs_log.h"
30#include "xfs_btree.h"
31#include "xfs_rmap.h"
32
33
34kmem_zone_t *xfs_efi_zone;
35kmem_zone_t *xfs_efd_zone;
36
37static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
38{
39 return container_of(lip, struct xfs_efi_log_item, efi_item);
40}
41
42void
43xfs_efi_item_free(
44 struct xfs_efi_log_item *efip)
45{
46 kmem_free(efip->efi_item.li_lv_shadow);
47 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
48 kmem_free(efip);
49 else
50 kmem_zone_free(xfs_efi_zone, efip);
51}
52
53
54
55
56
57
58static inline int
59xfs_efi_item_sizeof(
60 struct xfs_efi_log_item *efip)
61{
62 return sizeof(struct xfs_efi_log_format) +
63 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
64}
65
66STATIC void
67xfs_efi_item_size(
68 struct xfs_log_item *lip,
69 int *nvecs,
70 int *nbytes)
71{
72 *nvecs += 1;
73 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
74}
75
76
77
78
79
80
81
82
83STATIC void
84xfs_efi_item_format(
85 struct xfs_log_item *lip,
86 struct xfs_log_vec *lv)
87{
88 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
89 struct xfs_log_iovec *vecp = NULL;
90
91 ASSERT(atomic_read(&efip->efi_next_extent) ==
92 efip->efi_format.efi_nextents);
93
94 efip->efi_format.efi_type = XFS_LI_EFI;
95 efip->efi_format.efi_size = 1;
96
97 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
98 &efip->efi_format,
99 xfs_efi_item_sizeof(efip));
100}
101
102
103
104
105
106STATIC void
107xfs_efi_item_pin(
108 struct xfs_log_item *lip)
109{
110}
111
112
113
114
115
116
117
118
119
120STATIC void
121xfs_efi_item_unpin(
122 struct xfs_log_item *lip,
123 int remove)
124{
125 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
126 xfs_efi_release(efip);
127}
128
129
130
131
132
133
134
135
136STATIC uint
137xfs_efi_item_push(
138 struct xfs_log_item *lip,
139 struct list_head *buffer_list)
140{
141 return XFS_ITEM_PINNED;
142}
143
144
145
146
147
148
149STATIC void
150xfs_efi_item_unlock(
151 struct xfs_log_item *lip)
152{
153 if (lip->li_flags & XFS_LI_ABORTED)
154 xfs_efi_item_free(EFI_ITEM(lip));
155}
156
157
158
159
160
161STATIC xfs_lsn_t
162xfs_efi_item_committed(
163 struct xfs_log_item *lip,
164 xfs_lsn_t lsn)
165{
166 return lsn;
167}
168
169
170
171
172
173
174
175
176STATIC void
177xfs_efi_item_committing(
178 struct xfs_log_item *lip,
179 xfs_lsn_t lsn)
180{
181}
182
183
184
185
186static const struct xfs_item_ops xfs_efi_item_ops = {
187 .iop_size = xfs_efi_item_size,
188 .iop_format = xfs_efi_item_format,
189 .iop_pin = xfs_efi_item_pin,
190 .iop_unpin = xfs_efi_item_unpin,
191 .iop_unlock = xfs_efi_item_unlock,
192 .iop_committed = xfs_efi_item_committed,
193 .iop_push = xfs_efi_item_push,
194 .iop_committing = xfs_efi_item_committing
195};
196
197
198
199
200
201struct xfs_efi_log_item *
202xfs_efi_init(
203 struct xfs_mount *mp,
204 uint nextents)
205
206{
207 struct xfs_efi_log_item *efip;
208 uint size;
209
210 ASSERT(nextents > 0);
211 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
212 size = (uint)(sizeof(xfs_efi_log_item_t) +
213 ((nextents - 1) * sizeof(xfs_extent_t)));
214 efip = kmem_zalloc(size, KM_SLEEP);
215 } else {
216 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
217 }
218
219 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
220 efip->efi_format.efi_nextents = nextents;
221 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
222 atomic_set(&efip->efi_next_extent, 0);
223 atomic_set(&efip->efi_refcount, 2);
224
225 return efip;
226}
227
228
229
230
231
232
233
234
235int
236xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
237{
238 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
239 uint i;
240 uint len = sizeof(xfs_efi_log_format_t) +
241 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
242 uint len32 = sizeof(xfs_efi_log_format_32_t) +
243 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
244 uint len64 = sizeof(xfs_efi_log_format_64_t) +
245 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
246
247 if (buf->i_len == len) {
248 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
249 return 0;
250 } else if (buf->i_len == len32) {
251 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
252
253 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
254 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
255 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
256 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
257 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
258 dst_efi_fmt->efi_extents[i].ext_start =
259 src_efi_fmt_32->efi_extents[i].ext_start;
260 dst_efi_fmt->efi_extents[i].ext_len =
261 src_efi_fmt_32->efi_extents[i].ext_len;
262 }
263 return 0;
264 } else if (buf->i_len == len64) {
265 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
266
267 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
268 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
269 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
270 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
271 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
272 dst_efi_fmt->efi_extents[i].ext_start =
273 src_efi_fmt_64->efi_extents[i].ext_start;
274 dst_efi_fmt->efi_extents[i].ext_len =
275 src_efi_fmt_64->efi_extents[i].ext_len;
276 }
277 return 0;
278 }
279 return -EFSCORRUPTED;
280}
281
282
283
284
285
286
287
288
289void
290xfs_efi_release(
291 struct xfs_efi_log_item *efip)
292{
293 ASSERT(atomic_read(&efip->efi_refcount) > 0);
294 if (atomic_dec_and_test(&efip->efi_refcount)) {
295 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
296 xfs_efi_item_free(efip);
297 }
298}
299
300static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
301{
302 return container_of(lip, struct xfs_efd_log_item, efd_item);
303}
304
305STATIC void
306xfs_efd_item_free(struct xfs_efd_log_item *efdp)
307{
308 kmem_free(efdp->efd_item.li_lv_shadow);
309 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
310 kmem_free(efdp);
311 else
312 kmem_zone_free(xfs_efd_zone, efdp);
313}
314
315
316
317
318
319
320static inline int
321xfs_efd_item_sizeof(
322 struct xfs_efd_log_item *efdp)
323{
324 return sizeof(xfs_efd_log_format_t) +
325 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
326}
327
328STATIC void
329xfs_efd_item_size(
330 struct xfs_log_item *lip,
331 int *nvecs,
332 int *nbytes)
333{
334 *nvecs += 1;
335 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
336}
337
338
339
340
341
342
343
344
345STATIC void
346xfs_efd_item_format(
347 struct xfs_log_item *lip,
348 struct xfs_log_vec *lv)
349{
350 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
351 struct xfs_log_iovec *vecp = NULL;
352
353 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
354
355 efdp->efd_format.efd_type = XFS_LI_EFD;
356 efdp->efd_format.efd_size = 1;
357
358 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
359 &efdp->efd_format,
360 xfs_efd_item_sizeof(efdp));
361}
362
363
364
365
366STATIC void
367xfs_efd_item_pin(
368 struct xfs_log_item *lip)
369{
370}
371
372
373
374
375
376STATIC void
377xfs_efd_item_unpin(
378 struct xfs_log_item *lip,
379 int remove)
380{
381}
382
383
384
385
386
387STATIC uint
388xfs_efd_item_push(
389 struct xfs_log_item *lip,
390 struct list_head *buffer_list)
391{
392 return XFS_ITEM_PINNED;
393}
394
395
396
397
398
399STATIC void
400xfs_efd_item_unlock(
401 struct xfs_log_item *lip)
402{
403 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
404
405 if (lip->li_flags & XFS_LI_ABORTED) {
406 xfs_efi_release(efdp->efd_efip);
407 xfs_efd_item_free(efdp);
408 }
409}
410
411
412
413
414
415
416
417STATIC xfs_lsn_t
418xfs_efd_item_committed(
419 struct xfs_log_item *lip,
420 xfs_lsn_t lsn)
421{
422 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
423
424
425
426
427
428
429
430 xfs_efi_release(efdp->efd_efip);
431 xfs_efd_item_free(efdp);
432
433 return (xfs_lsn_t)-1;
434}
435
436
437
438
439
440
441
442
443STATIC void
444xfs_efd_item_committing(
445 struct xfs_log_item *lip,
446 xfs_lsn_t lsn)
447{
448}
449
450
451
452
453static const struct xfs_item_ops xfs_efd_item_ops = {
454 .iop_size = xfs_efd_item_size,
455 .iop_format = xfs_efd_item_format,
456 .iop_pin = xfs_efd_item_pin,
457 .iop_unpin = xfs_efd_item_unpin,
458 .iop_unlock = xfs_efd_item_unlock,
459 .iop_committed = xfs_efd_item_committed,
460 .iop_push = xfs_efd_item_push,
461 .iop_committing = xfs_efd_item_committing
462};
463
464
465
466
467struct xfs_efd_log_item *
468xfs_efd_init(
469 struct xfs_mount *mp,
470 struct xfs_efi_log_item *efip,
471 uint nextents)
472
473{
474 struct xfs_efd_log_item *efdp;
475 uint size;
476
477 ASSERT(nextents > 0);
478 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
479 size = (uint)(sizeof(xfs_efd_log_item_t) +
480 ((nextents - 1) * sizeof(xfs_extent_t)));
481 efdp = kmem_zalloc(size, KM_SLEEP);
482 } else {
483 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
484 }
485
486 xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
487 efdp->efd_efip = efip;
488 efdp->efd_format.efd_nextents = nextents;
489 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
490
491 return efdp;
492}
493
494
495
496
497
498int
499xfs_efi_recover(
500 struct xfs_mount *mp,
501 struct xfs_efi_log_item *efip)
502{
503 struct xfs_efd_log_item *efdp;
504 struct xfs_trans *tp;
505 int i;
506 int error = 0;
507 xfs_extent_t *extp;
508 xfs_fsblock_t startblock_fsb;
509 struct xfs_owner_info oinfo;
510
511 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
512
513
514
515
516
517
518 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
519 extp = &efip->efi_format.efi_extents[i];
520 startblock_fsb = XFS_BB_TO_FSB(mp,
521 XFS_FSB_TO_DADDR(mp, extp->ext_start));
522 if (startblock_fsb == 0 ||
523 extp->ext_len == 0 ||
524 startblock_fsb >= mp->m_sb.sb_dblocks ||
525 extp->ext_len >= mp->m_sb.sb_agblocks) {
526
527
528
529
530 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
531 xfs_efi_release(efip);
532 return -EIO;
533 }
534 }
535
536 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
537 if (error)
538 return error;
539 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
540
541 xfs_rmap_any_owner_update(&oinfo);
542 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
543 extp = &efip->efi_format.efi_extents[i];
544 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
545 extp->ext_len, &oinfo);
546 if (error)
547 goto abort_error;
548
549 }
550
551 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
552 error = xfs_trans_commit(tp);
553 return error;
554
555abort_error:
556 xfs_trans_cancel(tp);
557 return error;
558}
559