Whamcloud - gitweb
LU-1824 obdfilter: reset lnb[n].rc in filter_grant_check()
[fs/lustre-release.git] / lustre / obdfilter / filter_io.c
index bfd7092..a946d90 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.
@@ -26,7 +24,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
@@ -42,9 +40,6 @@
 
 #define DEBUG_SUBSYSTEM S_FILTER
 
-#ifndef AUTOCONF_INCLUDED
-#include <linux/config.h>
-#endif
 #include <linux/module.h>
 #include <linux/pagemap.h> // XXX kill me soon
 #include <linux/version.h>
@@ -109,14 +104,14 @@ void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
         fed->fed_grant -= oa->o_dropped;
         fed->fed_dirty = oa->o_dirty;
 
-        if (oa->o_flags & OBD_FL_SHRINK_GRANT) {
+        if (oa->o_valid & OBD_MD_FLFLAGS && oa->o_flags & OBD_FL_SHRINK_GRANT) {
                 obd_size left_space = filter_grant_space_left(exp);
                 struct filter_obd *filter = &exp->exp_obd->u.filter;
 
-                /*Only if left_space < fo_tot_clients * 32M, 
+                /*Only if left_space < fo_tot_clients * 32M,
                  *then the grant space could be shrinked */
-                if (left_space < filter->fo_tot_granted_clients * 
-                                 FILTER_GRANT_SHRINK_LIMIT) { 
+                if (left_space < filter->fo_tot_granted_clients *
+                                 FILTER_GRANT_SHRINK_LIMIT) {
                         fed->fed_grant -= oa->o_grant;
                         filter->fo_tot_granted -= oa->o_grant;
                         CDEBUG(D_CACHE, "%s: cli %s/%p shrink "LPU64
@@ -132,7 +127,7 @@ void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
                 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
-                spin_unlock(&obd->obd_osfs_lock);
+                cfs_spin_unlock(&obd->obd_osfs_lock);
                 LBUG();
         }
         EXIT;
@@ -152,10 +147,11 @@ obd_size filter_grant_space_left(struct obd_export *exp)
 
         LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
 
-        if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
+        if (cfs_time_before_64(obd->obd_osfs_age,
+                               cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
 restat:
                 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
-                                   cfs_time_current_64() + HZ);
+                                   cfs_time_shift_64(OBD_STATFS_CACHE_SECONDS));
                 if (rc) /* N.B. statfs can't really fail */
                         RETURN(0);
                 statfs_done = 1;
@@ -198,9 +194,13 @@ restat:
 /* Calculate how much grant space to allocate to this client, based on how
  * much space is currently free and how much of that is already granted.
  *
+ * if @conservative != 0, we limit the maximum grant to FILTER_GRANT_CHUNK;
+ * otherwise we'll satisfy the requested amount as possible as we can, this
+ * is usually due to client reconnect.
+ *
  * Caller must hold obd_osfs_lock. */
 long filter_grant(struct obd_export *exp, obd_size current_grant,
-                  obd_size want, obd_size fs_space_left)
+                  obd_size want, obd_size fs_space_left, int conservative)
 {
         struct obd_device *obd = exp->exp_obd;
         struct filter_export_data *fed = &exp->exp_filter_data;
@@ -223,16 +223,11 @@ long filter_grant(struct obd_export *exp, obd_size current_grant,
                        obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
         } else if (current_grant < want &&
                    current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
-                grant = min((want >> blockbits),
-                            (fs_space_left >> blockbits) / 8);
-                grant <<= blockbits;
+                grant = min(want + (1 << blockbits) - 1, fs_space_left / 8);
+                grant &= ~((1ULL << blockbits) - 1);
 
                 if (grant) {
-                        /* Allow >FILTER_GRANT_CHUNK size when clients
-                         * reconnect due to a server reboot.
-                         */
-                        if ((grant > FILTER_GRANT_CHUNK) &&
-                            (!obd->obd_recovering))
+                        if (grant > FILTER_GRANT_CHUNK && conservative)
                                 grant = FILTER_GRANT_CHUNK;
 
                         obd->u.filter.fo_tot_granted += grant;
@@ -242,7 +237,7 @@ long filter_grant(struct obd_export *exp, obd_size current_grant,
                                        "current"LPU64"\n",
                                        obd->obd_name, exp->exp_client_uuid.uuid,
                                        exp, fed->fed_grant, want,current_grant);
-                                spin_unlock(&obd->obd_osfs_lock);
+                                cfs_spin_unlock(&obd->obd_osfs_lock);
                                 LBUG();
                         }
                 }
@@ -264,20 +259,25 @@ long filter_grant(struct obd_export *exp, obd_size current_grant,
 /*
  * the routine is used to request pages from pagecache
  *
- * use GFP_NOFS not allowing to enter FS as the client can run on this node
- * and we might end waiting on a page he sent in the request we're serving.
- *
+ * use GFP_NOFS for requests from a local client not allowing to enter FS
+ * as we might end up waiting on a page he sent in the request we're serving.
  * use __GFP_HIGHMEM so that the pages can use all of the available memory
  * on 32-bit machines
+ * use more aggressive GFP_HIGHUSER flags from non-local clients to be able to
+ * generate more memory pressure.
+ *
+ * See Bug 19529 and Bug 19917 for details.
  */
 static struct page *filter_get_page(struct obd_device *obd,
                                     struct inode *inode,
-                                    obd_off offset)
+                                    obd_off offset,
+                                    int localreq)
 {
         struct page *page;
 
         page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
-                                   GFP_NOFS | __GFP_HIGHMEM);
+                                   (localreq ? (GFP_NOFS | __GFP_HIGHMEM)
+                                             : GFP_HIGHUSER));
         if (unlikely(page == NULL))
                 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_NO_PAGE, 1);
 
@@ -335,56 +335,22 @@ static int filter_map_remote_to_local(int objcount, struct obd_ioobj *obj,
 }
 
 /*
- * the function is used to free all pages used for request
- * just to mimic cacheless OSS which don't occupy much memory
- */
-void filter_invalidate_cache(struct obd_device *obd, struct obd_ioobj *obj,
-                             struct niobuf_remote *nb, struct inode *inode)
-{
-        struct niobuf_remote *rnb;
-        int i;
-
-        LASSERT(inode != NULL);
-
-        for (i = 0, rnb = nb; i < obj->ioo_bufcnt; i++, rnb++) {
-                obd_off start;
-                obd_off end;
-
-                start = rnb->offset >> CFS_PAGE_SHIFT;
-                end = (rnb->offset + rnb->len) >> CFS_PAGE_SHIFT;
-                invalidate_mapping_pages(inode->i_mapping, start, end);
-                /* just to avoid warnings */
-                start = 0;
-                end = 0;
-        }
-}
-
-/*
  * the invalidate above doesn't work during read because lnet pins pages.
  * The truncate is used here instead to drop pages from cache
  */
-void filter_truncate_cache(struct obd_device *obd, struct obd_ioobj *obj,
-                           struct niobuf_remote *nb, int pages,
-                           struct niobuf_local *res, struct inode *inode)
+void filter_release_cache(struct obd_device *obd, struct obd_ioobj *obj,
+                          struct niobuf_remote *rnb, struct inode *inode)
 {
-        struct niobuf_remote *rnb;
         int i;
 
         LASSERT(inode != NULL);
-#ifdef HAVE_TRUNCATE_RANGE
-        for (i = 0, rnb = nb; i < obj->ioo_bufcnt; i++, rnb++) {
+        for (i = 0; i < obj->ioo_bufcnt; i++, rnb++) {
                 /* remove pages in which range is fit */
                 truncate_inode_pages_range(inode->i_mapping,
                                            rnb->offset & CFS_PAGE_MASK,
                                            (rnb->offset + rnb->len - 1) |
                                            ~CFS_PAGE_MASK);
         }
-#elif (defined HAVE_TRUNCATE_COMPLETE)
-        for (i = 0, lnb = res; i < pages; i++, lnb++)
-                truncate_complete_page(inode->i_mapping, lnb->page);
-#else
-#error "Nor truncate_inode_pages_range or truncate_complete_page are supported"
-#endif
 }
 
 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
@@ -404,6 +370,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
         int rc = 0, i, tot_bytes = 0;
         unsigned long now = jiffies;
         long timediff;
+        loff_t isize;
         ENTRY;
 
         /* We are currently not supporting multi-obj BRW_READ RPCS at all.
@@ -412,18 +379,19 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
         LASSERTF(objcount == 1, "%d\n", objcount);
         LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
 
-        rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
+        rc = filter_auth_capa(exp, NULL, oa->o_seq, capa,
                               CAPA_OPC_OSS_READ);
         if (rc)
                 RETURN(rc);
 
         if (oa && oa->o_valid & OBD_MD_FLGRANT) {
-                spin_lock(&obd->obd_osfs_lock);
+                cfs_spin_lock(&obd->obd_osfs_lock);
                 filter_grant_incoming(exp, oa);
 
-                if (!(oa->o_flags & OBD_FL_SHRINK_GRANT))
+                if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
+                    !(oa->o_flags & OBD_FL_SHRINK_GRANT))
                         oa->o_grant = 0;
-                spin_unlock(&obd->obd_osfs_lock);
+                cfs_spin_unlock(&obd->obd_osfs_lock);
         }
 
         iobuf = filter_iobuf_get(&obd->u.filter, oti);
@@ -431,7 +399,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                 RETURN(PTR_ERR(iobuf));
 
         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
-        dentry = filter_oa2dentry(obd, oa);
+        dentry = filter_oa2dentry(obd, &oa->o_oi);
         if (IS_ERR(dentry)) {
                 rc = PTR_ERR(dentry);
                 dentry = NULL;
@@ -439,6 +407,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
         }
 
         inode = dentry->d_inode;
+        isize = i_size_read(inode);
 
         obdo_to_inode(inode, oa, OBD_MD_FLATIME);
 
@@ -449,24 +418,24 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
         fsfilt_check_slow(obd, now, "preprw_read setup");
 
         /* find pages for all segments, fill array with them */
-        do_gettimeofday(&start);
+        cfs_gettimeofday(&start);
         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
 
                 lnb->dentry = dentry;
 
-                if (i_size_read(inode) <= lnb->offset)
+                if (isize <= lnb->offset)
                         /* If there's no more data, abort early.  lnb->rc == 0,
                          * so it's easy to detect later. */
                         break;
 
-                lnb->page = filter_get_page(obd, inode, lnb->offset);
+                lnb->page = filter_get_page(obd, inode, lnb->offset, 0);
                 if (lnb->page == NULL)
                         GOTO(cleanup, rc = -ENOMEM);
 
                 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_ACCESS, 1);
 
-                if (i_size_read(inode) < lnb->offset + lnb->len - 1)
-                        lnb->rc = i_size_read(inode) - lnb->offset;
+                if (isize < lnb->offset + lnb->len - 1)
+                        lnb->rc = isize - lnb->offset;
                 else
                         lnb->rc = lnb->len;
 
@@ -481,7 +450,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_MISS, 1);
                 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
         }
