Whamcloud - gitweb
LU-3384 llite: use READ, WRITE around ll_rw_stats_tally()
[fs/lustre-release.git] / lustre / llite / vvp_io.c
index f8b28b2..e7ca14e 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2011, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -65,6 +65,39 @@ int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
         return vio->cui_io_subtype == IO_NORMAL;
 }
 
+/**
+ * For swapping layout. The file's layout may have changed.
+ * To avoid populating pages to a wrong stripe, we have to verify the
+ * correctness of layout. It works because swapping layout processes
+ * have to acquire group lock.
+ */
+static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
+                               struct inode *inode)
+{
+       struct ll_inode_info    *lli = ll_i2info(inode);
+       struct ccc_io           *cio = ccc_env_io(env);
+       bool rc = true;
+
+       switch (io->ci_type) {
+       case CIT_READ:
+       case CIT_WRITE:
+               /* don't need lock here to check lli_layout_gen as we have held
+                * extent lock and GROUP lock has to hold to swap layout */
+               if (lli->lli_layout_gen != cio->cui_layout_gen) {
+                       io->ci_need_restart = 1;
+                       /* this will return application a short read/write */
+                       io->ci_continue = 0;
+                       rc = false;
+               }
+       case CIT_FAULT:
+               /* fault is okay because we've already had a page. */
+       default:
+               break;
+       }
+
+       return rc;
+}
+
 /*****************************************************************************
  *
  * io operations.
@@ -88,14 +121,22 @@ static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
        struct cl_io     *io  = ios->cis_io;
        struct cl_object *obj = io->ci_obj;
        struct ccc_io    *cio = cl2ccc_io(env, ios);
-       __u32 gen;
 
         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
 
-       /* check layout version */
-       ll_layout_refresh(ccc_object_inode(obj), &gen);
-       if (cio->cui_layout_gen > 0)
-               io->ci_need_restart = cio->cui_layout_gen == gen;
+       CDEBUG(D_VFSTRACE, "ignore/verify layout %d/%d, layout version %d.\n",
+               io->ci_ignore_layout, io->ci_verify_layout, cio->cui_layout_gen);
+
+       if (!io->ci_ignore_layout && io->ci_verify_layout) {
+               __u32 gen = 0;
+
+               /* check layout version */
+               ll_layout_refresh(ccc_object_inode(obj), &gen);
+               io->ci_need_restart = cio->cui_layout_gen != gen;
+               if (io->ci_need_restart)
+                       CDEBUG(D_VFSTRACE, "layout changed from %d to %d.\n",
+                               cio->cui_layout_gen, gen);
+       }
 }
 
 static void vvp_io_fault_fini(const struct lu_env *env,
@@ -281,21 +322,6 @@ static int vvp_io_write_lock(const struct lu_env *env,
 static int vvp_io_setattr_iter_init(const struct lu_env *env,
                                    const struct cl_io_slice *ios)
 {
-       struct ccc_io *cio   = ccc_env_io(env);
-       struct inode  *inode = ccc_object_inode(ios->cis_obj);
-
-       /*
-        * We really need to get our PW lock before we change inode->i_size.
-        * If we don't we can race with other i_size updaters on our node,
-        * like ll_file_read.  We can also race with i_size propogation to
-        * other nodes through dirtying and writeback of final cached pages.
-        * This last one is especially bad for racing o_append users on other
-        * nodes.
-        */
-       mutex_unlock(&inode->i_mutex);
-       if (cl_io_is_trunc(ios->cis_io))
-               UP_WRITE_I_ALLOC_SEM(inode);
-       cio->u.setattr.cui_locks_released = 1;
        return 0;
 }
 
@@ -307,10 +333,10 @@ static int vvp_io_setattr_iter_init(const struct lu_env *env,
 static int vvp_io_setattr_lock(const struct lu_env *env,
                                const struct cl_io_slice *ios)
 {
-        struct ccc_io      *cio       = ccc_env_io(env);
-        struct cl_io       *io        = ios->cis_io;
-        size_t              new_size;
-        __u32               enqflags = 0;
+       struct ccc_io *cio = ccc_env_io(env);
+       struct cl_io  *io  = ios->cis_io;
+       __u64 new_size;
+       __u32 enqflags = 0;
 
         if (cl_io_is_trunc(io)) {
                 new_size = io->u.ci_setattr.sa_attr.lvb_size;
@@ -346,7 +372,7 @@ static int vvp_io_setattr_trunc(const struct lu_env *env,
                                 const struct cl_io_slice *ios,
                                 struct inode *inode, loff_t size)
 {
-       DOWN_WRITE_I_ALLOC_SEM(inode);
+       inode_dio_wait(inode);
        return 0;
 }
 
@@ -378,15 +404,10 @@ static int vvp_io_setattr_time(const struct lu_env *env,
 static int vvp_io_setattr_start(const struct lu_env *env,
                                const struct cl_io_slice *ios)
 {
-       struct ccc_io   *cio   = cl2ccc_io(env, ios);
        struct cl_io    *io    = ios->cis_io;
        struct inode    *inode = ccc_object_inode(io->ci_obj);
 
-       LASSERT(cio->u.setattr.cui_locks_released);
-
        mutex_lock(&inode->i_mutex);
-       cio->u.setattr.cui_locks_released = 0;
-
        if (cl_io_is_trunc(io))
                return vvp_io_setattr_trunc(env, ios, inode,
                                            io->u.ci_setattr.sa_attr.lvb_size);
@@ -397,30 +418,21 @@ static int vvp_io_setattr_start(const struct lu_env *env,
 static void vvp_io_setattr_end(const struct lu_env *env,
                                const struct cl_io_slice *ios)
 {
-        struct cl_io         *io    = ios->cis_io;
-        struct inode         *inode = ccc_object_inode(io->ci_obj);
-
-        if (!cl_io_is_trunc(io))
-                return;
-
-       /* Truncate in memory pages - they must be clean pages because osc
-        * has already notified to destroy osc_extents. */
-       vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
+       struct cl_io *io    = ios->cis_io;
+       struct inode *inode = ccc_object_inode(io->ci_obj);
+
+       if (cl_io_is_trunc(io)) {
+               /* Truncate in memory pages - they must be clean pages
+                * because osc has already notified to destroy osc_extents. */
+               vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
+               inode_dio_write_done(inode);
+       }
+       mutex_unlock(&inode->i_mutex);
 }
 
 static void vvp_io_setattr_fini(const struct lu_env *env,
                                const struct cl_io_slice *ios)
 {
-       struct ccc_io *cio   = ccc_env_io(env);
-       struct cl_io  *io    = ios->cis_io;
-       struct inode  *inode = ccc_object_inode(ios->cis_io->ci_obj);
-
-       if (cio->u.setattr.cui_locks_released) {
-               mutex_lock(&inode->i_mutex);
-               if (cl_io_is_trunc(io))
-                       DOWN_WRITE_I_ALLOC_SEM(inode);
-               cio->u.setattr.cui_locks_released = 0;
-       }
        vvp_io_fini(env, ios);
 }
 
@@ -473,6 +485,9 @@ static int vvp_io_read_start(const struct lu_env *env,
 
         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
 
+       if (!can_populate_pages(env, io, inode))
+               return 0;
+
         result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
         if (result != 0)
                 return result;
@@ -527,15 +542,16 @@ static int vvp_io_read_start(const struct lu_env *env,
         }
 
 out:
-        if (result >= 0) {
-                if (result < cnt)
-                        io->ci_continue = 0;
-                io->ci_nob += result;
-                ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
-                                  cio->cui_fd, pos, result, 0);
-                result = 0;
-        }
-        return result;
+       if (result >= 0) {
+               if (result < cnt)
+                       io->ci_continue = 0;
+               io->ci_nob += result;
+               ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
+                                 pos, result, READ);
+               result = 0;
+       }
+
+       return result;
 }
 
 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
@@ -563,6 +579,9 @@ static int vvp_io_write_start(const struct lu_env *env,
 
         ENTRY;
 
+       if (!can_populate_pages(env, io, inode))
+               return 0;
+
         if (cl_io_is_append(io)) {
                 /*
                  * PARALLEL IO This has to be changed for parallel IO doing
@@ -581,15 +600,16 @@ static int vvp_io_write_start(const struct lu_env *env,
         else
                 result = lustre_generic_file_write(file, cio, &pos);
 
-        if (result > 0) {
-                if (result < cnt)
-                        io->ci_continue = 0;
-                io->ci_nob += result;
-                ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
-                                  cio->cui_fd, pos, result, 0);
-                result = 0;
-        }
-        RETURN(result);
+       if (result > 0) {
+               if (result < cnt)
+                       io->ci_continue = 0;
+               io->ci_nob += result;
+               ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
+                                 cio->cui_fd, pos, result, WRITE);
+               result = 0;
+       }
+
+       RETURN(result);
 }
 
 #ifndef HAVE_VM_OP_FAULT
@@ -659,18 +679,18 @@ static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
 static int vvp_io_fault_start(const struct lu_env *env,
                               const struct cl_io_slice *ios)
 {
-        struct vvp_io       *vio     = cl2vvp_io(env, ios);
-        struct cl_io        *io      = ios->cis_io;
-        struct cl_object    *obj     = io->ci_obj;
-        struct inode        *inode   = ccc_object_inode(obj);
-        struct cl_fault_io  *fio     = &io->u.ci_fault;
-        struct vvp_fault_io *cfio    = &vio->u.fault;
-        loff_t               offset;
-        int                  result  = 0;
-        cfs_page_t          *vmpage  = NULL;
-        struct cl_page      *page;
-        loff_t               size;
-        pgoff_t              last; /* last page in a file data region */
+       struct vvp_io       *vio     = cl2vvp_io(env, ios);
+       struct cl_io        *io      = ios->cis_io;
+       struct cl_object    *obj     = io->ci_obj;
+       struct inode        *inode   = ccc_object_inode(obj);
+       struct cl_fault_io  *fio     = &io->u.ci_fault;
+       struct vvp_fault_io *cfio    = &vio->u.fault;
+       loff_t               offset;
+       int                  result  = 0;
+       cfs_page_t          *vmpage  = NULL;
+       struct cl_page      *page;
+       loff_t               size;
+       pgoff_t              last; /* last page in a file data region */
 
         if (fio->ft_executable &&
             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
@@ -685,30 +705,27 @@ static int vvp_io_fault_start(const struct lu_env *env,
         if (result != 0)
                 return result;
 
-        /* must return locked page */
-        if (fio->ft_mkwrite) {
-               /* we grab alloc_sem to exclude truncate case.
-                * Otherwise, we could add dirty pages into osc cache
-                * while truncate is on-going. */
-               DOWN_READ_I_ALLOC_SEM(inode);
-
-                LASSERT(cfio->ft_vmpage != NULL);
-                lock_page(cfio->ft_vmpage);
-        } else {
-                result = vvp_io_kernel_fault(cfio);
-                if (result != 0)
-                        return result;
-        }
+       /* must return locked page */
+       if (fio->ft_mkwrite) {
+               LASSERT(cfio->ft_vmpage != NULL);
+               lock_page(cfio->ft_vmpage);
+       } else {
+               result = vvp_io_kernel_fault(cfio);
+               if (result != 0)
+                       return result;
+       }
 
-        vmpage = cfio->ft_vmpage;
-        LASSERT(PageLocked(vmpage));
+       vmpage = cfio->ft_vmpage;
+       LASSERT(PageLocked(vmpage));
 
-        if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
-                ll_invalidate_page(vmpage);
+       if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
+               ll_invalidate_page(vmpage);
 
+       size = i_size_read(inode);
         /* Though we have already held a cl_lock upon this page, but
          * it still can be truncated locally. */
-        if (unlikely(vmpage->mapping == NULL)) {
+       if (unlikely((vmpage->mapping != inode->i_mapping) ||
+                    (page_offset(vmpage) > size))) {
                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
 
                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
@@ -716,6 +733,36 @@ static int vvp_io_fault_start(const struct lu_env *env,
                 GOTO(out, result = +1);
         }
 
+
+       if (fio->ft_mkwrite ) {
+               pgoff_t last_index;
+               /*
+                * Capture the size while holding the lli_trunc_sem from above
+                * we want to make sure that we complete the mkwrite action
+                * while holding this lock. We need to make sure that we are
+                * not past the end of the file.
+                */
+               last_index = cl_index(obj, size - 1);
+               if (last_index < fio->ft_index) {
+                       CDEBUG(D_PAGE,
+                               "llite: mkwrite and truncate race happened: "
+                               "%p: 0x%lx 0x%lx\n",
+                               vmpage->mapping,fio->ft_index,last_index);
+                       /*
+                        * We need to return if we are
+                        * passed the end of the file. This will propagate
+                        * up the call stack to ll_page_mkwrite where
+                        * we will return VM_FAULT_NOPAGE. Any non-negative
+                        * value returned here will be silently
+                        * converted to 0. If the vmpage->mapping is null
+                        * the error code would be converted back to ENODATA
+                        * in ll_page_mkwrite0. Thus we return -ENODATA
+                        * to handle both cases
+                        */
+                       GOTO(out, result = -ENODATA);
+               }
+       }
+
         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
         if (IS_ERR(page))
                 GOTO(out, result = PTR_ERR(page));
@@ -753,12 +800,17 @@ static int vvp_io_fault_start(const struct lu_env *env,
                                GOTO(out, result);
                        } else
                                cl_page_disown(env, io, page);
-                }
-        }
+               }
+       }
 
-        size = i_size_read(inode);
-        last = cl_index(obj, size - 1);
-        LASSERT(fio->ft_index <= last);
+       last = cl_index(obj, size - 1);
+       /*
+        * The ft_index is only used in the case of
+        * a mkwrite action. We need to check
+        * our assertions are correct, since
+        * we should have caught this above
+        */
+       LASSERT(!fio->ft_mkwrite || fio->ft_index <= last);
         if (fio->ft_index == last)
                 /*
                  * Last page is mapped partially.
@@ -772,11 +824,9 @@ static int vvp_io_fault_start(const struct lu_env *env,
         EXIT;
 
 out:
-        /* return unlocked vmpage to avoid deadlocking */
+       /* return unlocked vmpage to avoid deadlocking */
        if (vmpage != NULL)
                unlock_page(vmpage);
-       if (fio->ft_mkwrite)
-               UP_READ_I_ALLOC_SEM(inode);
 #ifdef HAVE_VM_OP_FAULT
        cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
 #endif
@@ -893,10 +943,10 @@ static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
                  * purposes here we can treat it like i_size.
                  */
                 if (attr->cat_kms <= offset) {
-                        char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
+                        char *kaddr = ll_kmap_atomic(cp->cpg_page, KM_USER0);
 
                         memset(kaddr, 0, cl_page_size(obj));
-                        kunmap_atomic(kaddr, KM_USER0);
+                        ll_kunmap_atomic(kaddr, KM_USER0);
                 } else if (cp->cpg_defer_uptodate)
                         cp->cpg_ra_used = 1;
                 else
@@ -962,6 +1012,7 @@ static int vvp_io_commit_write(const struct lu_env *env,
         struct cl_page    *pg     = slice->cpl_page;
         struct inode      *inode  = ccc_object_inode(obj);
         struct ll_sb_info *sbi    = ll_i2sbi(inode);
+       struct ll_inode_info *lli = ll_i2info(inode);
         cfs_page_t        *vmpage = cp->cpg_page;
 
         int    result;
@@ -1042,6 +1093,20 @@ static int vvp_io_commit_write(const struct lu_env *env,
         }
         ll_stats_ops_tally(sbi, tallyop, 1);
 
+       /* Inode should be marked DIRTY even if no new page was marked DIRTY
+        * because page could have been not flushed between 2 modifications.
+        * It is important the file is marked DIRTY as soon as the I/O is done
+        * Indeed, when cache is flushed, file could be already closed and it
+        * is too late to warn the MDT.
+        * It is acceptable that file is marked DIRTY even if I/O is dropped
+        * for some reasons before being flushed to OST.
+        */
+       if (result == 0) {
+               spin_lock(&lli->lli_lock);
+               lli->lli_flags |= LLIF_DATA_MODIFIED;
+               spin_unlock(&lli->lli_lock);
+       }
+
         size = cl_offset(obj, pg->cp_index) + to;
 
        ll_inode_size_lock(inode);
@@ -1142,11 +1207,28 @@ int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
                        io->ci_lockreq = CILR_MANDATORY;
        }
 
+       /* ignore layout change for generic CIT_MISC but not for glimpse.
+        * io context for glimpse must set ci_verify_layout to true,
+        * see cl_glimpse_size0() for details. */
+       if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
+               io->ci_ignore_layout = 1;
+
        /* Enqueue layout lock and get layout version. We need to do this
         * even for operations requiring to open file, such as read and write,
         * because it might not grant layout lock in IT_OPEN. */
-       if (result == 0 && !io->ci_ignore_layout)
+       if (result == 0 && !io->ci_ignore_layout) {
                result = ll_layout_refresh(inode, &cio->cui_layout_gen);
+               if (result == -ENOENT)
+                       /* If the inode on MDS has been removed, but the objects
+                        * on OSTs haven't been destroyed (async unlink), layout
+                        * fetch will return -ENOENT, we'd ingore this error
+                        * and continue with dirty flush. LU-3230. */
+                       result = 0;
+               if (result < 0)
+                       CERROR("%s: refresh file layout " DFID " error %d.\n",
+                               ll_get_fsname(inode->i_sb, NULL, 0),
+                               PFID(lu_object_fid(&obj->co_lu)), result);
+       }
 
        RETURN(result);
 }