+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 = 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_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE;
+ 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 && 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_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)
+ GOTO(out_req, -abs(rc));
+ if (!fd->fd_mdshandle) {
+ CERROR("mdc_open didn't assign fd_mdshandle\n");
+ /* XXX handle this how, abort or is it non-fatal? */
+ }
+ if (!fd->fd_mdshandle)
+ CERROR("mdc_open didn't assign fd_mdshandle\n");
+
+ 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;
+ rc = obd_open(ll_i2obdconn(inode), oa, lli->lli_smd);
+ obdo_free(oa);
+ oa = NULL;
+
+ if (rc)
+ GOTO(out_mdc, rc = -abs(rc));
+
+ file->private_data = fd;
+
+ EXIT;
+
+ return 0;
+out_mdc:
+ 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:
+ return rc;
+}
+
+static int ll_file_release(struct inode *inode, struct file *file)
+{
+ int rc;
+ struct ptlrpc_request *req = NULL;
+ struct ll_file_data *fd;
+ struct obdo *oa;
+ struct ll_sb_info *sbi = ll_i2sbi(inode);
+ struct ll_inode_info *lli = ll_i2info(inode);
+
+ ENTRY;
+
+ fd = (struct ll_file_data *)file->private_data;
+ if (!fd || !fd->fd_mdshandle) {
+ LBUG();
+ GOTO(out, rc = -EINVAL);
+ }
+
+ oa = obdo_alloc();
+ if (oa == NULL) {
+ LBUG();
+ GOTO(out_fd, rc = -ENOENT);
+ }
+ 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)
+ GOTO(out_fd, abs(rc));
+
+ if (file->f_mode & FMODE_WRITE) {
+ struct iattr attr;
+ 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;
+ attr.ia_size = inode->i_size;
+
+ /* XXX: this introduces a small race that we should evaluate */
+ rc = ll_inode_setattr(inode, &attr, 0);
+ if (rc) {
+ CERROR("failed - %d.\n", rc);
+ rc = -EIO; /* XXX - GOTO(out)? -phil */
+ }
+ }
+
+ 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)
+ rc = -rc;
+ GOTO(out, rc);
+ }
+ ptlrpc_free_req(fd->fd_req);
+
+ EXIT;
+
+out_fd:
+ kmem_cache_free(ll_file_data_slab, fd);
+ file->private_data = NULL;
+out:
+ return rc;
+}