Whamcloud - gitweb
Starting with 1.6.0 our kernel package will now actually be
[fs/lustre-release.git] / lustre / liblustre / file.c
index f666cdc..d592124 100644 (file)
@@ -1,9 +1,9 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Lustre Light Super operations
+ * Lustre Light file operations
  *
- *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
+ *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
  *
  *   This file is part of Lustre, http://www.lustre.org.
  *
 #include <time.h>
 #include <sys/types.h>
 #include <sys/queue.h>
+#include <fcntl.h>
 
+#ifdef HAVE_XTIO_H
+#include <xtio.h>
+#endif
 #include <sysio.h>
 #include <fs.h>
 #include <mount.h>
 #include <inode.h>
+#ifdef HAVE_FILE_H
 #include <file.h>
+#endif
 
 #undef LIST_HEAD
 
 #include "llite_lib.h"
 
-void llu_prepare_mdc_op_data(struct mdc_op_data *data,
-                             struct inode *i1,
-                             struct inode *i2,
-                             const char *name,
-                             int namelen,
-                             int mode)
+void llu_prepare_mdc_data(struct mdc_op_data *data, struct inode *i1,
+                          struct inode *i2, const char *name,
+                          int namelen, int mode)
 {
         LASSERT(i1);
         
-        ll_i2uctxt(&data->ctxt, i1, i2);
-        ll_inode2fid(&data->fid1, i1);
-
-        if (i2) {
-                ll_inode2fid(&data->fid2, i2);
-        }
+        ll_inode2id(&data->id1, i1);
+        if (i2)
+                ll_inode2id(&data->id2, i2);
 
+       data->valid = 0;
         data->name = name;
         data->namelen = namelen;
         data->create_mode = mode;
         data->mod_time = CURRENT_TIME;
 }
 
+static void llu_inode2mdc_data(struct mdc_op_data *op_data,
+                               struct inode *inode,
+                               obd_valid valid)
+{
+        struct llu_inode_info *lli = llu_i2info(inode);
+        obd_valid newvalid = 0;
+        
+        LASSERT(op_data != NULL);
+        LASSERT(inode != NULL);
+
+        /* put object id there all the time. */
+        if (valid & OBD_MD_FLID) {
+                ll_inode2id(&op_data->id1, inode);
+                newvalid |= OBD_MD_FLID;
+        }
+        
+        if (valid & OBD_MD_FLSIZE) {
+                op_data->size = lli->lli_st_size;
+                newvalid |= OBD_MD_MEA;
+        }
+        if (valid & OBD_MD_FLBLOCKS) {
+                op_data->blocks = lli->lli_st_blocks;
+                newvalid |= OBD_MD_FLBLOCKS;
+        }
+        if (valid & OBD_MD_FLFLAGS) {
+                op_data->flags = lli->lli_st_flags;
+                newvalid |= OBD_MD_FLFLAGS;
+        }
+        if (valid & OBD_MD_FLATIME) {
+                op_data->atime = LTIME_S(lli->lli_st_atime);
+                newvalid |= OBD_MD_FLATIME;
+        }
+        if (valid & OBD_MD_FLMTIME) {
+                op_data->mtime = LTIME_S(lli->lli_st_mtime);
+                newvalid |= OBD_MD_FLMTIME;
+        }
+        if (valid & OBD_MD_FLCTIME) {
+                op_data->ctime = LTIME_S(lli->lli_st_ctime);
+                newvalid |= OBD_MD_FLCTIME;
+        }
+
+        if (valid & OBD_MD_FLTYPE) {
+                op_data->mode = (op_data->mode & S_IALLUGO) |
+                        (lli->lli_st_mode & S_IFMT);
+                newvalid |= OBD_MD_FLTYPE;
+        }
+
+        if (valid & OBD_MD_FLMODE) {
+                op_data->mode = (op_data->mode & S_IFMT) |
+                        (lli->lli_st_mode & S_IALLUGO);
+                newvalid |= OBD_MD_FLMODE;
+        }
+
+        op_data->valid |= newvalid;
+}
+
 void obdo_refresh_inode(struct inode *dst,
                         struct obdo *src,
-                        obd_flag valid)
+                        obd_valid valid)
 {
         struct llu_inode_info *lli = llu_i2info(dst);
         valid &= src->o_valid;
 
         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
-                CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
+                CDEBUG(D_INODE, "valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
                        src->o_valid, LTIME_S(lli->lli_st_mtime), 
                        LTIME_S(lli->lli_st_ctime),
                        (long)src->o_mtime, (long)src->o_ctime);
@@ -93,7 +150,7 @@ void obdo_refresh_inode(struct inode *dst,
 
 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
 {
-        struct ptlrpc_request *req = it->d.lustre.it_data;
+        struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
         struct ll_file_data *fd;
         struct mds_body *body;
         ENTRY;
@@ -105,19 +162,19 @@ static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
         /* already opened? */
         if (lli->lli_open_count++)
                 RETURN(0);
-                
+
         LASSERT(!lli->lli_file_data);
 
         OBD_ALLOC(fd, sizeof(*fd));
         /* We can't handle this well without reorganizing ll_file_open and
-         * ll_mdc_close, so don't even try right now. */
+         * ll_md_close(), so don't even try right now. */
         LASSERT(fd != NULL);
 
         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
         lli->lli_file_data = fd;
 
-        mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
+        mdc_set_open_replay_data(NULL, &fd->fd_mds_och, LUSTRE_IT(it)->it_data);
 
         RETURN(0);
 }
@@ -133,6 +190,8 @@ int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
         int rc = 0;
         ENTRY;
 
+        liblustre_wait_event(0);
+
         /* don't do anything for '/' */
         if (llu_is_root_inode(inode))
                 RETURN(0);
@@ -140,9 +199,8 @@ int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
         LL_GET_INTENT(inode, it);
 
-        if (!it->d.lustre.it_disposition) {
+        if (!LUSTRE_IT(it)->it_disposition)
                 LBUG();
-        }
 
         rc = it_open_error(DISP_OPEN_OPEN, it);
         if (rc)
@@ -166,15 +224,31 @@ int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
         }
         fd->fd_flags &= ~O_LOV_DELAY_CREATE;
 
-        lli->lli_open_flags = flags;
+        lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
 
  out_release:
-        request = it->d.lustre.it_data;
+        request = LUSTRE_IT(it)->it_data;
         ptlrpc_req_finished(request);
 
         it->it_op_release(it);
         OBD_FREE(it, sizeof(*it));
 
+        /* libsysio haven't doing anything for O_TRUNC. here we
+         * simply simulate it as open(...); truncate(...);
+         */
+        if (rc == 0 && (flags & O_TRUNC) &&
+            S_ISREG(lli->lli_st_mode)) {
+                struct iattr attr;
+
+                memset(&attr, 0, sizeof(attr));
+                attr.ia_size = 0;
+                attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
+                rc  = llu_setattr_raw(inode, &attr);
+                if (rc) {
+                        CERROR("error %d truncate in open()\n", rc);
+                }
+        }
+
         RETURN(rc);
 }
 
