Whamcloud - gitweb
Change lov.xml file a little so that it works with localhost.
[fs/lustre-release.git] / lustre / llite / file.c
index 4fc5c81..b1a6287 100644 (file)
 
 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
 extern int ll_setattr(struct dentry *de, struct iattr *attr);
-extern inline struct obdo * ll_oa_from_inode(struct inode *inode,
-                                             unsigned long valid);
 
 static int ll_file_open(struct inode *inode, struct file *file)
 {
         int rc;
         struct ptlrpc_request *req = NULL;
         struct ll_file_data *fd;
-        struct obdo *oa;
+        struct obdo *oa = NULL;
+        struct lov_stripe_md *md = NULL; 
         struct ll_sb_info *sbi = ll_i2sbi(inode);
+        struct ll_inode_info *lli = ll_i2info(inode);
         ENTRY;
 
         if (file->private_data)
                 LBUG();
 
+        /*  delayed create of object (intent created inode) */
+        /*  XXX object needs to be cleaned up if mdc_open fails */
+        /*  XXX error handling appropriate here? */
+        if (lli->lli_smd == NULL) {
+                struct client_obd *mdc = sbi2mdc(ll_s2sbi(inode->i_sb));
+                struct inode * inode = file->f_dentry->d_inode;
+
+                down(&lli->lli_open_sem);
+                /* Check to see if we lost the race */
+                if (lli->lli_smd == NULL) {
+                        oa = obdo_alloc();
+                        if (!oa) {
+                                up(&lli->lli_open_sem);
+                                RETURN(-ENOMEM);
+                        }
+                        oa->o_mode = S_IFREG | 0600;
+                        oa->o_easize = mdc->cl_max_mdsize;
+                        oa->o_id = inode->i_ino;
+                        oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE |
+                                        OBD_MD_FLID;
+                        rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
+                        if (rc) {
+                                obdo_free(oa);
+                                up(&lli->lli_open_sem);
+                                RETURN(rc);
+                        }
+                        md = lli->lli_smd;
+                }
+                if (lli->lli_smd->lmd_object_id == 0)
+                        LBUG();
+                up(&lli->lli_open_sem);
+        }
+
         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
         if (!fd)
                 GOTO(out, rc = -ENOMEM);
         memset(fd, 0, sizeof(*fd));
 
-        rc = mdc_open(&sbi->ll_mds_client, sbi->ll_mds_conn, inode->i_ino,
-                      S_IFREG, file->f_flags, (__u64)(unsigned long)file, 
-                      &fd->fd_mdshandle, &req); 
+        rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
+                      file->f_flags, md, (__u64)(unsigned long)file,
+                      &fd->fd_mdshandle, &req);
         fd->fd_req = req;
         ptlrpc_req_finished(req);
         if (rc)
@@ -62,16 +95,18 @@ static int ll_file_open(struct inode *inode, struct file *file)
                 /* XXX handle this how, abort or is it non-fatal? */
         }
 
-        oa = ll_oa_from_inode(inode, (OBD_MD_FLMODE | OBD_MD_FLID));
-        if (oa == NULL) {
-                LBUG();
-                GOTO(out_mdc, rc = -ENOMEM);
-        }
-        rc = obd_open(ll_i2obdconn(inode), oa);
+        if (oa == NULL && (oa = obdo_alloc()) == NULL)
+                GOTO(out_mdc, rc = -EINVAL);
+
+        oa->o_id = lli->lli_smd->lmd_object_id;
+        oa->o_mode = S_IFREG | inode->i_mode;
+        oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLID | OBD_MD_FLSIZE;
+        rc = obd_open(ll_i2obdconn(inode), oa, lli->lli_smd);
         obdo_free(oa);
-        if (rc) {
+        oa = NULL;
+
+        if (rc)
                 GOTO(out_mdc, rc = -abs(rc));
-        }
 
         file->private_data = fd;
 
@@ -79,11 +114,12 @@ static int ll_file_open(struct inode *inode, struct file *file)
 
         return 0;
 out_mdc:
-        mdc_close(&sbi->ll_mds_client, sbi->ll_mds_conn, inode->i_ino,
+        mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
                   S_IFREG, fd->fd_mdshandle, &req);
 out_req:
         ptlrpc_free_req(req);
 //out_fd:
+        obdo_free(oa);
         kmem_cache_free(ll_file_data_slab, fd);
         file->private_data = NULL;
 out:
@@ -97,6 +133,9 @@ static int ll_file_release(struct inode *inode, struct file *file)
         struct ll_file_data *fd;
         struct obdo *oa;
         struct ll_sb_info *sbi = ll_i2sbi(inode);
+        struct ll_inode_info *lli = ll_i2info(inode);
+        struct obd_device *obddev = class_conn2obd(&sbi->ll_osc_conn);
+        struct list_head *tmp, *next;
 
         ENTRY;
 
@@ -106,20 +145,23 @@ static int ll_file_release(struct inode *inode, struct file *file)
                 GOTO(out, rc = -EINVAL);
         }
 
-        oa = ll_oa_from_inode(inode, (OBD_MD_FLMODE | OBD_MD_FLID));
+        oa = obdo_alloc();
         if (oa == NULL) {
                 LBUG();
                 GOTO(out_fd, rc = -ENOENT);
         }
-        rc = obd_close(ll_i2obdconn(inode), oa);
+        oa->o_id = lli->lli_smd->lmd_object_id;
+        oa->o_mode = S_IFREG;
+        oa->o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
+        rc = obd_close(ll_i2obdconn(inode), oa, lli->lli_smd);
         obdo_free(oa);
-        if (rc) {
+        if (rc)
                 GOTO(out_fd, abs(rc));
-        }
 
-        if (file->f_flags & O_WRONLY) {
+        if (file->f_mode & FMODE_WRITE) {
                 struct iattr attr;
-                attr.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_ATIME | ATTR_SIZE;
+                attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
+                                 ATTR_SIZE);
                 attr.ia_mtime = inode->i_mtime;
                 attr.ia_ctime = inode->i_ctime;
                 attr.ia_atime = inode->i_atime;
@@ -133,16 +175,34 @@ static int ll_file_release(struct inode *inode, struct file *file)
                 }
         }
 
-        rc = mdc_close(&sbi->ll_mds_client, sbi->ll_mds_conn, inode->i_ino,
+        rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
                        S_IFREG, fd->fd_mdshandle, &req);
         ptlrpc_req_finished(req);
-        if (rc) { 
-                if (rc > 0) 
+        if (rc) {
+                if (rc > 0)
                         rc = -rc;
                 GOTO(out, rc);
         }
         ptlrpc_free_req(fd->fd_req);
 
+        // XXX Phil lov devices have no namespace
+        //l_lock(&obddev->obd_namespace->ns_lock);
+        list_for_each_safe(tmp, next, &lli->lli_osc_locks) {
+                struct ldlm_lock *lock;
+                struct lustre_handle lockh;
+                lock = list_entry(tmp, struct ldlm_lock, l_inode_link);
+
+                if (!list_empty(&lock->l_inode_link)) {
+                        list_del_init(&lock->l_inode_link);
+                        LDLM_LOCK_PUT(lock);
+                }
+                ldlm_lock2handle(lock, &lockh);
+                rc = ldlm_cli_cancel(&lockh);
+                if (rc < 0)
+                        CERROR("ldlm_cli_cancel: %d\n", rc);
+        }
+        //l_unlock(&obddev->obd_namespace->ns_lock);
+
         EXIT;
 
 out_fd:
@@ -183,6 +243,39 @@ static void ll_update_atime(struct inode *inode)
         ll_inode_setattr(inode, &attr, 0);
 }
 
