blob: 5a54dcd7e7b14f13242e4e0691c40d5ebf4a235e [file] [log] [blame]
Darrick J. Wong6413a012016-10-03 09:11:25 -07001/*
2 * Copyright (C) 2016 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Darrick J. Wong77d61fe2016-10-03 09:11:26 -070025#include "xfs_bit.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070026#include "xfs_mount.h"
Darrick J. Wong77d61fe2016-10-03 09:11:26 -070027#include "xfs_defer.h"
28#include "xfs_inode.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070029#include "xfs_trans.h"
30#include "xfs_trans_priv.h"
31#include "xfs_buf_item.h"
32#include "xfs_bmap_item.h"
33#include "xfs_log.h"
Darrick J. Wong77d61fe2016-10-03 09:11:26 -070034#include "xfs_bmap.h"
35#include "xfs_icache.h"
36#include "xfs_trace.h"
Darrick J. Wongd457f822017-04-12 12:26:07 -070037#include "xfs_bmap_btree.h"
38#include "xfs_trans_space.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070039
40
41kmem_zone_t *xfs_bui_zone;
42kmem_zone_t *xfs_bud_zone;
43
44static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
45{
46 return container_of(lip, struct xfs_bui_log_item, bui_item);
47}
48
49void
50xfs_bui_item_free(
51 struct xfs_bui_log_item *buip)
52{
53 kmem_zone_free(xfs_bui_zone, buip);
54}
55
56STATIC void
57xfs_bui_item_size(
58 struct xfs_log_item *lip,
59 int *nvecs,
60 int *nbytes)
61{
62 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
63
64 *nvecs += 1;
65 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
66}
67
68/*
69 * This is called to fill in the vector of log iovecs for the
70 * given bui log item. We use only 1 iovec, and we point that
71 * at the bui_log_format structure embedded in the bui item.
72 * It is at this point that we assert that all of the extent
73 * slots in the bui item have been filled.
74 */
75STATIC void
76xfs_bui_item_format(
77 struct xfs_log_item *lip,
78 struct xfs_log_vec *lv)
79{
80 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
81 struct xfs_log_iovec *vecp = NULL;
82
83 ASSERT(atomic_read(&buip->bui_next_extent) ==
84 buip->bui_format.bui_nextents);
85
86 buip->bui_format.bui_type = XFS_LI_BUI;
87 buip->bui_format.bui_size = 1;
88
89 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
90 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
91}
92
93/*
94 * Pinning has no meaning for an bui item, so just return.
95 */
96STATIC void
97xfs_bui_item_pin(
98 struct xfs_log_item *lip)
99{
100}
101
102/*
103 * The unpin operation is the last place an BUI is manipulated in the log. It is
104 * either inserted in the AIL or aborted in the event of a log I/O error. In
105 * either case, the BUI transaction has been successfully committed to make it
106 * this far. Therefore, we expect whoever committed the BUI to either construct
107 * and commit the BUD or drop the BUD's reference in the event of error. Simply
108 * drop the log's BUI reference now that the log is done with it.
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 * BUI items have no locking or pushing. However, since BUIs are pulled from
122 * the AIL when their corresponding BUDs are committed to disk, their situation
123 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
124 * will eventually flush the log. This should help in getting the BUI out of
125 * the AIL.
126 */
127STATIC uint
128xfs_bui_item_push(
129 struct xfs_log_item *lip,
130 struct list_head *buffer_list)
131{
132 return XFS_ITEM_PINNED;
133}
134
135/*
136 * The BUI has been either committed or aborted if the transaction has been
137 * cancelled. If the transaction was cancelled, an BUD isn't going to be
138 * constructed and thus we free the BUI here directly.
139 */
140STATIC void
141xfs_bui_item_unlock(
142 struct xfs_log_item *lip)
143{
144 if (lip->li_flags & XFS_LI_ABORTED)
145 xfs_bui_item_free(BUI_ITEM(lip));
146}
147
148/*
149 * The BUI is logged only once and cannot be moved in the log, so simply return
150 * the lsn at which it's been logged.
151 */
152STATIC xfs_lsn_t
153xfs_bui_item_committed(
154 struct xfs_log_item *lip,
155 xfs_lsn_t lsn)
156{
157 return lsn;
158}
159
160/*
161 * The BUI dependency tracking op doesn't do squat. It can't because
162 * it doesn't know where the free extent is coming from. The dependency
163 * tracking has to be handled by the "enclosing" metadata object. For
164 * example, for inodes, the inode is locked throughout the extent freeing
165 * so the dependency should be recorded there.
166 */
167STATIC void
168xfs_bui_item_committing(
169 struct xfs_log_item *lip,
170 xfs_lsn_t lsn)
171{
172}
173
174/*
175 * This is the ops vector shared by all bui log items.
176 */
177static const struct xfs_item_ops xfs_bui_item_ops = {
178 .iop_size = xfs_bui_item_size,
179 .iop_format = xfs_bui_item_format,
180 .iop_pin = xfs_bui_item_pin,
181 .iop_unpin = xfs_bui_item_unpin,
182 .iop_unlock = xfs_bui_item_unlock,
183 .iop_committed = xfs_bui_item_committed,
184 .iop_push = xfs_bui_item_push,
185 .iop_committing = xfs_bui_item_committing,
186};
187
188/*
189 * Allocate and initialize an bui item with the given number of extents.
190 */
191struct xfs_bui_log_item *
192xfs_bui_init(
193 struct xfs_mount *mp)
194
195{
196 struct xfs_bui_log_item *buip;
197
198 buip = kmem_zone_zalloc(xfs_bui_zone, KM_SLEEP);
199
200 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
201 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
202 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
203 atomic_set(&buip->bui_next_extent, 0);
204 atomic_set(&buip->bui_refcount, 2);
205
206 return buip;
207}
208
209/*
210 * Freeing the BUI requires that we remove it from the AIL if it has already
211 * been placed there. However, the BUI may not yet have been placed in the AIL
212 * when called by xfs_bui_release() from BUD processing due to the ordering of
213 * committed vs unpin operations in bulk insert operations. Hence the reference
214 * count to ensure only the last caller frees the BUI.
215 */
216void
217xfs_bui_release(
218 struct xfs_bui_log_item *buip)
219{
220 if (atomic_dec_and_test(&buip->bui_refcount)) {
221 xfs_trans_ail_remove(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
222 xfs_bui_item_free(buip);
223 }
224}
225
226static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
227{
228 return container_of(lip, struct xfs_bud_log_item, bud_item);
229}
230
231STATIC void
232xfs_bud_item_size(
233 struct xfs_log_item *lip,
234 int *nvecs,
235 int *nbytes)
236{
237 *nvecs += 1;
238 *nbytes += sizeof(struct xfs_bud_log_format);
239}
240
241/*
242 * This is called to fill in the vector of log iovecs for the
243 * given bud log item. We use only 1 iovec, and we point that
244 * at the bud_log_format structure embedded in the bud item.
245 * It is at this point that we assert that all of the extent
246 * slots in the bud item have been filled.
247 */
248STATIC void
249xfs_bud_item_format(
250 struct xfs_log_item *lip,
251 struct xfs_log_vec *lv)
252{
253 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
254 struct xfs_log_iovec *vecp = NULL;
255
256 budp->bud_format.bud_type = XFS_LI_BUD;
257 budp->bud_format.bud_size = 1;
258
259 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
260 sizeof(struct xfs_bud_log_format));
261}
262
263/*
264 * Pinning has no meaning for an bud item, so just return.
265 */
266STATIC void
267xfs_bud_item_pin(
268 struct xfs_log_item *lip)
269{
270}
271
272/*
273 * Since pinning has no meaning for an bud item, unpinning does
274 * not either.
275 */
276STATIC void
277xfs_bud_item_unpin(
278 struct xfs_log_item *lip,
279 int remove)
280{
281}
282
283/*
284 * There isn't much you can do to push on an bud item. It is simply stuck
285 * waiting for the log to be flushed to disk.
286 */
287STATIC uint
288xfs_bud_item_push(
289 struct xfs_log_item *lip,
290 struct list_head *buffer_list)
291{
292 return XFS_ITEM_PINNED;
293}
294
295/*
296 * The BUD is either committed or aborted if the transaction is cancelled. If
297 * the transaction is cancelled, drop our reference to the BUI and free the
298 * BUD.
299 */
300STATIC void
301xfs_bud_item_unlock(
302 struct xfs_log_item *lip)
303{
304 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
305
306 if (lip->li_flags & XFS_LI_ABORTED) {
307 xfs_bui_release(budp->bud_buip);
308 kmem_zone_free(xfs_bud_zone, budp);
309 }
310}
311
312/*
313 * When the bud item is committed to disk, all we need to do is delete our
314 * reference to our partner bui item and then free ourselves. Since we're
315 * freeing ourselves we must return -1 to keep the transaction code from
316 * further referencing this item.
317 */
318STATIC xfs_lsn_t
319xfs_bud_item_committed(
320 struct xfs_log_item *lip,
321 xfs_lsn_t lsn)
322{
323 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
324
325 /*
326 * Drop the BUI reference regardless of whether the BUD has been
327 * aborted. Once the BUD transaction is constructed, it is the sole
328 * responsibility of the BUD to release the BUI (even if the BUI is
329 * aborted due to log I/O error).
330 */
331 xfs_bui_release(budp->bud_buip);
332 kmem_zone_free(xfs_bud_zone, budp);
333
334 return (xfs_lsn_t)-1;
335}
336
337/*
338 * The BUD dependency tracking op doesn't do squat. It can't because
339 * it doesn't know where the free extent is coming from. The dependency
340 * tracking has to be handled by the "enclosing" metadata object. For
341 * example, for inodes, the inode is locked throughout the extent freeing
342 * so the dependency should be recorded there.
343 */
344STATIC void
345xfs_bud_item_committing(
346 struct xfs_log_item *lip,
347 xfs_lsn_t lsn)
348{
349}
350
351/*
352 * This is the ops vector shared by all bud log items.
353 */
354static const struct xfs_item_ops xfs_bud_item_ops = {
355 .iop_size = xfs_bud_item_size,
356 .iop_format = xfs_bud_item_format,
357 .iop_pin = xfs_bud_item_pin,
358 .iop_unpin = xfs_bud_item_unpin,
359 .iop_unlock = xfs_bud_item_unlock,
360 .iop_committed = xfs_bud_item_committed,
361 .iop_push = xfs_bud_item_push,
362 .iop_committing = xfs_bud_item_committing,
363};
364
365/*
366 * Allocate and initialize an bud item with the given number of extents.
367 */
368struct xfs_bud_log_item *
369xfs_bud_init(
370 struct xfs_mount *mp,
371 struct xfs_bui_log_item *buip)
372
373{
374 struct xfs_bud_log_item *budp;
375
376 budp = kmem_zone_zalloc(xfs_bud_zone, KM_SLEEP);
377 xfs_log_item_init(mp, &budp->bud_item, XFS_LI_BUD, &xfs_bud_item_ops);
378 budp->bud_buip = buip;
379 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
380
381 return budp;
382}
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700383
384/*
385 * Process a bmap update intent item that was recovered from the log.
386 * We need to update some inode's bmbt.
387 */
388int
389xfs_bui_recover(
390 struct xfs_mount *mp,
391 struct xfs_bui_log_item *buip)
392{
393 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700394 unsigned int bui_type;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700395 struct xfs_map_extent *bmap;
396 xfs_fsblock_t startblock_fsb;
397 xfs_fsblock_t inode_fsb;
Darrick J. Wongce83e492017-06-14 21:25:57 -0700398 xfs_filblks_t count;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700399 bool op_ok;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700400 struct xfs_bud_log_item *budp;
401 enum xfs_bmap_intent_type type;
402 int whichfork;
403 xfs_exntst_t state;
404 struct xfs_trans *tp;
405 struct xfs_inode *ip = NULL;
406 struct xfs_defer_ops dfops;
Darrick J. Wongce83e492017-06-14 21:25:57 -0700407 struct xfs_bmbt_irec irec;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700408 xfs_fsblock_t firstfsb;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700409
410 ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
411
412 /* Only one mapping operation per BUI... */
413 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
414 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
415 xfs_bui_release(buip);
416 return -EIO;
417 }
418
419 /*
420 * First check the validity of the extent described by the
421 * BUI. If anything is bad, then toss the BUI.
422 */
423 bmap = &buip->bui_format.bui_extents[0];
424 startblock_fsb = XFS_BB_TO_FSB(mp,
425 XFS_FSB_TO_DADDR(mp, bmap->me_startblock));
426 inode_fsb = XFS_BB_TO_FSB(mp, XFS_FSB_TO_DADDR(mp,
427 XFS_INO_TO_FSB(mp, bmap->me_owner)));
428 switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
429 case XFS_BMAP_MAP:
430 case XFS_BMAP_UNMAP:
431 op_ok = true;
432 break;
433 default:
434 op_ok = false;
435 break;
436 }
437 if (!op_ok || startblock_fsb == 0 ||
438 bmap->me_len == 0 ||
439 inode_fsb == 0 ||
440 startblock_fsb >= mp->m_sb.sb_dblocks ||
441 bmap->me_len >= mp->m_sb.sb_agblocks ||
442 inode_fsb >= mp->m_sb.sb_dblocks ||
443 (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) {
444 /*
445 * This will pull the BUI from the AIL and
446 * free the memory associated with it.
447 */
448 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
449 xfs_bui_release(buip);
450 return -EIO;
451 }
452
Darrick J. Wongd457f822017-04-12 12:26:07 -0700453 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
454 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700455 if (error)
456 return error;
457 budp = xfs_trans_get_bud(tp, buip);
458
459 /* Grab the inode. */
460 error = xfs_iget(mp, tp, bmap->me_owner, 0, XFS_ILOCK_EXCL, &ip);
461 if (error)
462 goto err_inode;
463
Darrick J. Wong17c12bc2016-10-03 09:11:29 -0700464 if (VFS_I(ip)->i_nlink == 0)
465 xfs_iflags_set(ip, XFS_IRECOVERY);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700466 xfs_defer_init(&dfops, &firstfsb);
467
468 /* Process deferred bmap item. */
469 state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
470 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
471 whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
472 XFS_ATTR_FORK : XFS_DATA_FORK;
473 bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
474 switch (bui_type) {
475 case XFS_BMAP_MAP:
476 case XFS_BMAP_UNMAP:
477 type = bui_type;
478 break;
479 default:
480 error = -EFSCORRUPTED;
481 goto err_dfops;
482 }
483 xfs_trans_ijoin(tp, ip, 0);
484
Darrick J. Wongce83e492017-06-14 21:25:57 -0700485 count = bmap->me_len;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700486 error = xfs_trans_log_finish_bmap_update(tp, budp, &dfops, type,
487 ip, whichfork, bmap->me_startoff,
Darrick J. Wongce83e492017-06-14 21:25:57 -0700488 bmap->me_startblock, &count, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700489 if (error)
490 goto err_dfops;
491
Darrick J. Wongce83e492017-06-14 21:25:57 -0700492 if (count > 0) {
493 ASSERT(type == XFS_BMAP_UNMAP);
494 irec.br_startblock = bmap->me_startblock;
495 irec.br_blockcount = count;
496 irec.br_startoff = bmap->me_startoff;
497 irec.br_state = state;
498 error = xfs_bmap_unmap_extent(tp->t_mountp, &dfops, ip, &irec);
499 if (error)
500 goto err_dfops;
501 }
502
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700503 /* Finish transaction, free inodes. */
504 error = xfs_defer_finish(&tp, &dfops, NULL);
505 if (error)
506 goto err_dfops;
507
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700508 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -0700509 error = xfs_trans_commit(tp);
510 xfs_iunlock(ip, XFS_ILOCK_EXCL);
511 IRELE(ip);
512
513 return error;
514
515err_dfops:
516 xfs_defer_cancel(&dfops);
517err_inode:
518 xfs_trans_cancel(tp);
519 if (ip) {
520 xfs_iunlock(ip, XFS_ILOCK_EXCL);
521 IRELE(ip);
522 }
Darrick J. Wong77d61fe2016-10-03 09:11:26 -0700523 return error;
524}