-        do_gettimeofday(&end);
+        cfs_gettimeofday(&end);
         timediff = cfs_timeval_sub(&end, &start, NULL);
         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
 
@@ -500,7 +469,8 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
                                     LPROC_FILTER_READ_BYTES, tot_bytes);
-
+       filter_counter_incr(exp, LPROC_FILTER_STATS_READ,
+                           oti ? oti->oti_jobid : NULL, tot_bytes);
         EXIT;
 
  cleanup:
@@ -548,6 +518,14 @@ static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
         int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
         unsigned long used = 0, ungranted = 0, using;
         int i, rc = -ENOSPC, obj, n = 0;
+        int resend = 0;
+
+        if ((oa->o_valid & OBD_MD_FLFLAGS) &&
+            (oa->o_flags & OBD_FL_RECOV_RESEND)) {
+                resend = 1;
+                CDEBUG(D_CACHE, "Recoverable resend arrived, skipping "
+                       "accounting\n");
+        }
 
         LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
 
@@ -562,9 +540,15 @@ static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
                         if (tmp)
                                 bytes += blocksize - tmp;
 
+                       lnb[n].rc = 0;
                         if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
                             (oa->o_valid & OBD_MD_FLGRANT)) {
-                                if (fed->fed_grant < used + bytes) {
+                                if (resend) {
+                                        /* this is a recoverable resent */
+                                        lnb[n].flags |= OBD_BRW_GRANTED;
+                                        rc = 0;
+                                        continue;
+                                } else if (fed->fed_grant < used + bytes) {
                                         CDEBUG(D_CACHE,
                                                "%s: cli %s/%p claims %ld+%d "
                                                "GRANT, real grant %lu idx %d\n",
@@ -640,7 +624,7 @@ static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
                 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
-                spin_unlock(&exp->exp_obd->obd_osfs_lock);
+                cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
                 LBUG();
         }
         return rc;
@@ -673,37 +657,69 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
         void *iobuf;
         obd_size left;
         unsigned long now = jiffies, timediff;
-        int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
+        int rc = 0, i, tot_bytes = 0, cleanup_phase = 0, localreq = 0;
+       int retries = 0;
         ENTRY;
         LASSERT(objcount == 1);
         LASSERT(obj->ioo_bufcnt > 0);
 
-        rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
+        rc = filter_auth_capa(exp, NULL, oa->o_seq, capa,
                               CAPA_OPC_OSS_WRITE);
         if (rc)
                 RETURN(rc);
 
+        if (exp->exp_connection &&
+            exp->exp_connection->c_peer.nid == exp->exp_connection->c_self)
+                localreq = 1;
+
         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
         iobuf = filter_iobuf_get(&obd->u.filter, oti);
         if (IS_ERR(iobuf))
                 GOTO(cleanup, rc = PTR_ERR(iobuf));
         cleanup_phase = 1;
 
-        dentry = filter_fid2dentry(obd, NULL, obj->ioo_gr,
+        dentry = filter_fid2dentry(obd, NULL, obj->ioo_seq,
                                    obj->ioo_id);
         if (IS_ERR(dentry))
                 GOTO(cleanup, rc = PTR_ERR(dentry));
         cleanup_phase = 2;
 
         if (dentry->d_inode == NULL) {
-                CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
-                       obd->obd_name, obj->ioo_id);
-                GOTO(cleanup, rc = -ENOENT);
+                if (exp->exp_obd->obd_recovering) {
+                        struct obdo *noa = oa;
+
+                        if (oa == NULL) {
+                                OBDO_ALLOC(noa);
+                                if (noa == NULL)
+                                        GOTO(recreate_out, rc = -ENOMEM);
+                                noa->o_id = obj->ioo_id;
+                                noa->o_valid = OBD_MD_FLID;
+                        }
+
+                        if (filter_create(NULL, exp, noa, NULL, oti) == 0) {
+                                f_dput(dentry);
+                                dentry = filter_fid2dentry(exp->exp_obd, NULL,
+                                                           obj->ioo_seq,
+                                                           obj->ioo_id);
+                        }
+                        if (oa == NULL)
+                                OBDO_FREE(noa);
+                }
+    recreate_out:
+                if (IS_ERR(dentry) || dentry->d_inode == NULL) {
+                        CERROR("%s: BRW to missing obj "LPU64"/"LPU64":rc %d\n",
+                               exp->exp_obd->obd_name,
+                               obj->ioo_id, obj->ioo_seq,
+                               IS_ERR(dentry) ? (int)PTR_ERR(dentry) : -ENOENT);
+                        if (IS_ERR(dentry))
+                                cleanup_phase = 1;
+                        GOTO(cleanup, rc = -ENOENT);
+                }
         }
 
         if (oa->o_valid & (OBD_MD_FLUID | OBD_MD_FLGID) &&
             dentry->d_inode->i_mode & (S_ISUID | S_ISGID)) {
-                rc = filter_capa_fixoa(exp, oa, obdo_mdsno(oa), capa);
+                rc = filter_capa_fixoa(exp, oa, oa->o_seq, capa);
                 if (rc)
                         GOTO(cleanup, rc);
         }
@@ -714,53 +730,79 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
 
         fsfilt_check_slow(obd, now, "preprw_write setup");
 
+        /* Filter truncate first locks i_mutex then partially truncated
+         * page, filter write code first locks pages then take
+         * i_mutex.  To avoid a deadlock in case of concurrent
+         * punch/write requests from one client, filter writes and
+         * filter truncates are serialized by i_alloc_sem, allowing
+         * multiple writes or single truncate. */
+        down_read(&dentry->d_inode->i_alloc_sem);
+        fsfilt_check_slow(obd, now, "i_alloc_sem");
+
         /* Don't update inode timestamps if this write is older than a
          * setattr which modifies the timestamps. b=10150 */
         /* XXX when we start having persistent reservations this needs to
          * be changed to filter_fmd_get() to create the fmd if it doesn't
          * already exist so we can store the reservation handle there. */
-        fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
-
-        LASSERT(oa != NULL);
-        spin_lock(&obd->obd_osfs_lock);
-        filter_grant_incoming(exp, oa);
-        if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
-                oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
-                                 OBD_MD_FLATIME);
-        else
-                obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
-                              OBD_MD_FLMTIME | OBD_MD_FLCTIME);
-        cleanup_phase = 3;
-
-        left = filter_grant_space_left(exp);
-
-        fso.fso_dentry = dentry;
-        fso.fso_bufcnt = *npages;
+        fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_seq);
+
+       LASSERT(oa != NULL);
+retry:
+       cfs_spin_lock(&obd->obd_osfs_lock);
+       if (retries == 0)
+               filter_grant_incoming(exp, oa);
+       if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
+               oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
+                                OBD_MD_FLATIME);
+       else
+               obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
+                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
+       cleanup_phase = 3;
+
+       left = filter_grant_space_left(exp);
+
+       fso.fso_dentry = dentry;
+       fso.fso_bufcnt = *npages;
+
+       rc = filter_grant_check(exp, oa, objcount, &fso, *npages, res,
+                               &left, dentry->d_inode);
+
+       /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
+        * for setting UID/GID and fid EA in first write time. */
+       /* If OBD_FL_SHRINK_GRANT is set, the client just returned us some grant
+        * so no sense in allocating it some more. We either return the grant
+        * back to the client if we have plenty of space or we don't return
+        * anything if we are short. This was decided in filter_grant_incoming*/
+       if ((retries == 0) && (oa->o_valid & OBD_MD_FLGRANT) &&
+           (!(oa->o_valid & OBD_MD_FLFLAGS) ||
+            !(oa->o_flags & OBD_FL_SHRINK_GRANT)))
+               oa->o_grant = filter_grant(exp, oa->o_grant, oa->o_undirty,
+                                          left, 1);
+
+       cfs_spin_unlock(&obd->obd_osfs_lock);
+
+       if (rc == -ENOSPC && retries == 0) {
+               void *handle = NULL;
+
+               CDEBUG(D_INODE, "retry after commit pending journals");
+
+               retries = 1;
+               handle = fsfilt_start(obd, dentry->d_inode,
+                                     FSFILT_OP_SETATTR, NULL);
+               if (handle != NULL &&
+                   fsfilt_commit(obd, dentry->d_inode, handle, 1) == 0)
+                       goto retry;
+       }
 