+static int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
+                            void *data, __u32 data_len)
+{
+        struct inode *inode = data;
+        struct lustre_handle lockh;
+        int rc;
+        ENTRY;
+
+        if (data_len != sizeof(struct inode))
+                LBUG();
+
+        if (inode == NULL)
+                LBUG();
+        down(&inode->i_sem);
+        CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
+        /* FIXME: do something better than throwing away everything */
+        invalidate_inode_pages(inode);
+        up(&inode->i_sem);
+
+        ldlm_lock2handle(lock, &lockh);
+        l_lock(&lock->l_resource->lr_namespace->ns_lock);
+        if (!list_empty(&lock->l_inode_link)) {
+                list_del_init(&lock->l_inode_link);
+                LDLM_LOCK_PUT(lock);
+        }
+        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+
+        rc = ldlm_cli_cancel(&lockh);
+        if (rc != ELDLM_OK)
+                CERROR("ldlm_cli_cancel failed: %d\n", rc);
+        RETURN(0);
+}
+
 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
                             loff_t *ppos)
 {
@@ -190,39 +283,52 @@ static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
         struct inode *inode = filp->f_dentry->d_inode;
         struct ll_sb_info *sbi = ll_i2sbi(inode);
         struct ldlm_extent extent;
-        struct ldlm_handle lockh;
-        __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
+        struct lustre_handle *lockhs = NULL;
+        struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
         int flags = 0;
         ldlm_error_t err;
         ssize_t retval;
         ENTRY;
 
         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
+                OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
+                if (!lockhs)
+                        RETURN(-ENOMEM);
+
                 extent.start = *ppos;
                 extent.end = *ppos + count;
                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
                        inode->i_ino, extent.start, extent.end);
 
-                err = obd_enqueue(&sbi->ll_conn, sbi->ll_namespace, NULL,
-                                  res_id, LDLM_EXTENT, &extent, LCK_PR, &flags,
-                                  inode, sizeof(*inode), &lockh);
-                if (err != ELDLM_OK)
+                err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
+                                  &extent, sizeof(extent), LCK_PR, &flags,
+                                  ll_lock_callback, inode, sizeof(*inode),
+                                  lockhs);
+                if (err != ELDLM_OK) {
+                        OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
                         CERROR("lock enqueue: err: %d\n", err);
-                ldlm_lock_dump((void *)(unsigned long)lockh.addr);
+                        RETURN(err);
+                }
         }
 
         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
                inode->i_ino, count, *ppos);
         retval = generic_file_read(filp, buf, count, ppos);
+
         if (retval > 0)
                 ll_update_atime(inode);
 
         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
-                err = obd_cancel(&sbi->ll_conn, LCK_PR, &lockh);
-                if (err != ELDLM_OK)
+                err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PR, lockhs);
+                if (err != ELDLM_OK) {
+                        OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
                         CERROR("lock cancel: err: %d\n", err);
+                        RETURN(err);
+                }
         }
 
+        if (lockhs)
+                OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
         RETURN(retval);
 }
 
@@ -236,25 +342,33 @@ ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
         struct inode *inode = file->f_dentry->d_inode;
         struct ll_sb_info *sbi = ll_i2sbi(inode);
         struct ldlm_extent extent;
-        struct ldlm_handle lockh;
-        __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
+        struct lustre_handle *lockhs = NULL;
+        struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
         int flags = 0;
         ldlm_error_t err;
         ssize_t retval;
         ENTRY;
 
         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
+                OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
+                if (!lockhs)
+                        RETURN(-ENOMEM); 
+                /* FIXME: this should check whether O_APPEND is set and adjust
+                 * extent.start accordingly */
                 extent.start = *ppos;
                 extent.end = *ppos + count;
                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
                        inode->i_ino, extent.start, extent.end);
 
-                err = obd_enqueue(&sbi->ll_conn, sbi->ll_namespace, NULL,
-                                  res_id, LDLM_EXTENT, &extent, LCK_PW, &flags,
-                                  inode, sizeof(*inode), &lockh);
-                if (err != ELDLM_OK)
+                err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
+                                  &extent, sizeof(extent), LCK_PW, &flags,
+                                  ll_lock_callback, inode, sizeof(*inode),
+                                  lockhs);
+                if (err != ELDLM_OK) {
+                        OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
                         CERROR("lock enqueue: err: %d\n", err);
-                ldlm_lock_dump((void *)(unsigned long)lockh.addr);
+                        RETURN(err);
+                }
         }
 
         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
@@ -263,11 +377,16 @@ ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
         retval = generic_file_write(file, buf, count, ppos);
 
         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
-                err = obd_cancel(&sbi->ll_conn, LCK_PW, &lockh);
-                if (err != ELDLM_OK)
+                err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PW, lockhs);
+                if (err != ELDLM_OK) {
+                        OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
                         CERROR("lock cancel: err: %d\n", err);
+                        RETURN(err);
+                }
         }
 
+        if (lockhs)
+                OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
         RETURN(retval);
 }