exofs: dbg-print less
[linux-2.6.git] / fs / exofs / inode.c
index a3691d8..01fa798 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>
  *
@@ -43,6 +41,8 @@
 #  define EXOFS_DEBUG_OBJ_ISIZE 1
 #endif
 
+#define EXOFS_DBGMSG2(M...) do {} while (0)
+
 struct page_collect {
        struct exofs_sb_info *sbi;
        struct request_queue *req_q;
@@ -59,10 +59,9 @@ static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
                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;
+       pcol->req_q = osd_request_queue(sbi->s_dev);
        pcol->inode = inode;
        pcol->expected_pages = expected_pages;
 
@@ -201,7 +200,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");
 
@@ -266,7 +265,7 @@ static int read_exec(struct page_collect *pcol, bool is_sync)
                goto err;
        }
 
-       osd_req_read(or, &obj, pcol->bio, i_start);
+       osd_req_read(or, &obj, i_start, pcol->bio, pcol->length);
 
        if (is_sync) {
                exofs_sync_op(or, pcol->sbi->s_timeout, oi->i_cred);
@@ -296,6 +295,9 @@ static int read_exec(struct page_collect *pcol, bool is_sync)
 err:
        if (!is_sync)
                _unlock_pcol_pages(pcol, ret, READ);
+       else /* Pages unlocked by caller in sync mode only free bio */
+               pcol_free(pcol);
+
        kfree(pcol_copy);
        if (or)
                osd_end_request(or);
@@ -370,12 +372,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);
 
@@ -482,7 +484,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;
@@ -522,7 +524,8 @@ static int write_exec(struct page_collect *pcol)
 
        *pcol_copy = *pcol;
 