-        rc = filter_grant_check(exp, oa, objcount, &fso, *npages, res,
-                                &left, dentry->d_inode);
-
-        /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
-         * for setting UID/GID and fid EA in first write time. */
-        if (oa->o_valid & OBD_MD_FLGRANT)
-                oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
-
-        spin_unlock(&obd->obd_osfs_lock);
         filter_fmd_put(exp, fmd);
 
+        OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK2, (obd_timeout + 1) / 4);
+
         if (rc)
                 GOTO(cleanup, rc);
         cleanup_phase = 4;
 
-        /* Filter truncate first locks i_mutex then partally truncated
-         * page, filter write code first locks pages then take
-         * i_mutex.  To avoid a deadlock in case of concurrent
-         * punch/write requests from one client, filter writes and
-         * filter truncates are serialized by i_alloc_sem, allowing
-         * multiple writes or single truncate. */
-        down_read(&dentry->d_inode->i_alloc_sem);
-
-        do_gettimeofday(&start);
+        cfs_gettimeofday(&start);
         for (i = 0, lnb = res; i < *npages; i++, lnb++) {
 
                 /* We still set up for ungranted pages so that granted pages
@@ -768,7 +810,8 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
                  * needs to keep the pages all aligned properly. */
                 lnb->dentry = dentry;
 
-                lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset);
+                lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset,
+                                            localreq);
                 if (lnb->page == NULL)
                         GOTO(cleanup, rc = -ENOMEM);
 
