2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
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
7 * published by the Free Software Foundation.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
25 #include "xfs_buf_item.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_trans_priv.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_btree.h"
41 #include "xfs_ialloc.h"
45 kmem_zone_t *xfs_ili_zone; /* inode log item zone */
48 * This returns the number of iovecs needed to log the given inode item.
50 * We need one iovec for the inode log format structure, one for the
51 * inode core, and possibly one for the inode data/extents/b-tree root
52 * and one for the inode attribute data/extents/b-tree root.
56 xfs_inode_log_item_t *iip)
65 * Only log the data/extents/b-tree root if there is something
68 iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
70 switch (ip->i_d.di_format) {
71 case XFS_DINODE_FMT_EXTENTS:
72 iip->ili_format.ilf_fields &=
73 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
74 XFS_ILOG_DEV | XFS_ILOG_UUID);
75 if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) &&
76 (ip->i_d.di_nextents > 0) &&
77 (ip->i_df.if_bytes > 0)) {
78 ASSERT(ip->i_df.if_u1.if_extents != NULL);
81 iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT;
85 case XFS_DINODE_FMT_BTREE:
86 ASSERT(ip->i_df.if_ext_max ==
87 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
88 iip->ili_format.ilf_fields &=
89 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
90 XFS_ILOG_DEV | XFS_ILOG_UUID);
91 if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) &&
92 (ip->i_df.if_broot_bytes > 0)) {
93 ASSERT(ip->i_df.if_broot != NULL);
96 ASSERT(!(iip->ili_format.ilf_fields &
98 #ifdef XFS_TRANS_DEBUG
99 if (iip->ili_root_size > 0) {
100 ASSERT(iip->ili_root_size ==
101 ip->i_df.if_broot_bytes);
102 ASSERT(memcmp(iip->ili_orig_root,
104 iip->ili_root_size) == 0);
106 ASSERT(ip->i_df.if_broot_bytes == 0);
109 iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT;
113 case XFS_DINODE_FMT_LOCAL:
114 iip->ili_format.ilf_fields &=
115 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
116 XFS_ILOG_DEV | XFS_ILOG_UUID);
117 if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) &&
118 (ip->i_df.if_bytes > 0)) {
119 ASSERT(ip->i_df.if_u1.if_data != NULL);
120 ASSERT(ip->i_d.di_size > 0);
123 iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA;
127 case XFS_DINODE_FMT_DEV:
128 iip->ili_format.ilf_fields &=
129 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
130 XFS_ILOG_DEXT | XFS_ILOG_UUID);
133 case XFS_DINODE_FMT_UUID:
134 iip->ili_format.ilf_fields &=
135 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
136 XFS_ILOG_DEXT | XFS_ILOG_DEV);
145 * If there are no attributes associated with this file,
146 * then there cannot be anything more to log.
147 * Clear all attribute-related log flags.
149 if (!XFS_IFORK_Q(ip)) {
150 iip->ili_format.ilf_fields &=
151 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
156 * Log any necessary attribute data.
158 switch (ip->i_d.di_aformat) {
159 case XFS_DINODE_FMT_EXTENTS:
160 iip->ili_format.ilf_fields &=
161 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
162 if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) &&
163 (ip->i_d.di_anextents > 0) &&
164 (ip->i_afp->if_bytes > 0)) {
165 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
168 iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT;
172 case XFS_DINODE_FMT_BTREE:
173 iip->ili_format.ilf_fields &=
174 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
175 if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) &&
176 (ip->i_afp->if_broot_bytes > 0)) {
177 ASSERT(ip->i_afp->if_broot != NULL);
180 iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT;
184 case XFS_DINODE_FMT_LOCAL:
185 iip->ili_format.ilf_fields &=
186 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
187 if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) &&
188 (ip->i_afp->if_bytes > 0)) {
189 ASSERT(ip->i_afp->if_u1.if_data != NULL);
192 iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA;
205 * This is called to fill in the vector of log iovecs for the
206 * given inode log item. It fills the first item with an inode
207 * log format structure, the second with the on-disk inode structure,
208 * and a possible third and/or fourth with the inode data/extents/b-tree
209 * root and inode attributes data/extents/b-tree root.
212 xfs_inode_item_format(
213 xfs_inode_log_item_t *iip,
214 xfs_log_iovec_t *log_vector)
217 xfs_log_iovec_t *vecp;
220 xfs_bmbt_rec_t *ext_buffer;
227 vecp->i_addr = (xfs_caddr_t)&iip->ili_format;
228 vecp->i_len = sizeof(xfs_inode_log_format_t);
229 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IFORMAT);
234 * Clear i_update_core if the timestamps (or any other
235 * non-transactional modification) need flushing/logging
236 * and we're about to log them with the rest of the core.
238 * This is the same logic as xfs_iflush() but this code can't
239 * run at the same time as xfs_iflush because we're in commit
240 * processing here and so we have the inode lock held in
241 * exclusive mode. Although it doesn't really matter
242 * for the timestamps if both routines were to grab the
243 * timestamps or not. That would be ok.
245 * We clear i_update_core before copying out the data.
246 * This is for coordination with our timestamp updates
247 * that don't hold the inode lock. They will always
248 * update the timestamps BEFORE setting i_update_core,
249 * so if we clear i_update_core after they set it we
250 * are guaranteed to see their updates to the timestamps
251 * either here. Likewise, if they set it after we clear it
252 * here, we'll see it either on the next commit of this
253 * inode or the next time the inode gets flushed via
254 * xfs_iflush(). This depends on strongly ordered memory
255 * semantics, but we have that. We use the SYNCHRONIZE
256 * macro to make sure that the compiler does not reorder
257 * the i_update_core access below the data copy below.
259 if (ip->i_update_core) {
260 ip->i_update_core = 0;
265 * We don't have to worry about re-ordering here because
266 * the update_size field is protected by the inode lock
267 * and we have that held in exclusive mode.
269 if (ip->i_update_size)
270 ip->i_update_size = 0;
273 * Make sure to get the latest atime from the Linux inode.
275 xfs_synchronize_atime(ip);
278 * make sure the linux inode is dirty
280 xfs_mark_inode_dirty_sync(ip);
282 vecp->i_addr = (xfs_caddr_t)&ip->i_d;
283 vecp->i_len = sizeof(xfs_dinode_core_t);
284 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ICORE);
287 iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
290 * If this is really an old format inode, then we need to
291 * log it as such. This means that we have to copy the link
292 * count from the new field to the old. We don't have to worry
293 * about the new fields, because nothing trusts them as long as
294 * the old inode version number is there. If the superblock already
295 * has a new version number, then we don't bother converting back.
298 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
299 XFS_SB_VERSION_HASNLINK(&mp->m_sb));
300 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
301 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
305 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
306 ip->i_d.di_onlink = ip->i_d.di_nlink;
309 * The superblock version has already been bumped,
310 * so just make the conversion to the new inode
313 ip->i_d.di_version = XFS_DINODE_VERSION_2;
314 ip->i_d.di_onlink = 0;
315 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
319 switch (ip->i_d.di_format) {
320 case XFS_DINODE_FMT_EXTENTS:
321 ASSERT(!(iip->ili_format.ilf_fields &
322 (XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
323 XFS_ILOG_DEV | XFS_ILOG_UUID)));
324 if (iip->ili_format.ilf_fields & XFS_ILOG_DEXT) {
325 ASSERT(ip->i_df.if_bytes > 0);
326 ASSERT(ip->i_df.if_u1.if_extents != NULL);
327 ASSERT(ip->i_d.di_nextents > 0);
328 ASSERT(iip->ili_extents_buf == NULL);
329 nrecs = ip->i_df.if_bytes /
330 (uint)sizeof(xfs_bmbt_rec_t);
332 #ifdef XFS_NATIVE_HOST
333 if (nrecs == ip->i_d.di_nextents) {
335 * There are no delayed allocation
336 * extents, so just point to the
337 * real extents array.
340 (char *)(ip->i_df.if_u1.if_extents);
341 vecp->i_len = ip->i_df.if_bytes;
342 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT);
347 * There are delayed allocation extents
348 * in the inode, or we need to convert
349 * the extents to on disk format.
350 * Use xfs_iextents_copy()
351 * to copy only the real extents into
352 * a separate buffer. We'll free the
353 * buffer in the unlock routine.
355 ext_buffer = kmem_alloc(ip->i_df.if_bytes,
357 iip->ili_extents_buf = ext_buffer;
358 vecp->i_addr = (xfs_caddr_t)ext_buffer;
359 vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
361 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT);
363 ASSERT(vecp->i_len <= ip->i_df.if_bytes);
364 iip->ili_format.ilf_dsize = vecp->i_len;
370 case XFS_DINODE_FMT_BTREE:
371 ASSERT(!(iip->ili_format.ilf_fields &
372 (XFS_ILOG_DDATA | XFS_ILOG_DEXT |
373 XFS_ILOG_DEV | XFS_ILOG_UUID)));
374 if (iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) {
375 ASSERT(ip->i_df.if_broot_bytes > 0);
376 ASSERT(ip->i_df.if_broot != NULL);
377 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_broot;
378 vecp->i_len = ip->i_df.if_broot_bytes;
379 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IBROOT);
382 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
386 case XFS_DINODE_FMT_LOCAL:
387 ASSERT(!(iip->ili_format.ilf_fields &
388 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
389 XFS_ILOG_DEV | XFS_ILOG_UUID)));
390 if (iip->ili_format.ilf_fields & XFS_ILOG_DDATA) {
391 ASSERT(ip->i_df.if_bytes > 0);
392 ASSERT(ip->i_df.if_u1.if_data != NULL);
393 ASSERT(ip->i_d.di_size > 0);
395 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_u1.if_data;
397 * Round i_bytes up to a word boundary.
398 * The underlying memory is guaranteed to
399 * to be there by xfs_idata_realloc().
401 data_bytes = roundup(ip->i_df.if_bytes, 4);
402 ASSERT((ip->i_df.if_real_bytes == 0) ||
403 (ip->i_df.if_real_bytes == data_bytes));
404 vecp->i_len = (int)data_bytes;
405 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ILOCAL);
408 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
412 case XFS_DINODE_FMT_DEV:
413 ASSERT(!(iip->ili_format.ilf_fields &
414 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
415 XFS_ILOG_DDATA | XFS_ILOG_UUID)));
416 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
417 iip->ili_format.ilf_u.ilfu_rdev =
418 ip->i_df.if_u2.if_rdev;
422 case XFS_DINODE_FMT_UUID:
423 ASSERT(!(iip->ili_format.ilf_fields &
424 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
425 XFS_ILOG_DDATA | XFS_ILOG_DEV)));
426 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
427 iip->ili_format.ilf_u.ilfu_uuid =
428 ip->i_df.if_u2.if_uuid;
438 * If there are no attributes associated with the file,
440 * Assert that no attribute-related log flags are set.
442 if (!XFS_IFORK_Q(ip)) {
443 ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
444 iip->ili_format.ilf_size = nvecs;
445 ASSERT(!(iip->ili_format.ilf_fields &
446 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
450 switch (ip->i_d.di_aformat) {
451 case XFS_DINODE_FMT_EXTENTS:
452 ASSERT(!(iip->ili_format.ilf_fields &
453 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT)));
454 if (iip->ili_format.ilf_fields & XFS_ILOG_AEXT) {
455 ASSERT(ip->i_afp->if_bytes > 0);
456 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
457 ASSERT(ip->i_d.di_anextents > 0);
459 nrecs = ip->i_afp->if_bytes /
460 (uint)sizeof(xfs_bmbt_rec_t);
463 ASSERT(nrecs == ip->i_d.di_anextents);
464 #ifdef XFS_NATIVE_HOST
466 * There are not delayed allocation extents
467 * for attributes, so just point at the array.
469 vecp->i_addr = (char *)(ip->i_afp->if_u1.if_extents);
470 vecp->i_len = ip->i_afp->if_bytes;
472 ASSERT(iip->ili_aextents_buf == NULL);
474 * Need to endian flip before logging
476 ext_buffer = kmem_alloc(ip->i_afp->if_bytes,
478 iip->ili_aextents_buf = ext_buffer;
479 vecp->i_addr = (xfs_caddr_t)ext_buffer;
480 vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
483 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_EXT);
484 iip->ili_format.ilf_asize = vecp->i_len;
490 case XFS_DINODE_FMT_BTREE:
491 ASSERT(!(iip->ili_format.ilf_fields &
492 (XFS_ILOG_ADATA | XFS_ILOG_AEXT)));
493 if (iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) {
494 ASSERT(ip->i_afp->if_broot_bytes > 0);
495 ASSERT(ip->i_afp->if_broot != NULL);
496 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_broot;
497 vecp->i_len = ip->i_afp->if_broot_bytes;
498 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_BROOT);
501 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
505 case XFS_DINODE_FMT_LOCAL:
506 ASSERT(!(iip->ili_format.ilf_fields &
507 (XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
508 if (iip->ili_format.ilf_fields & XFS_ILOG_ADATA) {
509 ASSERT(ip->i_afp->if_bytes > 0);
510 ASSERT(ip->i_afp->if_u1.if_data != NULL);
512 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_u1.if_data;
514 * Round i_bytes up to a word boundary.
515 * The underlying memory is guaranteed to
516 * to be there by xfs_idata_realloc().
518 data_bytes = roundup(ip->i_afp->if_bytes, 4);
519 ASSERT((ip->i_afp->if_real_bytes == 0) ||
520 (ip->i_afp->if_real_bytes == data_bytes));
521 vecp->i_len = (int)data_bytes;
522 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_LOCAL);
525 iip->ili_format.ilf_asize = (unsigned)data_bytes;
534 ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
535 iip->ili_format.ilf_size = nvecs;
540 * This is called to pin the inode associated with the inode log
541 * item in memory so it cannot be written out. Do this by calling
542 * xfs_ipin() to bump the pin count in the inode while holding the
547 xfs_inode_log_item_t *iip)
549 ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
550 xfs_ipin(iip->ili_inode);
555 * This is called to unpin the inode associated with the inode log
556 * item which was previously pinned with a call to xfs_inode_item_pin().
557 * Just call xfs_iunpin() on the inode to do this.
561 xfs_inode_item_unpin(
562 xfs_inode_log_item_t *iip,
565 xfs_iunpin(iip->ili_inode);
570 xfs_inode_item_unpin_remove(
571 xfs_inode_log_item_t *iip,
574 xfs_iunpin(iip->ili_inode);
578 * This is called to attempt to lock the inode associated with this
579 * inode log item, in preparation for the push routine which does the actual
580 * iflush. Don't sleep on the inode lock or the flush lock.
582 * If the flush lock is already held, indicating that the inode has
583 * been or is in the process of being flushed, then (ideally) we'd like to
584 * see if the inode's buffer is still incore, and if so give it a nudge.
585 * We delay doing so until the pushbuf routine, though, to avoid holding
586 * the AIL lock across a call to the blackhole which is the buffer cache.
587 * Also we don't want to sleep in any device strategy routines, which can happen
588 * if we do the subsequent bawrite in here.
591 xfs_inode_item_trylock(
592 xfs_inode_log_item_t *iip)
594 register xfs_inode_t *ip;
598 if (xfs_ipincount(ip) > 0) {
599 return XFS_ITEM_PINNED;
602 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
603 return XFS_ITEM_LOCKED;
606 if (!xfs_iflock_nowait(ip)) {
608 * If someone else isn't already trying to push the inode
609 * buffer, we get to do it.
611 if (iip->ili_pushbuf_flag == 0) {
612 iip->ili_pushbuf_flag = 1;
614 iip->ili_push_owner = current_pid();
617 * Inode is left locked in shared mode.
618 * Pushbuf routine gets to unlock it.
620 return XFS_ITEM_PUSHBUF;
623 * We hold the AIL lock, so we must specify the
624 * NONOTIFY flag so that we won't double trip.
626 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
627 return XFS_ITEM_FLUSHING;
632 /* Stale items should force out the iclog */
633 if (ip->i_flags & XFS_ISTALE) {
635 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
636 return XFS_ITEM_PINNED;
640 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
641 ASSERT(iip->ili_format.ilf_fields != 0);
642 ASSERT(iip->ili_logged == 0);
643 ASSERT(iip->ili_item.li_flags & XFS_LI_IN_AIL);
646 return XFS_ITEM_SUCCESS;
650 * Unlock the inode associated with the inode log item.
651 * Clear the fields of the inode and inode log item that
652 * are specific to the current transaction. If the
653 * hold flags is set, do not unlock the inode.
656 xfs_inode_item_unlock(
657 xfs_inode_log_item_t *iip)
665 ASSERT(iip->ili_inode->i_itemp != NULL);
666 ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
667 ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
668 XFS_ILI_IOLOCKED_EXCL)) ||
669 ismrlocked(&(iip->ili_inode->i_iolock), MR_UPDATE));
670 ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
671 XFS_ILI_IOLOCKED_SHARED)) ||
672 ismrlocked(&(iip->ili_inode->i_iolock), MR_ACCESS));
674 * Clear the transaction pointer in the inode.
680 * If the inode needed a separate buffer with which to log
681 * its extents, then free it now.
683 if (iip->ili_extents_buf != NULL) {
684 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
685 ASSERT(ip->i_d.di_nextents > 0);
686 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
687 ASSERT(ip->i_df.if_bytes > 0);
688 kmem_free(iip->ili_extents_buf, ip->i_df.if_bytes);
689 iip->ili_extents_buf = NULL;
691 if (iip->ili_aextents_buf != NULL) {
692 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
693 ASSERT(ip->i_d.di_anextents > 0);
694 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
695 ASSERT(ip->i_afp->if_bytes > 0);
696 kmem_free(iip->ili_aextents_buf, ip->i_afp->if_bytes);
697 iip->ili_aextents_buf = NULL;
701 * Figure out if we should unlock the inode or not.
703 hold = iip->ili_flags & XFS_ILI_HOLD;
706 * Before clearing out the flags, remember whether we
707 * are holding the inode's IO lock.
709 iolocked = iip->ili_flags & XFS_ILI_IOLOCKED_ANY;
712 * Clear out the fields of the inode log item particular
713 * to the current transaction.
715 iip->ili_ilock_recur = 0;
716 iip->ili_iolock_recur = 0;
720 * Unlock the inode if XFS_ILI_HOLD was not set.
723 lock_flags = XFS_ILOCK_EXCL;
724 if (iolocked & XFS_ILI_IOLOCKED_EXCL) {
725 lock_flags |= XFS_IOLOCK_EXCL;
726 } else if (iolocked & XFS_ILI_IOLOCKED_SHARED) {
727 lock_flags |= XFS_IOLOCK_SHARED;
729 xfs_iput(iip->ili_inode, lock_flags);
734 * This is called to find out where the oldest active copy of the
735 * inode log item in the on disk log resides now that the last log
736 * write of it completed at the given lsn. Since we always re-log
737 * all dirty data in an inode, the latest copy in the on disk log
738 * is the only one that matters. Therefore, simply return the
743 xfs_inode_item_committed(
744 xfs_inode_log_item_t *iip,
751 * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
752 * failed to get the inode flush lock but did get the inode locked SHARED.
753 * Here we're trying to see if the inode buffer is incore, and if so whether it's
754 * marked delayed write. If that's the case, we'll initiate a bawrite on that
755 * buffer to expedite the process.
757 * We aren't holding the AIL lock (or the flush lock) when this gets called,
758 * so it is inherently race-y.
761 xfs_inode_item_pushbuf(
762 xfs_inode_log_item_t *iip)
771 ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
774 * The ili_pushbuf_flag keeps others from
775 * trying to duplicate our effort.
777 ASSERT(iip->ili_pushbuf_flag != 0);
778 ASSERT(iip->ili_push_owner == current_pid());
781 * If flushlock isn't locked anymore, chances are that the
782 * inode flush completed and the inode was taken off the AIL.
785 if (!issemalocked(&(ip->i_flock)) ||
786 ((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0)) {
787 iip->ili_pushbuf_flag = 0;
788 xfs_iunlock(ip, XFS_ILOCK_SHARED);
793 bp = xfs_incore(mp->m_ddev_targp, iip->ili_format.ilf_blkno,
794 iip->ili_format.ilf_len, XFS_INCORE_TRYLOCK);
797 if (XFS_BUF_ISDELAYWRITE(bp)) {
799 * We were racing with iflush because we don't hold
800 * the AIL lock or the flush lock. However, at this point,
801 * we have the buffer, and we know that it's dirty.
802 * So, it's possible that iflush raced with us, and
803 * this item is already taken off the AIL.
804 * If not, we can flush it async.
806 dopush = ((iip->ili_item.li_flags & XFS_LI_IN_AIL) &&
807 issemalocked(&(ip->i_flock)));
808 iip->ili_pushbuf_flag = 0;
809 xfs_iunlock(ip, XFS_ILOCK_SHARED);
810 xfs_buftrace("INODE ITEM PUSH", bp);
811 if (XFS_BUF_ISPINNED(bp)) {
812 xfs_log_force(mp, (xfs_lsn_t)0,
821 iip->ili_pushbuf_flag = 0;
822 xfs_iunlock(ip, XFS_ILOCK_SHARED);
828 * We have to be careful about resetting pushbuf flag too early (above).
829 * Even though in theory we can do it as soon as we have the buflock,
830 * we don't want others to be doing work needlessly. They'll come to
831 * this function thinking that pushing the buffer is their
832 * responsibility only to find that the buffer is still locked by
833 * another doing the same thing
835 iip->ili_pushbuf_flag = 0;
836 xfs_iunlock(ip, XFS_ILOCK_SHARED);
842 * This is called to asynchronously write the inode associated with this
843 * inode log item out to disk. The inode will already have been locked by
844 * a successful call to xfs_inode_item_trylock().
848 xfs_inode_log_item_t *iip)
854 ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
855 ASSERT(issemalocked(&(ip->i_flock)));
857 * Since we were able to lock the inode's flush lock and
858 * we found it on the AIL, the inode must be dirty. This
859 * is because the inode is removed from the AIL while still
860 * holding the flush lock in xfs_iflush_done(). Thus, if
861 * we found it in the AIL and were able to obtain the flush
862 * lock without sleeping, then there must not have been
863 * anyone in the process of flushing the inode.
865 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
866 iip->ili_format.ilf_fields != 0);
869 * Write out the inode. The completion routine ('iflush_done') will
870 * pull it from the AIL, mark it clean, unlock the flush lock.
872 (void) xfs_iflush(ip, XFS_IFLUSH_ASYNC);
873 xfs_iunlock(ip, XFS_ILOCK_SHARED);
879 * XXX rcc - this one really has to do something. Probably needs
880 * to stamp in a new field in the incore inode.
884 xfs_inode_item_committing(
885 xfs_inode_log_item_t *iip,
888 iip->ili_last_lsn = lsn;
893 * This is the ops vector shared by all buf log items.
895 static struct xfs_item_ops xfs_inode_item_ops = {
896 .iop_size = (uint(*)(xfs_log_item_t*))xfs_inode_item_size,
897 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
898 xfs_inode_item_format,
899 .iop_pin = (void(*)(xfs_log_item_t*))xfs_inode_item_pin,
900 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_inode_item_unpin,
901 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
902 xfs_inode_item_unpin_remove,
903 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_inode_item_trylock,
904 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_inode_item_unlock,
905 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
906 xfs_inode_item_committed,
907 .iop_push = (void(*)(xfs_log_item_t*))xfs_inode_item_push,
908 .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_inode_item_pushbuf,
909 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
910 xfs_inode_item_committing
915 * Initialize the inode log item for a newly allocated (in-core) inode.
922 xfs_inode_log_item_t *iip;
924 ASSERT(ip->i_itemp == NULL);
925 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
927 iip->ili_item.li_type = XFS_LI_INODE;
928 iip->ili_item.li_ops = &xfs_inode_item_ops;
929 iip->ili_item.li_mountp = mp;
933 We have zeroed memory. No need ...
934 iip->ili_extents_buf = NULL;
935 iip->ili_pushbuf_flag = 0;
938 iip->ili_format.ilf_type = XFS_LI_INODE;
939 iip->ili_format.ilf_ino = ip->i_ino;
940 iip->ili_format.ilf_blkno = ip->i_blkno;
941 iip->ili_format.ilf_len = ip->i_len;
942 iip->ili_format.ilf_boffset = ip->i_boffset;
946 * Free the inode log item and any memory hanging off of it.
949 xfs_inode_item_destroy(
952 #ifdef XFS_TRANS_DEBUG
953 if (ip->i_itemp->ili_root_size != 0) {
954 kmem_free(ip->i_itemp->ili_orig_root,
955 ip->i_itemp->ili_root_size);
958 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
963 * This is the inode flushing I/O completion routine. It is called
964 * from interrupt level when the buffer containing the inode is
965 * flushed to disk. It is responsible for removing the inode item
966 * from the AIL if it has not been re-logged, and unlocking the inode's
973 xfs_inode_log_item_t *iip)
980 * We only want to pull the item from the AIL if it is
981 * actually there and its location in the log has not
982 * changed since we started the flush. Thus, we only bother
983 * if the ili_logged flag is set and the inode's lsn has not
984 * changed. First we check the lsn outside
985 * the lock since it's cheaper, and then we recheck while
986 * holding the lock before removing the inode from the AIL.
988 if (iip->ili_logged &&
989 (iip->ili_item.li_lsn == iip->ili_flush_lsn)) {
990 spin_lock(&ip->i_mount->m_ail_lock);
991 if (iip->ili_item.li_lsn == iip->ili_flush_lsn) {
993 * xfs_trans_delete_ail() drops the AIL lock.
995 xfs_trans_delete_ail(ip->i_mount,
996 (xfs_log_item_t*)iip);
998 spin_unlock(&ip->i_mount->m_ail_lock);
1002 iip->ili_logged = 0;
1005 * Clear the ili_last_fields bits now that we know that the
1006 * data corresponding to them is safely on disk.
1008 iip->ili_last_fields = 0;
1011 * Release the inode's flush lock since we're done with it.
1019 * This is the inode flushing abort routine. It is called
1020 * from xfs_iflush when the filesystem is shutting down to clean
1021 * up the inode state.
1022 * It is responsible for removing the inode item
1023 * from the AIL if it has not been re-logged, and unlocking the inode's
1030 xfs_inode_log_item_t *iip;
1036 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
1037 spin_lock(&mp->m_ail_lock);
1038 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
1040 * xfs_trans_delete_ail() drops the AIL lock.
1042 xfs_trans_delete_ail(mp, (xfs_log_item_t *)iip);
1044 spin_unlock(&mp->m_ail_lock);
1046 iip->ili_logged = 0;
1048 * Clear the ili_last_fields bits now that we know that the
1049 * data corresponding to them is safely on disk.
1051 iip->ili_last_fields = 0;
1053 * Clear the inode logging fields so no more flushes are
1056 iip->ili_format.ilf_fields = 0;
1059 * Release the inode's flush lock since we're done with it.
1067 xfs_inode_log_item_t *iip)
1069 xfs_iflush_abort(iip->ili_inode);
1073 * convert an xfs_inode_log_format struct from either 32 or 64 bit versions
1074 * (which can have different field alignments) to the native version
1077 xfs_inode_item_format_convert(
1078 xfs_log_iovec_t *buf,
1079 xfs_inode_log_format_t *in_f)
1081 if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) {
1082 xfs_inode_log_format_32_t *in_f32;
1084 in_f32 = (xfs_inode_log_format_32_t *)buf->i_addr;
1085 in_f->ilf_type = in_f32->ilf_type;
1086 in_f->ilf_size = in_f32->ilf_size;
1087 in_f->ilf_fields = in_f32->ilf_fields;
1088 in_f->ilf_asize = in_f32->ilf_asize;
1089 in_f->ilf_dsize = in_f32->ilf_dsize;
1090 in_f->ilf_ino = in_f32->ilf_ino;
1091 /* copy biggest field of ilf_u */
1092 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
1093 in_f32->ilf_u.ilfu_uuid.__u_bits,
1095 in_f->ilf_blkno = in_f32->ilf_blkno;
1096 in_f->ilf_len = in_f32->ilf_len;
1097 in_f->ilf_boffset = in_f32->ilf_boffset;
1099 } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){
1100 xfs_inode_log_format_64_t *in_f64;
1102 in_f64 = (xfs_inode_log_format_64_t *)buf->i_addr;
1103 in_f->ilf_type = in_f64->ilf_type;
1104 in_f->ilf_size = in_f64->ilf_size;
1105 in_f->ilf_fields = in_f64->ilf_fields;
1106 in_f->ilf_asize = in_f64->ilf_asize;
1107 in_f->ilf_dsize = in_f64->ilf_dsize;
1108 in_f->ilf_ino = in_f64->ilf_ino;
1109 /* copy biggest field of ilf_u */
1110 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits,
1111 in_f64->ilf_u.ilfu_uuid.__u_bits,
1113 in_f->ilf_blkno = in_f64->ilf_blkno;
1114 in_f->ilf_len = in_f64->ilf_len;
1115 in_f->ilf_boffset = in_f64->ilf_boffset;
1118 return EFSCORRUPTED;