This source file includes following definitions.
- CUI_ITEM
- xfs_cui_item_free
- xfs_cui_release
- xfs_cui_item_size
- xfs_cui_item_format
- xfs_cui_item_unpin
- xfs_cui_item_release
- xfs_cui_init
- CUD_ITEM
- xfs_cud_item_size
- xfs_cud_item_format
- xfs_cud_item_release
- xfs_trans_get_cud
- xfs_trans_log_finish_refcount_update
- xfs_refcount_update_diff_items
- xfs_refcount_update_create_intent
- xfs_trans_set_refcount_flags
- xfs_refcount_update_log_item
- xfs_refcount_update_create_done
- xfs_refcount_update_finish_item
- xfs_refcount_update_finish_cleanup
- xfs_refcount_update_abort_intent
- xfs_refcount_update_cancel_item
- xfs_cui_recover
1
2
3
4
5
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_bit.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_refcount_item.h"
18 #include "xfs_log.h"
19 #include "xfs_refcount.h"
20
21
22 kmem_zone_t *xfs_cui_zone;
23 kmem_zone_t *xfs_cud_zone;
24
25 static inline struct xfs_cui_log_item *CUI_ITEM(struct xfs_log_item *lip)
26 {
27 return container_of(lip, struct xfs_cui_log_item, cui_item);
28 }
29
30 void
31 xfs_cui_item_free(
32 struct xfs_cui_log_item *cuip)
33 {
34 if (cuip->cui_format.cui_nextents > XFS_CUI_MAX_FAST_EXTENTS)
35 kmem_free(cuip);
36 else
37 kmem_zone_free(xfs_cui_zone, cuip);
38 }
39
40
41
42
43
44
45
46
47 void
48 xfs_cui_release(
49 struct xfs_cui_log_item *cuip)
50 {
51 ASSERT(atomic_read(&cuip->cui_refcount) > 0);
52 if (atomic_dec_and_test(&cuip->cui_refcount)) {
53 xfs_trans_ail_remove(&cuip->cui_item, SHUTDOWN_LOG_IO_ERROR);
54 xfs_cui_item_free(cuip);
55 }
56 }
57
58
59 STATIC void
60 xfs_cui_item_size(
61 struct xfs_log_item *lip,
62 int *nvecs,
63 int *nbytes)
64 {
65 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
66
67 *nvecs += 1;
68 *nbytes += xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents);
69 }
70
71
72
73
74
75
76
77
78 STATIC void
79 xfs_cui_item_format(
80 struct xfs_log_item *lip,
81 struct xfs_log_vec *lv)
82 {
83 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
84 struct xfs_log_iovec *vecp = NULL;
85
86 ASSERT(atomic_read(&cuip->cui_next_extent) ==
87 cuip->cui_format.cui_nextents);
88
89 cuip->cui_format.cui_type = XFS_LI_CUI;
90 cuip->cui_format.cui_size = 1;
91
92 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUI_FORMAT, &cuip->cui_format,
93 xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents));
94 }
95
96
97
98
99
100
101
102
103
104 STATIC void
105 xfs_cui_item_unpin(
106 struct xfs_log_item *lip,
107 int remove)
108 {
109 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
110
111 xfs_cui_release(cuip);
112 }
113
114
115
116
117
118
119 STATIC void
120 xfs_cui_item_release(
121 struct xfs_log_item *lip)
122 {
123 xfs_cui_release(CUI_ITEM(lip));
124 }
125
126 static const struct xfs_item_ops xfs_cui_item_ops = {
127 .iop_size = xfs_cui_item_size,
128 .iop_format = xfs_cui_item_format,
129 .iop_unpin = xfs_cui_item_unpin,
130 .iop_release = xfs_cui_item_release,
131 };
132
133
134
135
136 struct xfs_cui_log_item *
137 xfs_cui_init(
138 struct xfs_mount *mp,
139 uint nextents)
140
141 {
142 struct xfs_cui_log_item *cuip;
143
144 ASSERT(nextents > 0);
145 if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
146 cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
147 0);
148 else
149 cuip = kmem_zone_zalloc(xfs_cui_zone, 0);
150
151 xfs_log_item_init(mp, &cuip->cui_item, XFS_LI_CUI, &xfs_cui_item_ops);
152 cuip->cui_format.cui_nextents = nextents;
153 cuip->cui_format.cui_id = (uintptr_t)(void *)cuip;
154 atomic_set(&cuip->cui_next_extent, 0);
155 atomic_set(&cuip->cui_refcount, 2);
156
157 return cuip;
158 }
159
160 static inline struct xfs_cud_log_item *CUD_ITEM(struct xfs_log_item *lip)
161 {
162 return container_of(lip, struct xfs_cud_log_item, cud_item);
163 }
164
165 STATIC void
166 xfs_cud_item_size(
167 struct xfs_log_item *lip,
168 int *nvecs,
169 int *nbytes)
170 {
171 *nvecs += 1;
172 *nbytes += sizeof(struct xfs_cud_log_format);
173 }
174
175
176
177
178
179
180
181
182 STATIC void
183 xfs_cud_item_format(
184 struct xfs_log_item *lip,
185 struct xfs_log_vec *lv)
186 {
187 struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
188 struct xfs_log_iovec *vecp = NULL;
189
190 cudp->cud_format.cud_type = XFS_LI_CUD;
191 cudp->cud_format.cud_size = 1;
192
193 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUD_FORMAT, &cudp->cud_format,
194 sizeof(struct xfs_cud_log_format));
195 }
196
197
198
199
200
201
202 STATIC void
203 xfs_cud_item_release(
204 struct xfs_log_item *lip)
205 {
206 struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
207
208 xfs_cui_release(cudp->cud_cuip);
209 kmem_zone_free(xfs_cud_zone, cudp);
210 }
211
212 static const struct xfs_item_ops xfs_cud_item_ops = {
213 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
214 .iop_size = xfs_cud_item_size,
215 .iop_format = xfs_cud_item_format,
216 .iop_release = xfs_cud_item_release,
217 };
218
219 static struct xfs_cud_log_item *
220 xfs_trans_get_cud(
221 struct xfs_trans *tp,
222 struct xfs_cui_log_item *cuip)
223 {
224 struct xfs_cud_log_item *cudp;
225
226 cudp = kmem_zone_zalloc(xfs_cud_zone, 0);
227 xfs_log_item_init(tp->t_mountp, &cudp->cud_item, XFS_LI_CUD,
228 &xfs_cud_item_ops);
229 cudp->cud_cuip = cuip;
230 cudp->cud_format.cud_cui_id = cuip->cui_format.cui_id;
231
232 xfs_trans_add_item(tp, &cudp->cud_item);
233 return cudp;
234 }
235
236
237
238
239
240
241 static int
242 xfs_trans_log_finish_refcount_update(
243 struct xfs_trans *tp,
244 struct xfs_cud_log_item *cudp,
245 enum xfs_refcount_intent_type type,
246 xfs_fsblock_t startblock,
247 xfs_extlen_t blockcount,
248 xfs_fsblock_t *new_fsb,
249 xfs_extlen_t *new_len,
250 struct xfs_btree_cur **pcur)
251 {
252 int error;
253
254 error = xfs_refcount_finish_one(tp, type, startblock,
255 blockcount, new_fsb, new_len, pcur);
256
257
258
259
260
261
262
263
264 tp->t_flags |= XFS_TRANS_DIRTY;
265 set_bit(XFS_LI_DIRTY, &cudp->cud_item.li_flags);
266
267 return error;
268 }
269
270
271 static int
272 xfs_refcount_update_diff_items(
273 void *priv,
274 struct list_head *a,
275 struct list_head *b)
276 {
277 struct xfs_mount *mp = priv;
278 struct xfs_refcount_intent *ra;
279 struct xfs_refcount_intent *rb;
280
281 ra = container_of(a, struct xfs_refcount_intent, ri_list);
282 rb = container_of(b, struct xfs_refcount_intent, ri_list);
283 return XFS_FSB_TO_AGNO(mp, ra->ri_startblock) -
284 XFS_FSB_TO_AGNO(mp, rb->ri_startblock);
285 }
286
287
288 STATIC void *
289 xfs_refcount_update_create_intent(
290 struct xfs_trans *tp,
291 unsigned int count)
292 {
293 struct xfs_cui_log_item *cuip;
294
295 ASSERT(tp != NULL);
296 ASSERT(count > 0);
297
298 cuip = xfs_cui_init(tp->t_mountp, count);
299 ASSERT(cuip != NULL);
300
301
302
303
304 xfs_trans_add_item(tp, &cuip->cui_item);
305 return cuip;
306 }
307
308
309 static void
310 xfs_trans_set_refcount_flags(
311 struct xfs_phys_extent *refc,
312 enum xfs_refcount_intent_type type)
313 {
314 refc->pe_flags = 0;
315 switch (type) {
316 case XFS_REFCOUNT_INCREASE:
317 case XFS_REFCOUNT_DECREASE:
318 case XFS_REFCOUNT_ALLOC_COW:
319 case XFS_REFCOUNT_FREE_COW:
320 refc->pe_flags |= type;
321 break;
322 default:
323 ASSERT(0);
324 }
325 }
326
327
328 STATIC void
329 xfs_refcount_update_log_item(
330 struct xfs_trans *tp,
331 void *intent,
332 struct list_head *item)
333 {
334 struct xfs_cui_log_item *cuip = intent;
335 struct xfs_refcount_intent *refc;
336 uint next_extent;
337 struct xfs_phys_extent *ext;
338
339 refc = container_of(item, struct xfs_refcount_intent, ri_list);
340
341 tp->t_flags |= XFS_TRANS_DIRTY;
342 set_bit(XFS_LI_DIRTY, &cuip->cui_item.li_flags);
343
344
345
346
347
348
349 next_extent = atomic_inc_return(&cuip->cui_next_extent) - 1;
350 ASSERT(next_extent < cuip->cui_format.cui_nextents);
351 ext = &cuip->cui_format.cui_extents[next_extent];
352 ext->pe_startblock = refc->ri_startblock;
353 ext->pe_len = refc->ri_blockcount;
354 xfs_trans_set_refcount_flags(ext, refc->ri_type);
355 }
356
357
358 STATIC void *
359 xfs_refcount_update_create_done(
360 struct xfs_trans *tp,
361 void *intent,
362 unsigned int count)
363 {
364 return xfs_trans_get_cud(tp, intent);
365 }
366
367
368 STATIC int
369 xfs_refcount_update_finish_item(
370 struct xfs_trans *tp,
371 struct list_head *item,
372 void *done_item,
373 void **state)
374 {
375 struct xfs_refcount_intent *refc;
376 xfs_fsblock_t new_fsb;
377 xfs_extlen_t new_aglen;
378 int error;
379
380 refc = container_of(item, struct xfs_refcount_intent, ri_list);
381 error = xfs_trans_log_finish_refcount_update(tp, done_item,
382 refc->ri_type,
383 refc->ri_startblock,
384 refc->ri_blockcount,
385 &new_fsb, &new_aglen,
386 (struct xfs_btree_cur **)state);
387
388 if (!error && new_aglen > 0) {
389 ASSERT(refc->ri_type == XFS_REFCOUNT_INCREASE ||
390 refc->ri_type == XFS_REFCOUNT_DECREASE);
391 refc->ri_startblock = new_fsb;
392 refc->ri_blockcount = new_aglen;
393 return -EAGAIN;
394 }
395 kmem_free(refc);
396 return error;
397 }
398
399
400 STATIC void
401 xfs_refcount_update_finish_cleanup(
402 struct xfs_trans *tp,
403 void *state,
404 int error)
405 {
406 struct xfs_btree_cur *rcur = state;
407
408 xfs_refcount_finish_one_cleanup(tp, rcur, error);
409 }
410
411
412 STATIC void
413 xfs_refcount_update_abort_intent(
414 void *intent)
415 {
416 xfs_cui_release(intent);
417 }
418
419
420 STATIC void
421 xfs_refcount_update_cancel_item(
422 struct list_head *item)
423 {
424 struct xfs_refcount_intent *refc;
425
426 refc = container_of(item, struct xfs_refcount_intent, ri_list);
427 kmem_free(refc);
428 }
429
430 const struct xfs_defer_op_type xfs_refcount_update_defer_type = {
431 .max_items = XFS_CUI_MAX_FAST_EXTENTS,
432 .diff_items = xfs_refcount_update_diff_items,
433 .create_intent = xfs_refcount_update_create_intent,
434 .abort_intent = xfs_refcount_update_abort_intent,
435 .log_item = xfs_refcount_update_log_item,
436 .create_done = xfs_refcount_update_create_done,
437 .finish_item = xfs_refcount_update_finish_item,
438 .finish_cleanup = xfs_refcount_update_finish_cleanup,
439 .cancel_item = xfs_refcount_update_cancel_item,
440 };
441
442
443
444
445
446 int
447 xfs_cui_recover(
448 struct xfs_trans *parent_tp,
449 struct xfs_cui_log_item *cuip)
450 {
451 int i;
452 int error = 0;
453 unsigned int refc_type;
454 struct xfs_phys_extent *refc;
455 xfs_fsblock_t startblock_fsb;
456 bool op_ok;
457 struct xfs_cud_log_item *cudp;
458 struct xfs_trans *tp;
459 struct xfs_btree_cur *rcur = NULL;
460 enum xfs_refcount_intent_type type;
461 xfs_fsblock_t new_fsb;
462 xfs_extlen_t new_len;
463 struct xfs_bmbt_irec irec;
464 bool requeue_only = false;
465 struct xfs_mount *mp = parent_tp->t_mountp;
466
467 ASSERT(!test_bit(XFS_CUI_RECOVERED, &cuip->cui_flags));
468
469
470
471
472
473
474 for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
475 refc = &cuip->cui_format.cui_extents[i];
476 startblock_fsb = XFS_BB_TO_FSB(mp,
477 XFS_FSB_TO_DADDR(mp, refc->pe_startblock));
478 switch (refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK) {
479 case XFS_REFCOUNT_INCREASE:
480 case XFS_REFCOUNT_DECREASE:
481 case XFS_REFCOUNT_ALLOC_COW:
482 case XFS_REFCOUNT_FREE_COW:
483 op_ok = true;
484 break;
485 default:
486 op_ok = false;
487 break;
488 }
489 if (!op_ok || startblock_fsb == 0 ||
490 refc->pe_len == 0 ||
491 startblock_fsb >= mp->m_sb.sb_dblocks ||
492 refc->pe_len >= mp->m_sb.sb_agblocks ||
493 (refc->pe_flags & ~XFS_REFCOUNT_EXTENT_FLAGS)) {
494
495
496
497
498 set_bit(XFS_CUI_RECOVERED, &cuip->cui_flags);
499 xfs_cui_release(cuip);
500 return -EIO;
501 }
502 }
503
504
505
506
507
508
509
510
511
512
513
514
515
516 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
517 mp->m_refc_maxlevels * 2, 0, XFS_TRANS_RESERVE, &tp);
518 if (error)
519 return error;
520
521
522
523
524
525 xfs_defer_move(tp, parent_tp);
526 cudp = xfs_trans_get_cud(tp, cuip);
527
528 for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
529 refc = &cuip->cui_format.cui_extents[i];
530 refc_type = refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK;
531 switch (refc_type) {
532 case XFS_REFCOUNT_INCREASE:
533 case XFS_REFCOUNT_DECREASE:
534 case XFS_REFCOUNT_ALLOC_COW:
535 case XFS_REFCOUNT_FREE_COW:
536 type = refc_type;
537 break;
538 default:
539 error = -EFSCORRUPTED;
540 goto abort_error;
541 }
542 if (requeue_only) {
543 new_fsb = refc->pe_startblock;
544 new_len = refc->pe_len;
545 } else
546 error = xfs_trans_log_finish_refcount_update(tp, cudp,
547 type, refc->pe_startblock, refc->pe_len,
548 &new_fsb, &new_len, &rcur);
549 if (error)
550 goto abort_error;
551
552
553 if (new_len > 0) {
554 irec.br_startblock = new_fsb;
555 irec.br_blockcount = new_len;
556 switch (type) {
557 case XFS_REFCOUNT_INCREASE:
558 xfs_refcount_increase_extent(tp, &irec);
559 break;
560 case XFS_REFCOUNT_DECREASE:
561 xfs_refcount_decrease_extent(tp, &irec);
562 break;
563 case XFS_REFCOUNT_ALLOC_COW:
564 xfs_refcount_alloc_cow_extent(tp,
565 irec.br_startblock,
566 irec.br_blockcount);
567 break;
568 case XFS_REFCOUNT_FREE_COW:
569 xfs_refcount_free_cow_extent(tp,
570 irec.br_startblock,
571 irec.br_blockcount);
572 break;
573 default:
574 ASSERT(0);
575 }
576 requeue_only = true;
577 }
578 }
579
580 xfs_refcount_finish_one_cleanup(tp, rcur, error);
581 set_bit(XFS_CUI_RECOVERED, &cuip->cui_flags);
582 xfs_defer_move(parent_tp, tp);
583 error = xfs_trans_commit(tp);
584 return error;
585
586 abort_error:
587 xfs_refcount_finish_one_cleanup(tp, rcur, error);
588 xfs_defer_move(parent_tp, tp);
589 xfs_trans_cancel(tp);
590 return error;
591 }