@@ -780,8 +823,7 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
                  * be able to proceed in filter_commitrw_write(). thus let's
                  * just wait for writeout completion, should be rare enough.
                  * -bzzz */
-                if (obd->u.filter.fo_writethrough_cache)
-                        wait_on_page_writeback(lnb->page);
+                wait_on_page_writeback(lnb->page);
                 BUG_ON(PageWriteback(lnb->page));
 
                 /* If the filter writes a partial page, then has the file
@@ -819,7 +861,7 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
                 if (lnb->rc == 0)
                         tot_bytes += lnb->len;
         }
-        do_gettimeofday(&end);
+        cfs_gettimeofday(&end);
         timediff = cfs_timeval_sub(&end, &start, NULL);
         lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
 
@@ -832,9 +874,14 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
 
         fsfilt_check_slow(obd, now, "start_page_write");
 
+        lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
+                            tot_bytes);
+
         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
                                     LPROC_FILTER_WRITE_BYTES, tot_bytes);
+       filter_counter_incr(exp, LPROC_FILTER_STATS_WRITE,
+                           oti ? oti->oti_jobid : NULL, tot_bytes);
         EXIT;
 cleanup:
         switch(cleanup_phase) {
@@ -847,9 +894,11 @@ cleanup:
                                         lnb->page = NULL;
                                 }
                         }
-                        up_read(&dentry->d_inode->i_alloc_sem);
                 }
         case 3:
+                if (rc)
+                        up_read(&dentry->d_inode->i_alloc_sem);
+
                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
         case 2:
                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
@@ -859,10 +908,10 @@ cleanup:
         case 1:
                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
         case 0:
-                spin_lock(&obd->obd_osfs_lock);
+                cfs_spin_lock(&obd->obd_osfs_lock);
                 if (oa)
                         filter_grant_incoming(exp, oa);
-                spin_unlock(&obd->obd_osfs_lock);
+                cfs_spin_unlock(&obd->obd_osfs_lock);
                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
                 break;
         default:;
@@ -870,8 +919,8 @@ cleanup:
         return rc;
 }
 
-int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
-                  int objcount, struct obd_ioobj *obj,
+int filter_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
+                  struct obdo *oa, int objcount, struct obd_ioobj *obj,
                   struct niobuf_remote *nb, int *npages,
                   struct niobuf_local *res, struct obd_trans_info *oti,
                   struct lustre_capa *capa)
@@ -901,7 +950,7 @@ static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
         int i;
         ENTRY;
 
-        osc_build_res_name(obj->ioo_id, obj->ioo_gr, &res_id);
+        osc_build_res_name(obj->ioo_id, obj->ioo_seq, &res_id);
         /* If oa != NULL then filter_preprw_read updated the inode atime
          * and we should update the lvb so that other glimpses will also
          * get the updated value. bug 5972 */
