[PATCH] OCFS2: The Second Oracle Cluster Filesystem
Mark Fasheh [Thu, 15 Dec 2005 22:31:24 +0000 (14:31 -0800)]
The OCFS2 file system module.

Signed-off-by: Mark Fasheh <mark.fasheh@oracle.com>
Signed-off-by: Kurt Hackel <kurt.hackel@oracle.com>

55 files changed:
Documentation/filesystems/00-INDEX
Documentation/filesystems/ocfs2.txt [new file with mode: 0644]
MAINTAINERS
fs/ocfs2/Makefile [new file with mode: 0644]
fs/ocfs2/alloc.c [new file with mode: 0644]
fs/ocfs2/alloc.h [new file with mode: 0644]
fs/ocfs2/aops.c [new file with mode: 0644]
fs/ocfs2/aops.h [new file with mode: 0644]
fs/ocfs2/buffer_head_io.c [new file with mode: 0644]
fs/ocfs2/buffer_head_io.h [new file with mode: 0644]
fs/ocfs2/dcache.c [new file with mode: 0644]
fs/ocfs2/dcache.h [new file with mode: 0644]
fs/ocfs2/dir.c [new file with mode: 0644]
fs/ocfs2/dir.h [new file with mode: 0644]
fs/ocfs2/dlmglue.c [new file with mode: 0644]
fs/ocfs2/dlmglue.h [new file with mode: 0644]
fs/ocfs2/endian.h [new file with mode: 0644]
fs/ocfs2/export.c [new file with mode: 0644]
fs/ocfs2/export.h [new file with mode: 0644]
fs/ocfs2/extent_map.c [new file with mode: 0644]
fs/ocfs2/extent_map.h [new file with mode: 0644]
fs/ocfs2/file.c [new file with mode: 0644]
fs/ocfs2/file.h [new file with mode: 0644]
fs/ocfs2/heartbeat.c [new file with mode: 0644]
fs/ocfs2/heartbeat.h [new file with mode: 0644]
fs/ocfs2/inode.c [new file with mode: 0644]
fs/ocfs2/inode.h [new file with mode: 0644]
fs/ocfs2/journal.c [new file with mode: 0644]
fs/ocfs2/journal.h [new file with mode: 0644]
fs/ocfs2/localalloc.c [new file with mode: 0644]
fs/ocfs2/localalloc.h [new file with mode: 0644]
fs/ocfs2/mmap.c [new file with mode: 0644]
fs/ocfs2/mmap.h [new file with mode: 0644]
fs/ocfs2/namei.c [new file with mode: 0644]
fs/ocfs2/namei.h [new file with mode: 0644]
fs/ocfs2/ocfs1_fs_compat.h [new file with mode: 0644]
fs/ocfs2/ocfs2.h [new file with mode: 0644]
fs/ocfs2/ocfs2_fs.h [new file with mode: 0644]
fs/ocfs2/ocfs2_lockid.h [new file with mode: 0644]
fs/ocfs2/slot_map.c [new file with mode: 0644]
fs/ocfs2/slot_map.h [new file with mode: 0644]
fs/ocfs2/suballoc.c [new file with mode: 0644]
fs/ocfs2/suballoc.h [new file with mode: 0644]
fs/ocfs2/super.c [new file with mode: 0644]
fs/ocfs2/super.h [new file with mode: 0644]
fs/ocfs2/symlink.c [new file with mode: 0644]
fs/ocfs2/symlink.h [new file with mode: 0644]
fs/ocfs2/sysfile.c [new file with mode: 0644]
fs/ocfs2/sysfile.h [new file with mode: 0644]
fs/ocfs2/uptodate.c [new file with mode: 0644]
fs/ocfs2/uptodate.h [new file with mode: 0644]
fs/ocfs2/ver.c [new file with mode: 0644]
fs/ocfs2/ver.h [new file with mode: 0644]
fs/ocfs2/vote.c [new file with mode: 0644]
fs/ocfs2/vote.h [new file with mode: 0644]

index d9b0a06..2580ada 100644 (file)
@@ -36,6 +36,8 @@ ntfs.txt
        - info and mount options for the NTFS filesystem (Windows NT).
 proc.txt
        - info on Linux's /proc filesystem.
+ocfs2.txt
+       - info and mount options for the OCFS2 clustered filesystem.
 romfs.txt
        - Description of the ROMFS filesystem.
 smbfs.txt
diff --git a/Documentation/filesystems/ocfs2.txt b/Documentation/filesystems/ocfs2.txt
new file mode 100644 (file)
index 0000000..f2595ca
--- /dev/null
@@ -0,0 +1,55 @@
+OCFS2 filesystem
+==================
+OCFS2 is a general purpose extent based shared disk cluster file
+system with many similarities to ext3. It supports 64 bit inode
+numbers, and has automatically extending metadata groups which may
+also make it attractive for non-clustered use.
+
+You'll want to install the ocfs2-tools package in order to at least
+get "mount.ocfs2" and "ocfs2_hb_ctl".
+
+Project web page:    http://oss.oracle.com/projects/ocfs2
+Tools web page:      http://oss.oracle.com/projects/ocfs2-tools
+OCFS2 mailing lists: http://oss.oracle.com/projects/ocfs2/mailman/
+
+All code copyright 2005 Oracle except when otherwise noted.
+
+CREDITS:
+Lots of code taken from ext3 and other projects.
+
+Authors in alphabetical order:
+Joel Becker   <joel.becker@oracle.com>
+Zach Brown    <zach.brown@oracle.com>
+Mark Fasheh   <mark.fasheh@oracle.com>
+Kurt Hackel   <kurt.hackel@oracle.com>
+Sunil Mushran <sunil.mushran@oracle.com>
+Manish Singh  <manish.singh@oracle.com>
+
+Caveats
+=======
+Features which OCFS2 does not support yet:
+       - sparse files
+       - extended attributes
+       - shared writeable mmap
+       - loopback is supported, but data written will not
+         be cluster coherent.
+       - quotas
+       - cluster aware flock
+       - Directory change notification (F_NOTIFY)
+       - Distributed Caching (F_SETLEASE/F_GETLEASE/break_lease)
+       - POSIX ACLs
+       - readpages / writepages (not user visible)
+
+Mount options
+=============
+
+OCFS2 supports the following mount options:
+(*) == default
+
+barrier=1              This enables/disables barriers. barrier=0 disables it,
+                       barrier=1 enables it.
+errors=remount-ro(*)   Remount the filesystem read-only on an error.
+errors=panic           Panic and halt the machine if an error occurs.
+intr           (*)     Allow signals to interrupt cluster operations.
+nointr                 Do not allow signals to interrupt cluster
+                       operations.
index 86ee06f..1588830 100644 (file)
@@ -1905,6 +1905,15 @@ M:       ajoshi@shell.unixbox.com
 L:     linux-nvidia@lists.surfsouth.com
 S:     Maintained
 
+ORACLE CLUSTER FILESYSTEM 2 (OCFS2)
+P:     Mark Fasheh
+M:     mark.fasheh@oracle.com
+P:     Kurt Hackel
+M:     kurt.hackel@oracle.com
+L:     ocfs2-devel@oss.oracle.com
+W:     http://oss.oracle.com/projects/ocfs2/
+S:     Supported       
+
 OLYMPIC NETWORK DRIVER
 P:     Peter De Shrijver
 M:     p2@ace.ulyssis.student.kuleuven.ac.be
