83f2032641cff56e91f84fdb764aafeaca1eab58
[platform/kernel/linux-rpi.git] / fs / xfs / xfs_trans.c
1 /*
2  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3  * Copyright (C) 2010 Red Hat, Inc.
4  * All Rights Reserved.
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 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it would be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write the Free Software Foundation,
17  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_shared.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_extent_busy.h"
28 #include "xfs_quota.h"
29 #include "xfs_trans.h"
30 #include "xfs_trans_priv.h"
31 #include "xfs_log.h"
32 #include "xfs_trace.h"
33 #include "xfs_error.h"
34 #include "xfs_defer.h"
35
36 kmem_zone_t     *xfs_trans_zone;
37 kmem_zone_t     *xfs_log_item_desc_zone;
38
39 #if defined(CONFIG_TRACEPOINTS)
40 static void
41 xfs_trans_trace_reservations(
42         struct xfs_mount        *mp)
43 {
44         struct xfs_trans_res    resv;
45         struct xfs_trans_res    *res;
46         struct xfs_trans_res    *end_res;
47         int                     i;
48
49         res = (struct xfs_trans_res *)M_RES(mp);
50         end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
51         for (i = 0; res < end_res; i++, res++)
52                 trace_xfs_trans_resv_calc(mp, i, res);
53         xfs_log_get_max_trans_res(mp, &resv);
54         trace_xfs_trans_resv_calc(mp, -1, &resv);
55 }
56 #else
57 # define xfs_trans_trace_reservations(mp)
58 #endif
59
60 /*
61  * Initialize the precomputed transaction reservation values
62  * in the mount structure.
63  */
64 void
65 xfs_trans_init(
66         struct xfs_mount        *mp)
67 {
68         xfs_trans_resv_calc(mp, M_RES(mp));
69         xfs_trans_trace_reservations(mp);
70 }
71
72 /*
73  * Free the transaction structure.  If there is more clean up
74  * to do when the structure is freed, add it here.
75  */
76 STATIC void
77 xfs_trans_free(
78         struct xfs_trans        *tp)
79 {
80         xfs_extent_busy_sort(&tp->t_busy);
81         xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
82
83         atomic_dec(&tp->t_mountp->m_active_trans);
84         if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
85                 sb_end_intwrite(tp->t_mountp->m_super);
86         xfs_trans_free_dqinfo(tp);
87         kmem_zone_free(xfs_trans_zone, tp);
88 }
89
90 /*
91  * This is called to create a new transaction which will share the
92  * permanent log reservation of the given transaction.  The remaining
93  * unused block and rt extent reservations are also inherited.  This
94  * implies that the original transaction is no longer allowed to allocate
95  * blocks.  Locks and log items, however, are no inherited.  They must
96  * be added to the new transaction explicitly.
97  */
98 STATIC struct xfs_trans *
99 xfs_trans_dup(
100         struct xfs_trans        *tp)
101 {
102         struct xfs_trans        *ntp;
103
104         ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
105
106         /*
107          * Initialize the new transaction structure.
108          */
109         ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
110         ntp->t_mountp = tp->t_mountp;
111         INIT_LIST_HEAD(&ntp->t_items);
112         INIT_LIST_HEAD(&ntp->t_busy);
113
114         ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
115         ASSERT(tp->t_ticket != NULL);
116
117         ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
118                        (tp->t_flags & XFS_TRANS_RESERVE) |
119                        (tp->t_flags & XFS_TRANS_NO_WRITECOUNT);
120         /* We gave our writer reference to the new transaction */
121         tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
122         ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
123
124         ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
125         ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
126         tp->t_blk_res = tp->t_blk_res_used;
127
128         ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
129         tp->t_rtx_res = tp->t_rtx_res_used;
130         ntp->t_pflags = tp->t_pflags;
131         ntp->t_agfl_dfops = tp->t_agfl_dfops;
132
133         xfs_trans_dup_dqinfo(tp, ntp);
134
135         atomic_inc(&tp->t_mountp->m_active_trans);
136         return ntp;
137 }
138
139 /*
140  * This is called to reserve free disk blocks and log space for the
141  * given transaction.  This must be done before allocating any resources
142  * within the transaction.
143  *
144  * This will return ENOSPC if there are not enough blocks available.
145  * It will sleep waiting for available log space.
146  * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
147  * is used by long running transactions.  If any one of the reservations
148  * fails then they will all be backed out.
149  *
150  * This does not do quota reservations. That typically is done by the
151  * caller afterwards.
152  */
153 static int
154 xfs_trans_reserve(
155         struct xfs_trans        *tp,
156         struct xfs_trans_res    *resp,
157         uint                    blocks,
158         uint                    rtextents)
159 {
160         int             error = 0;
161         bool            rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
162
163         /* Mark this thread as being in a transaction */
164         current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
165
166         /*
167          * Attempt to reserve the needed disk blocks by decrementing
168          * the number needed from the number available.  This will
169          * fail if the count would go below zero.
170          */
171         if (blocks > 0) {
172                 error = xfs_mod_fdblocks(tp->t_mountp, -((int64_t)blocks), rsvd);
173                 if (error != 0) {
174                         current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
175                         return -ENOSPC;
176                 }
177                 tp->t_blk_res += blocks;
178         }
179
180         /*
181          * Reserve the log space needed for this transaction.
182          */
183         if (resp->tr_logres > 0) {
184                 bool    permanent = false;
185
186                 ASSERT(tp->t_log_res == 0 ||
187                        tp->t_log_res == resp->tr_logres);
188                 ASSERT(tp->t_log_count == 0 ||
189                        tp->t_log_count == resp->tr_logcount);
190
191                 if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
192                         tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
193                         permanent = true;
194                 } else {
195                         ASSERT(tp->t_ticket == NULL);
196                         ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
197                 }
198
199                 if (tp->t_ticket != NULL) {
200                         ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
201                         error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
202                 } else {
203                         error = xfs_log_reserve(tp->t_mountp,
204                                                 resp->tr_logres,
205                                                 resp->tr_logcount,
206                                                 &tp->t_ticket, XFS_TRANSACTION,
207                                                 permanent);
208                 }
209
210                 if (error)
211                         goto undo_blocks;
212
213                 tp->t_log_res = resp->tr_logres;
214                 tp->t_log_count = resp->tr_logcount;
215         }
216
217         /*
218          * Attempt to reserve the needed realtime extents by decrementing
219          * the number needed from the number available.  This will
220          * fail if the count would go below zero.
221          */
222         if (rtextents > 0) {
223                 error = xfs_mod_frextents(tp->t_mountp, -((int64_t)rtextents));
224                 if (error) {
225                         error = -ENOSPC;
226                         goto undo_log;
227                 }
228                 tp->t_rtx_res += rtextents;
229         }
230
231         return 0;
232
233         /*
234          * Error cases jump to one of these labels to undo any
235          * reservations which have already been performed.
236          */
237 undo_log:
238         if (resp->tr_logres > 0) {
239                 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, false);
240                 tp->t_ticket = NULL;
241                 tp->t_log_res = 0;
242                 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
243         }
244
245 undo_blocks:
246         if (blocks > 0) {
247                 xfs_mod_fdblocks(tp->t_mountp, (int64_t)blocks, rsvd);
248                 tp->t_blk_res = 0;
249         }
250
251         current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
252
253         return error;
254 }
255
256 int
257 xfs_trans_alloc(
258         struct xfs_mount        *mp,
259         struct xfs_trans_res    *resp,
260         uint                    blocks,
261         uint                    rtextents,
262         uint                    flags,
263         struct xfs_trans        **tpp)
264 {
265         struct xfs_trans        *tp;
266         int                     error;
267
268         if (!(flags & XFS_TRANS_NO_WRITECOUNT))
269                 sb_start_intwrite(mp->m_super);
270
271         WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
272         atomic_inc(&mp->m_active_trans);
273
274         tp = kmem_zone_zalloc(xfs_trans_zone,
275                 (flags & XFS_TRANS_NOFS) ? KM_NOFS : KM_SLEEP);
276         tp->t_magic = XFS_TRANS_HEADER_MAGIC;
277         tp->t_flags = flags;
278         tp->t_mountp = mp;
279         INIT_LIST_HEAD(&tp->t_items);
280         INIT_LIST_HEAD(&tp->t_busy);
281
282         error = xfs_trans_reserve(tp, resp, blocks, rtextents);
283         if (error) {
284                 xfs_trans_cancel(tp);
285                 return error;
286         }
287
288         *tpp = tp;
289         return 0;
290 }
291
292 /*
293  * Create an empty transaction with no reservation.  This is a defensive
294  * mechanism for routines that query metadata without actually modifying
295  * them -- if the metadata being queried is somehow cross-linked (think a
296  * btree block pointer that points higher in the tree), we risk deadlock.
297  * However, blocks grabbed as part of a transaction can be re-grabbed.
298  * The verifiers will notice the corrupt block and the operation will fail
299  * back to userspace without deadlocking.
300  *
301  * Note the zero-length reservation; this transaction MUST be cancelled
302  * without any dirty data.
303  */
304 int
305 xfs_trans_alloc_empty(
306         struct xfs_mount                *mp,
307         struct xfs_trans                **tpp)
308 {
309         struct xfs_trans_res            resv = {0};
310
311         return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
312 }
313
314 /*
315  * Record the indicated change to the given field for application
316  * to the file system's superblock when the transaction commits.
317  * For now, just store the change in the transaction structure.
318  *
319  * Mark the transaction structure to indicate that the superblock
320  * needs to be updated before committing.
321  *
322  * Because we may not be keeping track of allocated/free inodes and
323  * used filesystem blocks in the superblock, we do not mark the
324  * superblock dirty in this transaction if we modify these fields.
325  * We still need to update the transaction deltas so that they get
326  * applied to the incore superblock, but we don't want them to
327  * cause the superblock to get locked and logged if these are the
328  * only fields in the superblock that the transaction modifies.
329  */
330 void
331 xfs_trans_mod_sb(
332         xfs_trans_t     *tp,
333         uint            field,
334         int64_t         delta)
335 {
336         uint32_t        flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
337         xfs_mount_t     *mp = tp->t_mountp;
338
339         switch (field) {
340         case XFS_TRANS_SB_ICOUNT:
341                 tp->t_icount_delta += delta;
342                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
343                         flags &= ~XFS_TRANS_SB_DIRTY;
344                 break;
345         case XFS_TRANS_SB_IFREE:
346                 tp->t_ifree_delta += delta;
347                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
348                         flags &= ~XFS_TRANS_SB_DIRTY;
349                 break;
350         case XFS_TRANS_SB_FDBLOCKS:
351                 /*
352                  * Track the number of blocks allocated in the transaction.
353                  * Make sure it does not exceed the number reserved. If so,
354                  * shutdown as this can lead to accounting inconsistency.
355                  */
356                 if (delta < 0) {
357                         tp->t_blk_res_used += (uint)-delta;
358                         if (tp->t_blk_res_used > tp->t_blk_res)
359                                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
360                 }
361                 tp->t_fdblocks_delta += delta;
362                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
363                         flags &= ~XFS_TRANS_SB_DIRTY;
364                 break;
365         case XFS_TRANS_SB_RES_FDBLOCKS:
366                 /*
367                  * The allocation has already been applied to the
368                  * in-core superblock's counter.  This should only
369                  * be applied to the on-disk superblock.
370                  */
371                 tp->t_res_fdblocks_delta += delta;
372                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
373                         flags &= ~XFS_TRANS_SB_DIRTY;
374                 break;
375         case XFS_TRANS_SB_FREXTENTS:
376                 /*
377                  * Track the number of blocks allocated in the
378                  * transaction.  Make sure it does not exceed the
379                  * number reserved.
380                  */
381                 if (delta < 0) {
382                         tp->t_rtx_res_used += (uint)-delta;
383                         ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
384                 }
385                 tp->t_frextents_delta += delta;
386                 break;
387         case XFS_TRANS_SB_RES_FREXTENTS:
388                 /*
389                  * The allocation has already been applied to the
390                  * in-core superblock's counter.  This should only
391                  * be applied to the on-disk superblock.
392                  */
393                 ASSERT(delta < 0);
394                 tp->t_res_frextents_delta += delta;
395                 break;
396         case XFS_TRANS_SB_DBLOCKS:
397                 ASSERT(delta > 0);
398                 tp->t_dblocks_delta += delta;
399                 break;
400         case XFS_TRANS_SB_AGCOUNT:
401                 ASSERT(delta > 0);
402                 tp->t_agcount_delta += delta;
403                 break;
404         case XFS_TRANS_SB_IMAXPCT:
405                 tp->t_imaxpct_delta += delta;
406                 break;
407         case XFS_TRANS_SB_REXTSIZE:
408                 tp->t_rextsize_delta += delta;
409                 break;
410         case XFS_TRANS_SB_RBMBLOCKS:
411                 tp->t_rbmblocks_delta += delta;
412                 break;
413         case XFS_TRANS_SB_RBLOCKS:
414                 tp->t_rblocks_delta += delta;
415                 break;
416         case XFS_TRANS_SB_REXTENTS:
417                 tp->t_rextents_delta += delta;
418                 break;
419         case XFS_TRANS_SB_REXTSLOG:
420                 tp->t_rextslog_delta += delta;
421                 break;
422         default:
423                 ASSERT(0);
424                 return;
425         }
426
427         tp->t_flags |= flags;
428 }
429
430 /*
431  * xfs_trans_apply_sb_deltas() is called from the commit code
432  * to bring the superblock buffer into the current transaction
433  * and modify it as requested by earlier calls to xfs_trans_mod_sb().
434  *
435  * For now we just look at each field allowed to change and change
436  * it if necessary.
437  */
438 STATIC void
439 xfs_trans_apply_sb_deltas(
440         xfs_trans_t     *tp)
441 {
442         xfs_dsb_t       *sbp;
443         xfs_buf_t       *bp;
444         int             whole = 0;
445
446         bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
447         sbp = XFS_BUF_TO_SBP(bp);
448
449         /*
450          * Check that superblock mods match the mods made to AGF counters.
451          */
452         ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
453                (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
454                 tp->t_ag_btree_delta));
455
456         /*
457          * Only update the superblock counters if we are logging them
458          */
459         if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
460                 if (tp->t_icount_delta)
461                         be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
462                 if (tp->t_ifree_delta)
463                         be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
464                 if (tp->t_fdblocks_delta)
465                         be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
466                 if (tp->t_res_fdblocks_delta)
467                         be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
468         }
469
470         if (tp->t_frextents_delta)
471                 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
472         if (tp->t_res_frextents_delta)
473                 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
474
475         if (tp->t_dblocks_delta) {
476                 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
477                 whole = 1;
478         }
479         if (tp->t_agcount_delta) {
480                 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
481                 whole = 1;
482         }
483         if (tp->t_imaxpct_delta) {
484                 sbp->sb_imax_pct += tp->t_imaxpct_delta;
485                 whole = 1;
486         }
487         if (tp->t_rextsize_delta) {
488                 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
489                 whole = 1;
490         }
491         if (tp->t_rbmblocks_delta) {
492                 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
493                 whole = 1;
494         }
495         if (tp->t_rblocks_delta) {
496                 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
497                 whole = 1;
498         }
499         if (tp->t_rextents_delta) {
500                 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
501                 whole = 1;
502         }
503         if (tp->t_rextslog_delta) {
504                 sbp->sb_rextslog += tp->t_rextslog_delta;
505                 whole = 1;
506         }
507
508         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
509         if (whole)
510                 /*
511                  * Log the whole thing, the fields are noncontiguous.
512                  */
513                 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
514         else
515                 /*
516                  * Since all the modifiable fields are contiguous, we
517                  * can get away with this.
518                  */
519                 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
520                                   offsetof(xfs_dsb_t, sb_frextents) +
521                                   sizeof(sbp->sb_frextents) - 1);
522 }
523
524 STATIC int
525 xfs_sb_mod8(
526         uint8_t                 *field,
527         int8_t                  delta)
528 {
529         int8_t                  counter = *field;
530
531         counter += delta;
532         if (counter < 0) {
533                 ASSERT(0);
534                 return -EINVAL;
535         }
536         *field = counter;
537         return 0;
538 }
539
540 STATIC int
541 xfs_sb_mod32(
542         uint32_t                *field,
543         int32_t                 delta)
544 {
545         int32_t                 counter = *field;
546
547         counter += delta;
548         if (counter < 0) {
549                 ASSERT(0);
550                 return -EINVAL;
551         }
552         *field = counter;
553         return 0;
554 }
555
556 STATIC int
557 xfs_sb_mod64(
558         uint64_t                *field,
559         int64_t                 delta)
560 {
561         int64_t                 counter = *field;
562
563         counter += delta;
564         if (counter < 0) {
565                 ASSERT(0);
566                 return -EINVAL;
567         }
568         *field = counter;
569         return 0;
570 }
571
572 /*
573  * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
574  * and apply superblock counter changes to the in-core superblock.  The
575  * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
576  * applied to the in-core superblock.  The idea is that that has already been
577  * done.
578  *
579  * If we are not logging superblock counters, then the inode allocated/free and
580  * used block counts are not updated in the on disk superblock. In this case,
581  * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
582  * still need to update the incore superblock with the changes.
583  */
584 void
585 xfs_trans_unreserve_and_mod_sb(
586         struct xfs_trans        *tp)
587 {
588         struct xfs_mount        *mp = tp->t_mountp;
589         bool                    rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
590         int64_t                 blkdelta = 0;
591         int64_t                 rtxdelta = 0;
592         int64_t                 idelta = 0;
593         int64_t                 ifreedelta = 0;
594         int                     error;
595
596         /* calculate deltas */
597         if (tp->t_blk_res > 0)
598                 blkdelta = tp->t_blk_res;
599         if ((tp->t_fdblocks_delta != 0) &&
600             (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
601              (tp->t_flags & XFS_TRANS_SB_DIRTY)))
602                 blkdelta += tp->t_fdblocks_delta;
603
604         if (tp->t_rtx_res > 0)
605                 rtxdelta = tp->t_rtx_res;
606         if ((tp->t_frextents_delta != 0) &&
607             (tp->t_flags & XFS_TRANS_SB_DIRTY))
608                 rtxdelta += tp->t_frextents_delta;
609
610         if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
611              (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
612                 idelta = tp->t_icount_delta;
613                 ifreedelta = tp->t_ifree_delta;
614         }
615
616         /* apply the per-cpu counters */
617         if (blkdelta) {
618                 error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
619                 if (error)
620                         goto out;
621         }
622
623         if (idelta) {
624                 error = xfs_mod_icount(mp, idelta);
625                 if (error)
626                         goto out_undo_fdblocks;
627         }
628
629         if (ifreedelta) {
630                 error = xfs_mod_ifree(mp, ifreedelta);
631                 if (error)
632                         goto out_undo_icount;
633         }
634
635         if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY))
636                 return;
637
638         /* apply remaining deltas */
639         spin_lock(&mp->m_sb_lock);
640         if (rtxdelta) {
641                 error = xfs_sb_mod64(&mp->m_sb.sb_frextents, rtxdelta);
642                 if (error)
643                         goto out_undo_ifree;
644         }
645
646         if (tp->t_dblocks_delta != 0) {
647                 error = xfs_sb_mod64(&mp->m_sb.sb_dblocks, tp->t_dblocks_delta);
648                 if (error)
649                         goto out_undo_frextents;
650         }
651         if (tp->t_agcount_delta != 0) {
652                 error = xfs_sb_mod32(&mp->m_sb.sb_agcount, tp->t_agcount_delta);
653                 if (error)
654                         goto out_undo_dblocks;
655         }
656         if (tp->t_imaxpct_delta != 0) {
657                 error = xfs_sb_mod8(&mp->m_sb.sb_imax_pct, tp->t_imaxpct_delta);
658                 if (error)
659                         goto out_undo_agcount;
660         }
661         if (tp->t_rextsize_delta != 0) {
662                 error = xfs_sb_mod32(&mp->m_sb.sb_rextsize,
663                                      tp->t_rextsize_delta);
664                 if (error)
665                         goto out_undo_imaxpct;
666         }
667         if (tp->t_rbmblocks_delta != 0) {
668                 error = xfs_sb_mod32(&mp->m_sb.sb_rbmblocks,
669                                      tp->t_rbmblocks_delta);
670                 if (error)
671                         goto out_undo_rextsize;
672         }
673         if (tp->t_rblocks_delta != 0) {
674                 error = xfs_sb_mod64(&mp->m_sb.sb_rblocks, tp->t_rblocks_delta);
675                 if (error)
676                         goto out_undo_rbmblocks;
677         }
678         if (tp->t_rextents_delta != 0) {
679                 error = xfs_sb_mod64(&mp->m_sb.sb_rextents,
680                                      tp->t_rextents_delta);
681                 if (error)
682                         goto out_undo_rblocks;
683         }
684         if (tp->t_rextslog_delta != 0) {
685                 error = xfs_sb_mod8(&mp->m_sb.sb_rextslog,
686                                      tp->t_rextslog_delta);
687                 if (error)
688                         goto out_undo_rextents;
689         }
690         spin_unlock(&mp->m_sb_lock);
691         return;
692
693 out_undo_rextents:
694         if (tp->t_rextents_delta)
695                 xfs_sb_mod64(&mp->m_sb.sb_rextents, -tp->t_rextents_delta);
696 out_undo_rblocks:
697         if (tp->t_rblocks_delta)
698                 xfs_sb_mod64(&mp->m_sb.sb_rblocks, -tp->t_rblocks_delta);
699 out_undo_rbmblocks:
700         if (tp->t_rbmblocks_delta)
701                 xfs_sb_mod32(&mp->m_sb.sb_rbmblocks, -tp->t_rbmblocks_delta);
702 out_undo_rextsize:
703         if (tp->t_rextsize_delta)
704                 xfs_sb_mod32(&mp->m_sb.sb_rextsize, -tp->t_rextsize_delta);
705 out_undo_imaxpct:
706         if (tp->t_rextsize_delta)
707                 xfs_sb_mod8(&mp->m_sb.sb_imax_pct, -tp->t_imaxpct_delta);
708 out_undo_agcount:
709         if (tp->t_agcount_delta)
710                 xfs_sb_mod32(&mp->m_sb.sb_agcount, -tp->t_agcount_delta);
711 out_undo_dblocks:
712         if (tp->t_dblocks_delta)
713                 xfs_sb_mod64(&mp->m_sb.sb_dblocks, -tp->t_dblocks_delta);
714 out_undo_frextents:
715         if (rtxdelta)
716                 xfs_sb_mod64(&mp->m_sb.sb_frextents, -rtxdelta);
717 out_undo_ifree:
718         spin_unlock(&mp->m_sb_lock);
719         if (ifreedelta)
720                 xfs_mod_ifree(mp, -ifreedelta);
721 out_undo_icount:
722         if (idelta)
723                 xfs_mod_icount(mp, -idelta);
724 out_undo_fdblocks:
725         if (blkdelta)
726                 xfs_mod_fdblocks(mp, -blkdelta, rsvd);
727 out:
728         ASSERT(error == 0);
729         return;
730 }
731
732 /*
733  * Add the given log item to the transaction's list of log items.
734  *
735  * The log item will now point to its new descriptor with its li_desc field.
736  */
737 void
738 xfs_trans_add_item(
739         struct xfs_trans        *tp,
740         struct xfs_log_item     *lip)
741 {
742         struct xfs_log_item_desc *lidp;
743
744         ASSERT(lip->li_mountp == tp->t_mountp);
745         ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
746
747         lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
748
749         lidp->lid_item = lip;
750         lidp->lid_flags = 0;
751         list_add_tail(&lidp->lid_trans, &tp->t_items);
752
753         lip->li_desc = lidp;
754 }
755
756 STATIC void
757 xfs_trans_free_item_desc(
758         struct xfs_log_item_desc *lidp)
759 {
760         list_del_init(&lidp->lid_trans);
761         kmem_zone_free(xfs_log_item_desc_zone, lidp);
762 }
763
764 /*
765  * Unlink and free the given descriptor.
766  */
767 void
768 xfs_trans_del_item(
769         struct xfs_log_item     *lip)
770 {
771         xfs_trans_free_item_desc(lip->li_desc);
772         lip->li_desc = NULL;
773 }
774
775 /*
776  * Unlock all of the items of a transaction and free all the descriptors
777  * of that transaction.
778  */
779 void
780 xfs_trans_free_items(
781         struct xfs_trans        *tp,
782         xfs_lsn_t               commit_lsn,
783         bool                    abort)
784 {
785         struct xfs_log_item_desc *lidp, *next;
786
787         list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
788                 struct xfs_log_item     *lip = lidp->lid_item;
789
790                 lip->li_desc = NULL;
791
792                 if (commit_lsn != NULLCOMMITLSN)
793                         lip->li_ops->iop_committing(lip, commit_lsn);
794                 if (abort)
795                         set_bit(XFS_LI_ABORTED, &lip->li_flags);
796                 lip->li_ops->iop_unlock(lip);
797
798                 xfs_trans_free_item_desc(lidp);
799         }
800 }
801
802 static inline void
803 xfs_log_item_batch_insert(
804         struct xfs_ail          *ailp,
805         struct xfs_ail_cursor   *cur,
806         struct xfs_log_item     **log_items,
807         int                     nr_items,
808         xfs_lsn_t               commit_lsn)
809 {
810         int     i;
811
812         spin_lock(&ailp->ail_lock);
813         /* xfs_trans_ail_update_bulk drops ailp->ail_lock */
814         xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
815
816         for (i = 0; i < nr_items; i++) {
817                 struct xfs_log_item *lip = log_items[i];
818
819                 lip->li_ops->iop_unpin(lip, 0);
820         }
821 }
822
823 /*
824  * Bulk operation version of xfs_trans_committed that takes a log vector of
825  * items to insert into the AIL. This uses bulk AIL insertion techniques to
826  * minimise lock traffic.
827  *
828  * If we are called with the aborted flag set, it is because a log write during
829  * a CIL checkpoint commit has failed. In this case, all the items in the
830  * checkpoint have already gone through iop_commited and iop_unlock, which
831  * means that checkpoint commit abort handling is treated exactly the same
832  * as an iclog write error even though we haven't started any IO yet. Hence in
833  * this case all we need to do is iop_committed processing, followed by an
834  * iop_unpin(aborted) call.
835  *
836  * The AIL cursor is used to optimise the insert process. If commit_lsn is not
837  * at the end of the AIL, the insert cursor avoids the need to walk
838  * the AIL to find the insertion point on every xfs_log_item_batch_insert()
839  * call. This saves a lot of needless list walking and is a net win, even
840  * though it slightly increases that amount of AIL lock traffic to set it up
841  * and tear it down.
842  */
843 void
844 xfs_trans_committed_bulk(
845         struct xfs_ail          *ailp,
846         struct xfs_log_vec      *log_vector,
847         xfs_lsn_t               commit_lsn,
848         int                     aborted)
849 {
850 #define LOG_ITEM_BATCH_SIZE     32
851         struct xfs_log_item     *log_items[LOG_ITEM_BATCH_SIZE];
852         struct xfs_log_vec      *lv;
853         struct xfs_ail_cursor   cur;
854         int                     i = 0;
855
856         spin_lock(&ailp->ail_lock);
857         xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
858         spin_unlock(&ailp->ail_lock);
859
860         /* unpin all the log items */
861         for (lv = log_vector; lv; lv = lv->lv_next ) {
862                 struct xfs_log_item     *lip = lv->lv_item;
863                 xfs_lsn_t               item_lsn;
864
865                 if (aborted)
866                         set_bit(XFS_LI_ABORTED, &lip->li_flags);
867                 item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
868
869                 /* item_lsn of -1 means the item needs no further processing */
870                 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
871                         continue;
872
873                 /*
874                  * if we are aborting the operation, no point in inserting the
875                  * object into the AIL as we are in a shutdown situation.
876                  */
877                 if (aborted) {
878                         ASSERT(XFS_FORCED_SHUTDOWN(ailp->ail_mount));
879                         lip->li_ops->iop_unpin(lip, 1);
880                         continue;
881                 }
882
883                 if (item_lsn != commit_lsn) {
884
885                         /*
886                          * Not a bulk update option due to unusual item_lsn.
887                          * Push into AIL immediately, rechecking the lsn once
888                          * we have the ail lock. Then unpin the item. This does
889                          * not affect the AIL cursor the bulk insert path is
890                          * using.
891                          */
892                         spin_lock(&ailp->ail_lock);
893                         if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
894                                 xfs_trans_ail_update(ailp, lip, item_lsn);
895                         else
896                                 spin_unlock(&ailp->ail_lock);
897                         lip->li_ops->iop_unpin(lip, 0);
898                         continue;
899                 }
900
901                 /* Item is a candidate for bulk AIL insert.  */
902                 log_items[i++] = lv->lv_item;
903                 if (i >= LOG_ITEM_BATCH_SIZE) {
904                         xfs_log_item_batch_insert(ailp, &cur, log_items,
905                                         LOG_ITEM_BATCH_SIZE, commit_lsn);
906                         i = 0;
907                 }
908         }
909
910         /* make sure we insert the remainder! */
911         if (i)
912                 xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
913
914         spin_lock(&ailp->ail_lock);
915         xfs_trans_ail_cursor_done(&cur);
916         spin_unlock(&ailp->ail_lock);
917 }
918
919 /*
920  * Commit the given transaction to the log.
921  *
922  * XFS disk error handling mechanism is not based on a typical
923  * transaction abort mechanism. Logically after the filesystem
924  * gets marked 'SHUTDOWN', we can't let any new transactions
925  * be durable - ie. committed to disk - because some metadata might
926  * be inconsistent. In such cases, this returns an error, and the
927  * caller may assume that all locked objects joined to the transaction
928  * have already been unlocked as if the commit had succeeded.
929  * Do not reference the transaction structure after this call.
930  */
931 static int
932 __xfs_trans_commit(
933         struct xfs_trans        *tp,
934         bool                    regrant)
935 {
936         struct xfs_mount        *mp = tp->t_mountp;
937         xfs_lsn_t               commit_lsn = -1;
938         int                     error = 0;
939         int                     sync = tp->t_flags & XFS_TRANS_SYNC;
940
941         ASSERT(!tp->t_agfl_dfops ||
942                !xfs_defer_has_unfinished_work(tp->t_agfl_dfops) || regrant);
943
944         /*
945          * If there is nothing to be logged by the transaction,
946          * then unlock all of the items associated with the
947          * transaction and free the transaction structure.
948          * Also make sure to return any reserved blocks to
949          * the free pool.
950          */
951         if (!(tp->t_flags & XFS_TRANS_DIRTY))
952                 goto out_unreserve;
953
954         if (XFS_FORCED_SHUTDOWN(mp)) {
955                 error = -EIO;
956                 goto out_unreserve;
957         }
958
959         ASSERT(tp->t_ticket != NULL);
960
961         /*
962          * If we need to update the superblock, then do it now.
963          */
964         if (tp->t_flags & XFS_TRANS_SB_DIRTY)
965                 xfs_trans_apply_sb_deltas(tp);
966         xfs_trans_apply_dquot_deltas(tp);
967
968         xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
969
970         current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
971         xfs_trans_free(tp);
972
973         /*
974          * If the transaction needs to be synchronous, then force the
975          * log out now and wait for it.
976          */
977         if (sync) {
978                 error = xfs_log_force_lsn(mp, commit_lsn, XFS_LOG_SYNC, NULL);
979                 XFS_STATS_INC(mp, xs_trans_sync);
980         } else {
981                 XFS_STATS_INC(mp, xs_trans_async);
982         }
983
984         return error;
985
986 out_unreserve:
987         xfs_trans_unreserve_and_mod_sb(tp);
988
989         /*
990          * It is indeed possible for the transaction to be not dirty but
991          * the dqinfo portion to be.  All that means is that we have some
992          * (non-persistent) quota reservations that need to be unreserved.
993          */
994         xfs_trans_unreserve_and_mod_dquots(tp);
995         if (tp->t_ticket) {
996                 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, regrant);
997                 if (commit_lsn == -1 && !error)
998                         error = -EIO;
999         }
1000         current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
1001         xfs_trans_free_items(tp, NULLCOMMITLSN, !!error);
1002         xfs_trans_free(tp);
1003
1004         XFS_STATS_INC(mp, xs_trans_empty);
1005         return error;
1006 }
1007
1008 int
1009 xfs_trans_commit(
1010         struct xfs_trans        *tp)
1011 {
1012         return __xfs_trans_commit(tp, false);
1013 }
1014
1015 /*
1016  * Unlock all of the transaction's items and free the transaction.
1017  * The transaction must not have modified any of its items, because
1018  * there is no way to restore them to their previous state.
1019  *
1020  * If the transaction has made a log reservation, make sure to release
1021  * it as well.
1022  */
1023 void
1024 xfs_trans_cancel(
1025         struct xfs_trans        *tp)
1026 {
1027         struct xfs_mount        *mp = tp->t_mountp;
1028         bool                    dirty = (tp->t_flags & XFS_TRANS_DIRTY);
1029
1030         /*
1031          * See if the caller is relying on us to shut down the
1032          * filesystem.  This happens in paths where we detect
1033          * corruption and decide to give up.
1034          */
1035         if (dirty && !XFS_FORCED_SHUTDOWN(mp)) {
1036                 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1037                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1038         }
1039 #ifdef DEBUG
1040         if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) {
1041                 struct xfs_log_item_desc *lidp;
1042
1043                 list_for_each_entry(lidp, &tp->t_items, lid_trans)
1044                         ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
1045         }
1046 #endif
1047         xfs_trans_unreserve_and_mod_sb(tp);
1048         xfs_trans_unreserve_and_mod_dquots(tp);
1049
1050         if (tp->t_ticket)
1051                 xfs_log_done(mp, tp->t_ticket, NULL, false);
1052
1053         /* mark this thread as no longer being in a transaction */
1054         current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
1055
1056         xfs_trans_free_items(tp, NULLCOMMITLSN, dirty);
1057         xfs_trans_free(tp);
1058 }
1059
1060 /*
1061  * Roll from one trans in the sequence of PERMANENT transactions to
1062  * the next: permanent transactions are only flushed out when
1063  * committed with xfs_trans_commit(), but we still want as soon
1064  * as possible to let chunks of it go to the log. So we commit the
1065  * chunk we've been working on and get a new transaction to continue.
1066  */
1067 int
1068 xfs_trans_roll(
1069         struct xfs_trans        **tpp)
1070 {
1071         struct xfs_trans        *trans = *tpp;
1072         struct xfs_trans_res    tres;
1073         int                     error;
1074
1075         /*
1076          * Copy the critical parameters from one trans to the next.
1077          */
1078         tres.tr_logres = trans->t_log_res;
1079         tres.tr_logcount = trans->t_log_count;
1080
1081         *tpp = xfs_trans_dup(trans);
1082
1083         /*
1084          * Commit the current transaction.
1085          * If this commit failed, then it'd just unlock those items that
1086          * are not marked ihold. That also means that a filesystem shutdown
1087          * is in progress. The caller takes the responsibility to cancel
1088          * the duplicate transaction that gets returned.
1089          */
1090         error = __xfs_trans_commit(trans, true);
1091         if (error)
1092                 return error;
1093
1094         /*
1095          * Reserve space in the log for the next transaction.
1096          * This also pushes items in the "AIL", the list of logged items,
1097          * out to disk if they are taking up space at the tail of the log
1098          * that we want to use.  This requires that either nothing be locked
1099          * across this call, or that anything that is locked be logged in
1100          * the prior and the next transactions.
1101          */
1102         tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1103         return xfs_trans_reserve(*tpp, &tres, 0, 0);
1104 }