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_inode.h"
16#include "xfs_trans.h"
17#include "xfs_trans_priv.h"
18#include "xfs_bmap_item.h"
19#include "xfs_log.h"
20#include "xfs_bmap.h"
21#include "xfs_icache.h"
22#include "xfs_bmap_btree.h"
23#include "xfs_trans_space.h"
24#include "xfs_error.h"
25#include "xfs_log_priv.h"
26#include "xfs_log_recover.h"
27#include "xfs_quota.h"
28
29kmem_zone_t *xfs_bui_zone;
30kmem_zone_t *xfs_bud_zone;
31
32static const struct xfs_item_ops xfs_bui_item_ops;
33
34static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
35{
36 return container_of(lip, struct xfs_bui_log_item, bui_item);
37}
38
39STATIC void
40xfs_bui_item_free(
41 struct xfs_bui_log_item *buip)
42{
43 kmem_cache_free(xfs_bui_zone, buip);
44}
45
46
47
48
49
50
51
52
53STATIC void
54xfs_bui_release(
55 struct xfs_bui_log_item *buip)
56{
57 ASSERT(atomic_read(&buip->bui_refcount) > 0);
58 if (atomic_dec_and_test(&buip->bui_refcount)) {
59 xfs_trans_ail_delete(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
60 xfs_bui_item_free(buip);
61 }
62}
63
64
65STATIC void
66xfs_bui_item_size(
67 struct xfs_log_item *lip,
68 int *nvecs,
69 int *nbytes)
70{
71 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
72
73 *nvecs += 1;
74 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
75}
76
77
78
79
80
81
82
83
84STATIC void
85xfs_bui_item_format(
86 struct xfs_log_item *lip,
87 struct xfs_log_vec *lv)
88{
89 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
90 struct xfs_log_iovec *vecp = NULL;
91
92 ASSERT(atomic_read(&buip->bui_next_extent) ==
93 buip->bui_format.bui_nextents);
94
95 buip->bui_format.bui_type = XFS_LI_BUI;
96 buip->bui_format.bui_size = 1;
97
98 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
99 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
100}
101
102
103
104
105
106
107
108
109
110STATIC void
111xfs_bui_item_unpin(
112 struct xfs_log_item *lip,
113 int remove)
114{
115 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
116
117 xfs_bui_release(buip);
118}
119
120
121
122
123
124
125STATIC void
126xfs_bui_item_release(
127 struct xfs_log_item *lip)
128{
129 xfs_bui_release(BUI_ITEM(lip));
130}
131
132
133
134
135STATIC struct xfs_bui_log_item *
136xfs_bui_init(
137 struct xfs_mount *mp)
138
139{
140 struct xfs_bui_log_item *buip;
141
142 buip = kmem_cache_zalloc(xfs_bui_zone, GFP_KERNEL | __GFP_NOFAIL);
143
144 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
145 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
146 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
147 atomic_set(&buip->bui_next_extent, 0);
148 atomic_set(&buip->bui_refcount, 2);
149
150 return buip;
151}
152
153static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
154{
155 return container_of(lip, struct xfs_bud_log_item, bud_item);
156}
157
158STATIC void
159xfs_bud_item_size(
160 struct xfs_log_item *lip,
161 int *nvecs,
162 int *nbytes)
163{
164 *nvecs += 1;
165 *nbytes += sizeof(struct xfs_bud_log_format);
166}
167
168
169
170
171
172
173
174
175STATIC void
176xfs_bud_item_format(
177 struct xfs_log_item *lip,
178 struct xfs_log_vec *lv)
179{
180 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
181 struct xfs_log_iovec *vecp = NULL;
182
183 budp->bud_format.bud_type = XFS_LI_BUD;
184 budp->bud_format.bud_size = 1;
185
186 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
187 sizeof(struct xfs_bud_log_format));
188}
189
190
191
192
193
194
195STATIC void
196xfs_bud_item_release(
197 struct xfs_log_item *lip)
198{
199 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
200
201 xfs_bui_release(budp->bud_buip);
202 kmem_cache_free(xfs_bud_zone, budp);
203}
204
205static const struct xfs_item_ops xfs_bud_item_ops = {
206 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
207 .iop_size = xfs_bud_item_size,
208 .iop_format = xfs_bud_item_format,
209 .iop_release = xfs_bud_item_release,
210};
211
212static struct xfs_bud_log_item *
213xfs_trans_get_bud(
214 struct xfs_trans *tp,
215 struct xfs_bui_log_item *buip)
216{
217 struct xfs_bud_log_item *budp;
218
219 budp = kmem_cache_zalloc(xfs_bud_zone, GFP_KERNEL | __GFP_NOFAIL);
220 xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
221 &xfs_bud_item_ops);
222 budp->bud_buip = buip;
223 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
224
225 xfs_trans_add_item(tp, &budp->bud_item);
226 return budp;
227}
228
229
230
231
232
233
234static int
235xfs_trans_log_finish_bmap_update(
236 struct xfs_trans *tp,
237 struct xfs_bud_log_item *budp,
238 enum xfs_bmap_intent_type type,
239 struct xfs_inode *ip,
240 int whichfork,
241 xfs_fileoff_t startoff,
242 xfs_fsblock_t startblock,
243 xfs_filblks_t *blockcount,
244 xfs_exntst_t state)
245{
246 int error;
247
248 error = xfs_bmap_finish_one(tp, ip, type, whichfork, startoff,
249 startblock, blockcount, state);
250
251
252
253
254
255
256
257
258 tp->t_flags |= XFS_TRANS_DIRTY;
259 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
260
261 return error;
262}
263
264
265static int
266xfs_bmap_update_diff_items(
267 void *priv,
268 struct list_head *a,
269 struct list_head *b)
270{
271 struct xfs_bmap_intent *ba;
272 struct xfs_bmap_intent *bb;
273
274 ba = container_of(a, struct xfs_bmap_intent, bi_list);
275 bb = container_of(b, struct xfs_bmap_intent, bi_list);
276 return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
277}
278
279
280static void
281xfs_trans_set_bmap_flags(
282 struct xfs_map_extent *bmap,
283 enum xfs_bmap_intent_type type,
284 int whichfork,
285 xfs_exntst_t state)
286{
287 bmap->me_flags = 0;
288 switch (type) {
289 case XFS_BMAP_MAP:
290 case XFS_BMAP_UNMAP:
291 bmap->me_flags = type;
292 break;
293 default:
294 ASSERT(0);
295 }
296 if (state == XFS_EXT_UNWRITTEN)
297 bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
298 if (whichfork == XFS_ATTR_FORK)
299 bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
300}
301
302
303STATIC void
304xfs_bmap_update_log_item(
305 struct xfs_trans *tp,
306 struct xfs_bui_log_item *buip,
307 struct xfs_bmap_intent *bmap)
308{
309 uint next_extent;
310 struct xfs_map_extent *map;
311
312 tp->t_flags |= XFS_TRANS_DIRTY;
313 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
314
315
316
317
318
319
320 next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
321 ASSERT(next_extent < buip->bui_format.bui_nextents);
322 map = &buip->bui_format.bui_extents[next_extent];
323 map->me_owner = bmap->bi_owner->i_ino;
324 map->me_startblock = bmap->bi_bmap.br_startblock;
325 map->me_startoff = bmap->bi_bmap.br_startoff;
326 map->me_len = bmap->bi_bmap.br_blockcount;
327 xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
328 bmap->bi_bmap.br_state);
329}
330
331static struct xfs_log_item *
332xfs_bmap_update_create_intent(
333 struct xfs_trans *tp,
334 struct list_head *items,
335 unsigned int count,
336 bool sort)
337{
338 struct xfs_mount *mp = tp->t_mountp;
339 struct xfs_bui_log_item *buip = xfs_bui_init(mp);
340 struct xfs_bmap_intent *bmap;
341
342 ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
343
344 xfs_trans_add_item(tp, &buip->bui_item);
345 if (sort)
346 list_sort(mp, items, xfs_bmap_update_diff_items);
347 list_for_each_entry(bmap, items, bi_list)
348 xfs_bmap_update_log_item(tp, buip, bmap);
349 return &buip->bui_item;
350}
351
352
353static struct xfs_log_item *
354xfs_bmap_update_create_done(
355 struct xfs_trans *tp,
356 struct xfs_log_item *intent,
357 unsigned int count)
358{
359 return &xfs_trans_get_bud(tp, BUI_ITEM(intent))->bud_item;
360}
361
362
363STATIC int
364xfs_bmap_update_finish_item(
365 struct xfs_trans *tp,
366 struct xfs_log_item *done,
367 struct list_head *item,
368 struct xfs_btree_cur **state)
369{
370 struct xfs_bmap_intent *bmap;
371 xfs_filblks_t count;
372 int error;
373
374 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
375 count = bmap->bi_bmap.br_blockcount;
376 error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done),
377 bmap->bi_type,
378 bmap->bi_owner, bmap->bi_whichfork,
379 bmap->bi_bmap.br_startoff,
380 bmap->bi_bmap.br_startblock,
381 &count,
382 bmap->bi_bmap.br_state);
383 if (!error && count > 0) {
384 ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
385 bmap->bi_bmap.br_blockcount = count;
386 return -EAGAIN;
387 }
388 kmem_free(bmap);
389 return error;
390}
391
392
393STATIC void
394xfs_bmap_update_abort_intent(
395 struct xfs_log_item *intent)
396{
397 xfs_bui_release(BUI_ITEM(intent));
398}
399
400
401STATIC void
402xfs_bmap_update_cancel_item(
403 struct list_head *item)
404{
405 struct xfs_bmap_intent *bmap;
406
407 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
408 kmem_free(bmap);
409}
410
411const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
412 .max_items = XFS_BUI_MAX_FAST_EXTENTS,
413 .create_intent = xfs_bmap_update_create_intent,
414 .abort_intent = xfs_bmap_update_abort_intent,
415 .create_done = xfs_bmap_update_create_done,
416 .finish_item = xfs_bmap_update_finish_item,
417 .cancel_item = xfs_bmap_update_cancel_item,
418};
419
420
421static inline bool
422xfs_bui_validate(
423 struct xfs_mount *mp,
424 struct xfs_bui_log_item *buip)
425{
426 struct xfs_map_extent *bmap;
427
428
429 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
430 return false;
431
432 bmap = &buip->bui_format.bui_extents[0];
433
434 if (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
435 return false;
436
437 switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
438 case XFS_BMAP_MAP:
439 case XFS_BMAP_UNMAP:
440 break;
441 default:
442 return false;
443 }
444
445 if (!xfs_verify_ino(mp, bmap->me_owner))
446 return false;
447
448 if (!xfs_verify_fileext(mp, bmap->me_startoff, bmap->me_len))
449 return false;
450
451 return xfs_verify_fsbext(mp, bmap->me_startblock, bmap->me_len);
452}
453
454
455
456
457
458STATIC int
459xfs_bui_item_recover(
460 struct xfs_log_item *lip,
461 struct list_head *capture_list)
462{
463 struct xfs_bmbt_irec irec;
464 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
465 struct xfs_trans *tp;
466 struct xfs_inode *ip = NULL;
467 struct xfs_mount *mp = lip->li_mountp;
468 struct xfs_map_extent *bmap;
469 struct xfs_bud_log_item *budp;
470 xfs_filblks_t count;
471 xfs_exntst_t state;
472 unsigned int bui_type;
473 int whichfork;
474 int iext_delta;
475 int error = 0;
476
477 if (!xfs_bui_validate(mp, buip)) {
478 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
479 &buip->bui_format, sizeof(buip->bui_format));
480 return -EFSCORRUPTED;
481 }
482
483 bmap = &buip->bui_format.bui_extents[0];
484 state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
485 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
486 whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
487 XFS_ATTR_FORK : XFS_DATA_FORK;
488 bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
489
490
491 error = xfs_iget(mp, NULL, bmap->me_owner, 0, 0, &ip);
492 if (error)
493 return error;
494
495 error = xfs_qm_dqattach(ip);
496 if (error)
497 goto err_rele;
498
499 if (VFS_I(ip)->i_nlink == 0)
500 xfs_iflags_set(ip, XFS_IRECOVERY);
501
502
503 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
504 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
505 if (error)
506 goto err_rele;
507
508 budp = xfs_trans_get_bud(tp, buip);
509 xfs_ilock(ip, XFS_ILOCK_EXCL);
510 xfs_trans_ijoin(tp, ip, 0);
511
512 if (bui_type == XFS_BMAP_MAP)
513 iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
514 else
515 iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
516
517 error = xfs_iext_count_may_overflow(ip, whichfork, iext_delta);
518 if (error)
519 goto err_cancel;
520
521 count = bmap->me_len;
522 error = xfs_trans_log_finish_bmap_update(tp, budp, bui_type, ip,
523 whichfork, bmap->me_startoff, bmap->me_startblock,
524 &count, state);
525 if (error)
526 goto err_cancel;
527
528 if (count > 0) {
529 ASSERT(bui_type == XFS_BMAP_UNMAP);
530 irec.br_startblock = bmap->me_startblock;
531 irec.br_blockcount = count;
532 irec.br_startoff = bmap->me_startoff;
533 irec.br_state = state;
534 xfs_bmap_unmap_extent(tp, ip, &irec);
535 }
536
537
538
539
540
541 error = xfs_defer_ops_capture_and_commit(tp, ip, capture_list);
542 if (error)
543 goto err_unlock;
544
545 xfs_iunlock(ip, XFS_ILOCK_EXCL);
546 xfs_irele(ip);
547 return 0;
548
549err_cancel:
550 xfs_trans_cancel(tp);
551err_unlock:
552 xfs_iunlock(ip, XFS_ILOCK_EXCL);
553err_rele:
554 xfs_irele(ip);
555 return error;
556}
557
558STATIC bool
559xfs_bui_item_match(
560 struct xfs_log_item *lip,
561 uint64_t intent_id)
562{
563 return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
564}
565
566
567static struct xfs_log_item *
568xfs_bui_item_relog(
569 struct xfs_log_item *intent,
570 struct xfs_trans *tp)
571{
572 struct xfs_bud_log_item *budp;
573 struct xfs_bui_log_item *buip;
574 struct xfs_map_extent *extp;
575 unsigned int count;
576
577 count = BUI_ITEM(intent)->bui_format.bui_nextents;
578 extp = BUI_ITEM(intent)->bui_format.bui_extents;
579
580 tp->t_flags |= XFS_TRANS_DIRTY;
581 budp = xfs_trans_get_bud(tp, BUI_ITEM(intent));
582 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
583
584 buip = xfs_bui_init(tp->t_mountp);
585 memcpy(buip->bui_format.bui_extents, extp, count * sizeof(*extp));
586 atomic_set(&buip->bui_next_extent, count);
587 xfs_trans_add_item(tp, &buip->bui_item);
588 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
589 return &buip->bui_item;
590}
591
592static const struct xfs_item_ops xfs_bui_item_ops = {
593 .iop_size = xfs_bui_item_size,
594 .iop_format = xfs_bui_item_format,
595 .iop_unpin = xfs_bui_item_unpin,
596 .iop_release = xfs_bui_item_release,
597 .iop_recover = xfs_bui_item_recover,
598 .iop_match = xfs_bui_item_match,
599 .iop_relog = xfs_bui_item_relog,
600};
601
602
603
604
605
606
607static int
608xfs_bui_copy_format(
609 struct xfs_log_iovec *buf,
610 struct xfs_bui_log_format *dst_bui_fmt)
611{
612 struct xfs_bui_log_format *src_bui_fmt;
613 uint len;
614
615 src_bui_fmt = buf->i_addr;
616 len = xfs_bui_log_format_sizeof(src_bui_fmt->bui_nextents);
617
618 if (buf->i_len == len) {
619 memcpy(dst_bui_fmt, src_bui_fmt, len);
620 return 0;
621 }
622 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
623 return -EFSCORRUPTED;
624}
625
626
627
628
629
630
631
632
633STATIC int
634xlog_recover_bui_commit_pass2(
635 struct xlog *log,
636 struct list_head *buffer_list,
637 struct xlog_recover_item *item,
638 xfs_lsn_t lsn)
639{
640 int error;
641 struct xfs_mount *mp = log->l_mp;
642 struct xfs_bui_log_item *buip;
643 struct xfs_bui_log_format *bui_formatp;
644
645 bui_formatp = item->ri_buf[0].i_addr;
646
647 if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
648 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
649 return -EFSCORRUPTED;
650 }
651 buip = xfs_bui_init(mp);
652 error = xfs_bui_copy_format(&item->ri_buf[0], &buip->bui_format);
653 if (error) {
654 xfs_bui_item_free(buip);
655 return error;
656 }
657 atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
658
659
660
661
662 xfs_trans_ail_insert(log->l_ailp, &buip->bui_item, lsn);
663 xfs_bui_release(buip);
664 return 0;
665}
666
667const struct xlog_recover_item_ops xlog_bui_item_ops = {
668 .item_type = XFS_LI_BUI,
669 .commit_pass2 = xlog_recover_bui_commit_pass2,
670};
671
672
673
674
675
676
677
678
679STATIC int
680xlog_recover_bud_commit_pass2(
681 struct xlog *log,
682 struct list_head *buffer_list,
683 struct xlog_recover_item *item,
684 xfs_lsn_t lsn)
685{
686 struct xfs_bud_log_format *bud_formatp;
687
688 bud_formatp = item->ri_buf[0].i_addr;
689 if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
690 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
691 return -EFSCORRUPTED;
692 }
693
694 xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
695 return 0;
696}
697
698const struct xlog_recover_item_ops xlog_bud_item_ops = {
699 .item_type = XFS_LI_BUD,
700 .commit_pass2 = xlog_recover_bud_commit_pass2,
701};
702