Whamcloud - gitweb
minor fix for buffer offsets.
[fs/lustre-release.git] / lustre / obdfilter / filter_io.c
index c2867b5..10cc687 100644 (file)
 
 #include <linux/obd_class.h>
 #include <linux/lustre_fsfilt.h>
+#include <linux/lustre_smfs.h>
+#include <linux/lustre_snap.h>
 #include "filter_internal.h"
 
-static int filter_start_page_read(struct inode *inode, struct niobuf_local *lnb)
+static int filter_start_page_read(struct obd_device *obd, struct inode *inode,
+                                  struct niobuf_local *lnb)
 {
-        struct address_space *mapping = inode->i_mapping;
         struct page *page;
         unsigned long index = lnb->offset >> PAGE_SHIFT;
-        int rc;
-
-        page = grab_cache_page(mapping, index); /* locked page */
-        if (page == NULL)
-                return lnb->rc = -ENOMEM;
 
-        LASSERT(page->mapping == mapping);
-
-        lnb->page = page;
+        page = fsfilt_getpage(obd, inode, index);
+        if (IS_ERR(page)) {
+                CERROR("page index %lu, rc = %ld\n", index, PTR_ERR(page));
 
-        if (inode->i_size < lnb->offset + lnb->len - 1)
-                lnb->rc = inode->i_size - lnb->offset;
-        else
-                lnb->rc = lnb->len;
-
-        if (PageUptodate(page)) {
-                unlock_page(page);
-                return 0;
-        }
-
-        rc = mapping->a_ops->readpage(NULL, page);
-        if (rc < 0) {
-                CERROR("page index %lu, rc = %d\n", index, rc);
                 lnb->page = NULL;
-                page_cache_release(page);
-                return lnb->rc = rc;
+                lnb->rc = PTR_ERR(page);
+                return lnb->rc;
         }
 
+        lnb->page = page;
+
         return 0;
 }
 
@@ -106,8 +92,13 @@ static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
 {
         struct filter_export_data *fed;
         struct obd_device *obd = exp->exp_obd;
+        static unsigned long last_msg;
+        static int last_count;
+        int mask = D_CACHE;
         ENTRY;
 
+        LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
+
         if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
                                         (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
                 oa->o_valid &= ~OBD_MD_FLGRANT;
@@ -117,13 +108,22 @@ static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
 
         fed = &exp->exp_filter_data;
 
+        /* Don't print this to the console the first time it happens, since
+         * it can happen legitimately on occasion, but only rarely. */
+        if (time_after(jiffies, last_msg + 60 * HZ)) {
+                last_count = 0;
+                last_msg = jiffies;
+        }
+        if ((last_count & (-last_count)) == last_count)
+                mask = D_WARNING;
+        last_count++;
+
         /* Add some margin, since there is a small race if other RPCs arrive
          * out-or-order and have already consumed some grant.  We want to
          * leave this here in case there is a large error in accounting. */
-        CDEBUG(oa->o_grant > fed->fed_grant + FILTER_GRANT_CHUNK ?
-               D_ERROR : D_CACHE,
-               "%s: cli %s reports granted: "LPU64" dropped: %u, local: %lu\n",
-               obd->obd_name, exp->exp_client_uuid.uuid, oa->o_grant,
+        CDEBUG(oa->o_grant > fed->fed_grant + FILTER_GRANT_CHUNK ? mask:D_CACHE,
+               "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
+               obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
                oa->o_dropped, fed->fed_grant);
 
         /* Update our accounting now so that statfs takes it into account.
@@ -132,14 +132,14 @@ static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
          * on fed_dirty however. */
         obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
         if (fed->fed_grant < oa->o_dropped) {
-                CERROR("%s: cli %s reports %u dropped > fed_grant %lu\n",
-                       obd->obd_name, exp->exp_client_uuid.uuid,
+                CERROR("%s: cli %s/%p reports %u dropped > fed_grant %lu\n",
+                       obd->obd_name, exp->exp_client_uuid.uuid, exp,
                        oa->o_dropped, fed->fed_grant);
                 oa->o_dropped = 0;
         }
         if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
-                CERROR("%s: cli %s reports %u dropped > tot_granted "LPU64"\n",
-                       obd->obd_name, exp->exp_client_uuid.uuid,
+                CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
+                       obd->obd_name, exp->exp_client_uuid.uuid, exp,
                        oa->o_dropped, obd->u.filter.fo_tot_granted);
                 oa->o_dropped = 0;
         }
@@ -149,7 +149,7 @@ static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
         EXIT;
 }
 
-#define GRANT_FOR_LLOG 16
+#define GRANT_FOR_LLOG(obd) 16
 
 /* Figure out how much space is available between what we've granted
  * and what remains in the filesystem.  Compensate for ext3 indirect
@@ -163,6 +163,8 @@ obd_size filter_grant_space_left(struct obd_export *exp)
         obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
         int rc, statfs_done = 0;
 
+        LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
+
         if (time_before(obd->obd_osfs_age, jiffies - HZ)) {
 restat:
                 rc = fsfilt_statfs(obd, obd->u.filter.fo_sb, jiffies + 1);
@@ -173,8 +175,8 @@ restat:
 
         avail = obd->obd_osfs.os_bavail;
         left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
-        if (left > GRANT_FOR_LLOG) {
-                left = (left - GRANT_FOR_LLOG) << blockbits;
+        if (left > GRANT_FOR_LLOG(obd)) {
+                left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
         } else {
                 left = 0 /* << blockbits */;
         }
@@ -191,10 +193,10 @@ restat:
                 if (left < tot_granted - obd->u.filter.fo_tot_pending &&
                     time_after(jiffies, next)) {
                         spin_unlock(&obd->obd_osfs_lock);
-                        CERROR("%s: cli %s granted "LPU64" more than available "
+                        CERROR("%s: cli %s/%p grant "LPU64" > available "
                                LPU64" and pending "LPU64"\n", obd->obd_name,
-                               exp->exp_client_uuid.uuid, tot_granted, left,
-                               obd->u.filter.fo_tot_pending);
+                               exp->exp_client_uuid.uuid, exp, tot_granted,
+                               left, obd->u.filter.fo_tot_pending);
                         if (next == 0)
                                 portals_debug_dumplog();
                         next = jiffies + 20 * HZ;
@@ -203,11 +205,11 @@ restat:
                 left = 0;
         }
 
-        CDEBUG(D_CACHE, "%s: cli %s free: "LPU64" avail: "LPU64" grant "LPU64
+        CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
                " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
-               exp->exp_client_uuid.uuid, obd->obd_osfs.os_bfree << blockbits,
-               avail << blockbits, tot_granted, left,
-               obd->u.filter.fo_tot_pending);
+               exp->exp_client_uuid.uuid, exp,
+               obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
+               tot_granted, left, obd->u.filter.fo_tot_pending);
 
         return left;
 }
