linux/fs/xfs/xfs_inode_item.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
   3 * All Rights Reserved.
   4 *
   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.
   8 *
   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.
  13 *
  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
  17 */
  18#include "xfs.h"
  19#include "xfs_fs.h"
  20#include "xfs_format.h"
  21#include "xfs_log_format.h"
  22#include "xfs_trans_resv.h"
  23#include "xfs_mount.h"
  24#include "xfs_inode.h"
  25#include "xfs_trans.h"
  26#include "xfs_inode_item.h"
  27#include "xfs_error.h"
  28#include "xfs_trace.h"
  29#include "xfs_trans_priv.h"
  30#include "xfs_buf_item.h"
  31#include "xfs_log.h"
  32
  33
  34kmem_zone_t     *xfs_ili_zone;          /* inode log item zone */
  35
  36static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
  37{
  38        return container_of(lip, struct xfs_inode_log_item, ili_item);
  39}
  40
  41STATIC void
  42xfs_inode_item_data_fork_size(
  43        struct xfs_inode_log_item *iip,
  44        int                     *nvecs,
  45        int                     *nbytes)
  46{
  47        struct xfs_inode        *ip = iip->ili_inode;
  48
  49        switch (ip->i_d.di_format) {
  50        case XFS_DINODE_FMT_EXTENTS:
  51                if ((iip->ili_fields & XFS_ILOG_DEXT) &&
  52                    ip->i_d.di_nextents > 0 &&
  53                    ip->i_df.if_bytes > 0) {
  54                        /* worst case, doesn't subtract delalloc extents */
  55                        *nbytes += XFS_IFORK_DSIZE(ip);
  56                        *nvecs += 1;
  57                }
  58                break;
  59        case XFS_DINODE_FMT_BTREE:
  60                if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
  61                    ip->i_df.if_broot_bytes > 0) {
  62                        *nbytes += ip->i_df.if_broot_bytes;
  63                        *nvecs += 1;
  64                }
  65                break;
  66        case XFS_DINODE_FMT_LOCAL:
  67                if ((iip->ili_fields & XFS_ILOG_DDATA) &&
  68                    ip->i_df.if_bytes > 0) {
  69                        *nbytes += roundup(ip->i_df.if_bytes, 4);
  70                        *nvecs += 1;
  71                }
  72                break;
  73
  74        case XFS_DINODE_FMT_DEV:
  75                break;
  76        default:
  77                ASSERT(0);
  78                break;
  79        }
  80}
  81
  82STATIC void
  83xfs_inode_item_attr_fork_size(
  84        struct xfs_inode_log_item *iip,
  85        int                     *nvecs,
  86        int                     *nbytes)
  87{
  88        struct xfs_inode        *ip = iip->ili_inode;
  89
  90        switch (ip->i_d.di_aformat) {
  91        case XFS_DINODE_FMT_EXTENTS:
  92                if ((iip->ili_fields & XFS_ILOG_AEXT) &&
  93                    ip->i_d.di_anextents > 0 &&
  94                    ip->i_afp->if_bytes > 0) {
  95                        /* worst case, doesn't subtract unused space */
  96                        *nbytes += XFS_IFORK_ASIZE(ip);
  97                        *nvecs += 1;
  98                }
  99                break;
 100        case XFS_DINODE_FMT_BTREE:
 101                if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
 102                    ip->i_afp->if_broot_bytes > 0) {
 103                        *nbytes += ip->i_afp->if_broot_bytes;
 104                        *nvecs += 1;
 105                }
 106                break;
 107        case XFS_DINODE_FMT_LOCAL:
 108                if ((iip->ili_fields & XFS_ILOG_ADATA) &&
 109                    ip->i_afp->if_bytes > 0) {
 110                        *nbytes += roundup(ip->i_afp->if_bytes, 4);
 111                        *nvecs += 1;
 112                }
 113                break;
 114        default:
 115                ASSERT(0);
 116                break;
 117        }
 118}
 119
 120/*
 121 * This returns the number of iovecs needed to log the given inode item.
 122 *
 123 * We need one iovec for the inode log format structure, one for the
 124 * inode core, and possibly one for the inode data/extents/b-tree root
 125 * and one for the inode attribute data/extents/b-tree root.
 126 */
 127STATIC void
 128xfs_inode_item_size(
 129        struct xfs_log_item     *lip,
 130        int                     *nvecs,
 131        int                     *nbytes)
 132{
 133        struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 134        struct xfs_inode        *ip = iip->ili_inode;
 135
 136        *nvecs += 2;
 137        *nbytes += sizeof(struct xfs_inode_log_format) +
 138                   xfs_log_dinode_size(ip->i_d.di_version);
 139
 140        xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
 141        if (XFS_IFORK_Q(ip))
 142                xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
 143}
 144
 145STATIC void
 146xfs_inode_item_format_data_fork(
 147        struct xfs_inode_log_item *iip,
 148        struct xfs_inode_log_format *ilf,
 149        struct xfs_log_vec      *lv,
 150        struct xfs_log_iovec    **vecp)
 151{
 152        struct xfs_inode        *ip = iip->ili_inode;
 153        size_t                  data_bytes;
 154
 155        switch (ip->i_d.di_format) {
 156        case XFS_DINODE_FMT_EXTENTS:
 157                iip->ili_fields &=
 158                        ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
 159
 160                if ((iip->ili_fields & XFS_ILOG_DEXT) &&
 161                    ip->i_d.di_nextents > 0 &&
 162                    ip->i_df.if_bytes > 0) {
 163                        struct xfs_bmbt_rec *p;
 164
 165                        ASSERT(xfs_iext_count(&ip->i_df) > 0);
 166
 167                        p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
 168                        data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
 169                        xlog_finish_iovec(lv, *vecp, data_bytes);
 170
 171                        ASSERT(data_bytes <= ip->i_df.if_bytes);
 172
 173                        ilf->ilf_dsize = data_bytes;
 174                        ilf->ilf_size++;
 175                } else {
 176                        iip->ili_fields &= ~XFS_ILOG_DEXT;
 177                }
 178                break;
 179        case XFS_DINODE_FMT_BTREE:
 180                iip->ili_fields &=
 181                        ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
 182
 183                if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
 184                    ip->i_df.if_broot_bytes > 0) {
 185                        ASSERT(ip->i_df.if_broot != NULL);
 186                        xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
 187                                        ip->i_df.if_broot,
 188                                        ip->i_df.if_broot_bytes);
 189                        ilf->ilf_dsize = ip->i_df.if_broot_bytes;
 190                        ilf->ilf_size++;
 191                } else {
 192                        ASSERT(!(iip->ili_fields &
 193                                 XFS_ILOG_DBROOT));
 194                        iip->ili_fields &= ~XFS_ILOG_DBROOT;
 195                }
 196                break;
 197        case XFS_DINODE_FMT_LOCAL:
 198                iip->ili_fields &=
 199                        ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
 200                if ((iip->ili_fields & XFS_ILOG_DDATA) &&
 201                    ip->i_df.if_bytes > 0) {
 202                        /*
 203                         * Round i_bytes up to a word boundary.
 204                         * The underlying memory is guaranteed to
 205                         * to be there by xfs_idata_realloc().
 206                         */
 207                        data_bytes = roundup(ip->i_df.if_bytes, 4);
 208                        ASSERT(ip->i_df.if_u1.if_data != NULL);
 209                        ASSERT(ip->i_d.di_size > 0);
 210                        xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
 211                                        ip->i_df.if_u1.if_data, data_bytes);
 212                        ilf->ilf_dsize = (unsigned)data_bytes;
 213                        ilf->ilf_size++;
 214                } else {
 215                        iip->ili_fields &= ~XFS_ILOG_DDATA;
 216                }
 217                break;
 218        case XFS_DINODE_FMT_DEV:
 219                iip->ili_fields &=
 220                        ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
 221                if (iip->ili_fields & XFS_ILOG_DEV)
 222                        ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev);
 223                break;
 224        default:
 225                ASSERT(0);
 226                break;
 227        }
 228}
 229
 230STATIC void
 231xfs_inode_item_format_attr_fork(
 232        struct xfs_inode_log_item *iip,
 233        struct xfs_inode_log_format *ilf,
 234        struct xfs_log_vec      *lv,
 235        struct xfs_log_iovec    **vecp)
 236{
 237        struct xfs_inode        *ip = iip->ili_inode;
 238        size_t                  data_bytes;
 239
 240        switch (ip->i_d.di_aformat) {
 241        case XFS_DINODE_FMT_EXTENTS:
 242                iip->ili_fields &=
 243                        ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
 244
 245                if ((iip->ili_fields & XFS_ILOG_AEXT) &&
 246                    ip->i_d.di_anextents > 0 &&
 247                    ip->i_afp->if_bytes > 0) {
 248                        struct xfs_bmbt_rec *p;
 249
 250                        ASSERT(xfs_iext_count(ip->i_afp) ==
 251                                ip->i_d.di_anextents);
 252
 253                        p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
 254                        data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
 255                        xlog_finish_iovec(lv, *vecp, data_bytes);
 256
 257                        ilf->ilf_asize = data_bytes;
 258                        ilf->ilf_size++;
 259                } else {
 260                        iip->ili_fields &= ~XFS_ILOG_AEXT;
 261                }
 262                break;
 263        case XFS_DINODE_FMT_BTREE:
 264                iip->ili_fields &=
 265                        ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
 266
 267                if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
 268                    ip->i_afp->if_broot_bytes > 0) {
 269                        ASSERT(ip->i_afp->if_broot != NULL);
 270
 271                        xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
 272                                        ip->i_afp->if_broot,
 273                                        ip->i_afp->if_broot_bytes);
 274                        ilf->ilf_asize = ip->i_afp->if_broot_bytes;
 275                        ilf->ilf_size++;
 276                } else {
 277                        iip->ili_fields &= ~XFS_ILOG_ABROOT;
 278                }
 279                break;
 280        case XFS_DINODE_FMT_LOCAL:
 281                iip->ili_fields &=
 282                        ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
 283
 284                if ((iip->ili_fields & XFS_ILOG_ADATA) &&
 285                    ip->i_afp->if_bytes > 0) {
 286                        /*
 287                         * Round i_bytes up to a word boundary.
 288                         * The underlying memory is guaranteed to
 289                         * to be there by xfs_idata_realloc().
 290                         */
 291                        data_bytes = roundup(ip->i_afp->if_bytes, 4);
 292                        ASSERT(ip->i_afp->if_u1.if_data != NULL);
 293                        xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
 294                                        ip->i_afp->if_u1.if_data,
 295                                        data_bytes);
 296                        ilf->ilf_asize = (unsigned)data_bytes;
 297                        ilf->ilf_size++;
 298                } else {
 299                        iip->ili_fields &= ~XFS_ILOG_ADATA;
 300                }
 301                break;
 302        default:
 303                ASSERT(0);
 304                break;
 305        }
 306}
 307
 308static void
 309xfs_inode_to_log_dinode(
 310        struct xfs_inode        *ip,
 311        struct xfs_log_dinode   *to,
 312        xfs_lsn_t               lsn)
 313{
 314        struct xfs_icdinode     *from = &ip->i_d;
 315        struct inode            *inode = VFS_I(ip);
 316
 317        to->di_magic = XFS_DINODE_MAGIC;
 318
 319        to->di_version = from->di_version;
 320        to->di_format = from->di_format;
 321        to->di_uid = from->di_uid;
 322        to->di_gid = from->di_gid;
 323        to->di_projid_lo = from->di_projid_lo;
 324        to->di_projid_hi = from->di_projid_hi;
 325
 326        memset(to->di_pad, 0, sizeof(to->di_pad));
 327        memset(to->di_pad3, 0, sizeof(to->di_pad3));
 328        to->di_atime.t_sec = inode->i_atime.tv_sec;
 329        to->di_atime.t_nsec = inode->i_atime.tv_nsec;
 330        to->di_mtime.t_sec = inode->i_mtime.tv_sec;
 331        to->di_mtime.t_nsec = inode->i_mtime.tv_nsec;
 332        to->di_ctime.t_sec = inode->i_ctime.tv_sec;
 333        to->di_ctime.t_nsec = inode->i_ctime.tv_nsec;
 334        to->di_nlink = inode->i_nlink;
 335        to->di_gen = inode->i_generation;
 336        to->di_mode = inode->i_mode;
 337
 338        to->di_size = from->di_size;
 339        to->di_nblocks = from->di_nblocks;
 340        to->di_extsize = from->di_extsize;
 341        to->di_nextents = from->di_nextents;
 342        to->di_anextents = from->di_anextents;
 343        to->di_forkoff = from->di_forkoff;
 344        to->di_aformat = from->di_aformat;
 345        to->di_dmevmask = from->di_dmevmask;
 346        to->di_dmstate = from->di_dmstate;
 347        to->di_flags = from->di_flags;
 348
 349        /* log a dummy value to ensure log structure is fully initialised */
 350        to->di_next_unlinked = NULLAGINO;
 351
 352        if (from->di_version == 3) {
 353                to->di_changecount = inode->i_version;
 354                to->di_crtime.t_sec = from->di_crtime.t_sec;
 355                to->di_crtime.t_nsec = from->di_crtime.t_nsec;
 356                to->di_flags2 = from->di_flags2;
 357
 358                to->di_ino = ip->i_ino;
 359                to->di_lsn = lsn;
 360                memset(to->di_pad2, 0, sizeof(to->di_pad2));
 361                uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
 362                to->di_flushiter = 0;
 363        } else {
 364                to->di_flushiter = from->di_flushiter;
 365        }
 366}
 367
 368/*
 369 * Format the inode core. Current timestamp data is only in the VFS inode
 370 * fields, so we need to grab them from there. Hence rather than just copying
 371 * the XFS inode core structure, format the fields directly into the iovec.
 372 */
 373static void
 374xfs_inode_item_format_core(
 375        struct xfs_inode        *ip,
 376        struct xfs_log_vec      *lv,
 377        struct xfs_log_iovec    **vecp)
 378{
 379        struct xfs_log_dinode   *dic;
 380
 381        dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
 382        xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
 383        xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_d.di_version));
 384}
 385
 386/*
 387 * This is called to fill in the vector of log iovecs for the given inode
 388 * log item.  It fills the first item with an inode log format structure,
 389 * the second with the on-disk inode structure, and a possible third and/or
 390 * fourth with the inode data/extents/b-tree root and inode attributes
 391 * data/extents/b-tree root.
 392 *
 393 * Note: Always use the 64 bit inode log format structure so we don't
 394 * leave an uninitialised hole in the format item on 64 bit systems. Log
 395 * recovery on 32 bit systems handles this just fine, so there's no reason
 396 * for not using an initialising the properly padded structure all the time.
 397 */
 398STATIC void
 399xfs_inode_item_format(
 400        struct xfs_log_item     *lip,
 401        struct xfs_log_vec      *lv)
 402{
 403        struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 404        struct xfs_inode        *ip = iip->ili_inode;
 405        struct xfs_log_iovec    *vecp = NULL;
 406        struct xfs_inode_log_format *ilf;
 407
 408        ASSERT(ip->i_d.di_version > 1);
 409
 410        ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
 411        ilf->ilf_type = XFS_LI_INODE;
 412        ilf->ilf_ino = ip->i_ino;
 413        ilf->ilf_blkno = ip->i_imap.im_blkno;
 414        ilf->ilf_len = ip->i_imap.im_len;
 415        ilf->ilf_boffset = ip->i_imap.im_boffset;
 416        ilf->ilf_fields = XFS_ILOG_CORE;
 417        ilf->ilf_size = 2; /* format + core */
 418
 419        /*
 420         * make sure we don't leak uninitialised data into the log in the case
 421         * when we don't log every field in the inode.
 422         */
 423        ilf->ilf_dsize = 0;
 424        ilf->ilf_asize = 0;
 425        ilf->ilf_pad = 0;
 426        memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
 427
 428        xlog_finish_iovec(lv, vecp, sizeof(*ilf));
 429
 430        xfs_inode_item_format_core(ip, lv, &vecp);
 431        xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
 432        if (XFS_IFORK_Q(ip)) {
 433                xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
 434        } else {
 435                iip->ili_fields &=
 436                        ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
 437        }
 438
 439        /* update the format with the exact fields we actually logged */
 440        ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
 441}
 442
 443/*
 444 * This is called to pin the inode associated with the inode log
 445 * item in memory so it cannot be written out.
 446 */
 447STATIC void
 448xfs_inode_item_pin(
 449        struct xfs_log_item     *lip)
 450{
 451        struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
 452
 453        ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
 454
 455        trace_xfs_inode_pin(ip, _RET_IP_);
 456        atomic_inc(&ip->i_pincount);
 457}
 458
 459
 460/*
 461 * This is called to unpin the inode associated with the inode log
 462 * item which was previously pinned with a call to xfs_inode_item_pin().
 463 *
 464 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
 465 */
 466STATIC void
 467xfs_inode_item_unpin(
 468        struct xfs_log_item     *lip,
 469        int                     remove)
 470{
 471        struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
 472
 473        trace_xfs_inode_unpin(ip, _RET_IP_);
 474        ASSERT(atomic_read(&ip->i_pincount) > 0);
 475        if (atomic_dec_and_test(&ip->i_pincount))
 476                wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
 477}
 478
 479/*
 480 * Callback used to mark a buffer with XFS_LI_FAILED when items in the buffer
 481 * have been failed during writeback
 482 *
 483 * This informs the AIL that the inode is already flush locked on the next push,
 484 * and acquires a hold on the buffer to ensure that it isn't reclaimed before
 485 * dirty data makes it to disk.
 486 */
 487STATIC void
 488xfs_inode_item_error(
 489        struct xfs_log_item     *lip,
 490        struct xfs_buf          *bp)
 491{
 492        ASSERT(xfs_isiflocked(INODE_ITEM(lip)->ili_inode));
 493        xfs_set_li_failed(lip, bp);
 494}
 495
 496STATIC uint
 497xfs_inode_item_push(
 498        struct xfs_log_item     *lip,
 499        struct list_head        *buffer_list)
 500                __releases(&lip->li_ailp->xa_lock)
 501                __acquires(&lip->li_ailp->xa_lock)
 502{
 503        struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 504        struct xfs_inode        *ip = iip->ili_inode;
 505        struct xfs_buf          *bp = lip->li_buf;
 506        uint                    rval = XFS_ITEM_SUCCESS;
 507        int                     error;
 508
 509        if (xfs_ipincount(ip) > 0)
 510                return XFS_ITEM_PINNED;
 511
 512        /*
 513         * The buffer containing this item failed to be written back
 514         * previously. Resubmit the buffer for IO.
 515         */
 516        if (test_bit(XFS_LI_FAILED, &lip->li_flags)) {
 517                if (!xfs_buf_trylock(bp))
 518                        return XFS_ITEM_LOCKED;
 519
 520                if (!xfs_buf_resubmit_failed_buffers(bp, lip, buffer_list))
 521                        rval = XFS_ITEM_FLUSHING;
 522
 523                xfs_buf_unlock(bp);
 524                return rval;
 525        }
 526
 527        if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
 528                return XFS_ITEM_LOCKED;
 529
 530        /*
 531         * Re-check the pincount now that we stabilized the value by
 532         * taking the ilock.
 533         */
 534        if (xfs_ipincount(ip) > 0) {
 535                rval = XFS_ITEM_PINNED;
 536                goto out_unlock;
 537        }
 538
 539        /*
 540         * Stale inode items should force out the iclog.
 541         */
 542        if (ip->i_flags & XFS_ISTALE) {
 543                rval = XFS_ITEM_PINNED;
 544                goto out_unlock;
 545        }
 546
 547        /*
 548         * Someone else is already flushing the inode.  Nothing we can do
 549         * here but wait for the flush to finish and remove the item from
 550         * the AIL.
 551         */
 552        if (!xfs_iflock_nowait(ip)) {
 553                rval = XFS_ITEM_FLUSHING;
 554                goto out_unlock;
 555        }
 556
 557        ASSERT(iip->ili_fields != 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
 558        ASSERT(iip->ili_logged == 0 || XFS_FORCED_SHUTDOWN(ip->i_mount));
 559
 560        spin_unlock(&lip->li_ailp->xa_lock);
 561
 562        error = xfs_iflush(ip, &bp);
 563        if (!error) {
 564                if (!xfs_buf_delwri_queue(bp, buffer_list))
 565                        rval = XFS_ITEM_FLUSHING;
 566                xfs_buf_relse(bp);
 567        }
 568
 569        spin_lock(&lip->li_ailp->xa_lock);
 570out_unlock:
 571        xfs_iunlock(ip, XFS_ILOCK_SHARED);
 572        return rval;
 573}
 574
 575/*
 576 * Unlock the inode associated with the inode log item.
 577 */
 578STATIC void
 579xfs_inode_item_unlock(
 580        struct xfs_log_item     *lip)
 581{
 582        struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 583        struct xfs_inode        *ip = iip->ili_inode;
 584        unsigned short          lock_flags;
 585
 586        ASSERT(ip->i_itemp != NULL);
 587        ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
 588
 589        lock_flags = iip->ili_lock_flags;
 590        iip->ili_lock_flags = 0;
 591        if (lock_flags)
 592                xfs_iunlock(ip, lock_flags);
 593}
 594
 595/*
 596 * This is called to find out where the oldest active copy of the inode log
 597 * item in the on disk log resides now that the last log write of it completed
 598 * at the given lsn.  Since we always re-log all dirty data in an inode, the
 599 * latest copy in the on disk log is the only one that matters.  Therefore,
 600 * simply return the given lsn.
 601 *
 602 * If the inode has been marked stale because the cluster is being freed, we
 603 * don't want to (re-)insert this inode into the AIL. There is a race condition
 604 * where the cluster buffer may be unpinned before the inode is inserted into
 605 * the AIL during transaction committed processing. If the buffer is unpinned
 606 * before the inode item has been committed and inserted, then it is possible
 607 * for the buffer to be written and IO completes before the inode is inserted
 608 * into the AIL. In that case, we'd be inserting a clean, stale inode into the
 609 * AIL which will never get removed. It will, however, get reclaimed which
 610 * triggers an assert in xfs_inode_free() complaining about freein an inode
 611 * still in the AIL.
 612 *
 613 * To avoid this, just unpin the inode directly and return a LSN of -1 so the
 614 * transaction committed code knows that it does not need to do any further
 615 * processing on the item.
 616 */
 617STATIC xfs_lsn_t
 618xfs_inode_item_committed(
 619        struct xfs_log_item     *lip,
 620        xfs_lsn_t               lsn)
 621{
 622        struct xfs_inode_log_item *iip = INODE_ITEM(lip);
 623        struct xfs_inode        *ip = iip->ili_inode;
 624
 625        if (xfs_iflags_test(ip, XFS_ISTALE)) {
 626                xfs_inode_item_unpin(lip, 0);
 627                return -1;
 628        }
 629        return lsn;
 630}
 631
 632STATIC void
 633xfs_inode_item_committing(
 634        struct xfs_log_item     *lip,
 635        xfs_lsn_t               lsn)
 636{
 637        INODE_ITEM(lip)->ili_last_lsn = lsn;
 638}
 639
 640/*
 641 * This is the ops vector shared by all buf log items.
 642 */
 643static const struct xfs_item_ops xfs_inode_item_ops = {
 644        .iop_size       = xfs_inode_item_size,
 645        .iop_format     = xfs_inode_item_format,
 646        .iop_pin        = xfs_inode_item_pin,
 647        .iop_unpin      = xfs_inode_item_unpin,
 648        .iop_unlock     = xfs_inode_item_unlock,
 649        .iop_committed  = xfs_inode_item_committed,
 650        .iop_push       = xfs_inode_item_push,
 651        .iop_committing = xfs_inode_item_committing,
 652        .iop_error      = xfs_inode_item_error
 653};
 654
 655
 656/*
 657 * Initialize the inode log item for a newly allocated (in-core) inode.
 658 */
 659void
 660xfs_inode_item_init(
 661        struct xfs_inode        *ip,
 662        struct xfs_mount        *mp)
 663{
 664        struct xfs_inode_log_item *iip;
 665
 666        ASSERT(ip->i_itemp == NULL);
 667        iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP);
 668
 669        iip->ili_inode = ip;
 670        xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
 671                                                &xfs_inode_item_ops);
 672}
 673
 674/*
 675 * Free the inode log item and any memory hanging off of it.
 676 */
 677void
 678xfs_inode_item_destroy(
 679        xfs_inode_t     *ip)
 680{
 681        kmem_free(ip->i_itemp->ili_item.li_lv_shadow);
 682        kmem_zone_free(xfs_ili_zone, ip->i_itemp);
 683}
 684
 685
 686/*
 687 * This is the inode flushing I/O completion routine.  It is called
 688 * from interrupt level when the buffer containing the inode is
 689 * flushed to disk.  It is responsible for removing the inode item
 690 * from the AIL if it has not been re-logged, and unlocking the inode's
 691 * flush lock.
 692 *
 693 * To reduce AIL lock traffic as much as possible, we scan the buffer log item
 694 * list for other inodes that will run this function. We remove them from the
 695 * buffer list so we can process all the inode IO completions in one AIL lock
 696 * traversal.
 697 */
 698void
 699xfs_iflush_done(
 700        struct xfs_buf          *bp,
 701        struct xfs_log_item     *lip)
 702{
 703        struct xfs_inode_log_item *iip;
 704        struct xfs_log_item     *blip;
 705        struct xfs_log_item     *next;
 706        struct xfs_log_item     *prev;
 707        struct xfs_ail          *ailp = lip->li_ailp;
 708        int                     need_ail = 0;
 709
 710        /*
 711         * Scan the buffer IO completions for other inodes being completed and
 712         * attach them to the current inode log item.
 713         */
 714        blip = bp->b_fspriv;
 715        prev = NULL;
 716        while (blip != NULL) {
 717                if (blip->li_cb != xfs_iflush_done) {
 718                        prev = blip;
 719                        blip = blip->li_bio_list;
 720                        continue;
 721                }
 722
 723                /* remove from list */
 724                next = blip->li_bio_list;
 725                if (!prev) {
 726                        bp->b_fspriv = next;
 727                } else {
 728                        prev->li_bio_list = next;
 729                }
 730
 731                /* add to current list */
 732                blip->li_bio_list = lip->li_bio_list;
 733                lip->li_bio_list = blip;
 734
 735                /*
 736                 * while we have the item, do the unlocked check for needing
 737                 * the AIL lock.
 738                 */
 739                iip = INODE_ITEM(blip);
 740                if ((iip->ili_logged && blip->li_lsn == iip->ili_flush_lsn) ||
 741                    test_bit(XFS_LI_FAILED, &blip->li_flags))
 742                        need_ail++;
 743
 744                blip = next;
 745        }
 746
 747        /* make sure we capture the state of the initial inode. */
 748        iip = INODE_ITEM(lip);
 749        if ((iip->ili_logged && lip->li_lsn == iip->ili_flush_lsn) ||
 750            test_bit(XFS_LI_FAILED, &lip->li_flags))
 751                need_ail++;
 752
 753        /*
 754         * We only want to pull the item from the AIL if it is
 755         * actually there and its location in the log has not
 756         * changed since we started the flush.  Thus, we only bother
 757         * if the ili_logged flag is set and the inode's lsn has not
 758         * changed.  First we check the lsn outside
 759         * the lock since it's cheaper, and then we recheck while
 760         * holding the lock before removing the inode from the AIL.
 761         */
 762        if (need_ail) {
 763                bool                    mlip_changed = false;
 764
 765                /* this is an opencoded batch version of xfs_trans_ail_delete */
 766                spin_lock(&ailp->xa_lock);
 767                for (blip = lip; blip; blip = blip->li_bio_list) {
 768                        if (INODE_ITEM(blip)->ili_logged &&
 769                            blip->li_lsn == INODE_ITEM(blip)->ili_flush_lsn)
 770                                mlip_changed |= xfs_ail_delete_one(ailp, blip);
 771                        else {
 772                                xfs_clear_li_failed(blip);
 773                        }
 774                }
 775
 776                if (mlip_changed) {
 777                        if (!XFS_FORCED_SHUTDOWN(ailp->xa_mount))
 778                                xlog_assign_tail_lsn_locked(ailp->xa_mount);
 779                        if (list_empty(&ailp->xa_ail))
 780                                wake_up_all(&ailp->xa_empty);
 781                }
 782                spin_unlock(&ailp->xa_lock);
 783
 784                if (mlip_changed)
 785                        xfs_log_space_wake(ailp->xa_mount);
 786        }
 787
 788        /*
 789         * clean up and unlock the flush lock now we are done. We can clear the
 790         * ili_last_fields bits now that we know that the data corresponding to
 791         * them is safely on disk.
 792         */
 793        for (blip = lip; blip; blip = next) {
 794                next = blip->li_bio_list;
 795                blip->li_bio_list = NULL;
 796
 797                iip = INODE_ITEM(blip);
 798                iip->ili_logged = 0;
 799                iip->ili_last_fields = 0;
 800                xfs_ifunlock(iip->ili_inode);
 801        }
 802}
 803
 804/*
 805 * This is the inode flushing abort routine.  It is called from xfs_iflush when
 806 * the filesystem is shutting down to clean up the inode state.  It is
 807 * responsible for removing the inode item from the AIL if it has not been
 808 * re-logged, and unlocking the inode's flush lock.
 809 */
 810void
 811xfs_iflush_abort(
 812        xfs_inode_t             *ip,
 813        bool                    stale)
 814{
 815        xfs_inode_log_item_t    *iip = ip->i_itemp;
 816
 817        if (iip) {
 818                if (test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags)) {
 819                        xfs_trans_ail_remove(&iip->ili_item,
 820                                             stale ? SHUTDOWN_LOG_IO_ERROR :
 821                                                     SHUTDOWN_CORRUPT_INCORE);
 822                }
 823                iip->ili_logged = 0;
 824                /*
 825                 * Clear the ili_last_fields bits now that we know that the
 826                 * data corresponding to them is safely on disk.
 827                 */
 828                iip->ili_last_fields = 0;
 829                /*
 830                 * Clear the inode logging fields so no more flushes are
 831                 * attempted.
 832                 */
 833                iip->ili_fields = 0;
 834                iip->ili_fsync_fields = 0;
 835        }
 836        /*
 837         * Release the inode's flush lock since we're done with it.
 838         */
 839        xfs_ifunlock(ip);
 840}
 841
 842void
 843xfs_istale_done(
 844        struct xfs_buf          *bp,
 845        struct xfs_log_item     *lip)
 846{
 847        xfs_iflush_abort(INODE_ITEM(lip)->ili_inode, true);
 848}
 849
 850/*
 851 * convert an xfs_inode_log_format struct from the old 32 bit version
 852 * (which can have different field alignments) to the native 64 bit version
 853 */
 854int
 855xfs_inode_item_format_convert(
 856        struct xfs_log_iovec            *buf,
 857        struct xfs_inode_log_format     *in_f)
 858{
 859        struct xfs_inode_log_format_32  *in_f32 = buf->i_addr;
 860
 861        if (buf->i_len != sizeof(*in_f32))
 862                return -EFSCORRUPTED;
 863
 864        in_f->ilf_type = in_f32->ilf_type;
 865        in_f->ilf_size = in_f32->ilf_size;
 866        in_f->ilf_fields = in_f32->ilf_fields;
 867        in_f->ilf_asize = in_f32->ilf_asize;
 868        in_f->ilf_dsize = in_f32->ilf_dsize;
 869        in_f->ilf_ino = in_f32->ilf_ino;
 870        memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
 871        in_f->ilf_blkno = in_f32->ilf_blkno;
 872        in_f->ilf_len = in_f32->ilf_len;
 873        in_f->ilf_boffset = in_f32->ilf_boffset;
 874        return 0;
 875}
 876