diff --git a/fs/ocfs2/Makefile b/fs/ocfs2/Makefile
new file mode 100644 (file)
index 0000000..7d3be84
--- /dev/null
@@ -0,0 +1,33 @@
+EXTRA_CFLAGS += -Ifs/ocfs2
+
+EXTRA_CFLAGS += -DCATCH_BH_JBD_RACES
+
+obj-$(CONFIG_OCFS2_FS) += ocfs2.o
+
+ocfs2-objs := \
+       alloc.o                 \
+       aops.o                  \
+       buffer_head_io.o        \
+       dcache.o                \
+       dir.o                   \
+       dlmglue.o               \
+       export.o                \
+       extent_map.o            \
+       file.o                  \
+       heartbeat.o             \
+       inode.o                 \
+       journal.o               \
+       localalloc.o            \
+       mmap.o                  \
+       namei.o                 \
+       slot_map.o              \
+       suballoc.o              \
+       super.o                 \
+       symlink.o               \
+       sysfile.o               \
+       uptodate.o              \
+       ver.o                   \
+       vote.o
+
+obj-$(CONFIG_OCFS2_FS) += cluster/
+obj-$(CONFIG_OCFS2_FS) += dlm/
diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
new file mode 100644 (file)
index 0000000..465f797
--- /dev/null
@@ -0,0 +1,2040 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * alloc.c
+ *
+ * Extent allocs and frees
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/highmem.h>
+
+#define MLOG_MASK_PREFIX ML_DISK_ALLOC
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "alloc.h"
+#include "dlmglue.h"
+#include "extent_map.h"
+#include "inode.h"
+#include "journal.h"
+#include "localalloc.h"
+#include "suballoc.h"
+#include "sysfile.h"
+#include "file.h"
+#include "super.h"
+#include "uptodate.h"
+
+#include "buffer_head_io.h"
+
+static int ocfs2_extent_contig(struct inode *inode,
+                              struct ocfs2_extent_rec *ext,
+                              u64 blkno);
+
+static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
+                                    struct ocfs2_journal_handle *handle,
+                                    struct inode *inode,
+                                    int wanted,
+                                    struct ocfs2_alloc_context *meta_ac,
+                                    struct buffer_head *bhs[]);
+
+static int ocfs2_add_branch(struct ocfs2_super *osb,
+                           struct ocfs2_journal_handle *handle,
+                           struct inode *inode,
+                           struct buffer_head *fe_bh,
+                           struct buffer_head *eb_bh,
+                           struct buffer_head *last_eb_bh,
+                           struct ocfs2_alloc_context *meta_ac);
+
+static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
+                                 struct ocfs2_journal_handle *handle,
+                                 struct inode *inode,
+                                 struct buffer_head *fe_bh,
+                                 struct ocfs2_alloc_context *meta_ac,
+                                 struct buffer_head **ret_new_eb_bh);
+
+static int ocfs2_do_insert_extent(struct ocfs2_super *osb,
+                                 struct ocfs2_journal_handle *handle,
+                                 struct inode *inode,
+                                 struct buffer_head *fe_bh,
+                                 u64 blkno,
+                                 u32 new_clusters);
+
+static int ocfs2_find_branch_target(struct ocfs2_super *osb,
+                                   struct inode *inode,
+                                   struct buffer_head *fe_bh,
+                                   struct buffer_head **target_bh);
+
+static int ocfs2_find_new_last_ext_blk(struct ocfs2_super *osb,
+                                      struct inode *inode,
+                                      struct ocfs2_dinode *fe,
+                                      unsigned int new_i_clusters,
+                                      struct buffer_head *old_last_eb,
+                                      struct buffer_head **new_last_eb);
+
+static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
+
+static int ocfs2_extent_contig(struct inode *inode,
+                              struct ocfs2_extent_rec *ext,
+                              u64 blkno)
+{
+       return blkno == (le64_to_cpu(ext->e_blkno) +
+                        ocfs2_clusters_to_blocks(inode->i_sb,
+                                                 le32_to_cpu(ext->e_clusters)));
+}
+
+/*
+ * How many free extents have we got before we need more meta data?
+ */
+int ocfs2_num_free_extents(struct ocfs2_super *osb,
+                          struct inode *inode,
+                          struct ocfs2_dinode *fe)
+{
+       int retval;
+       struct ocfs2_extent_list *el;
+       struct ocfs2_extent_block *eb;
+       struct buffer_head *eb_bh = NULL;
+
+       mlog_entry_void();
+
+       if (!OCFS2_IS_VALID_DINODE(fe)) {
+               OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
+               retval = -EIO;
+               goto bail;
+       }
+
+       if (fe->i_last_eb_blk) {
+               retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
+                                         &eb_bh, OCFS2_BH_CACHED, inode);
+               if (retval < 0) {
+                       mlog_errno(retval);
+                       goto bail;
+               }
+               eb = (struct ocfs2_extent_block *) eb_bh->b_data;
+               el = &eb->h_list;
+       } else
+               el = &fe->id2.i_list;
+
+       BUG_ON(el->l_tree_depth != 0);
+
+       retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
+bail:
+       if (eb_bh)
+               brelse(eb_bh);
+
+       mlog_exit(retval);
+       return retval;
+}
+
+/* expects array to already be allocated
+ *
+ * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
+ * l_count for you
+ */
+static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
+                                    struct ocfs2_journal_handle *handle,
+                                    struct inode *inode,
+                                    int wanted,
+                                    struct ocfs2_alloc_context *meta_ac,
+                                    struct buffer_head *bhs[])
+{
+       int count, status, i;
+       u16 suballoc_bit_start;
+       u32 num_got;
+       u64 first_blkno;
+       struct ocfs2_extent_block *eb;
+
+       mlog_entry_void();
+
+       count = 0;
+       while (count < wanted) {
+               status = ocfs2_claim_metadata(osb,
+                                             handle,
+                                             meta_ac,
+                                             wanted - count,
+                                             &suballoc_bit_start,
+                                             &num_got,
+                                             &first_blkno);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               for(i = count;  i < (num_got + count); i++) {
+                       bhs[i] = sb_getblk(osb->sb, first_blkno);
+                       if (bhs[i] == NULL) {
+                               status = -EIO;
+                               mlog_errno(status);
+                               goto bail;
+                       }
+                       ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
+
+                       status = ocfs2_journal_access(handle, inode, bhs[i],
+                                                     OCFS2_JOURNAL_ACCESS_CREATE);
+                       if (status < 0) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+
+                       memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
+                       eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
+                       /* Ok, setup the minimal stuff here. */
+                       strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
+                       eb->h_blkno = cpu_to_le64(first_blkno);
+                       eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
+
+#ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
+                       /* we always use slot zero's suballocator */
+                       eb->h_suballoc_slot = 0;
+#else
+                       eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
+#endif
+                       eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
+                       eb->h_list.l_count =
+                               cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
+
+                       suballoc_bit_start++;
+                       first_blkno++;
+
+                       /* We'll also be dirtied by the caller, so
+                        * this isn't absolutely necessary. */
+                       status = ocfs2_journal_dirty(handle, bhs[i]);
+                       if (status < 0) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+               }
+
+               count += num_got;
+       }
+
+       status = 0;
+bail:
+       if (status < 0) {
+               for(i = 0; i < wanted; i++) {
+                       if (bhs[i])
+                               brelse(bhs[i]);
+                       bhs[i] = NULL;
+               }
+       }
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * Add an entire tree branch to our inode. eb_bh is the extent block
+ * to start at, if we don't want to start the branch at the dinode
+ * structure.
+ *
+ * last_eb_bh is required as we have to update it's next_leaf pointer
+ * for the new last extent block.
+ *
+ * the new branch will be 'empty' in the sense that every block will
+ * contain a single record with e_clusters == 0.
+ */
+static int ocfs2_add_branch(struct ocfs2_super *osb,
+                           struct ocfs2_journal_handle *handle,
+                           struct inode *inode,
+                           struct buffer_head *fe_bh,
+                           struct buffer_head *eb_bh,
+                           struct buffer_head *last_eb_bh,
+                           struct ocfs2_alloc_context *meta_ac)
+{
+       int status, new_blocks, i;
+       u64 next_blkno, new_last_eb_blk;
+       struct buffer_head *bh;
+       struct buffer_head **new_eb_bhs = NULL;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list  *eb_el;
+       struct ocfs2_extent_list  *el;
+
+       mlog_entry_void();
+
+       BUG_ON(!last_eb_bh);
+
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+
+       if (eb_bh) {
+               eb = (struct ocfs2_extent_block *) eb_bh->b_data;
+               el = &eb->h_list;
+       } else
+               el = &fe->id2.i_list;
+
+       /* we never add a branch to a leaf. */
+       BUG_ON(!el->l_tree_depth);
+
+       new_blocks = le16_to_cpu(el->l_tree_depth);
+
+       /* allocate the number of new eb blocks we need */
+       new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
+                            GFP_KERNEL);
+       if (!new_eb_bhs) {
+               status = -ENOMEM;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
+                                          meta_ac, new_eb_bhs);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
+        * linked with the rest of the tree.
+        * conversly, new_eb_bhs[0] is the new bottommost leaf.
+        *
+        * when we leave the loop, new_last_eb_blk will point to the
+        * newest leaf, and next_blkno will point to the topmost extent
+        * block. */
+       next_blkno = new_last_eb_blk = 0;
+       for(i = 0; i < new_blocks; i++) {
+               bh = new_eb_bhs[i];
+               eb = (struct ocfs2_extent_block *) bh->b_data;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+                       status = -EIO;
+                       goto bail;
+               }
+               eb_el = &eb->h_list;
+
+               status = ocfs2_journal_access(handle, inode, bh,
+                                             OCFS2_JOURNAL_ACCESS_CREATE);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               eb->h_next_leaf_blk = 0;
+               eb_el->l_tree_depth = cpu_to_le16(i);
+               eb_el->l_next_free_rec = cpu_to_le16(1);
+               eb_el->l_recs[0].e_cpos = fe->i_clusters;
+               eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
+               eb_el->l_recs[0].e_clusters = cpu_to_le32(0);
+               if (!eb_el->l_tree_depth)
+                       new_last_eb_blk = le64_to_cpu(eb->h_blkno);
+
+               status = ocfs2_journal_dirty(handle, bh);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               next_blkno = le64_to_cpu(eb->h_blkno);
+       }
+
+       /* This is a bit hairy. We want to update up to three blocks
+        * here without leaving any of them in an inconsistent state
+        * in case of error. We don't have to worry about
+        * journal_dirty erroring as it won't unless we've aborted the
+        * handle (in which case we would never be here) so reserving
+        * the write with journal_access is all we need to do. */
+       status = ocfs2_journal_access(handle, inode, last_eb_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       status = ocfs2_journal_access(handle, inode, fe_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       if (eb_bh) {
+               status = ocfs2_journal_access(handle, inode, eb_bh,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
+       /* Link the new branch into the rest of the tree (el will
+        * either be on the fe, or the extent block passed in. */
+       i = le16_to_cpu(el->l_next_free_rec);
+       el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
+       el->l_recs[i].e_cpos = fe->i_clusters;
+       el->l_recs[i].e_clusters = 0;
+       le16_add_cpu(&el->l_next_free_rec, 1);
+
+       /* fe needs a new last extent block pointer, as does the
+        * next_leaf on the previously last-extent-block. */
+       fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
+
+       eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
+       eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
+
+       status = ocfs2_journal_dirty(handle, last_eb_bh);
+       if (status < 0)
+               mlog_errno(status);
+       status = ocfs2_journal_dirty(handle, fe_bh);
+       if (status < 0)
+               mlog_errno(status);
+       if (eb_bh) {
+               status = ocfs2_journal_dirty(handle, eb_bh);
+               if (status < 0)
+                       mlog_errno(status);
+       }
+
+       status = 0;
+bail:
+       if (new_eb_bhs) {
+               for (i = 0; i < new_blocks; i++)
+                       if (new_eb_bhs[i])
+                               brelse(new_eb_bhs[i]);
+               kfree(new_eb_bhs);
+       }
+
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * adds another level to the allocation tree.
+ * returns back the new extent block so you can add a branch to it
+ * after this call.
+ */
+static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
+                                 struct ocfs2_journal_handle *handle,
+                                 struct inode *inode,
+                                 struct buffer_head *fe_bh,
+                                 struct ocfs2_alloc_context *meta_ac,
+                                 struct buffer_head **ret_new_eb_bh)
+{
+       int status, i;
+       struct buffer_head *new_eb_bh = NULL;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list  *fe_el;
+       struct ocfs2_extent_list  *eb_el;
+
+       mlog_entry_void();
+
+       status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
+                                          &new_eb_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
+       if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+               OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+               status = -EIO;
+               goto bail;
+       }
+
+       eb_el = &eb->h_list;
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+       fe_el = &fe->id2.i_list;
+
+       status = ocfs2_journal_access(handle, inode, new_eb_bh,
+                                     OCFS2_JOURNAL_ACCESS_CREATE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* copy the fe data into the new extent block */
+       eb_el->l_tree_depth = fe_el->l_tree_depth;
+       eb_el->l_next_free_rec = fe_el->l_next_free_rec;
+       for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++) {
+               eb_el->l_recs[i].e_cpos = fe_el->l_recs[i].e_cpos;
+               eb_el->l_recs[i].e_clusters = fe_el->l_recs[i].e_clusters;
+               eb_el->l_recs[i].e_blkno = fe_el->l_recs[i].e_blkno;
+       }
+
+       status = ocfs2_journal_dirty(handle, new_eb_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       status = ocfs2_journal_access(handle, inode, fe_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* update fe now */
+       le16_add_cpu(&fe_el->l_tree_depth, 1);
+       fe_el->l_recs[0].e_cpos = 0;
+       fe_el->l_recs[0].e_blkno = eb->h_blkno;
+       fe_el->l_recs[0].e_clusters = fe->i_clusters;
+       for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++) {
+               fe_el->l_recs[i].e_cpos = 0;
+               fe_el->l_recs[i].e_clusters = 0;
+               fe_el->l_recs[i].e_blkno = 0;
+       }
+       fe_el->l_next_free_rec = cpu_to_le16(1);
+
+       /* If this is our 1st tree depth shift, then last_eb_blk
+        * becomes the allocated extent block */
+       if (fe_el->l_tree_depth == cpu_to_le16(1))
+               fe->i_last_eb_blk = eb->h_blkno;
+
+       status = ocfs2_journal_dirty(handle, fe_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       *ret_new_eb_bh = new_eb_bh;
+       new_eb_bh = NULL;
+       status = 0;
+bail:
+       if (new_eb_bh)
+               brelse(new_eb_bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * Expects the tree to already have room in the rightmost leaf for the
+ * extent.  Updates all the extent blocks (and the dinode) on the way
+ * down.
+ */
+static int ocfs2_do_insert_extent(struct ocfs2_super *osb,
+                                 struct ocfs2_journal_handle *handle,
+                                 struct inode *inode,
+                                 struct buffer_head *fe_bh,
+                                 u64 start_blk,
+                                 u32 new_clusters)
+{
+       int status, i, num_bhs = 0;
+       u64 next_blkno;
+       u16 next_free;
+       struct buffer_head **eb_bhs = NULL;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list  *el;
+
+       mlog_entry_void();
+
+       status = ocfs2_journal_access(handle, inode, fe_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+       el = &fe->id2.i_list;
+       if (el->l_tree_depth) {
+               /* This is another operation where we want to be
+                * careful about our tree updates. An error here means
+                * none of the previous changes we made should roll
+                * forward. As a result, we have to record the buffers
+                * for this part of the tree in an array and reserve a
+                * journal write to them before making any changes. */
+               num_bhs = le16_to_cpu(fe->id2.i_list.l_tree_depth);
+               eb_bhs = kcalloc(num_bhs, sizeof(struct buffer_head *),
+                                GFP_KERNEL);
+               if (!eb_bhs) {
+                       status = -ENOMEM;
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               i = 0;
+               while(el->l_tree_depth) {
+                       next_free = le16_to_cpu(el->l_next_free_rec);
+                       if (next_free == 0) {
+                               ocfs2_error(inode->i_sb,
+                                           "Dinode %"MLFu64" has a bad "
+                                           "extent list",
+                                           OCFS2_I(inode)->ip_blkno);
+                               status = -EIO;
+                               goto bail;
+                       }
+                       next_blkno = le64_to_cpu(el->l_recs[next_free - 1].e_blkno);
+
+                       BUG_ON(i >= num_bhs);
+                       status = ocfs2_read_block(osb, next_blkno, &eb_bhs[i],
+                                                 OCFS2_BH_CACHED, inode);
+                       if (status < 0) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+                       eb = (struct ocfs2_extent_block *) eb_bhs[i]->b_data;
+                       if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                               OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
+                                                                eb);
+                               status = -EIO;
+                               goto bail;
+                       }
+
+                       status = ocfs2_journal_access(handle, inode, eb_bhs[i],
+                                                     OCFS2_JOURNAL_ACCESS_WRITE);
+                       if (status < 0) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+
+                       el = &eb->h_list;
+                       i++;
+                       /* When we leave this loop, eb_bhs[num_bhs - 1] will
+                        * hold the bottom-most leaf extent block. */
+               }
+               BUG_ON(el->l_tree_depth);
+
+               el = &fe->id2.i_list;
+               /* If we have tree depth, then the fe update is
+                * trivial, and we want to switch el out for the
+                * bottom-most leaf in order to update it with the
+                * actual extent data below. */
+               next_free = le16_to_cpu(el->l_next_free_rec);
+               if (next_free == 0) {
+                       ocfs2_error(inode->i_sb,
+                                   "Dinode %"MLFu64" has a bad "
+                                   "extent list",
+                                   OCFS2_I(inode)->ip_blkno);
+                       status = -EIO;
+                       goto bail;
+               }
+               le32_add_cpu(&el->l_recs[next_free - 1].e_clusters,
+                            new_clusters);
+               /* (num_bhs - 1) to avoid the leaf */
+               for(i = 0; i < (num_bhs - 1); i++) {
+                       eb = (struct ocfs2_extent_block *) eb_bhs[i]->b_data;
+                       el = &eb->h_list;
+
+                       /* finally, make our actual change to the
+                        * intermediate extent blocks. */
+                       next_free = le16_to_cpu(el->l_next_free_rec);
+                       le32_add_cpu(&el->l_recs[next_free - 1].e_clusters,
+                                    new_clusters);
+
+                       status = ocfs2_journal_dirty(handle, eb_bhs[i]);
+                       if (status < 0)
+                               mlog_errno(status);
+               }
+               BUG_ON(i != (num_bhs - 1));
+               /* note that the leaf block wasn't touched in
+                * the loop above */
+               eb = (struct ocfs2_extent_block *) eb_bhs[num_bhs - 1]->b_data;
+               el = &eb->h_list;
+               BUG_ON(el->l_tree_depth);
+       }
+
+       /* yay, we can finally add the actual extent now! */
+       i = le16_to_cpu(el->l_next_free_rec) - 1;
+       if (le16_to_cpu(el->l_next_free_rec) &&
+           ocfs2_extent_contig(inode, &el->l_recs[i], start_blk)) {
+               le32_add_cpu(&el->l_recs[i].e_clusters, new_clusters);
+       } else if (le16_to_cpu(el->l_next_free_rec) &&
+                  (le32_to_cpu(el->l_recs[i].e_clusters) == 0)) {
+               /* having an empty extent at eof is legal. */
+               if (el->l_recs[i].e_cpos != fe->i_clusters) {
+                       ocfs2_error(inode->i_sb,
+                                   "Dinode %"MLFu64" trailing extent is bad: "
+                                   "cpos (%u) != number of clusters (%u)",
+                                   le32_to_cpu(el->l_recs[i].e_cpos),
+                                   le32_to_cpu(fe->i_clusters));
+                       status = -EIO;
+                       goto bail;
+               }
+               el->l_recs[i].e_blkno = cpu_to_le64(start_blk);
+               el->l_recs[i].e_clusters = cpu_to_le32(new_clusters);
+       } else {
+               /* No contiguous record, or no empty record at eof, so
+                * we add a new one. */
+
+               BUG_ON(le16_to_cpu(el->l_next_free_rec) >=
+                      le16_to_cpu(el->l_count));
+               i = le16_to_cpu(el->l_next_free_rec);
+
+               el->l_recs[i].e_blkno = cpu_to_le64(start_blk);
+               el->l_recs[i].e_clusters = cpu_to_le32(new_clusters);
+               el->l_recs[i].e_cpos = fe->i_clusters;
+               le16_add_cpu(&el->l_next_free_rec, 1);
+       }
+
+       /*
+        * extent_map errors are not fatal, so they are ignored outside
+        * of flushing the thing.
+        */
+       status = ocfs2_extent_map_append(inode, &el->l_recs[i],
+                                        new_clusters);
+       if (status) {
+               mlog_errno(status);
+               ocfs2_extent_map_drop(inode, le32_to_cpu(fe->i_clusters));
+       }
+
+       status = ocfs2_journal_dirty(handle, fe_bh);
+       if (status < 0)
+               mlog_errno(status);
+       if (fe->id2.i_list.l_tree_depth) {
+               status = ocfs2_journal_dirty(handle, eb_bhs[num_bhs - 1]);
+               if (status < 0)
+                       mlog_errno(status);
+       }
+
+       status = 0;
+bail:
+       if (eb_bhs) {
+               for (i = 0; i < num_bhs; i++)
+                       if (eb_bhs[i])
+                               brelse(eb_bhs[i]);
+               kfree(eb_bhs);
+       }
+
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * Should only be called when there is no space left in any of the
+ * leaf nodes. What we want to do is find the lowest tree depth
+ * non-leaf extent block with room for new records. There are three
+ * valid results of this search:
+ *
+ * 1) a lowest extent block is found, then we pass it back in
+ *    *lowest_eb_bh and return '0'
+ *
+ * 2) the search fails to find anything, but the dinode has room. We
+ *    pass NULL back in *lowest_eb_bh, but still return '0'
+ *
+ * 3) the search fails to find anything AND the dinode is full, in
+ *    which case we return > 0
+ *
+ * return status < 0 indicates an error.
+ */
+static int ocfs2_find_branch_target(struct ocfs2_super *osb,
+                                   struct inode *inode,
+                                   struct buffer_head *fe_bh,
+                                   struct buffer_head **target_bh)
+{
+       int status = 0, i;
+       u64 blkno;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list  *el;
+       struct buffer_head *bh = NULL;
+       struct buffer_head *lowest_bh = NULL;
+
+       mlog_entry_void();
+
+       *target_bh = NULL;
+
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+       el = &fe->id2.i_list;
+
+       while(le16_to_cpu(el->l_tree_depth) > 1) {
+               if (le16_to_cpu(el->l_next_free_rec) == 0) {
+                       ocfs2_error(inode->i_sb, "Dinode %"MLFu64" has empty "
+                                   "extent list (next_free_rec == 0)",
+                                   OCFS2_I(inode)->ip_blkno);
+                       status = -EIO;
+                       goto bail;
+               }
+               i = le16_to_cpu(el->l_next_free_rec) - 1;
+               blkno = le64_to_cpu(el->l_recs[i].e_blkno);
+               if (!blkno) {
+                       ocfs2_error(inode->i_sb, "Dinode %"MLFu64" has extent "
+                                   "list where extent # %d has no physical "
+                                   "block start",
+                                   OCFS2_I(inode)->ip_blkno, i);
+                       status = -EIO;
+                       goto bail;
+               }
+
+               if (bh) {
+                       brelse(bh);
+                       bh = NULL;
+               }
+
+               status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
+                                         inode);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               eb = (struct ocfs2_extent_block *) bh->b_data;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+                       status = -EIO;
+                       goto bail;
+               }
+               el = &eb->h_list;
+
+               if (le16_to_cpu(el->l_next_free_rec) <
+                   le16_to_cpu(el->l_count)) {
+                       if (lowest_bh)
+                               brelse(lowest_bh);
+                       lowest_bh = bh;
+                       get_bh(lowest_bh);
+               }
+       }
+
+       /* If we didn't find one and the fe doesn't have any room,
+        * then return '1' */
+       if (!lowest_bh
+           && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
+               status = 1;
+
+       *target_bh = lowest_bh;
+bail:
+       if (bh)
+               brelse(bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+/* the caller needs to update fe->i_clusters */
+int ocfs2_insert_extent(struct ocfs2_super *osb,
+                       struct ocfs2_journal_handle *handle,
+                       struct inode *inode,
+                       struct buffer_head *fe_bh,
+                       u64 start_blk,
+                       u32 new_clusters,
+                       struct ocfs2_alloc_context *meta_ac)
+{
+       int status, i, shift;
+       struct buffer_head *last_eb_bh = NULL;
+       struct buffer_head *bh = NULL;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list  *el;
+
+       mlog_entry_void();
+
+       mlog(0, "add %u clusters starting at block %"MLFu64" to "
+               "inode %"MLFu64"\n",
+            new_clusters, start_blk, OCFS2_I(inode)->ip_blkno);
+
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+       el = &fe->id2.i_list;
+
+       if (el->l_tree_depth) {
+               /* jump to end of tree */
+               status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
+                                         &last_eb_bh, OCFS2_BH_CACHED, inode);
+               if (status < 0) {
+                       mlog_exit(status);
+                       goto bail;
+               }
+               eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
+               el = &eb->h_list;
+       }
+
+       /* Can we allocate without adding/shifting tree bits? */
+       i = le16_to_cpu(el->l_next_free_rec) - 1;
+       if (le16_to_cpu(el->l_next_free_rec) == 0
+           || (le16_to_cpu(el->l_next_free_rec) < le16_to_cpu(el->l_count))
+           || le32_to_cpu(el->l_recs[i].e_clusters) == 0
+           || ocfs2_extent_contig(inode, &el->l_recs[i], start_blk))
+               goto out_add;
+
+       mlog(0, "ocfs2_allocate_extent: couldn't do a simple add, traversing "
+            "tree now.\n");
+
+       shift = ocfs2_find_branch_target(osb, inode, fe_bh, &bh);
+       if (shift < 0) {
+               status = shift;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* We traveled all the way to the bottom of the allocation tree
+        * and didn't find room for any more extents - we need to add
+        * another tree level */
+       if (shift) {
+               /* if we hit a leaf, we'd better be empty :) */
+               BUG_ON(le16_to_cpu(el->l_next_free_rec) !=
+                      le16_to_cpu(el->l_count));
+               BUG_ON(bh);
+               mlog(0, "ocfs2_allocate_extent: need to shift tree depth "
+                    "(current = %u)\n",
+                    le16_to_cpu(fe->id2.i_list.l_tree_depth));
+
+               /* ocfs2_shift_tree_depth will return us a buffer with
+                * the new extent block (so we can pass that to
+                * ocfs2_add_branch). */
+               status = ocfs2_shift_tree_depth(osb, handle, inode, fe_bh,
+                                               meta_ac, &bh);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+               /* Special case: we have room now if we shifted from
+                * tree_depth 0 */
+               if (fe->id2.i_list.l_tree_depth == cpu_to_le16(1))
+                       goto out_add;
+       }
+
+       /* call ocfs2_add_branch to add the final part of the tree with
+        * the new data. */
+       mlog(0, "ocfs2_allocate_extent: add branch. bh = %p\n", bh);
+       status = ocfs2_add_branch(osb, handle, inode, fe_bh, bh, last_eb_bh,
+                                 meta_ac);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+out_add:
+       /* Finally, we can add clusters. */
+       status = ocfs2_do_insert_extent(osb, handle, inode, fe_bh,
+                                       start_blk, new_clusters);
+       if (status < 0)
+               mlog_errno(status);
+
+bail:
+       if (bh)
+               brelse(bh);
+
+       if (last_eb_bh)
+               brelse(last_eb_bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
+{
+       struct buffer_head *tl_bh = osb->osb_tl_bh;
+       struct ocfs2_dinode *di;
+       struct ocfs2_truncate_log *tl;
+
+       di = (struct ocfs2_dinode *) tl_bh->b_data;
+       tl = &di->id2.i_dealloc;
+
+       mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
+                       "slot %d, invalid truncate log parameters: used = "
+                       "%u, count = %u\n", osb->slot_num,
+                       le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
+       return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
+}
+
+static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
+                                          unsigned int new_start)
+{
+       unsigned int tail_index;
+       unsigned int current_tail;
+
+       /* No records, nothing to coalesce */
+       if (!le16_to_cpu(tl->tl_used))
+               return 0;
+
+       tail_index = le16_to_cpu(tl->tl_used) - 1;
+       current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
+       current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
+
+       return current_tail == new_start;
+}
+
+static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
+                                    struct ocfs2_journal_handle *handle,
+                                    u64 start_blk,
+                                    unsigned int num_clusters)
+{
+       int status, index;
+       unsigned int start_cluster, tl_count;
+       struct inode *tl_inode = osb->osb_tl_inode;
+       struct buffer_head *tl_bh = osb->osb_tl_bh;
+       struct ocfs2_dinode *di;
+       struct ocfs2_truncate_log *tl;
+
+       mlog_entry("start_blk = %"MLFu64", num_clusters = %u\n", start_blk,
+                  num_clusters);
+
+       BUG_ON(!down_trylock(&tl_inode->i_sem));
+
+       start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
+
+       di = (struct ocfs2_dinode *) tl_bh->b_data;
+       tl = &di->id2.i_dealloc;
+       if (!OCFS2_IS_VALID_DINODE(di)) {
+               OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
+               status = -EIO;
+               goto bail;
+       }
+
+       tl_count = le16_to_cpu(tl->tl_count);
+       mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
+                       tl_count == 0,
+                       "Truncate record count on #%"MLFu64" invalid ("
+                       "wanted %u, actual %u\n", OCFS2_I(tl_inode)->ip_blkno,
+                       ocfs2_truncate_recs_per_inode(osb->sb),
+                       le16_to_cpu(tl->tl_count));
+
+       /* Caller should have known to flush before calling us. */
+       index = le16_to_cpu(tl->tl_used);
+       if (index >= tl_count) {
+               status = -ENOSPC;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       status = ocfs2_journal_access(handle, tl_inode, tl_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       mlog(0, "Log truncate of %u clusters starting at cluster %u to "
+            "%"MLFu64" (index = %d)\n", num_clusters, start_cluster,
+            OCFS2_I(tl_inode)->ip_blkno, index);
+
+       if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
+               /*
+                * Move index back to the record we are coalescing with.
+                * ocfs2_truncate_log_can_coalesce() guarantees nonzero
+                */
+               index--;
+
+               num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
+               mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
+                    index, le32_to_cpu(tl->tl_recs[index].t_start),
+                    num_clusters);
+       } else {
+               tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
+               tl->tl_used = cpu_to_le16(index + 1);
+       }
+       tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
+
+       status = ocfs2_journal_dirty(handle, tl_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+bail:
+       mlog_exit(status);
+       return status;
+}
+
+static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
+                                        struct ocfs2_journal_handle *handle,
+                                        struct inode *data_alloc_inode,
+                                        struct buffer_head *data_alloc_bh)
+{
+       int status = 0;
+       int i;
+       unsigned int num_clusters;
+       u64 start_blk;
+       struct ocfs2_truncate_rec rec;
+       struct ocfs2_dinode *di;
+       struct ocfs2_truncate_log *tl;
+       struct inode *tl_inode = osb->osb_tl_inode;
+       struct buffer_head *tl_bh = osb->osb_tl_bh;
+
+       mlog_entry_void();
+
+       di = (struct ocfs2_dinode *) tl_bh->b_data;
+       tl = &di->id2.i_dealloc;
+       i = le16_to_cpu(tl->tl_used) - 1;
+       while (i >= 0) {
+               /* Caller has given us at least enough credits to
+                * update the truncate log dinode */
+               status = ocfs2_journal_access(handle, tl_inode, tl_bh,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               tl->tl_used = cpu_to_le16(i);
+
+               status = ocfs2_journal_dirty(handle, tl_bh);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               /* TODO: Perhaps we can calculate the bulk of the
+                * credits up front rather than extending like
+                * this. */
+               status = ocfs2_extend_trans(handle,
+                                           OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               rec = tl->tl_recs[i];
+               start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
+                                                   le32_to_cpu(rec.t_start));
+               num_clusters = le32_to_cpu(rec.t_clusters);
+
+               /* if start_blk is not set, we ignore the record as
+                * invalid. */
+               if (start_blk) {
+                       mlog(0, "free record %d, start = %u, clusters = %u\n",
+                            i, le32_to_cpu(rec.t_start), num_clusters);
+
+                       status = ocfs2_free_clusters(handle, data_alloc_inode,
+                                                    data_alloc_bh, start_blk,
+                                                    num_clusters);
+                       if (status < 0) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+               }
+               i--;
+       }
+
+bail:
+       mlog_exit(status);
+       return status;
+}
+
+/* Expects you to already be holding tl_inode->i_sem */
+static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
+{
+       int status;
+       unsigned int num_to_flush;
+       struct ocfs2_journal_handle *handle = NULL;
+       struct inode *tl_inode = osb->osb_tl_inode;
+       struct inode *data_alloc_inode = NULL;
+       struct buffer_head *tl_bh = osb->osb_tl_bh;
+       struct buffer_head *data_alloc_bh = NULL;
+       struct ocfs2_dinode *di;
+       struct ocfs2_truncate_log *tl;
+
+       mlog_entry_void();
+
+       BUG_ON(!down_trylock(&tl_inode->i_sem));
+
+       di = (struct ocfs2_dinode *) tl_bh->b_data;
+       tl = &di->id2.i_dealloc;
+       if (!OCFS2_IS_VALID_DINODE(di)) {
+               OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
+               status = -EIO;
+               goto bail;
+       }
+
+       num_to_flush = le16_to_cpu(tl->tl_used);
+       mlog(0, "Flush %u records from truncate log #%"MLFu64"\n",
+            num_to_flush, OCFS2_I(tl_inode)->ip_blkno);
+       if (!num_to_flush) {
+               status = 0;
+               goto bail;
+       }
+
+       handle = ocfs2_alloc_handle(osb);
+       if (!handle) {
+               status = -ENOMEM;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       data_alloc_inode = ocfs2_get_system_file_inode(osb,
+                                                      GLOBAL_BITMAP_SYSTEM_INODE,
+                                                      OCFS2_INVALID_SLOT);
+       if (!data_alloc_inode) {
+               status = -EINVAL;
+               mlog(ML_ERROR, "Could not get bitmap inode!\n");
+               goto bail;
+       }
+
+       ocfs2_handle_add_inode(handle, data_alloc_inode);
+       status = ocfs2_meta_lock(data_alloc_inode, handle, &data_alloc_bh, 1);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       handle = ocfs2_start_trans(osb, handle, OCFS2_TRUNCATE_LOG_UPDATE);
+       if (IS_ERR(handle)) {
+               status = PTR_ERR(handle);
+               handle = NULL;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
+                                              data_alloc_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+bail:
+       if (handle)
+               ocfs2_commit_trans(handle);
+
+       if (data_alloc_inode)
+               iput(data_alloc_inode);
+
+       if (data_alloc_bh)
+               brelse(data_alloc_bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
+{
+       int status;
+       struct inode *tl_inode = osb->osb_tl_inode;
+
+       down(&tl_inode->i_sem);
+       status = __ocfs2_flush_truncate_log(osb);
+       up(&tl_inode->i_sem);
+
+       return status;
+}
+
+static void ocfs2_truncate_log_worker(void *data)
+{
+       int status;
+       struct ocfs2_super *osb = data;
+
+       mlog_entry_void();
+
+       status = ocfs2_flush_truncate_log(osb);
+       if (status < 0)
+               mlog_errno(status);
+
+       mlog_exit(status);
+}
+
+#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
+void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
+                                      int cancel)
+{
+       if (osb->osb_tl_inode) {
+               /* We want to push off log flushes while truncates are
+                * still running. */
+               if (cancel)
+                       cancel_delayed_work(&osb->osb_truncate_log_wq);
+
+               queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
+                                  OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
+       }
+}
+
+static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
+                                      int slot_num,
+                                      struct inode **tl_inode,
+                                      struct buffer_head **tl_bh)
+{
+       int status;
+       struct inode *inode = NULL;
+       struct buffer_head *bh = NULL;
+
+       inode = ocfs2_get_system_file_inode(osb,
+                                          TRUNCATE_LOG_SYSTEM_INODE,
+                                          slot_num);
+       if (!inode) {
+               status = -EINVAL;
+               mlog(ML_ERROR, "Could not get load truncate log inode!\n");
+               goto bail;
+       }
+
+       status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
+                                 OCFS2_BH_CACHED, inode);
+       if (status < 0) {
+               iput(inode);
+               mlog_errno(status);
+               goto bail;
+       }
+
+       *tl_inode = inode;
+       *tl_bh    = bh;
+bail:
+       mlog_exit(status);
+       return status;
+}
+
+/* called during the 1st stage of node recovery. we stamp a clean
+ * truncate log and pass back a copy for processing later. if the
+ * truncate log does not require processing, a *tl_copy is set to
+ * NULL. */
+int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
+                                     int slot_num,
+                                     struct ocfs2_dinode **tl_copy)
+{
+       int status;
+       struct inode *tl_inode = NULL;
+       struct buffer_head *tl_bh = NULL;
+       struct ocfs2_dinode *di;
+       struct ocfs2_truncate_log *tl;
+
+       *tl_copy = NULL;
+
+       mlog(0, "recover truncate log from slot %d\n", slot_num);
+
+       status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       di = (struct ocfs2_dinode *) tl_bh->b_data;
+       tl = &di->id2.i_dealloc;
+       if (!OCFS2_IS_VALID_DINODE(di)) {
+               OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
+               status = -EIO;
+               goto bail;
+       }
+
+       if (le16_to_cpu(tl->tl_used)) {
+               mlog(0, "We'll have %u logs to recover\n",
+                    le16_to_cpu(tl->tl_used));
+
+               *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
+               if (!(*tl_copy)) {
+                       status = -ENOMEM;
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               /* Assuming the write-out below goes well, this copy
+                * will be passed back to recovery for processing. */
+               memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
+
+               /* All we need to do to clear the truncate log is set
+                * tl_used. */
+               tl->tl_used = 0;
+
+               status = ocfs2_write_block(osb, tl_bh, tl_inode);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
+bail:
+       if (tl_inode)
+               iput(tl_inode);
+       if (tl_bh)
+               brelse(tl_bh);
+
+       if (status < 0 && (*tl_copy)) {
+               kfree(*tl_copy);
+               *tl_copy = NULL;
+       }
+
+       mlog_exit(status);
+       return status;
+}
+
+int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
+                                        struct ocfs2_dinode *tl_copy)
+{
+       int status = 0;
+       int i;
+       unsigned int clusters, num_recs, start_cluster;
+       u64 start_blk;
+       struct ocfs2_journal_handle *handle;
+       struct inode *tl_inode = osb->osb_tl_inode;
+       struct ocfs2_truncate_log *tl;
+
+       mlog_entry_void();
+
+       if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
+               mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
+               return -EINVAL;
+       }
+
+       tl = &tl_copy->id2.i_dealloc;
+       num_recs = le16_to_cpu(tl->tl_used);
+       mlog(0, "cleanup %u records from %"MLFu64"\n", num_recs,
+            tl_copy->i_blkno);
+
+       down(&tl_inode->i_sem);
+       for(i = 0; i < num_recs; i++) {
+               if (ocfs2_truncate_log_needs_flush(osb)) {
+                       status = __ocfs2_flush_truncate_log(osb);
+                       if (status < 0) {
+                               mlog_errno(status);
+                               goto bail_up;
+                       }
+               }
+
+               handle = ocfs2_start_trans(osb, NULL,
+                                          OCFS2_TRUNCATE_LOG_UPDATE);
+               if (IS_ERR(handle)) {
+                       status = PTR_ERR(handle);
+                       mlog_errno(status);
+                       goto bail_up;
+               }
+
+               clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
+               start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
+               start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
+
+               status = ocfs2_truncate_log_append(osb, handle,
+                                                  start_blk, clusters);
+               ocfs2_commit_trans(handle);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail_up;
+               }
+       }
+
+bail_up:
+       up(&tl_inode->i_sem);
+
+       mlog_exit(status);
+       return status;
+}
+
+void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
+{
+       int status;
+       struct inode *tl_inode = osb->osb_tl_inode;
+
+       mlog_entry_void();
+
+       if (tl_inode) {
+               cancel_delayed_work(&osb->osb_truncate_log_wq);
+               flush_workqueue(ocfs2_wq);
+
+               status = ocfs2_flush_truncate_log(osb);
+               if (status < 0)
+                       mlog_errno(status);
+
+               brelse(osb->osb_tl_bh);
+               iput(osb->osb_tl_inode);
+       }
+
+       mlog_exit_void();
+}
+
+int ocfs2_truncate_log_init(struct ocfs2_super *osb)
+{
+       int status;
+       struct inode *tl_inode = NULL;
+       struct buffer_head *tl_bh = NULL;
+
+       mlog_entry_void();
+
+       status = ocfs2_get_truncate_log_info(osb,
+                                            osb->slot_num,
+                                            &tl_inode,
+                                            &tl_bh);
+       if (status < 0)
+               mlog_errno(status);
+
+       /* ocfs2_truncate_log_shutdown keys on the existence of
+        * osb->osb_tl_inode so we don't set any of the osb variables
+        * until we're sure all is well. */
+       INIT_WORK(&osb->osb_truncate_log_wq, ocfs2_truncate_log_worker, osb);
+       osb->osb_tl_bh    = tl_bh;
+       osb->osb_tl_inode = tl_inode;
+
+       mlog_exit(status);
+       return status;
+}
+
+/* This function will figure out whether the currently last extent
+ * block will be deleted, and if it will, what the new last extent
+ * block will be so we can update his h_next_leaf_blk field, as well
+ * as the dinodes i_last_eb_blk */
+static int ocfs2_find_new_last_ext_blk(struct ocfs2_super *osb,
+                                      struct inode *inode,
+                                      struct ocfs2_dinode *fe,
+                                      u32 new_i_clusters,
+                                      struct buffer_head *old_last_eb,
+                                      struct buffer_head **new_last_eb)
+{
+       int i, status = 0;
+       u64 block = 0;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list *el;
+       struct buffer_head *bh = NULL;
+
+       *new_last_eb = NULL;
+
+       if (!OCFS2_IS_VALID_DINODE(fe)) {
+               OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
+               status = -EIO;
+               goto bail;
+       }
+
+       /* we have no tree, so of course, no last_eb. */
+       if (!fe->id2.i_list.l_tree_depth)
+               goto bail;
+
+       /* trunc to zero special case - this makes tree_depth = 0
+        * regardless of what it is.  */
+       if (!new_i_clusters)
+               goto bail;
+
+       eb = (struct ocfs2_extent_block *) old_last_eb->b_data;
+       el = &(eb->h_list);
+       BUG_ON(!el->l_next_free_rec);
+
+       /* Make sure that this guy will actually be empty after we
+        * clear away the data. */
+       if (le32_to_cpu(el->l_recs[0].e_cpos) < new_i_clusters)
+               goto bail;
+
+       /* Ok, at this point, we know that last_eb will definitely
+        * change, so lets traverse the tree and find the second to
+        * last extent block. */
+       el = &(fe->id2.i_list);
+       /* go down the tree, */
+       do {
+               for(i = (le16_to_cpu(el->l_next_free_rec) - 1); i >= 0; i--) {
+                       if (le32_to_cpu(el->l_recs[i].e_cpos) <
+                           new_i_clusters) {
+                               block = le64_to_cpu(el->l_recs[i].e_blkno);
+                               break;
+                       }
+               }
+               BUG_ON(i < 0);
+
+               if (bh) {
+                       brelse(bh);
+                       bh = NULL;
+               }
+
+               status = ocfs2_read_block(osb, block, &bh, OCFS2_BH_CACHED,
+                                        inode);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+               eb = (struct ocfs2_extent_block *) bh->b_data;
+               el = &eb->h_list;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+                       status = -EIO;
+                       goto bail;
+               }
+       } while (el->l_tree_depth);
+
+       *new_last_eb = bh;
+       get_bh(*new_last_eb);
+       mlog(0, "returning block %"MLFu64"\n", le64_to_cpu(eb->h_blkno));
+bail:
+       if (bh)
+               brelse(bh);
+
+       return status;
+}
+
+static int ocfs2_do_truncate(struct ocfs2_super *osb,
+                            unsigned int clusters_to_del,
+                            struct inode *inode,
+                            struct buffer_head *fe_bh,
+                            struct buffer_head *old_last_eb_bh,
+                            struct ocfs2_journal_handle *handle,
+                            struct ocfs2_truncate_context *tc)
+{
+       int status, i, depth;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_block *last_eb = NULL;
+       struct ocfs2_extent_list *el;
+       struct buffer_head *eb_bh = NULL;
+       struct buffer_head *last_eb_bh = NULL;
+       u64 next_eb = 0;
+       u64 delete_blk = 0;
+
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+
+       status = ocfs2_find_new_last_ext_blk(osb,
+                                            inode,
+                                            fe,
+                                            le32_to_cpu(fe->i_clusters) -
+                                                       clusters_to_del,
+                                            old_last_eb_bh,
+                                            &last_eb_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       if (last_eb_bh)
+               last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
+
+       status = ocfs2_journal_access(handle, inode, fe_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       el = &(fe->id2.i_list);
+
+       spin_lock(&OCFS2_I(inode)->ip_lock);
+       OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
+                                     clusters_to_del;
+       spin_unlock(&OCFS2_I(inode)->ip_lock);
+       le32_add_cpu(&fe->i_clusters, -clusters_to_del);
+       fe->i_mtime = cpu_to_le64(CURRENT_TIME.tv_sec);
+       fe->i_mtime_nsec = cpu_to_le32(CURRENT_TIME.tv_nsec);
+
+       i = le16_to_cpu(el->l_next_free_rec) - 1;
+
+       BUG_ON(le32_to_cpu(el->l_recs[i].e_clusters) < clusters_to_del);
+       le32_add_cpu(&el->l_recs[i].e_clusters, -clusters_to_del);
+       /* tree depth zero, we can just delete the clusters, otherwise
+        * we need to record the offset of the next level extent block
+        * as we may overwrite it. */
+       if (!el->l_tree_depth)
+               delete_blk = le64_to_cpu(el->l_recs[i].e_blkno)
+                       + ocfs2_clusters_to_blocks(osb->sb,
+                                       le32_to_cpu(el->l_recs[i].e_clusters));
+       else
+               next_eb = le64_to_cpu(el->l_recs[i].e_blkno);
+
+       if (!el->l_recs[i].e_clusters) {
+               /* if we deleted the whole extent record, then clear
+                * out the other fields and update the extent
+                * list. For depth > 0 trees, we've already recorded
+                * the extent block in 'next_eb' */
+               el->l_recs[i].e_cpos = 0;
+               el->l_recs[i].e_blkno = 0;
+               BUG_ON(!el->l_next_free_rec);
+               le16_add_cpu(&el->l_next_free_rec, -1);
+       }
+
+       depth = le16_to_cpu(el->l_tree_depth);
+       if (!fe->i_clusters) {
+               /* trunc to zero is a special case. */
+               el->l_tree_depth = 0;
+               fe->i_last_eb_blk = 0;
+       } else if (last_eb)
+               fe->i_last_eb_blk = last_eb->h_blkno;
+
+       status = ocfs2_journal_dirty(handle, fe_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       if (last_eb) {
+               /* If there will be a new last extent block, then by
+                * definition, there cannot be any leaves to the right of
+                * him. */
+               status = ocfs2_journal_access(handle, inode, last_eb_bh,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+               last_eb->h_next_leaf_blk = 0;
+               status = ocfs2_journal_dirty(handle, last_eb_bh);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
+       /* if our tree depth > 0, update all the tree blocks below us. */
+       while (depth) {
+               mlog(0, "traveling tree (depth = %d, next_eb = %"MLFu64")\n",
+                    depth,  next_eb);
+               status = ocfs2_read_block(osb, next_eb, &eb_bh,
+                                         OCFS2_BH_CACHED, inode);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+               eb = (struct ocfs2_extent_block *)eb_bh->b_data;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+                       status = -EIO;
+                       goto bail;
+               }
+               el = &(eb->h_list);
+
+               status = ocfs2_journal_access(handle, inode, eb_bh,
+                                             OCFS2_JOURNAL_ACCESS_WRITE);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
+               BUG_ON(depth != (le16_to_cpu(el->l_tree_depth) + 1));
+
+               i = le16_to_cpu(el->l_next_free_rec) - 1;
+
+               mlog(0, "extent block %"MLFu64", before: record %d: "
+                    "(%u, %u, %"MLFu64"), next = %u\n",
+                    le64_to_cpu(eb->h_blkno), i,
+                    le32_to_cpu(el->l_recs[i].e_cpos),
+                    le32_to_cpu(el->l_recs[i].e_clusters),
+                    le64_to_cpu(el->l_recs[i].e_blkno),
+                    le16_to_cpu(el->l_next_free_rec));
+
+               BUG_ON(le32_to_cpu(el->l_recs[i].e_clusters) < clusters_to_del);
+               le32_add_cpu(&el->l_recs[i].e_clusters, -clusters_to_del);
+
+               next_eb = le64_to_cpu(el->l_recs[i].e_blkno);
+               /* bottom-most block requires us to delete data.*/
+               if (!el->l_tree_depth)
+                       delete_blk = le64_to_cpu(el->l_recs[i].e_blkno)
+                               + ocfs2_clusters_to_blocks(osb->sb,
+                                       le32_to_cpu(el->l_recs[i].e_clusters));
+               if (!el->l_recs[i].e_clusters) {
+                       el->l_recs[i].e_cpos = 0;
+                       el->l_recs[i].e_blkno = 0;
+                       BUG_ON(!el->l_next_free_rec);
+                       le16_add_cpu(&el->l_next_free_rec, -1);
+               }
+               mlog(0, "extent block %"MLFu64", after: record %d: "
+                    "(%u, %u, %"MLFu64"), next = %u\n",
+                    le64_to_cpu(eb->h_blkno), i,
+                    le32_to_cpu(el->l_recs[i].e_cpos),
+                    le32_to_cpu(el->l_recs[i].e_clusters),
+                    le64_to_cpu(el->l_recs[i].e_blkno),
+                    le16_to_cpu(el->l_next_free_rec));
+
+               status = ocfs2_journal_dirty(handle, eb_bh);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               if (!el->l_next_free_rec) {
+                       mlog(0, "deleting this extent block.\n");
+
+                       ocfs2_remove_from_cache(inode, eb_bh);
+
+                       BUG_ON(eb->h_suballoc_slot);
+                       BUG_ON(el->l_recs[0].e_clusters);
+                       BUG_ON(el->l_recs[0].e_cpos);
+                       BUG_ON(el->l_recs[0].e_blkno);
+                       status = ocfs2_free_extent_block(handle,
+                                                        tc->tc_ext_alloc_inode,
+                                                        tc->tc_ext_alloc_bh,
+                                                        eb);
+                       if (status < 0) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+               }
+               brelse(eb_bh);
+               eb_bh = NULL;
+               depth--;
+       }
+
+       BUG_ON(!delete_blk);
+       status = ocfs2_truncate_log_append(osb, handle, delete_blk,
+                                          clusters_to_del);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       status = 0;
+bail:
+       if (!status)
+               ocfs2_extent_map_trunc(inode, le32_to_cpu(fe->i_clusters));
+       else
+               ocfs2_extent_map_drop(inode, 0);
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * It is expected, that by the time you call this function,
+ * inode->i_size and fe->i_size have been adjusted.
+ *
+ * WARNING: This will kfree the truncate context
+ */
+int ocfs2_commit_truncate(struct ocfs2_super *osb,
+                         struct inode *inode,
+                         struct buffer_head *fe_bh,
+                         struct ocfs2_truncate_context *tc)
+{
+       int status, i, credits, tl_sem = 0;
+       u32 clusters_to_del, target_i_clusters;
+       u64 last_eb = 0;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list *el;
+       struct buffer_head *last_eb_bh;
+       struct ocfs2_journal_handle *handle = NULL;
+       struct inode *tl_inode = osb->osb_tl_inode;
+
+       mlog_entry_void();
+
+       down_write(&OCFS2_I(inode)->ip_alloc_sem);
+
+       target_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
+                                                    i_size_read(inode));
+
+       last_eb_bh = tc->tc_last_eb_bh;
+       tc->tc_last_eb_bh = NULL;
+
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+
+       if (fe->id2.i_list.l_tree_depth) {
+               eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
+               el = &eb->h_list;
+       } else
+               el = &fe->id2.i_list;
+       last_eb = le64_to_cpu(fe->i_last_eb_blk);
+start:
+       mlog(0, "ocfs2_commit_truncate: fe->i_clusters = %u, "
+            "last_eb = %"MLFu64", fe->i_last_eb_blk = %"MLFu64", "
+            "fe->id2.i_list.l_tree_depth = %u last_eb_bh = %p\n",
+            le32_to_cpu(fe->i_clusters), last_eb,
+            le64_to_cpu(fe->i_last_eb_blk),
+            le16_to_cpu(fe->id2.i_list.l_tree_depth), last_eb_bh);
+
+       if (last_eb != le64_to_cpu(fe->i_last_eb_blk)) {
+               mlog(0, "last_eb changed!\n");
+               BUG_ON(!fe->id2.i_list.l_tree_depth);
+               last_eb = le64_to_cpu(fe->i_last_eb_blk);
+               /* i_last_eb_blk may have changed, read it if
+                * necessary. We don't have to worry about the
+                * truncate to zero case here (where there becomes no
+                * last_eb) because we never loop back after our work
+                * is done. */
+               if (last_eb_bh) {
+                       brelse(last_eb_bh);
+                       last_eb_bh = NULL;
+               }
+
+               status = ocfs2_read_block(osb, last_eb,
+                                         &last_eb_bh, OCFS2_BH_CACHED,
+                                         inode);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+               eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+                       status = -EIO;
+                       goto bail;
+               }
+               el = &(eb->h_list);
+       }
+
+       /* by now, el will point to the extent list on the bottom most
+        * portion of this tree. */
+       i = le16_to_cpu(el->l_next_free_rec) - 1;
+       if (le32_to_cpu(el->l_recs[i].e_cpos) >= target_i_clusters)
+               clusters_to_del = le32_to_cpu(el->l_recs[i].e_clusters);
+       else
+               clusters_to_del = (le32_to_cpu(el->l_recs[i].e_clusters) +
+                                  le32_to_cpu(el->l_recs[i].e_cpos)) -
+                                 target_i_clusters;
+
+       mlog(0, "clusters_to_del = %u in this pass\n", clusters_to_del);
+
+       down(&tl_inode->i_sem);
+       tl_sem = 1;
+       /* ocfs2_truncate_log_needs_flush guarantees us at least one
+        * record is free for use. If there isn't any, we flush to get
+        * an empty truncate log.  */
+       if (ocfs2_truncate_log_needs_flush(osb)) {
+               status = __ocfs2_flush_truncate_log(osb);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
+       credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
+                                               fe, el);
+       handle = ocfs2_start_trans(osb, NULL, credits);
+       if (IS_ERR(handle)) {
+               status = PTR_ERR(handle);
+               handle = NULL;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       inode->i_ctime = inode->i_mtime = CURRENT_TIME;
+       status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
+       if (status < 0)
+               mlog_errno(status);
+
+       status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh,
+                                  last_eb_bh, handle, tc);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       up(&tl_inode->i_sem);
+       tl_sem = 0;
+
+       ocfs2_commit_trans(handle);
+       handle = NULL;
+
+       BUG_ON(le32_to_cpu(fe->i_clusters) < target_i_clusters);
+       if (le32_to_cpu(fe->i_clusters) > target_i_clusters)
+               goto start;
+bail:
+       up_write(&OCFS2_I(inode)->ip_alloc_sem);
+
+       ocfs2_schedule_truncate_log_flush(osb, 1);
+
+       if (tl_sem)
+               up(&tl_inode->i_sem);
+
+       if (handle)
+               ocfs2_commit_trans(handle);
+
+       if (last_eb_bh)
+               brelse(last_eb_bh);
+
+       /* This will drop the ext_alloc cluster lock for us */
+       ocfs2_free_truncate_context(tc);
+
+       mlog_exit(status);
+       return status;
+}
+
+
+/*
+ * Expects the inode to already be locked. This will figure out which
+ * inodes need to be locked and will put them on the returned truncate
+ * context.
+ */
+int ocfs2_prepare_truncate(struct ocfs2_super *osb,
+                          struct inode *inode,
+                          struct buffer_head *fe_bh,
+                          struct ocfs2_truncate_context **tc)
+{
+       int status, metadata_delete;
+       unsigned int new_i_clusters;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_list *el;
+       struct buffer_head *last_eb_bh = NULL;
+       struct inode *ext_alloc_inode = NULL;
+       struct buffer_head *ext_alloc_bh = NULL;
+
+       mlog_entry_void();
+
+       *tc = NULL;
+
+       new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
+                                                 i_size_read(inode));
+       fe = (struct ocfs2_dinode *) fe_bh->b_data;
+
+       mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
+            "%"MLFu64"\n", fe->i_clusters, new_i_clusters, fe->i_size);
+
+       if (le32_to_cpu(fe->i_clusters) <= new_i_clusters) {
+               ocfs2_error(inode->i_sb, "Dinode %"MLFu64" has cluster count "
+                           "%u and size %"MLFu64" whereas struct inode has "
+                           "cluster count %u and size %llu which caused an "
+                           "invalid truncate to %u clusters.",
+                           le64_to_cpu(fe->i_blkno),
+                           le32_to_cpu(fe->i_clusters),
+                           le64_to_cpu(fe->i_size),
+                           OCFS2_I(inode)->ip_clusters, i_size_read(inode),
+                           new_i_clusters);
+               mlog_meta_lvb(ML_ERROR, &OCFS2_I(inode)->ip_meta_lockres);
+               status = -EIO;
+               goto bail;
+       }
+
+       *tc = kcalloc(1, sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
+       if (!(*tc)) {
+               status = -ENOMEM;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       metadata_delete = 0;
+       if (fe->id2.i_list.l_tree_depth) {
+               /* If we have a tree, then the truncate may result in
+                * metadata deletes. Figure this out from the
+                * rightmost leaf block.*/
+               status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
+                                         &last_eb_bh, OCFS2_BH_CACHED, inode);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+               eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+
+                       brelse(last_eb_bh);
+                       status = -EIO;
+                       goto bail;
+               }
+               el = &(eb->h_list);
+               if (le32_to_cpu(el->l_recs[0].e_cpos) >= new_i_clusters)
+                       metadata_delete = 1;
+       }
+
+       (*tc)->tc_last_eb_bh = last_eb_bh;
+
+       if (metadata_delete) {
+               mlog(0, "Will have to delete metadata for this trunc. "
+                    "locking allocator.\n");
+               ext_alloc_inode = ocfs2_get_system_file_inode(osb, EXTENT_ALLOC_SYSTEM_INODE, 0);
+               if (!ext_alloc_inode) {
+                       status = -ENOMEM;
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               down(&ext_alloc_inode->i_sem);
+               (*tc)->tc_ext_alloc_inode = ext_alloc_inode;
+
+               status = ocfs2_meta_lock(ext_alloc_inode,
+                                        NULL,
+                                        &ext_alloc_bh,
+                                        1);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+               (*tc)->tc_ext_alloc_bh = ext_alloc_bh;
+               (*tc)->tc_ext_alloc_locked = 1;
+       }
+
+       status = 0;
+bail:
+       if (status < 0) {
+               if (*tc)
+                       ocfs2_free_truncate_context(*tc);
+               *tc = NULL;
+       }
+       mlog_exit_void();
+       return status;
+}
+
+static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
+{
+       if (tc->tc_ext_alloc_inode) {
+               if (tc->tc_ext_alloc_locked)
+                       ocfs2_meta_unlock(tc->tc_ext_alloc_inode, 1);
+
+               up(&tc->tc_ext_alloc_inode->i_sem);
+               iput(tc->tc_ext_alloc_inode);
+       }
+
+       if (tc->tc_ext_alloc_bh)
+               brelse(tc->tc_ext_alloc_bh);
+
+       if (tc->tc_last_eb_bh)
+               brelse(tc->tc_last_eb_bh);
+
+       kfree(tc);
+}
diff --git a/fs/ocfs2/alloc.h b/fs/ocfs2/alloc.h
new file mode 100644 (file)
index 0000000..12ba897
--- /dev/null
@@ -0,0 +1,82 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * alloc.h
+ *
+ * Function prototypes
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef OCFS2_ALLOC_H
+#define OCFS2_ALLOC_H
+
+struct ocfs2_alloc_context;
+int ocfs2_insert_extent(struct ocfs2_super *osb,
+                       struct ocfs2_journal_handle *handle,
+                       struct inode *inode,
+                       struct buffer_head *fe_bh,
+                       u64 blkno,
+                       u32 new_clusters,
+                       struct ocfs2_alloc_context *meta_ac);
+int ocfs2_num_free_extents(struct ocfs2_super *osb,
+                          struct inode *inode,
+                          struct ocfs2_dinode *fe);
+/* how many new metadata chunks would an allocation need at maximum? */
+static inline int ocfs2_extend_meta_needed(struct ocfs2_dinode *fe)
+{
+       /*
+        * Rather than do all the work of determining how much we need
+        * (involves a ton of reads and locks), just ask for the
+        * maximal limit.  That's a tree depth shift.  So, one block for
+        * level of the tree (current l_tree_depth), one block for the
+        * new tree_depth==0 extent_block, and one block at the new
+        * top-of-the tree.
+        */
+       return le16_to_cpu(fe->id2.i_list.l_tree_depth) + 2;
+}
+
+int ocfs2_truncate_log_init(struct ocfs2_super *osb);
+void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb);
+void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
+                                      int cancel);
+int ocfs2_flush_truncate_log(struct ocfs2_super *osb);
+int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
+                                     int slot_num,
+                                     struct ocfs2_dinode **tl_copy);
+int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
+                                        struct ocfs2_dinode *tl_copy);
+
+struct ocfs2_truncate_context {
+       struct inode *tc_ext_alloc_inode;
+       struct buffer_head *tc_ext_alloc_bh;
+       int tc_ext_alloc_locked; /* is it cluster locked? */
+       /* these get destroyed once it's passed to ocfs2_commit_truncate. */
+       struct buffer_head *tc_last_eb_bh;
+};
+
+int ocfs2_prepare_truncate(struct ocfs2_super *osb,
+                          struct inode *inode,
+                          struct buffer_head *fe_bh,
+                          struct ocfs2_truncate_context **tc);
+int ocfs2_commit_truncate(struct ocfs2_super *osb,
+                         struct inode *inode,
+                         struct buffer_head *fe_bh,
+                         struct ocfs2_truncate_context *tc);
+
+#endif /* OCFS2_ALLOC_H */
diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
new file mode 100644 (file)
index 0000000..8f4467a
--- /dev/null
@@ -0,0 +1,643 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/highmem.h>
+#include <linux/pagemap.h>
+#include <asm/byteorder.h>
+
+#define MLOG_MASK_PREFIX ML_FILE_IO
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "alloc.h"
+#include "aops.h"
+#include "dlmglue.h"
+#include "extent_map.h"
+#include "file.h"
+#include "inode.h"
+#include "journal.h"
+#include "super.h"
+#include "symlink.h"
+
+#include "buffer_head_io.h"
+
+static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
+                                  struct buffer_head *bh_result, int create)
+{
+       int err = -EIO;
+       int status;
+       struct ocfs2_dinode *fe = NULL;
+       struct buffer_head *bh = NULL;
+       struct buffer_head *buffer_cache_bh = NULL;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       void *kaddr;
+
+       mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
+                  (unsigned long long)iblock, bh_result, create);
+
+       BUG_ON(ocfs2_inode_is_fast_symlink(inode));
+
+       if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
+               mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
+                    (unsigned long long)iblock);
+               goto bail;
+       }
+
+       status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
+                                 OCFS2_I(inode)->ip_blkno,
+                                 &bh, OCFS2_BH_CACHED, inode);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       fe = (struct ocfs2_dinode *) bh->b_data;
+
+       if (!OCFS2_IS_VALID_DINODE(fe)) {
+               mlog(ML_ERROR, "Invalid dinode #%"MLFu64": signature = %.*s\n",
+                    fe->i_blkno, 7, fe->i_signature);
+               goto bail;
+       }
+
+       if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
+                                                   le32_to_cpu(fe->i_clusters))) {
+               mlog(ML_ERROR, "block offset is outside the allocated size: "
+                    "%llu\n", (unsigned long long)iblock);
+               goto bail;
+       }
+
+       /* We don't use the page cache to create symlink data, so if
+        * need be, copy it over from the buffer cache. */
+       if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
+               u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
+                           iblock;
+               buffer_cache_bh = sb_getblk(osb->sb, blkno);
+               if (!buffer_cache_bh) {
+                       mlog(ML_ERROR, "couldn't getblock for symlink!\n");
+                       goto bail;
+               }
+
+               /* we haven't locked out transactions, so a commit
+                * could've happened. Since we've got a reference on
+                * the bh, even if it commits while we're doing the
+                * copy, the data is still good. */
+               if (buffer_jbd(buffer_cache_bh)
+                   && ocfs2_inode_is_new(inode)) {
+                       kaddr = kmap_atomic(bh_result->b_page, KM_USER0);
+                       if (!kaddr) {
+                               mlog(ML_ERROR, "couldn't kmap!\n");
+                               goto bail;
+                       }
+                       memcpy(kaddr + (bh_result->b_size * iblock),
+                              buffer_cache_bh->b_data,
+                              bh_result->b_size);
+                       kunmap_atomic(kaddr, KM_USER0);
+                       set_buffer_uptodate(bh_result);
+               }
+               brelse(buffer_cache_bh);
+       }
+
+       map_bh(bh_result, inode->i_sb,
+              le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
+
+       err = 0;
+
+bail:
+       if (bh)
+               brelse(bh);
+
+       mlog_exit(err);
+       return err;
+}
+
+static int ocfs2_get_block(struct inode *inode, sector_t iblock,
+                          struct buffer_head *bh_result, int create)
+{
+       int err = 0;
+       u64 p_blkno, past_eof;
+
+       mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
+                  (unsigned long long)iblock, bh_result, create);
+
+       if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
+               mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
+                    inode, inode->i_ino);
+
+       if (S_ISLNK(inode->i_mode)) {
+               /* this always does I/O for some reason. */
+               err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
+               goto bail;
+       }
+
+       /* this can happen if another node truncs after our extend! */
+       spin_lock(&OCFS2_I(inode)->ip_lock);
+       if (iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
+                                              OCFS2_I(inode)->ip_clusters))
+               err = -EIO;
+       spin_unlock(&OCFS2_I(inode)->ip_lock);
+       if (err)
+               goto bail;
+
+       err = ocfs2_extent_map_get_blocks(inode, iblock, 1, &p_blkno,
+                                         NULL);
+       if (err) {
+               mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
+                    "%"MLFu64", NULL)\n", err, inode,
+                    (unsigned long long)iblock, p_blkno);
+               goto bail;
+       }
+
+       map_bh(bh_result, inode->i_sb, p_blkno);
+
+       if (bh_result->b_blocknr == 0) {
+               err = -EIO;
+               mlog(ML_ERROR, "iblock = %llu p_blkno = %"MLFu64" "
+                    "blkno=(%"MLFu64")\n", (unsigned long long)iblock,
+                    p_blkno, OCFS2_I(inode)->ip_blkno);
+       }
+
+       past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
+       mlog(0, "Inode %lu, past_eof = %"MLFu64"\n", inode->i_ino, past_eof);
+
+       if (create && (iblock >= past_eof))
+               set_buffer_new(bh_result);
+
+bail:
+       if (err < 0)
+               err = -EIO;
+
+       mlog_exit(err);
+       return err;
+}
+
+static int ocfs2_readpage(struct file *file, struct page *page)
+{
+       struct inode *inode = page->mapping->host;
+       loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
+       int ret, unlock = 1;
+
+       mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
+
+       ret = ocfs2_meta_lock_with_page(inode, NULL, NULL, 0, page);
+       if (ret != 0) {
+               if (ret == AOP_TRUNCATED_PAGE)
+                       unlock = 0;
+               mlog_errno(ret);
+               goto out;
+       }
+
+       down_read(&OCFS2_I(inode)->ip_alloc_sem);
+
+       /*
+        * i_size might have just been updated as we grabed the meta lock.  We
+        * might now be discovering a truncate that hit on another node.
+        * block_read_full_page->get_block freaks out if it is asked to read
+        * beyond the end of a file, so we check here.  Callers
+        * (generic_file_read, fault->nopage) are clever enough to check i_size
+        * and notice that the page they just read isn't needed.
+        *
+        * XXX sys_readahead() seems to get that wrong?
+        */
+       if (start >= i_size_read(inode)) {
+               char *addr = kmap(page);
+               memset(addr, 0, PAGE_SIZE);
+               flush_dcache_page(page);
+               kunmap(page);
+               SetPageUptodate(page);
+               ret = 0;
+               goto out_alloc;
+       }
+
+       ret = ocfs2_data_lock_with_page(inode, 0, page);
+       if (ret != 0) {
+               if (ret == AOP_TRUNCATED_PAGE)
+                       unlock = 0;
+               mlog_errno(ret);
+               goto out_alloc;
+       }
+
+       ret = block_read_full_page(page, ocfs2_get_block);
+       unlock = 0;
+
+       ocfs2_data_unlock(inode, 0);
+out_alloc:
+       up_read(&OCFS2_I(inode)->ip_alloc_sem);
+       ocfs2_meta_unlock(inode, 0);
+out:
+       if (unlock)
+               unlock_page(page);
+       mlog_exit(ret);
+       return ret;
+}
+
+/* Note: Because we don't support holes, our allocation has
+ * already happened (allocation writes zeros to the file data)
+ * so we don't have to worry about ordered writes in
+ * ocfs2_writepage.
+ *
+ * ->writepage is called during the process of invalidating the page cache
+ * during blocked lock processing.  It can't block on any cluster locks
+ * to during block mapping.  It's relying on the fact that the block
+ * mapping can't have disappeared under the dirty pages that it is
+ * being asked to write back.
+ */
+static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
+{
+       int ret;
+
+       mlog_entry("(0x%p)\n", page);
+
+       ret = block_write_full_page(page, ocfs2_get_block, wbc);
+
+       mlog_exit(ret);
+
+       return ret;
+}
+
+/*
+ * ocfs2_prepare_write() can be an outer-most ocfs2 call when it is called
+ * from loopback.  It must be able to perform its own locking around
+ * ocfs2_get_block().
+ */
+int ocfs2_prepare_write(struct file *file, struct page *page,
+                       unsigned from, unsigned to)
+{
+       struct inode *inode = page->mapping->host;
+       int ret;
+
+       mlog_entry("(0x%p, 0x%p, %u, %u)\n", file, page, from, to);
+
+       ret = ocfs2_meta_lock_with_page(inode, NULL, NULL, 0, page);
+       if (ret != 0) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       down_read(&OCFS2_I(inode)->ip_alloc_sem);
+
+       ret = block_prepare_write(page, from, to, ocfs2_get_block);
+
+       up_read(&OCFS2_I(inode)->ip_alloc_sem);
+
+       ocfs2_meta_unlock(inode, 0);
+out:
+       mlog_exit(ret);
+       return ret;
+}
+
+/* Taken from ext3. We don't necessarily need the full blown
+ * functionality yet, but IMHO it's better to cut and paste the whole
+ * thing so we can avoid introducing our own bugs (and easily pick up
+ * their fixes when they happen) --Mark */
+static int walk_page_buffers(  handle_t *handle,
+                               struct buffer_head *head,
+                               unsigned from,
+                               unsigned to,
+                               int *partial,
+                               int (*fn)(      handle_t *handle,
+                                               struct buffer_head *bh))
+{
+       struct buffer_head *bh;
+       unsigned block_start, block_end;
+       unsigned blocksize = head->b_size;
+       int err, ret = 0;
+       struct buffer_head *next;
+
+       for (   bh = head, block_start = 0;
+               ret == 0 && (bh != head || !block_start);
+               block_start = block_end, bh = next)
+       {
+               next = bh->b_this_page;
+               block_end = block_start + blocksize;
+               if (block_end <= from || block_start >= to) {
+                       if (partial && !buffer_uptodate(bh))
+                               *partial = 1;
+                       continue;
+               }
+               err = (*fn)(handle, bh);
+               if (!ret)
+                       ret = err;
+       }
+       return ret;
+}
+
+struct ocfs2_journal_handle *ocfs2_start_walk_page_trans(struct inode *inode,
+                                                        struct page *page,
+                                                        unsigned from,
+                                                        unsigned to)
+{
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct ocfs2_journal_handle *handle = NULL;
+       int ret = 0;
+
+       handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
+       if (!handle) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto out;
+       }
+
+       if (ocfs2_should_order_data(inode)) {
+               ret = walk_page_buffers(handle->k_handle,
+                                       page_buffers(page),
+                                       from, to, NULL,
+                                       ocfs2_journal_dirty_data);
+               if (ret < 0) 
+                       mlog_errno(ret);
+       }
+out:
+       if (ret) {
+               if (handle)
+                       ocfs2_commit_trans(handle);
+               handle = ERR_PTR(ret);
+       }
+       return handle;
+}
+
+static int ocfs2_commit_write(struct file *file, struct page *page,
+                             unsigned from, unsigned to)
+{
+       int ret, extending = 0, locklevel = 0;
+       loff_t new_i_size;
+       struct buffer_head *di_bh = NULL;
+       struct inode *inode = page->mapping->host;
+       struct ocfs2_journal_handle *handle = NULL;
+
+       mlog_entry("(0x%p, 0x%p, %u, %u)\n", file, page, from, to);
+
+       /* NOTE: ocfs2_file_aio_write has ensured that it's safe for
+        * us to sample inode->i_size here without the metadata lock:
+        *
+        * 1) We're currently holding the inode alloc lock, so no
+        *    nodes can change it underneath us.
+        *
+        * 2) We've had to take the metadata lock at least once
+        *    already to check for extending writes, hence insuring
+        *    that our current copy is also up to date.
+        */
+       new_i_size = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
+       if (new_i_size > i_size_read(inode)) {
+               extending = 1;
+               locklevel = 1;
+       }
+
+       ret = ocfs2_meta_lock_with_page(inode, NULL, &di_bh, locklevel, page);
+       if (ret != 0) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       ret = ocfs2_data_lock_with_page(inode, 1, page);
+       if (ret != 0) {
+               mlog_errno(ret);
+               goto out_unlock_meta;
+       }
+
+       if (extending) {
+               handle = ocfs2_start_walk_page_trans(inode, page, from, to);
+               if (IS_ERR(handle)) {
+                       ret = PTR_ERR(handle);
+                       handle = NULL;
+                       goto out_unlock_data;
+               }
+
+               /* Mark our buffer early. We'd rather catch this error up here
+                * as opposed to after a successful commit_write which would
+                * require us to set back inode->i_size. */
+               ret = ocfs2_journal_access(handle, inode, di_bh,
+                                          OCFS2_JOURNAL_ACCESS_WRITE);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out_commit;
+               }
+       }
+
+       /* might update i_size */
+       ret = generic_commit_write(file, page, from, to);
+       if (ret < 0) {
+               mlog_errno(ret);
+               goto out_commit;
+       }
+
+       if (extending) {
+               loff_t size = (u64) i_size_read(inode);
+               struct ocfs2_dinode *di =
+                       (struct ocfs2_dinode *)di_bh->b_data;
+
+               /* ocfs2_mark_inode_dirty is too heavy to use here. */
+               inode->i_blocks = ocfs2_align_bytes_to_sectors(size);
+               inode->i_ctime = inode->i_mtime = CURRENT_TIME;
+
+               di->i_size = cpu_to_le64(size);
+               di->i_ctime = di->i_mtime = 
+                               cpu_to_le64(inode->i_mtime.tv_sec);
+               di->i_ctime_nsec = di->i_mtime_nsec = 
+                               cpu_to_le32(inode->i_mtime.tv_nsec);
+
+               ret = ocfs2_journal_dirty(handle, di_bh);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out_commit;
+               }
+       }
+
+       BUG_ON(extending && (i_size_read(inode) != new_i_size));
+
+out_commit:
+       if (handle)
+               ocfs2_commit_trans(handle);
+out_unlock_data:
+       ocfs2_data_unlock(inode, 1);
+out_unlock_meta:
+       ocfs2_meta_unlock(inode, locklevel);
+out:
+       if (di_bh)
+               brelse(di_bh);
+
+       mlog_exit(ret);
+       return ret;
+}
+
+static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
+{
+       sector_t status;
+       u64 p_blkno = 0;
+       int err = 0;
+       struct inode *inode = mapping->host;
+
+       mlog_entry("(block = %llu)\n", (unsigned long long)block);
+
+       /* We don't need to lock journal system files, since they aren't
+        * accessed concurrently from multiple nodes.
+        */
+       if (!INODE_JOURNAL(inode)) {
+               err = ocfs2_meta_lock(inode, NULL, NULL, 0);
+               if (err) {
+                       if (err != -ENOENT)
+                               mlog_errno(err);
+                       goto bail;
+               }
+               down_read(&OCFS2_I(inode)->ip_alloc_sem);
+       }
+
+       err = ocfs2_extent_map_get_blocks(inode, block, 1, &p_blkno,
+                                         NULL);
+
+       if (!INODE_JOURNAL(inode)) {
+               up_read(&OCFS2_I(inode)->ip_alloc_sem);
+               ocfs2_meta_unlock(inode, 0);
+       }
+
+       if (err) {
+               mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
+                    (unsigned long long)block);
+               mlog_errno(err);
+               goto bail;
+       }
+
+
+bail:
+       status = err ? 0 : p_blkno;
+
+       mlog_exit((int)status);
+
+       return status;
+}
+
+/*
+ * TODO: Make this into a generic get_blocks function.
+ *
+ * From do_direct_io in direct-io.c:
+ *  "So what we do is to permit the ->get_blocks function to populate
+ *   bh.b_size with the size of IO which is permitted at this offset and
+ *   this i_blkbits."
+ *
+ * This function is called directly from get_more_blocks in direct-io.c.
+ *
+ * called like this: dio->get_blocks(dio->inode, fs_startblk,
+ *                                     fs_count, map_bh, dio->rw == WRITE);
+ */
+static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
+                                    unsigned long max_blocks,
+                                    struct buffer_head *bh_result, int create)
+{
+       int ret;
+       u64 vbo_max; /* file offset, max_blocks from iblock */
+       u64 p_blkno;
+       int contig_blocks;
+       unsigned char blocksize_bits;
+
+       if (!inode || !bh_result) {
+               mlog(ML_ERROR, "inode or bh_result is null\n");
+               return -EIO;
+       }
+
+       blocksize_bits = inode->i_sb->s_blocksize_bits;
+
+       /* This function won't even be called if the request isn't all
+        * nicely aligned and of the right size, so there's no need
+        * for us to check any of that. */
+
+       vbo_max = ((u64)iblock + max_blocks) << blocksize_bits;
+
+       spin_lock(&OCFS2_I(inode)->ip_lock);
+       if ((iblock + max_blocks) >
+           ocfs2_clusters_to_blocks(inode->i_sb,
+                                    OCFS2_I(inode)->ip_clusters)) {
+               spin_unlock(&OCFS2_I(inode)->ip_lock);
+               ret = -EIO;
+               goto bail;
+       }
+       spin_unlock(&OCFS2_I(inode)->ip_lock);
+
+       /* This figures out the size of the next contiguous block, and
+        * our logical offset */
+       ret = ocfs2_extent_map_get_blocks(inode, iblock, 1, &p_blkno,
+                                         &contig_blocks);
+       if (ret) {
+               mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
+                    (unsigned long long)iblock);
+               ret = -EIO;
+               goto bail;
+       }
+
+       map_bh(bh_result, inode->i_sb, p_blkno);
+
+       /* make sure we don't map more than max_blocks blocks here as
+          that's all the kernel will handle at this point. */
+       if (max_blocks < contig_blocks)
+               contig_blocks = max_blocks;
+       bh_result->b_size = contig_blocks << blocksize_bits;
+bail:
+       return ret;
+}
+
+/* 
+ * ocfs2_dio_end_io is called by the dio core when a dio is finished.  We're
+ * particularly interested in the aio/dio case.  Like the core uses
+ * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
+ * truncation on another.
+ */
+static void ocfs2_dio_end_io(struct kiocb *iocb,
+                            loff_t offset,
+                            ssize_t bytes,
+                            void *private)
+{
+       struct inode *inode = iocb->ki_filp->f_dentry->d_inode;
+
+       /* this io's submitter should not have unlocked this before we could */
+       BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
+       ocfs2_iocb_clear_rw_locked(iocb);
+       up_read(&inode->i_alloc_sem);
+       ocfs2_rw_unlock(inode, 0);
+}
+
+static ssize_t ocfs2_direct_IO(int rw,
+                              struct kiocb *iocb,
+                              const struct iovec *iov,
+                              loff_t offset,
+                              unsigned long nr_segs)
+{
+       struct file *file = iocb->ki_filp;
+       struct inode *inode = file->f_dentry->d_inode->i_mapping->host;
+       int ret;
+
+       mlog_entry_void();
+       ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
+                                           inode->i_sb->s_bdev, iov, offset,
+                                           nr_segs, 
+                                           ocfs2_direct_IO_get_blocks,
+                                           ocfs2_dio_end_io);
+       mlog_exit(ret);
+       return ret;
+}
+
+struct address_space_operations ocfs2_aops = {
+       .readpage       = ocfs2_readpage,
+       .writepage      = ocfs2_writepage,
+       .prepare_write  = ocfs2_prepare_write,
+       .commit_write   = ocfs2_commit_write,
+       .bmap           = ocfs2_bmap,
+       .sync_page      = block_sync_page,
+       .direct_IO      = ocfs2_direct_IO
+};
diff --git a/fs/ocfs2/aops.h b/fs/ocfs2/aops.h
new file mode 100644 (file)
index 0000000..d40456d
--- /dev/null
@@ -0,0 +1,41 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * Copyright (C) 2002, 2004, 2005 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef OCFS2_AOPS_H
+#define OCFS2_AOPS_H
+
+int ocfs2_prepare_write(struct file *file, struct page *page,
+                       unsigned from, unsigned to);
+
+struct ocfs2_journal_handle *ocfs2_start_walk_page_trans(struct inode *inode,
+                                                        struct page *page,
+                                                        unsigned from,
+                                                        unsigned to);
+
+/* all ocfs2_dio_end_io()'s fault */
+#define ocfs2_iocb_is_rw_locked(iocb) \
+       test_bit(0, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_set_rw_locked(iocb) \
+       set_bit(0, (unsigned long *)&iocb->private)
+#define ocfs2_iocb_clear_rw_locked(iocb) \
+       clear_bit(0, (unsigned long *)&iocb->private)
+
+#endif /* OCFS2_FILE_H */
diff --git a/fs/ocfs2/buffer_head_io.c b/fs/ocfs2/buffer_head_io.c
new file mode 100644 (file)
index 0000000..d424041
--- /dev/null
@@ -0,0 +1,232 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * io.c
+ *
+ * Buffer cache handling
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/highmem.h>
+
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "alloc.h"
+#include "inode.h"
+#include "journal.h"
+#include "uptodate.h"
+
+#include "buffer_head_io.h"
+
+int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
+                     struct inode *inode)
+{
+       int ret = 0;
+
+       mlog_entry("(bh->b_blocknr = %llu, inode=%p)\n",
+                  (unsigned long long)bh->b_blocknr, inode);
+
+       BUG_ON(bh->b_blocknr < OCFS2_SUPER_BLOCK_BLKNO);
+       BUG_ON(buffer_jbd(bh));
+
+       /* No need to check for a soft readonly file system here. non
+        * journalled writes are only ever done on system files which
+        * can get modified during recovery even if read-only. */
+       if (ocfs2_is_hard_readonly(osb)) {
+               ret = -EROFS;
+               goto out;
+       }
+
+       down(&OCFS2_I(inode)->ip_io_sem);
+
+       lock_buffer(bh);
+       set_buffer_uptodate(bh);
+
+       /* remove from dirty list before I/O. */
+       clear_buffer_dirty(bh);
+
+       get_bh(bh); /* for end_buffer_write_sync() */                   
+       bh->b_end_io = end_buffer_write_sync;
+       submit_bh(WRITE, bh);
+
+       wait_on_buffer(bh);
+
+       if (buffer_uptodate(bh)) {
+               ocfs2_set_buffer_uptodate(inode, bh);
+       } else {
+               /* We don't need to remove the clustered uptodate
+                * information for this bh as it's not marked locally
+                * uptodate. */
+               ret = -EIO;
+               brelse(bh);
+       }
+
+       up(&OCFS2_I(inode)->ip_io_sem);
+out:
+       mlog_exit(ret);
+       return ret;
+}
+
+int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
+                     struct buffer_head *bhs[], int flags,
+                     struct inode *inode)
+{
+       int status = 0;
+       struct super_block *sb;
+       int i, ignore_cache = 0;
+       struct buffer_head *bh;
+
+       mlog_entry("(block=(%"MLFu64"), nr=(%d), flags=%d, inode=%p)\n",
+                  block, nr, flags, inode);
+
+       if (osb == NULL || osb->sb == NULL || bhs == NULL) {
+               status = -EINVAL;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       if (nr < 0) {
+               mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
+               status = -EINVAL;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       if (nr == 0) {
+               mlog(ML_BH_IO, "No buffers will be read!\n");
+               status = 0;
+               goto bail;
+       }
+
+       sb = osb->sb;
+
+       if (flags & OCFS2_BH_CACHED && !inode)
+               flags &= ~OCFS2_BH_CACHED;
+
+       if (inode)
+               down(&OCFS2_I(inode)->ip_io_sem);
+       for (i = 0 ; i < nr ; i++) {
+               if (bhs[i] == NULL) {
+                       bhs[i] = sb_getblk(sb, block++);
+                       if (bhs[i] == NULL) {
+                               if (inode)
+                                       up(&OCFS2_I(inode)->ip_io_sem);
+                               status = -EIO;
+                               mlog_errno(status);
+                               goto bail;
+                       }
+               }
+               bh = bhs[i];
+               ignore_cache = 0;
+
+               if (flags & OCFS2_BH_CACHED &&
+                   !ocfs2_buffer_uptodate(inode, bh)) {
+                       mlog(ML_UPTODATE,
+                            "bh (%llu), inode %"MLFu64" not uptodate\n",
+                            (unsigned long long)bh->b_blocknr,
+                            OCFS2_I(inode)->ip_blkno);
+                       ignore_cache = 1;
+               }
+
+               /* XXX: Can we ever get this and *not* have the cached
+                * flag set? */
+               if (buffer_jbd(bh)) {
+                       if (!(flags & OCFS2_BH_CACHED) || ignore_cache)
+                               mlog(ML_BH_IO, "trying to sync read a jbd "
+                                              "managed bh (blocknr = %llu)\n",
+                                    (unsigned long long)bh->b_blocknr);
+                       continue;
+               }
+
+               if (!(flags & OCFS2_BH_CACHED) || ignore_cache) {
+                       if (buffer_dirty(bh)) {
+                               /* This should probably be a BUG, or
+                                * at least return an error. */
+                               mlog(ML_BH_IO, "asking me to sync read a dirty "
+                                              "buffer! (blocknr = %llu)\n",
+                                    (unsigned long long)bh->b_blocknr);
+                               continue;
+                       }
+
+                       lock_buffer(bh);
+                       if (buffer_jbd(bh)) {
+#ifdef CATCH_BH_JBD_RACES
+                               mlog(ML_ERROR, "block %llu had the JBD bit set "
+                                              "while I was in lock_buffer!",
+                                    (unsigned long long)bh->b_blocknr);
+                               BUG();
+#else
+                               unlock_buffer(bh);
+                               continue;
+#endif
+                       }
+                       clear_buffer_uptodate(bh);
+                       get_bh(bh); /* for end_buffer_read_sync() */
+                       bh->b_end_io = end_buffer_read_sync;
+                       if (flags & OCFS2_BH_READAHEAD)
+                               submit_bh(READA, bh);
+                       else
+                               submit_bh(READ, bh);
+                       continue;
+               }
+       }
+
+       status = 0;
+
+       for (i = (nr - 1); i >= 0; i--) {
+               bh = bhs[i];
+
+               /* We know this can't have changed as we hold the
+                * inode sem. Avoid doing any work on the bh if the
+                * journal has it. */
+               if (!buffer_jbd(bh))
+                       wait_on_buffer(bh);
+
+               if (!buffer_uptodate(bh)) {
+                       /* Status won't be cleared from here on out,
+                        * so we can safely record this and loop back
+                        * to cleanup the other buffers. Don't need to
+                        * remove the clustered uptodate information
+                        * for this bh as it's not marked locally
+                        * uptodate. */
+                       status = -EIO;
+                       brelse(bh);
+                       bhs[i] = NULL;
+                       continue;
+               }
+
+               if (inode)
+                       ocfs2_set_buffer_uptodate(inode, bh);
+       }
+       if (inode)
+               up(&OCFS2_I(inode)->ip_io_sem);
+
+       mlog(ML_BH_IO, "block=(%"MLFu64"), nr=(%d), cached=%s\n", block, nr,
+            (!(flags & OCFS2_BH_CACHED) || ignore_cache) ? "no" : "yes");
+
+bail:
+
+       mlog_exit(status);
+       return status;
+}
diff --git a/fs/ocfs2/buffer_head_io.h b/fs/ocfs2/buffer_head_io.h
new file mode 100644 (file)
index 0000000..6ecb909
--- /dev/null
@@ -0,0 +1,73 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * ocfs2_buffer_head.h
+ *
+ * Buffer cache handling functions defined
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef OCFS2_BUFFER_HEAD_IO_H
+#define OCFS2_BUFFER_HEAD_IO_H
+
+#include <linux/buffer_head.h>
+
+void ocfs2_end_buffer_io_sync(struct buffer_head *bh,
+                            int uptodate);
+
+static inline int ocfs2_read_block(struct ocfs2_super          *osb,
+                                  u64                  off,
+                                  struct buffer_head **bh,
+                                  int                  flags,
+                                  struct inode        *inode);
+
+int ocfs2_write_block(struct ocfs2_super          *osb,
+                     struct buffer_head  *bh,
+                     struct inode        *inode);
+int ocfs2_read_blocks(struct ocfs2_super          *osb,
+                     u64                  block,
+                     int                  nr,
+                     struct buffer_head  *bhs[],
+                     int                  flags,
+                     struct inode        *inode);
+
+
+#define OCFS2_BH_CACHED            1
+#define OCFS2_BH_READAHEAD         8   /* use this to pass READA down to submit_bh */
+
+static inline int ocfs2_read_block(struct ocfs2_super * osb, u64 off,
+                                  struct buffer_head **bh, int flags,
+                                  struct inode *inode)
+{
+       int status = 0;
+
+       if (bh == NULL) {
+               printk("ocfs2: bh == NULL\n");
+               status = -EINVAL;
+               goto bail;
+       }
+
+       status = ocfs2_read_blocks(osb, off, 1, bh,
+                                  flags, inode);
+
+bail:
+       return status;
+}
+
+#endif /* OCFS2_BUFFER_HEAD_IO_H */
diff --git a/fs/ocfs2/dcache.c b/fs/ocfs2/dcache.c
new file mode 100644 (file)
index 0000000..bd85182
--- /dev/null
@@ -0,0 +1,91 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dcache.c
+ *
+ * dentry cache handling code
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/namei.h>
+
+#define MLOG_MASK_PREFIX ML_DCACHE
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "alloc.h"
+#include "dcache.h"
+#include "file.h"
+#include "inode.h"
+
+static int ocfs2_dentry_revalidate(struct dentry *dentry,
+                                  struct nameidata *nd)
+{
+       struct inode *inode = dentry->d_inode;
+       int ret = 0;    /* if all else fails, just return false */
+       struct ocfs2_super *osb;
+
+       mlog_entry("(0x%p, '%.*s')\n", dentry,
+                  dentry->d_name.len, dentry->d_name.name);
+
+       /* Never trust a negative dentry - force a new lookup. */
+       if (inode == NULL) {
+               mlog(0, "negative dentry: %.*s\n", dentry->d_name.len,
+                    dentry->d_name.name);
+               goto bail;
+       }
+
+       osb = OCFS2_SB(inode->i_sb);
+
+       BUG_ON(!osb);
+
+       if (inode != osb->root_inode) {
+               spin_lock(&OCFS2_I(inode)->ip_lock);
+               /* did we or someone else delete this inode? */
+               if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
+                       spin_unlock(&OCFS2_I(inode)->ip_lock);
+                       mlog(0, "inode (%"MLFu64") deleted, returning false\n",
+                            OCFS2_I(inode)->ip_blkno);
+                       goto bail;
+               }
+               spin_unlock(&OCFS2_I(inode)->ip_lock);
+
+               if (!inode->i_nlink) {
+                       mlog(0, "Inode %"MLFu64" orphaned, returning false "
+                            "dir = %d\n", OCFS2_I(inode)->ip_blkno,
+                            S_ISDIR(inode->i_mode));
+                       goto bail;
+               }
+       }
+
+       ret = 1;
+
+bail:
+       mlog_exit(ret);
+
+       return ret;
+}
+
+struct dentry_operations ocfs2_dentry_ops = {
+       .d_revalidate           = ocfs2_dentry_revalidate,
+};
diff --git a/fs/ocfs2/dcache.h b/fs/ocfs2/dcache.h
new file mode 100644 (file)
index 0000000..9007277
--- /dev/null
@@ -0,0 +1,31 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dcache.h
+ *
+ * Function prototypes
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef OCFS2_DCACHE_H
+#define OCFS2_DCACHE_H
+
+extern struct dentry_operations ocfs2_dentry_ops;
+
+#endif /* OCFS2_DCACHE_H */
diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c
new file mode 100644 (file)
index 0000000..856e20a
--- /dev/null
@@ -0,0 +1,618 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dir.c
+ *
+ * Creates, reads, walks and deletes directory-nodes
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ *  Portions of this code from linux/fs/ext3/dir.c
+ *
+ *  Copyright (C) 1992, 1993, 1994, 1995
+ *  Remy Card (card@masi.ibp.fr)
+ *  Laboratoire MASI - Institut Blaise pascal
+ *  Universite Pierre et Marie Curie (Paris VI)
+ *
+ *   from
+ *
+ *   linux/fs/minix/dir.c
+ *
+ *   Copyright (C) 1991, 1992 Linux Torvalds
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/highmem.h>
+
+#define MLOG_MASK_PREFIX ML_NAMEI
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "alloc.h"
+#include "dir.h"
+#include "dlmglue.h"
+#include "extent_map.h"
+#include "file.h"
+#include "inode.h"
+#include "journal.h"
+#include "namei.h"
+#include "suballoc.h"
+#include "uptodate.h"
+
+#include "buffer_head_io.h"
+
+static unsigned char ocfs2_filetype_table[] = {
+       DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
+};
+
+static int ocfs2_extend_dir(struct ocfs2_super *osb,
+                           struct inode *dir,
+                           struct buffer_head *parent_fe_bh,
+                           struct buffer_head **new_de_bh);
+/*
+ * ocfs2_readdir()
+ *
+ */
+int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
+{
+       int error = 0;
+       unsigned long offset, blk;
+       int i, num, stored;
+       struct buffer_head * bh, * tmp;
+       struct ocfs2_dir_entry * de;
+       int err;
+       struct inode *inode = filp->f_dentry->d_inode;
+       struct super_block * sb = inode->i_sb;
+       int have_disk_lock = 0;
+
+       mlog_entry("dirino=%"MLFu64"\n", OCFS2_I(inode)->ip_blkno);
+
+       stored = 0;
+       bh = NULL;
+
+       error = ocfs2_meta_lock(inode, NULL, NULL, 0);
+       if (error < 0) {
+               if (error != -ENOENT)
+                       mlog_errno(error);
+               /* we haven't got any yet, so propagate the error. */
+               stored = error;
+               goto bail;
+       }
+       have_disk_lock = 1;
+
+       offset = filp->f_pos & (sb->s_blocksize - 1);
+
+       while (!error && !stored && filp->f_pos < i_size_read(inode)) {
+               blk = (filp->f_pos) >> sb->s_blocksize_bits;
+               bh = ocfs2_bread(inode, blk, &err, 0);
+               if (!bh) {
+                       mlog(ML_ERROR, "directory #%"MLFu64" contains a hole "
+                                      "at offset %lld\n",
+                            OCFS2_I(inode)->ip_blkno,
+                            filp->f_pos);
+                       filp->f_pos += sb->s_blocksize - offset;
+                       continue;
+               }
+
+               /*
+                * Do the readahead (8k)
+                */
+               if (!offset) {
+                       for (i = 16 >> (sb->s_blocksize_bits - 9), num = 0;
+                            i > 0; i--) {
+                               tmp = ocfs2_bread(inode, ++blk, &err, 1);
+                               if (tmp)
+                                       brelse(tmp);
+                       }
+               }
+
+revalidate:
+               /* If the dir block has changed since the last call to
+                * readdir(2), then we might be pointing to an invalid
+                * dirent right now.  Scan from the start of the block
+                * to make sure. */
+               if (filp->f_version != inode->i_version) {
+                       for (i = 0; i < sb->s_blocksize && i < offset; ) {
+                               de = (struct ocfs2_dir_entry *) (bh->b_data + i);
+                               /* It's too expensive to do a full
+                                * dirent test each time round this
+                                * loop, but we do have to test at
+                                * least that it is non-zero.  A
+                                * failure will be detected in the
+                                * dirent test below. */
+                               if (le16_to_cpu(de->rec_len) <
+                                   OCFS2_DIR_REC_LEN(1))
+                                       break;
+                               i += le16_to_cpu(de->rec_len);
+                       }
+                       offset = i;
+                       filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
+                               | offset;
+                       filp->f_version = inode->i_version;
+               }
+
+               while (!error && filp->f_pos < i_size_read(inode)
+                      && offset < sb->s_blocksize) {
+                       de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
+                       if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
+                               /* On error, skip the f_pos to the
+                                  next block. */
+                               filp->f_pos = (filp->f_pos |
+                                              (sb->s_blocksize - 1)) + 1;
+                               brelse(bh);
+                               goto bail;
+                       }
+                       offset += le16_to_cpu(de->rec_len);
+                       if (le64_to_cpu(de->inode)) {
+                               /* We might block in the next section
+                                * if the data destination is
+                                * currently swapped out.  So, use a
+                                * version stamp to detect whether or
+                                * not the directory has been modified
+                                * during the copy operation.
+                                */
+                               unsigned long version = filp->f_version;
+                               unsigned char d_type = DT_UNKNOWN;
+
+                               if (de->file_type < OCFS2_FT_MAX)
+                                       d_type = ocfs2_filetype_table[de->file_type];
+                               error = filldir(dirent, de->name,
+                                               de->name_len,
+                                               filp->f_pos,
+                                               ino_from_blkno(sb, le64_to_cpu(de->inode)),
+                                               d_type);
+                               if (error)
+                                       break;
+                               if (version != filp->f_version)
+                                       goto revalidate;
+                               stored ++;
+                       }
+                       filp->f_pos += le16_to_cpu(de->rec_len);
+               }
+               offset = 0;
+               brelse(bh);
+       }
+
+       stored = 0;
+bail:
+       if (have_disk_lock)
+               ocfs2_meta_unlock(inode, 0);
+
+       mlog_exit(stored);
+
+       return stored;
+}
+
+/*
+ * NOTE: this should always be called with parent dir i_sem taken.
+ */
+int ocfs2_find_files_on_disk(const char *name,
+                            int namelen,
+                            u64 *blkno,
+                            struct inode *inode,
+                            struct buffer_head **dirent_bh,
+                            struct ocfs2_dir_entry **dirent)
+{
+       int status = -ENOENT;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+
+       mlog_entry("(osb=%p, parent=%"MLFu64", name='%.*s', blkno=%p, "
+                  "inode=%p)\n",
+                  osb, OCFS2_I(inode)->ip_blkno, namelen, name, blkno, inode);
+
+       *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
+       if (!*dirent_bh || !*dirent) {
+               status = -ENOENT;
+               goto leave;
+       }
+
+       *blkno = le64_to_cpu((*dirent)->inode);
+
+       status = 0;
+leave:
+       if (status < 0) {
+               *dirent = NULL;
+               if (*dirent_bh) {
+                       brelse(*dirent_bh);
+                       *dirent_bh = NULL;
+               }
+       }
+
+       mlog_exit(status);
+       return status;
+}
+
+/* Check for a name within a directory.
+ *
+ * Return 0 if the name does not exist
+ * Return -EEXIST if the directory contains the name
+ *
+ * Callers should have i_sem + a cluster lock on dir
+ */
+int ocfs2_check_dir_for_entry(struct inode *dir,
+                             const char *name,
+                             int namelen)
+{
+       int ret;
+       struct buffer_head *dirent_bh = NULL;
+       struct ocfs2_dir_entry *dirent = NULL;
+
+       mlog_entry("dir %"MLFu64", name '%.*s'\n", OCFS2_I(dir)->ip_blkno,
+                  namelen, name);
+
+       ret = -EEXIST;
+       dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
+       if (dirent_bh)
+               goto bail;
+
+       ret = 0;
+bail:
+       if (dirent_bh)
+               brelse(dirent_bh);
+
+       mlog_exit(ret);
+       return ret;
+}
+
+/*
+ * routine to check that the specified directory is empty (for rmdir)
+ */
+int ocfs2_empty_dir(struct inode *inode)
+{
+       unsigned long offset;
+       struct buffer_head * bh;
+       struct ocfs2_dir_entry * de, * de1;
+       struct super_block * sb;
+       int err;
+
+       sb = inode->i_sb;
+       if ((i_size_read(inode) <
+            (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
+           !(bh = ocfs2_bread(inode, 0, &err, 0))) {
+               mlog(ML_ERROR, "bad directory (dir #%"MLFu64") - "
+                              "no data block\n",
+                    OCFS2_I(inode)->ip_blkno);
+               return 1;
+       }
+
+       de = (struct ocfs2_dir_entry *) bh->b_data;
+       de1 = (struct ocfs2_dir_entry *)
+                       ((char *)de + le16_to_cpu(de->rec_len));
+       if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
+                       !le64_to_cpu(de1->inode) ||
+                       strcmp(".", de->name) ||
+                       strcmp("..", de1->name)) {
+               mlog(ML_ERROR, "bad directory (dir #%"MLFu64") - "
+                              "no `.' or `..'\n",
+                    OCFS2_I(inode)->ip_blkno);
+               brelse(bh);
+               return 1;
+       }
+       offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
+       de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
+       while (offset < i_size_read(inode) ) {
+               if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
+                       brelse(bh);
+                       bh = ocfs2_bread(inode,
+                                        offset >> sb->s_blocksize_bits, &err, 0);
+                       if (!bh) {
+                               mlog(ML_ERROR, "directory #%"MLFu64" contains "
+                                              "a hole at offset %lu\n",
+                                    OCFS2_I(inode)->ip_blkno, offset);
+                               offset += sb->s_blocksize;
+                               continue;
+                       }
+                       de = (struct ocfs2_dir_entry *) bh->b_data;
+               }
+               if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
+                       brelse(bh);
+                       return 1;
+               }
+               if (le64_to_cpu(de->inode)) {
+                       brelse(bh);
+                       return 0;
+               }
+               offset += le16_to_cpu(de->rec_len);
+               de = (struct ocfs2_dir_entry *)
+                       ((char *)de + le16_to_cpu(de->rec_len));
+       }
+       brelse(bh);
+       return 1;
+}
+
+/* returns a bh of the 1st new block in the allocation. */
+int ocfs2_do_extend_dir(struct super_block *sb,
+                       struct ocfs2_journal_handle *handle,
+                       struct inode *dir,
+                       struct buffer_head *parent_fe_bh,
+                       struct ocfs2_alloc_context *data_ac,
+                       struct ocfs2_alloc_context *meta_ac,
+                       struct buffer_head **new_bh)
+{
+       int status;
+       int extend;
+       u64 p_blkno;
+
+       spin_lock(&OCFS2_I(dir)->ip_lock);
+       extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
+       spin_unlock(&OCFS2_I(dir)->ip_lock);
+
+       if (extend) {
+               status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, 1,
+                                                   parent_fe_bh, handle,
+                                                   data_ac, meta_ac, NULL);
+               BUG_ON(status == -EAGAIN);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
+       status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >>
+                                                  (sb->s_blocksize_bits - 9)),
+                                            1, &p_blkno, NULL);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       *new_bh = sb_getblk(sb, p_blkno);
+       if (!*new_bh) {
+               status = -EIO;
+               mlog_errno(status);
+               goto bail;
+       }
+       status = 0;
+bail:
+       mlog_exit(status);
+       return status;
+}
+
+/* assumes you already have a cluster lock on the directory. */
+static int ocfs2_extend_dir(struct ocfs2_super *osb,
+                           struct inode *dir,
+                           struct buffer_head *parent_fe_bh,
+                           struct buffer_head **new_de_bh)
+{
+       int status = 0;
+       int credits, num_free_extents;
+       loff_t dir_i_size;
+       struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
+       struct ocfs2_alloc_context *data_ac = NULL;
+       struct ocfs2_alloc_context *meta_ac = NULL;
+       struct ocfs2_journal_handle *handle = NULL;
+       struct buffer_head *new_bh = NULL;
+       struct ocfs2_dir_entry * de;
+       struct super_block *sb = osb->sb;
+
+       mlog_entry_void();
+
+       dir_i_size = i_size_read(dir);
+       mlog(0, "extending dir %"MLFu64" (i_size = %lld)\n",
+            OCFS2_I(dir)->ip_blkno, dir_i_size);
+
+       handle = ocfs2_alloc_handle(osb);
+       if (handle == NULL) {
+               status = -ENOMEM;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* dir->i_size is always block aligned. */
+       spin_lock(&OCFS2_I(dir)->ip_lock);
+       if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
+               spin_unlock(&OCFS2_I(dir)->ip_lock);
+               num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
+               if (num_free_extents < 0) {
+                       status = num_free_extents;
+                       mlog_errno(status);
+                       goto bail;
+               }
+
+               if (!num_free_extents) {
+                       status = ocfs2_reserve_new_metadata(osb, handle,
+                                                           fe, &meta_ac);
+                       if (status < 0) {
+                               if (status != -ENOSPC)
+                                       mlog_errno(status);
+                               goto bail;
+                       }
+               }
+
+               status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
+               if (status < 0) {
+                       if (status != -ENOSPC)
+                               mlog_errno(status);
+                       goto bail;
+               }
+
+               credits = ocfs2_calc_extend_credits(sb, fe, 1);
+       } else {
+               spin_unlock(&OCFS2_I(dir)->ip_lock);
+               credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
+       }
+
+       handle = ocfs2_start_trans(osb, handle, credits);
+       if (IS_ERR(handle)) {
+               status = PTR_ERR(handle);
+               handle = NULL;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
+                                    data_ac, meta_ac, &new_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       ocfs2_set_new_buffer_uptodate(dir, new_bh);
+
+       status = ocfs2_journal_access(handle, dir, new_bh,
+                                     OCFS2_JOURNAL_ACCESS_CREATE);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       memset(new_bh->b_data, 0, sb->s_blocksize);
+       de = (struct ocfs2_dir_entry *) new_bh->b_data;
+       de->inode = 0;
+       de->rec_len = cpu_to_le16(sb->s_blocksize);
+       status = ocfs2_journal_dirty(handle, new_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       dir_i_size += dir->i_sb->s_blocksize;
+       i_size_write(dir, dir_i_size);
+       dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size);
+       status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       *new_de_bh = new_bh;
+       get_bh(*new_de_bh);
+bail:
+       if (handle)
+               ocfs2_commit_trans(handle);
+
+       if (data_ac)
+               ocfs2_free_alloc_context(data_ac);
+       if (meta_ac)
+               ocfs2_free_alloc_context(meta_ac);
+
+       if (new_bh)
+               brelse(new_bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * Search the dir for a good spot, extending it if necessary. The
+ * block containing an appropriate record is returned in ret_de_bh.
+ */
+int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
+                                struct inode *dir,
+                                struct buffer_head *parent_fe_bh,
+                                const char *name,
+                                int namelen,
+                                struct buffer_head **ret_de_bh)
+{
+       unsigned long offset;
+       struct buffer_head * bh = NULL;
+       unsigned short rec_len;
+       struct ocfs2_dinode *fe;
+       struct ocfs2_dir_entry *de;
+       struct super_block *sb;
+       int status;
+
+       mlog_entry_void();
+
+       mlog(0, "getting ready to insert namelen %d into dir %"MLFu64"\n",
+            namelen, OCFS2_I(dir)->ip_blkno);
+
+       BUG_ON(!S_ISDIR(dir->i_mode));
+       fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
+       BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
+
+       sb = dir->i_sb;
+
+       if (!namelen) {
+               status = -EINVAL;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       bh = ocfs2_bread(dir, 0, &status, 0);
+       if (!bh) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       rec_len = OCFS2_DIR_REC_LEN(namelen);
+       offset = 0;
+       de = (struct ocfs2_dir_entry *) bh->b_data;
+       while (1) {
+               if ((char *)de >= sb->s_blocksize + bh->b_data) {
+                       brelse(bh);
+                       bh = NULL;
+
+                       if (i_size_read(dir) <= offset) {
+                               status = ocfs2_extend_dir(osb,
+                                                         dir,
+                                                         parent_fe_bh,
+                                                         &bh);
+                               if (status < 0) {
+                                       mlog_errno(status);
+                                       goto bail;
+                               }
+                               BUG_ON(!bh);
+                               *ret_de_bh = bh;
+                               get_bh(*ret_de_bh);
+                               goto bail;
+                       }
+                       bh = ocfs2_bread(dir,
+                                        offset >> sb->s_blocksize_bits,
+                                        &status,
+                                        0);
+                       if (!bh) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+                       /* move to next block */
+                       de = (struct ocfs2_dir_entry *) bh->b_data;
+               }
+               if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                       status = -ENOENT;
+                       goto bail;
+               }
+               if (ocfs2_match(namelen, name, de)) {
+                       status = -EEXIST;
+                       goto bail;
+               }
+               if (((le64_to_cpu(de->inode) == 0) &&
+                    (le16_to_cpu(de->rec_len) >= rec_len)) ||
+                   (le16_to_cpu(de->rec_len) >=
+                    (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
+                       /* Ok, we found a spot. Return this bh and let
+                        * the caller actually fill it in. */
+                       *ret_de_bh = bh;
+                       get_bh(*ret_de_bh);
+                       status = 0;
+                       goto bail;
+               }
+               offset += le16_to_cpu(de->rec_len);
+               de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
+       }
+
+       status = 0;
+bail:
+       if (bh)
+               brelse(bh);
+
+       mlog_exit(status);
+       return status;
+}
diff --git a/fs/ocfs2/dir.h b/fs/ocfs2/dir.h
new file mode 100644 (file)
index 0000000..5f614ec
--- /dev/null
@@ -0,0 +1,54 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dir.h
+ *
+ * Function prototypes
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef OCFS2_DIR_H
+#define OCFS2_DIR_H
+
+int ocfs2_check_dir_for_entry(struct inode *dir,
+                             const char *name,
+                             int namelen);
+int ocfs2_empty_dir(struct inode *inode);  /* FIXME: to namei.c */
+int ocfs2_find_files_on_disk(const char *name,
+                            int namelen,
+                            u64 *blkno,
+                            struct inode *inode,
+                            struct buffer_head **dirent_bh,
+                            struct ocfs2_dir_entry **dirent);
+int ocfs2_readdir(struct file *filp, void *dirent, filldir_t filldir);
+int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
+                                struct inode *dir,
+                                struct buffer_head *parent_fe_bh,
+                                const char *name,
+                                int namelen,
+                                struct buffer_head **ret_de_bh);
+struct ocfs2_alloc_context;
+int ocfs2_do_extend_dir(struct super_block *sb,
+                       struct ocfs2_journal_handle *handle,
+                       struct inode *dir,
+                       struct buffer_head *parent_fe_bh,
+                       struct ocfs2_alloc_context *data_ac,
+                       struct ocfs2_alloc_context *meta_ac,
+                       struct buffer_head **new_bh);
+#endif /* OCFS2_DIR_H */
diff --git a/fs/ocfs2/dlmglue.c b/fs/ocfs2/dlmglue.c
new file mode 100644 (file)
index 0000000..e971ec2
--- /dev/null
@@ -0,0 +1,2904 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dlmglue.c
+ *
+ * Code which implements an OCFS2 specific interface to our DLM.
+ *
+ * Copyright (C) 2003, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/highmem.h>
+#include <linux/mm.h>
+#include <linux/smp_lock.h>
+#include <linux/crc32.h>
+#include <linux/kthread.h>
+#include <linux/pagemap.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
+
+#include <cluster/heartbeat.h>
+#include <cluster/nodemanager.h>
+#include <cluster/tcp.h>
+
+#include <dlm/dlmapi.h>
+
+#define MLOG_MASK_PREFIX ML_DLM_GLUE
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "alloc.h"
+#include "dlmglue.h"
+#include "extent_map.h"
+#include "heartbeat.h"
+#include "inode.h"
+#include "journal.h"
+#include "slot_map.h"
+#include "super.h"
+#include "uptodate.h"
+#include "vote.h"
+
+#include "buffer_head_io.h"
+
+struct ocfs2_mask_waiter {
+       struct list_head        mw_item;
+       int                     mw_status;
+       struct completion       mw_complete;
+       unsigned long           mw_mask;
+       unsigned long           mw_goal;
+};
+
+static void ocfs2_inode_ast_func(void *opaque);
+static void ocfs2_inode_bast_func(void *opaque,
+                                 int level);
+static void ocfs2_super_ast_func(void *opaque);
+static void ocfs2_super_bast_func(void *opaque,
+                                 int level);
+static void ocfs2_rename_ast_func(void *opaque);
+static void ocfs2_rename_bast_func(void *opaque,
+                                  int level);
+
+/* so far, all locks have gotten along with the same unlock ast */
+static void ocfs2_unlock_ast_func(void *opaque,
+                                 enum dlm_status status);
+static int ocfs2_do_unblock_meta(struct inode *inode,
+                                int *requeue);
+static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
+                             int *requeue);
+static int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
+                             int *requeue);
+static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
+                             int *requeue);
+static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
+                                 int *requeue);
+typedef void (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
+static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
+                                     struct ocfs2_lock_res *lockres,
+                                     int *requeue,
+                                     ocfs2_convert_worker_t *worker);
+
+struct ocfs2_lock_res_ops {
+       void (*ast)(void *);
+       void (*bast)(void *, int);
+       void (*unlock_ast)(void *, enum dlm_status);
+       int  (*unblock)(struct ocfs2_lock_res *, int *);
+};
+
+static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
+       .ast            = ocfs2_inode_ast_func,
+       .bast           = ocfs2_inode_bast_func,
+       .unlock_ast     = ocfs2_unlock_ast_func,
+       .unblock        = ocfs2_unblock_inode_lock,
+};
+
+static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
+       .ast            = ocfs2_inode_ast_func,
+       .bast           = ocfs2_inode_bast_func,
+       .unlock_ast     = ocfs2_unlock_ast_func,
+       .unblock        = ocfs2_unblock_meta,
+};
+
+static void ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
+                                     int blocking);
+
+static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
+       .ast            = ocfs2_inode_ast_func,
+       .bast           = ocfs2_inode_bast_func,
+       .unlock_ast     = ocfs2_unlock_ast_func,
+       .unblock        = ocfs2_unblock_data,
+};
+
+static struct ocfs2_lock_res_ops ocfs2_super_lops = {
+       .ast            = ocfs2_super_ast_func,
+       .bast           = ocfs2_super_bast_func,
+       .unlock_ast     = ocfs2_unlock_ast_func,
+       .unblock        = ocfs2_unblock_osb_lock,
+};
+
+static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
+       .ast            = ocfs2_rename_ast_func,
+       .bast           = ocfs2_rename_bast_func,
+       .unlock_ast     = ocfs2_unlock_ast_func,
+       .unblock        = ocfs2_unblock_osb_lock,
+};
+
+static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
+{
+       return lockres->l_type == OCFS2_LOCK_TYPE_META ||
+               lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
+               lockres->l_type == OCFS2_LOCK_TYPE_RW;
+}
+
+static inline int ocfs2_is_super_lock(struct ocfs2_lock_res *lockres)
+{
+       return lockres->l_type == OCFS2_LOCK_TYPE_SUPER;
+}
+
+static inline int ocfs2_is_rename_lock(struct ocfs2_lock_res *lockres)
+{
+       return lockres->l_type == OCFS2_LOCK_TYPE_RENAME;
+}
+
+static inline struct ocfs2_super *ocfs2_lock_res_super(struct ocfs2_lock_res *lockres)
+{
+       BUG_ON(!ocfs2_is_super_lock(lockres)
+              && !ocfs2_is_rename_lock(lockres));
+
+       return (struct ocfs2_super *) lockres->l_priv;
+}
+
+static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
+{
+       BUG_ON(!ocfs2_is_inode_lock(lockres));
+
+       return (struct inode *) lockres->l_priv;
+}
+
+static int ocfs2_lock_create(struct ocfs2_super *osb,
+                            struct ocfs2_lock_res *lockres,
+                            int level,
+                            int dlm_flags);
+static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
+                                                    int wanted);
+static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
+                                struct ocfs2_lock_res *lockres,
+                                int level);
+static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
+static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
+static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
+static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
+static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
+                                       struct ocfs2_lock_res *lockres);
+static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
+                                               int convert);
+#define ocfs2_log_dlm_error(_func, _stat, _lockres) do {       \
+       mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on "  \
+               "resource %s: %s\n", dlm_errname(_stat), _func, \
+               _lockres->l_name, dlm_errmsg(_stat));           \
+} while (0)
+static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
+                                struct ocfs2_lock_res *lockres);
+static int ocfs2_meta_lock_update(struct inode *inode,
+                                 struct buffer_head **bh);
+static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
+static inline int ocfs2_highest_compat_lock_level(int level);
+static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
+                                                 struct ocfs2_lock_res *lockres,
+                                                 int new_level);
+
+static char *ocfs2_lock_type_strings[] = {
+       [OCFS2_LOCK_TYPE_META] = "Meta",
+       [OCFS2_LOCK_TYPE_DATA] = "Data",
+       [OCFS2_LOCK_TYPE_SUPER] = "Super",
+       [OCFS2_LOCK_TYPE_RENAME] = "Rename",
+       /* Need to differntiate from [R]ename.. serializing writes is the
+        * important job it does, anyway. */
+       [OCFS2_LOCK_TYPE_RW] = "Write/Read",
+};
+
+static char *ocfs2_lock_type_string(enum ocfs2_lock_type type)
+{
+       mlog_bug_on_msg(type >= OCFS2_NUM_LOCK_TYPES, "%d\n", type);
+       return ocfs2_lock_type_strings[type];
+}
+
+static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
+                                 u64 blkno,
+                                 u32 generation,
+                                 char *name)
+{
+       int len;
+
+       mlog_entry_void();
+
+       BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
+
+       len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016"MLFx64"%08x",
+                      ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD, blkno,
+                      generation);
+
+       BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
+
+       mlog(0, "built lock resource with name: %s\n", name);
+
+       mlog_exit_void();
+}
+
+static spinlock_t ocfs2_dlm_tracking_lock = SPIN_LOCK_UNLOCKED;
+
+static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
+                                      struct ocfs2_dlm_debug *dlm_debug)
+{
+       mlog(0, "Add tracking for lockres %s\n", res->l_name);
+
+       spin_lock(&ocfs2_dlm_tracking_lock);
+       list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
+       spin_unlock(&ocfs2_dlm_tracking_lock);
+}
+
+static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
+{
+       spin_lock(&ocfs2_dlm_tracking_lock);
+       if (!list_empty(&res->l_debug_list))
+               list_del_init(&res->l_debug_list);
+       spin_unlock(&ocfs2_dlm_tracking_lock);
+}
+
+static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
+                                      struct ocfs2_lock_res *res,
+                                      enum ocfs2_lock_type type,
+                                      u64 blkno,
+                                      u32 generation,
+                                      struct ocfs2_lock_res_ops *ops,
+                                      void *priv)
+{
+       ocfs2_build_lock_name(type, blkno, generation, res->l_name);
+
+       res->l_type          = type;
+       res->l_ops           = ops;
+       res->l_priv          = priv;
+
+       res->l_level         = LKM_IVMODE;
+       res->l_requested     = LKM_IVMODE;
+       res->l_blocking      = LKM_IVMODE;
+       res->l_action        = OCFS2_AST_INVALID;
+       res->l_unlock_action = OCFS2_UNLOCK_INVALID;
+
+       res->l_flags         = OCFS2_LOCK_INITIALIZED;
+
+       ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
+}
+
+void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
+{
+       /* This also clears out the lock status block */
+       memset(res, 0, sizeof(struct ocfs2_lock_res));
+       spin_lock_init(&res->l_lock);
+       init_waitqueue_head(&res->l_event);
+       INIT_LIST_HEAD(&res->l_blocked_list);
+       INIT_LIST_HEAD(&res->l_mask_waiters);
+}
+
+void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
+                              enum ocfs2_lock_type type,
+                              struct inode *inode)
+{
+       struct ocfs2_lock_res_ops *ops;
+
+       switch(type) {
+               case OCFS2_LOCK_TYPE_RW:
+                       ops = &ocfs2_inode_rw_lops;
+                       break;
+               case OCFS2_LOCK_TYPE_META:
+                       ops = &ocfs2_inode_meta_lops;
+                       break;
+               case OCFS2_LOCK_TYPE_DATA:
+                       ops = &ocfs2_inode_data_lops;
+                       break;
+               default:
+                       mlog_bug_on_msg(1, "type: %d\n", type);
+                       ops = NULL; /* thanks, gcc */
+                       break;
+       };
+
+       ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type,
+                                  OCFS2_I(inode)->ip_blkno,
+                                  inode->i_generation, ops, inode);
+}
+
+static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
+                                     struct ocfs2_super *osb)
+{
+       /* Superblock lockres doesn't come from a slab so we call init
+        * once on it manually.  */
+       ocfs2_lock_res_init_once(res);
+       ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
+                                  OCFS2_SUPER_BLOCK_BLKNO, 0,
+                                  &ocfs2_super_lops, osb);
+}
+
+static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
+                                      struct ocfs2_super *osb)
+{
+       /* Rename lockres doesn't come from a slab so we call init
+        * once on it manually.  */
+       ocfs2_lock_res_init_once(res);
+       ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME, 0, 0,
+                                  &ocfs2_rename_lops, osb);
+}
+
+void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
+{
+       mlog_entry_void();
+
+       if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
+               return;
+
+       ocfs2_remove_lockres_tracking(res);
+
+       mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
+                       "Lockres %s is on the blocked list\n",
+                       res->l_name);
+       mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
+                       "Lockres %s has mask waiters pending\n",
+                       res->l_name);
+       mlog_bug_on_msg(spin_is_locked(&res->l_lock),
+                       "Lockres %s is locked\n",
+                       res->l_name);
+       mlog_bug_on_msg(res->l_ro_holders,
+                       "Lockres %s has %u ro holders\n",
+                       res->l_name, res->l_ro_holders);
+       mlog_bug_on_msg(res->l_ex_holders,
+                       "Lockres %s has %u ex holders\n",
+                       res->l_name, res->l_ex_holders);
+
+       /* Need to clear out the lock status block for the dlm */
+       memset(&res->l_lksb, 0, sizeof(res->l_lksb));
+
+       res->l_flags = 0UL;
+       mlog_exit_void();
+}
+
+static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
+                                    int level)
+{
+       mlog_entry_void();
+
+       BUG_ON(!lockres);
+
+       switch(level) {
+       case LKM_EXMODE:
+               lockres->l_ex_holders++;
+               break;
+       case LKM_PRMODE:
+               lockres->l_ro_holders++;
+               break;
+       default:
+               BUG();
+       }
+
+       mlog_exit_void();
+}
+
+static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
+                                    int level)
+{
+       mlog_entry_void();
+
+       BUG_ON(!lockres);
+
+       switch(level) {
+       case LKM_EXMODE:
+               BUG_ON(!lockres->l_ex_holders);
+               lockres->l_ex_holders--;
+               break;
+       case LKM_PRMODE:
+               BUG_ON(!lockres->l_ro_holders);
+               lockres->l_ro_holders--;
+               break;
+       default:
+               BUG();
+       }
+       mlog_exit_void();
+}
+
+/* WARNING: This function lives in a world where the only three lock
+ * levels are EX, PR, and NL. It *will* have to be adjusted when more
+ * lock types are added. */
+static inline int ocfs2_highest_compat_lock_level(int level)
+{
+       int new_level = LKM_EXMODE;
+
+       if (level == LKM_EXMODE)
+               new_level = LKM_NLMODE;
+       else if (level == LKM_PRMODE)
+               new_level = LKM_PRMODE;
+       return new_level;
+}
+
+static void lockres_set_flags(struct ocfs2_lock_res *lockres,
+                             unsigned long newflags)
+{
+       struct list_head *pos, *tmp;
+       struct ocfs2_mask_waiter *mw;
+
+       assert_spin_locked(&lockres->l_lock);
+
+       lockres->l_flags = newflags;
+
+       list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
+               mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
+               if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
+                       continue;
+
+               list_del_init(&mw->mw_item);
+               mw->mw_status = 0;
+               complete(&mw->mw_complete);
+       }
+}
+static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
+{
+       lockres_set_flags(lockres, lockres->l_flags | or);
+}
+static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
+                               unsigned long clear)
+{
+       lockres_set_flags(lockres, lockres->l_flags & ~clear);
+}
+
+static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
+{
+       mlog_entry_void();
+
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
+       BUG_ON(lockres->l_blocking <= LKM_NLMODE);
+
+       lockres->l_level = lockres->l_requested;
+       if (lockres->l_level <=
+           ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
+               lockres->l_blocking = LKM_NLMODE;
+               lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
+       }
+       lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
+
+       mlog_exit_void();
+}
+
+static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
+{
+       mlog_entry_void();
+
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
+
+       /* Convert from RO to EX doesn't really need anything as our
+        * information is already up to data. Convert from NL to
+        * *anything* however should mark ourselves as needing an
+        * update */
+       if (lockres->l_level == LKM_NLMODE)
+               lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
+
+       lockres->l_level = lockres->l_requested;
+       lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
+
+       mlog_exit_void();
+}
+
+static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
+{
+       mlog_entry_void();
+
+       BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
+       BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
+
+       if (lockres->l_requested > LKM_NLMODE &&
+           !(lockres->l_flags & OCFS2_LOCK_LOCAL))
+               lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
+
+       lockres->l_level = lockres->l_requested;
+       lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
+       lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_inode_ast_func(void *opaque)
+{
+       struct ocfs2_lock_res *lockres = opaque;
+       struct inode *inode;
+       struct dlm_lockstatus *lksb;
+       unsigned long flags;
+
+       mlog_entry_void();
+
+       inode = ocfs2_lock_res_inode(lockres);
+
+       mlog(0, "AST fired for inode %"MLFu64", l_action = %u, type = %s\n",
+            OCFS2_I(inode)->ip_blkno, lockres->l_action,
+            ocfs2_lock_type_string(lockres->l_type));
+
+       BUG_ON(!ocfs2_is_inode_lock(lockres));
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+
+       lksb = &(lockres->l_lksb);
+       if (lksb->status != DLM_NORMAL) {
+               mlog(ML_ERROR, "ocfs2_inode_ast_func: lksb status value of %u "
+                    "on inode %"MLFu64"\n", lksb->status,
+                    OCFS2_I(inode)->ip_blkno);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               mlog_exit_void();
+               return;
+       }
+
+       switch(lockres->l_action) {
+       case OCFS2_AST_ATTACH:
+               ocfs2_generic_handle_attach_action(lockres);
+               lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
+               break;
+       case OCFS2_AST_CONVERT:
+               ocfs2_generic_handle_convert_action(lockres);
+               break;
+       case OCFS2_AST_DOWNCONVERT:
+               ocfs2_generic_handle_downconvert_action(lockres);
+               break;
+       default:
+               mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
+                    "lockres flags = 0x%lx, unlock action: %u\n",
+                    lockres->l_name, lockres->l_action, lockres->l_flags,
+                    lockres->l_unlock_action);
+
+               BUG();
+       }
+
+       /* data and rw locking ignores refresh flag for now. */
+       if (lockres->l_type != OCFS2_LOCK_TYPE_META)
+               lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
+
+       /* set it to something invalid so if we get called again we
+        * can catch it. */
+       lockres->l_action = OCFS2_AST_INVALID;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+       wake_up(&lockres->l_event);
+
+       mlog_exit_void();
+}
+
+static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
+                                    int level)
+{
+       int needs_downconvert = 0;
+       mlog_entry_void();
+
+       assert_spin_locked(&lockres->l_lock);
+
+       lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
+
+       if (level > lockres->l_blocking) {
+               /* only schedule a downconvert if we haven't already scheduled
+                * one that goes low enough to satisfy the level we're
+                * blocking.  this also catches the case where we get
+                * duplicate BASTs */
+               if (ocfs2_highest_compat_lock_level(level) <
+                   ocfs2_highest_compat_lock_level(lockres->l_blocking))
+                       needs_downconvert = 1;
+
+               lockres->l_blocking = level;
+       }
+
+       mlog_exit(needs_downconvert);
+       return needs_downconvert;
+}
+
+static void ocfs2_generic_bast_func(struct ocfs2_super *osb,
+                                   struct ocfs2_lock_res *lockres,
+                                   int level)
+{
+       int needs_downconvert;
+       unsigned long flags;
+
+       mlog_entry_void();
+
+       BUG_ON(level <= LKM_NLMODE);
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
+       if (needs_downconvert)
+               ocfs2_schedule_blocked_lock(osb, lockres);
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       ocfs2_kick_vote_thread(osb);
+
+       wake_up(&lockres->l_event);
+       mlog_exit_void();
+}
+
+static void ocfs2_inode_bast_func(void *opaque, int level)
+{
+       struct ocfs2_lock_res *lockres = opaque;
+       struct inode *inode;
+       struct ocfs2_super *osb;
+
+       mlog_entry_void();
+
+       BUG_ON(!ocfs2_is_inode_lock(lockres));
+
+       inode = ocfs2_lock_res_inode(lockres);
+       osb = OCFS2_SB(inode->i_sb);
+
+       mlog(0, "BAST fired for inode %"MLFu64", blocking = %d, level = %d "
+            "type = %s\n", OCFS2_I(inode)->ip_blkno, level,
+            lockres->l_level,
+            ocfs2_lock_type_string(lockres->l_type));
+
+       ocfs2_generic_bast_func(osb, lockres, level);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_generic_ast_func(struct ocfs2_lock_res *lockres,
+                                  int ignore_refresh)
+{
+       struct dlm_lockstatus *lksb = &lockres->l_lksb;
+       unsigned long flags;
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+
+       if (lksb->status != DLM_NORMAL) {
+               mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
+                    lockres->l_name, lksb->status);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               return;
+       }
+
+       switch(lockres->l_action) {
+       case OCFS2_AST_ATTACH:
+               ocfs2_generic_handle_attach_action(lockres);
+               break;
+       case OCFS2_AST_CONVERT:
+               ocfs2_generic_handle_convert_action(lockres);
+               break;
+       case OCFS2_AST_DOWNCONVERT:
+               ocfs2_generic_handle_downconvert_action(lockres);
+               break;
+       default:
+               BUG();
+       }
+
+       if (ignore_refresh)
+               lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
+
+       /* set it to something invalid so if we get called again we
+        * can catch it. */
+       lockres->l_action = OCFS2_AST_INVALID;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       wake_up(&lockres->l_event);
+}
+
+static void ocfs2_super_ast_func(void *opaque)
+{
+       struct ocfs2_lock_res *lockres = opaque;
+
+       mlog_entry_void();
+       mlog(0, "Superblock AST fired\n");
+
+       BUG_ON(!ocfs2_is_super_lock(lockres));
+       ocfs2_generic_ast_func(lockres, 0);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_super_bast_func(void *opaque,
+                                 int level)
+{
+       struct ocfs2_lock_res *lockres = opaque;
+       struct ocfs2_super *osb;
+
+       mlog_entry_void();
+       mlog(0, "Superblock BAST fired\n");
+
+       BUG_ON(!ocfs2_is_super_lock(lockres));
+               osb = ocfs2_lock_res_super(lockres);
+       ocfs2_generic_bast_func(osb, lockres, level);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_rename_ast_func(void *opaque)
+{
+       struct ocfs2_lock_res *lockres = opaque;
+
+       mlog_entry_void();
+
+       mlog(0, "Rename AST fired\n");
+
+       BUG_ON(!ocfs2_is_rename_lock(lockres));
+
+       ocfs2_generic_ast_func(lockres, 1);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_rename_bast_func(void *opaque,
+                                  int level)
+{
+       struct ocfs2_lock_res *lockres = opaque;
+       struct ocfs2_super *osb;
+
+       mlog_entry_void();
+
+       mlog(0, "Rename BAST fired\n");
+
+       BUG_ON(!ocfs2_is_rename_lock(lockres));
+
+       osb = ocfs2_lock_res_super(lockres);
+       ocfs2_generic_bast_func(osb, lockres, level);
+
+       mlog_exit_void();
+}
+
+static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
+                                               int convert)
+{
+       unsigned long flags;
+
+       mlog_entry_void();
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
+       if (convert)
+               lockres->l_action = OCFS2_AST_INVALID;
+       else
+               lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       wake_up(&lockres->l_event);
+       mlog_exit_void();
+}
+
+/* Note: If we detect another process working on the lock (i.e.,
+ * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
+ * to do the right thing in that case.
+ */
+static int ocfs2_lock_create(struct ocfs2_super *osb,
+                            struct ocfs2_lock_res *lockres,
+                            int level,
+                            int dlm_flags)
+{
+       int ret = 0;
+       enum dlm_status status;
+       unsigned long flags;
+
+       mlog_entry_void();
+
+       mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
+            dlm_flags);
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
+           (lockres->l_flags & OCFS2_LOCK_BUSY)) {
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               goto bail;
+       }
+
+       lockres->l_action = OCFS2_AST_ATTACH;
+       lockres->l_requested = level;
+       lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       status = dlmlock(osb->dlm,
+                        level,
+                        &lockres->l_lksb,
+                        dlm_flags,
+                        lockres->l_name,
+                        lockres->l_ops->ast,
+                        lockres,
+                        lockres->l_ops->bast);
+       if (status != DLM_NORMAL) {
+               ocfs2_log_dlm_error("dlmlock", status, lockres);
+               ret = -EINVAL;
+               ocfs2_recover_from_dlm_error(lockres, 1);
+       }
+
+       mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
+
+bail:
+       mlog_exit(ret);
+       return ret;
+}
+
+static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
+                                       int flag)
+{
+       unsigned long flags;
+       int ret;
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       ret = lockres->l_flags & flag;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       return ret;
+}
+
+static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
+
+{
+       wait_event(lockres->l_event,
+                  !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
+}
+
+static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
+
+{
+       wait_event(lockres->l_event,
+                  !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
+}
+
+/* predict what lock level we'll be dropping down to on behalf
+ * of another node, and return true if the currently wanted
+ * level will be compatible with it. */
+static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
+                                                    int wanted)
+{
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
+
+       return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
+}
+
+static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
+{
+       INIT_LIST_HEAD(&mw->mw_item);
+       init_completion(&mw->mw_complete);
+}
+
+static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
+{
+       wait_for_completion(&mw->mw_complete);
+       /* Re-arm the completion in case we want to wait on it again */
+       INIT_COMPLETION(mw->mw_complete);
+       return mw->mw_status;
+}
+
+static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
+                                   struct ocfs2_mask_waiter *mw,
+                                   unsigned long mask,
+                                   unsigned long goal)
+{
+       BUG_ON(!list_empty(&mw->mw_item));
+
+       assert_spin_locked(&lockres->l_lock);
+
+       list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
+       mw->mw_mask = mask;
+       mw->mw_goal = goal;
+}
+
+/* returns 0 if the mw that was removed was already satisfied, -EBUSY
+ * if the mask still hadn't reached its goal */
+static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
+                                     struct ocfs2_mask_waiter *mw)
+{
+       unsigned long flags;
+       int ret = 0;
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       if (!list_empty(&mw->mw_item)) {
+               if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
+                       ret = -EBUSY;
+
+               list_del_init(&mw->mw_item);
+               init_completion(&mw->mw_complete);
+       }
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       return ret;
+
+}
+
+static int ocfs2_cluster_lock(struct ocfs2_super *osb,
+                             struct ocfs2_lock_res *lockres,
+                             int level,
+                             int lkm_flags,
+                             int arg_flags)
+{
+       struct ocfs2_mask_waiter mw;
+       enum dlm_status status;
+       int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
+       int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
+       unsigned long flags;
+
+       mlog_entry_void();
+
+       ocfs2_init_mask_waiter(&mw);
+
+again:
+       wait = 0;
+
+       if (catch_signals && signal_pending(current)) {
+               ret = -ERESTARTSYS;
+               goto out;
+       }
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+
+       mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
+                       "Cluster lock called on freeing lockres %s! flags "
+                       "0x%lx\n", lockres->l_name, lockres->l_flags);
+
+       /* We only compare against the currently granted level
+        * here. If the lock is blocked waiting on a downconvert,
+        * we'll get caught below. */
+       if (lockres->l_flags & OCFS2_LOCK_BUSY &&
+           level > lockres->l_level) {
+               /* is someone sitting in dlm_lock? If so, wait on
+                * them. */
+               lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
+               wait = 1;
+               goto unlock;
+       }
+
+       if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
+               /* lock has not been created yet. */
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+               ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
+               if (ret < 0) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+               goto again;
+       }
+
+       if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
+           !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
+               /* is the lock is currently blocked on behalf of
+                * another node */
+               lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
+               wait = 1;
+               goto unlock;
+       }
+
+       if (level > lockres->l_level) {
+               if (lockres->l_action != OCFS2_AST_INVALID)
+                       mlog(ML_ERROR, "lockres %s has action %u pending\n",
+                            lockres->l_name, lockres->l_action);
+
+               lockres->l_action = OCFS2_AST_CONVERT;
+               lockres->l_requested = level;
+               lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+               BUG_ON(level == LKM_IVMODE);
+               BUG_ON(level == LKM_NLMODE);
+
+               mlog(0, "lock %s, convert from %d to level = %d\n",
+                    lockres->l_name, lockres->l_level, level);
+
+               /* call dlm_lock to upgrade lock now */
+               status = dlmlock(osb->dlm,
+                                level,
+                                &lockres->l_lksb,
+                                lkm_flags|LKM_CONVERT|LKM_VALBLK,
+                                lockres->l_name,
+                                lockres->l_ops->ast,
+                                lockres,
+                                lockres->l_ops->bast);
+               if (status != DLM_NORMAL) {
+                       if ((lkm_flags & LKM_NOQUEUE) &&
+                           (status == DLM_NOTQUEUED))
+                               ret = -EAGAIN;
+                       else {
+                               ocfs2_log_dlm_error("dlmlock", status,
+                                                   lockres);
+                               ret = -EINVAL;
+                       }
+                       ocfs2_recover_from_dlm_error(lockres, 1);
+                       goto out;
+               }
+
+               mlog(0, "lock %s, successfull return from dlmlock\n",
+                    lockres->l_name);
+
+               /* At this point we've gone inside the dlm and need to
+                * complete our work regardless. */
+               catch_signals = 0;
+
+               /* wait for busy to clear and carry on */
+               goto again;
+       }
+
+       /* Ok, if we get here then we're good to go. */
+       ocfs2_inc_holders(lockres, level);
+
+       ret = 0;
+unlock:
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+out:
+       /*
+        * This is helping work around a lock inversion between the page lock
+        * and dlm locks.  One path holds the page lock while calling aops
+        * which block acquiring dlm locks.  The voting thread holds dlm
+        * locks while acquiring page locks while down converting data locks.
+        * This block is helping an aop path notice the inversion and back
+        * off to unlock its page lock before trying the dlm lock again.
+        */
+       if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
+           mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
+               wait = 0;
+               if (lockres_remove_mask_waiter(lockres, &mw))
+                       ret = -EAGAIN;
+               else
+                       goto again;
+       }
+       if (wait) {
+               ret = ocfs2_wait_for_mask(&mw);
+               if (ret == 0)
+                       goto again;
+               mlog_errno(ret);
+       }
+
+       mlog_exit(ret);
+       return ret;
+}
+
+static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
+                                struct ocfs2_lock_res *lockres,
+                                int level)
+{
+       unsigned long flags;
+
+       mlog_entry_void();
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       ocfs2_dec_holders(lockres, level);
+       ocfs2_vote_on_unlock(osb, lockres);
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+       mlog_exit_void();
+}
+
+static int ocfs2_create_new_inode_lock(struct inode *inode,
+                                      struct ocfs2_lock_res *lockres)
+{
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       unsigned long flags;
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
+       lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       return ocfs2_lock_create(osb, lockres, LKM_EXMODE, LKM_LOCAL);
+}
+
+/* Grants us an EX lock on the data and metadata resources, skipping
+ * the normal cluster directory lookup. Use this ONLY on newly created
+ * inodes which other nodes can't possibly see, and which haven't been
+ * hashed in the inode hash yet. This can give us a good performance
+ * increase as it'll skip the network broadcast normally associated
+ * with creating a new lock resource. */
+int ocfs2_create_new_inode_locks(struct inode *inode)
+{
+       int ret;
+
+       BUG_ON(!inode);
+       BUG_ON(!ocfs2_inode_is_new(inode));
+
+       mlog_entry_void();
+
+       mlog(0, "Inode %"MLFu64"\n", OCFS2_I(inode)->ip_blkno);
+
+       /* NOTE: That we don't increment any of the holder counts, nor
+        * do we add anything to a journal handle. Since this is
+        * supposed to be a new inode which the cluster doesn't know
+        * about yet, there is no need to.  As far as the LVB handling
+        * is concerned, this is basically like acquiring an EX lock
+        * on a resource which has an invalid one -- we'll set it
+        * valid when we release the EX. */
+
+       ret = ocfs2_create_new_inode_lock(inode,
+                                         &OCFS2_I(inode)->ip_rw_lockres);
+       if (ret) {
+               mlog_errno(ret);
+               goto bail;
+       }
+
+       ret = ocfs2_create_new_inode_lock(inode,
+                                         &OCFS2_I(inode)->ip_meta_lockres);
+       if (ret) {
+               mlog_errno(ret);
+               goto bail;
+       }
+
+       ret = ocfs2_create_new_inode_lock(inode,
+                                         &OCFS2_I(inode)->ip_data_lockres);
+       if (ret) {
+               mlog_errno(ret);
+               goto bail;
+       }
+
+bail:
+       mlog_exit(ret);
+       return ret;
+}
+
+int ocfs2_rw_lock(struct inode *inode, int write)
+{
+       int status, level;
+       struct ocfs2_lock_res *lockres;
+
+       BUG_ON(!inode);
+
+       mlog_entry_void();
+
+       mlog(0, "inode %"MLFu64" take %s RW lock\n",
+            OCFS2_I(inode)->ip_blkno,
+            write ? "EXMODE" : "PRMODE");
+
+       lockres = &OCFS2_I(inode)->ip_rw_lockres;
+
+       level = write ? LKM_EXMODE : LKM_PRMODE;
+
+       status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
+                                   0);
+       if (status < 0)
+               mlog_errno(status);
+
+       mlog_exit(status);
+       return status;
+}
+
+void ocfs2_rw_unlock(struct inode *inode, int write)
+{
+       int level = write ? LKM_EXMODE : LKM_PRMODE;
+       struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
+
+       mlog_entry_void();
+
+       mlog(0, "inode %"MLFu64" drop %s RW lock\n",
+            OCFS2_I(inode)->ip_blkno,
+            write ? "EXMODE" : "PRMODE");
+
+       ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
+
+       mlog_exit_void();
+}
+
+int ocfs2_data_lock_full(struct inode *inode,
+                        int write,
+                        int arg_flags)
+{
+       int status = 0, level;
+       struct ocfs2_lock_res *lockres;
+
+       BUG_ON(!inode);
+
+       mlog_entry_void();
+
+       mlog(0, "inode %"MLFu64" take %s DATA lock\n",
+            OCFS2_I(inode)->ip_blkno,
+            write ? "EXMODE" : "PRMODE");
+
+       /* We'll allow faking a readonly data lock for
+        * rodevices. */
+       if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
+               if (write) {
+                       status = -EROFS;
+                       mlog_errno(status);
+               }
+               goto out;
+       }
+
+       lockres = &OCFS2_I(inode)->ip_data_lockres;
+
+       level = write ? LKM_EXMODE : LKM_PRMODE;
+
+       status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
+                                   0, arg_flags);
+       if (status < 0 && status != -EAGAIN)
+               mlog_errno(status);
+
+out:
+       mlog_exit(status);
+       return status;
+}
+
+/* see ocfs2_meta_lock_with_page() */
+int ocfs2_data_lock_with_page(struct inode *inode,
+                             int write,
+                             struct page *page)
+{
+       int ret;
+
+       ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
+       if (ret == -EAGAIN) {
+               unlock_page(page);
+               if (ocfs2_data_lock(inode, write) == 0)
+                       ocfs2_data_unlock(inode, write);
+               ret = AOP_TRUNCATED_PAGE;
+       }
+
+       return ret;
+}
+
+static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
+                                struct ocfs2_lock_res *lockres)
+{
+       int kick = 0;
+
+       mlog_entry_void();
+
+       /* If we know that another node is waiting on our lock, kick
+        * the vote thread * pre-emptively when we reach a release
+        * condition. */
+       if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
+               switch(lockres->l_blocking) {
+               case LKM_EXMODE:
+                       if (!lockres->l_ex_holders && !lockres->l_ro_holders)
+                               kick = 1;
+                       break;
+               case LKM_PRMODE:
+                       if (!lockres->l_ex_holders)
+                               kick = 1;
+                       break;
+               default:
+                       BUG();
+               }
+       }
+
+       if (kick)
+               ocfs2_kick_vote_thread(osb);
+
+       mlog_exit_void();
+}
+
+void ocfs2_data_unlock(struct inode *inode,
+                      int write)
+{
+       int level = write ? LKM_EXMODE : LKM_PRMODE;
+       struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
+
+       mlog_entry_void();
+
+       mlog(0, "inode %"MLFu64" drop %s DATA lock\n",
+            OCFS2_I(inode)->ip_blkno,
+            write ? "EXMODE" : "PRMODE");
+
+       if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
+               ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
+
+       mlog_exit_void();
+}
+
+#define OCFS2_SEC_BITS   34
+#define OCFS2_SEC_SHIFT  (64 - 34)
+#define OCFS2_NSEC_MASK  ((1ULL << OCFS2_SEC_SHIFT) - 1)
+
+/* LVB only has room for 64 bits of time here so we pack it for
+ * now. */
+static u64 ocfs2_pack_timespec(struct timespec *spec)
+{
+       u64 res;
+       u64 sec = spec->tv_sec;
+       u32 nsec = spec->tv_nsec;
+
+       res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
+
+       return res;
+}
+
+/* Call this with the lockres locked. I am reasonably sure we don't
+ * need ip_lock in this function as anyone who would be changing those
+ * values is supposed to be blocked in ocfs2_meta_lock right now. */
+static void __ocfs2_stuff_meta_lvb(struct inode *inode)
+{
+       struct ocfs2_inode_info *oi = OCFS2_I(inode);
+       struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
+       struct ocfs2_meta_lvb *lvb;
+
+       mlog_entry_void();
+
+       lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
+
+       lvb->lvb_version   = cpu_to_be32(OCFS2_LVB_VERSION);
+       lvb->lvb_isize     = cpu_to_be64(i_size_read(inode));
+       lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
+       lvb->lvb_iuid      = cpu_to_be32(inode->i_uid);
+       lvb->lvb_igid      = cpu_to_be32(inode->i_gid);
+       lvb->lvb_imode     = cpu_to_be16(inode->i_mode);
+       lvb->lvb_inlink    = cpu_to_be16(inode->i_nlink);
+       lvb->lvb_iatime_packed  =
+               cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
+       lvb->lvb_ictime_packed =
+               cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
+       lvb->lvb_imtime_packed =
+               cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
+
+       mlog_meta_lvb(0, lockres);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_unpack_timespec(struct timespec *spec,
+                                 u64 packed_time)
+{
+       spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
+       spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
+}
+
+static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
+{
+       struct ocfs2_inode_info *oi = OCFS2_I(inode);
+       struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
+       struct ocfs2_meta_lvb *lvb;
+
+       mlog_entry_void();
+
+       mlog_meta_lvb(0, lockres);
+
+       lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
+
+       /* We're safe here without the lockres lock... */
+       spin_lock(&oi->ip_lock);
+       oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
+       i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
+
+       /* fast-symlinks are a special case */
+       if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
+               inode->i_blocks = 0;
+       else
+               inode->i_blocks =
+                       ocfs2_align_bytes_to_sectors(i_size_read(inode));
+
+       inode->i_uid     = be32_to_cpu(lvb->lvb_iuid);
+       inode->i_gid     = be32_to_cpu(lvb->lvb_igid);
+       inode->i_mode    = be16_to_cpu(lvb->lvb_imode);
+       inode->i_nlink   = be16_to_cpu(lvb->lvb_inlink);
+       ocfs2_unpack_timespec(&inode->i_atime,
+                             be64_to_cpu(lvb->lvb_iatime_packed));
+       ocfs2_unpack_timespec(&inode->i_mtime,
+                             be64_to_cpu(lvb->lvb_imtime_packed));
+       ocfs2_unpack_timespec(&inode->i_ctime,
+                             be64_to_cpu(lvb->lvb_ictime_packed));
+       spin_unlock(&oi->ip_lock);
+
+       mlog_exit_void();
+}
+
+static inline int ocfs2_meta_lvb_is_trustable(struct ocfs2_lock_res *lockres)
+{
+       struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
+
+       if (be32_to_cpu(lvb->lvb_version) == OCFS2_LVB_VERSION)
+               return 1;
+       return 0;
+}
+
+/* Determine whether a lock resource needs to be refreshed, and
+ * arbitrate who gets to refresh it.
+ *
+ *   0 means no refresh needed.
+ *
+ *   > 0 means you need to refresh this and you MUST call
+ *   ocfs2_complete_lock_res_refresh afterwards. */
+static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
+{
+       unsigned long flags;
+       int status = 0;
+
+       mlog_entry_void();
+
+refresh_check:
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               goto bail;
+       }
+
+       if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+               ocfs2_wait_on_refreshing_lock(lockres);
+               goto refresh_check;
+       }
+
+       /* Ok, I'll be the one to refresh this lock. */
+       lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       status = 1;
+bail:
+       mlog_exit(status);
+       return status;
+}
+
+/* If status is non zero, I'll mark it as not being in refresh
+ * anymroe, but i won't clear the needs refresh flag. */
+static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
+                                                  int status)
+{
+       unsigned long flags;
+       mlog_entry_void();
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
+       if (!status)
+               lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       wake_up(&lockres->l_event);
+
+       mlog_exit_void();
+}
+
+/* may or may not return a bh if it went to disk. */
+static int ocfs2_meta_lock_update(struct inode *inode,
+                                 struct buffer_head **bh)
+{
+       int status = 0;
+       struct ocfs2_inode_info *oi = OCFS2_I(inode);
+       struct ocfs2_lock_res *lockres;
+       struct ocfs2_dinode *fe;
+
+       mlog_entry_void();
+
+       spin_lock(&oi->ip_lock);
+       if (oi->ip_flags & OCFS2_INODE_DELETED) {
+               mlog(0, "Orphaned inode %"MLFu64" was deleted while we "
+                    "were waiting on a lock. ip_flags = 0x%x\n",
+                    oi->ip_blkno, oi->ip_flags);
+               spin_unlock(&oi->ip_lock);
+               status = -ENOENT;
+               goto bail;
+       }
+       spin_unlock(&oi->ip_lock);
+
+       lockres = &oi->ip_meta_lockres;
+
+       if (!ocfs2_should_refresh_lock_res(lockres))
+               goto bail;
+
+       /* This will discard any caching information we might have had
+        * for the inode metadata. */
+       ocfs2_metadata_cache_purge(inode);
+
+       /* will do nothing for inode types that don't use the extent
+        * map (directories, bitmap files, etc) */
+       ocfs2_extent_map_trunc(inode, 0);
+
+       if (ocfs2_meta_lvb_is_trustable(lockres)) {
+               mlog(0, "Trusting LVB on inode %"MLFu64"\n",
+                    oi->ip_blkno);
+               ocfs2_refresh_inode_from_lvb(inode);
+       } else {
+               /* Boo, we have to go to disk. */
+               /* read bh, cast, ocfs2_refresh_inode */
+               status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
+                                         bh, OCFS2_BH_CACHED, inode);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail_refresh;
+               }
+               fe = (struct ocfs2_dinode *) (*bh)->b_data;
+
+               /* This is a good chance to make sure we're not
+                * locking an invalid object.
+                *
+                * We bug on a stale inode here because we checked
+                * above whether it was wiped from disk. The wiping
+                * node provides a guarantee that we receive that
+                * message and can mark the inode before dropping any
+                * locks associated with it. */
+               if (!OCFS2_IS_VALID_DINODE(fe)) {
+                       OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
+                       status = -EIO;
+                       goto bail_refresh;
+               }
+               mlog_bug_on_msg(inode->i_generation !=
+                               le32_to_cpu(fe->i_generation),
+                               "Invalid dinode %"MLFu64" disk generation: %u "
+                               "inode->i_generation: %u\n",
+                               oi->ip_blkno, le32_to_cpu(fe->i_generation),
+                               inode->i_generation);
+               mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
+                               !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
+                               "Stale dinode %"MLFu64" dtime: %"MLFu64" "
+                               "flags: 0x%x\n", oi->ip_blkno,
+                               le64_to_cpu(fe->i_dtime),
+                               le32_to_cpu(fe->i_flags));
+
+               ocfs2_refresh_inode(inode, fe);
+       }
+
+       status = 0;
+bail_refresh:
+       ocfs2_complete_lock_res_refresh(lockres, status);
+bail:
+       mlog_exit(status);
+       return status;
+}
+
+static int ocfs2_assign_bh(struct inode *inode,
+                          struct buffer_head **ret_bh,
+                          struct buffer_head *passed_bh)
+{
+       int status;
+
+       if (passed_bh) {
+               /* Ok, the update went to disk for us, use the
+                * returned bh. */
+               *ret_bh = passed_bh;
+               get_bh(*ret_bh);
+
+               return 0;
+       }
+
+       status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
+                                 OCFS2_I(inode)->ip_blkno,
+                                 ret_bh,
+                                 OCFS2_BH_CACHED,
+                                 inode);
+       if (status < 0)
+               mlog_errno(status);
+
+       return status;
+}
+
+/*
+ * returns < 0 error if the callback will never be called, otherwise
+ * the result of the lock will be communicated via the callback.
+ */
+int ocfs2_meta_lock_full(struct inode *inode,
+                        struct ocfs2_journal_handle *handle,
+                        struct buffer_head **ret_bh,
+                        int ex,
+                        int arg_flags)
+{
+       int status, level, dlm_flags, acquired;
+       struct ocfs2_lock_res *lockres;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct buffer_head *local_bh = NULL;
+
+       BUG_ON(!inode);
+
+       mlog_entry_void();
+
+       mlog(0, "inode %"MLFu64", take %s META lock\n",
+            OCFS2_I(inode)->ip_blkno,
+            ex ? "EXMODE" : "PRMODE");
+
+       status = 0;
+       acquired = 0;
+       /* We'll allow faking a readonly metadata lock for
+        * rodevices. */
+       if (ocfs2_is_hard_readonly(osb)) {
+               if (ex)
+                       status = -EROFS;
+               goto bail;
+       }
+
+       if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
+               wait_event(osb->recovery_event,
+                          ocfs2_node_map_is_empty(osb, &osb->recovery_map));
+
+       acquired = 0;
+       lockres = &OCFS2_I(inode)->ip_meta_lockres;
+       level = ex ? LKM_EXMODE : LKM_PRMODE;
+       dlm_flags = 0;
+       if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
+               dlm_flags |= LKM_NOQUEUE;
+
+       status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
+       if (status < 0) {
+               if (status != -EAGAIN && status != -EIOCBRETRY)
+                       mlog_errno(status);
+               goto bail;
+       }
+
+       /* Notify the error cleanup path to drop the cluster lock. */
+       acquired = 1;
+
+       /* We wait twice because a node may have died while we were in
+        * the lower dlm layers. The second time though, we've
+        * committed to owning this lock so we don't allow signals to
+        * abort the operation. */
+       if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
+               wait_event(osb->recovery_event,
+                          ocfs2_node_map_is_empty(osb, &osb->recovery_map));
+
+       /* This is fun. The caller may want a bh back, or it may
+        * not. ocfs2_meta_lock_update definitely wants one in, but
+        * may or may not read one, depending on what's in the
+        * LVB. The result of all of this is that we've *only* gone to
+        * disk if we have to, so the complexity is worthwhile. */
+       status = ocfs2_meta_lock_update(inode, &local_bh);
+       if (status < 0) {
+               if (status != -ENOENT)
+                       mlog_errno(status);
+               goto bail;
+       }
+
+       if (ret_bh) {
+               status = ocfs2_assign_bh(inode, ret_bh, local_bh);
+               if (status < 0) {
+                       mlog_errno(status);
+                       goto bail;
+               }
+       }
+
+       if (handle) {
+               status = ocfs2_handle_add_lock(handle, inode);
+               if (status < 0)
+                       mlog_errno(status);
+       }
+
+bail:
+       if (status < 0) {
+               if (ret_bh && (*ret_bh)) {
+                       brelse(*ret_bh);
+                       *ret_bh = NULL;
+               }
+               if (acquired)
+                       ocfs2_meta_unlock(inode, ex);
+       }
+
+       if (local_bh)
+               brelse(local_bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * This is working around a lock inversion between tasks acquiring DLM locks
+ * while holding a page lock and the vote thread which blocks dlm lock acquiry
+ * while acquiring page locks.
+ *
+ * ** These _with_page variantes are only intended to be called from aop
+ * methods that hold page locks and return a very specific *positive* error
+ * code that aop methods pass up to the VFS -- test for errors with != 0. **
+ *
+ * The DLM is called such that it returns -EAGAIN if it would have blocked
+ * waiting for the vote thread.  In that case we unlock our page so the vote
+ * thread can make progress.  Once we've done this we have to return
+ * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
+ * into the VFS who will then immediately retry the aop call.
+ *
+ * We do a blocking lock and immediate unlock before returning, though, so that
+ * the lock has a great chance of being cached on this node by the time the VFS
+ * calls back to retry the aop.    This has a potential to livelock as nodes
+ * ping locks back and forth, but that's a risk we're willing to take to avoid
+ * the lock inversion simply.
+ */
+int ocfs2_meta_lock_with_page(struct inode *inode,
+                             struct ocfs2_journal_handle *handle,
+                             struct buffer_head **ret_bh,
+                             int ex,
+                             struct page *page)
+{
+       int ret;
+
+       ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
+                                  OCFS2_LOCK_NONBLOCK);
+       if (ret == -EAGAIN) {
+               unlock_page(page);
+               if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
+                       ocfs2_meta_unlock(inode, ex);
+               ret = AOP_TRUNCATED_PAGE;
+       }
+
+       return ret;
+}
+
+void ocfs2_meta_unlock(struct inode *inode,
+                      int ex)
+{
+       int level = ex ? LKM_EXMODE : LKM_PRMODE;
+       struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
+
+       mlog_entry_void();
+
+       mlog(0, "inode %"MLFu64" drop %s META lock\n",
+            OCFS2_I(inode)->ip_blkno,
+            ex ? "EXMODE" : "PRMODE");
+
+       if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
+               ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
+
+       mlog_exit_void();
+}
+
+int ocfs2_super_lock(struct ocfs2_super *osb,
+                    int ex)
+{
+       int status;
+       int level = ex ? LKM_EXMODE : LKM_PRMODE;
+       struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
+       struct buffer_head *bh;
+       struct ocfs2_slot_info *si = osb->slot_info;
+
+       mlog_entry_void();
+
+       if (ocfs2_is_hard_readonly(osb))
+               return -EROFS;
+
+       status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* The super block lock path is really in the best position to
+        * know when resources covered by the lock need to be
+        * refreshed, so we do it here. Of course, making sense of
+        * everything is up to the caller :) */
+       status = ocfs2_should_refresh_lock_res(lockres);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+       if (status) {
+               bh = si->si_bh;
+               status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
+                                         si->si_inode);
+               if (status == 0)
+                       ocfs2_update_slot_info(si);
+
+               ocfs2_complete_lock_res_refresh(lockres, status);
+
+               if (status < 0)
+                       mlog_errno(status);
+       }
+bail:
+       mlog_exit(status);
+       return status;
+}
+
+void ocfs2_super_unlock(struct ocfs2_super *osb,
+                       int ex)
+{
+       int level = ex ? LKM_EXMODE : LKM_PRMODE;
+       struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
+
+       ocfs2_cluster_unlock(osb, lockres, level);
+}
+
+int ocfs2_rename_lock(struct ocfs2_super *osb)
+{
+       int status;
+       struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
+
+       if (ocfs2_is_hard_readonly(osb))
+               return -EROFS;
+
+       status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
+       if (status < 0)
+               mlog_errno(status);
+
+       return status;
+}
+
+void ocfs2_rename_unlock(struct ocfs2_super *osb)
+{
+       struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
+
+       ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
+}
+
+/* Reference counting of the dlm debug structure. We want this because
+ * open references on the debug inodes can live on after a mount, so
+ * we can't rely on the ocfs2_super to always exist. */
+static void ocfs2_dlm_debug_free(struct kref *kref)
+{
+       struct ocfs2_dlm_debug *dlm_debug;
+
+       dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
+
+       kfree(dlm_debug);
+}
+
+void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
+{
+       if (dlm_debug)
+               kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
+}
+
+static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
+{
+       kref_get(&debug->d_refcnt);
+}
+
+struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
+{
+       struct ocfs2_dlm_debug *dlm_debug;
+
+       dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
+       if (!dlm_debug) {
+               mlog_errno(-ENOMEM);
+               goto out;
+       }
+
+       kref_init(&dlm_debug->d_refcnt);
+       INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
+       dlm_debug->d_locking_state = NULL;
+out:
+       return dlm_debug;
+}
+
+/* Access to this is arbitrated for us via seq_file->sem. */
+struct ocfs2_dlm_seq_priv {
+       struct ocfs2_dlm_debug *p_dlm_debug;
+       struct ocfs2_lock_res p_iter_res;
+       struct ocfs2_lock_res p_tmp_res;
+};
+
+static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
+                                                struct ocfs2_dlm_seq_priv *priv)
+{
+       struct ocfs2_lock_res *iter, *ret = NULL;
+       struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
+
+       assert_spin_locked(&ocfs2_dlm_tracking_lock);
+
+       list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
+               /* discover the head of the list */
+               if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
+                       mlog(0, "End of list found, %p\n", ret);
+                       break;
+               }
+
+               /* We track our "dummy" iteration lockres' by a NULL
+                * l_ops field. */
+               if (iter->l_ops != NULL) {
+                       ret = iter;
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
+{
+       struct ocfs2_dlm_seq_priv *priv = m->private;
+       struct ocfs2_lock_res *iter;
+
+       spin_lock(&ocfs2_dlm_tracking_lock);
+       iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
+       if (iter) {
+               /* Since lockres' have the lifetime of their container
+                * (which can be inodes, ocfs2_supers, etc) we want to
+                * copy this out to a temporary lockres while still
+                * under the spinlock. Obviously after this we can't
+                * trust any pointers on the copy returned, but that's
+                * ok as the information we want isn't typically held
+                * in them. */
+               priv->p_tmp_res = *iter;
+               iter = &priv->p_tmp_res;
+       }
+       spin_unlock(&ocfs2_dlm_tracking_lock);
+
+       return iter;
+}
+
+static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
+{
+}
+
+static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
+{
+       struct ocfs2_dlm_seq_priv *priv = m->private;
+       struct ocfs2_lock_res *iter = v;
+       struct ocfs2_lock_res *dummy = &priv->p_iter_res;
+
+       spin_lock(&ocfs2_dlm_tracking_lock);
+       iter = ocfs2_dlm_next_res(iter, priv);
+       list_del_init(&dummy->l_debug_list);
+       if (iter) {
+               list_add(&dummy->l_debug_list, &iter->l_debug_list);
+               priv->p_tmp_res = *iter;
+               iter = &priv->p_tmp_res;
+       }
+       spin_unlock(&ocfs2_dlm_tracking_lock);
+
+       return iter;
+}
+
+/* So that debugfs.ocfs2 can determine which format is being used */
+#define OCFS2_DLM_DEBUG_STR_VERSION 1
+static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
+{
+       int i;
+       char *lvb;
+       struct ocfs2_lock_res *lockres = v;
+
+       if (!lockres)
+               return -EINVAL;
+
+       seq_printf(m, "0x%x\t"
+                  "%.*s\t"
+                  "%d\t"
+                  "0x%lx\t"
+                  "0x%x\t"
+                  "0x%x\t"
+                  "%u\t"
+                  "%u\t"
+                  "%d\t"
+                  "%d\t",
+                  OCFS2_DLM_DEBUG_STR_VERSION,
+                  OCFS2_LOCK_ID_MAX_LEN, lockres->l_name,
+                  lockres->l_level,
+                  lockres->l_flags,
+                  lockres->l_action,
+                  lockres->l_unlock_action,
+                  lockres->l_ro_holders,
+                  lockres->l_ex_holders,
+                  lockres->l_requested,
+                  lockres->l_blocking);
+
+       /* Dump the raw LVB */
+       lvb = lockres->l_lksb.lvb;
+       for(i = 0; i < DLM_LVB_LEN; i++)
+               seq_printf(m, "0x%x\t", lvb[i]);
+
+       /* End the line */
+       seq_printf(m, "\n");
+       return 0;
+}
+
+static struct seq_operations ocfs2_dlm_seq_ops = {
+       .start =        ocfs2_dlm_seq_start,
+       .stop =         ocfs2_dlm_seq_stop,
+       .next =         ocfs2_dlm_seq_next,
+       .show =         ocfs2_dlm_seq_show,
+};
+
+static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
+{
+       struct seq_file *seq = (struct seq_file *) file->private_data;
+       struct ocfs2_dlm_seq_priv *priv = seq->private;
+       struct ocfs2_lock_res *res = &priv->p_iter_res;
+
+       ocfs2_remove_lockres_tracking(res);
+       ocfs2_put_dlm_debug(priv->p_dlm_debug);
+       return seq_release_private(inode, file);
+}
+
+static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
+{
+       int ret;
+       struct ocfs2_dlm_seq_priv *priv;
+       struct seq_file *seq;
+       struct ocfs2_super *osb;
+
+       priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
+       if (!priv) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto out;
+       }
+       osb = (struct ocfs2_super *) inode->u.generic_ip;
+       ocfs2_get_dlm_debug(osb->osb_dlm_debug);
+       priv->p_dlm_debug = osb->osb_dlm_debug;
+       INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
+
+       ret = seq_open(file, &ocfs2_dlm_seq_ops);
+       if (ret) {
+               kfree(priv);
+               mlog_errno(ret);
+               goto out;
+       }
+
+       seq = (struct seq_file *) file->private_data;
+       seq->private = priv;
+
+       ocfs2_add_lockres_tracking(&priv->p_iter_res,
+                                  priv->p_dlm_debug);
+
+out:
+       return ret;
+}
+
+static struct file_operations ocfs2_dlm_debug_fops = {
+       .open =         ocfs2_dlm_debug_open,
+       .release =      ocfs2_dlm_debug_release,
+       .read =         seq_read,
+       .llseek =       seq_lseek,
+};
+
+static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
+{
+       int ret = 0;
+       struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
+
+       dlm_debug->d_locking_state = debugfs_create_file("locking_state",
+                                                        S_IFREG|S_IRUSR,
+                                                        osb->osb_debug_root,
+                                                        osb,
+                                                        &ocfs2_dlm_debug_fops);
+       if (!dlm_debug->d_locking_state) {
+               ret = -EINVAL;
+               mlog(ML_ERROR,
+                    "Unable to create locking state debugfs file.\n");
+               goto out;
+       }
+
+       ocfs2_get_dlm_debug(dlm_debug);
+out:
+       return ret;
+}
+
+static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
+{
+       struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
+
+       if (dlm_debug) {
+               debugfs_remove(dlm_debug->d_locking_state);
+               ocfs2_put_dlm_debug(dlm_debug);
+       }
+}
+
+int ocfs2_dlm_init(struct ocfs2_super *osb)
+{
+       int status;
+       u32 dlm_key;
+       struct dlm_ctxt *dlm;
+
+       mlog_entry_void();
+
+       status = ocfs2_dlm_init_debug(osb);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* launch vote thread */
+       osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote-%d",
+                                    osb->osb_id);
+       if (IS_ERR(osb->vote_task)) {
+               status = PTR_ERR(osb->vote_task);
+               osb->vote_task = NULL;
+               mlog_errno(status);
+               goto bail;
+       }
+
+       /* used by the dlm code to make message headers unique, each
+        * node in this domain must agree on this. */
+       dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
+
+       /* for now, uuid == domain */
+       dlm = dlm_register_domain(osb->uuid_str, dlm_key);
+       if (IS_ERR(dlm)) {
+               status = PTR_ERR(dlm);
+               mlog_errno(status);
+               goto bail;
+       }
+
+       ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
+       ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
+
+       dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
+
+       osb->dlm = dlm;
+
+       status = 0;
+bail:
+       if (status < 0) {
+               ocfs2_dlm_shutdown_debug(osb);
+               if (osb->vote_task)
+                       kthread_stop(osb->vote_task);
+       }
+
+       mlog_exit(status);
+       return status;
+}
+
+void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
+{
+       mlog_entry_void();
+
+       dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
+
+       ocfs2_drop_osb_locks(osb);
+
+       if (osb->vote_task) {
+               kthread_stop(osb->vote_task);
+               osb->vote_task = NULL;
+       }
+
+       ocfs2_lock_res_free(&osb->osb_super_lockres);
+       ocfs2_lock_res_free(&osb->osb_rename_lockres);
+
+       dlm_unregister_domain(osb->dlm);
+       osb->dlm = NULL;
+
+       ocfs2_dlm_shutdown_debug(osb);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_unlock_ast_func(void *opaque, enum dlm_status status)
+{
+       struct ocfs2_lock_res *lockres = opaque;
+       unsigned long flags;
+
+       mlog_entry_void();
+
+       mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
+            lockres->l_unlock_action);
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       /* We tried to cancel a convert request, but it was already
+        * granted. All we want to do here is clear our unlock
+        * state. The wake_up call done at the bottom is redundant
+        * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
+        * hurt anything anyway */
+       if (status == DLM_CANCELGRANT &&
+           lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
+               mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
+
+               /* We don't clear the busy flag in this case as it
+                * should have been cleared by the ast which the dlm
+                * has called. */
+               goto complete_unlock;
+       }
+
+       if (status != DLM_NORMAL) {
+               mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
+                    "unlock_action %d\n", status, lockres->l_name,
+                    lockres->l_unlock_action);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               return;
+       }
+
+       switch(lockres->l_unlock_action) {
+       case OCFS2_UNLOCK_CANCEL_CONVERT:
+               mlog(0, "Cancel convert success for %s\n", lockres->l_name);
+               lockres->l_action = OCFS2_AST_INVALID;
+               break;
+       case OCFS2_UNLOCK_DROP_LOCK:
+               lockres->l_level = LKM_IVMODE;
+               break;
+       default:
+               BUG();
+       }
+
+       lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
+complete_unlock:
+       lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       wake_up(&lockres->l_event);
+
+       mlog_exit_void();
+}
+
+typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
+
+struct drop_lock_cb {
+       ocfs2_pre_drop_cb_t     *drop_func;
+       void                    *drop_data;
+};
+
+static int ocfs2_drop_lock(struct ocfs2_super *osb,
+                          struct ocfs2_lock_res *lockres,
+                          struct drop_lock_cb *dcb)
+{
+       enum dlm_status status;
+       unsigned long flags;
+
+       /* We didn't get anywhere near actually using this lockres. */
+       if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
+               goto out;
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+
+       mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
+                       "lockres %s, flags 0x%lx\n",
+                       lockres->l_name, lockres->l_flags);
+
+       while (lockres->l_flags & OCFS2_LOCK_BUSY) {
+               mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
+                    "%u, unlock_action = %u\n",
+                    lockres->l_name, lockres->l_flags, lockres->l_action,
+                    lockres->l_unlock_action);
+
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+               /* XXX: Today we just wait on any busy
+                * locks... Perhaps we need to cancel converts in the
+                * future? */
+               ocfs2_wait_on_busy_lock(lockres);
+
+               spin_lock_irqsave(&lockres->l_lock, flags);
+       }
+
+       if (dcb)
+               dcb->drop_func(lockres, dcb->drop_data);
+
+       if (lockres->l_flags & OCFS2_LOCK_BUSY)
+               mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
+                    lockres->l_name);
+       if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
+               mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
+
+       if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               goto out;
+       }
+
+       lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
+
+       /* make sure we never get here while waiting for an ast to
+        * fire. */
+       BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
+
+       /* is this necessary? */
+       lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
+       lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       mlog(0, "lock %s\n", lockres->l_name);
+
+       status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
+                          lockres->l_ops->unlock_ast, lockres);
+       if (status != DLM_NORMAL) {
+               ocfs2_log_dlm_error("dlmunlock", status, lockres);
+               mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
+               dlm_print_one_lock(lockres->l_lksb.lockid);
+               BUG();
+       }
+       mlog(0, "lock %s, successfull return from dlmunlock\n",
+            lockres->l_name);
+
+       ocfs2_wait_on_busy_lock(lockres);
+out:
+       mlog_exit(0);
+       return 0;
+}
+
+/* Mark the lockres as being dropped. It will no longer be
+ * queued if blocking, but we still may have to wait on it
+ * being dequeued from the vote thread before we can consider
+ * it safe to drop. 
+ *
+ * You can *not* attempt to call cluster_lock on this lockres anymore. */
+void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
+{
+       int status;
+       struct ocfs2_mask_waiter mw;
+       unsigned long flags;
+
+       ocfs2_init_mask_waiter(&mw);
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       lockres->l_flags |= OCFS2_LOCK_FREEING;
+       while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
+               lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+               mlog(0, "Waiting on lockres %s\n", lockres->l_name);
+
+               status = ocfs2_wait_for_mask(&mw);
+               if (status)
+                       mlog_errno(status);
+
+               spin_lock_irqsave(&lockres->l_lock, flags);
+       }
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+}
+
+static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
+{
+       int status;
+
+       mlog_entry_void();
+
+       ocfs2_mark_lockres_freeing(&osb->osb_super_lockres);
+
+       status = ocfs2_drop_lock(osb, &osb->osb_super_lockres, NULL);
+       if (status < 0)
+               mlog_errno(status);
+
+       ocfs2_mark_lockres_freeing(&osb->osb_rename_lockres);
+
+       status = ocfs2_drop_lock(osb, &osb->osb_rename_lockres, NULL);
+       if (status < 0)
+               mlog_errno(status);
+
+       mlog_exit(status);
+}
+
+static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
+{
+       struct inode *inode = data;
+
+       /* the metadata lock requires a bit more work as we have an
+        * LVB to worry about. */
+       if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
+           lockres->l_level == LKM_EXMODE &&
+           !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
+               __ocfs2_stuff_meta_lvb(inode);
+}
+
+int ocfs2_drop_inode_locks(struct inode *inode)
+{
+       int status, err;
+       struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
+
+       mlog_entry_void();
+
+       /* No need to call ocfs2_mark_lockres_freeing here -
+        * ocfs2_clear_inode has done it for us. */
+
+       err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
+                             &OCFS2_I(inode)->ip_data_lockres,
+                             NULL);
+       if (err < 0)
+               mlog_errno(err);
+
+       status = err;
+
+       err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
+                             &OCFS2_I(inode)->ip_meta_lockres,
+                             &meta_dcb);
+       if (err < 0)
+               mlog_errno(err);
+       if (err < 0 && !status)
+               status = err;
+
+       err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
+                             &OCFS2_I(inode)->ip_rw_lockres,
+                             NULL);
+       if (err < 0)
+               mlog_errno(err);
+       if (err < 0 && !status)
+               status = err;
+
+       mlog_exit(status);
+       return status;
+}
+
+static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
+                                     int new_level)
+{
+       assert_spin_locked(&lockres->l_lock);
+
+       BUG_ON(lockres->l_blocking <= LKM_NLMODE);
+
+       if (lockres->l_level <= new_level) {
+               mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
+                    lockres->l_level, new_level);
+               BUG();
+       }
+
+       mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
+            lockres->l_name, new_level, lockres->l_blocking);
+
+       lockres->l_action = OCFS2_AST_DOWNCONVERT;
+       lockres->l_requested = new_level;
+       lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
+}
+
+static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
+                                 struct ocfs2_lock_res *lockres,
+                                 int new_level,
+                                 int lvb)
+{
+       int ret, dlm_flags = LKM_CONVERT;
+       enum dlm_status status;
+
+       mlog_entry_void();
+
+       if (lvb)
+               dlm_flags |= LKM_VALBLK;
+
+       status = dlmlock(osb->dlm,
+                        new_level,
+                        &lockres->l_lksb,
+                        dlm_flags,
+                        lockres->l_name,
+                        lockres->l_ops->ast,
+                        lockres,
+                        lockres->l_ops->bast);
+       if (status != DLM_NORMAL) {
+               ocfs2_log_dlm_error("dlmlock", status, lockres);
+               ret = -EINVAL;
+               ocfs2_recover_from_dlm_error(lockres, 1);
+               goto bail;
+       }
+
+       ret = 0;
+bail:
+       mlog_exit(ret);
+       return ret;
+}
+
+/* returns 1 when the caller should unlock and call dlmunlock */
+static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
+                                       struct ocfs2_lock_res *lockres)
+{
+       assert_spin_locked(&lockres->l_lock);
+
+       mlog_entry_void();
+       mlog(0, "lock %s\n", lockres->l_name);
+
+       if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
+               /* If we're already trying to cancel a lock conversion
+                * then just drop the spinlock and allow the caller to
+                * requeue this lock. */
+
+               mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
+               return 0;
+       }
+
+       /* were we in a convert when we got the bast fire? */
+       BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
+              lockres->l_action != OCFS2_AST_DOWNCONVERT);
+       /* set things up for the unlockast to know to just
+        * clear out the ast_action and unset busy, etc. */
+       lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
+
+       mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
+                       "lock %s, invalid flags: 0x%lx\n",
+                       lockres->l_name, lockres->l_flags);
+
+       return 1;
+}
+
+static int ocfs2_cancel_convert(struct ocfs2_super *osb,
+                               struct ocfs2_lock_res *lockres)
+{
+       int ret;
+       enum dlm_status status;
+
+       mlog_entry_void();
+       mlog(0, "lock %s\n", lockres->l_name);
+
+       ret = 0;
+       status = dlmunlock(osb->dlm,
+                          &lockres->l_lksb,
+                          LKM_CANCEL,
+                          lockres->l_ops->unlock_ast,
+                          lockres);
+       if (status != DLM_NORMAL) {
+               ocfs2_log_dlm_error("dlmunlock", status, lockres);
+               ret = -EINVAL;
+               ocfs2_recover_from_dlm_error(lockres, 0);
+       }
+
+       mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
+
+       mlog_exit(ret);
+       return ret;
+}
+
+static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
+                                                 struct ocfs2_lock_res *lockres,
+                                                 int new_level)
+{
+       int ret;
+
+       mlog_entry_void();
+
+       BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
+
+       if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
+               ret = 0;
+               mlog(0, "lockres %s currently being refreshed -- backing "
+                    "off!\n", lockres->l_name);
+       } else if (new_level == LKM_PRMODE)
+               ret = !lockres->l_ex_holders &&
+                       ocfs2_inode_fully_checkpointed(inode);
+       else /* Must be NLMODE we're converting to. */
+               ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
+                       ocfs2_inode_fully_checkpointed(inode);
+
+       mlog_exit(ret);
+       return ret;
+}
+
+static int ocfs2_do_unblock_meta(struct inode *inode,
+                                int *requeue)
+{
+       int new_level;
+       int set_lvb = 0;
+       int ret = 0;
+       struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
+       unsigned long flags;
+
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+
+       mlog_entry_void();
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
+
+       mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
+            lockres->l_blocking);
+
+       BUG_ON(lockres->l_level != LKM_EXMODE &&
+              lockres->l_level != LKM_PRMODE);
+
+       if (lockres->l_flags & OCFS2_LOCK_BUSY) {
+               *requeue = 1;
+               ret = ocfs2_prepare_cancel_convert(osb, lockres);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               if (ret) {
+                       ret = ocfs2_cancel_convert(osb, lockres);
+                       if (ret < 0)
+                               mlog_errno(ret);
+               }
+               goto leave;
+       }
+
+       new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
+
+       mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
+            lockres->l_level, lockres->l_blocking, new_level);
+
+       if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
+               if (lockres->l_level == LKM_EXMODE)
+                       set_lvb = 1;
+
+               /* If the lock hasn't been refreshed yet (rare), then
+                * our memory inode values are old and we skip
+                * stuffing the lvb. There's no need to actually clear
+                * out the lvb here as it's value is still valid. */
+               if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
+                       if (set_lvb)
+                               __ocfs2_stuff_meta_lvb(inode);
+               } else
+                       mlog(0, "lockres %s: downconverting stale lock!\n",
+                            lockres->l_name);
+
+               mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
+                    "l_blocking=%d, new_level=%d\n",
+                    lockres->l_level, lockres->l_blocking, new_level);
+
+               ocfs2_prepare_downconvert(lockres, new_level);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
+               goto leave;
+       }
+       if (!ocfs2_inode_fully_checkpointed(inode))
+               ocfs2_start_checkpoint(osb);
+
+       *requeue = 1;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+       ret = 0;
+leave:
+       mlog_exit(ret);
+       return ret;
+}
+
+static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
+                                     struct ocfs2_lock_res *lockres,
+                                     int *requeue,
+                                     ocfs2_convert_worker_t *worker)
+{
+       unsigned long flags;
+       int blocking;
+       int new_level;
+       int ret = 0;
+
+       mlog_entry_void();
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+
+       BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
+
+recheck:
+       if (lockres->l_flags & OCFS2_LOCK_BUSY) {
+               *requeue = 1;
+               ret = ocfs2_prepare_cancel_convert(osb, lockres);
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               if (ret) {
+                       ret = ocfs2_cancel_convert(osb, lockres);
+                       if (ret < 0)
+                               mlog_errno(ret);
+               }
+               goto leave;
+       }
+
+       /* if we're blocking an exclusive and we have *any* holders,
+        * then requeue. */
+       if ((lockres->l_blocking == LKM_EXMODE)
+           && (lockres->l_ex_holders || lockres->l_ro_holders)) {
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               *requeue = 1;
+               ret = 0;
+               goto leave;
+       }
+
+       /* If it's a PR we're blocking, then only
+        * requeue if we've got any EX holders */
+       if (lockres->l_blocking == LKM_PRMODE &&
+           lockres->l_ex_holders) {
+               spin_unlock_irqrestore(&lockres->l_lock, flags);
+               *requeue = 1;
+               ret = 0;
+               goto leave;
+       }
+
+       /* If we get here, then we know that there are no more
+        * incompatible holders (and anyone asking for an incompatible
+        * lock is blocked). We can now downconvert the lock */
+       if (!worker)
+               goto downconvert;
+
+       /* Some lockres types want to do a bit of work before
+        * downconverting a lock. Allow that here. The worker function
+        * may sleep, so we save off a copy of what we're blocking as
+        * it may change while we're not holding the spin lock. */
+       blocking = lockres->l_blocking;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       worker(lockres, blocking);
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       if (blocking != lockres->l_blocking) {
+               /* If this changed underneath us, then we can't drop
+                * it just yet. */
+               goto recheck;
+       }
+
+downconvert:
+       *requeue = 0;
+       new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
+
+       ocfs2_prepare_downconvert(lockres, new_level);
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+       ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
+leave:
+       mlog_exit(ret);
+       return ret;
+}
+
+static void ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
+                                     int blocking)
+{
+       struct inode *inode;
+       struct address_space *mapping;
+
+       mlog_entry_void();
+
+               inode = ocfs2_lock_res_inode(lockres);
+       mapping = inode->i_mapping;
+
+       if (filemap_fdatawrite(mapping)) {
+               mlog(ML_ERROR, "Could not sync inode %"MLFu64" for downconvert!",
+                    OCFS2_I(inode)->ip_blkno);
+       }
+       sync_mapping_buffers(mapping);
+       if (blocking == LKM_EXMODE) {
+               truncate_inode_pages(mapping, 0);
+               unmap_mapping_range(mapping, 0, 0, 0);
+       } else {
+               /* We only need to wait on the I/O if we're not also
+                * truncating pages because truncate_inode_pages waits
+                * for us above. We don't truncate pages if we're
+                * blocking anything < EXMODE because we want to keep
+                * them around in that case. */
+               filemap_fdatawait(mapping);
+       }
+
+       mlog_exit_void();
+}
+
+int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
+                      int *requeue)
+{
+       int status;
+       struct inode *inode;
+       struct ocfs2_super *osb;
+
+       mlog_entry_void();
+
+       inode = ocfs2_lock_res_inode(lockres);
+       osb = OCFS2_SB(inode->i_sb);
+
+       mlog(0, "unblock inode %"MLFu64"\n", OCFS2_I(inode)->ip_blkno);
+
+       status = ocfs2_generic_unblock_lock(osb,
+                                           lockres,
+                                           requeue,
+                                           ocfs2_data_convert_worker);
+       if (status < 0)
+               mlog_errno(status);
+
+       mlog(0, "inode %"MLFu64", requeue = %d\n",
+            OCFS2_I(inode)->ip_blkno, *requeue);
+
+       mlog_exit(status);
+       return status;
+}
+
+static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
+                                   int *requeue)
+{
+       int status;
+       struct inode *inode;
+
+       mlog_entry_void();
+
+       mlog(0, "Unblock lockres %s\n", lockres->l_name);
+
+       inode  = ocfs2_lock_res_inode(lockres);
+
+       status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
+                                           lockres,
+                                           requeue,
+                                           NULL);
+       if (status < 0)
+               mlog_errno(status);
+
+       mlog_exit(status);
+       return status;
+}
+
+
+int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
+                      int *requeue)
+{
+       int status;
+       struct inode *inode;
+
+       mlog_entry_void();
+
+               inode = ocfs2_lock_res_inode(lockres);
+
+       mlog(0, "unblock inode %"MLFu64"\n", OCFS2_I(inode)->ip_blkno);
+
+       status = ocfs2_do_unblock_meta(inode, requeue);
+       if (status < 0)
+               mlog_errno(status);
+
+       mlog(0, "inode %"MLFu64", requeue = %d\n",
+            OCFS2_I(inode)->ip_blkno, *requeue);
+
+       mlog_exit(status);
+       return status;
+}
+
+/* Generic unblock function for any lockres whose private data is an
+ * ocfs2_super pointer. */
+static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
+                                 int *requeue)
+{
+       int status;
+       struct ocfs2_super *osb;
+
+       mlog_entry_void();
+
+       mlog(0, "Unblock lockres %s\n", lockres->l_name);
+
+       osb = ocfs2_lock_res_super(lockres);
+
+       status = ocfs2_generic_unblock_lock(osb,
+                                           lockres,
+                                           requeue,
+                                           NULL);
+       if (status < 0)
+               mlog_errno(status);
+
+       mlog_exit(status);
+       return status;
+}
+
+void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
+                               struct ocfs2_lock_res *lockres)
+{
+       int status;
+       int requeue = 0;
+       unsigned long flags;
+
+       /* Our reference to the lockres in this function can be
+        * considered valid until we remove the OCFS2_LOCK_QUEUED
+        * flag. */
+
+       mlog_entry_void();
+
+       BUG_ON(!lockres);
+       BUG_ON(!lockres->l_ops);
+       BUG_ON(!lockres->l_ops->unblock);
+
+       mlog(0, "lockres %s blocked.\n", lockres->l_name);
+
+       /* Detect whether a lock has been marked as going away while
+        * the vote thread was processing other things. A lock can
+        * still be marked with OCFS2_LOCK_FREEING after this check,
+        * but short circuiting here will still save us some
+        * performance. */
+       spin_lock_irqsave(&lockres->l_lock, flags);
+       if (lockres->l_flags & OCFS2_LOCK_FREEING)
+               goto unqueue;
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       status = lockres->l_ops->unblock(lockres, &requeue);
+       if (status < 0)
+               mlog_errno(status);
+
+       spin_lock_irqsave(&lockres->l_lock, flags);
+unqueue:
+       if (lockres->l_flags & OCFS2_LOCK_FREEING || !requeue) {
+               lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
+       } else
+               ocfs2_schedule_blocked_lock(osb, lockres);
+
+       mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
+            requeue ? "yes" : "no");
+       spin_unlock_irqrestore(&lockres->l_lock, flags);
+
+       mlog_exit_void();
+}
+
+static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
+                                       struct ocfs2_lock_res *lockres)
+{
+       mlog_entry_void();
+
+       assert_spin_locked(&lockres->l_lock);
+
+       if (lockres->l_flags & OCFS2_LOCK_FREEING) {
+               /* Do not schedule a lock for downconvert when it's on
+                * the way to destruction - any nodes wanting access
+                * to the resource will get it soon. */
+               mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
+                    lockres->l_name, lockres->l_flags);
+               return;
+       }
+
+       lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
+
+       spin_lock(&osb->vote_task_lock);
+       if (list_empty(&lockres->l_blocked_list)) {
+               list_add_tail(&lockres->l_blocked_list,
+                             &osb->blocked_lock_list);
+               osb->blocked_lock_count++;
+       }
+       spin_unlock(&osb->vote_task_lock);
+
+       mlog_exit_void();
+}
+
+/* This aids in debugging situations where a bad LVB might be involved. */
+void ocfs2_dump_meta_lvb_info(u64 level,
+                             const char *function,
+                             unsigned int line,
+                             struct ocfs2_lock_res *lockres)
+{
+       struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
+
+       mlog(level, "LVB information for %s (called from %s:%u):\n",
+            lockres->l_name, function, line);
+       mlog(level, "version: %u, clusters: %u\n",
+            be32_to_cpu(lvb->lvb_version), be32_to_cpu(lvb->lvb_iclusters));
+       mlog(level, "size: %"MLFu64", uid %u, gid %u, mode 0x%x\n",
+            be64_to_cpu(lvb->lvb_isize), be32_to_cpu(lvb->lvb_iuid),
+            be32_to_cpu(lvb->lvb_igid), be16_to_cpu(lvb->lvb_imode));
+       mlog(level, "nlink %u, atime_packed 0x%"MLFx64", "
+            "ctime_packed 0x%"MLFx64", mtime_packed 0x%"MLFx64"\n",
+            be16_to_cpu(lvb->lvb_inlink), be64_to_cpu(lvb->lvb_iatime_packed),
+            be64_to_cpu(lvb->lvb_ictime_packed),
+            be64_to_cpu(lvb->lvb_imtime_packed));
+}
diff --git a/fs/ocfs2/dlmglue.h b/fs/ocfs2/dlmglue.h
new file mode 100644 (file)
index 0000000..8f2d1db
--- /dev/null
@@ -0,0 +1,111 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dlmglue.h
+ *
+ * description here
+ *
+ * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+
+#ifndef DLMGLUE_H
+#define DLMGLUE_H
+
+#define OCFS2_LVB_VERSION 2
+
+struct ocfs2_meta_lvb {
+       __be32       lvb_version;
+       __be32       lvb_iclusters;
+       __be32       lvb_iuid;
+       __be32       lvb_igid;
+       __be64       lvb_iatime_packed;
+       __be64       lvb_ictime_packed;
+       __be64       lvb_imtime_packed;
+       __be64       lvb_isize;
+       __be16       lvb_imode;
+       __be16       lvb_inlink;
+       __be32       lvb_reserved[3];
+};
+
+/* ocfs2_meta_lock_full() and ocfs2_data_lock_full() 'arg_flags' flags */
+/* don't wait on recovery. */
+#define OCFS2_META_LOCK_RECOVERY       (0x01)
+/* Instruct the dlm not to queue ourselves on the other node. */
+#define OCFS2_META_LOCK_NOQUEUE                (0x02)
+/* don't block waiting for the vote thread, instead return -EAGAIN */
+#define OCFS2_LOCK_NONBLOCK            (0x04)
+
+int ocfs2_dlm_init(struct ocfs2_super *osb);
+void ocfs2_dlm_shutdown(struct ocfs2_super *osb);
+void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res);
+void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
+                              enum ocfs2_lock_type type,
+                              struct inode *inode);
+void ocfs2_lock_res_free(struct ocfs2_lock_res *res);
+int ocfs2_create_new_inode_locks(struct inode *inode);
+int ocfs2_drop_inode_locks(struct inode *inode);
+int ocfs2_data_lock_full(struct inode *inode,
+                        int write,
+                        int arg_flags);
+#define ocfs2_data_lock(inode, write) ocfs2_data_lock_full(inode, write, 0)
+int ocfs2_data_lock_with_page(struct inode *inode,
+                             int write,
+                             struct page *page);
+void ocfs2_data_unlock(struct inode *inode,
+                      int write);
+int ocfs2_rw_lock(struct inode *inode, int write);
+void ocfs2_rw_unlock(struct inode *inode, int write);
+int ocfs2_meta_lock_full(struct inode *inode,
+                        struct ocfs2_journal_handle *handle,
+                        struct buffer_head **ret_bh,
+                        int ex,
+                        int arg_flags);
+int ocfs2_meta_lock_with_page(struct inode *inode,
+                             struct ocfs2_journal_handle *handle,
+                             struct buffer_head **ret_bh,
+                             int ex,
+                             struct page *page);
+/* 99% of the time we don't want to supply any additional flags --
+ * those are for very specific cases only. */
+#define ocfs2_meta_lock(i, h, b, e) ocfs2_meta_lock_full(i, h, b, e, 0)
+void ocfs2_meta_unlock(struct inode *inode,
+                      int ex);
+int ocfs2_super_lock(struct ocfs2_super *osb,
+                    int ex);
+void ocfs2_super_unlock(struct ocfs2_super *osb,
+                       int ex);
+int ocfs2_rename_lock(struct ocfs2_super *osb);
+void ocfs2_rename_unlock(struct ocfs2_super *osb);
+void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres);
+
+/* for the vote thread */
+void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
+                               struct ocfs2_lock_res *lockres);
+
+struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void);
+void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
+
+/* aids in debugging and tracking lvbs */
+void ocfs2_dump_meta_lvb_info(u64 level,
+                             const char *function,
+                             unsigned int line,
+                             struct ocfs2_lock_res *lockres);
+#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
+
+#endif /* DLMGLUE_H */
diff --git a/fs/ocfs2/endian.h b/fs/ocfs2/endian.h
new file mode 100644 (file)
index 0000000..f226b22
--- /dev/null
@@ -0,0 +1,45 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * Copyright (C) 2005 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef OCFS2_ENDIAN_H
+#define OCFS2_ENDIAN_H
+
+static inline void le16_add_cpu(__le16 *var, u16 val)
+{
+       *var = cpu_to_le16(le16_to_cpu(*var) + val);
+}
+
+static inline void le32_add_cpu(__le32 *var, u32 val)
+{
+       *var = cpu_to_le32(le32_to_cpu(*var) + val);
+}
+
+static inline void le32_and_cpu(__le32 *var, u32 val)
+{
+       *var = cpu_to_le32(le32_to_cpu(*var) & val);
+}
+
+static inline void be32_add_cpu(__be32 *var, u32 val)
+{
+       *var = cpu_to_be32(be32_to_cpu(*var) + val);
+}
+
+#endif /* OCFS2_ENDIAN_H */
diff --git a/fs/ocfs2/export.c b/fs/ocfs2/export.c
new file mode 100644 (file)
index 0000000..5810160
--- /dev/null
@@ -0,0 +1,248 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * export.c
+ *
+ * Functions to facilitate NFS exporting
+ *
+ * Copyright (C) 2002, 2005 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/types.h>
+
+#define MLOG_MASK_PREFIX ML_EXPORT
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "dir.h"
+#include "dlmglue.h"
+#include "export.h"
+#include "inode.h"
+
+#include "buffer_head_io.h"
+
+struct ocfs2_inode_handle
+{
+       u64 ih_blkno;
+       u32 ih_generation;
+};
+
+static struct dentry *ocfs2_get_dentry(struct super_block *sb, void *vobjp)
+{
+       struct ocfs2_inode_handle *handle = vobjp;
+       struct inode *inode;
+       struct dentry *result;
+
+       mlog_entry("(0x%p, 0x%p)\n", sb, handle);
+
+       if (handle->ih_blkno == 0) {
+               mlog_errno(-ESTALE);
+               return ERR_PTR(-ESTALE);
+       }
+
+       inode = ocfs2_iget(OCFS2_SB(sb), handle->ih_blkno);
+
+       if (IS_ERR(inode)) {
+               mlog_errno(PTR_ERR(inode));
+               return (void *)inode;
+       }
+
+       if (handle->ih_generation != inode->i_generation) {
+               iput(inode);
+               mlog_errno(-ESTALE);
+               return ERR_PTR(-ESTALE);
+       }
+
+       result = d_alloc_anon(inode);
+
+       if (!result) {
+               iput(inode);
+               mlog_errno(-ENOMEM);
+               return ERR_PTR(-ENOMEM);
+       }
+
+       mlog_exit_ptr(result);
+       return result;
+}
+
+static struct dentry *ocfs2_get_parent(struct dentry *child)
+{
+       int status;
+       u64 blkno;
+       struct dentry *parent;
+       struct inode *inode;
+       struct inode *dir = child->d_inode;
+       struct buffer_head *dirent_bh = NULL;
+       struct ocfs2_dir_entry *dirent;
+
+       mlog_entry("(0x%p, '%.*s')\n", child,
+                  child->d_name.len, child->d_name.name);
+
+       mlog(0, "find parent of directory %"MLFu64"\n",
+            OCFS2_I(dir)->ip_blkno);
+
+       status = ocfs2_meta_lock(dir, NULL, NULL, 0);
+       if (status < 0) {
+               if (status != -ENOENT)
+                       mlog_errno(status);
+               parent = ERR_PTR(status);
+               goto bail;
+       }
+
+       status = ocfs2_find_files_on_disk("..", 2, &blkno, dir, &dirent_bh,
+                                         &dirent);
+       if (status < 0) {
+               parent = ERR_PTR(-ENOENT);
+               goto bail_unlock;
+       }
+
+       inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno);
+       if (IS_ERR(inode)) {
+               mlog(ML_ERROR, "Unable to create inode %"MLFu64"\n", blkno);
+               parent = ERR_PTR(-EACCES);
+               goto bail_unlock;
+       }
+
+       parent = d_alloc_anon(inode);
+       if (!parent) {
+               iput(inode);
+               parent = ERR_PTR(-ENOMEM);
+       }
+
+bail_unlock:
+       ocfs2_meta_unlock(dir, 0);
+
+       if (dirent_bh)
+               brelse(dirent_bh);
+
+bail:
+       mlog_exit_ptr(parent);
+
+       return parent;
+}
+
+static int ocfs2_encode_fh(struct dentry *dentry, __be32 *fh, int *max_len,
+                          int connectable)
+{
+       struct inode *inode = dentry->d_inode;
+       int len = *max_len;
+       int type = 1;
+       u64 blkno;
+       u32 generation;
+
+       mlog_entry("(0x%p, '%.*s', 0x%p, %d, %d)\n", dentry,
+                  dentry->d_name.len, dentry->d_name.name,
+                  fh, len, connectable);
+
+       if (len < 3 || (connectable && len < 6)) {
+               mlog(ML_ERROR, "fh buffer is too small for encoding\n");
+               type = 255;
+               goto bail;
+       }
+
+       blkno = OCFS2_I(inode)->ip_blkno;
+       generation = inode->i_generation;
+
+       mlog(0, "Encoding fh: blkno: %"MLFu64", generation: %u\n",
+            blkno, generation);
+
+       len = 3;
+       fh[0] = cpu_to_le32((u32)(blkno >> 32));
+       fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
+       fh[2] = cpu_to_le32(generation);
+
+       if (connectable && !S_ISDIR(inode->i_mode)) {
+               struct inode *parent;
+
+               spin_lock(&dentry->d_lock);
+
+               parent = dentry->d_parent->d_inode;
+               blkno = OCFS2_I(parent)->ip_blkno;
+               generation = parent->i_generation;
+
+               fh[3] = cpu_to_le32((u32)(blkno >> 32));
+               fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
+               fh[5] = cpu_to_le32(generation);
+
+               spin_unlock(&dentry->d_lock);
+
+               len = 6;
+               type = 2;
+
+               mlog(0, "Encoding parent: blkno: %"MLFu64", generation: %u\n",
+                    blkno, generation);
+       }
+       
+       *max_len = len;
+
+bail:
+       mlog_exit(type);
+       return type;
+}
+
+static struct dentry *ocfs2_decode_fh(struct super_block *sb, __be32 *fh,
+                                     int fh_len, int fileid_type,
+                                     int (*acceptable)(void *context,
+                                                       struct dentry *de),
+                                     void *context)
+{
+       struct ocfs2_inode_handle handle, parent;
+       struct dentry *ret = NULL;
+
+       mlog_entry("(0x%p, 0x%p, %d, %d, 0x%p, 0x%p)\n",
+                  sb, fh, fh_len, fileid_type, acceptable, context);
+
+       if (fh_len < 3 || fileid_type > 2)
+               goto bail;
+
+       if (fileid_type == 2) {
+               if (fh_len < 6)
+                       goto bail;
+
+               parent.ih_blkno = (u64)le32_to_cpu(fh[3]) << 32;
+               parent.ih_blkno |= (u64)le32_to_cpu(fh[4]);
+               parent.ih_generation = le32_to_cpu(fh[5]);
+
+               mlog(0, "Decoding parent: blkno: %"MLFu64", generation: %u\n",
+                    parent.ih_blkno, parent.ih_generation);
+       }
+
+       handle.ih_blkno = (u64)le32_to_cpu(fh[0]) << 32;
+       handle.ih_blkno |= (u64)le32_to_cpu(fh[1]);
+       handle.ih_generation = le32_to_cpu(fh[2]);
+
+       mlog(0, "Encoding fh: blkno: %"MLFu64", generation: %u\n",
+            handle.ih_blkno, handle.ih_generation);
+
+       ret = ocfs2_export_ops.find_exported_dentry(sb, &handle, &parent,
+                                                   acceptable, context);
+
+bail:
+       mlog_exit_ptr(ret);
+       return ret;
+}
+
+struct export_operations ocfs2_export_ops = {
+       .decode_fh      = ocfs2_decode_fh,
+       .encode_fh      = ocfs2_encode_fh,
+
+       .get_parent     = ocfs2_get_parent,
+       .get_dentry     = ocfs2_get_dentry,
+};
diff --git a/fs/ocfs2/export.h b/fs/ocfs2/export.h
new file mode 100644 (file)
index 0000000..5b77ee7
--- /dev/null
@@ -0,0 +1,31 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * export.h
+ *
+ * Function prototypes
+ *
+ * Copyright (C) 2002, 2005 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef OCFS2_EXPORT_H
+#define OCFS2_EXPORT_H
+
+extern struct export_operations ocfs2_export_ops;
+
+#endif /* OCFS2_EXPORT_H */
diff --git a/fs/ocfs2/extent_map.c b/fs/ocfs2/extent_map.c
new file mode 100644 (file)
index 0000000..f2fb40c
--- /dev/null
@@ -0,0 +1,994 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * extent_map.c
+ *
+ * In-memory extent map for OCFS2.  Man, this code was prettier in
+ * the library.
+ *
+ * Copyright (C) 2004 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License, version 2,  as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/rbtree.h>
+
+#define MLOG_MASK_PREFIX ML_EXTENT_MAP
+#include <cluster/masklog.h>
+
+#include "ocfs2.h"
+
+#include "extent_map.h"
+#include "inode.h"
+#include "super.h"
+
+#include "buffer_head_io.h"
+
+
+/*
+ * SUCK SUCK SUCK
+ * Our headers are so bad that struct ocfs2_extent_map is in ocfs.h
+ */
+
+struct ocfs2_extent_map_entry {
+       struct rb_node e_node;
+       int e_tree_depth;
+       struct ocfs2_extent_rec e_rec;
+};
+
+struct ocfs2_em_insert_context {
+       int need_left;
+       int need_right;
+       struct ocfs2_extent_map_entry *new_ent;
+       struct ocfs2_extent_map_entry *old_ent;
+       struct ocfs2_extent_map_entry *left_ent;
+       struct ocfs2_extent_map_entry *right_ent;
+};
+
+static kmem_cache_t *ocfs2_em_ent_cachep = NULL;
+
+
+static struct ocfs2_extent_map_entry *
+ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
+                       u32 cpos, u32 clusters,
+                       struct rb_node ***ret_p,
+                       struct rb_node **ret_parent);
+static int ocfs2_extent_map_insert(struct inode *inode,
+                                  struct ocfs2_extent_rec *rec,
+                                  int tree_depth);
+static int ocfs2_extent_map_insert_entry(struct ocfs2_extent_map *em,
+                                        struct ocfs2_extent_map_entry *ent);
+static int ocfs2_extent_map_find_leaf(struct inode *inode,
+                                     u32 cpos, u32 clusters,
+                                     struct ocfs2_extent_list *el);
+static int ocfs2_extent_map_lookup_read(struct inode *inode,
+                                       u32 cpos, u32 clusters,
+                                       struct ocfs2_extent_map_entry **ret_ent);
+static int ocfs2_extent_map_try_insert(struct inode *inode,
+                                      struct ocfs2_extent_rec *rec,
+                                      int tree_depth,
+                                      struct ocfs2_em_insert_context *ctxt);
+
+/* returns 1 only if the rec contains all the given clusters -- that is that
+ * rec's cpos is <= the cluster cpos and that the rec endpoint (cpos +
+ * clusters) is >= the argument's endpoint */
+static int ocfs2_extent_rec_contains_clusters(struct ocfs2_extent_rec *rec,
+                                             u32 cpos, u32 clusters)
+{
+       if (le32_to_cpu(rec->e_cpos) > cpos)
+               return 0;
+       if (cpos + clusters > le32_to_cpu(rec->e_cpos) + 
+                             le32_to_cpu(rec->e_clusters))
+               return 0;
+       return 1;
+}
+
+
+/*
+ * Find an entry in the tree that intersects the region passed in.
+ * Note that this will find straddled intervals, it is up to the
+ * callers to enforce any boundary conditions.
+ *
+ * Callers must hold ip_lock.  This lookup is not guaranteed to return
+ * a tree_depth 0 match, and as such can race inserts if the lock
+ * were not held.
+ *
+ * The rb_node garbage lets insertion share the search.  Trivial
+ * callers pass NULL.
+ */
+static struct ocfs2_extent_map_entry *
+ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
+                       u32 cpos, u32 clusters,
+                       struct rb_node ***ret_p,
+                       struct rb_node **ret_parent)
+{
+       struct rb_node **p = &em->em_extents.rb_node;
+       struct rb_node *parent = NULL;
+       struct ocfs2_extent_map_entry *ent = NULL;
+
+       while (*p)
+       {
+               parent = *p;
+               ent = rb_entry(parent, struct ocfs2_extent_map_entry,
+                              e_node);
+               if ((cpos + clusters) <= le32_to_cpu(ent->e_rec.e_cpos)) {
+                       p = &(*p)->rb_left;
+                       ent = NULL;
+               } else if (cpos >= (le32_to_cpu(ent->e_rec.e_cpos) +
+                                   le32_to_cpu(ent->e_rec.e_clusters))) {
+                       p = &(*p)->rb_right;
+                       ent = NULL;
+               } else
+                       break;
+       }
+
+       if (ret_p != NULL)
+               *ret_p = p;
+       if (ret_parent != NULL)
+               *ret_parent = parent;
+       return ent;
+}
+
+/*
+ * Find the leaf containing the interval we want.  While we're on our
+ * way down the tree, fill in every record we see at any depth, because
+ * we might want it later.
+ *
+ * Note that this code is run without ip_lock.  That's because it
+ * sleeps while reading.  If someone is also filling the extent list at
+ * the same time we are, we might have to restart.
+ */
+static int ocfs2_extent_map_find_leaf(struct inode *inode,
+                                     u32 cpos, u32 clusters,
+                                     struct ocfs2_extent_list *el)
+{
+       int i, ret;
+       struct buffer_head *eb_bh = NULL;
+       u64 blkno;
+       u32 rec_end;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_extent_rec *rec;
+
+       /*
+        * The bh data containing the el cannot change here, because
+        * we hold alloc_sem.  So we can do this without other
+        * locks.
+        */
+       while (el->l_tree_depth)
+       {
+               blkno = 0;
+               for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
+                       rec = &el->l_recs[i];
+                       rec_end = (le32_to_cpu(rec->e_cpos) +
+                                  le32_to_cpu(rec->e_clusters));
+
+                       ret = -EBADR;
+                       if (rec_end > OCFS2_I(inode)->ip_clusters) {
+                               mlog_errno(ret);
+                               goto out_free;
+                       }
+
+                       if (rec_end <= cpos) {
+                               ret = ocfs2_extent_map_insert(inode, rec,
+                                               le16_to_cpu(el->l_tree_depth));
+                               if (ret && (ret != -EEXIST)) {
+                                       mlog_errno(ret);
+                                       goto out_free;
+                               }
+                               continue;
+                       }
+                       if ((cpos + clusters) <= le32_to_cpu(rec->e_cpos)) {
+                               ret = ocfs2_extent_map_insert(inode, rec,
+                                               le16_to_cpu(el->l_tree_depth));
+                               if (ret && (ret != -EEXIST)) {
+                                       mlog_errno(ret);
+                                       goto out_free;
+                               }
+                               continue;
+                       }
+
+                       /*
+                        * We've found a record that matches our
+                        * interval.  We don't insert it because we're
+                        * about to traverse it.
+                        */
+
+                       /* Check to see if we're stradling */
+                       ret = -ESRCH;
+                       if (!ocfs2_extent_rec_contains_clusters(rec,
+                                                               cpos,
+                                                               clusters)) {
+                               mlog_errno(ret);
+                               goto out_free;
+                       }
+
+                       /*
+                        * If we've already found a record, the el has
+                        * two records covering the same interval.
+                        * EEEK!
+                        */
+                       ret = -EBADR;
+                       if (blkno) {
+                               mlog_errno(ret);
+                               goto out_free;
+                       }
+
+                       blkno = le64_to_cpu(rec->e_blkno);
+               }
+
+               /*
+                * We don't support holes, and we're still up
+                * in the branches, so we'd better have found someone
+                */
+               ret = -EBADR;
+               if (!blkno) {
+                       mlog_errno(ret);
+                       goto out_free;
+               }
+
+               if (eb_bh) {
+                       brelse(eb_bh);
+                       eb_bh = NULL;
+               }
+               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
+                                      blkno, &eb_bh, OCFS2_BH_CACHED,
+                                      inode);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out_free;
+               }
+               eb = (struct ocfs2_extent_block *)eb_bh->b_data;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+                       ret = -EIO;
+                       goto out_free;
+               }
+               el = &eb->h_list;
+       }
+
+       if (el->l_tree_depth)
+               BUG();
+
+       for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
+               rec = &el->l_recs[i];
+               ret = ocfs2_extent_map_insert(inode, rec,
+                                             le16_to_cpu(el->l_tree_depth));
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out_free;
+               }
+       }
+
+       ret = 0;
+
+out_free:
+       if (eb_bh)
+               brelse(eb_bh);
+
+       return ret;
+}
+
+/*
+ * This lookup actually will read from disk.  It has one invariant:
+ * It will never re-traverse blocks.  This means that all inserts should
+ * be new regions or more granular regions (both allowed by insert).
+ */
+static int ocfs2_extent_map_lookup_read(struct inode *inode,
+                                       u32 cpos,
+                                       u32 clusters,
+                                       struct ocfs2_extent_map_entry **ret_ent)
+{
+       int ret;
+       u64 blkno;
+       struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
+       struct ocfs2_extent_map_entry *ent;
+       struct buffer_head *bh = NULL;
+       struct ocfs2_extent_block *eb;
+       struct ocfs2_dinode *di;
+       struct ocfs2_extent_list *el;
+
+       spin_lock(&OCFS2_I(inode)->ip_lock);
+       ent = ocfs2_extent_map_lookup(em, cpos, clusters, NULL, NULL);
+       if (ent) {
+               if (!ent->e_tree_depth) {
+                       spin_unlock(&OCFS2_I(inode)->ip_lock);
+                       *ret_ent = ent;
+                       return 0;
+               }
+               blkno = le64_to_cpu(ent->e_rec.e_blkno);
+               spin_unlock(&OCFS2_I(inode)->ip_lock);
+
+               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, &bh,
+                                      OCFS2_BH_CACHED, inode);
+               if (ret) {
+                       mlog_errno(ret);
+                       if (bh)
+                               brelse(bh);
+                       return ret;
+               }
+               eb = (struct ocfs2_extent_block *)bh->b_data;
+               if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
+                       OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
+                       brelse(bh);
+                       return -EIO;
+               }
+               el = &eb->h_list;
+       } else {
+               spin_unlock(&OCFS2_I(inode)->ip_lock);
+
+               ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
+                                      OCFS2_I(inode)->ip_blkno, &bh,
+                                      OCFS2_BH_CACHED, inode);
+               if (ret) {
+                       mlog_errno(ret);
+                       if (bh)
+                               brelse(bh);
+                       return ret;
+               }
+               di = (struct ocfs2_dinode *)bh->b_data;
+               if (!OCFS2_IS_VALID_DINODE(di)) {
+                       brelse(bh);
+                       OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, di);
+                       return -EIO;
+               }
+               el = &di->id2.i_list;
+       }
+
+       ret = ocfs2_extent_map_find_leaf(inode, cpos, clusters, el);
+       brelse(bh);
+       if (ret) {
+               mlog_errno(ret);
+               return ret;
+       }
+
+       ent = ocfs2_extent_map_lookup(em, cpos, clusters, NULL, NULL);
+       if (!ent) {
+               ret = -ESRCH;
+               mlog_errno(ret);
+               return ret;
+       }
+
+       if (ent->e_tree_depth)
+               BUG();  /* FIXME: Make sure this isn't a corruption */
+
+       *ret_ent = ent;
+
+       return 0;
+}
+
+/*
+ * Callers must hold ip_lock.  This can insert pieces of the tree,
+ * thus racing lookup if the lock weren't held.
+ */
+static int ocfs2_extent_map_insert_entry(struct ocfs2_extent_map *em,
+                                        struct ocfs2_extent_map_entry *ent)
+{
+       struct rb_node **p, *parent;
+       struct ocfs2_extent_map_entry *old_ent;
+
+       old_ent = ocfs2_extent_map_lookup(em, le32_to_cpu(ent->e_rec.e_cpos),
+                                         le32_to_cpu(ent->e_rec.e_clusters),
+                                         &p, &parent);
+       if (old_ent)
+               return -EEXIST;
+
+       rb_link_node(&ent->e_node, parent, p);
+       rb_insert_color(&ent->e_node, &em->em_extents);
+
+       return 0;
+}
+
+
+/*
+ * Simple rule: on any return code other than -EAGAIN, anything left
+ * in the insert_context will be freed.
+ */
+static int ocfs2_extent_map_try_insert(struct inode *inode,
+                                      struct ocfs2_extent_rec *rec,
+                                      int tree_depth,
+                                      struct ocfs2_em_insert_context *ctxt)
+{
+       int ret;
+       struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
+       struct ocfs2_extent_map_entry *old_ent;
+
+       ctxt->need_left = 0;
+       ctxt->need_right = 0;
+       ctxt->old_ent = NULL;
+
+       spin_lock(&OCFS2_I(inode)->ip_lock);
+       ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
+       if (!ret) {
+               ctxt->new_ent = NULL;
+               goto out_unlock;
+       }
+
+       old_ent = ocfs2_extent_map_lookup(em, le32_to_cpu(rec->e_cpos),
+                                         le32_to_cpu(rec->e_clusters), NULL,
+                                         NULL);
+
+       if (!old_ent)
+               BUG();
+
+       ret = -EEXIST;
+       if (old_ent->e_tree_depth < tree_depth)
+               goto out_unlock;
+
+       if (old_ent->e_tree_depth == tree_depth) {
+               if (!memcmp(rec, &old_ent->e_rec,
+                           sizeof(struct ocfs2_extent_rec)))
+                       ret = 0;
+
+               /* FIXME: Should this be ESRCH/EBADR??? */
+               goto out_unlock;
+       }
+
+       /*
+        * We do it in this order specifically so that no actual tree
+        * changes occur until we have all the pieces we need.  We
+        * don't want malloc failures to leave an inconsistent tree.
+        * Whenever we drop the lock, another process could be
+        * inserting.  Also note that, if another process just beat us
+        * to an insert, we might not need the same pieces we needed
+        * the first go round.  In the end, the pieces we need will
+        * be used, and the pieces we don't will be freed.
+        */
+       ctxt->need_left = !!(le32_to_cpu(rec->e_cpos) >
+                            le32_to_cpu(old_ent->e_rec.e_cpos));
+       ctxt->need_right = !!((le32_to_cpu(old_ent->e_rec.e_cpos) +
+                              le32_to_cpu(old_ent->e_rec.e_clusters)) >
+                             (le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)));
+       ret = -EAGAIN;
+       if (ctxt->need_left) {
+               if (!ctxt->left_ent)
+                       goto out_unlock;
+               *(ctxt->left_ent) = *old_ent;
+               ctxt->left_ent->e_rec.e_clusters =
+                       cpu_to_le32(le32_to_cpu(rec->e_cpos) -
+                                   le32_to_cpu(ctxt->left_ent->e_rec.e_cpos));
+       }
+       if (ctxt->need_right) {
+               if (!ctxt->right_ent)
+                       goto out_unlock;
+               *(ctxt->right_ent) = *old_ent;
+               ctxt->right_ent->e_rec.e_cpos =
+                       cpu_to_le32(le32_to_cpu(rec->e_cpos) +
+                                   le32_to_cpu(rec->e_clusters));
+               ctxt->right_ent->e_rec.e_clusters =
+                       cpu_to_le32((le32_to_cpu(old_ent->e_rec.e_cpos) +
+                                    le32_to_cpu(old_ent->e_rec.e_clusters)) -
+                                   le32_to_cpu(ctxt->right_ent->e_rec.e_cpos));
+       }
+
+       rb_erase(&old_ent->e_node, &em->em_extents);
+       /* Now that he's erased, set him up for deletion */
+       ctxt->old_ent = old_ent;
+
+       if (ctxt->need_left) {
+               ret = ocfs2_extent_map_insert_entry(em,
+                                                   ctxt->left_ent);
+               if (ret)
+                       goto out_unlock;
+               ctxt->left_ent = NULL;
+       }
+
+       if (ctxt->need_right) {
+               ret = ocfs2_extent_map_insert_entry(em,
+                                                   ctxt->right_ent);
+               if (ret)
+                       goto out_unlock;
+               ctxt->right_ent = NULL;
+       }
+
+       ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
+
+       if (!ret)
+               ctxt->new_ent = NULL;
+
+out_unlock:
+       spin_unlock(&OCFS2_I(inode)->ip_lock);
+
+       return ret;
+}
+
+
+static int ocfs2_extent_map_insert(struct inode *inode,
+                                  struct ocfs2_extent_rec *rec,
+                                  int tree_depth)
+{
+       int ret;
+       struct ocfs2_em_insert_context ctxt = {0, };
+
+       if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) >
+           OCFS2_I(inode)->ip_map.em_clusters) {
+               ret = -EBADR;
+               mlog_errno(ret);
+               return ret;
+       }
+
+       /* Zero e_clusters means a truncated tail record.  It better be EOF */
+       if (!rec->e_clusters) {
+               if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) !=
+                   OCFS2_I(inode)->ip_map.em_clusters) {
+                       ret = -EBADR;
+                       mlog_errno(ret);
+                       return ret;
+               }
+
+               /* Ignore the truncated tail */
+               return 0;
+       }
+
+       ret = -ENOMEM;
+       ctxt.new_ent = kmem_cache_alloc(ocfs2_em_ent_cachep,
+                                       GFP_KERNEL);
+       if (!ctxt.new_ent) {
+               mlog_errno(ret);
+               return ret;
+       }
+
+       ctxt.new_ent->e_rec = *rec;
+       ctxt.new_ent->e_tree_depth = tree_depth;
+
+       do {
+               ret = -ENOMEM;
+               if (ctxt.need_left && !ctxt.left_ent) {
+                       ctxt.left_ent =
+                               kmem_cache_alloc(ocfs2_em_ent_cachep,
+                                                GFP_KERNEL);
+                       if (!ctxt.left_ent)
+                               break;
+               }
+               if (ctxt.need_right && !ctxt.right_ent) {
+                       ctxt.right_ent =
+                               kmem_cache_alloc(ocfs2_em_ent_cachep,
+                                                GFP_KERNEL);
+                       if (!ctxt.right_ent)
+                               break;
+               }
+
+               ret = ocfs2_extent_map_try_insert(inode, rec,
+                                                 tree_depth, &ctxt);
+       } while (ret == -EAGAIN);
+
+       if (ret < 0)
+               mlog_errno(ret