@@ -210,7 +284,7 @@ int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
                 GOTO(out, rc = -EPROTO);
         }
 
-        rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata, body->eadatasize);
+        rc = obd_unpackmd(llu_i2dtexp(dir), &lsm, eadata, body->eadatasize);
         if (rc < 0) {
                 CERROR("obd_unpackmd: %d\n", rc);
                 GOTO(out, rc);
@@ -237,13 +311,13 @@ int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
                 }
         }
 
-        rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti);
+        rc = obd_destroy(llu_i2dtexp(dir), oa, lsm, &oti);
         obdo_free(oa);
         if (rc)
                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
                        lsm->lsm_object_id, rc);
  out_free_memmd:
-        obd_free_memmd(llu_i2obdexp(dir), &lsm);
+        obd_free_memmd(llu_i2dtexp(dir), &lsm);
  out:
         return rc;
 }
@@ -254,39 +328,40 @@ int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
         struct ll_file_data *fd = lli->lli_file_data;
         struct ptlrpc_request *req = NULL;
         struct obd_client_handle *och = &fd->fd_mds_och;
-        struct obdo obdo;
+        struct mdc_op_data op_data;
         int rc, valid;
         ENTRY;
 
         /* clear group lock, if present */
-        if (fd->fd_flags & LL_FILE_CW_LOCKED) {
+        if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
                 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
-                fd->fd_flags &= ~(LL_FILE_CW_LOCKED|LL_FILE_IGNORE_LOCK);
-                rc = llu_extent_unlock(fd, inode, lsm, LCK_CW, &fd->fd_cwlockh);
+                fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
+                rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
+                                       &fd->fd_cwlockh);
         }
 
-        valid = OBD_MD_FLID;
+       memset(&op_data, 0, sizeof(op_data));
+       id_ino(&op_data.id1) = lli->lli_st_ino;
+        op_data.valid = OBD_MD_FLID;
+        valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
+                OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
 
