blob: 050d2540f7b4cb07114fa9a56f3290bd9fd566ce [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * This is called to fill in the vector of log iovecs for the
184 * given inode log item. It fills the first item with an inode
185 * log format structure, the second with the on-disk inode structure,
186 * and a possible third and/or fourth with the inode data/extents/b-tree
187 * root and inode attributes data/extents/b-tree root.
188 */
189STATIC void
190xfs_inode_item_format(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000191 struct xfs_log_item *lip,
192 struct xfs_log_iovec *vecp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000194 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
195 struct xfs_inode *ip = iip->ili_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 uint nvecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 size_t data_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 xfs_mount_t *mp;
199
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000200 vecp->i_addr = &iip->ili_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 vecp->i_len = sizeof(xfs_inode_log_format_t);
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000202 vecp->i_type = XLOG_REG_TYPE_IFORMAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 vecp++;
204 nvecs = 1;
205
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000206 vecp->i_addr = &ip->i_d;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100207 vecp->i_len = xfs_icdinode_size(ip->i_d.di_version);
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000208 vecp->i_type = XLOG_REG_TYPE_ICORE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 vecp++;
210 nvecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /*
213 * If this is really an old format inode, then we need to
214 * log it as such. This means that we have to copy the link
215 * count from the new field to the old. We don't have to worry
216 * about the new fields, because nothing trusts them as long as
217 * the old inode version number is there. If the superblock already
218 * has a new version number, then we don't bother converting back.
219 */
220 mp = ip->i_mount;
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100221 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
222 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +1100223 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /*
225 * Convert it back.
226 */
227 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
228 ip->i_d.di_onlink = ip->i_d.di_nlink;
229 } else {
230 /*
231 * The superblock version has already been bumped,
232 * so just make the conversion to the new inode
233 * format permanent.
234 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100235 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ip->i_d.di_onlink = 0;
237 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
238 }
239 }
240
241 switch (ip->i_d.di_format) {
242 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000243 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000244 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
245 XFS_ILOG_DEV | XFS_ILOG_UUID);
246
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000247 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000248 ip->i_d.di_nextents > 0 &&
249 ip->i_df.if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ASSERT(ip->i_df.if_u1.if_extents != NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000251 ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ASSERT(iip->ili_extents_buf == NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000253
Nathan Scottf016bad2005-09-08 15:30:05 +1000254#ifdef XFS_NATIVE_HOST
Dave Chinner696123f2010-07-26 13:51:46 -0500255 if (ip->i_d.di_nextents == ip->i_df.if_bytes /
256 (uint)sizeof(xfs_bmbt_rec_t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /*
258 * There are no delayed allocation
259 * extents, so just point to the
260 * real extents array.
261 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000262 vecp->i_addr = ip->i_df.if_u1.if_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 vecp->i_len = ip->i_df.if_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000264 vecp->i_type = XLOG_REG_TYPE_IEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 } else
266#endif
267 {
Dave Chinnere8287762011-04-08 12:45:07 +1000268 xfs_inode_item_format_extents(ip, vecp,
269 XFS_DATA_FORK, XLOG_REG_TYPE_IEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 ASSERT(vecp->i_len <= ip->i_df.if_bytes);
272 iip->ili_format.ilf_dsize = vecp->i_len;
273 vecp++;
274 nvecs++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000275 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000276 iip->ili_fields &= ~XFS_ILOG_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 break;
279
280 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000281 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000282 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
283 XFS_ILOG_DEV | XFS_ILOG_UUID);
284
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000285 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000286 ip->i_df.if_broot_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 ASSERT(ip->i_df.if_broot != NULL);
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000288 vecp->i_addr = ip->i_df.if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 vecp->i_len = ip->i_df.if_broot_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000290 vecp->i_type = XLOG_REG_TYPE_IBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 vecp++;
292 nvecs++;
293 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000294 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000295 ASSERT(!(iip->ili_fields &
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000296 XFS_ILOG_DBROOT));
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000297 iip->ili_fields &= ~XFS_ILOG_DBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 break;
300
301 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000302 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000303 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
304 XFS_ILOG_DEV | XFS_ILOG_UUID);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000305 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000306 ip->i_df.if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ASSERT(ip->i_df.if_u1.if_data != NULL);
308 ASSERT(ip->i_d.di_size > 0);
309
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000310 vecp->i_addr = ip->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
312 * Round i_bytes up to a word boundary.
313 * The underlying memory is guaranteed to
314 * to be there by xfs_idata_realloc().
315 */
316 data_bytes = roundup(ip->i_df.if_bytes, 4);
317 ASSERT((ip->i_df.if_real_bytes == 0) ||
318 (ip->i_df.if_real_bytes == data_bytes));
319 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000320 vecp->i_type = XLOG_REG_TYPE_ILOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 vecp++;
322 nvecs++;
323 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000324 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000325 iip->ili_fields &= ~XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 break;
328
329 case XFS_DINODE_FMT_DEV:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000330 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000331 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
332 XFS_ILOG_DEXT | XFS_ILOG_UUID);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000333 if (iip->ili_fields & XFS_ILOG_DEV) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 iip->ili_format.ilf_u.ilfu_rdev =
335 ip->i_df.if_u2.if_rdev;
336 }
337 break;
338
339 case XFS_DINODE_FMT_UUID:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000340 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000341 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
342 XFS_ILOG_DEXT | XFS_ILOG_DEV);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000343 if (iip->ili_fields & XFS_ILOG_UUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 iip->ili_format.ilf_u.ilfu_uuid =
345 ip->i_df.if_u2.if_uuid;
346 }
347 break;
348
349 default:
350 ASSERT(0);
351 break;
352 }
353
354 /*
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000355 * If there are no attributes associated with the file, then we're done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
357 if (!XFS_IFORK_Q(ip)) {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000358 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000359 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 switch (ip->i_d.di_aformat) {
364 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000365 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000366 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
367
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000368 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000369 ip->i_d.di_anextents > 0 &&
370 ip->i_afp->if_bytes > 0) {
371 ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
372 ip->i_d.di_anextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
Nathan Scottf016bad2005-09-08 15:30:05 +1000374#ifdef XFS_NATIVE_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * There are not delayed allocation extents
377 * for attributes, so just point at the array.
378 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000379 vecp->i_addr = ip->i_afp->if_u1.if_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 vecp->i_len = ip->i_afp->if_bytes;
Dave Chinnere8287762011-04-08 12:45:07 +1000381 vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#else
383 ASSERT(iip->ili_aextents_buf == NULL);
Dave Chinnere8287762011-04-08 12:45:07 +1000384 xfs_inode_item_format_extents(ip, vecp,
385 XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#endif
387 iip->ili_format.ilf_asize = vecp->i_len;
388 vecp++;
389 nvecs++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000390 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000391 iip->ili_fields &= ~XFS_ILOG_AEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 break;
394
395 case XFS_DINODE_FMT_BTREE:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000396 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000397 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
398
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000399 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000400 ip->i_afp->if_broot_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ASSERT(ip->i_afp->if_broot != NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000402
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000403 vecp->i_addr = ip->i_afp->if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 vecp->i_len = ip->i_afp->if_broot_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000405 vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 vecp++;
407 nvecs++;
408 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000409 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000410 iip->ili_fields &= ~XFS_ILOG_ABROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 break;
413
414 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000415 iip->ili_fields &=
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000416 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
417
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000418 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000419 ip->i_afp->if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ASSERT(ip->i_afp->if_u1.if_data != NULL);
421
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000422 vecp->i_addr = ip->i_afp->if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
424 * Round i_bytes up to a word boundary.
425 * The underlying memory is guaranteed to
426 * to be there by xfs_idata_realloc().
427 */
428 data_bytes = roundup(ip->i_afp->if_bytes, 4);
429 ASSERT((ip->i_afp->if_real_bytes == 0) ||
430 (ip->i_afp->if_real_bytes == data_bytes));
431 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000432 vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 vecp++;
434 nvecs++;
435 iip->ili_format.ilf_asize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000436 } else {
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000437 iip->ili_fields &= ~XFS_ILOG_ADATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 break;
440
441 default:
442 ASSERT(0);
443 break;
444 }
445
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000446out:
447 /*
448 * Now update the log format that goes out to disk from the in-core
449 * values. We always write the inode core to make the arithmetic
450 * games in recovery easier, which isn't a big deal as just about any
451 * transaction would dirty it anyway.
452 */
Christoph Hellwig8f639dd2012-02-29 09:53:55 +0000453 iip->ili_format.ilf_fields = XFS_ILOG_CORE |
454 (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 iip->ili_format.ilf_size = nvecs;
456}
457
458
459/*
460 * This is called to pin the inode associated with the inode log
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000461 * item in memory so it cannot be written out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
463STATIC void
464xfs_inode_item_pin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000465 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000467 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000468
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000469 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
470
471 trace_xfs_inode_pin(ip, _RET_IP_);
472 atomic_inc(&ip->i_pincount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475
476/*
477 * This is called to unpin the inode associated with the inode log
478 * item which was previously pinned with a call to xfs_inode_item_pin().
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000479 *
480 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482STATIC void
483xfs_inode_item_unpin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000484 struct xfs_log_item *lip,
Christoph Hellwig9412e312010-06-23 18:11:15 +1000485 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000487 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000488
Dave Chinner4aaf15d2010-03-08 11:24:07 +1100489 trace_xfs_inode_unpin(ip, _RET_IP_);
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000490 ASSERT(atomic_read(&ip->i_pincount) > 0);
491 if (atomic_dec_and_test(&ip->i_pincount))
Christoph Hellwigf392e632011-12-18 20:00:10 +0000492 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000496xfs_inode_item_push(
497 struct xfs_log_item *lip,
498 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000500 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
501 struct xfs_inode *ip = iip->ili_inode;
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000502 struct xfs_buf *bp = NULL;
503 uint rval = XFS_ITEM_SUCCESS;
504 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000506 if (xfs_ipincount(ip) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return XFS_ITEM_PINNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000509 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return XFS_ITEM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Christoph Hellwig4c468192012-04-23 15:58:36 +1000512 /*
513 * Re-check the pincount now that we stabilized the value by
514 * taking the ilock.
515 */
516 if (xfs_ipincount(ip) > 0) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000517 rval = XFS_ITEM_PINNED;
518 goto out_unlock;
Christoph Hellwig4c468192012-04-23 15:58:36 +1000519 }
520
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000521 /*
Brian Foster9a3a5da2012-06-11 10:39:43 -0400522 * Stale inode items should force out the iclog.
523 */
524 if (ip->i_flags & XFS_ISTALE) {
525 rval = XFS_ITEM_PINNED;
526 goto out_unlock;
527 }
528
529 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000530 * Someone else is already flushing the inode. Nothing we can do
531 * here but wait for the flush to finish and remove the item from
532 * the AIL.
533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (!xfs_iflock_nowait(ip)) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000535 rval = XFS_ITEM_FLUSHING;
536 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000539 ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
540 ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
541
542 spin_unlock(&lip->li_ailp->xa_lock);
543
544 error = xfs_iflush(ip, &bp);
545 if (!error) {
546 if (!xfs_buf_delwri_queue(bp, buffer_list))
547 rval = XFS_ITEM_FLUSHING;
548 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000550
551 spin_lock(&lip->li_ailp->xa_lock);
552out_unlock:
553 xfs_iunlock(ip, XFS_ILOCK_SHARED);
554 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/*
558 * Unlock the inode associated with the inode log item.
559 * Clear the fields of the inode and inode log item that
560 * are specific to the current transaction. If the
561 * hold flags is set, do not unlock the inode.
562 */
563STATIC void
564xfs_inode_item_unlock(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000565 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000567 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
568 struct xfs_inode *ip = iip->ili_inode;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000569 unsigned short lock_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200571 ASSERT(ip->i_itemp != NULL);
572 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /*
575 * If the inode needed a separate buffer with which to log
576 * its extents, then free it now.
577 */
578 if (iip->ili_extents_buf != NULL) {
579 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
580 ASSERT(ip->i_d.di_nextents > 0);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000581 ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ASSERT(ip->i_df.if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000583 kmem_free(iip->ili_extents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 iip->ili_extents_buf = NULL;
585 }
586 if (iip->ili_aextents_buf != NULL) {
587 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
588 ASSERT(ip->i_d.di_anextents > 0);
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000589 ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ASSERT(ip->i_afp->if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000591 kmem_free(iip->ili_aextents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 iip->ili_aextents_buf = NULL;
593 }
594
Christoph Hellwig898621d2010-06-24 11:36:58 +1000595 lock_flags = iip->ili_lock_flags;
596 iip->ili_lock_flags = 0;
Christoph Hellwigddc34152011-09-19 15:00:54 +0000597 if (lock_flags)
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200598 xfs_iunlock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601/*
Dave Chinnerde25c182010-11-30 15:15:46 +1100602 * This is called to find out where the oldest active copy of the inode log
603 * item in the on disk log resides now that the last log write of it completed
604 * at the given lsn. Since we always re-log all dirty data in an inode, the
605 * latest copy in the on disk log is the only one that matters. Therefore,
606 * simply return the given lsn.
607 *
608 * If the inode has been marked stale because the cluster is being freed, we
609 * don't want to (re-)insert this inode into the AIL. There is a race condition
610 * where the cluster buffer may be unpinned before the inode is inserted into
611 * the AIL during transaction committed processing. If the buffer is unpinned
612 * before the inode item has been committed and inserted, then it is possible
Dave Chinner1316d4d2011-07-04 05:27:36 +0000613 * for the buffer to be written and IO completes before the inode is inserted
Dave Chinnerde25c182010-11-30 15:15:46 +1100614 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
615 * AIL which will never get removed. It will, however, get reclaimed which
616 * triggers an assert in xfs_inode_free() complaining about freein an inode
617 * still in the AIL.
618 *
Dave Chinner1316d4d2011-07-04 05:27:36 +0000619 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
620 * transaction committed code knows that it does not need to do any further
621 * processing on the item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623STATIC xfs_lsn_t
624xfs_inode_item_committed(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000625 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 xfs_lsn_t lsn)
627{
Dave Chinnerde25c182010-11-30 15:15:46 +1100628 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
629 struct xfs_inode *ip = iip->ili_inode;
630
Dave Chinner1316d4d2011-07-04 05:27:36 +0000631 if (xfs_iflags_test(ip, XFS_ISTALE)) {
632 xfs_inode_item_unpin(lip, 0);
633 return -1;
634 }
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000635 return lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * XXX rcc - this one really has to do something. Probably needs
640 * to stamp in a new field in the incore inode.
641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642STATIC void
643xfs_inode_item_committing(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000644 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 xfs_lsn_t lsn)
646{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000647 INODE_ITEM(lip)->ili_last_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
651 * This is the ops vector shared by all buf log items.
652 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000653static const struct xfs_item_ops xfs_inode_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000654 .iop_size = xfs_inode_item_size,
655 .iop_format = xfs_inode_item_format,
656 .iop_pin = xfs_inode_item_pin,
657 .iop_unpin = xfs_inode_item_unpin,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000658 .iop_unlock = xfs_inode_item_unlock,
659 .iop_committed = xfs_inode_item_committed,
660 .iop_push = xfs_inode_item_push,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000661 .iop_committing = xfs_inode_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662};
663
664
665/*
666 * Initialize the inode log item for a newly allocated (in-core) inode.
667 */
668void
669xfs_inode_item_init(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000670 struct xfs_inode *ip,
671 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000673 struct xfs_inode_log_item *iip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 ASSERT(ip->i_itemp == NULL);
676 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 iip->ili_inode = ip;
Dave Chinner43f5efc2010-03-23 10:10:00 +1100679 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
680 &xfs_inode_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 iip->ili_format.ilf_type = XFS_LI_INODE;
682 iip->ili_format.ilf_ino = ip->i_ino;
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100683 iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
684 iip->ili_format.ilf_len = ip->i_imap.im_len;
685 iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688/*
689 * Free the inode log item and any memory hanging off of it.
690 */
691void
692xfs_inode_item_destroy(
693 xfs_inode_t *ip)
694{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
696}
697
698
699/*
700 * This is the inode flushing I/O completion routine. It is called
701 * from interrupt level when the buffer containing the inode is
702 * flushed to disk. It is responsible for removing the inode item
703 * from the AIL if it has not been re-logged, and unlocking the inode's
704 * flush lock.
Dave Chinner30136832010-12-20 12:03:17 +1100705 *
706 * To reduce AIL lock traffic as much as possible, we scan the buffer log item
707 * list for other inodes that will run this function. We remove them from the
708 * buffer list so we can process all the inode IO completions in one AIL lock
709 * traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711void
712xfs_iflush_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000713 struct xfs_buf *bp,
714 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Dave Chinner30136832010-12-20 12:03:17 +1100716 struct xfs_inode_log_item *iip;
717 struct xfs_log_item *blip;
718 struct xfs_log_item *next;
719 struct xfs_log_item *prev;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000720 struct xfs_ail *ailp = lip->li_ailp;
Dave Chinner30136832010-12-20 12:03:17 +1100721 int need_ail = 0;
722
723 /*
724 * Scan the buffer IO completions for other inodes being completed and
725 * attach them to the current inode log item.
726 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200727 blip = bp->b_fspriv;
Dave Chinner30136832010-12-20 12:03:17 +1100728 prev = NULL;
729 while (blip != NULL) {
730 if (lip->li_cb != xfs_iflush_done) {
731 prev = blip;
732 blip = blip->li_bio_list;
733 continue;
734 }
735
736 /* remove from list */
737 next = blip->li_bio_list;
738 if (!prev) {
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200739 bp->b_fspriv = next;
Dave Chinner30136832010-12-20 12:03:17 +1100740 } else {
741 prev->li_bio_list = next;
742 }
743
744 /* add to current list */
745 blip->li_bio_list = lip->li_bio_list;
746 lip->li_bio_list = blip;
747
748 /*
749 * while we have the item, do the unlocked check for needing
750 * the AIL lock.
751 */
752 iip = INODE_ITEM(blip);
753 if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
754 need_ail++;
755
756 blip = next;
757 }
758
759 /* make sure we capture the state of the initial inode. */
760 iip = INODE_ITEM(lip);
761 if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
762 need_ail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /*
765 * We only want to pull the item from the AIL if it is
766 * actually there and its location in the log has not
767 * changed since we started the flush. Thus, we only bother
768 * if the ili_logged flag is set and the inode's lsn has not
769 * changed. First we check the lsn outside
770 * the lock since it's cheaper, and then we recheck while
771 * holding the lock before removing the inode from the AIL.
772 */
Dave Chinner30136832010-12-20 12:03:17 +1100773 if (need_ail) {
774 struct xfs_log_item *log_items[need_ail];
775 int i = 0;
David Chinner783a2f62008-10-30 17:39:58 +1100776 spin_lock(&ailp->xa_lock);
Dave Chinner30136832010-12-20 12:03:17 +1100777 for (blip = lip; blip; blip = blip->li_bio_list) {
778 iip = INODE_ITEM(blip);
779 if (iip->ili_logged &&
780 blip->li_lsn == iip->ili_flush_lsn) {
781 log_items[i++] = blip;
782 }
783 ASSERT(i <= need_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Dave Chinner30136832010-12-20 12:03:17 +1100785 /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
Dave Chinner04913fd2012-04-23 15:58:41 +1000786 xfs_trans_ail_delete_bulk(ailp, log_items, i,
787 SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /*
Dave Chinner30136832010-12-20 12:03:17 +1100792 * clean up and unlock the flush lock now we are done. We can clear the
793 * ili_last_fields bits now that we know that the data corresponding to
794 * them is safely on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Dave Chinner30136832010-12-20 12:03:17 +1100796 for (blip = lip; blip; blip = next) {
797 next = blip->li_bio_list;
798 blip->li_bio_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Chinner30136832010-12-20 12:03:17 +1100800 iip = INODE_ITEM(blip);
801 iip->ili_logged = 0;
802 iip->ili_last_fields = 0;
803 xfs_ifunlock(iip->ili_inode);
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807/*
Dave Chinner04913fd2012-04-23 15:58:41 +1000808 * This is the inode flushing abort routine. It is called from xfs_iflush when
809 * the filesystem is shutting down to clean up the inode state. It is
810 * responsible for removing the inode item from the AIL if it has not been
811 * re-logged, and unlocking the inode's flush lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
813void
814xfs_iflush_abort(
Dave Chinner04913fd2012-04-23 15:58:41 +1000815 xfs_inode_t *ip,
816 bool stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
David Chinner783a2f62008-10-30 17:39:58 +1100818 xfs_inode_log_item_t *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (iip) {
David Chinner783a2f62008-10-30 17:39:58 +1100821 struct xfs_ail *ailp = iip->ili_item.li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100823 spin_lock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100825 /* xfs_trans_ail_delete() drops the AIL lock. */
Dave Chinner04913fd2012-04-23 15:58:41 +1000826 xfs_trans_ail_delete(ailp, &iip->ili_item,
827 stale ?
828 SHUTDOWN_LOG_IO_ERROR :
829 SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 } else
David Chinner783a2f62008-10-30 17:39:58 +1100831 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833 iip->ili_logged = 0;
834 /*
835 * Clear the ili_last_fields bits now that we know that the
836 * data corresponding to them is safely on disk.
837 */
838 iip->ili_last_fields = 0;
839 /*
840 * Clear the inode logging fields so no more flushes are
841 * attempted.
842 */
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +0000843 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 /*
846 * Release the inode's flush lock since we're done with it.
847 */
848 xfs_ifunlock(ip);
849}
850
851void
852xfs_istale_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000853 struct xfs_buf *bp,
854 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Dave Chinner04913fd2012-04-23 15:58:41 +1000856 xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
Tim Shimmin6d192a92006-06-09 14:55:38 +1000858
859/*
860 * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
861 * (which can have different field alignments) to the native version
862 */
863int
864xfs_inode_item_format_convert(
865 xfs_log_iovec_t *buf,
866 xfs_inode_log_format_t *in_f)
867{
868 if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000869 xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000870
Tim Shimmin6d192a92006-06-09 14:55:38 +1000871 in_f->ilf_type = in_f32->ilf_type;
872 in_f->ilf_size = in_f32->ilf_size;
873 in_f->ilf_fields = in_f32->ilf_fields;
874 in_f->ilf_asize = in_f32->ilf_asize;
875 in_f->ilf_dsize = in_f32->ilf_dsize;
876 in_f->ilf_ino = in_f32->ilf_ino;
877 /* copy biggest field of ilf_u */
878 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
879 in_f32->ilf_u.ilfu_uuid.__u_bits,
880 sizeof(uuid_t));
881 in_f->ilf_blkno = in_f32->ilf_blkno;
882 in_f->ilf_len = in_f32->ilf_len;
883 in_f->ilf_boffset = in_f32->ilf_boffset;
884 return 0;
885 } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000886 xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000887
Tim Shimmin6d192a92006-06-09 14:55:38 +1000888 in_f->ilf_type = in_f64->ilf_type;
889 in_f->ilf_size = in_f64->ilf_size;
890 in_f->ilf_fields = in_f64->ilf_fields;
891 in_f->ilf_asize = in_f64->ilf_asize;
892 in_f->ilf_dsize = in_f64->ilf_dsize;
893 in_f->ilf_ino = in_f64->ilf_ino;
894 /* copy biggest field of ilf_u */
895 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
896 in_f64->ilf_u.ilfu_uuid.__u_bits,
897 sizeof(uuid_t));
898 in_f->ilf_blkno = in_f64->ilf_blkno;
899 in_f->ilf_len = in_f64->ilf_len;
900 in_f->ilf_boffset = in_f64->ilf_boffset;
901 return 0;
902 }
903 return EFSCORRUPTED;
904}