Whamcloud - gitweb
LU-359 llite: no close error if application has known failure
[fs/lustre-release.git] / lustre / llite / file.c
index c9dad81..96379d6 100644 (file)
 
 struct ll_file_data *ll_file_data_get(void)
 {
-        struct ll_file_data *fd;
+       struct ll_file_data *fd;
 
-        OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, CFS_ALLOC_IO);
-        return fd;
+       OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, CFS_ALLOC_IO);
+       fd->fd_write_failed = false;
+       return fd;
 }
 
 static void ll_file_data_put(struct ll_file_data *fd)
@@ -372,6 +373,7 @@ static int ll_intent_file_open(struct file *file, void *lmm,
         if (IS_ERR(op_data))
                 RETURN(PTR_ERR(op_data));
 
+       itp->it_flags |= MDS_OPEN_BY_FID;
         rc = md_intent_lock(sbi->ll_md_exp, op_data, lmm, lmmsize, itp,
                             0 /*unused */, &req, ll_md_blocking_ast, 0);
         ll_finish_md_op_data(op_data);
@@ -824,11 +826,13 @@ void ll_io_init(struct cl_io *io, const struct file *file, int write)
         }
 }
 
-static ssize_t ll_file_io_generic(const struct lu_env *env,
-                struct vvp_io_args *args, struct file *file,
-                enum cl_io_type iot, loff_t *ppos, size_t count)
+static ssize_t
+ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
+                  struct file *file, enum cl_io_type iot,
+                  loff_t *ppos, size_t count)
 {
-        struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
+       struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
+       struct ll_file_data  *fd  = LUSTRE_FPRIVATE(file);
         struct cl_io         *io;
         ssize_t               result;
         ENTRY;
@@ -900,13 +904,13 @@ out:
                 if (result >= 0) {
                         ll_stats_ops_tally(ll_i2sbi(file->f_dentry->d_inode),
                                            LPROC_LL_WRITE_BYTES, result);
-                        lli->lli_write_rc = 0;
-                } else {
-                        lli->lli_write_rc = result;
-                }
-        }
+                       fd->fd_write_failed = false;
+               } else {
+                       fd->fd_write_failed = true;
+               }
+       }
 
-        return result;
+       return result;
 }
 
 
@@ -1487,7 +1491,7 @@ static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
        lsm = ccc_inode_lsm_get(inode);
        if (lsm != NULL)
                rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0,
-                               lsm, (void *)arg);
+                                  lsm, (void *)arg);
        ccc_inode_lsm_put(inode, lsm);
        RETURN(rc);
 }
@@ -1972,30 +1976,27 @@ loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
         RETURN(retval);
 }
 
-#ifdef HAVE_FLUSH_OWNER_ID
 int ll_flush(struct file *file, fl_owner_t id)
-#else
-int ll_flush(struct file *file)
-#endif
 {
-        struct inode *inode = file->f_dentry->d_inode;
-        struct ll_inode_info *lli = ll_i2info(inode);
-        int rc, err;
-
-        LASSERT(!S_ISDIR(inode->i_mode));
+       struct inode *inode = file->f_dentry->d_inode;
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+       int rc, err;
 
-        /* the application should know write failure already. */
-        if (lli->lli_write_rc)
-                return 0;
+       LASSERT(!S_ISDIR(inode->i_mode));
 
-        /* catch async errors that were recorded back when async writeback
-         * failed for pages in this mapping. */
-        rc = lli->lli_async_rc;
-        lli->lli_async_rc = 0;
+       /* catch async errors that were recorded back when async writeback
+        * failed for pages in this mapping. */
+       rc = lli->lli_async_rc;
+       lli->lli_async_rc = 0;
        err = lov_read_and_clear_async_rc(lli->lli_clob);
        if (rc == 0)
                rc = err;
 
+       /* The application has been told write failure already.
+        * Do not report failure again. */
+       if (fd->fd_write_failed)
+               return 0;
        return rc ? -EIO : 0;
 }
 
@@ -2028,6 +2029,7 @@ int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
 
        io = ccc_env_thread_io(env);
        io->ci_obj = cl_i2info(inode)->lli_clob;
+       io->ci_ignore_layout = 1;
 
        /* initialize parameters for sync */
        fio = &io->u.ci_fsync;