-       osd_req_write(or, &obj, pcol_copy->bio, i_start);
+       pcol_copy->bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
+       osd_req_write(or, &obj, i_start, pcol_copy->bio, pcol_copy->length);
        ret = exofs_async_op(or, writepages_done, pcol_copy, oi->i_cred);
        if (unlikely(ret)) {
                EXOFS_ERR("write_exec: exofs_async_op() Faild\n");
@@ -608,7 +611,7 @@ 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);
@@ -843,3 +846,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 'sanity' argument if we got compiled with debugging turned
+ * on.
+ */
+static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
+                   struct exofs_fcb *inode, uint64_t *sanity)
+{
+       struct exofs_sb_info *sbi = sb->s_fs_info;
+       struct osd_request *or;
+       struct osd_attr attr;
+       struct osd_obj_id obj = {sbi->s_pid,
+                                oi->vfs_inode.i_ino + EXOFS_OBJ_OFF};
+       int ret;
+
+       exofs_make_credential(oi->i_cred, &obj);
+
+       or = osd_start_request(sbi->s_dev, GFP_KERNEL);
+       if (unlikely(!or)) {
+               EXOFS_ERR("exofs_get_inode: osd_start_request failed.\n");
+               return -ENOMEM;
+       }
+       osd_req_get_attributes(or, &obj);
+
+       /* we need the inode attribute */
+       osd_req_add_get_attr_list(or, &g_attr_inode_data, 1);
+
+#ifdef EXOFS_DEBUG_OBJ_ISIZE
+       /* we get the size attributes to do a sanity check */
+       osd_req_add_get_attr_list(or, &g_attr_logical_length, 1);
+#endif
+
+       ret = exofs_sync_op(or, sbi->s_timeout, oi->i_cred);
+       if (ret)
+               goto out;
+
+       attr = g_attr_inode_data;
+       ret = extract_attr_from_req(or, &attr);
+       if (ret) {
+               EXOFS_ERR("exofs_get_inode: extract_attr_from_req failed\n");
+               goto out;
+       }
+
+       WARN_ON(attr.len != EXOFS_INO_ATTR_SIZE);
+       memcpy(inode, attr.val_ptr, EXOFS_INO_ATTR_SIZE);
+
+#ifdef EXOFS_DEBUG_OBJ_ISIZE
+       attr = g_attr_logical_length;
+       ret = extract_attr_from_req(or, &attr);
+       if (ret) {
+               EXOFS_ERR("ERROR: extract attr from or failed\n");
+               goto out;
+       }
+       *sanity = get_unaligned_be64(attr.val_ptr);
+#endif
+
+out:
+       osd_end_request(or);
+       return ret;
+}
+
+/*
+ * 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 uninitialized_var(sanity);
+       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);
+
+       /* read the inode from the osd */
+       ret = exofs_get_inode(sb, oi, &fcb, &sanity);
+       if (ret)
+               goto bad_inode;
+
+       init_waitqueue_head(&oi->i_wq);
+       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);
+
+#ifdef EXOFS_DEBUG_OBJ_ISIZE
+       if ((inode->i_size != sanity) &&
+               (!exofs_inode_is_fast_symlink(inode))) {
+               EXOFS_ERR("WARNING: Size of inode=%llu != object=%llu\n",
+                         inode->i_size, _LLU(sanity));
+       }
+#endif
+
+       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 osd_request *or, 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_ok(or);
+       osd_end_request(or);
+       atomic_dec(&sbi->s_curr_pending);
+
+       if (unlikely(ret)) {
+               EXOFS_ERR("object=0x%llx creation faild in pid=0x%llx",
+                         _LLU(sbi->s_pid), _LLU(inode->i_ino + EXOFS_OBJ_OFF));
+               make_bad_inode(inode);
+       } else
+               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 osd_request *or;
+       struct osd_obj_id obj;
+       int ret;
+
+       sb = dir->i_sb;
+       inode = new_inode(sb);
+       if (!inode)
+               return ERR_PTR(-ENOMEM);
+
+       oi = exofs_i(inode);
+
+       init_waitqueue_head(&oi->i_wq);
+       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);
+
+       obj.partition = sbi->s_pid;
+       obj.id = inode->i_ino + EXOFS_OBJ_OFF;
+       exofs_make_credential(oi->i_cred, &obj);
+
+       or = osd_start_request(sbi->s_dev, GFP_KERNEL);
+       if (unlikely(!or)) {
+               EXOFS_ERR("exofs_new_inode: osd_start_request failed\n");
+               return ERR_PTR(-ENOMEM);
+       }
+
+       osd_req_create_object(or, &obj);
+
+       /* increment the refcount so that the inode will still be around when we
+        * reach the callback
+        */
+       atomic_inc(&inode->i_count);
+
+       ret = exofs_async_op(or, create_done, inode, oi->i_cred);
+       if (ret) {
+               atomic_dec(&inode->i_count);
+               osd_end_request(or);
+               return ERR_PTR(-EIO);
+       }
+       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 osd_request *or, void *p)
+{
+       struct updatei_args *args = p;
+
+       osd_end_request(or);
+
+       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 osd_obj_id obj = {sbi->s_pid, inode->i_ino + EXOFS_OBJ_OFF};
+       struct osd_request *or;
+       struct osd_attr attr;
+       struct exofs_fcb *fcb;
+       struct updatei_args *args;
+       int ret;
+
+       args = kzalloc(sizeof(*args), GFP_KERNEL);
+       if (!args)
+               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));
+
+       or = osd_start_request(sbi->s_dev, GFP_KERNEL);
+       if (unlikely(!or)) {
+               EXOFS_ERR("exofs_update_inode: osd_start_request failed.\n");
+               ret = -ENOMEM;
+               goto free_args;
+       }
+
+       osd_req_set_attributes(or, &obj);
+
+       attr = g_attr_inode_data;
+       attr.val_ptr = fcb;
+       osd_req_add_set_attr_list(or, &attr, 1);
+
+       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) {
+               ret = exofs_sync_op(or, sbi->s_timeout, oi->i_cred);
+               osd_end_request(or);
+               goto free_args;
+       } else {
+               args->sbi = sbi;
+
+               ret = exofs_async_op(or, updatei_done, args, oi->i_cred);
+               if (ret) {
+                       osd_end_request(or);
+                       goto free_args;
+               }
+               atomic_inc(&sbi->s_curr_pending);
+               goto out; /* deallocation in updatei_done */
+       }
+
+free_args:
+       kfree(args);
+out:
+       EXOFS_DBGMSG("ret=>%d\n", 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 osd_request *or, void *p)
+{
+       struct exofs_sb_info *sbi;
+       osd_end_request(or);
+       sbi = p;
+       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 osd_obj_id obj = {sbi->s_pid, inode->i_ino + EXOFS_OBJ_OFF};
+       struct osd_request *or;
+       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);
+
+       or = osd_start_request(sbi->s_dev, GFP_KERNEL);
+       if (unlikely(!or)) {
+               EXOFS_ERR("exofs_delete_inode: osd_start_request failed\n");
+               return;
+       }
+
+       osd_req_remove_object(or, &obj);
+
+       /* 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));
+       }
+
+       ret = exofs_async_op(or, delete_done, sbi, oi->i_cred);
+       if (ret) {
+               EXOFS_ERR(
+                      "ERROR: @exofs_delete_inode exofs_async_op failed\n");
+               osd_end_request(or);
+               return;
+       }
+       atomic_inc(&sbi->s_curr_pending);
+
+       return;
+
+no_delete:
+       clear_inode(inode);
+}