xfs: move xfsagino_t to xfs_types.h
[deliverable/linux.git] / fs / xfs / xfs_inode_item.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4
LT
22#include "xfs_log.h"
23#include "xfs_trans.h"
1da177e4 24#include "xfs_sb.h"
a844f451 25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_mount.h"
27#include "xfs_trans_priv.h"
1da177e4 28#include "xfs_bmap_btree.h"
1da177e4 29#include "xfs_dinode.h"
1da177e4 30#include "xfs_inode.h"
a844f451 31#include "xfs_inode_item.h"
db7a19f2 32#include "xfs_error.h"
0b1b213f 33#include "xfs_trace.h"
1da177e4
LT
34
35
36kmem_zone_t *xfs_ili_zone; /* inode log item zone */
37
7bfa31d8
CH
38static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
39{
40 return container_of(lip, struct xfs_inode_log_item, ili_item);
41}
42
43
1da177e4
LT
44/*
45 * This returns the number of iovecs needed to log the given inode item.
46 *
47 * We need one iovec for the inode log format structure, one for the
48 * inode core, and possibly one for the inode data/extents/b-tree root
49 * and one for the inode attribute data/extents/b-tree root.
50 */
51STATIC uint
52xfs_inode_item_size(
7bfa31d8 53 struct xfs_log_item *lip)
1da177e4 54{
7bfa31d8
CH
55 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
56 struct xfs_inode *ip = iip->ili_inode;
57 uint nvecs = 2;
1da177e4 58
1da177e4
LT
59 switch (ip->i_d.di_format) {
60 case XFS_DINODE_FMT_EXTENTS:
f5d8d5c4 61 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
339a5f5d
CH
62 ip->i_d.di_nextents > 0 &&
63 ip->i_df.if_bytes > 0)
1da177e4 64 nvecs++;
1da177e4
LT
65 break;
66
67 case XFS_DINODE_FMT_BTREE:
f5d8d5c4 68 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
339a5f5d 69 ip->i_df.if_broot_bytes > 0)
1da177e4 70 nvecs++;
1da177e4
LT
71 break;
72
73 case XFS_DINODE_FMT_LOCAL:
f5d8d5c4 74 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
339a5f5d 75 ip->i_df.if_bytes > 0)
1da177e4 76 nvecs++;
1da177e4
LT
77 break;
78
79 case XFS_DINODE_FMT_DEV:
1da177e4 80 case XFS_DINODE_FMT_UUID:
1da177e4
LT
81 break;
82
83 default:
84 ASSERT(0);
85 break;
86 }
87
339a5f5d 88 if (!XFS_IFORK_Q(ip))
1da177e4 89 return nvecs;
339a5f5d 90
1da177e4
LT
91
92 /*
93 * Log any necessary attribute data.
94 */
95 switch (ip->i_d.di_aformat) {
96 case XFS_DINODE_FMT_EXTENTS:
f5d8d5c4 97 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
339a5f5d
CH
98 ip->i_d.di_anextents > 0 &&
99 ip->i_afp->if_bytes > 0)
1da177e4 100 nvecs++;
1da177e4
LT
101 break;
102
103 case XFS_DINODE_FMT_BTREE:
f5d8d5c4 104 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
339a5f5d 105 ip->i_afp->if_broot_bytes > 0)
1da177e4 106 nvecs++;
1da177e4
LT
107 break;
108
109 case XFS_DINODE_FMT_LOCAL:
f5d8d5c4 110 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
339a5f5d 111 ip->i_afp->if_bytes > 0)
1da177e4 112 nvecs++;
1da177e4
LT
113 break;
114
115 default:
116 ASSERT(0);
117 break;
118 }
119
120 return nvecs;
121}
122
e828776a
DC
123/*
124 * xfs_inode_item_format_extents - convert in-core extents to on-disk form
125 *
126 * For either the data or attr fork in extent format, we need to endian convert
127 * the in-core extent as we place them into the on-disk inode. In this case, we
128 * need to do this conversion before we write the extents into the log. Because
129 * we don't have the disk inode to write into here, we allocate a buffer and
130 * format the extents into it via xfs_iextents_copy(). We free the buffer in
131 * the unlock routine after the copy for the log has been made.
132 *
133 * In the case of the data fork, the in-core and on-disk fork sizes can be
134 * different due to delayed allocation extents. We only log on-disk extents
135 * here, so always use the physical fork size to determine the size of the
136 * buffer we need to allocate.
137 */
138STATIC void
139xfs_inode_item_format_extents(
140 struct xfs_inode *ip,
141 struct xfs_log_iovec *vecp,
142 int whichfork,
143 int type)
144{
145 xfs_bmbt_rec_t *ext_buffer;
146
147 ext_buffer = kmem_alloc(XFS_IFORK_SIZE(ip, whichfork), KM_SLEEP);
148 if (whichfork == XFS_DATA_FORK)
149 ip->i_itemp->ili_extents_buf = ext_buffer;
150 else
151 ip->i_itemp->ili_aextents_buf = ext_buffer;
152
153 vecp->i_addr = ext_buffer;
154 vecp->i_len = xfs_iextents_copy(ip, ext_buffer, whichfork);
155 vecp->i_type = type;
156}
157
1da177e4
LT
158/*
159 * This is called to fill in the vector of log iovecs for the
160 * given inode log item. It fills the first item with an inode
161 * log format structure, the second with the on-disk inode structure,
162 * and a possible third and/or fourth with the inode data/extents/b-tree
163 * root and inode attributes data/extents/b-tree root.
164 */
165STATIC void
166xfs_inode_item_format(
7bfa31d8
CH
167 struct xfs_log_item *lip,
168 struct xfs_log_iovec *vecp)
1da177e4 169{
7bfa31d8
CH
170 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
171 struct xfs_inode *ip = iip->ili_inode;
1da177e4 172 uint nvecs;
1da177e4 173 size_t data_bytes;
1da177e4
LT
174 xfs_mount_t *mp;
175
4e0d5f92 176 vecp->i_addr = &iip->ili_format;
1da177e4 177 vecp->i_len = sizeof(xfs_inode_log_format_t);
4139b3b3 178 vecp->i_type = XLOG_REG_TYPE_IFORMAT;
1da177e4
LT
179 vecp++;
180 nvecs = 1;
181
4e0d5f92 182 vecp->i_addr = &ip->i_d;
81591fe2 183 vecp->i_len = sizeof(struct xfs_icdinode);
4139b3b3 184 vecp->i_type = XLOG_REG_TYPE_ICORE;
1da177e4
LT
185 vecp++;
186 nvecs++;
1da177e4
LT
187
188 /*
189 * If this is really an old format inode, then we need to
190 * log it as such. This means that we have to copy the link
191 * count from the new field to the old. We don't have to worry
192 * about the new fields, because nothing trusts them as long as
193 * the old inode version number is there. If the superblock already
194 * has a new version number, then we don't bother converting back.
195 */
196 mp = ip->i_mount;
51ce16d5
CH
197 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
198 if (ip->i_d.di_version == 1) {
62118709 199 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1da177e4
LT
200 /*
201 * Convert it back.
202 */
203 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
204 ip->i_d.di_onlink = ip->i_d.di_nlink;
205 } else {
206 /*
207 * The superblock version has already been bumped,
208 * so just make the conversion to the new inode
209 * format permanent.
210 */
51ce16d5 211 ip->i_d.di_version = 2;
1da177e4
LT
212 ip->i_d.di_onlink = 0;
213 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
214 }
215 }
216
217 switch (ip->i_d.di_format) {
218 case XFS_DINODE_FMT_EXTENTS:
f5d8d5c4 219 iip->ili_fields &=
339a5f5d
CH
220 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
221 XFS_ILOG_DEV | XFS_ILOG_UUID);
222
f5d8d5c4 223 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
339a5f5d
CH
224 ip->i_d.di_nextents > 0 &&
225 ip->i_df.if_bytes > 0) {
1da177e4 226 ASSERT(ip->i_df.if_u1.if_extents != NULL);
339a5f5d 227 ASSERT(ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) > 0);
1da177e4 228 ASSERT(iip->ili_extents_buf == NULL);
339a5f5d 229
f016bad6 230#ifdef XFS_NATIVE_HOST
696123fc
DC
231 if (ip->i_d.di_nextents == ip->i_df.if_bytes /
232 (uint)sizeof(xfs_bmbt_rec_t)) {
1da177e4
LT
233 /*
234 * There are no delayed allocation
235 * extents, so just point to the
236 * real extents array.
237 */
4e0d5f92 238 vecp->i_addr = ip->i_df.if_u1.if_extents;
1da177e4 239 vecp->i_len = ip->i_df.if_bytes;
4139b3b3 240 vecp->i_type = XLOG_REG_TYPE_IEXT;
1da177e4
LT
241 } else
242#endif
243 {
e828776a
DC
244 xfs_inode_item_format_extents(ip, vecp,
245 XFS_DATA_FORK, XLOG_REG_TYPE_IEXT);
1da177e4
LT
246 }
247 ASSERT(vecp->i_len <= ip->i_df.if_bytes);
248 iip->ili_format.ilf_dsize = vecp->i_len;
249 vecp++;
250 nvecs++;
339a5f5d 251 } else {
f5d8d5c4 252 iip->ili_fields &= ~XFS_ILOG_DEXT;
1da177e4
LT
253 }
254 break;
255
256 case XFS_DINODE_FMT_BTREE:
f5d8d5c4 257 iip->ili_fields &=
339a5f5d
CH
258 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
259 XFS_ILOG_DEV | XFS_ILOG_UUID);
260
f5d8d5c4 261 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
339a5f5d 262 ip->i_df.if_broot_bytes > 0) {
1da177e4 263 ASSERT(ip->i_df.if_broot != NULL);
4e0d5f92 264 vecp->i_addr = ip->i_df.if_broot;
1da177e4 265 vecp->i_len = ip->i_df.if_broot_bytes;
4139b3b3 266 vecp->i_type = XLOG_REG_TYPE_IBROOT;
1da177e4
LT
267 vecp++;
268 nvecs++;
269 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
339a5f5d 270 } else {
f5d8d5c4 271 ASSERT(!(iip->ili_fields &
339a5f5d
CH
272 XFS_ILOG_DBROOT));
273#ifdef XFS_TRANS_DEBUG
274 if (iip->ili_root_size > 0) {
275 ASSERT(iip->ili_root_size ==
276 ip->i_df.if_broot_bytes);
277 ASSERT(memcmp(iip->ili_orig_root,
278 ip->i_df.if_broot,
279 iip->ili_root_size) == 0);
280 } else {
281 ASSERT(ip->i_df.if_broot_bytes == 0);
282 }
283#endif
f5d8d5c4 284 iip->ili_fields &= ~XFS_ILOG_DBROOT;
1da177e4
LT
285 }
286 break;
287
288 case XFS_DINODE_FMT_LOCAL:
f5d8d5c4 289 iip->ili_fields &=
339a5f5d
CH
290 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
291 XFS_ILOG_DEV | XFS_ILOG_UUID);
f5d8d5c4 292 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
339a5f5d 293 ip->i_df.if_bytes > 0) {
1da177e4
LT
294 ASSERT(ip->i_df.if_u1.if_data != NULL);
295 ASSERT(ip->i_d.di_size > 0);
296
4e0d5f92 297 vecp->i_addr = ip->i_df.if_u1.if_data;
1da177e4
LT
298 /*
299 * Round i_bytes up to a word boundary.
300 * The underlying memory is guaranteed to
301 * to be there by xfs_idata_realloc().
302 */
303 data_bytes = roundup(ip->i_df.if_bytes, 4);
304 ASSERT((ip->i_df.if_real_bytes == 0) ||
305 (ip->i_df.if_real_bytes == data_bytes));
306 vecp->i_len = (int)data_bytes;
4139b3b3 307 vecp->i_type = XLOG_REG_TYPE_ILOCAL;
1da177e4
LT
308 vecp++;
309 nvecs++;
310 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
339a5f5d 311 } else {
f5d8d5c4 312 iip->ili_fields &= ~XFS_ILOG_DDATA;
1da177e4
LT
313 }
314 break;
315
316 case XFS_DINODE_FMT_DEV:
f5d8d5c4 317 iip->ili_fields &=
339a5f5d
CH
318 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
319 XFS_ILOG_DEXT | XFS_ILOG_UUID);
f5d8d5c4 320 if (iip->ili_fields & XFS_ILOG_DEV) {
1da177e4
LT
321 iip->ili_format.ilf_u.ilfu_rdev =
322 ip->i_df.if_u2.if_rdev;
323 }
324 break;
325
326 case XFS_DINODE_FMT_UUID:
f5d8d5c4 327 iip->ili_fields &=
339a5f5d
CH
328 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
329 XFS_ILOG_DEXT | XFS_ILOG_DEV);
f5d8d5c4 330 if (iip->ili_fields & XFS_ILOG_UUID) {
1da177e4
LT
331 iip->ili_format.ilf_u.ilfu_uuid =
332 ip->i_df.if_u2.if_uuid;
333 }
334 break;
335
336 default:
337 ASSERT(0);
338 break;
339 }
340
341 /*
339a5f5d 342 * If there are no attributes associated with the file, then we're done.
1da177e4
LT
343 */
344 if (!XFS_IFORK_Q(ip)) {
f5d8d5c4 345 iip->ili_fields &=
339a5f5d 346 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
f5d8d5c4 347 goto out;
1da177e4
LT
348 }
349
350 switch (ip->i_d.di_aformat) {
351 case XFS_DINODE_FMT_EXTENTS:
f5d8d5c4 352 iip->ili_fields &=
339a5f5d
CH
353 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
354
f5d8d5c4 355 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
339a5f5d
CH
356 ip->i_d.di_anextents > 0 &&
357 ip->i_afp->if_bytes > 0) {
358 ASSERT(ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) ==
359 ip->i_d.di_anextents);
73523a2e 360 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
f016bad6 361#ifdef XFS_NATIVE_HOST
1da177e4
LT
362 /*
363 * There are not delayed allocation extents
364 * for attributes, so just point at the array.
365 */
4e0d5f92 366 vecp->i_addr = ip->i_afp->if_u1.if_extents;
1da177e4 367 vecp->i_len = ip->i_afp->if_bytes;
e828776a 368 vecp->i_type = XLOG_REG_TYPE_IATTR_EXT;
1da177e4
LT
369#else
370 ASSERT(iip->ili_aextents_buf == NULL);
e828776a
DC
371 xfs_inode_item_format_extents(ip, vecp,
372 XFS_ATTR_FORK, XLOG_REG_TYPE_IATTR_EXT);
1da177e4
LT
373#endif
374 iip->ili_format.ilf_asize = vecp->i_len;
375 vecp++;
376 nvecs++;
339a5f5d 377 } else {
f5d8d5c4 378 iip->ili_fields &= ~XFS_ILOG_AEXT;
1da177e4
LT
379 }
380 break;
381
382 case XFS_DINODE_FMT_BTREE:
f5d8d5c4 383 iip->ili_fields &=
339a5f5d
CH
384 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
385
f5d8d5c4 386 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
339a5f5d 387 ip->i_afp->if_broot_bytes > 0) {
1da177e4 388 ASSERT(ip->i_afp->if_broot != NULL);
339a5f5d 389
4e0d5f92 390 vecp->i_addr = ip->i_afp->if_broot;
1da177e4 391 vecp->i_len = ip->i_afp->if_broot_bytes;
4139b3b3 392 vecp->i_type = XLOG_REG_TYPE_IATTR_BROOT;
1da177e4
LT
393 vecp++;
394 nvecs++;
395 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
339a5f5d 396 } else {
f5d8d5c4 397 iip->ili_fields &= ~XFS_ILOG_ABROOT;
1da177e4
LT
398 }
399 break;
400
401 case XFS_DINODE_FMT_LOCAL:
f5d8d5c4 402 iip->ili_fields &=
339a5f5d
CH
403 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
404
f5d8d5c4 405 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
339a5f5d 406 ip->i_afp->if_bytes > 0) {
1da177e4
LT
407 ASSERT(ip->i_afp->if_u1.if_data != NULL);
408
4e0d5f92 409 vecp->i_addr = ip->i_afp->if_u1.if_data;
1da177e4
LT
410 /*
411 * Round i_bytes up to a word boundary.
412 * The underlying memory is guaranteed to
413 * to be there by xfs_idata_realloc().
414 */
415 data_bytes = roundup(ip->i_afp->if_bytes, 4);
416 ASSERT((ip->i_afp->if_real_bytes == 0) ||
417 (ip->i_afp->if_real_bytes == data_bytes));
418 vecp->i_len = (int)data_bytes;
4139b3b3 419 vecp->i_type = XLOG_REG_TYPE_IATTR_LOCAL;
1da177e4
LT
420 vecp++;
421 nvecs++;
422 iip->ili_format.ilf_asize = (unsigned)data_bytes;
339a5f5d 423 } else {
f5d8d5c4 424 iip->ili_fields &= ~XFS_ILOG_ADATA;
1da177e4
LT
425 }
426 break;
427
428 default:
429 ASSERT(0);
430 break;
431 }
432
f5d8d5c4
CH
433out:
434 /*
435 * Now update the log format that goes out to disk from the in-core
436 * values. We always write the inode core to make the arithmetic
437 * games in recovery easier, which isn't a big deal as just about any
438 * transaction would dirty it anyway.
439 */
8f639dde
CH
440 iip->ili_format.ilf_fields = XFS_ILOG_CORE |
441 (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
1da177e4
LT
442 iip->ili_format.ilf_size = nvecs;
443}
444
445
446/*
447 * This is called to pin the inode associated with the inode log
a14a5ab5 448 * item in memory so it cannot be written out.
1da177e4
LT
449 */
450STATIC void
451xfs_inode_item_pin(
7bfa31d8 452 struct xfs_log_item *lip)
1da177e4 453{
7bfa31d8 454 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
a14a5ab5 455
7bfa31d8
CH
456 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
457
458 trace_xfs_inode_pin(ip, _RET_IP_);
459 atomic_inc(&ip->i_pincount);
1da177e4
LT
460}
461
462
463/*
464 * This is called to unpin the inode associated with the inode log
465 * item which was previously pinned with a call to xfs_inode_item_pin().
a14a5ab5
CH
466 *
467 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
1da177e4 468 */
1da177e4
LT
469STATIC void
470xfs_inode_item_unpin(
7bfa31d8 471 struct xfs_log_item *lip,
9412e318 472 int remove)
1da177e4 473{
7bfa31d8 474 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode;
a14a5ab5 475
4aaf15d1 476 trace_xfs_inode_unpin(ip, _RET_IP_);
a14a5ab5
CH
477 ASSERT(atomic_read(&ip->i_pincount) > 0);
478 if (atomic_dec_and_test(&ip->i_pincount))
f392e631 479 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
1da177e4
LT
480}
481
1da177e4 482STATIC uint
43ff2122
CH
483xfs_inode_item_push(
484 struct xfs_log_item *lip,
485 struct list_head *buffer_list)
1da177e4 486{
7bfa31d8
CH
487 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
488 struct xfs_inode *ip = iip->ili_inode;
43ff2122
CH
489 struct xfs_buf *bp = NULL;
490 uint rval = XFS_ITEM_SUCCESS;
491 int error;
1da177e4 492
7bfa31d8 493 if (xfs_ipincount(ip) > 0)
1da177e4 494 return XFS_ITEM_PINNED;
1da177e4 495
7bfa31d8 496 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
1da177e4 497 return XFS_ITEM_LOCKED;
1da177e4 498
4c46819a
CH
499 /*
500 * Re-check the pincount now that we stabilized the value by
501 * taking the ilock.
502 */
503 if (xfs_ipincount(ip) > 0) {
43ff2122
CH
504 rval = XFS_ITEM_PINNED;
505 goto out_unlock;
4c46819a
CH
506 }
507
43ff2122
CH
508 /*
509 * Someone else is already flushing the inode. Nothing we can do
510 * here but wait for the flush to finish and remove the item from
511 * the AIL.
512 */
1da177e4 513 if (!xfs_iflock_nowait(ip)) {
43ff2122
CH
514 rval = XFS_ITEM_FLUSHING;
515 goto out_unlock;
1da177e4
LT
516 }
517
43ff2122
CH
518 /*
519 * Stale inode items should force out the iclog.
520 */
1da177e4
LT
521 if (ip->i_flags & XFS_ISTALE) {
522 xfs_ifunlock(ip);
5b03ff1b 523 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1da177e4
LT
524 return XFS_ITEM_PINNED;
525 }
526
43ff2122
CH
527 ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
528 ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
529
530 spin_unlock(&lip->li_ailp->xa_lock);
531
532 error = xfs_iflush(ip, &bp);
533 if (!error) {
534 if (!xfs_buf_delwri_queue(bp, buffer_list))
535 rval = XFS_ITEM_FLUSHING;
536 xfs_buf_relse(bp);
1da177e4 537 }
43ff2122
CH
538
539 spin_lock(&lip->li_ailp->xa_lock);
540out_unlock:
541 xfs_iunlock(ip, XFS_ILOCK_SHARED);
542 return rval;
1da177e4
LT
543}
544
545/*
546 * Unlock the inode associated with the inode log item.
547 * Clear the fields of the inode and inode log item that
548 * are specific to the current transaction. If the
549 * hold flags is set, do not unlock the inode.
550 */
551STATIC void
552xfs_inode_item_unlock(
7bfa31d8 553 struct xfs_log_item *lip)
1da177e4 554{
7bfa31d8
CH
555 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
556 struct xfs_inode *ip = iip->ili_inode;
898621d5 557 unsigned short lock_flags;
1da177e4 558
f3ca8738
CH
559 ASSERT(ip->i_itemp != NULL);
560 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4
LT
561
562 /*
563 * If the inode needed a separate buffer with which to log
564 * its extents, then free it now.
565 */
566 if (iip->ili_extents_buf != NULL) {
567 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
568 ASSERT(ip->i_d.di_nextents > 0);
f5d8d5c4 569 ASSERT(iip->ili_fields & XFS_ILOG_DEXT);
1da177e4 570 ASSERT(ip->i_df.if_bytes > 0);
f0e2d93c 571 kmem_free(iip->ili_extents_buf);
1da177e4
LT
572 iip->ili_extents_buf = NULL;
573 }
574 if (iip->ili_aextents_buf != NULL) {
575 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
576 ASSERT(ip->i_d.di_anextents > 0);
f5d8d5c4 577 ASSERT(iip->ili_fields & XFS_ILOG_AEXT);
1da177e4 578 ASSERT(ip->i_afp->if_bytes > 0);
f0e2d93c 579 kmem_free(iip->ili_aextents_buf);
1da177e4
LT
580 iip->ili_aextents_buf = NULL;
581 }
582
898621d5
CH
583 lock_flags = iip->ili_lock_flags;
584 iip->ili_lock_flags = 0;
ddc3415a 585 if (lock_flags)
f3ca8738 586 xfs_iunlock(ip, lock_flags);
1da177e4
LT
587}
588
589/*
de25c181
DC
590 * This is called to find out where the oldest active copy of the inode log
591 * item in the on disk log resides now that the last log write of it completed
592 * at the given lsn. Since we always re-log all dirty data in an inode, the
593 * latest copy in the on disk log is the only one that matters. Therefore,
594 * simply return the given lsn.
595 *
596 * If the inode has been marked stale because the cluster is being freed, we
597 * don't want to (re-)insert this inode into the AIL. There is a race condition
598 * where the cluster buffer may be unpinned before the inode is inserted into
599 * the AIL during transaction committed processing. If the buffer is unpinned
600 * before the inode item has been committed and inserted, then it is possible
1316d4da 601 * for the buffer to be written and IO completes before the inode is inserted
de25c181
DC
602 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
603 * AIL which will never get removed. It will, however, get reclaimed which
604 * triggers an assert in xfs_inode_free() complaining about freein an inode
605 * still in the AIL.
606 *
1316d4da
DC
607 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
608 * transaction committed code knows that it does not need to do any further
609 * processing on the item.
1da177e4 610 */
1da177e4
LT
611STATIC xfs_lsn_t
612xfs_inode_item_committed(
7bfa31d8 613 struct xfs_log_item *lip,
1da177e4
LT
614 xfs_lsn_t lsn)
615{
de25c181
DC
616 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
617 struct xfs_inode *ip = iip->ili_inode;
618
1316d4da
DC
619 if (xfs_iflags_test(ip, XFS_ISTALE)) {
620 xfs_inode_item_unpin(lip, 0);
621 return -1;
622 }
7bfa31d8 623 return lsn;
1da177e4
LT
624}
625
1da177e4
LT
626/*
627 * XXX rcc - this one really has to do something. Probably needs
628 * to stamp in a new field in the incore inode.
629 */
1da177e4
LT
630STATIC void
631xfs_inode_item_committing(
7bfa31d8 632 struct xfs_log_item *lip,
1da177e4
LT
633 xfs_lsn_t lsn)
634{
7bfa31d8 635 INODE_ITEM(lip)->ili_last_lsn = lsn;
1da177e4
LT
636}
637
638/*
639 * This is the ops vector shared by all buf log items.
640 */
272e42b2 641static const struct xfs_item_ops xfs_inode_item_ops = {
7bfa31d8
CH
642 .iop_size = xfs_inode_item_size,
643 .iop_format = xfs_inode_item_format,
644 .iop_pin = xfs_inode_item_pin,
645 .iop_unpin = xfs_inode_item_unpin,
7bfa31d8
CH
646 .iop_unlock = xfs_inode_item_unlock,
647 .iop_committed = xfs_inode_item_committed,
648 .iop_push = xfs_inode_item_push,
7bfa31d8 649 .iop_committing = xfs_inode_item_committing
1da177e4
LT
650};
651
652
653/*
654 * Initialize the inode log item for a newly allocated (in-core) inode.
655 */
656void
657xfs_inode_item_init(
7bfa31d8
CH
658 struct xfs_inode *ip,
659 struct xfs_mount *mp)
1da177e4 660{
7bfa31d8 661 struct xfs_inode_log_item *iip;
1da177e4
LT
662
663 ASSERT(ip->i_itemp == NULL);
664 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
665
1da177e4 666 iip->ili_inode = ip;
43f5efc5
DC
667 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
668 &xfs_inode_item_ops);
1da177e4
LT
669 iip->ili_format.ilf_type = XFS_LI_INODE;
670 iip->ili_format.ilf_ino = ip->i_ino;
92bfc6e7
CH
671 iip->ili_format.ilf_blkno = ip->i_imap.im_blkno;
672 iip->ili_format.ilf_len = ip->i_imap.im_len;
673 iip->ili_format.ilf_boffset = ip->i_imap.im_boffset;
1da177e4
LT
674}
675
676/*
677 * Free the inode log item and any memory hanging off of it.
678 */
679void
680xfs_inode_item_destroy(
681 xfs_inode_t *ip)
682{
683#ifdef XFS_TRANS_DEBUG
684 if (ip->i_itemp->ili_root_size != 0) {
f0e2d93c 685 kmem_free(ip->i_itemp->ili_orig_root);
1da177e4
LT
686 }
687#endif
688 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
689}
690
691
692/*
693 * This is the inode flushing I/O completion routine. It is called
694 * from interrupt level when the buffer containing the inode is
695 * flushed to disk. It is responsible for removing the inode item
696 * from the AIL if it has not been re-logged, and unlocking the inode's
697 * flush lock.
30136832
DC
698 *
699 * To reduce AIL lock traffic as much as possible, we scan the buffer log item
700 * list for other inodes that will run this function. We remove them from the
701 * buffer list so we can process all the inode IO completions in one AIL lock
702 * traversal.
1da177e4 703 */
1da177e4
LT
704void
705xfs_iflush_done(
ca30b2a7
CH
706 struct xfs_buf *bp,
707 struct xfs_log_item *lip)
1da177e4 708{
30136832
DC
709 struct xfs_inode_log_item *iip;
710 struct xfs_log_item *blip;
711 struct xfs_log_item *next;
712 struct xfs_log_item *prev;
ca30b2a7 713 struct xfs_ail *ailp = lip->li_ailp;
30136832
DC
714 int need_ail = 0;
715
716 /*
717 * Scan the buffer IO completions for other inodes being completed and
718 * attach them to the current inode log item.
719 */
adadbeef 720 blip = bp->b_fspriv;
30136832
DC
721 prev = NULL;
722 while (blip != NULL) {
723 if (lip->li_cb != xfs_iflush_done) {
724 prev = blip;
725 blip = blip->li_bio_list;
726 continue;
727 }
728
729 /* remove from list */
730 next = blip->li_bio_list;
731 if (!prev) {
adadbeef 732 bp->b_fspriv = next;
30136832
DC
733 } else {
734 prev->li_bio_list = next;
735 }
736
737 /* add to current list */
738 blip->li_bio_list = lip->li_bio_list;
739 lip->li_bio_list = blip;
740
741 /*
742 * while we have the item, do the unlocked check for needing
743 * the AIL lock.
744 */
745 iip = INODE_ITEM(blip);
746 if (iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn)
747 need_ail++;
748
749 blip = next;
750 }
751
752 /* make sure we capture the state of the initial inode. */
753 iip = INODE_ITEM(lip);
754 if (iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn)
755 need_ail++;
1da177e4
LT
756
757 /*
758 * We only want to pull the item from the AIL if it is
759 * actually there and its location in the log has not
760 * changed since we started the flush. Thus, we only bother
761 * if the ili_logged flag is set and the inode's lsn has not
762 * changed. First we check the lsn outside
763 * the lock since it's cheaper, and then we recheck while
764 * holding the lock before removing the inode from the AIL.
765 */
30136832
DC
766 if (need_ail) {
767 struct xfs_log_item *log_items[need_ail];
768 int i = 0;
783a2f65 769 spin_lock(&ailp->xa_lock);
30136832
DC
770 for (blip = lip; blip; blip = blip->li_bio_list) {
771 iip = INODE_ITEM(blip);
772 if (iip->ili_logged &&
773 blip->li_lsn == iip->ili_flush_lsn) {
774 log_items[i++] = blip;
775 }
776 ASSERT(i <= need_ail);
1da177e4 777 }
30136832 778 /* xfs_trans_ail_delete_bulk() drops the AIL lock. */
04913fdd
DC
779 xfs_trans_ail_delete_bulk(ailp, log_items, i,
780 SHUTDOWN_CORRUPT_INCORE);
1da177e4
LT
781 }
782
1da177e4
LT
783
784 /*
30136832
DC
785 * clean up and unlock the flush lock now we are done. We can clear the
786 * ili_last_fields bits now that we know that the data corresponding to
787 * them is safely on disk.
1da177e4 788 */
30136832
DC
789 for (blip = lip; blip; blip = next) {
790 next = blip->li_bio_list;
791 blip->li_bio_list = NULL;
792
793 iip = INODE_ITEM(blip);
794 iip->ili_logged = 0;
795 iip->ili_last_fields = 0;
796 xfs_ifunlock(iip->ili_inode);
797 }
1da177e4
LT
798}
799
800/*
04913fdd
DC
801 * This is the inode flushing abort routine. It is called from xfs_iflush when
802 * the filesystem is shutting down to clean up the inode state. It is
803 * responsible for removing the inode item from the AIL if it has not been
804 * re-logged, and unlocking the inode's flush lock.
1da177e4
LT
805 */
806void
807xfs_iflush_abort(
04913fdd
DC
808 xfs_inode_t *ip,
809 bool stale)
1da177e4 810{
783a2f65 811 xfs_inode_log_item_t *iip = ip->i_itemp;
1da177e4 812
1da177e4 813 if (iip) {
783a2f65 814 struct xfs_ail *ailp = iip->ili_item.li_ailp;
1da177e4 815 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
783a2f65 816 spin_lock(&ailp->xa_lock);
1da177e4 817 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
783a2f65 818 /* xfs_trans_ail_delete() drops the AIL lock. */
04913fdd
DC
819 xfs_trans_ail_delete(ailp, &iip->ili_item,
820 stale ?
821 SHUTDOWN_LOG_IO_ERROR :
822 SHUTDOWN_CORRUPT_INCORE);
1da177e4 823 } else
783a2f65 824 spin_unlock(&ailp->xa_lock);
1da177e4
LT
825 }
826 iip->ili_logged = 0;
827 /*
828 * Clear the ili_last_fields bits now that we know that the
829 * data corresponding to them is safely on disk.
830 */
831 iip->ili_last_fields = 0;
832 /*
833 * Clear the inode logging fields so no more flushes are
834 * attempted.
835 */
f5d8d5c4 836 iip->ili_fields = 0;
1da177e4
LT
837 }
838 /*
839 * Release the inode's flush lock since we're done with it.
840 */
841 xfs_ifunlock(ip);
842}
843
844void
845xfs_istale_done(
ca30b2a7
CH
846 struct xfs_buf *bp,
847 struct xfs_log_item *lip)
1da177e4 848{
04913fdd 849 xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
1da177e4 850}
6d192a9b
TS
851
852/*
853 * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
854 * (which can have different field alignments) to the native version
855 */
856int
857xfs_inode_item_format_convert(
858 xfs_log_iovec_t *buf,
859 xfs_inode_log_format_t *in_f)
860{
861 if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
4e0d5f92 862 xfs_inode_log_format_32_t *in_f32 = buf->i_addr;
6d192a9b 863
6d192a9b
TS
864 in_f->ilf_type = in_f32->ilf_type;
865 in_f->ilf_size = in_f32->ilf_size;
866 in_f->ilf_fields = in_f32->ilf_fields;
867 in_f->ilf_asize = in_f32->ilf_asize;
868 in_f->ilf_dsize = in_f32->ilf_dsize;
869 in_f->ilf_ino = in_f32->ilf_ino;
870 /* copy biggest field of ilf_u */
871 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
872 in_f32->ilf_u.ilfu_uuid.__u_bits,
873 sizeof(uuid_t));
874 in_f->ilf_blkno = in_f32->ilf_blkno;
875 in_f->ilf_len = in_f32->ilf_len;
876 in_f->ilf_boffset = in_f32->ilf_boffset;
877 return 0;
878 } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
4e0d5f92 879 xfs_inode_log_format_64_t *in_f64 = buf->i_addr;
6d192a9b 880
6d192a9b
TS
881 in_f->ilf_type = in_f64->ilf_type;
882 in_f->ilf_size = in_f64->ilf_size;
883 in_f->ilf_fields = in_f64->ilf_fields;
884 in_f->ilf_asize = in_f64->ilf_asize;
885 in_f->ilf_dsize = in_f64->ilf_dsize;
886 in_f->ilf_ino = in_f64->ilf_ino;
887 /* copy biggest field of ilf_u */
888 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
889 in_f64->ilf_u.ilfu_uuid.__u_bits,
890 sizeof(uuid_t));
891 in_f->ilf_blkno = in_f64->ilf_blkno;
892 in_f->ilf_len = in_f64->ilf_len;
893 in_f->ilf_boffset = in_f64->ilf_boffset;
894 return 0;
895 }
896 return EFSCORRUPTED;
897}
This page took 0.590597 seconds and 5 git commands to generate.