@@ -2098,11 +2100,16 @@ int ll_fsync(struct file *file, struct dentry *dentry, int data)
 
        lsm = ccc_inode_lsm_get(inode);
        if (data && lsm) {
+               struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
+
                err = cl_sync_file_range(inode, 0, OBD_OBJECT_EOF,
                                CL_FSYNC_ALL);
                if (rc == 0 && err < 0)
                        rc = err;
-               lli->lli_write_rc = rc < 0 ? rc : 0;
+               if (rc < 0)
+                       fd->fd_write_failed = true;
+               else
+                       fd->fd_write_failed = false;
        }
        ccc_inode_lsm_put(inode, lsm);
 
@@ -2480,11 +2487,7 @@ int ll_getattr_it(struct vfsmount *mnt, struct dentry *de,
         stat->atime = inode->i_atime;
         stat->mtime = inode->i_mtime;
         stat->ctime = inode->i_ctime;
-#ifdef HAVE_INODE_BLKSIZE
-        stat->blksize = inode->i_blksize;
-#else
-        stat->blksize = 1 << inode->i_blkbits;
-#endif
+       stat->blksize = 1 << inode->i_blkbits;
 
         stat->size = i_size_read(inode);
         stat->blocks = inode->i_blocks;
@@ -2533,39 +2536,40 @@ int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 }
 #endif
 
-
+#ifndef HAVE_GENERIC_PERMISSION_2ARGS
 static int
-#ifdef HAVE_GENERIC_PERMISSION_4ARGS
+# ifdef HAVE_GENERIC_PERMISSION_4ARGS
 lustre_check_acl(struct inode *inode, int mask, unsigned int flags)
-#else
+# else
 lustre_check_acl(struct inode *inode, int mask)
-#endif
+# endif
 {
-#ifdef CONFIG_FS_POSIX_ACL
-        struct ll_inode_info *lli = ll_i2info(inode);
-        struct posix_acl *acl;
-        int rc;
-        ENTRY;
+# ifdef CONFIG_FS_POSIX_ACL
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct posix_acl *acl;
+       int rc;
+       ENTRY;
 
-#ifdef HAVE_GENERIC_PERMISSION_4ARGS
-        if (flags & IPERM_FLAG_RCU)
-                return -ECHILD;
-#endif
-        cfs_spin_lock(&lli->lli_lock);
-        acl = posix_acl_dup(lli->lli_posix_acl);
-        cfs_spin_unlock(&lli->lli_lock);
+#  ifdef HAVE_GENERIC_PERMISSION_4ARGS
+       if (flags & IPERM_FLAG_RCU)
+               return -ECHILD;
+#  endif
+       cfs_spin_lock(&lli->lli_lock);
+       acl = posix_acl_dup(lli->lli_posix_acl);
+       cfs_spin_unlock(&lli->lli_lock);
 
-        if (!acl)
-                RETURN(-EAGAIN);
+       if (!acl)
+               RETURN(-EAGAIN);
 
-        rc = posix_acl_permission(inode, acl, mask);
-        posix_acl_release(acl);
+       rc = posix_acl_permission(inode, acl, mask);
+       posix_acl_release(acl);
 
-        RETURN(rc);
-#else
-        return -EAGAIN;
-#endif
+       RETURN(rc);
+# else /* !CONFIG_FS_POSIX_ACL */
+       return -EAGAIN;
+# endif /* CONFIG_FS_POSIX_ACL */
 }
+#endif /* HAVE_GENERIC_PERMISSION_2ARGS */
 
 #ifdef HAVE_GENERIC_PERMISSION_4ARGS
 int ll_inode_permission(struct inode *inode, int mask, unsigned int flags)
@@ -2800,3 +2804,141 @@ enum llioc_iter ll_iocontrol_call(struct inode *inode, struct file *file,
                 *rcp = rc;
         return ret;
 }