@@ -224,15 +226,19 @@ long filter_grant(struct obd_export *exp, obd_size current_grant,
         int blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
         __u64 grant = 0;
 
+        LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
+
         /* Grant some fraction of the client's requested grant space so that
          * they are not always waiting for write credits (not all of it to
          * avoid overgranting in face of multiple RPCs in flight).  This
          * essentially will be able to control the OSC_MAX_RIF for a client.
          *
-         * If we do have a large disparity and multiple RPCs in flight we
-         * might grant "too much" but that's OK because it means we are
-         * dirtying a lot on the client and will likely use it up quickly. */
-        if (current_grant < want) {
+         * If we do have a large disparity between what the client thinks it
+         * has and what we think it has, don't grant very much and let the
+         * client consume its grant first.  Either it just has lots of RPCs
+         * in flight, or it was evicted and its grants will soon be used up. */
+        if (current_grant < want &&
+            current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
                 grant = min((want >> blockbits) / 2,
                             (fs_space_left >> blockbits) / 8);
                 grant <<= blockbits;
@@ -246,12 +252,12 @@ long filter_grant(struct obd_export *exp, obd_size current_grant,
                 }
         }
 
-        CDEBUG(D_CACHE,"%s: cli %s wants: "LPU64" granting: "LPU64"\n",
-               obd->obd_name, exp->exp_client_uuid.uuid, want, grant);
+        CDEBUG(D_CACHE,"%s: cli %s/%p wants: "LPU64" granting: "LPU64"\n",
+               obd->obd_name, exp->exp_client_uuid.uuid, exp, want, grant);
         CDEBUG(D_CACHE,
-               "%s: cli %s tot cached:"LPU64" granted:"LPU64
+               "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
                " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
-               obd->u.filter.fo_tot_dirty,
+               exp, obd->u.filter.fo_tot_dirty,
                obd->u.filter.fo_tot_granted, obd->obd_num_exports);
 
         return grant;
@@ -264,7 +270,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                               struct obd_trans_info *oti)
 {
         struct obd_device *obd = exp->exp_obd;
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
         struct obd_ioobj *o;
         struct niobuf_remote *rnb;
         struct niobuf_local *lnb = NULL;
@@ -300,7 +306,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
 
         memset(res, 0, niocount * sizeof(*res));
 
-        push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+        push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
         for (i = 0, o = obj; i < objcount; i++, o++) {
                 LASSERT(o->ioo_bufcnt);
 
@@ -341,7 +347,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                                  * easy to detect later. */
                                 break;
                         } else {
-                                rc = filter_start_page_read(inode, lnb);
+                                rc = filter_start_page_read(obd, inode, lnb);
                         }
 
                         if (rc) {
@@ -353,6 +359,11 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                                 GOTO(cleanup, rc);
                         }
 
+                        if (inode->i_size < lnb->offset + lnb->len - 1)
+                                lnb->rc = inode->i_size - lnb->offset;
+                        else
+                                lnb->rc = lnb->len;
+
                         tot_bytes += lnb->rc;
                         if (lnb->rc < lnb->len) {
                                 /* short read, be sure to wait on it */
@@ -402,7 +413,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                         CERROR("NULL dentry in cleanup -- tell CFS\n");
         case 0:
                 OBD_FREE(fso, objcount * sizeof(*fso));
-                pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+                pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
         }
         return rc;
 }
@@ -426,6 +437,8 @@ static int filter_grant_check(struct obd_export *exp, int objcount,
         unsigned long used = 0, ungranted = 0, using;
         int i, rc = -ENOSPC, obj, n = 0, mask = D_CACHE;
 
+        LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
+
         for (obj = 0; obj < objcount; obj++) {
                 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
                         int tmp, bytes;
@@ -440,10 +453,10 @@ static int filter_grant_check(struct obd_export *exp, int objcount,
                         if (rnb[n].flags & OBD_BRW_FROM_GRANT) {
                                 if (fed->fed_grant < used + bytes) {
                                         CDEBUG(D_CACHE,
-                                               "%s: cli %s claims %ld+%d GRANT,"
-                                               " no such grant %lu, idx %d\n",
+                                               "%s: cli %s/%p claims %ld+%d "
+                                               "GRANT, real grant %lu idx %d\n",
                                                exp->exp_obd->obd_name,
-                                               exp->exp_client_uuid.uuid,
+                                               exp->exp_client_uuid.uuid, exp,
                                                used, bytes, fed->fed_grant, n);
                                         mask = D_ERROR;
                                 } else {
@@ -472,9 +485,9 @@ static int filter_grant_check(struct obd_export *exp, int objcount,
                          * ignore this error. */
                         lnb[n].rc = -ENOSPC;
                         rnb[n].flags &= OBD_BRW_GRANTED;
-                        CDEBUG(D_CACHE, "%s: cli %s idx %d no space for %d\n",
+                        CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
                                exp->exp_obd->obd_name,
-                               exp->exp_client_uuid.uuid, n, bytes);
+                               exp->exp_client_uuid.uuid, exp, n, bytes);
                 }
         }
 
@@ -488,8 +501,8 @@ static int filter_grant_check(struct obd_export *exp, int objcount,
         exp->exp_obd->u.filter.fo_tot_pending += used;
 
         CDEBUG(mask,
-               "%s: cli %s used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
-               exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, used,
+               "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
+               exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
                ungranted, fed->fed_grant, fed->fed_dirty);
 
         /* Rough calc in case we don't refresh cached statfs data */
@@ -501,8 +514,8 @@ static int filter_grant_check(struct obd_export *exp, int objcount,
                 exp->exp_obd->obd_osfs.os_bavail = 0;
 
         if (fed->fed_dirty < used) {
-                CERROR("%s: cli %s claims used %lu > fed_dirty %lu\n",
-                       exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
+                CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
+                       exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
                        used, fed->fed_dirty);
                 used = fed->fed_dirty;
         }
@@ -512,25 +525,55 @@ static int filter_grant_check(struct obd_export *exp, int objcount,
         return rc;
 }
 
-static int filter_start_page_write(struct inode *inode,
+static int filter_start_page_write(struct obd_device *obd, struct inode *inode,
                                    struct niobuf_local *lnb)
 {
-        struct page *page = alloc_pages(GFP_HIGHUSER, 0);
+        struct page *page;
+
+        if (lnb->len != PAGE_SIZE)
+                return filter_start_page_read(obd, inode, lnb);
+
+        page = alloc_pages(GFP_HIGHUSER, 0);
         if (page == NULL) {
                 CERROR("no memory for a temp page\n");
                 RETURN(lnb->rc = -ENOMEM);
         }
+#if 0
         POISON_PAGE(page, 0xf1);
         if (lnb->len != PAGE_SIZE) {
                 memset(kmap(page) + lnb->len, 0, PAGE_SIZE - lnb->len);
                 kunmap(page);
         }
+#endif
         page->index = lnb->offset >> PAGE_SHIFT;
         lnb->page = page;
 
         return 0;
 }
 
+static void filter_abort_page_write(struct niobuf_local *lnb)
+{
+        LASSERT(lnb->page != NULL);
+
+        if (lnb->len != PAGE_SIZE)
+                page_cache_release(lnb->page);
+        else
+                __free_pages(lnb->page, 0);
+}
+
+/* a helper for both the 2.4 and 2.6 commitrw paths which are both built
+ * up by our shared filter_preprw_write() */
+void filter_release_write_page(struct filter_obd *filter, struct inode *inode,
+                               struct niobuf_local *lnb, int rc)
+{
+        if (lnb->len != PAGE_SIZE)
+                return filter_release_read_page(filter, inode, lnb->page);
+
+        if (rc == 0)
+                flip_into_page_cache(inode, lnb->page);
+        __free_page(lnb->page);
+}
+
 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
  * on mulitple inodes.  That isn't all, because there still exists the
  * possibility of a truncate starting a new transaction while holding the ext3
@@ -547,7 +590,7 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
                                struct niobuf_local *res,
                                struct obd_trans_info *oti)
 {
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
         struct niobuf_remote *rnb;
         struct niobuf_local *lnb;
         struct fsfilt_objinfo fso;
@@ -561,7 +604,7 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
 
         memset(res, 0, niocount * sizeof(*res));
 
-        push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+        push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
         dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
                                    obj->ioo_id);
         if (IS_ERR(dentry))
@@ -606,19 +649,19 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
              i++, lnb++, rnb++) {
                 /* We still set up for ungranted pages so that granted pages
                  * can be written to disk as they were promised, and portals
-                 * needs to keep the pages all aligned properly. */ 
+                 * needs to keep the pages all aligned properly. */
                 lnb->dentry = dentry;
                 lnb->offset = rnb->offset;
                 lnb->len    = rnb->len;
                 lnb->flags  = rnb->flags;
 
-                rc = filter_start_page_write(dentry->d_inode, lnb);
+                rc = filter_start_page_write(exp->exp_obd, dentry->d_inode,lnb);
                 if (rc) {
-                        CDEBUG(D_ERROR, "page err %u@"LPU64" %u/%u %p: rc %d\n",
+                        CERROR("page err %u@"LPU64" %u/%u %p: rc %d\n",
                                lnb->len, lnb->offset,
                                i, obj->ioo_bufcnt, dentry, rc);
                         while (lnb-- > res)
-                                __free_pages(lnb->page, 0);
+                                filter_abort_page_write(lnb);
                         f_dput(dentry);
                         GOTO(cleanup, rc);
                 }
@@ -626,6 +669,17 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
                         tot_bytes += lnb->len;
         }
 
+        while (lnb-- > res) {
+                if (lnb->len == PAGE_SIZE)
+                        continue;
+                rc = filter_finish_page_read(lnb);
+                if (rc) {
+                        CERROR("error page %u@"LPU64" %u %p: rc %d\n", lnb->len,
+                               lnb->offset, (int)(lnb - res), lnb->dentry, rc);
+                        GOTO(cleanup, rc);
+                }
+        }
+
         if (time_after(jiffies, now + 15 * HZ))
                 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
         else
@@ -644,7 +698,7 @@ cleanup:
                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
         default: ;
         }
-        pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+        pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
         return rc;
 }
 
@@ -665,36 +719,50 @@ int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
         return -EPROTO;
 }
 
+void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
+                              struct page *page)
+{
+        int drop = 0;
+
+        if (inode != NULL &&
+            (inode->i_size > filter->fo_readcache_max_filesize))
+                drop = 1;
+
+        /* drop from cache like truncate_list_pages() */
+        if (drop && !TryLockPage(page)) {
+                if (page->mapping)
+                        ll_truncate_complete_page(page);
+                unlock_page(page);
+        }
+        page_cache_release(page);
+}
+
 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
                                 int objcount, struct obd_ioobj *obj,
                                 int niocount, struct niobuf_local *res,
-                                struct obd_trans_info *oti)
+                                struct obd_trans_info *oti, int rc)
 {
         struct obd_ioobj *o;
         struct niobuf_local *lnb;
-        int i, j, drop = 0;
+        int i, j;
+        struct inode *inode = NULL;
         ENTRY;
 
         if (res->dentry != NULL)
-                drop = (res->dentry->d_inode->i_size >
-                        exp->exp_obd->u.filter.fo_readcache_max_filesize);
+                inode = res->dentry->d_inode;
 
         for (i = 0, o = obj, lnb = res; i < objcount; i++, o++) {
                 for (j = 0 ; j < o->ioo_bufcnt ; j++, lnb++) {
                         if (lnb->page == NULL)
                                 continue;
-                        /* drop from cache like truncate_list_pages() */
-                        if (drop && !TryLockPage(lnb->page)) {
-                                if (lnb->page->mapping)
-                                        truncate_complete_page(lnb->page);
-                                unlock_page(lnb->page);
-                        }
-                        page_cache_release(lnb->page);
+                        filter_release_read_page(&exp->exp_obd->u.filter,
+                                                 inode, lnb->page);
                 }
         }
+
         if (res->dentry != NULL)
                 f_dput(res->dentry);
-        RETURN(0);
+        RETURN(rc);
 }
 
 void flip_into_page_cache(struct inode *inode, struct page *new_page)
