]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_rmap_item.c
xfs: in btree_lshift, only allocate temporary cursor when needed
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / xfs_rmap_item.c
CommitLineData
5880f2d7
DW
1/*
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"
9e88b5d8 25#include "xfs_bit.h"
5880f2d7 26#include "xfs_mount.h"
9c194644 27#include "xfs_defer.h"
5880f2d7
DW
28#include "xfs_trans.h"
29#include "xfs_trans_priv.h"
30#include "xfs_buf_item.h"
31#include "xfs_rmap_item.h"
32#include "xfs_log.h"
9c194644 33#include "xfs_rmap.h"
5880f2d7
DW
34
35
36kmem_zone_t *xfs_rui_zone;
37kmem_zone_t *xfs_rud_zone;
38
39static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
40{
41 return container_of(lip, struct xfs_rui_log_item, rui_item);
42}
43
44void
45xfs_rui_item_free(
46 struct xfs_rui_log_item *ruip)
47{
48 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
49 kmem_free(ruip);
50 else
51 kmem_zone_free(xfs_rui_zone, ruip);
52}
53
54/*
55 * This returns the number of iovecs needed to log the given rui item.
56 * We only need 1 iovec for an rui item. It just logs the rui_log_format
57 * structure.
58 */
59static inline int
60xfs_rui_item_sizeof(
61 struct xfs_rui_log_item *ruip)
62{
63 return sizeof(struct xfs_rui_log_format) +
64 (ruip->rui_format.rui_nextents - 1) *
65 sizeof(struct xfs_map_extent);
66}
67
68STATIC void
69xfs_rui_item_size(
70 struct xfs_log_item *lip,
71 int *nvecs,
72 int *nbytes)
73{
74 *nvecs += 1;
75 *nbytes += xfs_rui_item_sizeof(RUI_ITEM(lip));
76}
77
78/*
79 * This is called to fill in the vector of log iovecs for the
80 * given rui log item. We use only 1 iovec, and we point that
81 * at the rui_log_format structure embedded in the rui item.
82 * It is at this point that we assert that all of the extent
83 * slots in the rui item have been filled.
84 */
85STATIC void
86xfs_rui_item_format(
87 struct xfs_log_item *lip,
88 struct xfs_log_vec *lv)
89{
90 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
91 struct xfs_log_iovec *vecp = NULL;
92
93 ASSERT(atomic_read(&ruip->rui_next_extent) ==
94 ruip->rui_format.rui_nextents);
95
96 ruip->rui_format.rui_type = XFS_LI_RUI;
97 ruip->rui_format.rui_size = 1;
98
99 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
100 xfs_rui_item_sizeof(ruip));
101}
102
103/*
104 * Pinning has no meaning for an rui item, so just return.
105 */
106STATIC void
107xfs_rui_item_pin(
108 struct xfs_log_item *lip)
109{
110}
111
112/*
113 * The unpin operation is the last place an RUI is manipulated in the log. It is
114 * either inserted in the AIL or aborted in the event of a log I/O error. In
115 * either case, the RUI transaction has been successfully committed to make it
116 * this far. Therefore, we expect whoever committed the RUI to either construct
117 * and commit the RUD or drop the RUD's reference in the event of error. Simply
118 * drop the log's RUI reference now that the log is done with it.
119 */
120STATIC void
121xfs_rui_item_unpin(
122 struct xfs_log_item *lip,
123 int remove)
124{
125 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
126
127 xfs_rui_release(ruip);
128}
129
130/*
131 * RUI items have no locking or pushing. However, since RUIs are pulled from
132 * the AIL when their corresponding RUDs are committed to disk, their situation
133 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
134 * will eventually flush the log. This should help in getting the RUI out of
135 * the AIL.
136 */
137STATIC uint
138xfs_rui_item_push(
139 struct xfs_log_item *lip,
140 struct list_head *buffer_list)
141{
142 return XFS_ITEM_PINNED;
143}
144
145/*
146 * The RUI has been either committed or aborted if the transaction has been
147 * cancelled. If the transaction was cancelled, an RUD isn't going to be
148 * constructed and thus we free the RUI here directly.
149 */
150STATIC void
151xfs_rui_item_unlock(
152 struct xfs_log_item *lip)
153{
154 if (lip->li_flags & XFS_LI_ABORTED)
155 xfs_rui_item_free(RUI_ITEM(lip));
156}
157
158/*
159 * The RUI is logged only once and cannot be moved in the log, so simply return
160 * the lsn at which it's been logged.
161 */
162STATIC xfs_lsn_t
163xfs_rui_item_committed(
164 struct xfs_log_item *lip,
165 xfs_lsn_t lsn)
166{
167 return lsn;
168}
169
170/*
171 * The RUI dependency tracking op doesn't do squat. It can't because
172 * it doesn't know where the free extent is coming from. The dependency
173 * tracking has to be handled by the "enclosing" metadata object. For
174 * example, for inodes, the inode is locked throughout the extent freeing
175 * so the dependency should be recorded there.
176 */
177STATIC void
178xfs_rui_item_committing(
179 struct xfs_log_item *lip,
180 xfs_lsn_t lsn)
181{
182}
183
184/*
185 * This is the ops vector shared by all rui log items.
186 */
187static const struct xfs_item_ops xfs_rui_item_ops = {
188 .iop_size = xfs_rui_item_size,
189 .iop_format = xfs_rui_item_format,
190 .iop_pin = xfs_rui_item_pin,
191 .iop_unpin = xfs_rui_item_unpin,
192 .iop_unlock = xfs_rui_item_unlock,
193 .iop_committed = xfs_rui_item_committed,
194 .iop_push = xfs_rui_item_push,
195 .iop_committing = xfs_rui_item_committing,
196};
197
198/*
199 * Allocate and initialize an rui item with the given number of extents.
200 */
201struct xfs_rui_log_item *
202xfs_rui_init(
203 struct xfs_mount *mp,
204 uint nextents)
205
206{
207 struct xfs_rui_log_item *ruip;
208 uint size;
209
210 ASSERT(nextents > 0);
211 if (nextents > XFS_RUI_MAX_FAST_EXTENTS) {
212 size = (uint)(sizeof(struct xfs_rui_log_item) +
213 ((nextents - 1) * sizeof(struct xfs_map_extent)));
214 ruip = kmem_zalloc(size, KM_SLEEP);
215 } else {
216 ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP);
217 }
218
219 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
220 ruip->rui_format.rui_nextents = nextents;
221 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
222 atomic_set(&ruip->rui_next_extent, 0);
223 atomic_set(&ruip->rui_refcount, 2);
224
225 return ruip;
226}
227
228/*
229 * Copy an RUI format buffer from the given buf, and into the destination
230 * RUI format structure. The RUI/RUD items were designed not to need any
231 * special alignment handling.
232 */
233int
234xfs_rui_copy_format(
235 struct xfs_log_iovec *buf,
236 struct xfs_rui_log_format *dst_rui_fmt)
237{
238 struct xfs_rui_log_format *src_rui_fmt;
239 uint len;
240
241 src_rui_fmt = buf->i_addr;
242 len = sizeof(struct xfs_rui_log_format) +
243 (src_rui_fmt->rui_nextents - 1) *
244 sizeof(struct xfs_map_extent);
245
246 if (buf->i_len != len)
247 return -EFSCORRUPTED;
248
249 memcpy((char *)dst_rui_fmt, (char *)src_rui_fmt, len);
250 return 0;
251}
252
253/*
254 * Freeing the RUI requires that we remove it from the AIL if it has already
255 * been placed there. However, the RUI may not yet have been placed in the AIL
256 * when called by xfs_rui_release() from RUD processing due to the ordering of
257 * committed vs unpin operations in bulk insert operations. Hence the reference
258 * count to ensure only the last caller frees the RUI.
259 */
260void
261xfs_rui_release(
262 struct xfs_rui_log_item *ruip)
263{
264 if (atomic_dec_and_test(&ruip->rui_refcount)) {
265 xfs_trans_ail_remove(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
266 xfs_rui_item_free(ruip);
267 }
268}
269
270static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
271{
272 return container_of(lip, struct xfs_rud_log_item, rud_item);
273}
274
275STATIC void
276xfs_rud_item_free(struct xfs_rud_log_item *rudp)
277{
278 if (rudp->rud_format.rud_nextents > XFS_RUD_MAX_FAST_EXTENTS)
279 kmem_free(rudp);
280 else
281 kmem_zone_free(xfs_rud_zone, rudp);
282}
283
284/*
285 * This returns the number of iovecs needed to log the given rud item.
286 * We only need 1 iovec for an rud item. It just logs the rud_log_format
287 * structure.
288 */
289static inline int
290xfs_rud_item_sizeof(
291 struct xfs_rud_log_item *rudp)
292{
293 return sizeof(struct xfs_rud_log_format) +
294 (rudp->rud_format.rud_nextents - 1) *
295 sizeof(struct xfs_map_extent);
296}
297
298STATIC void
299xfs_rud_item_size(
300 struct xfs_log_item *lip,
301 int *nvecs,
302 int *nbytes)
303{
304 *nvecs += 1;
305 *nbytes += xfs_rud_item_sizeof(RUD_ITEM(lip));
306}
307
308/*
309 * This is called to fill in the vector of log iovecs for the
310 * given rud log item. We use only 1 iovec, and we point that
311 * at the rud_log_format structure embedded in the rud item.
312 * It is at this point that we assert that all of the extent
313 * slots in the rud item have been filled.
314 */
315STATIC void
316xfs_rud_item_format(
317 struct xfs_log_item *lip,
318 struct xfs_log_vec *lv)
319{
320 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
321 struct xfs_log_iovec *vecp = NULL;
322
323 ASSERT(rudp->rud_next_extent == rudp->rud_format.rud_nextents);
324
325 rudp->rud_format.rud_type = XFS_LI_RUD;
326 rudp->rud_format.rud_size = 1;
327
328 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
329 xfs_rud_item_sizeof(rudp));
330}
331
332/*
333 * Pinning has no meaning for an rud item, so just return.
334 */
335STATIC void
336xfs_rud_item_pin(
337 struct xfs_log_item *lip)
338{
339}
340
341/*
342 * Since pinning has no meaning for an rud item, unpinning does
343 * not either.
344 */
345STATIC void
346xfs_rud_item_unpin(
347 struct xfs_log_item *lip,
348 int remove)
349{
350}
351
352/*
353 * There isn't much you can do to push on an rud item. It is simply stuck
354 * waiting for the log to be flushed to disk.
355 */
356STATIC uint
357xfs_rud_item_push(
358 struct xfs_log_item *lip,
359 struct list_head *buffer_list)
360{
361 return XFS_ITEM_PINNED;
362}
363
364/*
365 * The RUD is either committed or aborted if the transaction is cancelled. If
366 * the transaction is cancelled, drop our reference to the RUI and free the
367 * RUD.
368 */
369STATIC void
370xfs_rud_item_unlock(
371 struct xfs_log_item *lip)
372{
373 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
374
375 if (lip->li_flags & XFS_LI_ABORTED) {
376 xfs_rui_release(rudp->rud_ruip);
377 xfs_rud_item_free(rudp);
378 }
379}
380
381/*
382 * When the rud item is committed to disk, all we need to do is delete our
383 * reference to our partner rui item and then free ourselves. Since we're
384 * freeing ourselves we must return -1 to keep the transaction code from
385 * further referencing this item.
386 */
387STATIC xfs_lsn_t
388xfs_rud_item_committed(
389 struct xfs_log_item *lip,
390 xfs_lsn_t lsn)
391{
392 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
393
394 /*
395 * Drop the RUI reference regardless of whether the RUD has been
396 * aborted. Once the RUD transaction is constructed, it is the sole
397 * responsibility of the RUD to release the RUI (even if the RUI is
398 * aborted due to log I/O error).
399 */
400 xfs_rui_release(rudp->rud_ruip);
401 xfs_rud_item_free(rudp);
402
403 return (xfs_lsn_t)-1;
404}
405
406/*
407 * The RUD dependency tracking op doesn't do squat. It can't because
408 * it doesn't know where the free extent is coming from. The dependency
409 * tracking has to be handled by the "enclosing" metadata object. For
410 * example, for inodes, the inode is locked throughout the extent freeing
411 * so the dependency should be recorded there.
412 */
413STATIC void
414xfs_rud_item_committing(
415 struct xfs_log_item *lip,
416 xfs_lsn_t lsn)
417{
418}
419
420/*
421 * This is the ops vector shared by all rud log items.
422 */
423static const struct xfs_item_ops xfs_rud_item_ops = {
424 .iop_size = xfs_rud_item_size,
425 .iop_format = xfs_rud_item_format,
426 .iop_pin = xfs_rud_item_pin,
427 .iop_unpin = xfs_rud_item_unpin,
428 .iop_unlock = xfs_rud_item_unlock,
429 .iop_committed = xfs_rud_item_committed,
430 .iop_push = xfs_rud_item_push,
431 .iop_committing = xfs_rud_item_committing,
432};
433
434/*
435 * Allocate and initialize an rud item with the given number of extents.
436 */
437struct xfs_rud_log_item *
438xfs_rud_init(
439 struct xfs_mount *mp,
440 struct xfs_rui_log_item *ruip,
441 uint nextents)
442
443{
444 struct xfs_rud_log_item *rudp;
445 uint size;
446
447 ASSERT(nextents > 0);
448 if (nextents > XFS_RUD_MAX_FAST_EXTENTS) {
449 size = (uint)(sizeof(struct xfs_rud_log_item) +
450 ((nextents - 1) * sizeof(struct xfs_map_extent)));
451 rudp = kmem_zalloc(size, KM_SLEEP);
452 } else {
453 rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP);
454 }
455
456 xfs_log_item_init(mp, &rudp->rud_item, XFS_LI_RUD, &xfs_rud_item_ops);
457 rudp->rud_ruip = ruip;
458 rudp->rud_format.rud_nextents = nextents;
459 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
460
461 return rudp;
462}
9e88b5d8
DW
463
464/*
465 * Process an rmap update intent item that was recovered from the log.
466 * We need to update the rmapbt.
467 */
468int
469xfs_rui_recover(
470 struct xfs_mount *mp,
471 struct xfs_rui_log_item *ruip)
472{
473 int i;
474 int error = 0;
475 struct xfs_map_extent *rmap;
476 xfs_fsblock_t startblock_fsb;
477 bool op_ok;
9c194644
DW
478 struct xfs_rud_log_item *rudp;
479 enum xfs_rmap_intent_type type;
480 int whichfork;
481 xfs_exntst_t state;
482 struct xfs_trans *tp;
483 struct xfs_btree_cur *rcur = NULL;
9e88b5d8
DW
484
485 ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
486
487 /*
488 * First check the validity of the extents described by the
489 * RUI. If any are bad, then assume that all are bad and
490 * just toss the RUI.
491 */
492 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
493 rmap = &(ruip->rui_format.rui_extents[i]);
494 startblock_fsb = XFS_BB_TO_FSB(mp,
495 XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
496 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
497 case XFS_RMAP_EXTENT_MAP:
498 case XFS_RMAP_EXTENT_UNMAP:
499 case XFS_RMAP_EXTENT_CONVERT:
500 case XFS_RMAP_EXTENT_ALLOC:
501 case XFS_RMAP_EXTENT_FREE:
502 op_ok = true;
503 break;
504 default:
505 op_ok = false;
506 break;
507 }
508 if (!op_ok || (startblock_fsb == 0) ||
509 (rmap->me_len == 0) ||
510 (startblock_fsb >= mp->m_sb.sb_dblocks) ||
511 (rmap->me_len >= mp->m_sb.sb_agblocks) ||
512 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
513 /*
514 * This will pull the RUI from the AIL and
515 * free the memory associated with it.
516 */
517 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
518 xfs_rui_release(ruip);
519 return -EIO;
520 }
521 }
522
9c194644
DW
523 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
524 if (error)
525 return error;
526 rudp = xfs_trans_get_rud(tp, ruip, ruip->rui_format.rui_nextents);
527
528 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
529 rmap = &(ruip->rui_format.rui_extents[i]);
530 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
531 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
532 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
533 XFS_ATTR_FORK : XFS_DATA_FORK;
534 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
535 case XFS_RMAP_EXTENT_MAP:
536 type = XFS_RMAP_MAP;
537 break;
538 case XFS_RMAP_EXTENT_UNMAP:
539 type = XFS_RMAP_UNMAP;
540 break;
541 case XFS_RMAP_EXTENT_CONVERT:
542 type = XFS_RMAP_CONVERT;
543 break;
544 case XFS_RMAP_EXTENT_ALLOC:
545 type = XFS_RMAP_ALLOC;
546 break;
547 case XFS_RMAP_EXTENT_FREE:
548 type = XFS_RMAP_FREE;
549 break;
550 default:
551 error = -EFSCORRUPTED;
552 goto abort_error;
553 }
554 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
555 rmap->me_owner, whichfork,
556 rmap->me_startoff, rmap->me_startblock,
557 rmap->me_len, state, &rcur);
558 if (error)
559 goto abort_error;
560
561 }
562
563 xfs_rmap_finish_one_cleanup(tp, rcur, error);
9e88b5d8 564 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
9c194644
DW
565 error = xfs_trans_commit(tp);
566 return error;
567
568abort_error:
569 xfs_rmap_finish_one_cleanup(tp, rcur, error);
570 xfs_trans_cancel(tp);
9e88b5d8
DW
571 return error;
572}