+
+int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
+{
+       struct ll_inode_info *lli = ll_i2info(inode);
+       struct cl_env_nest nest;
+       struct lu_env *env;
+       int result;
+       ENTRY;
+
+       if (lli->lli_clob == NULL)
+               RETURN(0);
+
+       env = cl_env_nested_get(&nest);
+       if (IS_ERR(env))
+               RETURN(PTR_ERR(env));
+
+       result = cl_conf_set(env, lli->lli_clob, conf);
+       cl_env_nested_put(&nest, env);
+       RETURN(result);
+}
+
+/**
+ * This function checks if there exists a LAYOUT lock on the client side,
+ * or enqueues it if it doesn't have one in cache.
+ *
+ * This function will not hold layout lock so it may be revoked any time after
+ * this function returns. Any operations depend on layout should be redone
+ * in that case.
+ *
+ * This function should be called before lov_io_init() to get an uptodate
+ * layout version, the caller should save the version number and after IO
+ * is finished, this function should be called again to verify that layout
+ * is not changed during IO time.
+ */
+int ll_layout_refresh(struct inode *inode, __u32 *gen)
+{
+       struct ll_inode_info  *lli = ll_i2info(inode);
+       struct ll_sb_info     *sbi = ll_i2sbi(inode);
+       struct md_op_data     *op_data = NULL;
+       struct ptlrpc_request *req = NULL;
+       struct lookup_intent   it = { .it_op = IT_LAYOUT };
+       struct lustre_handle   lockh;
+       ldlm_mode_t            mode;
+       struct cl_object_conf  conf = {  .coc_inode = inode,
+                                        .coc_validate_only = true };
+       int rc;
+       ENTRY;
+
+       *gen = 0;
+       if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_LAYOUT_LOCK))
+               RETURN(0);
+
+       /* sanity checks */
+       LASSERT(fid_is_sane(ll_inode2fid(inode)));
+       LASSERT(S_ISREG(inode->i_mode));
+
+       /* mostly layout lock is caching on the local side, so try to match
+        * it before grabbing layout lock mutex. */
+       mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh);
+       if (mode != 0) { /* hit cached lock */
+               struct lov_stripe_md *lsm;
+
+               lsm = ccc_inode_lsm_get(inode);
+               if (lsm != NULL)
+                       *gen = lsm->lsm_layout_gen;
+               ccc_inode_lsm_put(inode, lsm);
+               ldlm_lock_decref(&lockh, mode);
+
+               RETURN(0);
+       }
+
+       op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
+                                    0, 0, LUSTRE_OPC_ANY, NULL);
+       if (IS_ERR(op_data))
+               RETURN(PTR_ERR(op_data));
+
+       /* take layout lock mutex to enqueue layout lock exclusively. */
+       cfs_mutex_lock(&lli->lli_layout_mutex);
+
+       /* make sure the old conf goes away */
+       ll_layout_conf(inode, &conf);
+
+       /* enqueue layout lock */
+       rc = md_intent_lock(sbi->ll_md_exp, op_data, NULL, 0, &it, 0,
+                       &req, ll_md_blocking_ast, 0);
+       if (rc == 0) {
+               /* we get a new lock, so update the lock data */
+               lockh.cookie = it.d.lustre.it_lock_handle;
+               md_set_lock_data(sbi->ll_md_exp, &lockh.cookie, inode, NULL);
+
+               /* req == NULL is when lock was found in client cache, without
+                * any request to server (but lsm can be canceled just after a
+                * release) */
+               if (req != NULL) {
+                       struct ldlm_lock *lock = ldlm_handle2lock(&lockh);
+                       struct lustre_md md = { NULL };
+                       void *lmm;
+                       int lmmsize;
+
+                       /* for IT_LAYOUT lock, lmm is returned in lock's lvb
+                        * data via completion callback */
+                       LASSERT(lock != NULL);
+                       lmm = lock->l_lvb_data;
+                       lmmsize = lock->l_lvb_len;
+                       if (lmm != NULL)
+                               rc = obd_unpackmd(sbi->ll_dt_exp, &md.lsm,
+                                               lmm, lmmsize);
+                       if (rc == 0) {
+                               if (md.lsm != NULL)
+                                       *gen = md.lsm->lsm_layout_gen;
+
+                               memset(&conf, 0, sizeof conf);
+                               conf.coc_inode = inode;
+                               conf.u.coc_md = &md;
+                               ll_layout_conf(inode, &conf);
+                               /* is this racy? */
+                               lli->lli_has_smd = md.lsm != NULL;
+                       }
+                       if (md.lsm != NULL)
+                               obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
+
+                       LDLM_LOCK_PUT(lock);
+                       ptlrpc_req_finished(req);
+               } else { /* hit caching lock */
+                       struct lov_stripe_md *lsm;
+
+                       lsm = ccc_inode_lsm_get(inode);
+                       if (lsm != NULL)
+                               *gen = lsm->lsm_layout_gen;
+                       ccc_inode_lsm_put(inode, lsm);
+               }
+               ll_intent_drop_lock(&it);
+       }
+       cfs_mutex_unlock(&lli->lli_layout_mutex);
+       ll_finish_md_op_data(op_data);
+
+       RETURN(rc);
+}