@@ -711,11 +779,7 @@ void flip_into_page_cache(struct inode *inode, struct page *new_page)
                  * as well. */
                 old_page = find_lock_page(inode->i_mapping, new_page->index);
                 if (old_page) {
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-                        truncate_complete_page(old_page);
-#else
-                        truncate_complete_page(old_page->mapping, old_page);
-#endif
+                        ll_truncate_complete_page(old_page);
                         unlock_page(old_page);
                         page_cache_release(old_page);
                 }
@@ -770,17 +834,142 @@ void filter_grant_commit(struct obd_export *exp, int niocount,
 
         spin_unlock(&exp->exp_obd->obd_osfs_lock);
 }
+int filter_do_cow(struct obd_export *exp, struct obd_ioobj *obj,
+                  int nioo, struct niobuf_remote *rnb)
+{
+        struct dentry *dentry;
+        struct lvfs_run_ctxt saved;
+        struct write_extents *extents = NULL;
+        int j, rc = 0, numexts = 0, flags = 0;
+
+        ENTRY;
+
+        LASSERT(nioo == 1);
+
+        push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+        
+        dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
+                                   obj->ioo_id);
+        if (IS_ERR(dentry)) {
+                pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+                RETURN (PTR_ERR(dentry));
+        }
+
+        if (dentry->d_inode == NULL) {
+                CERROR("trying to write extents to non-existent file "LPU64"\n",
+                       obj->ioo_id);
+                GOTO(cleanup, rc = -ENOENT);
+        }
+        
+        flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
+        if (!(flags & SM_DO_COW)) {
+                GOTO(cleanup, rc);
+        }
+        OBD_ALLOC(extents, obj->ioo_bufcnt * sizeof(struct write_extents)); 
+        if (!extents) {
+                CERROR("No Memory\n");
+                GOTO(cleanup, rc = -ENOMEM);
+        }
+        for (j = 0; j < obj->ioo_bufcnt; j++) {
+                if (rnb[j].len != 0) {
+                        extents[numexts].w_count = rnb[j].len;
+                        extents[numexts].w_pos = rnb[j].offset;
+                        numexts++;
+                } 
+        } 
+        rc = fsfilt_do_write_cow(exp->exp_obd, dentry, extents, numexts);
+        if (rc) {
+                CERROR("Do cow error id "LPU64" rc:%d \n",
+                        obj->ioo_id, rc);
+                GOTO(cleanup, rc); 
+        }
+        
+cleanup:
+        if (extents) {
+                OBD_FREE(extents, obj->ioo_bufcnt * sizeof(struct write_extents));
+        }
+        f_dput(dentry);
+        pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+        RETURN(rc);
+
+}
+int filter_write_extents(struct obd_export *exp, struct obd_ioobj *obj, int nobj,
+                         int niocount, struct niobuf_local *local, int rc)
+{
+        struct lvfs_run_ctxt saved;
+        struct dentry *dentry;
+        struct niobuf_local *lnb;
+        __u64  offset = 0;
+        __u32  len = 0;
+        int    i, flags; 
+        ENTRY;
+
+        LASSERT(nobj == 1);
+
+        push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+
+        dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
+                                   obj->ioo_id);
+        if (IS_ERR(dentry)) {
+                pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+                RETURN (PTR_ERR(dentry));
+        }
+
+        if (dentry->d_inode == NULL) {
+                CERROR("trying to write extents to non-existent file "LPU64"\n",
+                       obj->ioo_id);
+                GOTO(cleanup, rc = -ENOENT);
+        }
+        
+        flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
+        if (!(flags & SM_DO_REC)) {
+                GOTO(cleanup, rc);
+        }
+
+        for (i = 0, lnb = local; i < obj->ioo_bufcnt; i++, lnb++) {
+                if (len == 0) {
+                        offset = lnb->offset;
+                        len = lnb->len;
+                } else if (lnb->offset == (offset + len)) {
+                        len += lnb->len;
+                } else {
+                        rc = fsfilt_write_extents(exp->exp_obd, dentry, 
+                                                  offset, len);
+                        if (rc) {
+                                CERROR("write exts off "LPU64" num %u rc:%d\n",
+                                        offset, len, rc);
+                                GOTO(cleanup, rc);
+                        }
+                        offset = lnb->offset;
+                        len = lnb->len; 
+                } 
+        }
+        if (len > 0) {
+                rc = fsfilt_write_extents(exp->exp_obd, dentry, 
+                                          offset, len);
+                if (rc) {
+                        CERROR("write exts off "LPU64" num %u rc:%d\n",
+                                offset, len, rc);
+                        GOTO(cleanup, rc);
+                }
+        }
+cleanup:
+        f_dput(dentry);
+        pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
+        RETURN(rc);
+}
 
 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
                     int objcount, struct obd_ioobj *obj, int niocount,
-                    struct niobuf_local *res, struct obd_trans_info *oti)
+                    struct niobuf_local *res, struct obd_trans_info *oti,int rc)
 {
         if (cmd == OBD_BRW_WRITE)
                 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
-                                             res, oti);
+                                             res, oti, rc);
         if (cmd == OBD_BRW_READ)
                 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
-                                            res, oti);
+                                            res, oti, rc);
         LBUG();
         return -EPROTO;
 }
@@ -830,7 +1019,7 @@ int filter_brw(int cmd, struct obd_export *exp, struct obdo *oa,
                 kunmap(pga[i].pg);
         }
 
-        ret = filter_commitrw(cmd, exp, oa, 1, &ioo, oa_bufs, lnb, oti);
+        ret = filter_commitrw(cmd, exp, oa, 1, &ioo, oa_bufs, lnb, oti, ret);
 
 out:
         if (lnb)