@@ -910,7 +959,7 @@ static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
 
                 if (resource != NULL) {
                         LDLM_RESOURCE_ADDREF(resource);
-                        ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
+                        ns->ns_lvbo->lvbo_update(resource, NULL, 1);
                         LDLM_RESOURCE_DELREF(resource);
                         ldlm_resource_putref(resource);
                 }
@@ -919,17 +968,15 @@ static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
         if (res->dentry != NULL)
                 inode = res->dentry->d_inode;
 
-        for (i = 0, lnb = res; i < npages; i++, lnb++)
-                if (lnb->page != NULL)
+        for (i = 0, lnb = res; i < npages; i++, lnb++) {
+                if (lnb->page != NULL) {
                         page_cache_release(lnb->page);
-
+                        lnb->page = NULL;
+                }
+        }
         if (inode && (fo->fo_read_cache == 0 ||
                       i_size_read(inode) > fo->fo_readcache_max_filesize))
-                filter_truncate_cache(exp->exp_obd, obj, rnb, npages, res,
-                                      inode);
-
-        for (i = 0, lnb = res; i < npages; i++, lnb++)
-                lnb->page = NULL;
+                filter_release_cache(exp->exp_obd, obj, rnb, inode);
 
         if (res->dentry != NULL)
                 f_dput(res->dentry);
