Merge tag 'wireless-drivers-for-davem-2018-04-26' of git://git.kernel.org/pub/scm...
[linux-2.6-block.git] / fs / xfs / xfs_bmap_item.c
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"
25 #include "xfs_bit.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #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"
34 #include "xfs_bmap.h"
35 #include "xfs_icache.h"
36 #include "xfs_trace.h"
37 #include "xfs_bmap_btree.h"
38 #include "xfs_trans_space.h"
39
40
41 kmem_zone_t     *xfs_bui_zone;
42 kmem_zone_t     *xfs_bud_zone;
43
44 static 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
49 void
50 xfs_bui_item_free(
51         struct xfs_bui_log_item *buip)
52 {
53         kmem_zone_free(xfs_bui_zone, buip);
54 }
55
56 /*
57  * Freeing the BUI requires that we remove it from the AIL if it has already
58  * been placed there. However, the BUI may not yet have been placed in the AIL
59  * when called by xfs_bui_release() from BUD processing due to the ordering of
60  * committed vs unpin operations in bulk insert operations. Hence the reference
61  * count to ensure only the last caller frees the BUI.
62  */
63 void
64 xfs_bui_release(
65         struct xfs_bui_log_item *buip)
66 {
67         ASSERT(atomic_read(&buip->bui_refcount) > 0);
68         if (atomic_dec_and_test(&buip->bui_refcount)) {
69                 xfs_trans_ail_remove(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
70                 xfs_bui_item_free(buip);
71         }
72 }
73
74
75 STATIC void
76 xfs_bui_item_size(
77         struct xfs_log_item     *lip,
78         int                     *nvecs,
79         int                     *nbytes)
80 {
81         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
82
83         *nvecs += 1;
84         *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
85 }
86
87 /*
88  * This is called to fill in the vector of log iovecs for the
89  * given bui log item. We use only 1 iovec, and we point that
90  * at the bui_log_format structure embedded in the bui item.
91  * It is at this point that we assert that all of the extent
92  * slots in the bui item have been filled.
93  */
94 STATIC void
95 xfs_bui_item_format(
96         struct xfs_log_item     *lip,
97         struct xfs_log_vec      *lv)
98 {
99         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
100         struct xfs_log_iovec    *vecp = NULL;
101
102         ASSERT(atomic_read(&buip->bui_next_extent) ==
103                         buip->bui_format.bui_nextents);
104
105         buip->bui_format.bui_type = XFS_LI_BUI;
106         buip->bui_format.bui_size = 1;
107
108         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
109                         xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
110 }
111
112 /*
113  * Pinning has no meaning for an bui item, so just return.
114  */
115 STATIC void
116 xfs_bui_item_pin(
117         struct xfs_log_item     *lip)
118 {
119 }
120
121 /*
122  * The unpin operation is the last place an BUI is manipulated in the log. It is
123  * either inserted in the AIL or aborted in the event of a log I/O error. In
124  * either case, the BUI transaction has been successfully committed to make it
125  * this far. Therefore, we expect whoever committed the BUI to either construct
126  * and commit the BUD or drop the BUD's reference in the event of error. Simply
127  * drop the log's BUI reference now that the log is done with it.
128  */
129 STATIC void
130 xfs_bui_item_unpin(
131         struct xfs_log_item     *lip,
132         int                     remove)
133 {
134         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
135
136         xfs_bui_release(buip);
137 }
138
139 /*
140  * BUI items have no locking or pushing.  However, since BUIs are pulled from
141  * the AIL when their corresponding BUDs are committed to disk, their situation
142  * is very similar to being pinned.  Return XFS_ITEM_PINNED so that the caller
143  * will eventually flush the log.  This should help in getting the BUI out of
144  * the AIL.
145  */
146 STATIC uint
147 xfs_bui_item_push(
148         struct xfs_log_item     *lip,
149         struct list_head        *buffer_list)
150 {
151         return XFS_ITEM_PINNED;
152 }
153
154 /*
155  * The BUI has been either committed or aborted if the transaction has been
156  * cancelled. If the transaction was cancelled, an BUD isn't going to be
157  * constructed and thus we free the BUI here directly.
158  */
159 STATIC void
160 xfs_bui_item_unlock(
161         struct xfs_log_item     *lip)
162 {
163         if (lip->li_flags & XFS_LI_ABORTED)
164                 xfs_bui_release(BUI_ITEM(lip));
165 }
166
167 /*
168  * The BUI is logged only once and cannot be moved in the log, so simply return
169  * the lsn at which it's been logged.
170  */
171 STATIC xfs_lsn_t
172 xfs_bui_item_committed(
173         struct xfs_log_item     *lip,
174         xfs_lsn_t               lsn)
175 {
176         return lsn;
177 }
178
179 /*
180  * The BUI dependency tracking op doesn't do squat.  It can't because
181  * it doesn't know where the free extent is coming from.  The dependency
182  * tracking has to be handled by the "enclosing" metadata object.  For
183  * example, for inodes, the inode is locked throughout the extent freeing
184  * so the dependency should be recorded there.
185  */
186 STATIC void
187 xfs_bui_item_committing(
188         struct xfs_log_item     *lip,
189         xfs_lsn_t               lsn)
190 {
191 }
192
193 /*
194  * This is the ops vector shared by all bui log items.
195  */
196 static const struct xfs_item_ops xfs_bui_item_ops = {
197         .iop_size       = xfs_bui_item_size,
198         .iop_format     = xfs_bui_item_format,
199         .iop_pin        = xfs_bui_item_pin,
200         .iop_unpin      = xfs_bui_item_unpin,
201         .iop_unlock     = xfs_bui_item_unlock,
202         .iop_committed  = xfs_bui_item_committed,
203         .iop_push       = xfs_bui_item_push,
204         .iop_committing = xfs_bui_item_committing,
205 };
206
207 /*
208  * Allocate and initialize an bui item with the given number of extents.
209  */
210 struct xfs_bui_log_item *
211 xfs_bui_init(
212         struct xfs_mount                *mp)
213
214 {
215         struct xfs_bui_log_item         *buip;
216
217         buip = kmem_zone_zalloc(xfs_bui_zone, KM_SLEEP);
218
219         xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
220         buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
221         buip->bui_format.bui_id = (uintptr_t)(void *)buip;
222         atomic_set(&buip->bui_next_extent, 0);
223         atomic_set(&buip->bui_refcount, 2);
224
225         return buip;
226 }
227
228 static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
229 {
230         return container_of(lip, struct xfs_bud_log_item, bud_item);
231 }
232
233 STATIC void
234 xfs_bud_item_size(
235         struct xfs_log_item     *lip,
236         int                     *nvecs,
237         int                     *nbytes)
238 {
239         *nvecs += 1;
240         *nbytes += sizeof(struct xfs_bud_log_format);
241 }
242
243 /*
244  * This is called to fill in the vector of log iovecs for the
245  * given bud log item. We use only 1 iovec, and we point that
246  * at the bud_log_format structure embedded in the bud item.
247  * It is at this point that we assert that all of the extent
248  * slots in the bud item have been filled.
249  */
250 STATIC void
251 xfs_bud_item_format(
252         struct xfs_log_item     *lip,
253         struct xfs_log_vec      *lv)
254 {
255         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
256         struct xfs_log_iovec    *vecp = NULL;
257
258         budp->bud_format.bud_type = XFS_LI_BUD;
259         budp->bud_format.bud_size = 1;
260
261         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
262                         sizeof(struct xfs_bud_log_format));
263 }
264
265 /*
266  * Pinning has no meaning for an bud item, so just return.
267  */
268 STATIC void
269 xfs_bud_item_pin(
270         struct xfs_log_item     *lip)
271 {
272 }
273
274 /*
275  * Since pinning has no meaning for an bud item, unpinning does
276  * not either.
277  */
278 STATIC void
279 xfs_bud_item_unpin(
280         struct xfs_log_item     *lip,
281         int                     remove)
282 {
283 }
284
285 /*
286  * There isn't much you can do to push on an bud item.  It is simply stuck
287  * waiting for the log to be flushed to disk.
288  */
289 STATIC uint
290 xfs_bud_item_push(
291         struct xfs_log_item     *lip,
292         struct list_head        *buffer_list)
293 {
294         return XFS_ITEM_PINNED;
295 }
296
297 /*
298  * The BUD is either committed or aborted if the transaction is cancelled. If
299  * the transaction is cancelled, drop our reference to the BUI and free the
300  * BUD.
301  */
302 STATIC void
303 xfs_bud_item_unlock(
304         struct xfs_log_item     *lip)
305 {
306         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
307
308         if (lip->li_flags & XFS_LI_ABORTED) {
309                 xfs_bui_release(budp->bud_buip);
310                 kmem_zone_free(xfs_bud_zone, budp);
311         }
312 }
313
314 /*
315  * When the bud item is committed to disk, all we need to do is delete our
316  * reference to our partner bui item and then free ourselves. Since we're
317  * freeing ourselves we must return -1 to keep the transaction code from
318  * further referencing this item.
319  */
320 STATIC xfs_lsn_t
321 xfs_bud_item_committed(
322         struct xfs_log_item     *lip,
323         xfs_lsn_t               lsn)
324 {
325         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
326
327         /*
328          * Drop the BUI reference regardless of whether the BUD has been
329          * aborted. Once the BUD transaction is constructed, it is the sole
330          * responsibility of the BUD to release the BUI (even if the BUI is
331          * aborted due to log I/O error).
332          */
333         xfs_bui_release(budp->bud_buip);
334         kmem_zone_free(xfs_bud_zone, budp);
335
336         return (xfs_lsn_t)-1;
337 }
338
339 /*
340  * The BUD dependency tracking op doesn't do squat.  It can't because
341  * it doesn't know where the free extent is coming from.  The dependency
342  * tracking has to be handled by the "enclosing" metadata object.  For
343  * example, for inodes, the inode is locked throughout the extent freeing
344  * so the dependency should be recorded there.
345  */
346 STATIC void
347 xfs_bud_item_committing(
348         struct xfs_log_item     *lip,
349         xfs_lsn_t               lsn)
350 {
351 }
352
353 /*
354  * This is the ops vector shared by all bud log items.
355  */
356 static const struct xfs_item_ops xfs_bud_item_ops = {
357         .iop_size       = xfs_bud_item_size,
358         .iop_format     = xfs_bud_item_format,
359         .iop_pin        = xfs_bud_item_pin,
360         .iop_unpin      = xfs_bud_item_unpin,
361         .iop_unlock     = xfs_bud_item_unlock,
362         .iop_committed  = xfs_bud_item_committed,
363         .iop_push       = xfs_bud_item_push,
364         .iop_committing = xfs_bud_item_committing,
365 };
366
367 /*
368  * Allocate and initialize an bud item with the given number of extents.
369  */
370 struct xfs_bud_log_item *
371 xfs_bud_init(
372         struct xfs_mount                *mp,
373         struct xfs_bui_log_item         *buip)
374
375 {
376         struct xfs_bud_log_item *budp;
377
378         budp = kmem_zone_zalloc(xfs_bud_zone, KM_SLEEP);
379         xfs_log_item_init(mp, &budp->bud_item, XFS_LI_BUD, &xfs_bud_item_ops);
380         budp->bud_buip = buip;
381         budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
382
383         return budp;
384 }
385
386 /*
387  * Process a bmap update intent item that was recovered from the log.
388  * We need to update some inode's bmbt.
389  */
390 int
391 xfs_bui_recover(
392         struct xfs_mount                *mp,
393         struct xfs_bui_log_item         *buip,
394         struct xfs_defer_ops            *dfops)
395 {
396         int                             error = 0;
397         unsigned int                    bui_type;
398         struct xfs_map_extent           *bmap;
399         xfs_fsblock_t                   startblock_fsb;
400         xfs_fsblock_t                   inode_fsb;
401         xfs_filblks_t                   count;
402         bool                            op_ok;
403         struct xfs_bud_log_item         *budp;
404         enum xfs_bmap_intent_type       type;
405         int                             whichfork;
406         xfs_exntst_t                    state;
407         struct xfs_trans                *tp;
408         struct xfs_inode                *ip = NULL;
409         struct xfs_bmbt_irec            irec;
410
411         ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
412
413         /* Only one mapping operation per BUI... */
414         if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
415                 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
416                 xfs_bui_release(buip);
417                 return -EIO;
418         }
419
420         /*
421          * First check the validity of the extent described by the
422          * BUI.  If anything is bad, then toss the BUI.
423          */
424         bmap = &buip->bui_format.bui_extents[0];
425         startblock_fsb = XFS_BB_TO_FSB(mp,
426                            XFS_FSB_TO_DADDR(mp, bmap->me_startblock));
427         inode_fsb = XFS_BB_TO_FSB(mp, XFS_FSB_TO_DADDR(mp,
428                         XFS_INO_TO_FSB(mp, bmap->me_owner)));
429         switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
430         case XFS_BMAP_MAP:
431         case XFS_BMAP_UNMAP:
432                 op_ok = true;
433                 break;
434         default:
435                 op_ok = false;
436                 break;
437         }
438         if (!op_ok || startblock_fsb == 0 ||
439             bmap->me_len == 0 ||
440             inode_fsb == 0 ||
441             startblock_fsb >= mp->m_sb.sb_dblocks ||
442             bmap->me_len >= mp->m_sb.sb_agblocks ||
443             inode_fsb >= mp->m_sb.sb_dblocks ||
444             (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) {
445                 /*
446                  * This will pull the BUI from the AIL and
447                  * free the memory associated with it.
448                  */
449                 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
450                 xfs_bui_release(buip);
451                 return -EIO;
452         }
453
454         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
455                         XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
456         if (error)
457                 return error;
458         budp = xfs_trans_get_bud(tp, buip);
459
460         /* Grab the inode. */
461         error = xfs_iget(mp, tp, bmap->me_owner, 0, XFS_ILOCK_EXCL, &ip);
462         if (error)
463                 goto err_inode;
464
465         if (VFS_I(ip)->i_nlink == 0)
466                 xfs_iflags_set(ip, XFS_IRECOVERY);
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_inode;
482         }
483         xfs_trans_ijoin(tp, ip, 0);
484
485         count = bmap->me_len;
486         error = xfs_trans_log_finish_bmap_update(tp, budp, dfops, type,
487                         ip, whichfork, bmap->me_startoff,
488                         bmap->me_startblock, &count, state);
489         if (error)
490                 goto err_inode;
491
492         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_inode;
501         }
502
503         set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
504         error = xfs_trans_commit(tp);
505         xfs_iunlock(ip, XFS_ILOCK_EXCL);
506         IRELE(ip);
507
508         return error;
509
510 err_inode:
511         xfs_trans_cancel(tp);
512         if (ip) {
513                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
514                 IRELE(ip);
515         }
516         return error;
517 }