exofs: Micro-optimize exofs_i_info
[safe/jmp/linux-2.6] / fs / exofs / inode.c
index a3691d8..c88a0c5 100644 (file)
@@ -1,8 +1,6 @@
 /*
  * Copyright (C) 2005, 2006
- * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
- * Copyright (C) 2005, 2006
- * International Business Machines
+ * Avishay Traeger (avishay@gmail.com)
  * Copyright (C) 2008, 2009
  * Boaz Harrosh <bharrosh@panasas.com>
  *
 
 #include "exofs.h"
 
-#ifdef CONFIG_EXOFS_DEBUG
-#  define EXOFS_DEBUG_OBJ_ISIZE 1
-#endif
+#define EXOFS_DBGMSG2(M...) do {} while (0)
+
+enum { BIO_MAX_PAGES_KMALLOC =
+               (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),
+};
 
 struct page_collect {
        struct exofs_sb_info *sbi;
        struct request_queue *req_q;
        struct inode *inode;
        unsigned expected_pages;
+       struct exofs_io_state *ios;
 
        struct bio *bio;
        unsigned nr_pages;
@@ -56,23 +57,23 @@ struct page_collect {
 };
 
 static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
-               struct inode *inode)
+                      struct inode *inode)
 {
        struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
-       struct request_queue *req_q = sbi->s_dev->scsi_device->request_queue;
 
        pcol->sbi = sbi;
-       pcol->req_q = req_q;
+       /* Create master bios on first Q, later on cloning, each clone will be
+        * allocated on it's destination Q
+        */
+       pcol->req_q = osd_request_queue(sbi->s_ods[0]);
        pcol->inode = inode;
        pcol->expected_pages = expected_pages;
 
+       pcol->ios = NULL;
        pcol->bio = NULL;
        pcol->nr_pages = 0;
        pcol->length = 0;
        pcol->pg_first = -1;
-
-       EXOFS_DBGMSG("_pcol_init ino=0x%lx expected_pages=%u\n", inode->i_ino,
-                    expected_pages);
 }
 
 static void _pcol_reset(struct page_collect *pcol)
@@ -83,35 +84,49 @@ static void _pcol_reset(struct page_collect *pcol)
        pcol->nr_pages = 0;
        pcol->length = 0;
        pcol->pg_first = -1;
-       EXOFS_DBGMSG("_pcol_reset ino=0x%lx expected_pages=%u\n",
-                    pcol->inode->i_ino, pcol->expected_pages);
+       pcol->ios = NULL;
 
        /* this is probably the end of the loop but in writes
         * it might not end here. don't be left with nothing
         */
        if (!pcol->expected_pages)
