blob: 8aaebb2f9efaeff733c6dcc51d5c560a4751e15a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
28#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_inode_item.h"
David Chinnerdb7a19f2008-04-10 12:22:24 +100033#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
37kmem_zone_t *xfs_ili_zone; /* inode log item zone */
38
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100039static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
40{
41 return container_of(lip, struct xfs_inode_log_item, ili_item);
42}
43
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * This returns the number of iovecs needed to log the given inode item.
47 *
48 * We need one iovec for the inode log format structure, one for the
49 * inode core, and possibly one for the inode data/extents/b-tree root
50 * and one for the inode attribute data/extents/b-tree root.
51 */
52STATIC uint
53xfs_inode_item_size(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100054 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100056 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
57 struct xfs_inode *ip = iip->ili_inode;
58 uint nvecs = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 switch (ip->i_d.di_format) {
61 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +000062 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +000063 ip->i_d.di_nextents > 0 &&
64 ip->i_df.if_bytes > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
67
68 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +000069 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +000070 ip->i_df.if_broot_bytes > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 break;
73
74 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +000075 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +000076 ip->i_df.if_bytes > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79
80 case XFS_DINODE_FMT_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 case XFS_DINODE_FMT_UUID:
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83
84 default:
85 ASSERT(0);
86 break;
87 }
88
Christoph Hellwig339a5f52012-02-29 09:53:53 +000089 if (!XFS_IFORK_Q(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return nvecs;
Christoph Hellwig339a5f52012-02-29 09:53:53 +000091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /*
94 * Log any necessary attribute data.
95 */
96 switch (ip->i_d.di_aformat) {
97 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +000098 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +000099 ip->i_d.di_anextents > 0 &&
100 ip->i_afp->if_bytes > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 break;
103
104 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000105 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000106 ip->i_afp->if_broot_bytes > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 break;
109
110 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000111 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000112 ip->i_afp->if_bytes > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
115
116 default:
117 ASSERT(0);
118 break;
119 }
120
121 return nvecs;
122}
123
124/*
Dave Chinnere8287762011-04-08 12:45:07 +1000125 * xfs_inode_item_format_extents - convert in-core extents to on-disk form
126 *
127 * For either the data or attr fork in extent format, we need to endian convert
128 * the in-core extent as we place them into the on-disk inode. In this case, we
129 * need to do this conversion before we write the extents into the log. Because
130 * we don't have the disk inode to write into here, we allocate a buffer and
131 * format the extents into it via xfs_iextents_copy(). We free the buffer in
132 * the unlock routine after the copy for the log has been made.
133 *
134 * In the case of the data fork, the in-core and on-disk fork sizes can be
135 * different due to delayed allocation extents. We only log on-disk extents
136 * here, so always use the physical fork size to determine the size of the
137 * buffer we need to allocate.
138 */
139STATIC void
140xfs_inode_item_format_extents(
141 struct xfs_inode *ip,
142 struct xfs_log_iovec *vecp,
143 int whichfork,
144 int type)
145{
146 xfs_bmbt_rec_t *ext_buffer;
147
148 ext_buffer = kmem_alloc(XFS_IFORK_SIZE(ip, whichfork), KM_SLEEP);
149 if (whichfork == XFS_DATA_FORK)
150 ip->i_itemp->ili_extents_buf = ext_buffer;
151 else
152 ip->i_itemp->ili_aextents_buf = ext_buffer;
153
154 vecp->i_addr = ext_buffer;
155 vecp->i_len = xfs_iextents_copy(ip, ext_buffer, whichfork);
156 vecp->i_type = type;
157}
158
159/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * This is called to fill in the vector of log iovecs for the
161 * given inode log item. It fills the first item with an inode
162 * log format structure, the second with the on-disk inode structure,
163 * and a possible third and/or fourth with the inode data/extents/b-tree
164 * root and inode attributes data/extents/b-tree root.
165 */
166STATIC void
167xfs_inode_item_format(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000168 struct xfs_log_item *lip,
169 struct xfs_log_iovec *vecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000171 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
172 struct xfs_inode *ip = iip->ili_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 uint nvecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 size_t data_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 xfs_mount_t *mp;
176
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000177 vecp->i_addr = &iip->ili_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 vecp->i_len = sizeof(xfs_inode_log_format_t);
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000179 vecp->i_type = XLOG_REG_TYPE_IFORMAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 vecp++;
181 nvecs = 1;
182
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000183 vecp->i_addr = &ip->i_d;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100184 vecp->i_len = sizeof(struct xfs_icdinode);
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000185 vecp->i_type = XLOG_REG_TYPE_ICORE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 vecp++;
187 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /*
190 * If this is really an old format inode, then we need to
191 * log it as such. This means that we have to copy the link
192 * count from the new field to the old. We don't have to worry
193 * about the new fields, because nothing trusts them as long as
194 * the old inode version number is there. If the superblock already
195 * has a new version number, then we don't bother converting back.
196 */
197 mp = ip->i_mount;
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100198 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
199 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +1100200 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /*
202 * Convert it back.
203 */
204 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
205 ip->i_d.di_onlink = ip->i_d.di_nlink;
206 } else {
207 /*
208 * The superblock version has already been bumped,
209 * so just make the conversion to the new inode
210 * format permanent.
211 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100212 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 ip->i_d.di_onlink = 0;
214 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
215 }
216 }
217
218 switch (ip->i_d.di_format) {
219 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000220 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000221 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
222 XFS_ILOG_DEV | XFS_ILOG_UUID);
223
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000224 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000225 ip->i_d.di_nextents > 0 &&
226 ip->i_df.if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ASSERT(ip->i_df.if_u1.if_extents != NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000228 ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ASSERT(iip->ili_extents_buf == NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000230
Nathan Scottf016bad2005-09-08 15:30:05 +1000231#ifdef XFS_NATIVE_HOST
Dave Chinner696123f2010-07-26 13:51:46 -0500232 if (ip->i_d.di_nextents == ip->i_df.if_bytes /
233 (uint)sizeof(xfs_bmbt_rec_t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /*
235 * There are no delayed allocation
236 * extents, so just point to the
237 * real extents array.
238 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000239 vecp->i_addr = ip->i_df.if_u1.if_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 vecp->i_len = ip->i_df.if_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000241 vecp->i_type = XLOG_REG_TYPE_IEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 } else
243#endif
244 {
Dave Chinnere8287762011-04-08 12:45:07 +1000245 xfs_inode_item_format_extents(ip, vecp,
246 XFS_DATA_FORK, XLOG_REG_TYPE_IEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 ASSERT(vecp->i_len <= ip->i_df.if_bytes);
249 iip->ili_format.ilf_dsize = vecp->i_len;
250 vecp++;
251 nvecs++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000252 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000253 iip->ili_fields &= ~XFS_ILOG_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 break;
256
257 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000258 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000259 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
260 XFS_ILOG_DEV | XFS_ILOG_UUID);
261
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000262 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000263 ip->i_df.if_broot_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ASSERT(ip->i_df.if_broot != NULL);
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000265 vecp->i_addr = ip->i_df.if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 vecp->i_len = ip->i_df.if_broot_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000267 vecp->i_type = XLOG_REG_TYPE_IBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 vecp++;
269 nvecs++;
270 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000271 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000272 ASSERT(!(iip->ili_fields &
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000273 XFS_ILOG_DBROOT));
274#ifdef XFS_TRANS_DEBUG
275 if (iip->ili_root_size > 0) {
276 ASSERT(iip->ili_root_size ==
277 ip->i_df.if_broot_bytes);
278 ASSERT(memcmp(iip->ili_orig_root,
279 ip->i_df.if_broot,
280 iip->ili_root_size) == 0);
281 } else {
282 ASSERT(ip->i_df.if_broot_bytes == 0);
283 }
284#endif
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000285 iip->ili_fields &= ~XFS_ILOG_DBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 break;
288
289 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000290 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000291 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
292 XFS_ILOG_DEV | XFS_ILOG_UUID);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000293 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000294 ip->i_df.if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ASSERT(ip->i_df.if_u1.if_data != NULL);
296 ASSERT(ip->i_d.di_size > 0);
297
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000298 vecp->i_addr = ip->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /*
300 * Round i_bytes up to a word boundary.
301 * The underlying memory is guaranteed to
302 * to be there by xfs_idata_realloc().
303 */
304 data_bytes = roundup(ip->i_df.if_bytes, 4);
305 ASSERT((ip->i_df.if_real_bytes == 0) ||
306 (ip->i_df.if_real_bytes == data_bytes));
307 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000308 vecp->i_type = XLOG_REG_TYPE_ILOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 vecp++;
310 nvecs++;
311 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000312 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000313 iip->ili_fields &= ~XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 break;
316
317 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000318 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000319 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
320 XFS_ILOG_DEXT | XFS_ILOG_UUID);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000321 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 iip->ili_format.ilf_u.ilfu_rdev =
323 ip->i_df.if_u2.if_rdev;
324 }
325 break;
326
327 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000328 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000329 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
330 XFS_ILOG_DEXT | XFS_ILOG_DEV);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000331 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 iip->ili_format.ilf_u.ilfu_uuid =
333 ip->i_df.if_u2.if_uuid;
334 }
335 break;
336
337 default:
338 ASSERT(0);
339 break;
340 }
341
342 /*
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000343 * If there are no attributes associated with the file, then we're done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
345 if (!XFS_IFORK_Q(ip)) {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000346 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000347 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000348 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 switch (ip->i_d.di_aformat) {
352 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000353 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000354 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
355
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000356 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000357 ip->i_d.di_anextents > 0 &&
358 ip->i_afp->if_bytes > 0) {
359 ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
360 ip->i_d.di_anextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
Nathan Scottf016bad2005-09-08 15:30:05 +1000362#ifdef XFS_NATIVE_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /*
364 * There are not delayed allocation extents
365 * for attributes, so just point at the array.
366 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000367 vecp->i_addr = ip->i_afp->if_u1.if_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 vecp->i_len = ip->i_afp->if_bytes;
Dave Chinnere8287762011-04-08 12:45:07 +1000369 vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#else
371 ASSERT(iip->ili_aextents_buf == NULL);
Dave Chinnere8287762011-04-08 12:45:07 +1000372 xfs_inode_item_format_extents(ip, vecp,
373 XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#endif
375 iip->ili_format.ilf_asize = vecp->i_len;
376 vecp++;
377 nvecs++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000378 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000379 iip->ili_fields &= ~XFS_ILOG_AEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 break;
382
383 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000384 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000385 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
386
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000387 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000388 ip->i_afp->if_broot_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 ASSERT(ip->i_afp->if_broot != NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000390
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000391 vecp->i_addr = ip->i_afp->if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 vecp->i_len = ip->i_afp->if_broot_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000393 vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 vecp++;
395 nvecs++;
396 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000397 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000398 iip->ili_fields &= ~XFS_ILOG_ABROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 break;
401
402 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000403 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000404 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
405
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000406 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000407 ip->i_afp->if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ASSERT(ip->i_afp->if_u1.if_data != NULL);
409
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000410 vecp->i_addr = ip->i_afp->if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * Round i_bytes up to a word boundary.
413 * The underlying memory is guaranteed to
414 * to be there by xfs_idata_realloc().
415 */
416 data_bytes = roundup(ip->i_afp->if_bytes, 4);
417 ASSERT((ip->i_afp->if_real_bytes == 0) ||
418 (ip->i_afp->if_real_bytes == data_bytes));
419 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000420 vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 vecp++;
422 nvecs++;
423 iip->ili_format.ilf_asize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000424 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000425 iip->ili_fields &= ~XFS_ILOG_ADATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 break;
428
429 default:
430 ASSERT(0);
431 break;
432 }
433
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000434out:
435 /*
436 * Now update the log format that goes out to disk from the in-core
437 * values. We always write the inode core to make the arithmetic
438 * games in recovery easier, which isn't a big deal as just about any
439 * transaction would dirty it anyway.
440 */
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000441 iip->ili_format.ilf_fields = XFS_ILOG_CORE |
442 (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 iip->ili_format.ilf_size = nvecs;
444}
445
446
447/*
448 * This is called to pin the inode associated with the inode log
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000449 * item in memory so it cannot be written out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451STATIC void
452xfs_inode_item_pin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000453 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000455 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000456
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000457 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
458
459 trace_xfs_inode_pin(ip, _RET_IP_);
460 atomic_inc(&ip->i_pincount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463
464/*
465 * This is called to unpin the inode associated with the inode log
466 * item which was previously pinned with a call to xfs_inode_item_pin().
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000467 *
468 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470STATIC void
471xfs_inode_item_unpin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000472 struct xfs_log_item *lip,
Christoph Hellwig9412e312010-06-23 18:11:15 +1000473 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000475 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000476
Dave Chinner4aaf15d2010-03-08 11:24:07 +1100477 trace_xfs_inode_unpin(ip, _RET_IP_);
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000478 ASSERT(atomic_read(&ip->i_pincount) > 0);
479 if (atomic_dec_and_test(&ip->i_pincount))
Christoph Hellwigf392e632011-12-18 20:00:10 +0000480 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000484xfs_inode_item_push(
485 struct xfs_log_item *lip,
486 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000488 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
489 struct xfs_inode *ip = iip->ili_inode;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000490 struct xfs_buf *bp = NULL;
491 uint rval = XFS_ITEM_SUCCESS;
492 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000494 if (xfs_ipincount(ip) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return XFS_ITEM_PINNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000497 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return XFS_ITEM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Christoph Hellwig4c468192012-04-23 15:58:36 +1000500 /*
501 * Re-check the pincount now that we stabilized the value by
502 * taking the ilock.
503 */
504 if (xfs_ipincount(ip) > 0) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000505 rval = XFS_ITEM_PINNED;
506 goto out_unlock;
Christoph Hellwig4c468192012-04-23 15:58:36 +1000507 }
508
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000509 /*
510 * Someone else is already flushing the inode. Nothing we can do
511 * here but wait for the flush to finish and remove the item from
512 * the AIL.
513 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!xfs_iflock_nowait(ip)) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000515 rval = XFS_ITEM_FLUSHING;
516 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000519 /*
520 * Stale inode items should force out the iclog.
521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (ip->i_flags & XFS_ISTALE) {
523 xfs_ifunlock(ip);
Christoph Hellwig5b03ff12012-02-20 02:31:22 +0000524 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return XFS_ITEM_PINNED;
526 }
527
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000528 ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
529 ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
530
531 spin_unlock(&lip->li_ailp->xa_lock);
532
533 error = xfs_iflush(ip, &bp);
534 if (!error) {
535 if (!xfs_buf_delwri_queue(bp, buffer_list))
536 rval = XFS_ITEM_FLUSHING;
537 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000539
540 spin_lock(&lip->li_ailp->xa_lock);
541out_unlock:
542 xfs_iunlock(ip, XFS_ILOCK_SHARED);
543 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546/*
547 * Unlock the inode associated with the inode log item.
548 * Clear the fields of the inode and inode log item that
549 * are specific to the current transaction. If the
550 * hold flags is set, do not unlock the inode.
551 */
552STATIC void
553xfs_inode_item_unlock(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000554 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000556 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
557 struct xfs_inode *ip = iip->ili_inode;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000558 unsigned short lock_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200560 ASSERT(ip->i_itemp != NULL);
561 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /*
564 * If the inode needed a separate buffer with which to log
565 * its extents, then free it now.
566 */
567 if (iip->ili_extents_buf != NULL) {
568 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
569 ASSERT(ip->i_d.di_nextents > 0);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000570 ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ASSERT(ip->i_df.if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000572 kmem_free(iip->ili_extents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 iip->ili_extents_buf = NULL;
574 }
575 if (iip->ili_aextents_buf != NULL) {
576 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
577 ASSERT(ip->i_d.di_anextents > 0);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000578 ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 ASSERT(ip->i_afp->if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000580 kmem_free(iip->ili_aextents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 iip->ili_aextents_buf = NULL;
582 }
583
Christoph Hellwig898621d2010-06-24 11:36:58 +1000584 lock_flags = iip->ili_lock_flags;
585 iip->ili_lock_flags = 0;
Christoph Hellwigddc34152011-09-19 15:00:54 +0000586 if (lock_flags)
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200587 xfs_iunlock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
Dave Chinnerde25c182010-11-30 15:15:46 +1100591 * This is called to find out where the oldest active copy of the inode log
592 * item in the on disk log resides now that the last log write of it completed
593 * at the given lsn. Since we always re-log all dirty data in an inode, the
594 * latest copy in the on disk log is the only one that matters. Therefore,
595 * simply return the given lsn.
596 *
597 * If the inode has been marked stale because the cluster is being freed, we
598 * don't want to (re-)insert this inode into the AIL. There is a race condition
599 * where the cluster buffer may be unpinned before the inode is inserted into
600 * the AIL during transaction committed processing. If the buffer is unpinned
601 * before the inode item has been committed and inserted, then it is possible
Dave Chinner1316d4d2011-07-04 05:27:36 +0000602 * for the buffer to be written and IO completes before the inode is inserted
Dave Chinnerde25c182010-11-30 15:15:46 +1100603 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
604 * AIL which will never get removed. It will, however, get reclaimed which
605 * triggers an assert in xfs_inode_free() complaining about freein an inode
606 * still in the AIL.
607 *
Dave Chinner1316d4d2011-07-04 05:27:36 +0000608 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
609 * transaction committed code knows that it does not need to do any further
610 * processing on the item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612STATIC xfs_lsn_t
613xfs_inode_item_committed(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000614 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 xfs_lsn_t lsn)
616{
Dave Chinnerde25c182010-11-30 15:15:46 +1100617 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
618 struct xfs_inode *ip = iip->ili_inode;
619
Dave Chinner1316d4d2011-07-04 05:27:36 +0000620 if (xfs_iflags_test(ip, XFS_ISTALE)) {
621 xfs_inode_item_unpin(lip, 0);
622 return -1;
623 }
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000624 return lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * XXX rcc - this one really has to do something. Probably needs
629 * to stamp in a new field in the incore inode.
630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631STATIC void
632xfs_inode_item_committing(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000633 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 xfs_lsn_t lsn)
635{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000636 INODE_ITEM(lip)->ili_last_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/*
640 * This is the ops vector shared by all buf log items.
641 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000642static const struct xfs_item_ops xfs_inode_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000643 .iop_size = xfs_inode_item_size,
644 .iop_format = xfs_inode_item_format,
645 .iop_pin = xfs_inode_item_pin,
646 .iop_unpin = xfs_inode_item_unpin,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000647 .iop_unlock = xfs_inode_item_unlock,
648 .iop_committed = xfs_inode_item_committed,
649 .iop_push = xfs_inode_item_push,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000650 .iop_committing = xfs_inode_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651};
652
653
654/*
655 * Initialize the inode log item for a newly allocated (in-core) inode.
656 */
657void
658xfs_inode_item_init(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000659 struct xfs_inode *ip,
660 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000662 struct xfs_inode_log_item *iip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 ASSERT(ip->i_itemp == NULL);
665 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 iip->ili_inode = ip;
Dave Chinner43f5efc2010-03-23 10:10:00 +1100668 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
669 &xfs_inode_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 iip->ili_format.ilf_type = XFS_LI_INODE;
671 iip->ili_format.ilf_ino = ip->i_ino;
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100672 iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
673 iip->ili_format.ilf_len = ip->i_imap.im_len;
674 iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677/*
678 * Free the inode log item and any memory hanging off of it.
679 */
680void
681xfs_inode_item_destroy(
682 xfs_inode_t *ip)
683{
684#ifdef XFS_TRANS_DEBUG
685 if (ip->i_itemp->ili_root_size != 0) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000686 kmem_free(ip->i_itemp->ili_orig_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688#endif
689 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
690}
691
692
693/*
694 * This is the inode flushing I/O completion routine. It is called
695 * from interrupt level when the buffer containing the inode is
696 * flushed to disk. It is responsible for removing the inode item
697 * from the AIL if it has not been re-logged, and unlocking the inode's
698 * flush lock.
Dave Chinner30136832010-12-20 12:03:17 +1100699 *
700 * To reduce AIL lock traffic as much as possible, we scan the buffer log item
701 * list for other inodes that will run this function. We remove them from the
702 * buffer list so we can process all the inode IO completions in one AIL lock
703 * traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705void
706xfs_iflush_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000707 struct xfs_buf *bp,
708 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Dave Chinner30136832010-12-20 12:03:17 +1100710 struct xfs_inode_log_item *iip;
711 struct xfs_log_item *blip;
712 struct xfs_log_item *next;
713 struct xfs_log_item *prev;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000714 struct xfs_ail *ailp = lip->li_ailp;
Dave Chinner30136832010-12-20 12:03:17 +1100715 int need_ail = 0;
716
717 /*
718 * Scan the buffer IO completions for other inodes being completed and
719 * attach them to the current inode log item.
720 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200721 blip = bp->b_fspriv;
Dave Chinner30136832010-12-20 12:03:17 +1100722 prev = NULL;
723 while (blip != NULL) {
724 if (lip->li_cb != xfs_iflush_done) {
725 prev = blip;
726 blip = blip->li_bio_list;
727 continue;
728 }
729
730 /* remove from list */
731 next = blip->li_bio_list;
732 if (!prev) {
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200733 bp->b_fspriv = next;
Dave Chinner30136832010-12-20 12:03:17 +1100734 } else {
735 prev->li_bio_list = next;
736 }
737
738 /* add to current list */
739 blip->li_bio_list = lip->li_bio_list;
740 lip->li_bio_list = blip;
741
742 /*
743 * while we have the item, do the unlocked check for needing
744 * the AIL lock.
745 */
746 iip = INODE_ITEM(blip);
747 if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
748 need_ail++;
749
750 blip = next;
751 }
752
753 /* make sure we capture the state of the initial inode. */
754 iip = INODE_ITEM(lip);
755 if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
756 need_ail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /*
759 * We only want to pull the item from the AIL if it is
760 * actually there and its location in the log has not
761 * changed since we started the flush. Thus, we only bother
762 * if the ili_logged flag is set and the inode's lsn has not
763 * changed. First we check the lsn outside
764 * the lock since it's cheaper, and then we recheck while
765 * holding the lock before removing the inode from the AIL.
766 */
Dave Chinner30136832010-12-20 12:03:17 +1100767 if (need_ail) {
768 struct xfs_log_item *log_items[need_ail];
769 int i = 0;
David Chinner783a2f62008-10-30 17:39:58 +1100770 spin_lock(&ailp->xa_lock);
Dave Chinner30136832010-12-20 12:03:17 +1100771 for (blip = lip; blip; blip = blip->li_bio_list) {
772 iip = INODE_ITEM(blip);
773 if (iip->ili_logged &&
774 blip->li_lsn == iip->ili_flush_lsn) {
775 log_items[i++] = blip;
776 }
777 ASSERT(i <= need_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Dave Chinner30136832010-12-20 12:03:17 +1100779 /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
780 xfs_trans_ail_delete_bulk(ailp, log_items, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /*
Dave Chinner30136832010-12-20 12:03:17 +1100785 * clean up and unlock the flush lock now we are done. We can clear the
786 * ili_last_fields bits now that we know that the data corresponding to
787 * them is safely on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Dave Chinner30136832010-12-20 12:03:17 +1100789 for (blip = lip; blip; blip = next) {
790 next = blip->li_bio_list;
791 blip->li_bio_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Dave Chinner30136832010-12-20 12:03:17 +1100793 iip = INODE_ITEM(blip);
794 iip->ili_logged = 0;
795 iip->ili_last_fields = 0;
796 xfs_ifunlock(iip->ili_inode);
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/*
801 * This is the inode flushing abort routine. It is called
802 * from xfs_iflush when the filesystem is shutting down to clean
803 * up the inode state.
804 * It is responsible for removing the inode item
805 * from the AIL if it has not been re-logged, and unlocking the inode's
806 * flush lock.
807 */
808void
809xfs_iflush_abort(
810 xfs_inode_t *ip)
811{
David Chinner783a2f62008-10-30 17:39:58 +1100812 xfs_inode_log_item_t *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (iip) {
David Chinner783a2f62008-10-30 17:39:58 +1100815 struct xfs_ail *ailp = iip->ili_item.li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100817 spin_lock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100819 /* xfs_trans_ail_delete() drops the AIL lock. */
820 xfs_trans_ail_delete(ailp, (xfs_log_item_t *)iip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 } else
David Chinner783a2f62008-10-30 17:39:58 +1100822 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824 iip->ili_logged = 0;
825 /*
826 * Clear the ili_last_fields bits now that we know that the
827 * data corresponding to them is safely on disk.
828 */
829 iip->ili_last_fields = 0;
830 /*
831 * Clear the inode logging fields so no more flushes are
832 * attempted.
833 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000834 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 /*
837 * Release the inode's flush lock since we're done with it.
838 */
839 xfs_ifunlock(ip);
840}
841
842void
843xfs_istale_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000844 struct xfs_buf *bp,
845 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000847 xfs_iflush_abort(INODE_ITEM(lip)->ili_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
Tim Shimmin6d192a92006-06-09 14:55:38 +1000849
850/*
851 * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
852 * (which can have different field alignments) to the native version
853 */
854int
855xfs_inode_item_format_convert(
856 xfs_log_iovec_t *buf,
857 xfs_inode_log_format_t *in_f)
858{
859 if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000860 xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000861
Tim Shimmin6d192a92006-06-09 14:55:38 +1000862 in_f->ilf_type = in_f32->ilf_type;
863 in_f->ilf_size = in_f32->ilf_size;
864 in_f->ilf_fields = in_f32->ilf_fields;
865 in_f->ilf_asize = in_f32->ilf_asize;
866 in_f->ilf_dsize = in_f32->ilf_dsize;
867 in_f->ilf_ino = in_f32->ilf_ino;
868 /* copy biggest field of ilf_u */
869 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
870 in_f32->ilf_u.ilfu_uuid.__u_bits,
871 sizeof(uuid_t));
872 in_f->ilf_blkno = in_f32->ilf_blkno;
873 in_f->ilf_len = in_f32->ilf_len;
874 in_f->ilf_boffset = in_f32->ilf_boffset;
875 return 0;
876 } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000877 xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000878
Tim Shimmin6d192a92006-06-09 14:55:38 +1000879 in_f->ilf_type = in_f64->ilf_type;
880 in_f->ilf_size = in_f64->ilf_size;
881 in_f->ilf_fields = in_f64->ilf_fields;
882 in_f->ilf_asize = in_f64->ilf_asize;
883 in_f->ilf_dsize = in_f64->ilf_dsize;
884 in_f->ilf_ino = in_f64->ilf_ino;
885 /* copy biggest field of ilf_u */
886 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
887 in_f64->ilf_u.ilfu_uuid.__u_bits,
888 sizeof(uuid_t));
889 in_f->ilf_blkno = in_f64->ilf_blkno;
890 in_f->ilf_len = in_f64->ilf_len;
891 in_f->ilf_boffset = in_f64->ilf_boffset;
892 return 0;
893 }
894 return EFSCORRUPTED;
895}