Whamcloud - gitweb
LU-1030 clio: reimplement ll_fsync in clio way
[fs/lustre-release.git] / lustre / llite / vvp_io.c
index 32a8905..4a65b9e 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -28,6 +26,8 @@
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -130,6 +130,7 @@ static int vvp_mmap_locks(const struct lu_env *env,
                           struct ccc_io *vio, struct cl_io *io)
 {
         struct ccc_thread_info *cti = ccc_env_info(env);
+        struct mm_struct       *mm = current->mm;
         struct vm_area_struct  *vma;
         struct cl_lock_descr   *descr = &cti->cti_descr;
         ldlm_policy_data_t      policy;
@@ -144,6 +145,13 @@ static int vvp_mmap_locks(const struct lu_env *env,
         if (!cl_is_normalio(env, io))
                 RETURN(0);
 
+        if (vio->cui_iov == NULL) /* nfs or loop back device write */
+                RETURN(0);
+
+        /* No MM (e.g. NFS)? No vmas too. */
+        if (mm == NULL)
+                RETURN(0);
+
         for (seg = 0; seg < vio->cui_nrsegs; seg++) {
                 const struct iovec *iv = &vio->cui_iov[seg];
 
@@ -154,12 +162,14 @@ static int vvp_mmap_locks(const struct lu_env *env,
 
                 count += addr & (~CFS_PAGE_MASK);
                 addr &= CFS_PAGE_MASK;
-                while((vma = our_vma(addr, count)) != NULL) {
+
+                down_read(&mm->mmap_sem);
+                while((vma = our_vma(mm, addr, count)) != NULL) {
                         struct inode *inode = vma->vm_file->f_dentry->d_inode;
                         int flags = CEF_MUST;
 
                         if (ll_file_nolock(vma->vm_file)) {
-                                /* 
+                                /*
                                  * For no lock case, a lockless lock will be
                                  * generated.
                                  */
@@ -194,6 +204,7 @@ static int vvp_mmap_locks(const struct lu_env *env,
                         count -= vma->vm_end - addr;
                         addr = vma->vm_end;
                 }
+                up_read(&mm->mmap_sem);
         }
         RETURN(0);
 }
@@ -323,13 +334,11 @@ static int vvp_do_vmtruncate(struct inode *inode, size_t size)
         int     result;
         /*
          * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
-         * is grabbed by ll_truncate() only over call to obd_adjust_kms().  If
-         * vmtruncate returns 0, then ll_truncate dropped ll_inode_size_lock()
+         * is grabbed by ll_truncate() only over call to obd_adjust_kms().
          */
         ll_inode_size_lock(inode, 0);
         result = vmtruncate(inode, size);
-        if (result != 0)
-                ll_inode_size_unlock(inode, 0);
+        ll_inode_size_unlock(inode, 0);
 
         return result;
 }
@@ -509,7 +518,7 @@ static int vvp_io_read_start(const struct lu_env *env,
 
         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
 
-        result = ccc_prep_size(env, obj, io, pos, tot, 1, &exceed);
+        result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
         if (result != 0)
                 return result;
         else if (exceed != 0)
@@ -551,6 +560,10 @@ static int vvp_io_read_start(const struct lu_env *env,
                 result = generic_file_splice_read(file, &pos,
                                 vio->u.splice.cui_pipe, cnt,
                                 vio->u.splice.cui_flags);
+                /* LU-1109: do splice read stripe by stripe otherwise if it
+                 * may make nfsd stuck if this read occupied all internal pipe
+                 * buffers. */
+                io->ci_continue = 0;
                 break;
 #endif
         default:
@@ -635,45 +648,43 @@ static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
                        cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
 
         cfio->ft_vmpage = vmpage;
+        lock_page(vmpage);
 
         return 0;
 }
 #else
 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
 {
-        cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, cfio->fault.ft_vmf);
-
-        if (cfio->fault.ft_vmf->page) {
-                LL_CDEBUG_PAGE(D_PAGE, cfio->fault.ft_vmf->page,
-                               "got addr %p type NOPAGE\n",
-                               cfio->fault.ft_vmf->virtual_address);
-                /*XXX workaround to bug in CLIO - he deadlocked with
-                 lock cancel if page locked  */
-                if (likely(cfio->fault.ft_flags & VM_FAULT_LOCKED)) {
-                        unlock_page(cfio->fault.ft_vmf->page);
-                        cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
+        struct vm_fault *vmf = cfio->fault.ft_vmf;
+
+        cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
+
+        if (vmf->page) {
+                LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
+                               vmf->virtual_address);
+                if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
+                        lock_page(vmf->page);
+                        cfio->fault.ft_flags &= VM_FAULT_LOCKED;
                 }
 
-                cfio->ft_vmpage = cfio->fault.ft_vmf->page;
+                cfio->ft_vmpage = vmf->page;
                 return 0;
         }
 
-        if (unlikely (cfio->fault.ft_flags & VM_FAULT_ERROR)) {
-                CDEBUG(D_PAGE, "got addr %p - SIGBUS\n",
-                       cfio->fault.ft_vmf->virtual_address);
+        if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
+                CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
                 return -EFAULT;
         }
 
-        if (unlikely (cfio->fault.ft_flags & VM_FAULT_NOPAGE)) {
-                CDEBUG(D_PAGE, "got addr %p - OOM\n",
-                       cfio->fault.ft_vmf->virtual_address);
+        if (cfio->fault.ft_flags & VM_FAULT_OOM) {
+                CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
                 return -ENOMEM;
         }
 
-        if (unlikely(cfio->fault.ft_flags & VM_FAULT_RETRY))
+        if (cfio->fault.ft_flags & VM_FAULT_RETRY)
                 return -EAGAIN;
 
-        CERROR("unknow error in page fault!\n");
+        CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
         return -EINVAL;
 }
 
@@ -689,8 +700,8 @@ static int vvp_io_fault_start(const struct lu_env *env,
         struct cl_fault_io  *fio     = &io->u.ci_fault;
         struct vvp_fault_io *cfio    = &vio->u.fault;
         loff_t               offset;
-        int                  kernel_result = 0;
         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 */
@@ -704,56 +715,100 @@ static int vvp_io_fault_start(const struct lu_env *env,
         /* offset of the last byte on the page */
         offset = cl_offset(obj, fio->ft_index + 1) - 1;
         LASSERT(cl_index(obj, offset) == fio->ft_index);
-        result = ccc_prep_size(env, obj, io, 0, offset + 1, 0, NULL);
+        result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
         if (result != 0)
                 return result;
 
-        /* must return unlocked page */
-        kernel_result = vvp_io_kernel_fault(cfio);
-        if (kernel_result != 0)
-                return kernel_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));
+
+        if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
+                ll_invalidate_page(vmpage);
 
-        /* Temporarily lock vmpage to keep cl_page_find() happy. */
-        lock_page(cfio->ft_vmpage);
         /* Though we have already held a cl_lock upon this page, but
          * it still can be truncated locally. */
-        page = ERR_PTR(-EFAULT);
-        if (likely(cfio->ft_vmpage->mapping != NULL))
-                page = cl_page_find(env, obj, fio->ft_index, cfio->ft_vmpage,
-                                    CPT_CACHEABLE);
-        unlock_page(cfio->ft_vmpage);
-        if (IS_ERR(page)) {
-                page_cache_release(cfio->ft_vmpage);
-                cfio->ft_vmpage = NULL;
-                return PTR_ERR(page);
+        if (unlikely(vmpage->mapping == NULL)) {
+                CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
+
+                /* return +1 to stop cl_io_loop() and ll_fault() will catch
+                 * and retry. */
+                GOTO(out, result = +1);
+        }
+
+        page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
+        if (IS_ERR(page))
+                GOTO(out, result = PTR_ERR(page));
+
+        /* if page is going to be written, we should add this page into cache
+         * earlier. */
+        if (fio->ft_mkwrite) {
+                wait_on_page_writeback(vmpage);
+                if (set_page_dirty(vmpage)) {
+                        struct ccc_page *cp;
+
+                        /* vvp_page_assume() calls wait_on_page_writeback(). */
+                        cl_page_assume(env, io, page);
+
+                        cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
+                        vvp_write_pending(cl2ccc(obj), cp);
+
+                        /* Do not set Dirty bit here so that in case IO is
+                         * started before the page is really made dirty, we
+                         * still have chance to detect it. */
+                        result = cl_page_cache_add(env, io, page, CRT_WRITE);
+                        if (result < 0) {
+                                cl_page_unassume(env, io, page);
+                                cl_page_put(env, page);
+
+                                /* we're in big trouble, what can we do now? */
+                                if (result == -EDQUOT)
+                                        result = -ENOSPC;
+                                GOTO(out, result);
+                        }
+                }
         }
 
         size = i_size_read(inode);
         last = cl_index(obj, size - 1);
+        LASSERT(fio->ft_index <= last);
         if (fio->ft_index == last)
                 /*
                  * Last page is mapped partially.
                  */
                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
-         else
+        else
                 fio->ft_nob = cl_page_size(obj);
 
-         lu_ref_add(&page->cp_reference, "fault", io);
-         fio->ft_page = page;
-         /*
-          * Certain 2.6 kernels return not-NULL from
-          * filemap_nopage() when page is beyond the file size,
-          * on the grounds that "An external ptracer can access
-          * pages that normally aren't accessible.." Don't
-          * propagate such page fault to the lower layers to
-          * avoid side-effects like KMS updates.
-          */
-          if (fio->ft_index > last)
-                result = +1;
+        lu_ref_add(&page->cp_reference, "fault", io);
+        fio->ft_page = page;
+        EXIT;
 
+out:
+        /* return unlocked vmpage to avoid deadlocking */
+        unlock_page(vmpage);
+#ifdef HAVE_VM_OP_FAULT
+        cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
+#endif
         return result;
 }
 
+static void vvp_io_fsync_end(const struct lu_env *env,
+                            const struct cl_io_slice *ios)
+{
+       /* never try to verify there is no dirty pages in sync range
+        * because page_mkwrite() can generate new dirty pages any time. */
+}
+
 static int vvp_io_read_page(const struct lu_env *env,
                             const struct cl_io_slice *ios,
                             const struct cl_page_slice *slice)
@@ -775,7 +830,8 @@ static int vvp_io_read_page(const struct lu_env *env,
 
         ENTRY;
 
-        if (sbi->ll_ra_info.ra_max_pages_per_file)
+        if (sbi->ll_ra_info.ra_max_pages_per_file &&
+            sbi->ll_ra_info.ra_max_pages)
                 ras_update(sbi, inode, ras, page->cp_index,
                            cp->cpg_defer_uptodate);
 
@@ -798,7 +854,8 @@ static int vvp_io_read_page(const struct lu_env *env,
          * this will unlock it automatically as part of cl_page_list_disown().
          */
         cl_2queue_add(queue, page);
-        if (sbi->ll_ra_info.ra_max_pages_per_file)
+        if (sbi->ll_ra_info.ra_max_pages_per_file &&
+            sbi->ll_ra_info.ra_max_pages)
                 ll_readahead(env, io, ras,
                              vmpage->mapping, &queue->c2_qin, fd->fd_flags);
 
@@ -1050,6 +1107,10 @@ static const struct cl_io_operations vvp_io_ops = {
                         .cio_start     = vvp_io_fault_start,
                         .cio_end       = ccc_io_end
                 },
+               [CIT_FSYNC] = {
+                       .cio_end    = vvp_io_fsync_end,
+                       .cio_fini   = vvp_io_fini
+               },
                 [CIT_MISC] = {
                         .cio_fini   = vvp_io_fini
                 }
@@ -1064,8 +1125,6 @@ int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
 {
         struct vvp_io      *vio   = vvp_env_io(env);
         struct ccc_io      *cio   = ccc_env_io(env);
-        struct inode       *inode = ccc_object_inode(obj);
-        struct ll_sb_info  *sbi   = ll_i2sbi(inode);
         int                 result;
 
         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
@@ -1077,6 +1136,7 @@ int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
         result = 0;
         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
                 size_t count;
+               struct ll_inode_info *lli = ll_i2info(ccc_object_inode(obj));
 
                 count = io->u.ci_rw.crw_count;
                 /* "If nbyte is 0, read() will return 0 and have no other
@@ -1087,11 +1147,15 @@ int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
                         cio->cui_tot_count = count;
                         cio->cui_tot_nrsegs = 0;
                 }
+               /* for read/write, we store the jobid in the inode, and
+                * it'll be fetched by osc when building RPC.
+                *
+                * it's not accurate if the file is shared by different
+                * jobs.
+                */
+               lustre_get_jobid(lli->lli_jobid);
         } else if (io->ci_type == CIT_SETATTR) {
-                if (cl_io_is_trunc(io))
-                        /* lockless truncate? */
-                        ll_stats_ops_tally(sbi, LPROC_LL_TRUNC, 1);
-                else
+                if (!cl_io_is_trunc(io))
                         io->ci_lockreq = CILR_MANDATORY;
         }
         RETURN(result);