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