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_mount.h"
13#include "xfs_defer.h"
14#include "xfs_inode.h"
15#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
17#include "xfs_buf_item.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_trace.h"
23#include "xfs_bmap_btree.h"
24#include "xfs_trans_space.h"
25
26
27kmem_zone_t *xfs_bui_zone;
28kmem_zone_t *xfs_bud_zone;
29
30static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
31{
32 return container_of(lip, struct xfs_bui_log_item, bui_item);
33}
34
35void
36xfs_bui_item_free(
37 struct xfs_bui_log_item *buip)
38{
39 kmem_zone_free(xfs_bui_zone, buip);
40}
41
42
43
44
45
46
47
48
49void
50xfs_bui_release(
51 struct xfs_bui_log_item *buip)
52{
53 ASSERT(atomic_read(&buip->bui_refcount) > 0);
54 if (atomic_dec_and_test(&buip->bui_refcount)) {
55 xfs_trans_ail_remove(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
56 xfs_bui_item_free(buip);
57 }
58}
59
60
61STATIC void
62xfs_bui_item_size(
63 struct xfs_log_item *lip,
64 int *nvecs,
65 int *nbytes)
66{
67 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
68
69 *nvecs += 1;
70 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
71}
72
73
74
75
76
77
78
79
80STATIC void
81xfs_bui_item_format(
82 struct xfs_log_item *lip,
83 struct xfs_log_vec *lv)
84{
85 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
86 struct xfs_log_iovec *vecp = NULL;
87
88 ASSERT(atomic_read(&buip->bui_next_extent) ==
89 buip->bui_format.bui_nextents);
90
91 buip->bui_format.bui_type = XFS_LI_BUI;
92 buip->bui_format.bui_size = 1;
93
94 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
95 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
96}
97
98
99
100
101STATIC void
102xfs_bui_item_pin(
103 struct xfs_log_item *lip)
104{
105}
106
107
108
109
110
111
112
113
114
115STATIC void
116xfs_bui_item_unpin(
117 struct xfs_log_item *lip,
118 int remove)
119{
120 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
121
122 xfs_bui_release(buip);
123}
124
125
126
127
128
129
130
131
132STATIC uint
133xfs_bui_item_push(
134 struct xfs_log_item *lip,
135 struct list_head *buffer_list)
136{
137 return XFS_ITEM_PINNED;
138}
139
140
141
142
143
144
145STATIC void
146xfs_bui_item_unlock(
147 struct xfs_log_item *lip)
148{
149 if (test_bit(XFS_LI_ABORTED, &lip->li_flags))
150 xfs_bui_release(BUI_ITEM(lip));
151}
152
153
154
155
156
157STATIC xfs_lsn_t
158xfs_bui_item_committed(
159 struct xfs_log_item *lip,
160 xfs_lsn_t lsn)
161{
162 return lsn;
163}
164
165
166
167
168
169
170
171
172STATIC void
173xfs_bui_item_committing(
174 struct xfs_log_item *lip,
175 xfs_lsn_t lsn)
176{
177}
178
179
180
181
182static const struct xfs_item_ops xfs_bui_item_ops = {
183 .iop_size = xfs_bui_item_size,
184 .iop_format = xfs_bui_item_format,
185 .iop_pin = xfs_bui_item_pin,
186 .iop_unpin = xfs_bui_item_unpin,
187 .iop_unlock = xfs_bui_item_unlock,
188 .iop_committed = xfs_bui_item_committed,
189 .iop_push = xfs_bui_item_push,
190 .iop_committing = xfs_bui_item_committing,
191};
192
193
194
195
196struct xfs_bui_log_item *
197xfs_bui_init(
198 struct xfs_mount *mp)
199
200{
201 struct xfs_bui_log_item *buip;
202
203 buip = kmem_zone_zalloc(xfs_bui_zone, KM_SLEEP);
204
205 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
206 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
207 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
208 atomic_set(&buip->bui_next_extent, 0);
209 atomic_set(&buip->bui_refcount, 2);
210
211 return buip;
212}
213
214static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
215{
216 return container_of(lip, struct xfs_bud_log_item, bud_item);
217}
218
219STATIC void
220xfs_bud_item_size(
221 struct xfs_log_item *lip,
222 int *nvecs,
223 int *nbytes)
224{
225 *nvecs += 1;
226 *nbytes += sizeof(struct xfs_bud_log_format);
227}
228
229
230
231
232
233
234
235
236STATIC void
237xfs_bud_item_format(
238 struct xfs_log_item *lip,
239 struct xfs_log_vec *lv)
240{
241 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
242 struct xfs_log_iovec *vecp = NULL;
243
244 budp->bud_format.bud_type = XFS_LI_BUD;
245 budp->bud_format.bud_size = 1;
246
247 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
248 sizeof(struct xfs_bud_log_format));
249}
250
251
252
253
254STATIC void
255xfs_bud_item_pin(
256 struct xfs_log_item *lip)
257{
258}
259
260
261
262
263
264STATIC void
265xfs_bud_item_unpin(
266 struct xfs_log_item *lip,
267 int remove)
268{
269}
270
271
272
273
274
275STATIC uint
276xfs_bud_item_push(
277 struct xfs_log_item *lip,
278 struct list_head *buffer_list)
279{
280 return XFS_ITEM_PINNED;
281}
282
283
284
285
286
287
288STATIC void
289xfs_bud_item_unlock(
290 struct xfs_log_item *lip)
291{
292 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
293
294 if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) {
295 xfs_bui_release(budp->bud_buip);
296 kmem_zone_free(xfs_bud_zone, budp);
297 }
298}
299
300
301
302
303
304
305
306STATIC xfs_lsn_t
307xfs_bud_item_committed(
308 struct xfs_log_item *lip,
309 xfs_lsn_t lsn)
310{
311 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
312
313
314
315
316
317
318
319 xfs_bui_release(budp->bud_buip);
320 kmem_zone_free(xfs_bud_zone, budp);
321
322 return (xfs_lsn_t)-1;
323}
324
325
326
327
328
329
330
331
332STATIC void
333xfs_bud_item_committing(
334 struct xfs_log_item *lip,
335 xfs_lsn_t lsn)
336{
337}
338
339
340
341
342static const struct xfs_item_ops xfs_bud_item_ops = {
343 .iop_size = xfs_bud_item_size,
344 .iop_format = xfs_bud_item_format,
345 .iop_pin = xfs_bud_item_pin,
346 .iop_unpin = xfs_bud_item_unpin,
347 .iop_unlock = xfs_bud_item_unlock,
348 .iop_committed = xfs_bud_item_committed,
349 .iop_push = xfs_bud_item_push,
350 .iop_committing = xfs_bud_item_committing,
351};
352
353
354
355
356struct xfs_bud_log_item *
357xfs_bud_init(
358 struct xfs_mount *mp,
359 struct xfs_bui_log_item *buip)
360
361{
362 struct xfs_bud_log_item *budp;
363
364 budp = kmem_zone_zalloc(xfs_bud_zone, KM_SLEEP);
365 xfs_log_item_init(mp, &budp->bud_item, XFS_LI_BUD, &xfs_bud_item_ops);
366 budp->bud_buip = buip;
367 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
368
369 return budp;
370}
371
372
373
374
375
376int
377xfs_bui_recover(
378 struct xfs_trans *parent_tp,
379 struct xfs_bui_log_item *buip)
380{
381 int error = 0;
382 unsigned int bui_type;
383 struct xfs_map_extent *bmap;
384 xfs_fsblock_t startblock_fsb;
385 xfs_fsblock_t inode_fsb;
386 xfs_filblks_t count;
387 bool op_ok;
388 struct xfs_bud_log_item *budp;
389 enum xfs_bmap_intent_type type;
390 int whichfork;
391 xfs_exntst_t state;
392 struct xfs_trans *tp;
393 struct xfs_inode *ip = NULL;
394 struct xfs_bmbt_irec irec;
395 struct xfs_mount *mp = parent_tp->t_mountp;
396
397 ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
398
399
400 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
401 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
402 xfs_bui_release(buip);
403 return -EIO;
404 }
405
406
407
408
409
410 bmap = &buip->bui_format.bui_extents[0];
411 startblock_fsb = XFS_BB_TO_FSB(mp,
412 XFS_FSB_TO_DADDR(mp, bmap->me_startblock));
413 inode_fsb = XFS_BB_TO_FSB(mp, XFS_FSB_TO_DADDR(mp,
414 XFS_INO_TO_FSB(mp, bmap->me_owner)));
415 switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
416 case XFS_BMAP_MAP:
417 case XFS_BMAP_UNMAP:
418 op_ok = true;
419 break;
420 default:
421 op_ok = false;
422 break;
423 }
424 if (!op_ok || startblock_fsb == 0 ||
425 bmap->me_len == 0 ||
426 inode_fsb == 0 ||
427 startblock_fsb >= mp->m_sb.sb_dblocks ||
428 bmap->me_len >= mp->m_sb.sb_agblocks ||
429 inode_fsb >= mp->m_sb.sb_dblocks ||
430 (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) {
431
432
433
434
435 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
436 xfs_bui_release(buip);
437 return -EIO;
438 }
439
440 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
441 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
442 if (error)
443 return error;
444
445
446
447
448
449 xfs_defer_move(tp, parent_tp);
450 budp = xfs_trans_get_bud(tp, buip);
451
452
453 error = xfs_iget(mp, tp, bmap->me_owner, 0, XFS_ILOCK_EXCL, &ip);
454 if (error)
455 goto err_inode;
456
457 if (VFS_I(ip)->i_nlink == 0)
458 xfs_iflags_set(ip, XFS_IRECOVERY);
459
460
461 state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
462 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
463 whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
464 XFS_ATTR_FORK : XFS_DATA_FORK;
465 bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
466 switch (bui_type) {
467 case XFS_BMAP_MAP:
468 case XFS_BMAP_UNMAP:
469 type = bui_type;
470 break;
471 default:
472 error = -EFSCORRUPTED;
473 goto err_inode;
474 }
475 xfs_trans_ijoin(tp, ip, 0);
476
477 count = bmap->me_len;
478 error = xfs_trans_log_finish_bmap_update(tp, budp, type, ip, whichfork,
479 bmap->me_startoff, bmap->me_startblock, &count, state);
480 if (error)
481 goto err_inode;
482
483 if (count > 0) {
484 ASSERT(type == XFS_BMAP_UNMAP);
485 irec.br_startblock = bmap->me_startblock;
486 irec.br_blockcount = count;
487 irec.br_startoff = bmap->me_startoff;
488 irec.br_state = state;
489 error = xfs_bmap_unmap_extent(tp, ip, &irec);
490 if (error)
491 goto err_inode;
492 }
493
494 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
495 xfs_defer_move(parent_tp, tp);
496 error = xfs_trans_commit(tp);
497 xfs_iunlock(ip, XFS_ILOCK_EXCL);
498 xfs_irele(ip);
499
500 return error;
501
502err_inode:
503 xfs_defer_move(parent_tp, tp);
504 xfs_trans_cancel(tp);
505 if (ip) {
506 xfs_iunlock(ip, XFS_ILOCK_EXCL);
507 xfs_irele(ip);
508 }
509 return error;
510}
511