2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * This file contains the implementation of the xfs_inode_log_item.
35 * It contains the item operations used to manipulate the inode log
36 * items as well as utility routines used by the inode specific
37 * transaction routines.
40 #include "xfs_macros.h"
41 #include "xfs_types.h"
44 #include "xfs_trans.h"
45 #include "xfs_buf_item.h"
49 #include "xfs_dmapi.h"
50 #include "xfs_mount.h"
51 #include "xfs_trans_priv.h"
53 #include "xfs_alloc_btree.h"
54 #include "xfs_bmap_btree.h"
55 #include "xfs_ialloc_btree.h"
56 #include "xfs_btree.h"
57 #include "xfs_ialloc.h"
58 #include "xfs_attr_sf.h"
59 #include "xfs_dir_sf.h"
60 #include "xfs_dir2_sf.h"
61 #include "xfs_dinode.h"
62 #include "xfs_inode_item.h"
63 #include "xfs_inode.h"
67 kmem_zone_t *xfs_ili_zone; /* inode log item zone */
70 * This returns the number of iovecs needed to log the given inode item.
72 * We need one iovec for the inode log format structure, one for the
73 * inode core, and possibly one for the inode data/extents/b-tree root
74 * and one for the inode attribute data/extents/b-tree root.
78 xfs_inode_log_item_t *iip)
87 * Only log the data/extents/b-tree root if there is something
90 iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
92 switch (ip->i_d.di_format) {
93 case XFS_DINODE_FMT_EXTENTS:
94 iip->ili_format.ilf_fields &=
95 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
96 XFS_ILOG_DEV | XFS_ILOG_UUID);
97 if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) &&
98 (ip->i_d.di_nextents > 0) &&
99 (ip->i_df.if_bytes > 0)) {
100 ASSERT(ip->i_df.if_u1.if_extents != NULL);
103 iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT;
107 case XFS_DINODE_FMT_BTREE:
108 ASSERT(ip->i_df.if_ext_max ==
109 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
110 iip->ili_format.ilf_fields &=
111 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT |
112 XFS_ILOG_DEV | XFS_ILOG_UUID);
113 if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) &&
114 (ip->i_df.if_broot_bytes > 0)) {
115 ASSERT(ip->i_df.if_broot != NULL);
118 ASSERT(!(iip->ili_format.ilf_fields &
120 #ifdef XFS_TRANS_DEBUG
121 if (iip->ili_root_size > 0) {
122 ASSERT(iip->ili_root_size ==
123 ip->i_df.if_broot_bytes);
124 ASSERT(memcmp(iip->ili_orig_root,
126 iip->ili_root_size) == 0);
128 ASSERT(ip->i_df.if_broot_bytes == 0);
131 iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT;
135 case XFS_DINODE_FMT_LOCAL:
136 iip->ili_format.ilf_fields &=
137 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT |
138 XFS_ILOG_DEV | XFS_ILOG_UUID);
139 if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) &&
140 (ip->i_df.if_bytes > 0)) {
141 ASSERT(ip->i_df.if_u1.if_data != NULL);
142 ASSERT(ip->i_d.di_size > 0);
145 iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA;
149 case XFS_DINODE_FMT_DEV:
150 iip->ili_format.ilf_fields &=
151 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
152 XFS_ILOG_DEXT | XFS_ILOG_UUID);
155 case XFS_DINODE_FMT_UUID:
156 iip->ili_format.ilf_fields &=
157 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
158 XFS_ILOG_DEXT | XFS_ILOG_DEV);
167 * If there are no attributes associated with this file,
168 * then there cannot be anything more to log.
169 * Clear all attribute-related log flags.
171 if (!XFS_IFORK_Q(ip)) {
172 iip->ili_format.ilf_fields &=
173 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
178 * Log any necessary attribute data.
180 switch (ip->i_d.di_aformat) {
181 case XFS_DINODE_FMT_EXTENTS:
182 iip->ili_format.ilf_fields &=
183 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
184 if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) &&
185 (ip->i_d.di_anextents > 0) &&
186 (ip->i_afp->if_bytes > 0)) {
187 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
190 iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT;
194 case XFS_DINODE_FMT_BTREE:
195 iip->ili_format.ilf_fields &=
196 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
197 if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) &&
198 (ip->i_afp->if_broot_bytes > 0)) {
199 ASSERT(ip->i_afp->if_broot != NULL);
202 iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT;
206 case XFS_DINODE_FMT_LOCAL:
207 iip->ili_format.ilf_fields &=
208 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
209 if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) &&
210 (ip->i_afp->if_bytes > 0)) {
211 ASSERT(ip->i_afp->if_u1.if_data != NULL);
214 iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA;
227 * This is called to fill in the vector of log iovecs for the
228 * given inode log item. It fills the first item with an inode
229 * log format structure, the second with the on-disk inode structure,
230 * and a possible third and/or fourth with the inode data/extents/b-tree
231 * root and inode attributes data/extents/b-tree root.
234 xfs_inode_item_format(
235 xfs_inode_log_item_t *iip,
236 xfs_log_iovec_t *log_vector)
239 xfs_log_iovec_t *vecp;
242 xfs_bmbt_rec_t *ext_buffer;
249 vecp->i_addr = (xfs_caddr_t)&iip->ili_format;
250 vecp->i_len = sizeof(xfs_inode_log_format_t);
255 * Clear i_update_core if the timestamps (or any other
256 * non-transactional modification) need flushing/logging
257 * and we're about to log them with the rest of the core.
259 * This is the same logic as xfs_iflush() but this code can't
260 * run at the same time as xfs_iflush because we're in commit
261 * processing here and so we have the inode lock held in
262 * exclusive mode. Although it doesn't really matter
263 * for the timestamps if both routines were to grab the
264 * timestamps or not. That would be ok.
266 * We clear i_update_core before copying out the data.
267 * This is for coordination with our timestamp updates
268 * that don't hold the inode lock. They will always
269 * update the timestamps BEFORE setting i_update_core,
270 * so if we clear i_update_core after they set it we
271 * are guaranteed to see their updates to the timestamps
272 * either here. Likewise, if they set it after we clear it
273 * here, we'll see it either on the next commit of this
274 * inode or the next time the inode gets flushed via
275 * xfs_iflush(). This depends on strongly ordered memory
276 * semantics, but we have that. We use the SYNCHRONIZE
277 * macro to make sure that the compiler does not reorder
278 * the i_update_core access below the data copy below.
280 if (ip->i_update_core) {
281 ip->i_update_core = 0;
286 * We don't have to worry about re-ordering here because
287 * the update_size field is protected by the inode lock
288 * and we have that held in exclusive mode.
290 if (ip->i_update_size)
291 ip->i_update_size = 0;
293 vecp->i_addr = (xfs_caddr_t)&ip->i_d;
294 vecp->i_len = sizeof(xfs_dinode_core_t);
297 iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
300 * If this is really an old format inode, then we need to
301 * log it as such. This means that we have to copy the link
302 * count from the new field to the old. We don't have to worry
303 * about the new fields, because nothing trusts them as long as
304 * the old inode version number is there. If the superblock already
305 * has a new version number, then we don't bother converting back.
308 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
309 XFS_SB_VERSION_HASNLINK(&mp->m_sb));
310 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
311 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
315 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
316 ip->i_d.di_onlink = ip->i_d.di_nlink;
319 * The superblock version has already been bumped,
320 * so just make the conversion to the new inode
323 ip->i_d.di_version = XFS_DINODE_VERSION_2;
324 ip->i_d.di_onlink = 0;
325 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
329 switch (ip->i_d.di_format) {
330 case XFS_DINODE_FMT_EXTENTS:
331 ASSERT(!(iip->ili_format.ilf_fields &
332 (XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
333 XFS_ILOG_DEV | XFS_ILOG_UUID)));
334 if (iip->ili_format.ilf_fields & XFS_ILOG_DEXT) {
335 ASSERT(ip->i_df.if_bytes > 0);
336 ASSERT(ip->i_df.if_u1.if_extents != NULL);
337 ASSERT(ip->i_d.di_nextents > 0);
338 ASSERT(iip->ili_extents_buf == NULL);
339 nrecs = ip->i_df.if_bytes /
340 (uint)sizeof(xfs_bmbt_rec_t);
342 #if __BYTE_ORDER == __BIG_ENDIAN
343 if (nrecs == ip->i_d.di_nextents) {
345 * There are no delayed allocation
346 * extents, so just point to the
347 * real extents array.
350 (char *)(ip->i_df.if_u1.if_extents);
351 vecp->i_len = ip->i_df.if_bytes;
356 * There are delayed allocation extents
357 * in the inode, or we need to convert
358 * the extents to on disk format.
359 * Use xfs_iextents_copy()
360 * to copy only the real extents into
361 * a separate buffer. We'll free the
362 * buffer in the unlock routine.
364 ext_buffer = kmem_alloc(ip->i_df.if_bytes,
366 iip->ili_extents_buf = ext_buffer;
367 vecp->i_addr = (xfs_caddr_t)ext_buffer;
368 vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
371 ASSERT(vecp->i_len <= ip->i_df.if_bytes);
372 iip->ili_format.ilf_dsize = vecp->i_len;
378 case XFS_DINODE_FMT_BTREE:
379 ASSERT(!(iip->ili_format.ilf_fields &
380 (XFS_ILOG_DDATA | XFS_ILOG_DEXT |
381 XFS_ILOG_DEV | XFS_ILOG_UUID)));
382 if (iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) {
383 ASSERT(ip->i_df.if_broot_bytes > 0);
384 ASSERT(ip->i_df.if_broot != NULL);
385 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_broot;
386 vecp->i_len = ip->i_df.if_broot_bytes;
389 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
393 case XFS_DINODE_FMT_LOCAL:
394 ASSERT(!(iip->ili_format.ilf_fields &
395 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
396 XFS_ILOG_DEV | XFS_ILOG_UUID)));
397 if (iip->ili_format.ilf_fields & XFS_ILOG_DDATA) {
398 ASSERT(ip->i_df.if_bytes > 0);
399 ASSERT(ip->i_df.if_u1.if_data != NULL);
400 ASSERT(ip->i_d.di_size > 0);
402 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_u1.if_data;
404 * Round i_bytes up to a word boundary.
405 * The underlying memory is guaranteed to
406 * to be there by xfs_idata_realloc().
408 data_bytes = roundup(ip->i_df.if_bytes, 4);
409 ASSERT((ip->i_df.if_real_bytes == 0) ||
410 (ip->i_df.if_real_bytes == data_bytes));
411 vecp->i_len = (int)data_bytes;
414 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
418 case XFS_DINODE_FMT_DEV:
419 ASSERT(!(iip->ili_format.ilf_fields &
420 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
421 XFS_ILOG_DDATA | XFS_ILOG_UUID)));
422 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
423 iip->ili_format.ilf_u.ilfu_rdev =
424 ip->i_df.if_u2.if_rdev;
428 case XFS_DINODE_FMT_UUID:
429 ASSERT(!(iip->ili_format.ilf_fields &
430 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
431 XFS_ILOG_DDATA | XFS_ILOG_DEV)));
432 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
433 iip->ili_format.ilf_u.ilfu_uuid =
434 ip->i_df.if_u2.if_uuid;
444 * If there are no attributes associated with the file,
446 * Assert that no attribute-related log flags are set.
448 if (!XFS_IFORK_Q(ip)) {
449 ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
450 iip->ili_format.ilf_size = nvecs;
451 ASSERT(!(iip->ili_format.ilf_fields &
452 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
456 switch (ip->i_d.di_aformat) {
457 case XFS_DINODE_FMT_EXTENTS:
458 ASSERT(!(iip->ili_format.ilf_fields &
459 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT)));
460 if (iip->ili_format.ilf_fields & XFS_ILOG_AEXT) {
461 ASSERT(ip->i_afp->if_bytes > 0);
462 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
463 ASSERT(ip->i_d.di_anextents > 0);
465 nrecs = ip->i_afp->if_bytes /
466 (uint)sizeof(xfs_bmbt_rec_t);
469 ASSERT(nrecs == ip->i_d.di_anextents);
470 #if __BYTE_ORDER == __BIG_ENDIAN
472 * There are not delayed allocation extents
473 * for attributes, so just point at the array.
475 vecp->i_addr = (char *)(ip->i_afp->if_u1.if_extents);
476 vecp->i_len = ip->i_afp->if_bytes;
478 ASSERT(iip->ili_aextents_buf == NULL);
480 * Need to endian flip before logging
482 ext_buffer = kmem_alloc(ip->i_afp->if_bytes,
484 iip->ili_aextents_buf = ext_buffer;
485 vecp->i_addr = (xfs_caddr_t)ext_buffer;
486 vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
489 iip->ili_format.ilf_asize = vecp->i_len;
495 case XFS_DINODE_FMT_BTREE:
496 ASSERT(!(iip->ili_format.ilf_fields &
497 (XFS_ILOG_ADATA | XFS_ILOG_AEXT)));
498 if (iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) {
499 ASSERT(ip->i_afp->if_broot_bytes > 0);
500 ASSERT(ip->i_afp->if_broot != NULL);
501 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_broot;
502 vecp->i_len = ip->i_afp->if_broot_bytes;
505 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
509 case XFS_DINODE_FMT_LOCAL:
510 ASSERT(!(iip->ili_format.ilf_fields &
511 (XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
512 if (iip->ili_format.ilf_fields & XFS_ILOG_ADATA) {
513 ASSERT(ip->i_afp->if_bytes > 0);
514 ASSERT(ip->i_afp->if_u1.if_data != NULL);
516 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_u1.if_data;
518 * Round i_bytes up to a word boundary.
519 * The underlying memory is guaranteed to
520 * to be there by xfs_idata_realloc().
522 data_bytes = roundup(ip->i_afp->if_bytes, 4);
523 ASSERT((ip->i_afp->if_real_bytes == 0) ||
524 (ip->i_afp->if_real_bytes == data_bytes));
525 vecp->i_len = (int)data_bytes;
528 iip->ili_format.ilf_asize = (unsigned)data_bytes;
537 ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
538 iip->ili_format.ilf_size = nvecs;
543 * This is called to pin the inode associated with the inode log
544 * item in memory so it cannot be written out. Do this by calling
545 * xfs_ipin() to bump the pin count in the inode while holding the
550 xfs_inode_log_item_t *iip)
552 ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
553 xfs_ipin(iip->ili_inode);
558 * This is called to unpin the inode associated with the inode log
559 * item which was previously pinned with a call to xfs_inode_item_pin().
560 * Just call xfs_iunpin() on the inode to do this.
564 xfs_inode_item_unpin(
565 xfs_inode_log_item_t *iip,
568 xfs_iunpin(iip->ili_inode);
573 xfs_inode_item_unpin_remove(
574 xfs_inode_log_item_t *iip,
577 xfs_iunpin(iip->ili_inode);
581 * This is called to attempt to lock the inode associated with this
582 * inode log item, in preparation for the push routine which does the actual
583 * iflush. Don't sleep on the inode lock or the flush lock.
585 * If the flush lock is already held, indicating that the inode has
586 * been or is in the process of being flushed, then (ideally) we'd like to
587 * see if the inode's buffer is still incore, and if so give it a nudge.
588 * We delay doing so until the pushbuf routine, though, to avoid holding
589 * the AIL lock across a call to the blackhole which is the buffercache.
590 * Also we don't want to sleep in any device strategy routines, which can happen
591 * if we do the subsequent bawrite in here.
594 xfs_inode_item_trylock(
595 xfs_inode_log_item_t *iip)
597 register xfs_inode_t *ip;
601 if (xfs_ipincount(ip) > 0) {
602 return XFS_ITEM_PINNED;
605 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
606 return XFS_ITEM_LOCKED;
609 if (!xfs_iflock_nowait(ip)) {
611 * If someone else isn't already trying to push the inode
612 * buffer, we get to do it.
614 if (iip->ili_pushbuf_flag == 0) {
615 iip->ili_pushbuf_flag = 1;
617 iip->ili_push_owner = get_thread_id();
620 * Inode is left locked in shared mode.
621 * Pushbuf routine gets to unlock it.
623 return XFS_ITEM_PUSHBUF;
626 * We hold the AIL_LOCK, so we must specify the
627 * NONOTIFY flag so that we won't double trip.
629 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
630 return XFS_ITEM_FLUSHING;
635 /* Stale items should force out the iclog */
636 if (ip->i_flags & XFS_ISTALE) {
638 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
639 return XFS_ITEM_PINNED;
643 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
644 ASSERT(iip->ili_format.ilf_fields != 0);
645 ASSERT(iip->ili_logged == 0);
646 ASSERT(iip->ili_item.li_flags & XFS_LI_IN_AIL);
649 return XFS_ITEM_SUCCESS;
653 * Unlock the inode associated with the inode log item.
654 * Clear the fields of the inode and inode log item that
655 * are specific to the current transaction. If the
656 * hold flags is set, do not unlock the inode.
659 xfs_inode_item_unlock(
660 xfs_inode_log_item_t *iip)
668 ASSERT(iip->ili_inode->i_itemp != NULL);
669 ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
670 ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
671 XFS_ILI_IOLOCKED_EXCL)) ||
672 ismrlocked(&(iip->ili_inode->i_iolock), MR_UPDATE));
673 ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
674 XFS_ILI_IOLOCKED_SHARED)) ||
675 ismrlocked(&(iip->ili_inode->i_iolock), MR_ACCESS));
677 * Clear the transaction pointer in the inode.
683 * If the inode needed a separate buffer with which to log
684 * its extents, then free it now.
686 if (iip->ili_extents_buf != NULL) {
687 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
688 ASSERT(ip->i_d.di_nextents > 0);
689 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
690 ASSERT(ip->i_df.if_bytes > 0);
691 kmem_free(iip->ili_extents_buf, ip->i_df.if_bytes);
692 iip->ili_extents_buf = NULL;
694 if (iip->ili_aextents_buf != NULL) {
695 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
696 ASSERT(ip->i_d.di_anextents > 0);
697 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
698 ASSERT(ip->i_afp->if_bytes > 0);
699 kmem_free(iip->ili_aextents_buf, ip->i_afp->if_bytes);
700 iip->ili_aextents_buf = NULL;
704 * Figure out if we should unlock the inode or not.
706 hold = iip->ili_flags & XFS_ILI_HOLD;
709 * Before clearing out the flags, remember whether we
710 * are holding the inode's IO lock.
712 iolocked = iip->ili_flags & XFS_ILI_IOLOCKED_ANY;
715 * Clear out the fields of the inode log item particular
716 * to the current transaction.
718 iip->ili_ilock_recur = 0;
719 iip->ili_iolock_recur = 0;
723 * Unlock the inode if XFS_ILI_HOLD was not set.
726 lock_flags = XFS_ILOCK_EXCL;
727 if (iolocked & XFS_ILI_IOLOCKED_EXCL) {
728 lock_flags |= XFS_IOLOCK_EXCL;
729 } else if (iolocked & XFS_ILI_IOLOCKED_SHARED) {
730 lock_flags |= XFS_IOLOCK_SHARED;
732 xfs_iput(iip->ili_inode, lock_flags);
737 * This is called to find out where the oldest active copy of the
738 * inode log item in the on disk log resides now that the last log
739 * write of it completed at the given lsn. Since we always re-log
740 * all dirty data in an inode, the latest copy in the on disk log
741 * is the only one that matters. Therefore, simply return the
746 xfs_inode_item_committed(
747 xfs_inode_log_item_t *iip,
754 * The transaction with the inode locked has aborted. The inode
755 * must not be dirty within the transaction (unless we're forcibly
756 * shutting down). We simply unlock just as if the transaction
757 * had been cancelled.
760 xfs_inode_item_abort(
761 xfs_inode_log_item_t *iip)
763 xfs_inode_item_unlock(iip);
769 * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
770 * failed to get the inode flush lock but did get the inode locked SHARED.
771 * Here we're trying to see if the inode buffer is incore, and if so whether it's
772 * marked delayed write. If that's the case, we'll initiate a bawrite on that
773 * buffer to expedite the process.
775 * We aren't holding the AIL_LOCK (or the flush lock) when this gets called,
776 * so it is inherently race-y.
779 xfs_inode_item_pushbuf(
780 xfs_inode_log_item_t *iip)
789 ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
792 * The ili_pushbuf_flag keeps others from
793 * trying to duplicate our effort.
795 ASSERT(iip->ili_pushbuf_flag != 0);
796 ASSERT(iip->ili_push_owner == get_thread_id());
799 * If flushlock isn't locked anymore, chances are that the
800 * inode flush completed and the inode was taken off the AIL.
803 if ((valusema(&(ip->i_flock)) > 0) ||
804 ((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0)) {
805 iip->ili_pushbuf_flag = 0;
806 xfs_iunlock(ip, XFS_ILOCK_SHARED);
811 bp = xfs_incore(mp->m_ddev_targp, iip->ili_format.ilf_blkno,
812 iip->ili_format.ilf_len, XFS_INCORE_TRYLOCK);
815 if (XFS_BUF_ISDELAYWRITE(bp)) {
817 * We were racing with iflush because we don't hold
818 * the AIL_LOCK or the flush lock. However, at this point,
819 * we have the buffer, and we know that it's dirty.
820 * So, it's possible that iflush raced with us, and
821 * this item is already taken off the AIL.
822 * If not, we can flush it async.
824 dopush = ((iip->ili_item.li_flags & XFS_LI_IN_AIL) &&
825 (valusema(&(ip->i_flock)) <= 0));
826 iip->ili_pushbuf_flag = 0;
827 xfs_iunlock(ip, XFS_ILOCK_SHARED);
828 xfs_buftrace("INODE ITEM PUSH", bp);
829 if (XFS_BUF_ISPINNED(bp)) {
830 xfs_log_force(mp, (xfs_lsn_t)0,
839 iip->ili_pushbuf_flag = 0;
840 xfs_iunlock(ip, XFS_ILOCK_SHARED);
846 * We have to be careful about resetting pushbuf flag too early (above).
847 * Even though in theory we can do it as soon as we have the buflock,
848 * we don't want others to be doing work needlessly. They'll come to
849 * this function thinking that pushing the buffer is their
850 * responsibility only to find that the buffer is still locked by
851 * another doing the same thing
853 iip->ili_pushbuf_flag = 0;
854 xfs_iunlock(ip, XFS_ILOCK_SHARED);
860 * This is called to asynchronously write the inode associated with this
861 * inode log item out to disk. The inode will already have been locked by
862 * a successful call to xfs_inode_item_trylock().
866 xfs_inode_log_item_t *iip)
872 ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
873 ASSERT(valusema(&(ip->i_flock)) <= 0);
875 * Since we were able to lock the inode's flush lock and
876 * we found it on the AIL, the inode must be dirty. This
877 * is because the inode is removed from the AIL while still
878 * holding the flush lock in xfs_iflush_done(). Thus, if
879 * we found it in the AIL and were able to obtain the flush
880 * lock without sleeping, then there must not have been
881 * anyone in the process of flushing the inode.
883 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
884 iip->ili_format.ilf_fields != 0);
887 * Write out the inode. The completion routine ('iflush_done') will
888 * pull it from the AIL, mark it clean, unlock the flush lock.
890 (void) xfs_iflush(ip, XFS_IFLUSH_ASYNC);
891 xfs_iunlock(ip, XFS_ILOCK_SHARED);
897 * XXX rcc - this one really has to do something. Probably needs
898 * to stamp in a new field in the incore inode.
902 xfs_inode_item_committing(
903 xfs_inode_log_item_t *iip,
906 iip->ili_last_lsn = lsn;
911 * This is the ops vector shared by all buf log items.
913 STATIC struct xfs_item_ops xfs_inode_item_ops = {
914 .iop_size = (uint(*)(xfs_log_item_t*))xfs_inode_item_size,
915 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
916 xfs_inode_item_format,
917 .iop_pin = (void(*)(xfs_log_item_t*))xfs_inode_item_pin,
918 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_inode_item_unpin,
919 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
920 xfs_inode_item_unpin_remove,
921 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_inode_item_trylock,
922 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_inode_item_unlock,
923 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
924 xfs_inode_item_committed,
925 .iop_push = (void(*)(xfs_log_item_t*))xfs_inode_item_push,
926 .iop_abort = (void(*)(xfs_log_item_t*))xfs_inode_item_abort,
927 .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_inode_item_pushbuf,
928 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
929 xfs_inode_item_committing
934 * Initialize the inode log item for a newly allocated (in-core) inode.
941 xfs_inode_log_item_t *iip;
943 ASSERT(ip->i_itemp == NULL);
944 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
946 iip->ili_item.li_type = XFS_LI_INODE;
947 iip->ili_item.li_ops = &xfs_inode_item_ops;
948 iip->ili_item.li_mountp = mp;
952 We have zeroed memory. No need ...
953 iip->ili_extents_buf = NULL;
954 iip->ili_pushbuf_flag = 0;
957 iip->ili_format.ilf_type = XFS_LI_INODE;
958 iip->ili_format.ilf_ino = ip->i_ino;
959 iip->ili_format.ilf_blkno = ip->i_blkno;
960 iip->ili_format.ilf_len = ip->i_len;
961 iip->ili_format.ilf_boffset = ip->i_boffset;
965 * Free the inode log item and any memory hanging off of it.
968 xfs_inode_item_destroy(
971 #ifdef XFS_TRANS_DEBUG
972 if (ip->i_itemp->ili_root_size != 0) {
973 kmem_free(ip->i_itemp->ili_orig_root,
974 ip->i_itemp->ili_root_size);
977 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
982 * This is the inode flushing I/O completion routine. It is called
983 * from interrupt level when the buffer containing the inode is
984 * flushed to disk. It is responsible for removing the inode item
985 * from the AIL if it has not been re-logged, and unlocking the inode's
992 xfs_inode_log_item_t *iip)
1000 * We only want to pull the item from the AIL if it is
1001 * actually there and its location in the log has not
1002 * changed since we started the flush. Thus, we only bother
1003 * if the ili_logged flag is set and the inode's lsn has not
1004 * changed. First we check the lsn outside
1005 * the lock since it's cheaper, and then we recheck while
1006 * holding the lock before removing the inode from the AIL.
1008 if (iip->ili_logged &&
1009 (iip->ili_item.li_lsn == iip->ili_flush_lsn)) {
1010 AIL_LOCK(ip->i_mount, s);
1011 if (iip->ili_item.li_lsn == iip->ili_flush_lsn) {
1013 * xfs_trans_delete_ail() drops the AIL lock.
1015 xfs_trans_delete_ail(ip->i_mount,
1016 (xfs_log_item_t*)iip, s);
1018 AIL_UNLOCK(ip->i_mount, s);
1022 iip->ili_logged = 0;
1025 * Clear the ili_last_fields bits now that we know that the
1026 * data corresponding to them is safely on disk.
1028 iip->ili_last_fields = 0;
1031 * Release the inode's flush lock since we're done with it.
1039 * This is the inode flushing abort routine. It is called
1040 * from xfs_iflush when the filesystem is shutting down to clean
1041 * up the inode state.
1042 * It is responsible for removing the inode item
1043 * from the AIL if it has not been re-logged, and unlocking the inode's
1050 xfs_inode_log_item_t *iip;
1057 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
1059 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
1061 * xfs_trans_delete_ail() drops the AIL lock.
1063 xfs_trans_delete_ail(mp, (xfs_log_item_t *)iip,
1068 iip->ili_logged = 0;
1070 * Clear the ili_last_fields bits now that we know that the
1071 * data corresponding to them is safely on disk.
1073 iip->ili_last_fields = 0;
1075 * Clear the inode logging fields so no more flushes are
1078 iip->ili_format.ilf_fields = 0;
1081 * Release the inode's flush lock since we're done with it.
1089 xfs_inode_log_item_t *iip)
1091 xfs_iflush_abort(iip->ili_inode);