blob: 2ad12dcf83113942bf024af5b3341147f8c6d6ec [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_inode_item.h"
David Chinnerdb7a19f2008-04-10 12:22:24 +100029#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans_priv.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110032#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34
35kmem_zone_t *xfs_ili_zone; /* inode log item zone */
36
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100037static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
38{
39 return container_of(lip, struct xfs_inode_log_item, ili_item);
40}
41
Christoph Hellwigce9641d2013-12-13 11:00:43 +110042STATIC void
43xfs_inode_item_data_fork_size(
44 struct xfs_inode_log_item *iip,
45 int *nvecs,
46 int *nbytes)
47{
48 struct xfs_inode *ip = iip->ili_inode;
49
50 switch (ip->i_d.di_format) {
51 case XFS_DINODE_FMT_EXTENTS:
52 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
53 ip->i_d.di_nextents > 0 &&
54 ip->i_df.if_bytes > 0) {
55 /* worst case, doesn't subtract delalloc extents */
56 *nbytes += XFS_IFORK_DSIZE(ip);
57 *nvecs += 1;
58 }
59 break;
60 case XFS_DINODE_FMT_BTREE:
61 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
62 ip->i_df.if_broot_bytes > 0) {
63 *nbytes += ip->i_df.if_broot_bytes;
64 *nvecs += 1;
65 }
66 break;
67 case XFS_DINODE_FMT_LOCAL:
68 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
69 ip->i_df.if_bytes > 0) {
70 *nbytes += roundup(ip->i_df.if_bytes, 4);
71 *nvecs += 1;
72 }
73 break;
74
75 case XFS_DINODE_FMT_DEV:
76 case XFS_DINODE_FMT_UUID:
77 break;
78 default:
79 ASSERT(0);
80 break;
81 }
82}
83
84STATIC void
85xfs_inode_item_attr_fork_size(
86 struct xfs_inode_log_item *iip,
87 int *nvecs,
88 int *nbytes)
89{
90 struct xfs_inode *ip = iip->ili_inode;
91
92 switch (ip->i_d.di_aformat) {
93 case XFS_DINODE_FMT_EXTENTS:
94 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
95 ip->i_d.di_anextents > 0 &&
96 ip->i_afp->if_bytes > 0) {
97 /* worst case, doesn't subtract unused space */
98 *nbytes += XFS_IFORK_ASIZE(ip);
99 *nvecs += 1;
100 }
101 break;
102 case XFS_DINODE_FMT_BTREE:
103 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
104 ip->i_afp->if_broot_bytes > 0) {
105 *nbytes += ip->i_afp->if_broot_bytes;
106 *nvecs += 1;
107 }
108 break;
109 case XFS_DINODE_FMT_LOCAL:
110 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
111 ip->i_afp->if_bytes > 0) {
112 *nbytes += roundup(ip->i_afp->if_bytes, 4);
113 *nvecs += 1;
114 }
115 break;
116 default:
117 ASSERT(0);
118 break;
119 }
120}
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
123 * This returns the number of iovecs needed to log the given inode item.
124 *
125 * We need one iovec for the inode log format structure, one for the
126 * inode core, and possibly one for the inode data/extents/b-tree root
127 * and one for the inode attribute data/extents/b-tree root.
128 */
Dave Chinner166d1362013-08-12 20:50:04 +1000129STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130xfs_inode_item_size(
Dave Chinner166d1362013-08-12 20:50:04 +1000131 struct xfs_log_item *lip,
132 int *nvecs,
133 int *nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000135 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
136 struct xfs_inode *ip = iip->ili_inode;
Dave Chinner166d1362013-08-12 20:50:04 +1000137
138 *nvecs += 2;
139 *nbytes += sizeof(struct xfs_inode_log_format) +
140 xfs_icdinode_size(ip->i_d.di_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Christoph Hellwigce9641d2013-12-13 11:00:43 +1100142 xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
143 if (XFS_IFORK_Q(ip))
144 xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/*
Dave Chinnere8287762011-04-08 12:45:07 +1000148 * xfs_inode_item_format_extents - convert in-core extents to on-disk form
149 *
150 * For either the data or attr fork in extent format, we need to endian convert
151 * the in-core extent as we place them into the on-disk inode. In this case, we
152 * need to do this conversion before we write the extents into the log. Because
153 * we don't have the disk inode to write into here, we allocate a buffer and
154 * format the extents into it via xfs_iextents_copy(). We free the buffer in
155 * the unlock routine after the copy for the log has been made.
156 *
157 * In the case of the data fork, the in-core and on-disk fork sizes can be
158 * different due to delayed allocation extents. We only log on-disk extents
159 * here, so always use the physical fork size to determine the size of the
160 * buffer we need to allocate.
161 */
162STATIC void
163xfs_inode_item_format_extents(
164 struct xfs_inode *ip,
165 struct xfs_log_iovec *vecp,
166 int whichfork,
167 int type)
168{
169 xfs_bmbt_rec_t *ext_buffer;
170
171 ext_buffer = kmem_alloc(XFS_IFORK_SIZE(ip, whichfork), KM_SLEEP);
172 if (whichfork == XFS_DATA_FORK)
173 ip->i_itemp->ili_extents_buf = ext_buffer;
174 else
175 ip->i_itemp->ili_aextents_buf = ext_buffer;
176
177 vecp->i_addr = ext_buffer;
178 vecp->i_len = xfs_iextents_copy(ip, ext_buffer, whichfork);
179 vecp->i_type = type;
180}
181
182/*
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100183 * If this is a v1 format inode, then we need to log it as such. This means
184 * that we have to copy the link count from the new field to the old. We
185 * don't have to worry about the new fields, because nothing trusts them as
186 * long as the old inode version number is there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188STATIC void
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100189xfs_inode_item_format_v1_inode(
190 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100192 if (!xfs_sb_version_hasnlink(&ip->i_mount->m_sb)) {
193 /*
194 * Convert it back.
195 */
196 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
197 ip->i_d.di_onlink = ip->i_d.di_nlink;
198 } else {
199 /*
200 * The superblock version has already been bumped,
201 * so just make the conversion to the new inode
202 * format permanent.
203 */
204 ip->i_d.di_version = 2;
205 ip->i_d.di_onlink = 0;
206 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100208}
209
210STATIC struct xfs_log_iovec *
211xfs_inode_item_format_data_fork(
212 struct xfs_inode_log_item *iip,
213 struct xfs_log_iovec *vecp,
214 int *nvecs)
215{
216 struct xfs_inode *ip = iip->ili_inode;
217 size_t data_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 switch (ip->i_d.di_format) {
220 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000221 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000222 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
223 XFS_ILOG_DEV | XFS_ILOG_UUID);
224
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000225 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000226 ip->i_d.di_nextents > 0 &&
227 ip->i_df.if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ASSERT(ip->i_df.if_u1.if_extents != NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000229 ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 ASSERT(iip->ili_extents_buf == NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000231
Nathan Scottf016bad2005-09-08 15:30:05 +1000232#ifdef XFS_NATIVE_HOST
Dave Chinner696123f2010-07-26 13:51:46 -0500233 if (ip->i_d.di_nextents == ip->i_df.if_bytes /
234 (uint)sizeof(xfs_bmbt_rec_t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
236 * There are no delayed allocation
237 * extents, so just point to the
238 * real extents array.
239 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000240 vecp->i_addr = ip->i_df.if_u1.if_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 vecp->i_len = ip->i_df.if_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000242 vecp->i_type = XLOG_REG_TYPE_IEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 } else
244#endif
245 {
Dave Chinnere8287762011-04-08 12:45:07 +1000246 xfs_inode_item_format_extents(ip, vecp,
247 XFS_DATA_FORK, XLOG_REG_TYPE_IEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 ASSERT(vecp->i_len <= ip->i_df.if_bytes);
250 iip->ili_format.ilf_dsize = vecp->i_len;
251 vecp++;
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100252 (*nvecs)++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000253 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000254 iip->ili_fields &= ~XFS_ILOG_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 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++;
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100269 (*nvecs)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 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));
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000274 iip->ili_fields &= ~XFS_ILOG_DBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000278 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000279 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
280 XFS_ILOG_DEV | XFS_ILOG_UUID);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000281 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000282 ip->i_df.if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ASSERT(ip->i_df.if_u1.if_data != NULL);
284 ASSERT(ip->i_d.di_size > 0);
285
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000286 vecp->i_addr = ip->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /*
288 * Round i_bytes up to a word boundary.
289 * The underlying memory is guaranteed to
290 * to be there by xfs_idata_realloc().
291 */
292 data_bytes = roundup(ip->i_df.if_bytes, 4);
293 ASSERT((ip->i_df.if_real_bytes == 0) ||
294 (ip->i_df.if_real_bytes == data_bytes));
295 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000296 vecp->i_type = XLOG_REG_TYPE_ILOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 vecp++;
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100298 (*nvecs)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000300 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000301 iip->ili_fields &= ~XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000305 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000306 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
307 XFS_ILOG_DEXT | XFS_ILOG_UUID);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000308 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 iip->ili_format.ilf_u.ilfu_rdev =
310 ip->i_df.if_u2.if_rdev;
311 }
312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000314 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000315 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
316 XFS_ILOG_DEXT | XFS_ILOG_DEV);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000317 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 iip->ili_format.ilf_u.ilfu_uuid =
319 ip->i_df.if_u2.if_uuid;
320 }
321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 default:
323 ASSERT(0);
324 break;
325 }
326
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100327 return vecp;
328}
329
330STATIC struct xfs_log_iovec *
331xfs_inode_item_format_attr_fork(
332 struct xfs_inode_log_item *iip,
333 struct xfs_log_iovec *vecp,
334 int *nvecs)
335{
336 struct xfs_inode *ip = iip->ili_inode;
337 size_t data_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 switch (ip->i_d.di_aformat) {
340 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000341 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000342 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
343
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000344 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000345 ip->i_d.di_anextents > 0 &&
346 ip->i_afp->if_bytes > 0) {
347 ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
348 ip->i_d.di_anextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
Nathan Scottf016bad2005-09-08 15:30:05 +1000350#ifdef XFS_NATIVE_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
352 * There are not delayed allocation extents
353 * for attributes, so just point at the array.
354 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000355 vecp->i_addr = ip->i_afp->if_u1.if_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 vecp->i_len = ip->i_afp->if_bytes;
Dave Chinnere8287762011-04-08 12:45:07 +1000357 vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#else
359 ASSERT(iip->ili_aextents_buf == NULL);
Dave Chinnere8287762011-04-08 12:45:07 +1000360 xfs_inode_item_format_extents(ip, vecp,
361 XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#endif
363 iip->ili_format.ilf_asize = vecp->i_len;
364 vecp++;
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100365 (*nvecs)++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000366 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000367 iip->ili_fields &= ~XFS_ILOG_AEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000371 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000372 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
373
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000374 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000375 ip->i_afp->if_broot_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ASSERT(ip->i_afp->if_broot != NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000377
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000378 vecp->i_addr = ip->i_afp->if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 vecp->i_len = ip->i_afp->if_broot_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000380 vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 vecp++;
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100382 (*nvecs)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000384 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000385 iip->ili_fields &= ~XFS_ILOG_ABROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000389 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000390 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
391
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000392 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000393 ip->i_afp->if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ASSERT(ip->i_afp->if_u1.if_data != NULL);
395
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000396 vecp->i_addr = ip->i_afp->if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Round i_bytes up to a word boundary.
399 * The underlying memory is guaranteed to
400 * to be there by xfs_idata_realloc().
401 */
402 data_bytes = roundup(ip->i_afp->if_bytes, 4);
403 ASSERT((ip->i_afp->if_real_bytes == 0) ||
404 (ip->i_afp->if_real_bytes == data_bytes));
405 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000406 vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 vecp++;
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100408 (*nvecs)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 iip->ili_format.ilf_asize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000410 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000411 iip->ili_fields &= ~XFS_ILOG_ADATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 default:
415 ASSERT(0);
416 break;
417 }
418
Christoph Hellwig3de559fb2013-12-13 11:00:43 +1100419 return vecp;
420}
421
422/*
423 * This is called to fill in the vector of log iovecs for the given inode
424 * log item. It fills the first item with an inode log format structure,
425 * the second with the on-disk inode structure, and a possible third and/or
426 * fourth with the inode data/extents/b-tree root and inode attributes
427 * data/extents/b-tree root.
428 */
429STATIC void
430xfs_inode_item_format(
431 struct xfs_log_item *lip,
432 struct xfs_log_iovec *vecp)
433{
434 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
435 struct xfs_inode *ip = iip->ili_inode;
436 uint nvecs;
437
438 vecp->i_addr = &iip->ili_format;
439 vecp->i_len = sizeof(xfs_inode_log_format_t);
440 vecp->i_type = XLOG_REG_TYPE_IFORMAT;
441 vecp++;
442 nvecs = 1;
443
444 vecp->i_addr = &ip->i_d;
445 vecp->i_len = xfs_icdinode_size(ip->i_d.di_version);
446 vecp->i_type = XLOG_REG_TYPE_ICORE;
447 vecp++;
448 nvecs++;
449
450 if (ip->i_d.di_version == 1)
451 xfs_inode_item_format_v1_inode(ip);
452
453 vecp = xfs_inode_item_format_data_fork(iip, vecp, &nvecs);
454 if (XFS_IFORK_Q(ip)) {
455 vecp = xfs_inode_item_format_attr_fork(iip, vecp, &nvecs);
456 } else {
457 iip->ili_fields &=
458 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
459 }
460
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000461 /*
462 * Now update the log format that goes out to disk from the in-core
463 * values. We always write the inode core to make the arithmetic
464 * games in recovery easier, which isn't a big deal as just about any
465 * transaction would dirty it anyway.
466 */
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000467 iip->ili_format.ilf_fields = XFS_ILOG_CORE |
468 (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 iip->ili_format.ilf_size = nvecs;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/*
473 * This is called to pin the inode associated with the inode log
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000474 * item in memory so it cannot be written out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
476STATIC void
477xfs_inode_item_pin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000478 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000480 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000481
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000482 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
483
484 trace_xfs_inode_pin(ip, _RET_IP_);
485 atomic_inc(&ip->i_pincount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488
489/*
490 * This is called to unpin the inode associated with the inode log
491 * item which was previously pinned with a call to xfs_inode_item_pin().
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000492 *
493 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495STATIC void
496xfs_inode_item_unpin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000497 struct xfs_log_item *lip,
Christoph Hellwig9412e312010-06-23 18:11:15 +1000498 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000500 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000501
Dave Chinner4aaf15d2010-03-08 11:24:07 +1100502 trace_xfs_inode_unpin(ip, _RET_IP_);
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000503 ASSERT(atomic_read(&ip->i_pincount) > 0);
504 if (atomic_dec_and_test(&ip->i_pincount))
Christoph Hellwigf392e632011-12-18 20:00:10 +0000505 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000509xfs_inode_item_push(
510 struct xfs_log_item *lip,
511 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000513 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
514 struct xfs_inode *ip = iip->ili_inode;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000515 struct xfs_buf *bp = NULL;
516 uint rval = XFS_ITEM_SUCCESS;
517 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000519 if (xfs_ipincount(ip) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return XFS_ITEM_PINNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000522 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return XFS_ITEM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Christoph Hellwig4c468192012-04-23 15:58:36 +1000525 /*
526 * Re-check the pincount now that we stabilized the value by
527 * taking the ilock.
528 */
529 if (xfs_ipincount(ip) > 0) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000530 rval = XFS_ITEM_PINNED;
531 goto out_unlock;
Christoph Hellwig4c468192012-04-23 15:58:36 +1000532 }
533
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000534 /*
Brian Foster9a3a5da2012-06-11 10:39:43 -0400535 * Stale inode items should force out the iclog.
536 */
537 if (ip->i_flags & XFS_ISTALE) {
538 rval = XFS_ITEM_PINNED;
539 goto out_unlock;
540 }
541
542 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000543 * Someone else is already flushing the inode. Nothing we can do
544 * here but wait for the flush to finish and remove the item from
545 * the AIL.
546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!xfs_iflock_nowait(ip)) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000548 rval = XFS_ITEM_FLUSHING;
549 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000552 ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
553 ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
554
555 spin_unlock(&lip->li_ailp->xa_lock);
556
557 error = xfs_iflush(ip, &bp);
558 if (!error) {
559 if (!xfs_buf_delwri_queue(bp, buffer_list))
560 rval = XFS_ITEM_FLUSHING;
561 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000563
564 spin_lock(&lip->li_ailp->xa_lock);
565out_unlock:
566 xfs_iunlock(ip, XFS_ILOCK_SHARED);
567 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570/*
571 * Unlock the inode associated with the inode log item.
572 * Clear the fields of the inode and inode log item that
573 * are specific to the current transaction. If the
574 * hold flags is set, do not unlock the inode.
575 */
576STATIC void
577xfs_inode_item_unlock(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000578 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000580 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
581 struct xfs_inode *ip = iip->ili_inode;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000582 unsigned short lock_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200584 ASSERT(ip->i_itemp != NULL);
585 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /*
588 * If the inode needed a separate buffer with which to log
589 * its extents, then free it now.
590 */
591 if (iip->ili_extents_buf != NULL) {
592 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
593 ASSERT(ip->i_d.di_nextents > 0);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000594 ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ASSERT(ip->i_df.if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000596 kmem_free(iip->ili_extents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 iip->ili_extents_buf = NULL;
598 }
599 if (iip->ili_aextents_buf != NULL) {
600 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
601 ASSERT(ip->i_d.di_anextents > 0);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000602 ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ASSERT(ip->i_afp->if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000604 kmem_free(iip->ili_aextents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 iip->ili_aextents_buf = NULL;
606 }
607
Christoph Hellwig898621d2010-06-24 11:36:58 +1000608 lock_flags = iip->ili_lock_flags;
609 iip->ili_lock_flags = 0;
Christoph Hellwigddc34152011-09-19 15:00:54 +0000610 if (lock_flags)
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200611 xfs_iunlock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614/*
Dave Chinnerde25c182010-11-30 15:15:46 +1100615 * This is called to find out where the oldest active copy of the inode log
616 * item in the on disk log resides now that the last log write of it completed
617 * at the given lsn. Since we always re-log all dirty data in an inode, the
618 * latest copy in the on disk log is the only one that matters. Therefore,
619 * simply return the given lsn.
620 *
621 * If the inode has been marked stale because the cluster is being freed, we
622 * don't want to (re-)insert this inode into the AIL. There is a race condition
623 * where the cluster buffer may be unpinned before the inode is inserted into
624 * the AIL during transaction committed processing. If the buffer is unpinned
625 * before the inode item has been committed and inserted, then it is possible
Dave Chinner1316d4d2011-07-04 05:27:36 +0000626 * for the buffer to be written and IO completes before the inode is inserted
Dave Chinnerde25c182010-11-30 15:15:46 +1100627 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
628 * AIL which will never get removed. It will, however, get reclaimed which
629 * triggers an assert in xfs_inode_free() complaining about freein an inode
630 * still in the AIL.
631 *
Dave Chinner1316d4d2011-07-04 05:27:36 +0000632 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
633 * transaction committed code knows that it does not need to do any further
634 * processing on the item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636STATIC xfs_lsn_t
637xfs_inode_item_committed(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000638 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 xfs_lsn_t lsn)
640{
Dave Chinnerde25c182010-11-30 15:15:46 +1100641 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
642 struct xfs_inode *ip = iip->ili_inode;
643
Dave Chinner1316d4d2011-07-04 05:27:36 +0000644 if (xfs_iflags_test(ip, XFS_ISTALE)) {
645 xfs_inode_item_unpin(lip, 0);
646 return -1;
647 }
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000648 return lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * XXX rcc - this one really has to do something. Probably needs
653 * to stamp in a new field in the incore inode.
654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655STATIC void
656xfs_inode_item_committing(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000657 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_lsn_t lsn)
659{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000660 INODE_ITEM(lip)->ili_last_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663/*
664 * This is the ops vector shared by all buf log items.
665 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000666static const struct xfs_item_ops xfs_inode_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000667 .iop_size = xfs_inode_item_size,
668 .iop_format = xfs_inode_item_format,
669 .iop_pin = xfs_inode_item_pin,
670 .iop_unpin = xfs_inode_item_unpin,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000671 .iop_unlock = xfs_inode_item_unlock,
672 .iop_committed = xfs_inode_item_committed,
673 .iop_push = xfs_inode_item_push,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000674 .iop_committing = xfs_inode_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675};
676
677
678/*
679 * Initialize the inode log item for a newly allocated (in-core) inode.
680 */
681void
682xfs_inode_item_init(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000683 struct xfs_inode *ip,
684 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000686 struct xfs_inode_log_item *iip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 ASSERT(ip->i_itemp == NULL);
689 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 iip->ili_inode = ip;
Dave Chinner43f5efc2010-03-23 10:10:00 +1100692 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
693 &xfs_inode_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 iip->ili_format.ilf_type = XFS_LI_INODE;
695 iip->ili_format.ilf_ino = ip->i_ino;
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100696 iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
697 iip->ili_format.ilf_len = ip->i_imap.im_len;
698 iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701/*
702 * Free the inode log item and any memory hanging off of it.
703 */
704void
705xfs_inode_item_destroy(
706 xfs_inode_t *ip)
707{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
709}
710
711
712/*
713 * This is the inode flushing I/O completion routine. It is called
714 * from interrupt level when the buffer containing the inode is
715 * flushed to disk. It is responsible for removing the inode item
716 * from the AIL if it has not been re-logged, and unlocking the inode's
717 * flush lock.
Dave Chinner30136832010-12-20 12:03:17 +1100718 *
719 * To reduce AIL lock traffic as much as possible, we scan the buffer log item
720 * list for other inodes that will run this function. We remove them from the
721 * buffer list so we can process all the inode IO completions in one AIL lock
722 * traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724void
725xfs_iflush_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000726 struct xfs_buf *bp,
727 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Dave Chinner30136832010-12-20 12:03:17 +1100729 struct xfs_inode_log_item *iip;
730 struct xfs_log_item *blip;
731 struct xfs_log_item *next;
732 struct xfs_log_item *prev;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000733 struct xfs_ail *ailp = lip->li_ailp;
Dave Chinner30136832010-12-20 12:03:17 +1100734 int need_ail = 0;
735
736 /*
737 * Scan the buffer IO completions for other inodes being completed and
738 * attach them to the current inode log item.
739 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200740 blip = bp->b_fspriv;
Dave Chinner30136832010-12-20 12:03:17 +1100741 prev = NULL;
742 while (blip != NULL) {
743 if (lip->li_cb != xfs_iflush_done) {
744 prev = blip;
745 blip = blip->li_bio_list;
746 continue;
747 }
748
749 /* remove from list */
750 next = blip->li_bio_list;
751 if (!prev) {
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200752 bp->b_fspriv = next;
Dave Chinner30136832010-12-20 12:03:17 +1100753 } else {
754 prev->li_bio_list = next;
755 }
756
757 /* add to current list */
758 blip->li_bio_list = lip->li_bio_list;
759 lip->li_bio_list = blip;
760
761 /*
762 * while we have the item, do the unlocked check for needing
763 * the AIL lock.
764 */
765 iip = INODE_ITEM(blip);
766 if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
767 need_ail++;
768
769 blip = next;
770 }
771
772 /* make sure we capture the state of the initial inode. */
773 iip = INODE_ITEM(lip);
774 if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
775 need_ail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /*
778 * We only want to pull the item from the AIL if it is
779 * actually there and its location in the log has not
780 * changed since we started the flush. Thus, we only bother
781 * if the ili_logged flag is set and the inode's lsn has not
782 * changed. First we check the lsn outside
783 * the lock since it's cheaper, and then we recheck while
784 * holding the lock before removing the inode from the AIL.
785 */
Dave Chinner30136832010-12-20 12:03:17 +1100786 if (need_ail) {
787 struct xfs_log_item *log_items[need_ail];
788 int i = 0;
David Chinner783a2f62008-10-30 17:39:58 +1100789 spin_lock(&ailp->xa_lock);
Dave Chinner30136832010-12-20 12:03:17 +1100790 for (blip = lip; blip; blip = blip->li_bio_list) {
791 iip = INODE_ITEM(blip);
792 if (iip->ili_logged &&
793 blip->li_lsn == iip->ili_flush_lsn) {
794 log_items[i++] = blip;
795 }
796 ASSERT(i <= need_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Dave Chinner30136832010-12-20 12:03:17 +1100798 /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
Dave Chinner04913fd2012-04-23 15:58:41 +1000799 xfs_trans_ail_delete_bulk(ailp, log_items, i,
800 SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /*
Dave Chinner30136832010-12-20 12:03:17 +1100805 * clean up and unlock the flush lock now we are done. We can clear the
806 * ili_last_fields bits now that we know that the data corresponding to
807 * them is safely on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Dave Chinner30136832010-12-20 12:03:17 +1100809 for (blip = lip; blip; blip = next) {
810 next = blip->li_bio_list;
811 blip->li_bio_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Dave Chinner30136832010-12-20 12:03:17 +1100813 iip = INODE_ITEM(blip);
814 iip->ili_logged = 0;
815 iip->ili_last_fields = 0;
816 xfs_ifunlock(iip->ili_inode);
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820/*
Dave Chinner04913fd2012-04-23 15:58:41 +1000821 * This is the inode flushing abort routine. It is called from xfs_iflush when
822 * the filesystem is shutting down to clean up the inode state. It is
823 * responsible for removing the inode item from the AIL if it has not been
824 * re-logged, and unlocking the inode's flush lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826void
827xfs_iflush_abort(
Dave Chinner04913fd2012-04-23 15:58:41 +1000828 xfs_inode_t *ip,
829 bool stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
David Chinner783a2f62008-10-30 17:39:58 +1100831 xfs_inode_log_item_t *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (iip) {
David Chinner783a2f62008-10-30 17:39:58 +1100834 struct xfs_ail *ailp = iip->ili_item.li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100836 spin_lock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100838 /* xfs_trans_ail_delete() drops the AIL lock. */
Dave Chinner04913fd2012-04-23 15:58:41 +1000839 xfs_trans_ail_delete(ailp, &iip->ili_item,
840 stale ?
841 SHUTDOWN_LOG_IO_ERROR :
842 SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else
David Chinner783a2f62008-10-30 17:39:58 +1100844 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 iip->ili_logged = 0;
847 /*
848 * Clear the ili_last_fields bits now that we know that the
849 * data corresponding to them is safely on disk.
850 */
851 iip->ili_last_fields = 0;
852 /*
853 * Clear the inode logging fields so no more flushes are
854 * attempted.
855 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000856 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 /*
859 * Release the inode's flush lock since we're done with it.
860 */
861 xfs_ifunlock(ip);
862}
863
864void
865xfs_istale_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000866 struct xfs_buf *bp,
867 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Dave Chinner04913fd2012-04-23 15:58:41 +1000869 xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Tim Shimmin6d192a92006-06-09 14:55:38 +1000871
872/*
873 * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
874 * (which can have different field alignments) to the native version
875 */
876int
877xfs_inode_item_format_convert(
878 xfs_log_iovec_t *buf,
879 xfs_inode_log_format_t *in_f)
880{
881 if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000882 xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000883
Tim Shimmin6d192a92006-06-09 14:55:38 +1000884 in_f->ilf_type = in_f32->ilf_type;
885 in_f->ilf_size = in_f32->ilf_size;
886 in_f->ilf_fields = in_f32->ilf_fields;
887 in_f->ilf_asize = in_f32->ilf_asize;
888 in_f->ilf_dsize = in_f32->ilf_dsize;
889 in_f->ilf_ino = in_f32->ilf_ino;
890 /* copy biggest field of ilf_u */
891 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
892 in_f32->ilf_u.ilfu_uuid.__u_bits,
893 sizeof(uuid_t));
894 in_f->ilf_blkno = in_f32->ilf_blkno;
895 in_f->ilf_len = in_f32->ilf_len;
896 in_f->ilf_boffset = in_f32->ilf_boffset;
897 return 0;
898 } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000899 xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000900
Tim Shimmin6d192a92006-06-09 14:55:38 +1000901 in_f->ilf_type = in_f64->ilf_type;
902 in_f->ilf_size = in_f64->ilf_size;
903 in_f->ilf_fields = in_f64->ilf_fields;
904 in_f->ilf_asize = in_f64->ilf_asize;
905 in_f->ilf_dsize = in_f64->ilf_dsize;
906 in_f->ilf_ino = in_f64->ilf_ino;
907 /* copy biggest field of ilf_u */
908 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
909 in_f64->ilf_u.ilfu_uuid.__u_bits,
910 sizeof(uuid_t));
911 in_f->ilf_blkno = in_f64->ilf_blkno;
912 in_f->ilf_len = in_f64->ilf_len;
913 in_f->ilf_boffset = in_f64->ilf_boffset;
914 return 0;
915 }
916 return EFSCORRUPTED;
917}