-               pcol->expected_pages = 128;
+               pcol->expected_pages = BIO_MAX_PAGES_KMALLOC;
 }
 
 static int pcol_try_alloc(struct page_collect *pcol)
 {
-       int pages = min_t(unsigned, pcol->expected_pages, BIO_MAX_PAGES);
+       int pages = min_t(unsigned, pcol->expected_pages,
+                         BIO_MAX_PAGES_KMALLOC);
+
+       if (!pcol->ios) { /* First time allocate io_state */
+               int ret = exofs_get_io_state(pcol->sbi, &pcol->ios);
+
+               if (ret)
+                       return ret;
+       }
 
        for (; pages; pages >>= 1) {
-               pcol->bio = bio_alloc(GFP_KERNEL, pages);
+               pcol->bio = bio_kmalloc(GFP_KERNEL, pages);
                if (likely(pcol->bio))
                        return 0;
        }
 
-       EXOFS_ERR("Failed to kcalloc expected_pages=%u\n",
+       EXOFS_ERR("Failed to bio_kmalloc expected_pages=%u\n",
                  pcol->expected_pages);
        return -ENOMEM;
 }
 
 static void pcol_free(struct page_collect *pcol)
 {
-       bio_put(pcol->bio);
-       pcol->bio = NULL;
+       if (pcol->bio) {
+               bio_put(pcol->bio);
+               pcol->bio = NULL;
+       }
+
+       if (pcol->ios) {
+               exofs_put_io_state(pcol->ios);
+               pcol->ios = NULL;
+       }
 }
 
 static int pcol_add_page(struct page_collect *pcol, struct page *page,
@@ -164,26 +179,21 @@ static void update_write_page(struct page *page, int ret)
 /* Called at the end of reads, to optionally unlock pages and update their
  * status.
  */
-static int __readpages_done(struct osd_request *or, struct page_collect *pcol,
-                           bool do_unlock)
+static int __readpages_done(struct page_collect *pcol, bool do_unlock)
 {
        struct bio_vec *bvec;
        int i;
        u64 resid;
        u64 good_bytes;
        u64 length = 0;
-       int ret = exofs_check_ok_resid(or, &resid, NULL);
-
-       osd_end_request(or);
+       int ret = exofs_check_io(pcol->ios, &resid);
 
        if (likely(!ret))
                good_bytes = pcol->length;
-       else if (!resid)
-               good_bytes = 0;
        else
                good_bytes = pcol->length - resid;
 
-       EXOFS_DBGMSG("readpages_done(0x%lx) good_bytes=0x%llx"
+       EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
                     " length=0x%lx nr_pages=%u\n",
                     pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
                     pcol->nr_pages);
@@ -201,7 +211,7 @@ static int __readpages_done(struct osd_request *or, struct page_collect *pcol,
                else
                        page_stat = ret;
 
-               EXOFS_DBGMSG("    readpages_done(0x%lx, 0x%lx) %s\n",
+               EXOFS_DBGMSG2("    readpages_done(0x%lx, 0x%lx) %s\n",
                          inode->i_ino, page->index,
                          page_stat ? "bad_bytes" : "good_bytes");
 
@@ -212,18 +222,18 @@ static int __readpages_done(struct osd_request *or, struct page_collect *pcol,
        }
 
        pcol_free(pcol);
-       EXOFS_DBGMSG("readpages_done END\n");
+       EXOFS_DBGMSG2("readpages_done END\n");
        return ret;
 }
 
 /* callback of async reads */
-static void readpages_done(struct osd_request *or, void *p)
+static void readpages_done(struct exofs_io_state *ios, void *p)
 {
        struct page_collect *pcol = p;
 
-       __readpages_done(or, pcol, true);
+       __readpages_done(pcol, true);
        atomic_dec(&pcol->sbi->s_curr_pending);
-       kfree(p);
+       kfree(pcol);
 }
 
 static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
@@ -241,17 +251,13 @@ static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
 
                unlock_page(page);
        }
-       pcol_free(pcol);
 }
 
 static int read_exec(struct page_collect *pcol, bool is_sync)
 {
        struct exofs_i_info *oi = exofs_i(pcol->inode);
-       struct osd_obj_id obj = {pcol->sbi->s_pid,
-                                       pcol->inode->i_ino + EXOFS_OBJ_OFF};
-       struct osd_request *or = NULL;
+       struct exofs_io_state *ios = pcol->ios;
        struct page_collect *pcol_copy = NULL;
-       loff_t i_start = pcol->pg_first << PAGE_CACHE_SHIFT;
        int ret;
 
        if (!pcol->bio)
@@ -260,17 +266,13 @@ static int read_exec(struct page_collect *pcol, bool is_sync)
        /* see comment in _readpage() about sync reads */
        WARN_ON(is_sync && (pcol->nr_pages != 1));
 
-       or = osd_start_request(pcol->sbi->s_dev, GFP_KERNEL);
-       if (unlikely(!or)) {
-               ret = -ENOMEM;
-               goto err;
-       }
-
-       osd_req_read(or, &obj, pcol->bio, i_start);
+       ios->bio = pcol->bio;
+       ios->length = pcol->length;
+       ios->offset = pcol->pg_first << PAGE_CACHE_SHIFT;
 
        if (is_sync) {
-               exofs_sync_op(or, pcol->sbi->s_timeout, oi->i_cred);
-               return __readpages_done(or, pcol, false);
+               exofs_oi_read(oi, pcol->ios);
+               return __readpages_done(pcol, false);
        }
 
        pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
@@ -280,14 +282,16 @@ static int read_exec(struct page_collect *pcol, bool is_sync)
        }
 
        *pcol_copy = *pcol;
-       ret = exofs_async_op(or, readpages_done, pcol_copy, oi->i_cred);
+       ios->done = readpages_done;
+       ios->private = pcol_copy;
+       ret = exofs_oi_read(oi, ios);
        if (unlikely(ret))
                goto err;
 
        atomic_inc(&pcol->sbi->s_curr_pending);
 
-       EXOFS_DBGMSG("read_exec obj=0x%llx start=0x%llx length=0x%lx\n",
-                 obj.id, _LLU(i_start), pcol->length);
+       EXOFS_DBGMSG2("read_exec obj=0x%llx start=0x%llx length=0x%lx\n",
+                 ios->obj.id, _LLU(ios->offset), pcol->length);
 
        /* pages ownership was passed to pcol_copy */
        _pcol_reset(pcol);
@@ -296,9 +300,10 @@ static int read_exec(struct page_collect *pcol, bool is_sync)
 err:
        if (!is_sync)
                _unlock_pcol_pages(pcol, ret, READ);
+
+       pcol_free(pcol);
+
        kfree(pcol_copy);
-       if (or)
-               osd_end_request(or);
        return ret;
 }
 
@@ -370,12 +375,12 @@ try_again:
        if (len != PAGE_CACHE_SIZE)
                zero_user(page, len, PAGE_CACHE_SIZE - len);
 
-       EXOFS_DBGMSG("    readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
+       EXOFS_DBGMSG2("    readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
                     inode->i_ino, page->index, len);
 
        ret = pcol_add_page(pcol, page, len);
        if (ret) {
-               EXOFS_DBGMSG("Failed pcol_add_page pages[i]=%p "
+               EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
                          "this_len=0x%zx nr_pages=%u length=0x%lx\n",
                          page, len, pcol->nr_pages, pcol->length);
 
@@ -419,9 +424,8 @@ static int _readpage(struct page *page, bool is_sync)
 
        _pcol_init(&pcol, 1, page->mapping->host);
 
-       /* readpage_strip might call read_exec(,async) inside at several places
-        * but this is safe for is_async=0 since read_exec will not do anything
-        * when we have a single page.
+       /* readpage_strip might call read_exec(,is_sync==false) at several
+        * places but not if we have a single page.
         */
        ret = readpage_strip(&pcol, page);
        if (ret) {
@@ -440,8 +444,8 @@ static int exofs_readpage(struct file *file, struct page *page)
        return _readpage(page, false);
 }
 
-/* Callback for osd_write. All writes are asynchronouse */
-static void writepages_done(struct osd_request *or, void *p)
+/* Callback for osd_write. All writes are asynchronous */
+static void writepages_done(struct exofs_io_state *ios, void *p)
 {
        struct page_collect *pcol = p;
        struct bio_vec *bvec;
@@ -449,20 +453,16 @@ static void writepages_done(struct osd_request *or, void *p)
        u64 resid;
        u64  good_bytes;
        u64  length = 0;
+       int ret = exofs_check_io(ios, &resid);
 
-       int ret = exofs_check_ok_resid(or, NULL, &resid);
-
-       osd_end_request(or);
        atomic_dec(&pcol->sbi->s_curr_pending);
 
        if (likely(!ret))
                good_bytes = pcol->length;
-       else if (!resid)
-               good_bytes = 0;
        else
                good_bytes = pcol->length - resid;
 
-       EXOFS_DBGMSG("writepages_done(0x%lx) good_bytes=0x%llx"
+       EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
                     " length=0x%lx nr_pages=%u\n",
                     pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
                     pcol->nr_pages);
@@ -482,7 +482,7 @@ static void writepages_done(struct osd_request *or, void *p)
 
                update_write_page(page, page_stat);
                unlock_page(page);
-               EXOFS_DBGMSG("    writepages_done(0x%lx, 0x%lx) status=%d\n",
+               EXOFS_DBGMSG2("    writepages_done(0x%lx, 0x%lx) status=%d\n",
                             inode->i_ino, page->index, page_stat);
 
                length += bvec->bv_len;
@@ -490,29 +490,19 @@ static void writepages_done(struct osd_request *or, void *p)
 
        pcol_free(pcol);
        kfree(pcol);
-       EXOFS_DBGMSG("writepages_done END\n");
+       EXOFS_DBGMSG2("writepages_done END\n");
 }
 
 static int write_exec(struct page_collect *pcol)
 {
        struct exofs_i_info *oi = exofs_i(pcol->inode);
-       struct osd_obj_id obj = {pcol->sbi->s_pid,
-                                       pcol->inode->i_ino + EXOFS_OBJ_OFF};
-       struct osd_request *or = NULL;
+       struct exofs_io_state *ios = pcol->ios;
        struct page_collect *pcol_copy = NULL;
-       loff_t i_start = pcol->pg_first << PAGE_CACHE_SHIFT;
        int ret;
 
        if (!pcol->bio)
                return 0;
 
-       or = osd_start_request(pcol->sbi->s_dev, GFP_KERNEL);
-       if (unlikely(!or)) {
-               EXOFS_ERR("write_exec: Faild to osd_start_request()\n");
-               ret = -ENOMEM;
-               goto err;
-       }
-
        pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
        if (!pcol_copy) {
                EXOFS_ERR("write_exec: Faild to kmalloc(pcol)\n");
@@ -522,16 +512,23 @@ static int write_exec(struct page_collect *pcol)
 
        *pcol_copy = *pcol;
 
-       osd_req_write(or, &obj, pcol_copy->bio, i_start);
-       ret = exofs_async_op(or, writepages_done, pcol_copy, oi->i_cred);
+       pcol_copy->bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
+
+       ios->bio = pcol_copy->bio;
+       ios->offset = pcol_copy->pg_first << PAGE_CACHE_SHIFT;
+       ios->length = pcol_copy->length;
+       ios->done = writepages_done;
+       ios->private = pcol_copy;
+
+       ret = exofs_oi_write(oi, ios);
        if (unlikely(ret)) {
-               EXOFS_ERR("write_exec: exofs_async_op() Faild\n");
+               EXOFS_ERR("write_exec: exofs_oi_write() Faild\n");
                goto err;
        }
 
        atomic_inc(&pcol->sbi->s_curr_pending);
-       EXOFS_DBGMSG("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n",
-                 pcol->inode->i_ino, pcol->pg_first, _LLU(i_start),
+       EXOFS_DBGMSG2("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n",
+                 pcol->inode->i_ino, pcol->pg_first, _LLU(ios->offset),
                  pcol->length);
        /* pages ownership was passed to pcol_copy */
        _pcol_reset(pcol);
@@ -539,9 +536,9 @@ static int write_exec(struct page_collect *pcol)
 
 err:
        _unlock_pcol_pages(pcol, ret, WRITE);
+       pcol_free(pcol);
        kfree(pcol_copy);
-       if (or)
-               osd_end_request(or);
+
        return ret;
 }
 
@@ -585,6 +582,9 @@ static int writepage_strip(struct page *page,
                        if (PageError(page))
                                ClearPageError(page);
                        unlock_page(page);
+                       EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
+                                    "outside the limits\n",
+                                    inode->i_ino, page->index);
                        return 0;
                }
        }
@@ -599,6 +599,9 @@ try_again:
                ret = write_exec(pcol);
                if (unlikely(ret))
                        goto fail;
+
+               EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
+                            inode->i_ino, page->index);
                goto try_again;
        }
 
@@ -608,12 +611,12 @@ try_again:
                        goto fail;
        }
 
-       EXOFS_DBGMSG("    writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
+       EXOFS_DBGMSG2("    writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
                     inode->i_ino, page->index, len);
 
        ret = pcol_add_page(pcol, page, len);
        if (unlikely(ret)) {
-               EXOFS_DBGMSG("Failed pcol_add_page "
+               EXOFS_DBGMSG2("Failed pcol_add_page "
                             "nr_pages=%u total_length=0x%lx\n",
                             pcol->nr_pages, pcol->length);
 
@@ -633,6 +636,8 @@ try_again:
        return 0;
 
 fail:
+       EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
+                    inode->i_ino, page->index, ret);
        set_bit(AS_EIO, &page->mapping->flags);
        unlock_page(page);
        return ret;
@@ -651,14 +656,17 @@ static int exofs_writepages(struct address_space *mapping,
                        wbc->range_end >> PAGE_CACHE_SHIFT;
 
        if (start || end)
-               expected_pages = min(end - start + 1, 32L);
+               expected_pages = end - start + 1;
        else
                expected_pages = mapping->nrpages;
 
-       EXOFS_DBGMSG("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx"
-                    " m->nrpages=%lu start=0x%lx end=0x%lx\n",
+       if (expected_pages < 32L)
+               expected_pages = 32L;
+
+       EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
+                    "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
                     mapping->host->i_ino, wbc->range_start, wbc->range_end,
-                    mapping->nrpages, start, end);
+                    mapping->nrpages, start, end, expected_pages);
 
        _pcol_init(&pcol, expected_pages, mapping->host);
 
@@ -730,13 +738,28 @@ static int exofs_write_begin_export(struct file *file,
                                        fsdata);
 }
 
+static int exofs_write_end(struct file *file, struct address_space *mapping,
+                       loff_t pos, unsigned len, unsigned copied,
+                       struct page *page, void *fsdata)
+{
+       struct inode *inode = mapping->host;
+       /* According to comment in simple_write_end i_mutex is held */
+       loff_t i_size = inode->i_size;
+       int ret;
+
+       ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
+       if (i_size != inode->i_size)
+               mark_inode_dirty(inode);
+       return ret;
+}
+
 const struct address_space_operations exofs_aops = {
        .readpage       = exofs_readpage,
        .readpages      = exofs_readpages,
        .writepage      = exofs_writepage,
        .writepages     = exofs_writepages,
        .write_begin    = exofs_write_begin_export,
-       .write_end      = simple_write_end,
+       .write_end      = exofs_write_end,
 };
 
 /******************************************************************************
@@ -770,19 +793,28 @@ static int exofs_get_block(struct inode *inode, sector_t iblock,
 const struct osd_attr g_attr_logical_length = ATTR_DEF(
        OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
 
+static int _do_truncate(struct inode *inode)
+{
+       struct exofs_i_info *oi = exofs_i(inode);
+       loff_t isize = i_size_read(inode);
+       int ret;
+
+       inode->i_mtime = inode->i_ctime = CURRENT_TIME;
+
+       nobh_truncate_page(inode->i_mapping, isize, exofs_get_block);
+
+       ret = exofs_oi_truncate(oi, (u64)isize);
+       EXOFS_DBGMSG("(0x%lx) size=0x%llx\n", inode->i_ino, isize);
+       return ret;
+}
+
 /*
  * Truncate a file to the specified size - all we have to do is set the size
  * attribute.  We make sure the object exists first.
  */
 void exofs_truncate(struct inode *inode)
 {
-       struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
        struct exofs_i_info *oi = exofs_i(inode);
-       struct osd_obj_id obj = {sbi->s_pid, inode->i_ino + EXOFS_OBJ_OFF};
-       struct osd_request *or;
-       struct osd_attr attr;
-       loff_t isize = i_size_read(inode);
-       __be64 newsize;
        int ret;
 
        if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
@@ -792,22 +824,6 @@ void exofs_truncate(struct inode *inode)
                return;
        if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
                return;
-       inode->i_mtime = inode->i_ctime = CURRENT_TIME;
-
-       nobh_truncate_page(inode->i_mapping, isize, exofs_get_block);
-
-       or = osd_start_request(sbi->s_dev, GFP_KERNEL);
-       if (unlikely(!or)) {
-               EXOFS_ERR("ERROR: exofs_truncate: osd_start_request failed\n");
-               goto fail;
-       }
-
-       osd_req_set_attributes(or, &obj);
-
-       newsize = cpu_to_be64((u64)isize);
-       attr = g_attr_logical_length;
-       attr.val_ptr = &newsize;
-       osd_req_add_set_attr_list(or, &attr, 1);
 
        /* if we are about to truncate an object, and it hasn't been
         * created yet, wait
@@ -815,8 +831,7 @@ void exofs_truncate(struct inode *inode)
        if (unlikely(wait_obj_created(oi)))
                goto fail;
 
-       ret = exofs_sync_op(or, sbi->s_timeout, oi->i_cred);
-       osd_end_request(or);
+       ret = _do_truncate(inode);
        if (ret)
                goto fail;
 
@@ -843,3 +858,460 @@ int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
        error = inode_setattr(inode, iattr);
        return error;
 }
+
+/*
+ * Read an inode from the OSD, and return it as is.  We also return the size
+ * attribute in the 'obj_size' argument.
+ */
+static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
+                   struct exofs_fcb *inode, uint64_t *obj_size)
+{
+       struct exofs_sb_info *sbi = sb->s_fs_info;
+       struct osd_attr attrs[2];
+       struct exofs_io_state *ios;
+       int ret;
+
+       *obj_size = ~0;
+       ret = exofs_get_io_state(sbi, &ios);
+       if (unlikely(ret)) {
+               EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
+               return ret;
+       }
+
+       ios->obj.id = exofs_oi_objno(oi);
+       exofs_make_credential(oi->i_cred, &ios->obj);
+       ios->cred = oi->i_cred;
+
+       attrs[0] = g_attr_inode_data;
+       attrs[1] = g_attr_logical_length;
+       ios->in_attr = attrs;
+       ios->in_attr_len = ARRAY_SIZE(attrs);
+
+       ret = exofs_sbi_read(ios);
+       if (ret)
+               goto out;
+
+       ret = extract_attr_from_ios(ios, &attrs[0]);
+       if (ret) {
+               EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
+               goto out;
+       }
+       WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
+       memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
+
+       ret = extract_attr_from_ios(ios, &attrs[1]);
+       if (ret) {
+               EXOFS_ERR("%s: extract_attr of logical_length failed\n",
+                         __func__);
+               goto out;
+       }
+       *obj_size = get_unaligned_be64(attrs[1].val_ptr);
+
+out:
+       exofs_put_io_state(ios);
+       return ret;
+}
+
+static void __oi_init(struct exofs_i_info *oi)
+{
+       init_waitqueue_head(&oi->i_wq);
+       oi->i_flags = 0;
+}
+/*
+ * Fill in an inode read from the OSD and set it up for use
+ */
+struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
+{
+       struct exofs_i_info *oi;
+       struct exofs_fcb fcb;
+       struct inode *inode;
+       uint64_t obj_size;
+       int ret;
+
+       inode = iget_locked(sb, ino);
+       if (!inode)
+               return ERR_PTR(-ENOMEM);
+       if (!(inode->i_state & I_NEW))
+               return inode;
+       oi = exofs_i(inode);
+       __oi_init(oi);
+
+       /* read the inode from the osd */
+       ret = exofs_get_inode(sb, oi, &fcb, &obj_size);
+       if (ret)
+               goto bad_inode;
+
+       set_obj_created(oi);
+
+       /* copy stuff from on-disk struct to in-memory struct */
+       inode->i_mode = le16_to_cpu(fcb.i_mode);
+       inode->i_uid = le32_to_cpu(fcb.i_uid);
+       inode->i_gid = le32_to_cpu(fcb.i_gid);
+       inode->i_nlink = le16_to_cpu(fcb.i_links_count);
+       inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
+       inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
+       inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
+       inode->i_ctime.tv_nsec =
+               inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
+       oi->i_commit_size = le64_to_cpu(fcb.i_size);
+       i_size_write(inode, oi->i_commit_size);
+       inode->i_blkbits = EXOFS_BLKSHIFT;
+       inode->i_generation = le32_to_cpu(fcb.i_generation);
+
+       if ((inode->i_size != obj_size) &&
+               (!exofs_inode_is_fast_symlink(inode))) {
+               EXOFS_ERR("WARNING: Size of inode=%llu != object=%llu\n",
+                         inode->i_size, _LLU(obj_size));
+               /* FIXME: call exofs_inode_recovery() */
+       }
+
+       oi->i_dir_start_lookup = 0;
+
+       if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
+               ret = -ESTALE;
+               goto bad_inode;
+       }
+
+       if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
+               if (fcb.i_data[0])
+                       inode->i_rdev =
+                               old_decode_dev(le32_to_cpu(fcb.i_data[0]));
+               else
+                       inode->i_rdev =
+                               new_decode_dev(le32_to_cpu(fcb.i_data[1]));
+       } else {
+               memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
+       }
+
+       if (S_ISREG(inode->i_mode)) {
+               inode->i_op = &exofs_file_inode_operations;
+               inode->i_fop = &exofs_file_operations;
+               inode->i_mapping->a_ops = &exofs_aops;
+       } else if (S_ISDIR(inode->i_mode)) {
+               inode->i_op = &exofs_dir_inode_operations;
+               inode->i_fop = &exofs_dir_operations;
+               inode->i_mapping->a_ops = &exofs_aops;
+       } else if (S_ISLNK(inode->i_mode)) {
+               if (exofs_inode_is_fast_symlink(inode))
+                       inode->i_op = &exofs_fast_symlink_inode_operations;
+               else {
+                       inode->i_op = &exofs_symlink_inode_operations;
+                       inode->i_mapping->a_ops = &exofs_aops;
+               }
+       } else {
+               inode->i_op = &exofs_special_inode_operations;
+               if (fcb.i_data[0])
+                       init_special_inode(inode, inode->i_mode,
+                          old_decode_dev(le32_to_cpu(fcb.i_data[0])));
+               else
+                       init_special_inode(inode, inode->i_mode,
+                          new_decode_dev(le32_to_cpu(fcb.i_data[1])));
+       }
+
+       unlock_new_inode(inode);
+       return inode;
+
+bad_inode:
+       iget_failed(inode);
+       return ERR_PTR(ret);
+}
+
+int __exofs_wait_obj_created(struct exofs_i_info *oi)
+{
+       if (!obj_created(oi)) {
+               BUG_ON(!obj_2bcreated(oi));
+               wait_event(oi->i_wq, obj_created(oi));
+       }
+       return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
+}
+/*
+ * Callback function from exofs_new_inode().  The important thing is that we
+ * set the obj_created flag so that other methods know that the object exists on
+ * the OSD.
+ */
+static void create_done(struct exofs_io_state *ios, void *p)
+{
+       struct inode *inode = p;
+       struct exofs_i_info *oi = exofs_i(inode);
+       struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
+       int ret;
+
+       ret = exofs_check_io(ios, NULL);
+       exofs_put_io_state(ios);
+
+       atomic_dec(&sbi->s_curr_pending);
+
+       if (unlikely(ret)) {
+               EXOFS_ERR("object=0x%llx creation faild in pid=0x%llx",
+                         _LLU(exofs_oi_objno(oi)), _LLU(sbi->s_pid));
+               /*TODO: When FS is corrupted creation can fail, object already
+                * exist. Get rid of this asynchronous creation, if exist
+                * increment the obj counter and try the next object. Until we
+                * succeed. All these dangling objects will be made into lost
+                * files by chkfs.exofs
+                */
+       }
+
+       set_obj_created(oi);
+
+       atomic_dec(&inode->i_count);
+       wake_up(&oi->i_wq);
+}
+
+/*
+ * Set up a new inode and create an object for it on the OSD
+ */
+struct inode *exofs_new_inode(struct inode *dir, int mode)
+{
+       struct super_block *sb;
+       struct inode *inode;
+       struct exofs_i_info *oi;
+       struct exofs_sb_info *sbi;
+       struct exofs_io_state *ios;
+       int ret;
+
+       sb = dir->i_sb;
+       inode = new_inode(sb);
+       if (!inode)
+               return ERR_PTR(-ENOMEM);
+
+       oi = exofs_i(inode);
+       __oi_init(oi);
+
+       set_obj_2bcreated(oi);
+
+       sbi = sb->s_fs_info;
+
+       sb->s_dirt = 1;
+       inode->i_uid = current->cred->fsuid;
+       if (dir->i_mode & S_ISGID) {
+               inode->i_gid = dir->i_gid;
+               if (S_ISDIR(mode))
+                       mode |= S_ISGID;
+       } else {
+               inode->i_gid = current->cred->fsgid;
+       }
+       inode->i_mode = mode;
+
+       inode->i_ino = sbi->s_nextid++;
+       inode->i_blkbits = EXOFS_BLKSHIFT;
+       inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
+       oi->i_commit_size = inode->i_size = 0;
+       spin_lock(&sbi->s_next_gen_lock);
+       inode->i_generation = sbi->s_next_generation++;
+       spin_unlock(&sbi->s_next_gen_lock);
+       insert_inode_hash(inode);
+
+       mark_inode_dirty(inode);
+
+       ret = exofs_get_io_state(sbi, &ios);
+       if (unlikely(ret)) {
+               EXOFS_ERR("exofs_new_inode: exofs_get_io_state failed\n");
+               return ERR_PTR(ret);
+       }
+
+       ios->obj.id = exofs_oi_objno(oi);
+       exofs_make_credential(oi->i_cred, &ios->obj);
+
+       /* increment the refcount so that the inode will still be around when we
+        * reach the callback
+        */
+       atomic_inc(&inode->i_count);
+
+       ios->done = create_done;
+       ios->private = inode;
+       ios->cred = oi->i_cred;
+       ret = exofs_sbi_create(ios);
+       if (ret) {
+               atomic_dec(&inode->i_count);
+               exofs_put_io_state(ios);
+               return ERR_PTR(ret);
+       }
+       atomic_inc(&sbi->s_curr_pending);
+
+       return inode;
+}
+
+/*
+ * struct to pass two arguments to update_inode's callback
+ */
+struct updatei_args {
+       struct exofs_sb_info    *sbi;
+       struct exofs_fcb        fcb;
+};
+
+/*
+ * Callback function from exofs_update_inode().
+ */
+static void updatei_done(struct exofs_io_state *ios, void *p)
+{
+       struct updatei_args *args = p;
+
+       exofs_put_io_state(ios);
+
+       atomic_dec(&args->sbi->s_curr_pending);
+
+       kfree(args);
+}
+
+/*
+ * Write the inode to the OSD.  Just fill up the struct, and set the attribute
+ * synchronously or asynchronously depending on the do_sync flag.
+ */
+static int exofs_update_inode(struct inode *inode, int do_sync)
+{
+       struct exofs_i_info *oi = exofs_i(inode);
+       struct super_block *sb = inode->i_sb;
+       struct exofs_sb_info *sbi = sb->s_fs_info;
+       struct exofs_io_state *ios;
+       struct osd_attr attr;
+       struct exofs_fcb *fcb;
+       struct updatei_args *args;
+       int ret;
+
+       args = kzalloc(sizeof(*args), GFP_KERNEL);
+       if (!args) {
+               EXOFS_DBGMSG("Faild kzalloc of args\n");
+               return -ENOMEM;
+       }
+
+       fcb = &args->fcb;
+
+       fcb->i_mode = cpu_to_le16(inode->i_mode);
+       fcb->i_uid = cpu_to_le32(inode->i_uid);
+       fcb->i_gid = cpu_to_le32(inode->i_gid);
+       fcb->i_links_count = cpu_to_le16(inode->i_nlink);
+       fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
+       fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
+       fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
+       oi->i_commit_size = i_size_read(inode);
+       fcb->i_size = cpu_to_le64(oi->i_commit_size);
+       fcb->i_generation = cpu_to_le32(inode->i_generation);
+
+       if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
+               if (old_valid_dev(inode->i_rdev)) {
+                       fcb->i_data[0] =
+                               cpu_to_le32(old_encode_dev(inode->i_rdev));
+                       fcb->i_data[1] = 0;
+               } else {
+                       fcb->i_data[0] = 0;
+                       fcb->i_data[1] =
+                               cpu_to_le32(new_encode_dev(inode->i_rdev));
+                       fcb->i_data[2] = 0;
+               }
+       } else
+               memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
+
+       ret = exofs_get_io_state(sbi, &ios);
+       if (unlikely(ret)) {
+               EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
+               goto free_args;
+       }
+
+       attr = g_attr_inode_data;
+       attr.val_ptr = fcb;
+       ios->out_attr_len = 1;
+       ios->out_attr = &attr;
+
+       if (!obj_created(oi)) {
+               EXOFS_DBGMSG("!obj_created\n");
+               BUG_ON(!obj_2bcreated(oi));
+               wait_event(oi->i_wq, obj_created(oi));
+               EXOFS_DBGMSG("wait_event done\n");
+       }
+
+       if (!do_sync) {
+               args->sbi = sbi;
+               ios->done = updatei_done;
+               ios->private = args;
+       }
+
+       ret = exofs_oi_write(oi, ios);
+       if (!do_sync && !ret) {
+               atomic_inc(&sbi->s_curr_pending);
+               goto out; /* deallocation in updatei_done */
+       }
+
+       exofs_put_io_state(ios);
+free_args:
+       kfree(args);
+out:
+       EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
+                    inode->i_ino, do_sync, ret);
+       return ret;
+}
+
+int exofs_write_inode(struct inode *inode, int wait)
+{
+       return exofs_update_inode(inode, wait);
+}
+
+/*
+ * Callback function from exofs_delete_inode() - don't have much cleaning up to
+ * do.
+ */
+static void delete_done(struct exofs_io_state *ios, void *p)
+{
+       struct exofs_sb_info *sbi = p;
+
+       exofs_put_io_state(ios);
+
+       atomic_dec(&sbi->s_curr_pending);
+}
+
+/*
+ * Called when the refcount of an inode reaches zero.  We remove the object
+ * from the OSD here.  We make sure the object was created before we try and
+ * delete it.
+ */
+void exofs_delete_inode(struct inode *inode)
+{
+       struct exofs_i_info *oi = exofs_i(inode);
+       struct super_block *sb = inode->i_sb;
+       struct exofs_sb_info *sbi = sb->s_fs_info;
+       struct exofs_io_state *ios;
+       int ret;
+
+       truncate_inode_pages(&inode->i_data, 0);
+
+       if (is_bad_inode(inode))
+               goto no_delete;
+
+       mark_inode_dirty(inode);
+       exofs_update_inode(inode, inode_needs_sync(inode));
+
+       inode->i_size = 0;
+       if (inode->i_blocks)
+               exofs_truncate(inode);
+
+       clear_inode(inode);
+
+       ret = exofs_get_io_state(sbi, &ios);
+       if (unlikely(ret)) {
+               EXOFS_ERR("%s: exofs_get_io_state failed\n", __func__);
+               return;
+       }
+
+       /* if we are deleting an obj that hasn't been created yet, wait */
+       if (!obj_created(oi)) {
+               BUG_ON(!obj_2bcreated(oi));
+               wait_event(oi->i_wq, obj_created(oi));
+       }
+
+       ios->obj.id = exofs_oi_objno(oi);
+       ios->done = delete_done;
+       ios->private = sbi;
+       ios->cred = oi->i_cred;
+       ret = exofs_sbi_remove(ios);
+       if (ret) {
+               EXOFS_ERR("%s: exofs_sbi_remove failed\n", __func__);
+               exofs_put_io_state(ios);
+               return;
+       }
+       atomic_inc(&sbi->s_curr_pending);
+
+       return;
+
+no_delete:
+       clear_inode(inode);
+}