blob: 965d3d083625ee4fc0e28a39b6f2c173c36e5a31 [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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if ((iip->ili_format.ilf_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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if ((iip->ili_format.ilf_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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if ((iip->ili_format.ilf_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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if ((iip->ili_format.ilf_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:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if ((iip->ili_format.ilf_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:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if ((iip->ili_format.ilf_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++;
188 iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
189
190 /*
191 * If this is really an old format inode, then we need to
192 * log it as such. This means that we have to copy the link
193 * count from the new field to the old. We don't have to worry
194 * about the new fields, because nothing trusts them as long as
195 * the old inode version number is there. If the superblock already
196 * has a new version number, then we don't bother converting back.
197 */
198 mp = ip->i_mount;
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100199 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
200 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +1100201 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /*
203 * Convert it back.
204 */
205 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
206 ip->i_d.di_onlink = ip->i_d.di_nlink;
207 } else {
208 /*
209 * The superblock version has already been bumped,
210 * so just make the conversion to the new inode
211 * format permanent.
212 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100213 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ip->i_d.di_onlink = 0;
215 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
216 }
217 }
218
219 switch (ip->i_d.di_format) {
220 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000221 iip->ili_format.ilf_fields &=
222 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
223 XFS_ILOG_DEV | XFS_ILOG_UUID);
224
225 if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) &&
226 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++;
252 nvecs++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000253 } else {
254 iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 break;
257
258 case XFS_DINODE_FMT_BTREE:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000259 iip->ili_format.ilf_fields &=
260 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
261 XFS_ILOG_DEV | XFS_ILOG_UUID);
262
263 if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) &&
264 ip->i_df.if_broot_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ASSERT(ip->i_df.if_broot != NULL);
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000266 vecp->i_addr = ip->i_df.if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 vecp->i_len = ip->i_df.if_broot_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000268 vecp->i_type = XLOG_REG_TYPE_IBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 vecp++;
270 nvecs++;
271 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000272 } else {
273 ASSERT(!(iip->ili_format.ilf_fields &
274 XFS_ILOG_DBROOT));
275#ifdef XFS_TRANS_DEBUG
276 if (iip->ili_root_size > 0) {
277 ASSERT(iip->ili_root_size ==
278 ip->i_df.if_broot_bytes);
279 ASSERT(memcmp(iip->ili_orig_root,
280 ip->i_df.if_broot,
281 iip->ili_root_size) == 0);
282 } else {
283 ASSERT(ip->i_df.if_broot_bytes == 0);
284 }
285#endif
286 iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 break;
289
290 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000291 iip->ili_format.ilf_fields &=
292 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
293 XFS_ILOG_DEV | XFS_ILOG_UUID);
294 if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) &&
295 ip->i_df.if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ASSERT(ip->i_df.if_u1.if_data != NULL);
297 ASSERT(ip->i_d.di_size > 0);
298
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000299 vecp->i_addr = ip->i_df.if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
301 * Round i_bytes up to a word boundary.
302 * The underlying memory is guaranteed to
303 * to be there by xfs_idata_realloc().
304 */
305 data_bytes = roundup(ip->i_df.if_bytes, 4);
306 ASSERT((ip->i_df.if_real_bytes == 0) ||
307 (ip->i_df.if_real_bytes == data_bytes));
308 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000309 vecp->i_type = XLOG_REG_TYPE_ILOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 vecp++;
311 nvecs++;
312 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000313 } else {
314 iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 break;
317
318 case XFS_DINODE_FMT_DEV:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000319 iip->ili_format.ilf_fields &=
320 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
321 XFS_ILOG_DEXT | XFS_ILOG_UUID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
323 iip->ili_format.ilf_u.ilfu_rdev =
324 ip->i_df.if_u2.if_rdev;
325 }
326 break;
327
328 case XFS_DINODE_FMT_UUID:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000329 iip->ili_format.ilf_fields &=
330 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
331 XFS_ILOG_DEXT | XFS_ILOG_DEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
333 iip->ili_format.ilf_u.ilfu_uuid =
334 ip->i_df.if_u2.if_uuid;
335 }
336 break;
337
338 default:
339 ASSERT(0);
340 break;
341 }
342
343 /*
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000344 * If there are no attributes associated with the file, then we're done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
346 if (!XFS_IFORK_Q(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 iip->ili_format.ilf_size = nvecs;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000348 iip->ili_format.ilf_fields &=
349 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return;
351 }
352
353 switch (ip->i_d.di_aformat) {
354 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000355 iip->ili_format.ilf_fields &=
356 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
357
358 if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) &&
359 ip->i_d.di_anextents > 0 &&
360 ip->i_afp->if_bytes > 0) {
361 ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
362 ip->i_d.di_anextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
Nathan Scottf016bad2005-09-08 15:30:05 +1000364#ifdef XFS_NATIVE_HOST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * There are not delayed allocation extents
367 * for attributes, so just point at the array.
368 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000369 vecp->i_addr = ip->i_afp->if_u1.if_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 vecp->i_len = ip->i_afp->if_bytes;
Dave Chinnere8287762011-04-08 12:45:07 +1000371 vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#else
373 ASSERT(iip->ili_aextents_buf == NULL);
Dave Chinnere8287762011-04-08 12:45:07 +1000374 xfs_inode_item_format_extents(ip, vecp,
375 XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif
377 iip->ili_format.ilf_asize = vecp->i_len;
378 vecp++;
379 nvecs++;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000380 } else {
381 iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 break;
384
385 case XFS_DINODE_FMT_BTREE:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000386 iip->ili_format.ilf_fields &=
387 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
388
389 if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) &&
390 ip->i_afp->if_broot_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 ASSERT(ip->i_afp->if_broot != NULL);
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000392
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000393 vecp->i_addr = ip->i_afp->if_broot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 vecp->i_len = ip->i_afp->if_broot_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000395 vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 vecp++;
397 nvecs++;
398 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000399 } else {
400 iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 break;
403
404 case XFS_DINODE_FMT_LOCAL:
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000405 iip->ili_format.ilf_fields &=
406 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
407
408 if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) &&
409 ip->i_afp->if_bytes > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ASSERT(ip->i_afp->if_u1.if_data != NULL);
411
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000412 vecp->i_addr = ip->i_afp->if_u1.if_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
414 * Round i_bytes up to a word boundary.
415 * The underlying memory is guaranteed to
416 * to be there by xfs_idata_realloc().
417 */
418 data_bytes = roundup(ip->i_afp->if_bytes, 4);
419 ASSERT((ip->i_afp->if_real_bytes == 0) ||
420 (ip->i_afp->if_real_bytes == data_bytes));
421 vecp->i_len = (int)data_bytes;
Christoph Hellwig4139b3b2010-01-19 09:56:45 +0000422 vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 vecp++;
424 nvecs++;
425 iip->ili_format.ilf_asize = (unsigned)data_bytes;
Christoph Hellwig339a5f52012-02-29 09:53:53 +0000426 } else {
427 iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 break;
430
431 default:
432 ASSERT(0);
433 break;
434 }
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 iip->ili_format.ilf_size = nvecs;
437}
438
439
440/*
441 * This is called to pin the inode associated with the inode log
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000442 * item in memory so it cannot be written out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
444STATIC void
445xfs_inode_item_pin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000446 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000448 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000449
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000450 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
451
452 trace_xfs_inode_pin(ip, _RET_IP_);
453 atomic_inc(&ip->i_pincount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456
457/*
458 * This is called to unpin the inode associated with the inode log
459 * item which was previously pinned with a call to xfs_inode_item_pin().
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000460 *
461 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463STATIC void
464xfs_inode_item_unpin(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000465 struct xfs_log_item *lip,
Christoph Hellwig9412e312010-06-23 18:11:15 +1000466 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000468 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000469
Dave Chinner4aaf15d2010-03-08 11:24:07 +1100470 trace_xfs_inode_unpin(ip, _RET_IP_);
Christoph Hellwiga14a5ab2010-02-18 12:43:22 +0000471 ASSERT(atomic_read(&ip->i_pincount) > 0);
472 if (atomic_dec_and_test(&ip->i_pincount))
Christoph Hellwigf392e632011-12-18 20:00:10 +0000473 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/*
477 * This is called to attempt to lock the inode associated with this
478 * inode log item, in preparation for the push routine which does the actual
479 * iflush. Don't sleep on the inode lock or the flush lock.
480 *
481 * If the flush lock is already held, indicating that the inode has
482 * been or is in the process of being flushed, then (ideally) we'd like to
483 * see if the inode's buffer is still incore, and if so give it a nudge.
484 * We delay doing so until the pushbuf routine, though, to avoid holding
Nathan Scottc41564b2006-03-29 08:55:14 +1000485 * the AIL lock across a call to the blackhole which is the buffer cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * Also we don't want to sleep in any device strategy routines, which can happen
487 * if we do the subsequent bawrite in here.
488 */
489STATIC uint
490xfs_inode_item_trylock(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000491 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000493 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
494 struct xfs_inode *ip = iip->ili_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000496 if (xfs_ipincount(ip) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return XFS_ITEM_PINNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000499 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return XFS_ITEM_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (!xfs_iflock_nowait(ip)) {
503 /*
Dave Chinnerd808f612010-02-02 10:13:42 +1100504 * inode has already been flushed to the backing buffer,
505 * leave it locked in shared mode, pushbuf routine will
506 * unlock it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Dave Chinnerd808f612010-02-02 10:13:42 +1100508 return XFS_ITEM_PUSHBUF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 /* Stale items should force out the iclog */
512 if (ip->i_flags & XFS_ISTALE) {
513 xfs_ifunlock(ip);
Christoph Hellwig5b03ff12012-02-20 02:31:22 +0000514 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return XFS_ITEM_PINNED;
516 }
517
518#ifdef DEBUG
519 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
520 ASSERT(iip->ili_format.ilf_fields != 0);
521 ASSERT(iip->ili_logged == 0);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000522 ASSERT(lip->li_flags & XFS_LI_IN_AIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524#endif
525 return XFS_ITEM_SUCCESS;
526}
527
528/*
529 * Unlock the inode associated with the inode log item.
530 * Clear the fields of the inode and inode log item that
531 * are specific to the current transaction. If the
532 * hold flags is set, do not unlock the inode.
533 */
534STATIC void
535xfs_inode_item_unlock(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000536 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000538 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
539 struct xfs_inode *ip = iip->ili_inode;
Christoph Hellwig898621d2010-06-24 11:36:58 +1000540 unsigned short lock_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200542 ASSERT(ip->i_itemp != NULL);
543 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * If the inode needed a separate buffer with which to log
547 * its extents, then free it now.
548 */
549 if (iip->ili_extents_buf != NULL) {
550 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
551 ASSERT(ip->i_d.di_nextents > 0);
552 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
553 ASSERT(ip->i_df.if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000554 kmem_free(iip->ili_extents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 iip->ili_extents_buf = NULL;
556 }
557 if (iip->ili_aextents_buf != NULL) {
558 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
559 ASSERT(ip->i_d.di_anextents > 0);
560 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
561 ASSERT(ip->i_afp->if_bytes > 0);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000562 kmem_free(iip->ili_aextents_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 iip->ili_aextents_buf = NULL;
564 }
565
Christoph Hellwig898621d2010-06-24 11:36:58 +1000566 lock_flags = iip->ili_lock_flags;
567 iip->ili_lock_flags = 0;
Christoph Hellwigddc34152011-09-19 15:00:54 +0000568 if (lock_flags)
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200569 xfs_iunlock(ip, lock_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572/*
Dave Chinnerde25c182010-11-30 15:15:46 +1100573 * This is called to find out where the oldest active copy of the inode log
574 * item in the on disk log resides now that the last log write of it completed
575 * at the given lsn. Since we always re-log all dirty data in an inode, the
576 * latest copy in the on disk log is the only one that matters. Therefore,
577 * simply return the given lsn.
578 *
579 * If the inode has been marked stale because the cluster is being freed, we
580 * don't want to (re-)insert this inode into the AIL. There is a race condition
581 * where the cluster buffer may be unpinned before the inode is inserted into
582 * the AIL during transaction committed processing. If the buffer is unpinned
583 * before the inode item has been committed and inserted, then it is possible
Dave Chinner1316d4d2011-07-04 05:27:36 +0000584 * for the buffer to be written and IO completes before the inode is inserted
Dave Chinnerde25c182010-11-30 15:15:46 +1100585 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
586 * AIL which will never get removed. It will, however, get reclaimed which
587 * triggers an assert in xfs_inode_free() complaining about freein an inode
588 * still in the AIL.
589 *
Dave Chinner1316d4d2011-07-04 05:27:36 +0000590 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
591 * transaction committed code knows that it does not need to do any further
592 * processing on the item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594STATIC xfs_lsn_t
595xfs_inode_item_committed(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000596 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_lsn_t lsn)
598{
Dave Chinnerde25c182010-11-30 15:15:46 +1100599 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
600 struct xfs_inode *ip = iip->ili_inode;
601
Dave Chinner1316d4d2011-07-04 05:27:36 +0000602 if (xfs_iflags_test(ip, XFS_ISTALE)) {
603 xfs_inode_item_unpin(lip, 0);
604 return -1;
605 }
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000606 return lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
611 * failed to get the inode flush lock but did get the inode locked SHARED.
612 * Here we're trying to see if the inode buffer is incore, and if so whether it's
Dave Chinnerd808f612010-02-02 10:13:42 +1100613 * marked delayed write. If that's the case, we'll promote it and that will
614 * allow the caller to write the buffer by triggering the xfsbufd to run.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Christoph Hellwig17b38472011-10-11 15:14:09 +0000616STATIC bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617xfs_inode_item_pushbuf(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000618 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000620 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
621 struct xfs_inode *ip = iip->ili_inode;
622 struct xfs_buf *bp;
Christoph Hellwig17b38472011-10-11 15:14:09 +0000623 bool ret = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000625 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /*
David Chinnerc63942d2008-08-13 16:41:16 +1000628 * If a flush is not in progress anymore, chances are that the
629 * inode was taken off the AIL. So, just get out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Christoph Hellwig474fce02011-12-18 20:00:09 +0000631 if (!xfs_isiflocked(ip) ||
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000632 !(lip->li_flags & XFS_LI_IN_AIL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000634 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000637 bp = xfs_incore(ip->i_mount->m_ddev_targp, iip->ili_format.ilf_blkno,
638 iip->ili_format.ilf_len, XBF_TRYLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Dave Chinnerd808f612010-02-02 10:13:42 +1100641 if (!bp)
Christoph Hellwig17b38472011-10-11 15:14:09 +0000642 return true;
Dave Chinnerd808f612010-02-02 10:13:42 +1100643 if (XFS_BUF_ISDELAYWRITE(bp))
644 xfs_buf_delwri_promote(bp);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000645 if (xfs_buf_ispinned(bp))
646 ret = false;
Dave Chinnerd808f612010-02-02 10:13:42 +1100647 xfs_buf_relse(bp);
Christoph Hellwig17b38472011-10-11 15:14:09 +0000648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/*
652 * This is called to asynchronously write the inode associated with this
653 * inode log item out to disk. The inode will already have been locked by
654 * a successful call to xfs_inode_item_trylock().
655 */
656STATIC void
657xfs_inode_item_push(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000658 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000660 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
661 struct xfs_inode *ip = iip->ili_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000663 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +0000664 ASSERT(xfs_isiflocked(ip));
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /*
667 * Since we were able to lock the inode's flush lock and
668 * we found it on the AIL, the inode must be dirty. This
669 * is because the inode is removed from the AIL while still
670 * holding the flush lock in xfs_iflush_done(). Thus, if
671 * we found it in the AIL and were able to obtain the flush
672 * lock without sleeping, then there must not have been
673 * anyone in the process of flushing the inode.
674 */
675 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
676 iip->ili_format.ilf_fields != 0);
677
678 /*
Dave Chinnerc8543632010-02-06 12:39:36 +1100679 * Push the inode to it's backing buffer. This will not remove the
680 * inode from the AIL - a further push will be required to trigger a
681 * buffer push. However, this allows all the dirty inodes to be pushed
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100682 * to the buffer before it is pushed to disk. The buffer IO completion
683 * will pull the inode from the AIL, mark it clean and unlock the flush
Dave Chinnerc8543632010-02-06 12:39:36 +1100684 * lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100686 (void) xfs_iflush(ip, SYNC_TRYLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
691 * XXX rcc - this one really has to do something. Probably needs
692 * to stamp in a new field in the incore inode.
693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694STATIC void
695xfs_inode_item_committing(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000696 struct xfs_log_item *lip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 xfs_lsn_t lsn)
698{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000699 INODE_ITEM(lip)->ili_last_lsn = lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
703 * This is the ops vector shared by all buf log items.
704 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000705static const struct xfs_item_ops xfs_inode_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000706 .iop_size = xfs_inode_item_size,
707 .iop_format = xfs_inode_item_format,
708 .iop_pin = xfs_inode_item_pin,
709 .iop_unpin = xfs_inode_item_unpin,
710 .iop_trylock = xfs_inode_item_trylock,
711 .iop_unlock = xfs_inode_item_unlock,
712 .iop_committed = xfs_inode_item_committed,
713 .iop_push = xfs_inode_item_push,
714 .iop_pushbuf = xfs_inode_item_pushbuf,
715 .iop_committing = xfs_inode_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716};
717
718
719/*
720 * Initialize the inode log item for a newly allocated (in-core) inode.
721 */
722void
723xfs_inode_item_init(
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000724 struct xfs_inode *ip,
725 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000727 struct xfs_inode_log_item *iip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 ASSERT(ip->i_itemp == NULL);
730 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 iip->ili_inode = ip;
Dave Chinner43f5efc2010-03-23 10:10:00 +1100733 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
734 &xfs_inode_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 iip->ili_format.ilf_type = XFS_LI_INODE;
736 iip->ili_format.ilf_ino = ip->i_ino;
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +1100737 iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
738 iip->ili_format.ilf_len = ip->i_imap.im_len;
739 iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743 * Free the inode log item and any memory hanging off of it.
744 */
745void
746xfs_inode_item_destroy(
747 xfs_inode_t *ip)
748{
749#ifdef XFS_TRANS_DEBUG
750 if (ip->i_itemp->ili_root_size != 0) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000751 kmem_free(ip->i_itemp->ili_orig_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753#endif
754 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
755}
756
757
758/*
759 * This is the inode flushing I/O completion routine. It is called
760 * from interrupt level when the buffer containing the inode is
761 * flushed to disk. It is responsible for removing the inode item
762 * from the AIL if it has not been re-logged, and unlocking the inode's
763 * flush lock.
Dave Chinner30136832010-12-20 12:03:17 +1100764 *
765 * To reduce AIL lock traffic as much as possible, we scan the buffer log item
766 * list for other inodes that will run this function. We remove them from the
767 * buffer list so we can process all the inode IO completions in one AIL lock
768 * traversal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770void
771xfs_iflush_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000772 struct xfs_buf *bp,
773 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Dave Chinner30136832010-12-20 12:03:17 +1100775 struct xfs_inode_log_item *iip;
776 struct xfs_log_item *blip;
777 struct xfs_log_item *next;
778 struct xfs_log_item *prev;
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000779 struct xfs_ail *ailp = lip->li_ailp;
Dave Chinner30136832010-12-20 12:03:17 +1100780 int need_ail = 0;
781
782 /*
783 * Scan the buffer IO completions for other inodes being completed and
784 * attach them to the current inode log item.
785 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200786 blip = bp->b_fspriv;
Dave Chinner30136832010-12-20 12:03:17 +1100787 prev = NULL;
788 while (blip != NULL) {
789 if (lip->li_cb != xfs_iflush_done) {
790 prev = blip;
791 blip = blip->li_bio_list;
792 continue;
793 }
794
795 /* remove from list */
796 next = blip->li_bio_list;
797 if (!prev) {
Christoph Hellwigadadbee2011-07-13 13:43:49 +0200798 bp->b_fspriv = next;
Dave Chinner30136832010-12-20 12:03:17 +1100799 } else {
800 prev->li_bio_list = next;
801 }
802
803 /* add to current list */
804 blip->li_bio_list = lip->li_bio_list;
805 lip->li_bio_list = blip;
806
807 /*
808 * while we have the item, do the unlocked check for needing
809 * the AIL lock.
810 */
811 iip = INODE_ITEM(blip);
812 if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
813 need_ail++;
814
815 blip = next;
816 }
817
818 /* make sure we capture the state of the initial inode. */
819 iip = INODE_ITEM(lip);
820 if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
821 need_ail++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * We only want to pull the item from the AIL if it is
825 * actually there and its location in the log has not
826 * changed since we started the flush. Thus, we only bother
827 * if the ili_logged flag is set and the inode's lsn has not
828 * changed. First we check the lsn outside
829 * the lock since it's cheaper, and then we recheck while
830 * holding the lock before removing the inode from the AIL.
831 */
Dave Chinner30136832010-12-20 12:03:17 +1100832 if (need_ail) {
833 struct xfs_log_item *log_items[need_ail];
834 int i = 0;
David Chinner783a2f62008-10-30 17:39:58 +1100835 spin_lock(&ailp->xa_lock);
Dave Chinner30136832010-12-20 12:03:17 +1100836 for (blip = lip; blip; blip = blip->li_bio_list) {
837 iip = INODE_ITEM(blip);
838 if (iip->ili_logged &&
839 blip->li_lsn == iip->ili_flush_lsn) {
840 log_items[i++] = blip;
841 }
842 ASSERT(i <= need_ail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Dave Chinner30136832010-12-20 12:03:17 +1100844 /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
845 xfs_trans_ail_delete_bulk(ailp, log_items, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /*
Dave Chinner30136832010-12-20 12:03:17 +1100850 * clean up and unlock the flush lock now we are done. We can clear the
851 * ili_last_fields bits now that we know that the data corresponding to
852 * them is safely on disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
Dave Chinner30136832010-12-20 12:03:17 +1100854 for (blip = lip; blip; blip = next) {
855 next = blip->li_bio_list;
856 blip->li_bio_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dave Chinner30136832010-12-20 12:03:17 +1100858 iip = INODE_ITEM(blip);
859 iip->ili_logged = 0;
860 iip->ili_last_fields = 0;
861 xfs_ifunlock(iip->ili_inode);
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865/*
866 * This is the inode flushing abort routine. It is called
867 * from xfs_iflush when the filesystem is shutting down to clean
868 * up the inode state.
869 * It is responsible for removing the inode item
870 * from the AIL if it has not been re-logged, and unlocking the inode's
871 * flush lock.
872 */
873void
874xfs_iflush_abort(
875 xfs_inode_t *ip)
876{
David Chinner783a2f62008-10-30 17:39:58 +1100877 xfs_inode_log_item_t *iip = ip->i_itemp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (iip) {
David Chinner783a2f62008-10-30 17:39:58 +1100880 struct xfs_ail *ailp = iip->ili_item.li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100882 spin_lock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
David Chinner783a2f62008-10-30 17:39:58 +1100884 /* xfs_trans_ail_delete() drops the AIL lock. */
885 xfs_trans_ail_delete(ailp, (xfs_log_item_t *)iip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 } else
David Chinner783a2f62008-10-30 17:39:58 +1100887 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 iip->ili_logged = 0;
890 /*
891 * Clear the ili_last_fields bits now that we know that the
892 * data corresponding to them is safely on disk.
893 */
894 iip->ili_last_fields = 0;
895 /*
896 * Clear the inode logging fields so no more flushes are
897 * attempted.
898 */
899 iip->ili_format.ilf_fields = 0;
900 }
901 /*
902 * Release the inode's flush lock since we're done with it.
903 */
904 xfs_ifunlock(ip);
905}
906
907void
908xfs_istale_done(
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000909 struct xfs_buf *bp,
910 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000912 xfs_iflush_abort(INODE_ITEM(lip)->ili_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
Tim Shimmin6d192a92006-06-09 14:55:38 +1000914
915/*
916 * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
917 * (which can have different field alignments) to the native version
918 */
919int
920xfs_inode_item_format_convert(
921 xfs_log_iovec_t *buf,
922 xfs_inode_log_format_t *in_f)
923{
924 if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000925 xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000926
Tim Shimmin6d192a92006-06-09 14:55:38 +1000927 in_f->ilf_type = in_f32->ilf_type;
928 in_f->ilf_size = in_f32->ilf_size;
929 in_f->ilf_fields = in_f32->ilf_fields;
930 in_f->ilf_asize = in_f32->ilf_asize;
931 in_f->ilf_dsize = in_f32->ilf_dsize;
932 in_f->ilf_ino = in_f32->ilf_ino;
933 /* copy biggest field of ilf_u */
934 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
935 in_f32->ilf_u.ilfu_uuid.__u_bits,
936 sizeof(uuid_t));
937 in_f->ilf_blkno = in_f32->ilf_blkno;
938 in_f->ilf_len = in_f32->ilf_len;
939 in_f->ilf_boffset = in_f32->ilf_boffset;
940 return 0;
941 } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000942 xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000943
Tim Shimmin6d192a92006-06-09 14:55:38 +1000944 in_f->ilf_type = in_f64->ilf_type;
945 in_f->ilf_size = in_f64->ilf_size;
946 in_f->ilf_fields = in_f64->ilf_fields;
947 in_f->ilf_asize = in_f64->ilf_asize;
948 in_f->ilf_dsize = in_f64->ilf_dsize;
949 in_f->ilf_ino = in_f64->ilf_ino;
950 /* copy biggest field of ilf_u */
951 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
952 in_f64->ilf_u.ilfu_uuid.__u_bits,
953 sizeof(uuid_t));
954 in_f->ilf_blkno = in_f64->ilf_blkno;
955 in_f->ilf_len = in_f64->ilf_len;
956 in_f->ilf_boffset = in_f64->ilf_boffset;
957 return 0;
958 }
959 return EFSCORRUPTED;
960}