]> rtime.felk.cvut.cz Git - linux-imx.git/blob - fs/xfs/xfs_trans.c
xfs: calculate XFS_TRANS_QM_QUOTAOFF_END space log reservation at mount time
[linux-imx.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_types.h"
22 #include "xfs_log.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_mount.h"
27 #include "xfs_error.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_btree.h"
35 #include "xfs_ialloc.h"
36 #include "xfs_alloc.h"
37 #include "xfs_extent_busy.h"
38 #include "xfs_bmap.h"
39 #include "xfs_quota.h"
40 #include "xfs_qm.h"
41 #include "xfs_trans_priv.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_inode_item.h"
44 #include "xfs_log_priv.h"
45 #include "xfs_buf_item.h"
46 #include "xfs_trace.h"
47
48 kmem_zone_t     *xfs_trans_zone;
49 kmem_zone_t     *xfs_log_item_desc_zone;
50
51 /*
52  * A buffer has a format structure overhead in the log in addition
53  * to the data, so we need to take this into account when reserving
54  * space in a transaction for a buffer.  Round the space required up
55  * to a multiple of 128 bytes so that we don't change the historical
56  * reservation that has been used for this overhead.
57  */
58 STATIC uint
59 xfs_buf_log_overhead(void)
60 {
61         return round_up(sizeof(struct xlog_op_header) +
62                         sizeof(struct xfs_buf_log_format), 128);
63 }
64
65 /*
66  * Calculate out transaction log reservation per item in bytes.
67  *
68  * The nbufs argument is used to indicate the number of items that
69  * will be changed in a transaction.  size is used to tell how many
70  * bytes should be reserved per item.
71  */
72 STATIC uint
73 xfs_calc_buf_res(
74         uint            nbufs,
75         uint            size)
76 {
77         return nbufs * (size + xfs_buf_log_overhead());
78 }
79
80 /*
81  * Various log reservation values.
82  *
83  * These are based on the size of the file system block because that is what
84  * most transactions manipulate.  Each adds in an additional 128 bytes per
85  * item logged to try to account for the overhead of the transaction mechanism.
86  *
87  * Note:  Most of the reservations underestimate the number of allocation
88  * groups into which they could free extents in the xfs_bmap_finish() call.
89  * This is because the number in the worst case is quite high and quite
90  * unusual.  In order to fix this we need to change xfs_bmap_finish() to free
91  * extents in only a single AG at a time.  This will require changes to the
92  * EFI code as well, however, so that the EFI for the extents not freed is
93  * logged again in each transaction.  See SGI PV #261917.
94  *
95  * Reservation functions here avoid a huge stack in xfs_trans_init due to
96  * register overflow from temporaries in the calculations.
97  */
98
99
100 /*
101  * In a write transaction we can allocate a maximum of 2
102  * extents.  This gives:
103  *    the inode getting the new extents: inode size
104  *    the inode's bmap btree: max depth * block size
105  *    the agfs of the ags from which the extents are allocated: 2 * sector
106  *    the superblock free block counter: sector size
107  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
108  * And the bmap_finish transaction can free bmap blocks in a join:
109  *    the agfs of the ags containing the blocks: 2 * sector size
110  *    the agfls of the ags containing the blocks: 2 * sector size
111  *    the super block free block counter: sector size
112  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
113  */
114 STATIC uint
115 xfs_calc_write_reservation(
116         struct xfs_mount        *mp)
117 {
118         return XFS_DQUOT_LOGRES(mp) +
119                 MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
120                      xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
121                                       XFS_FSB_TO_B(mp, 1)) +
122                      xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
123                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
124                                       XFS_FSB_TO_B(mp, 1))),
125                     (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
126                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
127                                       XFS_FSB_TO_B(mp, 1))));
128 }
129
130 /*
131  * In truncating a file we free up to two extents at once.  We can modify:
132  *    the inode being truncated: inode size
133  *    the inode's bmap btree: (max depth + 1) * block size
134  * And the bmap_finish transaction can free the blocks and bmap blocks:
135  *    the agf for each of the ags: 4 * sector size
136  *    the agfl for each of the ags: 4 * sector size
137  *    the super block to reflect the freed blocks: sector size
138  *    worst case split in allocation btrees per extent assuming 4 extents:
139  *              4 exts * 2 trees * (2 * max depth - 1) * block size
140  *    the inode btree: max depth * blocksize
141  *    the allocation btrees: 2 trees * (max depth - 1) * block size
142  */
143 STATIC uint
144 xfs_calc_itruncate_reservation(
145         struct xfs_mount        *mp)
146 {
147         return XFS_DQUOT_LOGRES(mp) +
148                 MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
149                      xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) + 1,
150                                       XFS_FSB_TO_B(mp, 1))),
151                     (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
152                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
153                                       XFS_FSB_TO_B(mp, 1)) +
154                     xfs_calc_buf_res(5, 0) +
155                     xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
156                                      XFS_FSB_TO_B(mp, 1)) +
157                     xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
158                                      mp->m_in_maxlevels, 0)));
159 }
160
161 /*
162  * In renaming a files we can modify:
163  *    the four inodes involved: 4 * inode size
164  *    the two directory btrees: 2 * (max depth + v2) * dir block size
165  *    the two directory bmap btrees: 2 * max depth * block size
166  * And the bmap_finish transaction can free dir and bmap blocks (two sets
167  *      of bmap blocks) giving:
168  *    the agf for the ags in which the blocks live: 3 * sector size
169  *    the agfl for the ags in which the blocks live: 3 * sector size
170  *    the superblock for the free block count: sector size
171  *    the allocation btrees: 3 exts * 2 trees * (2 * max depth - 1) * block size
172  */
173 STATIC uint
174 xfs_calc_rename_reservation(
175         struct xfs_mount        *mp)
176 {
177         return XFS_DQUOT_LOGRES(mp) +
178                 MAX((xfs_calc_buf_res(4, mp->m_sb.sb_inodesize) +
179                      xfs_calc_buf_res(2 * XFS_DIROP_LOG_COUNT(mp),
180                                       XFS_FSB_TO_B(mp, 1))),
181                     (xfs_calc_buf_res(7, mp->m_sb.sb_sectsize) +
182                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 3),
183                                       XFS_FSB_TO_B(mp, 1))));
184 }
185
186 /*
187  * For creating a link to an inode:
188  *    the parent directory inode: inode size
189  *    the linked inode: inode size
190  *    the directory btree could split: (max depth + v2) * dir block size
191  *    the directory bmap btree could join or split: (max depth + v2) * blocksize
192  * And the bmap_finish transaction can free some bmap blocks giving:
193  *    the agf for the ag in which the blocks live: sector size
194  *    the agfl for the ag in which the blocks live: sector size
195  *    the superblock for the free block count: sector size
196  *    the allocation btrees: 2 trees * (2 * max depth - 1) * block size
197  */
198 STATIC uint
199 xfs_calc_link_reservation(
200         struct xfs_mount        *mp)
201 {
202         return XFS_DQUOT_LOGRES(mp) +
203                 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
204                      xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
205                                       XFS_FSB_TO_B(mp, 1))),
206                     (xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
207                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
208                                       XFS_FSB_TO_B(mp, 1))));
209 }
210
211 /*
212  * For removing a directory entry we can modify:
213  *    the parent directory inode: inode size
214  *    the removed inode: inode size
215  *    the directory btree could join: (max depth + v2) * dir block size
216  *    the directory bmap btree could join or split: (max depth + v2) * blocksize
217  * And the bmap_finish transaction can free the dir and bmap blocks giving:
218  *    the agf for the ag in which the blocks live: 2 * sector size
219  *    the agfl for the ag in which the blocks live: 2 * sector size
220  *    the superblock for the free block count: sector size
221  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
222  */
223 STATIC uint
224 xfs_calc_remove_reservation(
225         struct xfs_mount        *mp)
226 {
227         return XFS_DQUOT_LOGRES(mp) +
228                 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
229                      xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
230                                       XFS_FSB_TO_B(mp, 1))),
231                     (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
232                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
233                                       XFS_FSB_TO_B(mp, 1))));
234 }
235
236 /*
237  * For symlink we can modify:
238  *    the parent directory inode: inode size
239  *    the new inode: inode size
240  *    the inode btree entry: 1 block
241  *    the directory btree: (max depth + v2) * dir block size
242  *    the directory inode's bmap btree: (max depth + v2) * block size
243  *    the blocks for the symlink: 1 kB
244  * Or in the first xact we allocate some inodes giving:
245  *    the agi and agf of the ag getting the new inodes: 2 * sectorsize
246  *    the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
247  *    the inode btree: max depth * blocksize
248  *    the allocation btrees: 2 trees * (2 * max depth - 1) * block size
249  */
250 STATIC uint
251 xfs_calc_symlink_reservation(
252         struct xfs_mount        *mp)
253 {
254         return XFS_DQUOT_LOGRES(mp) +
255                 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
256                      xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
257                      xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
258                                       XFS_FSB_TO_B(mp, 1)) +
259                      xfs_calc_buf_res(1, 1024)),
260                     (xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
261                      xfs_calc_buf_res(XFS_IALLOC_BLOCKS(mp),
262                                       XFS_FSB_TO_B(mp, 1)) +
263                      xfs_calc_buf_res(mp->m_in_maxlevels,
264                                       XFS_FSB_TO_B(mp, 1)) +
265                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
266                                       XFS_FSB_TO_B(mp, 1))));
267 }
268
269 /*
270  * For create we can modify:
271  *    the parent directory inode: inode size
272  *    the new inode: inode size
273  *    the inode btree entry: block size
274  *    the superblock for the nlink flag: sector size
275  *    the directory btree: (max depth + v2) * dir block size
276  *    the directory inode's bmap btree: (max depth + v2) * block size
277  * Or in the first xact we allocate some inodes giving:
278  *    the agi and agf of the ag getting the new inodes: 2 * sectorsize
279  *    the superblock for the nlink flag: sector size
280  *    the inode blocks allocated: XFS_IALLOC_BLOCKS * blocksize
281  *    the inode btree: max depth * blocksize
282  *    the allocation btrees: 2 trees * (max depth - 1) * block size
283  */
284 STATIC uint
285 xfs_calc_create_reservation(
286         struct xfs_mount        *mp)
287 {
288         return XFS_DQUOT_LOGRES(mp) +
289                 MAX((xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
290                      xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
291                      (uint)XFS_FSB_TO_B(mp, 1) +
292                      xfs_calc_buf_res(XFS_DIROP_LOG_COUNT(mp),
293                                       XFS_FSB_TO_B(mp, 1))),
294                     (xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
295                      mp->m_sb.sb_sectsize +
296                      xfs_calc_buf_res(XFS_IALLOC_BLOCKS(mp),
297                                       XFS_FSB_TO_B(mp, 1)) +
298                      xfs_calc_buf_res(mp->m_in_maxlevels,
299                                       XFS_FSB_TO_B(mp, 1)) +
300                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
301                                       XFS_FSB_TO_B(mp, 1))));
302 }
303
304 /*
305  * Making a new directory is the same as creating a new file.
306  */
307 STATIC uint
308 xfs_calc_mkdir_reservation(
309         struct xfs_mount        *mp)
310 {
311         return xfs_calc_create_reservation(mp);
312 }
313
314 /*
315  * In freeing an inode we can modify:
316  *    the inode being freed: inode size
317  *    the super block free inode counter: sector size
318  *    the agi hash list and counters: sector size
319  *    the inode btree entry: block size
320  *    the on disk inode before ours in the agi hash list: inode cluster size
321  *    the inode btree: max depth * blocksize
322  *    the allocation btrees: 2 trees * (max depth - 1) * block size
323  */
324 STATIC uint
325 xfs_calc_ifree_reservation(
326         struct xfs_mount        *mp)
327 {
328         return XFS_DQUOT_LOGRES(mp) +
329                 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
330                 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
331                 xfs_calc_buf_res(1, XFS_FSB_TO_B(mp, 1)) +
332                 MAX((__uint16_t)XFS_FSB_TO_B(mp, 1),
333                     XFS_INODE_CLUSTER_SIZE(mp)) +
334                 xfs_calc_buf_res(1, 0) +
335                 xfs_calc_buf_res(2 + XFS_IALLOC_BLOCKS(mp) +
336                                  mp->m_in_maxlevels, 0) +
337                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
338                                  XFS_FSB_TO_B(mp, 1));
339 }
340
341 /*
342  * When only changing the inode we log the inode and possibly the superblock
343  * We also add a bit of slop for the transaction stuff.
344  */
345 STATIC uint
346 xfs_calc_ichange_reservation(
347         struct xfs_mount        *mp)
348 {
349         return XFS_DQUOT_LOGRES(mp) +
350                 mp->m_sb.sb_inodesize +
351                 mp->m_sb.sb_sectsize +
352                 512;
353
354 }
355
356 /*
357  * Growing the data section of the filesystem.
358  *      superblock
359  *      agi and agf
360  *      allocation btrees
361  */
362 STATIC uint
363 xfs_calc_growdata_reservation(
364         struct xfs_mount        *mp)
365 {
366         return xfs_calc_buf_res(3, mp->m_sb.sb_sectsize) +
367                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
368                                  XFS_FSB_TO_B(mp, 1));
369 }
370
371 /*
372  * Growing the rt section of the filesystem.
373  * In the first set of transactions (ALLOC) we allocate space to the
374  * bitmap or summary files.
375  *      superblock: sector size
376  *      agf of the ag from which the extent is allocated: sector size
377  *      bmap btree for bitmap/summary inode: max depth * blocksize
378  *      bitmap/summary inode: inode size
379  *      allocation btrees for 1 block alloc: 2 * (2 * maxdepth - 1) * blocksize
380  */
381 STATIC uint
382 xfs_calc_growrtalloc_reservation(
383         struct xfs_mount        *mp)
384 {
385         return xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
386                 xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK),
387                                  XFS_FSB_TO_B(mp, 1)) +
388                 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
389                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
390                                  XFS_FSB_TO_B(mp, 1));
391 }
392
393 /*
394  * Growing the rt section of the filesystem.
395  * In the second set of transactions (ZERO) we zero the new metadata blocks.
396  *      one bitmap/summary block: blocksize
397  */
398 STATIC uint
399 xfs_calc_growrtzero_reservation(
400         struct xfs_mount        *mp)
401 {
402         return xfs_calc_buf_res(1, mp->m_sb.sb_blocksize);
403 }
404
405 /*
406  * Growing the rt section of the filesystem.
407  * In the third set of transactions (FREE) we update metadata without
408  * allocating any new blocks.
409  *      superblock: sector size
410  *      bitmap inode: inode size
411  *      summary inode: inode size
412  *      one bitmap block: blocksize
413  *      summary blocks: new summary size
414  */
415 STATIC uint
416 xfs_calc_growrtfree_reservation(
417         struct xfs_mount        *mp)
418 {
419         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
420                 xfs_calc_buf_res(2, mp->m_sb.sb_inodesize) +
421                 xfs_calc_buf_res(1, mp->m_sb.sb_blocksize) +
422                 xfs_calc_buf_res(1, mp->m_rsumsize);
423 }
424
425 /*
426  * Logging the inode modification timestamp on a synchronous write.
427  *      inode
428  */
429 STATIC uint
430 xfs_calc_swrite_reservation(
431         struct xfs_mount        *mp)
432 {
433         return xfs_calc_buf_res(1, mp->m_sb.sb_inodesize);
434 }
435
436 /*
437  * Logging the inode mode bits when writing a setuid/setgid file
438  *      inode
439  */
440 STATIC uint
441 xfs_calc_writeid_reservation(xfs_mount_t *mp)
442 {
443         return xfs_calc_buf_res(1, mp->m_sb.sb_inodesize);
444 }
445
446 /*
447  * Converting the inode from non-attributed to attributed.
448  *      the inode being converted: inode size
449  *      agf block and superblock (for block allocation)
450  *      the new block (directory sized)
451  *      bmap blocks for the new directory block
452  *      allocation btrees
453  */
454 STATIC uint
455 xfs_calc_addafork_reservation(
456         struct xfs_mount        *mp)
457 {
458         return XFS_DQUOT_LOGRES(mp) +
459                 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
460                 xfs_calc_buf_res(2, mp->m_sb.sb_sectsize) +
461                 xfs_calc_buf_res(1, mp->m_dirblksize) +
462                 xfs_calc_buf_res(XFS_DAENTER_BMAP1B(mp, XFS_DATA_FORK) + 1,
463                                  XFS_FSB_TO_B(mp, 1)) +
464                 xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 1),
465                                  XFS_FSB_TO_B(mp, 1));
466 }
467
468 /*
469  * Removing the attribute fork of a file
470  *    the inode being truncated: inode size
471  *    the inode's bmap btree: max depth * block size
472  * And the bmap_finish transaction can free the blocks and bmap blocks:
473  *    the agf for each of the ags: 4 * sector size
474  *    the agfl for each of the ags: 4 * sector size
475  *    the super block to reflect the freed blocks: sector size
476  *    worst case split in allocation btrees per extent assuming 4 extents:
477  *              4 exts * 2 trees * (2 * max depth - 1) * block size
478  */
479 STATIC uint
480 xfs_calc_attrinval_reservation(
481         struct xfs_mount        *mp)
482 {
483         return MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
484                     xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK),
485                                      XFS_FSB_TO_B(mp, 1))),
486                    (xfs_calc_buf_res(9, mp->m_sb.sb_sectsize) +
487                     xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 4),
488                                      XFS_FSB_TO_B(mp, 1))));
489 }
490
491 /*
492  * Setting an attribute.
493  *      the inode getting the attribute
494  *      the superblock for allocations
495  *      the agfs extents are allocated from
496  *      the attribute btree * max depth
497  *      the inode allocation btree
498  * Since attribute transaction space is dependent on the size of the attribute,
499  * the calculation is done partially at mount time and partially at runtime.
500  */
501 STATIC uint
502 xfs_calc_attrset_reservation(
503         struct xfs_mount        *mp)
504 {
505         return XFS_DQUOT_LOGRES(mp) +
506                 xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
507                 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize) +
508                 xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH, XFS_FSB_TO_B(mp, 1));
509 }
510
511 /*
512  * Removing an attribute.
513  *    the inode: inode size
514  *    the attribute btree could join: max depth * block size
515  *    the inode bmap btree could join or split: max depth * block size
516  * And the bmap_finish transaction can free the attr blocks freed giving:
517  *    the agf for the ag in which the blocks live: 2 * sector size
518  *    the agfl for the ag in which the blocks live: 2 * sector size
519  *    the superblock for the free block count: sector size
520  *    the allocation btrees: 2 exts * 2 trees * (2 * max depth - 1) * block size
521  */
522 STATIC uint
523 xfs_calc_attrrm_reservation(
524         struct xfs_mount        *mp)
525 {
526         return XFS_DQUOT_LOGRES(mp) +
527                 MAX((xfs_calc_buf_res(1, mp->m_sb.sb_inodesize) +
528                      xfs_calc_buf_res(XFS_DA_NODE_MAXDEPTH,
529                                       XFS_FSB_TO_B(mp, 1)) +
530                      (uint)XFS_FSB_TO_B(mp,
531                                         XFS_BM_MAXLEVELS(mp, XFS_ATTR_FORK)) +
532                      xfs_calc_buf_res(XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK), 0)),
533                     (xfs_calc_buf_res(5, mp->m_sb.sb_sectsize) +
534                      xfs_calc_buf_res(XFS_ALLOCFREE_LOG_COUNT(mp, 2),
535                                       XFS_FSB_TO_B(mp, 1))));
536 }
537
538 /*
539  * Clearing a bad agino number in an agi hash bucket.
540  */
541 STATIC uint
542 xfs_calc_clear_agi_bucket_reservation(
543         struct xfs_mount        *mp)
544 {
545         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
546 }
547
548 /*
549  * Clearing the quotaflags in the superblock.
550  *      the super block for changing quota flags: sector size
551  */
552 STATIC uint
553 xfs_calc_qm_sbchange_reservation(
554         struct xfs_mount        *mp)
555 {
556         return xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
557 }
558
559 /*
560  * Adjusting quota limits.
561  *    the xfs_disk_dquot_t: sizeof(struct xfs_disk_dquot)
562  */
563 STATIC uint
564 xfs_calc_qm_setqlim_reservation(
565         struct xfs_mount        *mp)
566 {
567         return xfs_calc_buf_res(1, sizeof(struct xfs_disk_dquot));
568 }
569
570 /*
571  * Allocating quota on disk if needed.
572  *      the write transaction log space: XFS_WRITE_LOG_RES(mp)
573  *      the unit of quota allocation: one system block size
574  */
575 STATIC uint
576 xfs_calc_qm_dqalloc_reservation(
577         struct xfs_mount        *mp)
578 {
579         return XFS_WRITE_LOG_RES(mp) +
580                 xfs_calc_buf_res(1,
581                         XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) - 1);
582 }
583
584 /*
585  * Turning off quotas.
586  *    the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
587  *    the superblock for the quota flags: sector size
588  */
589 STATIC uint
590 xfs_calc_qm_quotaoff_reservation(
591         struct xfs_mount        *mp)
592 {
593         return sizeof(struct xfs_qoff_logitem) * 2 +
594                 xfs_calc_buf_res(1, mp->m_sb.sb_sectsize);
595 }
596
597 /*
598  * End of turning off quotas.
599  *    the xfs_qoff_logitem_t: sizeof(struct xfs_qoff_logitem) * 2
600  */
601 STATIC uint
602 xfs_calc_qm_quotaoff_end_reservation(
603         struct xfs_mount        *mp)
604 {
605         return sizeof(struct xfs_qoff_logitem) * 2;
606 }
607
608 /*
609  * Initialize the precomputed transaction reservation values
610  * in the mount structure.
611  */
612 void
613 xfs_trans_init(
614         struct xfs_mount        *mp)
615 {
616         struct xfs_trans_reservations *resp = &mp->m_reservations;
617
618         resp->tr_write = xfs_calc_write_reservation(mp);
619         resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
620         resp->tr_rename = xfs_calc_rename_reservation(mp);
621         resp->tr_link = xfs_calc_link_reservation(mp);
622         resp->tr_remove = xfs_calc_remove_reservation(mp);
623         resp->tr_symlink = xfs_calc_symlink_reservation(mp);
624         resp->tr_create = xfs_calc_create_reservation(mp);
625         resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
626         resp->tr_ifree = xfs_calc_ifree_reservation(mp);
627         resp->tr_ichange = xfs_calc_ichange_reservation(mp);
628         resp->tr_growdata = xfs_calc_growdata_reservation(mp);
629         resp->tr_swrite = xfs_calc_swrite_reservation(mp);
630         resp->tr_writeid = xfs_calc_writeid_reservation(mp);
631         resp->tr_addafork = xfs_calc_addafork_reservation(mp);
632         resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
633         resp->tr_attrset = xfs_calc_attrset_reservation(mp);
634         resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
635         resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
636         resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
637         resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
638         resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
639         resp->tr_qm_sbchange = xfs_calc_qm_sbchange_reservation(mp);
640         resp->tr_qm_setqlim = xfs_calc_qm_setqlim_reservation(mp);
641         resp->tr_qm_dqalloc = xfs_calc_qm_dqalloc_reservation(mp);
642         resp->tr_qm_quotaoff = xfs_calc_qm_quotaoff_reservation(mp);
643         resp->tr_qm_equotaoff = xfs_calc_qm_quotaoff_end_reservation(mp);
644 }
645
646 /*
647  * This routine is called to allocate a transaction structure.
648  * The type parameter indicates the type of the transaction.  These
649  * are enumerated in xfs_trans.h.
650  *
651  * Dynamically allocate the transaction structure from the transaction
652  * zone, initialize it, and return it to the caller.
653  */
654 xfs_trans_t *
655 xfs_trans_alloc(
656         xfs_mount_t     *mp,
657         uint            type)
658 {
659         xfs_trans_t     *tp;
660
661         sb_start_intwrite(mp->m_super);
662         tp = _xfs_trans_alloc(mp, type, KM_SLEEP);
663         tp->t_flags |= XFS_TRANS_FREEZE_PROT;
664         return tp;
665 }
666
667 xfs_trans_t *
668 _xfs_trans_alloc(
669         xfs_mount_t     *mp,
670         uint            type,
671         xfs_km_flags_t  memflags)
672 {
673         xfs_trans_t     *tp;
674
675         WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
676         atomic_inc(&mp->m_active_trans);
677
678         tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
679         tp->t_magic = XFS_TRANS_MAGIC;
680         tp->t_type = type;
681         tp->t_mountp = mp;
682         INIT_LIST_HEAD(&tp->t_items);
683         INIT_LIST_HEAD(&tp->t_busy);
684         return tp;
685 }
686
687 /*
688  * Free the transaction structure.  If there is more clean up
689  * to do when the structure is freed, add it here.
690  */
691 STATIC void
692 xfs_trans_free(
693         struct xfs_trans        *tp)
694 {
695         xfs_extent_busy_sort(&tp->t_busy);
696         xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
697
698         atomic_dec(&tp->t_mountp->m_active_trans);
699         if (tp->t_flags & XFS_TRANS_FREEZE_PROT)
700                 sb_end_intwrite(tp->t_mountp->m_super);
701         xfs_trans_free_dqinfo(tp);
702         kmem_zone_free(xfs_trans_zone, tp);
703 }
704
705 /*
706  * This is called to create a new transaction which will share the
707  * permanent log reservation of the given transaction.  The remaining
708  * unused block and rt extent reservations are also inherited.  This
709  * implies that the original transaction is no longer allowed to allocate
710  * blocks.  Locks and log items, however, are no inherited.  They must
711  * be added to the new transaction explicitly.
712  */
713 xfs_trans_t *
714 xfs_trans_dup(
715         xfs_trans_t     *tp)
716 {
717         xfs_trans_t     *ntp;
718
719         ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
720
721         /*
722          * Initialize the new transaction structure.
723          */
724         ntp->t_magic = XFS_TRANS_MAGIC;
725         ntp->t_type = tp->t_type;
726         ntp->t_mountp = tp->t_mountp;
727         INIT_LIST_HEAD(&ntp->t_items);
728         INIT_LIST_HEAD(&ntp->t_busy);
729
730         ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
731         ASSERT(tp->t_ticket != NULL);
732
733         ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
734                        (tp->t_flags & XFS_TRANS_RESERVE) |
735                        (tp->t_flags & XFS_TRANS_FREEZE_PROT);
736         /* We gave our writer reference to the new transaction */
737         tp->t_flags &= ~XFS_TRANS_FREEZE_PROT;
738         ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
739         ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
740         tp->t_blk_res = tp->t_blk_res_used;
741         ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
742         tp->t_rtx_res = tp->t_rtx_res_used;
743         ntp->t_pflags = tp->t_pflags;
744
745         xfs_trans_dup_dqinfo(tp, ntp);
746
747         atomic_inc(&tp->t_mountp->m_active_trans);
748         return ntp;
749 }
750
751 /*
752  * This is called to reserve free disk blocks and log space for the
753  * given transaction.  This must be done before allocating any resources
754  * within the transaction.
755  *
756  * This will return ENOSPC if there are not enough blocks available.
757  * It will sleep waiting for available log space.
758  * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
759  * is used by long running transactions.  If any one of the reservations
760  * fails then they will all be backed out.
761  *
762  * This does not do quota reservations. That typically is done by the
763  * caller afterwards.
764  */
765 int
766 xfs_trans_reserve(
767         xfs_trans_t     *tp,
768         uint            blocks,
769         uint            logspace,
770         uint            rtextents,
771         uint            flags,
772         uint            logcount)
773 {
774         int             error = 0;
775         int             rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
776
777         /* Mark this thread as being in a transaction */
778         current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
779
780         /*
781          * Attempt to reserve the needed disk blocks by decrementing
782          * the number needed from the number available.  This will
783          * fail if the count would go below zero.
784          */
785         if (blocks > 0) {
786                 error = xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
787                                           -((int64_t)blocks), rsvd);
788                 if (error != 0) {
789                         current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
790                         return (XFS_ERROR(ENOSPC));
791                 }
792                 tp->t_blk_res += blocks;
793         }
794
795         /*
796          * Reserve the log space needed for this transaction.
797          */
798         if (logspace > 0) {
799                 bool    permanent = false;
800
801                 ASSERT(tp->t_log_res == 0 || tp->t_log_res == logspace);
802                 ASSERT(tp->t_log_count == 0 || tp->t_log_count == logcount);
803
804                 if (flags & XFS_TRANS_PERM_LOG_RES) {
805                         tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
806                         permanent = true;
807                 } else {
808                         ASSERT(tp->t_ticket == NULL);
809                         ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
810                 }
811
812                 if (tp->t_ticket != NULL) {
813                         ASSERT(flags & XFS_TRANS_PERM_LOG_RES);
814                         error = xfs_log_regrant(tp->t_mountp, tp->t_ticket);
815                 } else {
816                         error = xfs_log_reserve(tp->t_mountp, logspace,
817                                                 logcount, &tp->t_ticket,
818                                                 XFS_TRANSACTION, permanent,
819                                                 tp->t_type);
820                 }
821
822                 if (error)
823                         goto undo_blocks;
824
825                 tp->t_log_res = logspace;
826                 tp->t_log_count = logcount;
827         }
828
829         /*
830          * Attempt to reserve the needed realtime extents by decrementing
831          * the number needed from the number available.  This will
832          * fail if the count would go below zero.
833          */
834         if (rtextents > 0) {
835                 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
836                                           -((int64_t)rtextents), rsvd);
837                 if (error) {
838                         error = XFS_ERROR(ENOSPC);
839                         goto undo_log;
840                 }
841                 tp->t_rtx_res += rtextents;
842         }
843
844         return 0;
845
846         /*
847          * Error cases jump to one of these labels to undo any
848          * reservations which have already been performed.
849          */
850 undo_log:
851         if (logspace > 0) {
852                 int             log_flags;
853
854                 if (flags & XFS_TRANS_PERM_LOG_RES) {
855                         log_flags = XFS_LOG_REL_PERM_RESERV;
856                 } else {
857                         log_flags = 0;
858                 }
859                 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
860                 tp->t_ticket = NULL;
861                 tp->t_log_res = 0;
862                 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
863         }
864
865 undo_blocks:
866         if (blocks > 0) {
867                 xfs_icsb_modify_counters(tp->t_mountp, XFS_SBS_FDBLOCKS,
868                                          (int64_t)blocks, rsvd);
869                 tp->t_blk_res = 0;
870         }
871
872         current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
873
874         return error;
875 }
876
877 /*
878  * Record the indicated change to the given field for application
879  * to the file system's superblock when the transaction commits.
880  * For now, just store the change in the transaction structure.
881  *
882  * Mark the transaction structure to indicate that the superblock
883  * needs to be updated before committing.
884  *
885  * Because we may not be keeping track of allocated/free inodes and
886  * used filesystem blocks in the superblock, we do not mark the
887  * superblock dirty in this transaction if we modify these fields.
888  * We still need to update the transaction deltas so that they get
889  * applied to the incore superblock, but we don't want them to
890  * cause the superblock to get locked and logged if these are the
891  * only fields in the superblock that the transaction modifies.
892  */
893 void
894 xfs_trans_mod_sb(
895         xfs_trans_t     *tp,
896         uint            field,
897         int64_t         delta)
898 {
899         uint32_t        flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
900         xfs_mount_t     *mp = tp->t_mountp;
901
902         switch (field) {
903         case XFS_TRANS_SB_ICOUNT:
904                 tp->t_icount_delta += delta;
905                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
906                         flags &= ~XFS_TRANS_SB_DIRTY;
907                 break;
908         case XFS_TRANS_SB_IFREE:
909                 tp->t_ifree_delta += delta;
910                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
911                         flags &= ~XFS_TRANS_SB_DIRTY;
912                 break;
913         case XFS_TRANS_SB_FDBLOCKS:
914                 /*
915                  * Track the number of blocks allocated in the
916                  * transaction.  Make sure it does not exceed the
917                  * number reserved.
918                  */
919                 if (delta < 0) {
920                         tp->t_blk_res_used += (uint)-delta;
921                         ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
922                 }
923                 tp->t_fdblocks_delta += delta;
924                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
925                         flags &= ~XFS_TRANS_SB_DIRTY;
926                 break;
927         case XFS_TRANS_SB_RES_FDBLOCKS:
928                 /*
929                  * The allocation has already been applied to the
930                  * in-core superblock's counter.  This should only
931                  * be applied to the on-disk superblock.
932                  */
933                 ASSERT(delta < 0);
934                 tp->t_res_fdblocks_delta += delta;
935                 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
936                         flags &= ~XFS_TRANS_SB_DIRTY;
937                 break;
938         case XFS_TRANS_SB_FREXTENTS:
939                 /*
940                  * Track the number of blocks allocated in the
941                  * transaction.  Make sure it does not exceed the
942                  * number reserved.
943                  */
944                 if (delta < 0) {
945                         tp->t_rtx_res_used += (uint)-delta;
946                         ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
947                 }
948                 tp->t_frextents_delta += delta;
949                 break;
950         case XFS_TRANS_SB_RES_FREXTENTS:
951                 /*
952                  * The allocation has already been applied to the
953                  * in-core superblock's counter.  This should only
954                  * be applied to the on-disk superblock.
955                  */
956                 ASSERT(delta < 0);
957                 tp->t_res_frextents_delta += delta;
958                 break;
959         case XFS_TRANS_SB_DBLOCKS:
960                 ASSERT(delta > 0);
961                 tp->t_dblocks_delta += delta;
962                 break;
963         case XFS_TRANS_SB_AGCOUNT:
964                 ASSERT(delta > 0);
965                 tp->t_agcount_delta += delta;
966                 break;
967         case XFS_TRANS_SB_IMAXPCT:
968                 tp->t_imaxpct_delta += delta;
969                 break;
970         case XFS_TRANS_SB_REXTSIZE:
971                 tp->t_rextsize_delta += delta;
972                 break;
973         case XFS_TRANS_SB_RBMBLOCKS:
974                 tp->t_rbmblocks_delta += delta;
975                 break;
976         case XFS_TRANS_SB_RBLOCKS:
977                 tp->t_rblocks_delta += delta;
978                 break;
979         case XFS_TRANS_SB_REXTENTS:
980                 tp->t_rextents_delta += delta;
981                 break;
982         case XFS_TRANS_SB_REXTSLOG:
983                 tp->t_rextslog_delta += delta;
984                 break;
985         default:
986                 ASSERT(0);
987                 return;
988         }
989
990         tp->t_flags |= flags;
991 }
992
993 /*
994  * xfs_trans_apply_sb_deltas() is called from the commit code
995  * to bring the superblock buffer into the current transaction
996  * and modify it as requested by earlier calls to xfs_trans_mod_sb().
997  *
998  * For now we just look at each field allowed to change and change
999  * it if necessary.
1000  */
1001 STATIC void
1002 xfs_trans_apply_sb_deltas(
1003         xfs_trans_t     *tp)
1004 {
1005         xfs_dsb_t       *sbp;
1006         xfs_buf_t       *bp;
1007         int             whole = 0;
1008
1009         bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
1010         sbp = XFS_BUF_TO_SBP(bp);
1011
1012         /*
1013          * Check that superblock mods match the mods made to AGF counters.
1014          */
1015         ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
1016                (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
1017                 tp->t_ag_btree_delta));
1018
1019         /*
1020          * Only update the superblock counters if we are logging them
1021          */
1022         if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
1023                 if (tp->t_icount_delta)
1024                         be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
1025                 if (tp->t_ifree_delta)
1026                         be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
1027                 if (tp->t_fdblocks_delta)
1028                         be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
1029                 if (tp->t_res_fdblocks_delta)
1030                         be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
1031         }
1032
1033         if (tp->t_frextents_delta)
1034                 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
1035         if (tp->t_res_frextents_delta)
1036                 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
1037
1038         if (tp->t_dblocks_delta) {
1039                 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
1040                 whole = 1;
1041         }
1042         if (tp->t_agcount_delta) {
1043                 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
1044                 whole = 1;
1045         }
1046         if (tp->t_imaxpct_delta) {
1047                 sbp->sb_imax_pct += tp->t_imaxpct_delta;
1048                 whole = 1;
1049         }
1050         if (tp->t_rextsize_delta) {
1051                 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
1052                 whole = 1;
1053         }
1054         if (tp->t_rbmblocks_delta) {
1055                 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
1056                 whole = 1;
1057         }
1058         if (tp->t_rblocks_delta) {
1059                 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
1060                 whole = 1;
1061         }
1062         if (tp->t_rextents_delta) {
1063                 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
1064                 whole = 1;
1065         }
1066         if (tp->t_rextslog_delta) {
1067                 sbp->sb_rextslog += tp->t_rextslog_delta;
1068                 whole = 1;
1069         }
1070
1071         if (whole)
1072                 /*
1073                  * Log the whole thing, the fields are noncontiguous.
1074                  */
1075                 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
1076         else
1077                 /*
1078                  * Since all the modifiable fields are contiguous, we
1079                  * can get away with this.
1080                  */
1081                 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
1082                                   offsetof(xfs_dsb_t, sb_frextents) +
1083                                   sizeof(sbp->sb_frextents) - 1);
1084 }
1085
1086 /*
1087  * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
1088  * and apply superblock counter changes to the in-core superblock.  The
1089  * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
1090  * applied to the in-core superblock.  The idea is that that has already been
1091  * done.
1092  *
1093  * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
1094  * However, we have to ensure that we only modify each superblock field only
1095  * once because the application of the delta values may not be atomic. That can
1096  * lead to ENOSPC races occurring if we have two separate modifcations of the
1097  * free space counter to put back the entire reservation and then take away
1098  * what we used.
1099  *
1100  * If we are not logging superblock counters, then the inode allocated/free and
1101  * used block counts are not updated in the on disk superblock. In this case,
1102  * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
1103  * still need to update the incore superblock with the changes.
1104  */
1105 void
1106 xfs_trans_unreserve_and_mod_sb(
1107         xfs_trans_t     *tp)
1108 {
1109         xfs_mod_sb_t    msb[9]; /* If you add cases, add entries */
1110         xfs_mod_sb_t    *msbp;
1111         xfs_mount_t     *mp = tp->t_mountp;
1112         /* REFERENCED */
1113         int             error;
1114         int             rsvd;
1115         int64_t         blkdelta = 0;
1116         int64_t         rtxdelta = 0;
1117         int64_t         idelta = 0;
1118         int64_t         ifreedelta = 0;
1119
1120         msbp = msb;
1121         rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
1122
1123         /* calculate deltas */
1124         if (tp->t_blk_res > 0)
1125                 blkdelta = tp->t_blk_res;
1126         if ((tp->t_fdblocks_delta != 0) &&
1127             (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1128              (tp->t_flags & XFS_TRANS_SB_DIRTY)))
1129                 blkdelta += tp->t_fdblocks_delta;
1130
1131         if (tp->t_rtx_res > 0)
1132                 rtxdelta = tp->t_rtx_res;
1133         if ((tp->t_frextents_delta != 0) &&
1134             (tp->t_flags & XFS_TRANS_SB_DIRTY))
1135                 rtxdelta += tp->t_frextents_delta;
1136
1137         if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
1138              (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
1139                 idelta = tp->t_icount_delta;
1140                 ifreedelta = tp->t_ifree_delta;
1141         }
1142
1143         /* apply the per-cpu counters */
1144         if (blkdelta) {
1145                 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
1146                                                  blkdelta, rsvd);
1147                 if (error)
1148                         goto out;
1149         }
1150
1151         if (idelta) {
1152                 error = xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT,
1153                                                  idelta, rsvd);
1154                 if (error)
1155                         goto out_undo_fdblocks;
1156         }
1157
1158         if (ifreedelta) {
1159                 error = xfs_icsb_modify_counters(mp, XFS_SBS_IFREE,
1160                                                  ifreedelta, rsvd);
1161                 if (error)
1162                         goto out_undo_icount;
1163         }
1164
1165         /* apply remaining deltas */
1166         if (rtxdelta != 0) {
1167                 msbp->msb_field = XFS_SBS_FREXTENTS;
1168                 msbp->msb_delta = rtxdelta;
1169                 msbp++;
1170         }
1171
1172         if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
1173                 if (tp->t_dblocks_delta != 0) {
1174                         msbp->msb_field = XFS_SBS_DBLOCKS;
1175                         msbp->msb_delta = tp->t_dblocks_delta;
1176                         msbp++;
1177                 }
1178                 if (tp->t_agcount_delta != 0) {
1179                         msbp->msb_field = XFS_SBS_AGCOUNT;
1180                         msbp->msb_delta = tp->t_agcount_delta;
1181                         msbp++;
1182                 }
1183                 if (tp->t_imaxpct_delta != 0) {
1184                         msbp->msb_field = XFS_SBS_IMAX_PCT;
1185                         msbp->msb_delta = tp->t_imaxpct_delta;
1186                         msbp++;
1187                 }
1188                 if (tp->t_rextsize_delta != 0) {
1189                         msbp->msb_field = XFS_SBS_REXTSIZE;
1190                         msbp->msb_delta = tp->t_rextsize_delta;
1191                         msbp++;
1192                 }
1193                 if (tp->t_rbmblocks_delta != 0) {
1194                         msbp->msb_field = XFS_SBS_RBMBLOCKS;
1195                         msbp->msb_delta = tp->t_rbmblocks_delta;
1196                         msbp++;
1197                 }
1198                 if (tp->t_rblocks_delta != 0) {
1199                         msbp->msb_field = XFS_SBS_RBLOCKS;
1200                         msbp->msb_delta = tp->t_rblocks_delta;
1201                         msbp++;
1202                 }
1203                 if (tp->t_rextents_delta != 0) {
1204                         msbp->msb_field = XFS_SBS_REXTENTS;
1205                         msbp->msb_delta = tp->t_rextents_delta;
1206                         msbp++;
1207                 }
1208                 if (tp->t_rextslog_delta != 0) {
1209                         msbp->msb_field = XFS_SBS_REXTSLOG;
1210                         msbp->msb_delta = tp->t_rextslog_delta;
1211                         msbp++;
1212                 }
1213         }
1214
1215         /*
1216          * If we need to change anything, do it.
1217          */
1218         if (msbp > msb) {
1219                 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
1220                         (uint)(msbp - msb), rsvd);
1221                 if (error)
1222                         goto out_undo_ifreecount;
1223         }
1224
1225         return;
1226
1227 out_undo_ifreecount:
1228         if (ifreedelta)
1229                 xfs_icsb_modify_counters(mp, XFS_SBS_IFREE, -ifreedelta, rsvd);
1230 out_undo_icount:
1231         if (idelta)
1232                 xfs_icsb_modify_counters(mp, XFS_SBS_ICOUNT, -idelta, rsvd);
1233 out_undo_fdblocks:
1234         if (blkdelta)
1235                 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, -blkdelta, rsvd);
1236 out:
1237         ASSERT(error == 0);
1238         return;
1239 }
1240
1241 /*
1242  * Add the given log item to the transaction's list of log items.
1243  *
1244  * The log item will now point to its new descriptor with its li_desc field.
1245  */
1246 void
1247 xfs_trans_add_item(
1248         struct xfs_trans        *tp,
1249         struct xfs_log_item     *lip)
1250 {
1251         struct xfs_log_item_desc *lidp;
1252
1253         ASSERT(lip->li_mountp == tp->t_mountp);
1254         ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
1255
1256         lidp = kmem_zone_zalloc(xfs_log_item_desc_zone, KM_SLEEP | KM_NOFS);
1257
1258         lidp->lid_item = lip;
1259         lidp->lid_flags = 0;
1260         list_add_tail(&lidp->lid_trans, &tp->t_items);
1261
1262         lip->li_desc = lidp;
1263 }
1264
1265 STATIC void
1266 xfs_trans_free_item_desc(
1267         struct xfs_log_item_desc *lidp)
1268 {
1269         list_del_init(&lidp->lid_trans);
1270         kmem_zone_free(xfs_log_item_desc_zone, lidp);
1271 }
1272
1273 /*
1274  * Unlink and free the given descriptor.
1275  */
1276 void
1277 xfs_trans_del_item(
1278         struct xfs_log_item     *lip)
1279 {
1280         xfs_trans_free_item_desc(lip->li_desc);
1281         lip->li_desc = NULL;
1282 }
1283
1284 /*
1285  * Unlock all of the items of a transaction and free all the descriptors
1286  * of that transaction.
1287  */
1288 void
1289 xfs_trans_free_items(
1290         struct xfs_trans        *tp,
1291         xfs_lsn_t               commit_lsn,
1292         int                     flags)
1293 {
1294         struct xfs_log_item_desc *lidp, *next;
1295
1296         list_for_each_entry_safe(lidp, next, &tp->t_items, lid_trans) {
1297                 struct xfs_log_item     *lip = lidp->lid_item;
1298
1299                 lip->li_desc = NULL;
1300
1301                 if (commit_lsn != NULLCOMMITLSN)
1302                         IOP_COMMITTING(lip, commit_lsn);
1303                 if (flags & XFS_TRANS_ABORT)
1304                         lip->li_flags |= XFS_LI_ABORTED;
1305                 IOP_UNLOCK(lip);
1306
1307                 xfs_trans_free_item_desc(lidp);
1308         }
1309 }
1310
1311 static inline void
1312 xfs_log_item_batch_insert(
1313         struct xfs_ail          *ailp,
1314         struct xfs_ail_cursor   *cur,
1315         struct xfs_log_item     **log_items,
1316         int                     nr_items,
1317         xfs_lsn_t               commit_lsn)
1318 {
1319         int     i;
1320
1321         spin_lock(&ailp->xa_lock);
1322         /* xfs_trans_ail_update_bulk drops ailp->xa_lock */
1323         xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
1324
1325         for (i = 0; i < nr_items; i++)
1326                 IOP_UNPIN(log_items[i], 0);
1327 }
1328
1329 /*
1330  * Bulk operation version of xfs_trans_committed that takes a log vector of
1331  * items to insert into the AIL. This uses bulk AIL insertion techniques to
1332  * minimise lock traffic.
1333  *
1334  * If we are called with the aborted flag set, it is because a log write during
1335  * a CIL checkpoint commit has failed. In this case, all the items in the
1336  * checkpoint have already gone through IOP_COMMITED and IOP_UNLOCK, which
1337  * means that checkpoint commit abort handling is treated exactly the same
1338  * as an iclog write error even though we haven't started any IO yet. Hence in
1339  * this case all we need to do is IOP_COMMITTED processing, followed by an
1340  * IOP_UNPIN(aborted) call.
1341  *
1342  * The AIL cursor is used to optimise the insert process. If commit_lsn is not
1343  * at the end of the AIL, the insert cursor avoids the need to walk
1344  * the AIL to find the insertion point on every xfs_log_item_batch_insert()
1345  * call. This saves a lot of needless list walking and is a net win, even
1346  * though it slightly increases that amount of AIL lock traffic to set it up
1347  * and tear it down.
1348  */
1349 void
1350 xfs_trans_committed_bulk(
1351         struct xfs_ail          *ailp,
1352         struct xfs_log_vec      *log_vector,
1353         xfs_lsn_t               commit_lsn,
1354         int                     aborted)
1355 {
1356 #define LOG_ITEM_BATCH_SIZE     32
1357         struct xfs_log_item     *log_items[LOG_ITEM_BATCH_SIZE];
1358         struct xfs_log_vec      *lv;
1359         struct xfs_ail_cursor   cur;
1360         int                     i = 0;
1361
1362         spin_lock(&ailp->xa_lock);
1363         xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
1364         spin_unlock(&ailp->xa_lock);
1365
1366         /* unpin all the log items */
1367         for (lv = log_vector; lv; lv = lv->lv_next ) {
1368                 struct xfs_log_item     *lip = lv->lv_item;
1369                 xfs_lsn_t               item_lsn;
1370
1371                 if (aborted)
1372                         lip->li_flags |= XFS_LI_ABORTED;
1373                 item_lsn = IOP_COMMITTED(lip, commit_lsn);
1374
1375                 /* item_lsn of -1 means the item needs no further processing */
1376                 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
1377                         continue;
1378
1379                 /*
1380                  * if we are aborting the operation, no point in inserting the
1381                  * object into the AIL as we are in a shutdown situation.
1382                  */
1383                 if (aborted) {
1384                         ASSERT(XFS_FORCED_SHUTDOWN(ailp->xa_mount));
1385                         IOP_UNPIN(lip, 1);
1386                         continue;
1387                 }
1388
1389                 if (item_lsn != commit_lsn) {
1390
1391                         /*
1392                          * Not a bulk update option due to unusual item_lsn.
1393                          * Push into AIL immediately, rechecking the lsn once
1394                          * we have the ail lock. Then unpin the item. This does
1395                          * not affect the AIL cursor the bulk insert path is
1396                          * using.
1397                          */
1398                         spin_lock(&ailp->xa_lock);
1399                         if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
1400                                 xfs_trans_ail_update(ailp, lip, item_lsn);
1401                         else
1402                                 spin_unlock(&ailp->xa_lock);
1403                         IOP_UNPIN(lip, 0);
1404                         continue;
1405                 }
1406
1407                 /* Item is a candidate for bulk AIL insert.  */
1408                 log_items[i++] = lv->lv_item;
1409                 if (i >= LOG_ITEM_BATCH_SIZE) {
1410                         xfs_log_item_batch_insert(ailp, &cur, log_items,
1411                                         LOG_ITEM_BATCH_SIZE, commit_lsn);
1412                         i = 0;
1413                 }
1414         }
1415
1416         /* make sure we insert the remainder! */
1417         if (i)
1418                 xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
1419
1420         spin_lock(&ailp->xa_lock);
1421         xfs_trans_ail_cursor_done(ailp, &cur);
1422         spin_unlock(&ailp->xa_lock);
1423 }
1424
1425 /*
1426  * Commit the given transaction to the log.
1427  *
1428  * XFS disk error handling mechanism is not based on a typical
1429  * transaction abort mechanism. Logically after the filesystem
1430  * gets marked 'SHUTDOWN', we can't let any new transactions
1431  * be durable - ie. committed to disk - because some metadata might
1432  * be inconsistent. In such cases, this returns an error, and the
1433  * caller may assume that all locked objects joined to the transaction
1434  * have already been unlocked as if the commit had succeeded.
1435  * Do not reference the transaction structure after this call.
1436  */
1437 int
1438 xfs_trans_commit(
1439         struct xfs_trans        *tp,
1440         uint                    flags)
1441 {
1442         struct xfs_mount        *mp = tp->t_mountp;
1443         xfs_lsn_t               commit_lsn = -1;
1444         int                     error = 0;
1445         int                     log_flags = 0;
1446         int                     sync = tp->t_flags & XFS_TRANS_SYNC;
1447
1448         /*
1449          * Determine whether this commit is releasing a permanent
1450          * log reservation or not.
1451          */
1452         if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1453                 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1454                 log_flags = XFS_LOG_REL_PERM_RESERV;
1455         }
1456
1457         /*
1458          * If there is nothing to be logged by the transaction,
1459          * then unlock all of the items associated with the
1460          * transaction and free the transaction structure.
1461          * Also make sure to return any reserved blocks to
1462          * the free pool.
1463          */
1464         if (!(tp->t_flags & XFS_TRANS_DIRTY))
1465                 goto out_unreserve;
1466
1467         if (XFS_FORCED_SHUTDOWN(mp)) {
1468                 error = XFS_ERROR(EIO);
1469                 goto out_unreserve;
1470         }
1471
1472         ASSERT(tp->t_ticket != NULL);
1473
1474         /*
1475          * If we need to update the superblock, then do it now.
1476          */
1477         if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1478                 xfs_trans_apply_sb_deltas(tp);
1479         xfs_trans_apply_dquot_deltas(tp);
1480
1481         error = xfs_log_commit_cil(mp, tp, &commit_lsn, flags);
1482         if (error == ENOMEM) {
1483                 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
1484                 error = XFS_ERROR(EIO);
1485                 goto out_unreserve;
1486         }
1487
1488         current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1489         xfs_trans_free(tp);
1490
1491         /*
1492          * If the transaction needs to be synchronous, then force the
1493          * log out now and wait for it.
1494          */
1495         if (sync) {
1496                 if (!error) {
1497                         error = _xfs_log_force_lsn(mp, commit_lsn,
1498                                       XFS_LOG_SYNC, NULL);
1499                 }
1500                 XFS_STATS_INC(xs_trans_sync);
1501         } else {
1502                 XFS_STATS_INC(xs_trans_async);
1503         }
1504
1505         return error;
1506
1507 out_unreserve:
1508         xfs_trans_unreserve_and_mod_sb(tp);
1509
1510         /*
1511          * It is indeed possible for the transaction to be not dirty but
1512          * the dqinfo portion to be.  All that means is that we have some
1513          * (non-persistent) quota reservations that need to be unreserved.
1514          */
1515         xfs_trans_unreserve_and_mod_dquots(tp);
1516         if (tp->t_ticket) {
1517                 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1518                 if (commit_lsn == -1 && !error)
1519                         error = XFS_ERROR(EIO);
1520         }
1521         current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1522         xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
1523         xfs_trans_free(tp);
1524
1525         XFS_STATS_INC(xs_trans_empty);
1526         return error;
1527 }
1528
1529 /*
1530  * Unlock all of the transaction's items and free the transaction.
1531  * The transaction must not have modified any of its items, because
1532  * there is no way to restore them to their previous state.
1533  *
1534  * If the transaction has made a log reservation, make sure to release
1535  * it as well.
1536  */
1537 void
1538 xfs_trans_cancel(
1539         xfs_trans_t             *tp,
1540         int                     flags)
1541 {
1542         int                     log_flags;
1543         xfs_mount_t             *mp = tp->t_mountp;
1544
1545         /*
1546          * See if the caller is being too lazy to figure out if
1547          * the transaction really needs an abort.
1548          */
1549         if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1550                 flags &= ~XFS_TRANS_ABORT;
1551         /*
1552          * See if the caller is relying on us to shut down the
1553          * filesystem.  This happens in paths where we detect
1554          * corruption and decide to give up.
1555          */
1556         if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
1557                 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1558                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1559         }
1560 #ifdef DEBUG
1561         if (!(flags & XFS_TRANS_ABORT) && !XFS_FORCED_SHUTDOWN(mp)) {
1562                 struct xfs_log_item_desc *lidp;
1563
1564                 list_for_each_entry(lidp, &tp->t_items, lid_trans)
1565                         ASSERT(!(lidp->lid_item->li_type == XFS_LI_EFD));
1566         }
1567 #endif
1568         xfs_trans_unreserve_and_mod_sb(tp);
1569         xfs_trans_unreserve_and_mod_dquots(tp);
1570
1571         if (tp->t_ticket) {
1572                 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1573                         ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1574                         log_flags = XFS_LOG_REL_PERM_RESERV;
1575                 } else {
1576                         log_flags = 0;
1577                 }
1578                 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1579         }
1580
1581         /* mark this thread as no longer being in a transaction */
1582         current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1583
1584         xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
1585         xfs_trans_free(tp);
1586 }
1587
1588 /*
1589  * Roll from one trans in the sequence of PERMANENT transactions to
1590  * the next: permanent transactions are only flushed out when
1591  * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1592  * as possible to let chunks of it go to the log. So we commit the
1593  * chunk we've been working on and get a new transaction to continue.
1594  */
1595 int
1596 xfs_trans_roll(
1597         struct xfs_trans        **tpp,
1598         struct xfs_inode        *dp)
1599 {
1600         struct xfs_trans        *trans;
1601         unsigned int            logres, count;
1602         int                     error;
1603
1604         /*
1605          * Ensure that the inode is always logged.
1606          */
1607         trans = *tpp;
1608         xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1609
1610         /*
1611          * Copy the critical parameters from one trans to the next.
1612          */
1613         logres = trans->t_log_res;
1614         count = trans->t_log_count;
1615         *tpp = xfs_trans_dup(trans);
1616
1617         /*
1618          * Commit the current transaction.
1619          * If this commit failed, then it'd just unlock those items that
1620          * are not marked ihold. That also means that a filesystem shutdown
1621          * is in progress. The caller takes the responsibility to cancel
1622          * the duplicate transaction that gets returned.
1623          */
1624         error = xfs_trans_commit(trans, 0);
1625         if (error)
1626                 return (error);
1627
1628         trans = *tpp;
1629
1630         /*
1631          * transaction commit worked ok so we can drop the extra ticket
1632          * reference that we gained in xfs_trans_dup()
1633          */
1634         xfs_log_ticket_put(trans->t_ticket);
1635
1636
1637         /*
1638          * Reserve space in the log for th next transaction.
1639          * This also pushes items in the "AIL", the list of logged items,
1640          * out to disk if they are taking up space at the tail of the log
1641          * that we want to use.  This requires that either nothing be locked
1642          * across this call, or that anything that is locked be logged in
1643          * the prior and the next transactions.
1644          */
1645         error = xfs_trans_reserve(trans, 0, logres, 0,
1646                                   XFS_TRANS_PERM_LOG_RES, count);
1647         /*
1648          *  Ensure that the inode is in the new transaction and locked.
1649          */
1650         if (error)
1651                 return error;
1652
1653         xfs_trans_ijoin(trans, dp, 0);
1654         return 0;
1655 }