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);
251 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IFORMAT);
256 * Clear i_update_core if the timestamps (or any other
257 * non-transactional modification) need flushing/logging
258 * and we're about to log them with the rest of the core.
260 * This is the same logic as xfs_iflush() but this code can't
261 * run at the same time as xfs_iflush because we're in commit
262 * processing here and so we have the inode lock held in
263 * exclusive mode. Although it doesn't really matter
264 * for the timestamps if both routines were to grab the
265 * timestamps or not. That would be ok.
267 * We clear i_update_core before copying out the data.
268 * This is for coordination with our timestamp updates
269 * that don't hold the inode lock. They will always
270 * update the timestamps BEFORE setting i_update_core,
271 * so if we clear i_update_core after they set it we
272 * are guaranteed to see their updates to the timestamps
273 * either here. Likewise, if they set it after we clear it
274 * here, we'll see it either on the next commit of this
275 * inode or the next time the inode gets flushed via
276 * xfs_iflush(). This depends on strongly ordered memory
277 * semantics, but we have that. We use the SYNCHRONIZE
278 * macro to make sure that the compiler does not reorder
279 * the i_update_core access below the data copy below.
281 if (ip->i_update_core) {
282 ip->i_update_core = 0;
287 * We don't have to worry about re-ordering here because
288 * the update_size field is protected by the inode lock
289 * and we have that held in exclusive mode.
291 if (ip->i_update_size)
292 ip->i_update_size = 0;
294 vecp->i_addr = (xfs_caddr_t)&ip->i_d;
295 vecp->i_len = sizeof(xfs_dinode_core_t);
296 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ICORE);
299 iip->ili_format.ilf_fields |= XFS_ILOG_CORE;
302 * If this is really an old format inode, then we need to
303 * log it as such. This means that we have to copy the link
304 * count from the new field to the old. We don't have to worry
305 * about the new fields, because nothing trusts them as long as
306 * the old inode version number is there. If the superblock already
307 * has a new version number, then we don't bother converting back.
310 ASSERT(ip->i_d.di_version == XFS_DINODE_VERSION_1 ||
311 XFS_SB_VERSION_HASNLINK(&mp->m_sb));
312 if (ip->i_d.di_version == XFS_DINODE_VERSION_1) {
313 if (!XFS_SB_VERSION_HASNLINK(&mp->m_sb)) {
317 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
318 ip->i_d.di_onlink = ip->i_d.di_nlink;
321 * The superblock version has already been bumped,
322 * so just make the conversion to the new inode
325 ip->i_d.di_version = XFS_DINODE_VERSION_2;
326 ip->i_d.di_onlink = 0;
327 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
331 switch (ip->i_d.di_format) {
332 case XFS_DINODE_FMT_EXTENTS:
333 ASSERT(!(iip->ili_format.ilf_fields &
334 (XFS_ILOG_DDATA | XFS_ILOG_DBROOT |
335 XFS_ILOG_DEV | XFS_ILOG_UUID)));
336 if (iip->ili_format.ilf_fields & XFS_ILOG_DEXT) {
337 ASSERT(ip->i_df.if_bytes > 0);
338 ASSERT(ip->i_df.if_u1.if_extents != NULL);
339 ASSERT(ip->i_d.di_nextents > 0);
340 ASSERT(iip->ili_extents_buf == NULL);
341 nrecs = ip->i_df.if_bytes /
342 (uint)sizeof(xfs_bmbt_rec_t);
344 #ifdef XFS_NATIVE_HOST
345 if (nrecs == ip->i_d.di_nextents) {
347 * There are no delayed allocation
348 * extents, so just point to the
349 * real extents array.
352 (char *)(ip->i_df.if_u1.if_extents);
353 vecp->i_len = ip->i_df.if_bytes;
354 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT);
359 * There are delayed allocation extents
360 * in the inode, or we need to convert
361 * the extents to on disk format.
362 * Use xfs_iextents_copy()
363 * to copy only the real extents into
364 * a separate buffer. We'll free the
365 * buffer in the unlock routine.
367 ext_buffer = kmem_alloc(ip->i_df.if_bytes,
369 iip->ili_extents_buf = ext_buffer;
370 vecp->i_addr = (xfs_caddr_t)ext_buffer;
371 vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
373 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT);
375 ASSERT(vecp->i_len <= ip->i_df.if_bytes);
376 iip->ili_format.ilf_dsize = vecp->i_len;
382 case XFS_DINODE_FMT_BTREE:
383 ASSERT(!(iip->ili_format.ilf_fields &
384 (XFS_ILOG_DDATA | XFS_ILOG_DEXT |
385 XFS_ILOG_DEV | XFS_ILOG_UUID)));
386 if (iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) {
387 ASSERT(ip->i_df.if_broot_bytes > 0);
388 ASSERT(ip->i_df.if_broot != NULL);
389 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_broot;
390 vecp->i_len = ip->i_df.if_broot_bytes;
391 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IBROOT);
394 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes;
398 case XFS_DINODE_FMT_LOCAL:
399 ASSERT(!(iip->ili_format.ilf_fields &
400 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
401 XFS_ILOG_DEV | XFS_ILOG_UUID)));
402 if (iip->ili_format.ilf_fields & XFS_ILOG_DDATA) {
403 ASSERT(ip->i_df.if_bytes > 0);
404 ASSERT(ip->i_df.if_u1.if_data != NULL);
405 ASSERT(ip->i_d.di_size > 0);
407 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_u1.if_data;
409 * Round i_bytes up to a word boundary.
410 * The underlying memory is guaranteed to
411 * to be there by xfs_idata_realloc().
413 data_bytes = roundup(ip->i_df.if_bytes, 4);
414 ASSERT((ip->i_df.if_real_bytes == 0) ||
415 (ip->i_df.if_real_bytes == data_bytes));
416 vecp->i_len = (int)data_bytes;
417 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ILOCAL);
420 iip->ili_format.ilf_dsize = (unsigned)data_bytes;
424 case XFS_DINODE_FMT_DEV:
425 ASSERT(!(iip->ili_format.ilf_fields &
426 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
427 XFS_ILOG_DDATA | XFS_ILOG_UUID)));
428 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
429 iip->ili_format.ilf_u.ilfu_rdev =
430 ip->i_df.if_u2.if_rdev;
434 case XFS_DINODE_FMT_UUID:
435 ASSERT(!(iip->ili_format.ilf_fields &
436 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT |
437 XFS_ILOG_DDATA | XFS_ILOG_DEV)));
438 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
439 iip->ili_format.ilf_u.ilfu_uuid =
440 ip->i_df.if_u2.if_uuid;
450 * If there are no attributes associated with the file,
452 * Assert that no attribute-related log flags are set.
454 if (!XFS_IFORK_Q(ip)) {
455 ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
456 iip->ili_format.ilf_size = nvecs;
457 ASSERT(!(iip->ili_format.ilf_fields &
458 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
462 switch (ip->i_d.di_aformat) {
463 case XFS_DINODE_FMT_EXTENTS:
464 ASSERT(!(iip->ili_format.ilf_fields &
465 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT)));
466 if (iip->ili_format.ilf_fields & XFS_ILOG_AEXT) {
467 ASSERT(ip->i_afp->if_bytes > 0);
468 ASSERT(ip->i_afp->if_u1.if_extents != NULL);
469 ASSERT(ip->i_d.di_anextents > 0);
471 nrecs = ip->i_afp->if_bytes /
472 (uint)sizeof(xfs_bmbt_rec_t);
475 ASSERT(nrecs == ip->i_d.di_anextents);
476 #ifdef XFS_NATIVE_HOST
478 * There are not delayed allocation extents
479 * for attributes, so just point at the array.
481 vecp->i_addr = (char *)(ip->i_afp->if_u1.if_extents);
482 vecp->i_len = ip->i_afp->if_bytes;
484 ASSERT(iip->ili_aextents_buf == NULL);
486 * Need to endian flip before logging
488 ext_buffer = kmem_alloc(ip->i_afp->if_bytes,
490 iip->ili_aextents_buf = ext_buffer;
491 vecp->i_addr = (xfs_caddr_t)ext_buffer;
492 vecp->i_len = xfs_iextents_copy(ip, ext_buffer,
495 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_EXT);
496 iip->ili_format.ilf_asize = vecp->i_len;
502 case XFS_DINODE_FMT_BTREE:
503 ASSERT(!(iip->ili_format.ilf_fields &
504 (XFS_ILOG_ADATA | XFS_ILOG_AEXT)));
505 if (iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) {
506 ASSERT(ip->i_afp->if_broot_bytes > 0);
507 ASSERT(ip->i_afp->if_broot != NULL);
508 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_broot;
509 vecp->i_len = ip->i_afp->if_broot_bytes;
510 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_BROOT);
513 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes;
517 case XFS_DINODE_FMT_LOCAL:
518 ASSERT(!(iip->ili_format.ilf_fields &
519 (XFS_ILOG_ABROOT | XFS_ILOG_AEXT)));
520 if (iip->ili_format.ilf_fields & XFS_ILOG_ADATA) {
521 ASSERT(ip->i_afp->if_bytes > 0);
522 ASSERT(ip->i_afp->if_u1.if_data != NULL);
524 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_u1.if_data;
526 * Round i_bytes up to a word boundary.
527 * The underlying memory is guaranteed to
528 * to be there by xfs_idata_realloc().
530 data_bytes = roundup(ip->i_afp->if_bytes, 4);
531 ASSERT((ip->i_afp->if_real_bytes == 0) ||
532 (ip->i_afp->if_real_bytes == data_bytes));
533 vecp->i_len = (int)data_bytes;
534 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_LOCAL);
537 iip->ili_format.ilf_asize = (unsigned)data_bytes;
546 ASSERT(nvecs == iip->ili_item.li_desc->lid_size);
547 iip->ili_format.ilf_size = nvecs;
552 * This is called to pin the inode associated with the inode log
553 * item in memory so it cannot be written out. Do this by calling
554 * xfs_ipin() to bump the pin count in the inode while holding the
559 xfs_inode_log_item_t *iip)
561 ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
562 xfs_ipin(iip->ili_inode);
567 * This is called to unpin the inode associated with the inode log
568 * item which was previously pinned with a call to xfs_inode_item_pin().
569 * Just call xfs_iunpin() on the inode to do this.
573 xfs_inode_item_unpin(
574 xfs_inode_log_item_t *iip,
577 xfs_iunpin(iip->ili_inode);
582 xfs_inode_item_unpin_remove(
583 xfs_inode_log_item_t *iip,
586 xfs_iunpin(iip->ili_inode);
590 * This is called to attempt to lock the inode associated with this
591 * inode log item, in preparation for the push routine which does the actual
592 * iflush. Don't sleep on the inode lock or the flush lock.
594 * If the flush lock is already held, indicating that the inode has
595 * been or is in the process of being flushed, then (ideally) we'd like to
596 * see if the inode's buffer is still incore, and if so give it a nudge.
597 * We delay doing so until the pushbuf routine, though, to avoid holding
598 * the AIL lock across a call to the blackhole which is the buffercache.
599 * Also we don't want to sleep in any device strategy routines, which can happen
600 * if we do the subsequent bawrite in here.
603 xfs_inode_item_trylock(
604 xfs_inode_log_item_t *iip)
606 register xfs_inode_t *ip;
610 if (xfs_ipincount(ip) > 0) {
611 return XFS_ITEM_PINNED;
614 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
615 return XFS_ITEM_LOCKED;
618 if (!xfs_iflock_nowait(ip)) {
620 * If someone else isn't already trying to push the inode
621 * buffer, we get to do it.
623 if (iip->ili_pushbuf_flag == 0) {
624 iip->ili_pushbuf_flag = 1;
626 iip->ili_push_owner = get_thread_id();
629 * Inode is left locked in shared mode.
630 * Pushbuf routine gets to unlock it.
632 return XFS_ITEM_PUSHBUF;
635 * We hold the AIL_LOCK, so we must specify the
636 * NONOTIFY flag so that we won't double trip.
638 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
639 return XFS_ITEM_FLUSHING;
644 /* Stale items should force out the iclog */
645 if (ip->i_flags & XFS_ISTALE) {
647 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY);
648 return XFS_ITEM_PINNED;
652 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
653 ASSERT(iip->ili_format.ilf_fields != 0);
654 ASSERT(iip->ili_logged == 0);
655 ASSERT(iip->ili_item.li_flags & XFS_LI_IN_AIL);
658 return XFS_ITEM_SUCCESS;
662 * Unlock the inode associated with the inode log item.
663 * Clear the fields of the inode and inode log item that
664 * are specific to the current transaction. If the
665 * hold flags is set, do not unlock the inode.
668 xfs_inode_item_unlock(
669 xfs_inode_log_item_t *iip)
677 ASSERT(iip->ili_inode->i_itemp != NULL);
678 ASSERT(ismrlocked(&(iip->ili_inode->i_lock), MR_UPDATE));
679 ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
680 XFS_ILI_IOLOCKED_EXCL)) ||
681 ismrlocked(&(iip->ili_inode->i_iolock), MR_UPDATE));
682 ASSERT((!(iip->ili_inode->i_itemp->ili_flags &
683 XFS_ILI_IOLOCKED_SHARED)) ||
684 ismrlocked(&(iip->ili_inode->i_iolock), MR_ACCESS));
686 * Clear the transaction pointer in the inode.
692 * If the inode needed a separate buffer with which to log
693 * its extents, then free it now.
695 if (iip->ili_extents_buf != NULL) {
696 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS);
697 ASSERT(ip->i_d.di_nextents > 0);
698 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT);
699 ASSERT(ip->i_df.if_bytes > 0);
700 kmem_free(iip->ili_extents_buf, ip->i_df.if_bytes);
701 iip->ili_extents_buf = NULL;
703 if (iip->ili_aextents_buf != NULL) {
704 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS);
705 ASSERT(ip->i_d.di_anextents > 0);
706 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT);
707 ASSERT(ip->i_afp->if_bytes > 0);
708 kmem_free(iip->ili_aextents_buf, ip->i_afp->if_bytes);
709 iip->ili_aextents_buf = NULL;
713 * Figure out if we should unlock the inode or not.
715 hold = iip->ili_flags & XFS_ILI_HOLD;
718 * Before clearing out the flags, remember whether we
719 * are holding the inode's IO lock.
721 iolocked = iip->ili_flags & XFS_ILI_IOLOCKED_ANY;
724 * Clear out the fields of the inode log item particular
725 * to the current transaction.
727 iip->ili_ilock_recur = 0;
728 iip->ili_iolock_recur = 0;
732 * Unlock the inode if XFS_ILI_HOLD was not set.
735 lock_flags = XFS_ILOCK_EXCL;
736 if (iolocked & XFS_ILI_IOLOCKED_EXCL) {
737 lock_flags |= XFS_IOLOCK_EXCL;
738 } else if (iolocked & XFS_ILI_IOLOCKED_SHARED) {
739 lock_flags |= XFS_IOLOCK_SHARED;
741 xfs_iput(iip->ili_inode, lock_flags);
746 * This is called to find out where the oldest active copy of the
747 * inode log item in the on disk log resides now that the last log
748 * write of it completed at the given lsn. Since we always re-log
749 * all dirty data in an inode, the latest copy in the on disk log
750 * is the only one that matters. Therefore, simply return the
755 xfs_inode_item_committed(
756 xfs_inode_log_item_t *iip,
763 * The transaction with the inode locked has aborted. The inode
764 * must not be dirty within the transaction (unless we're forcibly
765 * shutting down). We simply unlock just as if the transaction
766 * had been cancelled.
769 xfs_inode_item_abort(
770 xfs_inode_log_item_t *iip)
772 xfs_inode_item_unlock(iip);
778 * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK
779 * failed to get the inode flush lock but did get the inode locked SHARED.
780 * Here we're trying to see if the inode buffer is incore, and if so whether it's
781 * marked delayed write. If that's the case, we'll initiate a bawrite on that
782 * buffer to expedite the process.
784 * We aren't holding the AIL_LOCK (or the flush lock) when this gets called,
785 * so it is inherently race-y.
788 xfs_inode_item_pushbuf(
789 xfs_inode_log_item_t *iip)
798 ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
801 * The ili_pushbuf_flag keeps others from
802 * trying to duplicate our effort.
804 ASSERT(iip->ili_pushbuf_flag != 0);
805 ASSERT(iip->ili_push_owner == get_thread_id());
808 * If flushlock isn't locked anymore, chances are that the
809 * inode flush completed and the inode was taken off the AIL.
812 if ((valusema(&(ip->i_flock)) > 0) ||
813 ((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0)) {
814 iip->ili_pushbuf_flag = 0;
815 xfs_iunlock(ip, XFS_ILOCK_SHARED);
820 bp = xfs_incore(mp->m_ddev_targp, iip->ili_format.ilf_blkno,
821 iip->ili_format.ilf_len, XFS_INCORE_TRYLOCK);
824 if (XFS_BUF_ISDELAYWRITE(bp)) {
826 * We were racing with iflush because we don't hold
827 * the AIL_LOCK or the flush lock. However, at this point,
828 * we have the buffer, and we know that it's dirty.
829 * So, it's possible that iflush raced with us, and
830 * this item is already taken off the AIL.
831 * If not, we can flush it async.
833 dopush = ((iip->ili_item.li_flags & XFS_LI_IN_AIL) &&
834 (valusema(&(ip->i_flock)) <= 0));
835 iip->ili_pushbuf_flag = 0;
836 xfs_iunlock(ip, XFS_ILOCK_SHARED);
837 xfs_buftrace("INODE ITEM PUSH", bp);
838 if (XFS_BUF_ISPINNED(bp)) {
839 xfs_log_force(mp, (xfs_lsn_t)0,
848 iip->ili_pushbuf_flag = 0;
849 xfs_iunlock(ip, XFS_ILOCK_SHARED);
855 * We have to be careful about resetting pushbuf flag too early (above).
856 * Even though in theory we can do it as soon as we have the buflock,
857 * we don't want others to be doing work needlessly. They'll come to
858 * this function thinking that pushing the buffer is their
859 * responsibility only to find that the buffer is still locked by
860 * another doing the same thing
862 iip->ili_pushbuf_flag = 0;
863 xfs_iunlock(ip, XFS_ILOCK_SHARED);
869 * This is called to asynchronously write the inode associated with this
870 * inode log item out to disk. The inode will already have been locked by
871 * a successful call to xfs_inode_item_trylock().
875 xfs_inode_log_item_t *iip)
881 ASSERT(ismrlocked(&(ip->i_lock), MR_ACCESS));
882 ASSERT(valusema(&(ip->i_flock)) <= 0);
884 * Since we were able to lock the inode's flush lock and
885 * we found it on the AIL, the inode must be dirty. This
886 * is because the inode is removed from the AIL while still
887 * holding the flush lock in xfs_iflush_done(). Thus, if
888 * we found it in the AIL and were able to obtain the flush
889 * lock without sleeping, then there must not have been
890 * anyone in the process of flushing the inode.
892 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
893 iip->ili_format.ilf_fields != 0);
896 * Write out the inode. The completion routine ('iflush_done') will
897 * pull it from the AIL, mark it clean, unlock the flush lock.
899 (void) xfs_iflush(ip, XFS_IFLUSH_ASYNC);
900 xfs_iunlock(ip, XFS_ILOCK_SHARED);
906 * XXX rcc - this one really has to do something. Probably needs
907 * to stamp in a new field in the incore inode.
911 xfs_inode_item_committing(
912 xfs_inode_log_item_t *iip,
915 iip->ili_last_lsn = lsn;
920 * This is the ops vector shared by all buf log items.
922 STATIC struct xfs_item_ops xfs_inode_item_ops = {
923 .iop_size = (uint(*)(xfs_log_item_t*))xfs_inode_item_size,
924 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
925 xfs_inode_item_format,
926 .iop_pin = (void(*)(xfs_log_item_t*))xfs_inode_item_pin,
927 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_inode_item_unpin,
928 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
929 xfs_inode_item_unpin_remove,
930 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_inode_item_trylock,
931 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_inode_item_unlock,
932 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
933 xfs_inode_item_committed,
934 .iop_push = (void(*)(xfs_log_item_t*))xfs_inode_item_push,
935 .iop_abort = (void(*)(xfs_log_item_t*))xfs_inode_item_abort,
936 .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_inode_item_pushbuf,
937 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
938 xfs_inode_item_committing
943 * Initialize the inode log item for a newly allocated (in-core) inode.
950 xfs_inode_log_item_t *iip;
952 ASSERT(ip->i_itemp == NULL);
953 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
955 iip->ili_item.li_type = XFS_LI_INODE;
956 iip->ili_item.li_ops = &xfs_inode_item_ops;
957 iip->ili_item.li_mountp = mp;
961 We have zeroed memory. No need ...
962 iip->ili_extents_buf = NULL;
963 iip->ili_pushbuf_flag = 0;
966 iip->ili_format.ilf_type = XFS_LI_INODE;
967 iip->ili_format.ilf_ino = ip->i_ino;
968 iip->ili_format.ilf_blkno = ip->i_blkno;
969 iip->ili_format.ilf_len = ip->i_len;
970 iip->ili_format.ilf_boffset = ip->i_boffset;
974 * Free the inode log item and any memory hanging off of it.
977 xfs_inode_item_destroy(
980 #ifdef XFS_TRANS_DEBUG
981 if (ip->i_itemp->ili_root_size != 0) {
982 kmem_free(ip->i_itemp->ili_orig_root,
983 ip->i_itemp->ili_root_size);
986 kmem_zone_free(xfs_ili_zone, ip->i_itemp);
991 * This is the inode flushing I/O completion routine. It is called
992 * from interrupt level when the buffer containing the inode is
993 * flushed to disk. It is responsible for removing the inode item
994 * from the AIL if it has not been re-logged, and unlocking the inode's
1001 xfs_inode_log_item_t *iip)
1006 ip = iip->ili_inode;
1009 * We only want to pull the item from the AIL if it is
1010 * actually there and its location in the log has not
1011 * changed since we started the flush. Thus, we only bother
1012 * if the ili_logged flag is set and the inode's lsn has not
1013 * changed. First we check the lsn outside
1014 * the lock since it's cheaper, and then we recheck while
1015 * holding the lock before removing the inode from the AIL.
1017 if (iip->ili_logged &&
1018 (iip->ili_item.li_lsn == iip->ili_flush_lsn)) {
1019 AIL_LOCK(ip->i_mount, s);
1020 if (iip->ili_item.li_lsn == iip->ili_flush_lsn) {
1022 * xfs_trans_delete_ail() drops the AIL lock.
1024 xfs_trans_delete_ail(ip->i_mount,
1025 (xfs_log_item_t*)iip, s);
1027 AIL_UNLOCK(ip->i_mount, s);
1031 iip->ili_logged = 0;
1034 * Clear the ili_last_fields bits now that we know that the
1035 * data corresponding to them is safely on disk.
1037 iip->ili_last_fields = 0;
1040 * Release the inode's flush lock since we're done with it.
1048 * This is the inode flushing abort routine. It is called
1049 * from xfs_iflush when the filesystem is shutting down to clean
1050 * up the inode state.
1051 * It is responsible for removing the inode item
1052 * from the AIL if it has not been re-logged, and unlocking the inode's
1059 xfs_inode_log_item_t *iip;
1066 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
1068 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) {
1070 * xfs_trans_delete_ail() drops the AIL lock.
1072 xfs_trans_delete_ail(mp, (xfs_log_item_t *)iip,
1077 iip->ili_logged = 0;
1079 * Clear the ili_last_fields bits now that we know that the
1080 * data corresponding to them is safely on disk.
1082 iip->ili_last_fields = 0;
1084 * Clear the inode logging fields so no more flushes are
1087 iip->ili_format.ilf_fields = 0;
1090 * Release the inode's flush lock since we're done with it.
1098 xfs_inode_log_item_t *iip)
1100 xfs_iflush_abort(iip->ili_inode);