@@ -944,7 +991,7 @@ void filter_grant_commit(struct obd_export *exp, int niocount,
         unsigned long pending = 0;
         int i;
 
-        spin_lock(&exp->exp_obd->obd_osfs_lock);
+        cfs_spin_lock(&exp->exp_obd->obd_osfs_lock);
         for (i = 0, lnb = res; i < niocount; i++, lnb++)
                 pending += lnb->lnb_grant_used;
 
@@ -964,11 +1011,11 @@ void filter_grant_commit(struct obd_export *exp, int niocount,
                  filter->fo_tot_pending, pending);
         filter->fo_tot_pending -= pending;
 
-        spin_unlock(&exp->exp_obd->obd_osfs_lock);
+        cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
 }
 
-int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
-                    int objcount, struct obd_ioobj *obj,
+int filter_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
+                    struct obdo *oa, int objcount, struct obd_ioobj *obj,
                     struct niobuf_remote *nb, int npages,
                     struct niobuf_local *res, struct obd_trans_info *oti,
                     int rc)
@@ -982,47 +1029,3 @@ int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
         LBUG();
         return -EPROTO;
 }
-
-int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
-               obd_count oa_bufs, struct brw_page *pga,
-               struct obd_trans_info *oti)
-{
-        struct obd_ioobj ioo;
-        struct niobuf_local *lnb;
-        struct niobuf_remote *rnb;
-        obd_count i;
-        int ret = 0, npages;
-        ENTRY;
-
-        OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
-        OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
-
-        if (lnb == NULL || rnb == NULL)
-                GOTO(out, ret = -ENOMEM);
-
-        for (i = 0; i < oa_bufs; i++) {
-                lnb[i].page = pga[i].pg;
-                rnb[i].offset = pga[i].off;
-                rnb[i].len = pga[i].count;
-        }
-
-        obdo_to_ioobj(oinfo->oi_oa, &ioo);
-        ioo.ioo_bufcnt = oa_bufs;
-
-        npages = oa_bufs;
-        ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
-                            rnb, &npages, lnb, oti, oinfo_capa(oinfo));
-        if (ret != 0)
-                GOTO(out, ret);
-        LASSERTF(oa_bufs == npages, "%u != %u\n", oa_bufs, npages);
-
-        ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo, rnb,
-                              npages, lnb, oti, ret);
-
-out:
-        if (lnb)
-                OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
-        if (rnb)
-                OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));
-        RETURN(ret);
-}