-        memset(&obdo, 0, sizeof(obdo));
-        obdo.o_id = lli->lli_st_ino;
-        obdo.o_mode = lli->lli_st_mode;
-        obdo.o_size = lli->lli_st_size;
-        obdo.o_blocks = lli->lli_st_blocks;
+       llu_inode2mdc_data(&op_data, inode, valid);
+
         if (0 /* ll_is_inode_dirty(inode) */) {
-                obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
-                valid |= OBD_MD_FLFLAGS;
+                op_data.flags = MDS_BFLAG_UNCOMMITTED_WRITES;
+                op_data.valid |= OBD_MD_FLFLAGS;
         }
-        obdo.o_valid = valid;
-        rc = mdc_close(mdc_exp, &obdo, och, &req);
+        rc = mdc_close(mdc_exp, &op_data, och, &req);
         if (rc == EAGAIN) {
                 /* We are the last writer, so the MDS has instructed us to get
                  * the file size and any write cookies, then close again. */
                 //ll_queue_done_writing(inode);
                 rc = 0;
         } else if (rc) {
-                CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
+                CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
         } else {
                 rc = llu_objects_destroy(req, inode);
                 if (rc)
@@ -294,7 +369,7 @@ int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
                                 lli->lli_st_ino, rc);
         }
 
-        mdc_clear_open_replay_data(och);
+        mdc_clear_open_replay_data(NULL, och);
         ptlrpc_req_finished(req);
         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
         lli->lli_file_data = NULL;
@@ -325,61 +400,41 @@ int llu_file_release(struct inode *inode)
         if (!fd) /* no process opened the file after an mcreate */
                 RETURN(0);
 
-        rc2 = llu_mdc_close(sbi->ll_mdc_exp, inode);
+        rc2 = llu_mdc_close(sbi->ll_md_exp, inode);
         if (rc2 && !rc)
                 rc = rc2;
 
         RETURN(rc);
 }
 
+/*
+ * libsysio require us return 0
+ */
 int llu_iop_close(struct inode *inode)
 {
         int rc;
 
+        liblustre_wait_event(0);
+
         rc = llu_file_release(inode);
+        if (rc) {
+                CERROR("file close error %d\n", rc);
+        }
         /* if open count == 0 && stale_flag is set, should we
          * remove the inode immediately? */
-        return rc;
+        return 0;
 }
 
-int llu_iop_ipreadv(struct inode *ino,
-                    struct ioctx *ioctx)
+_SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
 {
         ENTRY;
 
-        if (!ioctx->ioctx_iovlen)
-                RETURN(0);
-        if (ioctx->ioctx_iovlen < 0)
-                RETURN(-EINVAL);
-
-        ioctx->ioctx_private = llu_file_read(ino,
-                                        ioctx->ioctx_iovec,
-                                        ioctx->ioctx_iovlen,
-                                        ioctx->ioctx_offset);
-        if (IS_ERR(ioctx->ioctx_private))
-                return (PTR_ERR(ioctx->ioctx_private));
-
-        RETURN(0);
-}
+        liblustre_wait_event(0);
 
-int llu_iop_ipwritev(struct inode *ino,
-                     struct ioctx *ioctx)
-{
-        ENTRY;
-
-        if (!ioctx->ioctx_iovlen)
-                RETURN(0);
-        if (ioctx->ioctx_iovlen < 0)
+        if (off < 0 || off > ll_file_maxbytes(ino))
                 RETURN(-EINVAL);
 
-        ioctx->ioctx_private = llu_file_write(ino,
-                                         ioctx->ioctx_iovec,
-                                         ioctx->ioctx_iovlen,
-                                         ioctx->ioctx_offset);
-        if (IS_ERR(ioctx->ioctx_private))
-                return (PTR_ERR(ioctx->ioctx_private));
-
-        RETURN(0);
+        RETURN(off);
 }
 
 /* this isn't where truncate starts.   roughly:
@@ -410,8 +465,9 @@ static void llu_truncate(struct inode *inode)
                oa.o_id, lli->lli_st_size);
 
         /* truncate == punch from new size to absolute end of file */
-        err = obd_punch(llu_i2obdexp(inode), &oa, lsm, lli->lli_st_size,
-                        OBD_OBJECT_EOF, NULL);
+        /* XXX: capa is NULL here, is it correct? */
+        err = obd_punch(llu_i2dtexp(inode), &oa, lsm, lli->lli_st_size,
+                        OBD_OBJECT_EOF, NULL, NULL);
         if (err)
                 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
         else