Whamcloud - gitweb
r=zab,phil
[fs/lustre-release.git] / lustre / obdfilter / filter_io.c
index 1ce8825..419b2a0 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/module.h>
 #include <linux/pagemap.h> // XXX kill me soon
 #include <linux/version.h>
+#include <asm/div64.h>
 
 #include <linux/obd_class.h>
 #include <linux/lustre_fsfilt.h>
@@ -99,12 +100,292 @@ err_page:
         return lnb->rc;
 }
 
+/* See if there are unallocated parts in given file region */
+static int filter_inode_has_holes(struct inode *inode, obd_size start,
+                                  int len)
+{
+        int j;
+#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
+        sector_t (*fs_bmap)(struct address_space *,
+                            sector_t);
+#else
+        int (*fs_bmap)(struct address_space *, long);
+#endif
+        fs_bmap = inode->i_mapping->a_ops->bmap;
+        if (fs_bmap) {
+                for (j = 0; j <= len ; j++) {
+                        if (!fs_bmap(inode->i_mapping, start+j)) {
+                                return 1;
+                        }
+                }
+                return 0;
+        } else {
+                /* Return -1 in case that caller cares about bmap availability.
+                 */
+                return -1;
+        }
+}
+/* Grab the dirty and seen grant announcements from the incoming obdo.
+ * We will later calculate the clients new grant and return it. */
+static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
+{
+        struct filter_export_data *fed;
+        struct obd_device *obd = exp->exp_obd;
+        obd_size client_cached;
+        ENTRY;
+
+        if (!oa || (oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
+                                  (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
+                if (oa)
+                        oa->o_valid &= ~OBD_MD_FLGRANT;
+                EXIT;
+                return;
+        }
+
+        client_cached = oa->o_blocks;
+        fed = &exp->exp_filter_data;
+
+        if (client_cached > fed->fed_grant)
+                CERROR("client %s claims "LPU64" granted, > "LPU64" granted\n",
+                       obd->obd_name, client_cached, fed->fed_grant);
+
+        spin_lock(&obd->obd_osfs_lock);
+        /* update our accounting now so that statfs takes it into account */
+        obd->u.filter.fo_tot_cached += client_cached - fed->fed_cached;
+        fed->fed_cached = client_cached;
+
+        /* Acknowledgement that the client has seen our published grant.
+         * If the client has met our shrinking target we can reuse its
+         * difference from the previous grant.  It is reasonable to announce
+         * more dirty that cached as it tries to purge its previously granted
+         * dirty data down to its newly received target. */
+        if (fed->fed_grant_waiting && (oa->o_grant <= fed->fed_grant_sent)) {
+                if (fed->fed_grant_sent < fed->fed_grant) {
+                        if (client_cached <= fed->fed_grant_sent) {
+                                obd->u.filter.fo_tot_granted -=
+                                        fed->fed_grant - oa->o_grant;
+                                CDEBUG(D_SUPER, "reduced grant from "LPU64" to "
+                                       LPU64", total grant now "LPU64"\n",
+                                       fed->fed_grant, oa->o_grant,
+                                       obd->u.filter.fo_tot_granted);
+                                fed->fed_grant = oa->o_grant;
+                                fed->fed_grant_waiting = 0;
+                        }
+                } else {
+                        fed->fed_grant_waiting = 0;
+                }
+        }
+        spin_unlock(&obd->obd_osfs_lock);
+        oa->o_valid &= ~(OBD_MD_FLGRANT|OBD_MD_FLBLOCKS);
+        EXIT;
+}
+
+/* Figure out how much space is available between what we've granted
+ * and what remains in the filesystem.  Compensate for ext3 indirect
+ * block overhead when computing how much free space is left ungranted.
+ *
+ * Caller must hold obd_osfs_lock. */
+obd_size filter_grant_space_left(struct obd_export *exp)
+{
+        obd_size left = 0;
+        struct obd_device *obd = exp->exp_obd;
+        int blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
+        /* XXX I disabled statfs caching as it only creates extra problems now.
+          -- green*/
+        unsigned long max_age = jiffies/* - HZ*/+1;
+        struct filter_export_data *fed = &exp->exp_filter_data;
+        int rc;
+
+restat:
+        rc = fsfilt_statfs(obd, obd->u.filter.fo_sb, max_age);
+        if (rc) /* N.B. statfs can't really fail, just for correctness */
+                RETURN(0);
+
+        left = obd->obd_osfs.os_bavail << blockbits;
+        left -= (left >> (blockbits - 2)) + (left >> (2 * blockbits - 2));
+        /* We cannot afford having absolutely no space, we need some for
+           llog stuff */
+        if ( left >= PAGE_SIZE * 10)
+                left -= PAGE_SIZE * 10;
+        else
+                left = 0;
+
+        /* If fed->fed_grant_waiting is set, this means
+           obd->u.filter.fo_tot_granted does not represent actual granted
+           amount and client is supposedly actively shrinks its cache, so
+           no point in printing this warning */
+        if (left < obd->u.filter.fo_tot_granted && !fed->fed_grant_waiting)
+                CERROR("granted space "LPU64" more than available "LPU64"\n",
+                       obd->u.filter.fo_tot_granted, left);
+
+        left -= min(left, obd->u.filter.fo_tot_granted);
+        if (left < FILTER_GRANT_CHUNK && time_after(jiffies,obd->obd_osfs_age)){
+                CDEBUG(D_SUPER, "fs has no space left and statfs too old\n");
+                max_age = jiffies;
+                goto restat;
+        }
+
+        CDEBUG(D_SUPER, "free: "LPU64" avail: "LPU64" grant left: "LPU64"\n",
+               obd->obd_osfs.os_bfree << blockbits,
+               obd->obd_osfs.os_bavail << blockbits, left);
+
+        return left;
+}
+
+/* When clients have dirtied as much space as they've been granted they
+ * fall through to sync writes.  These sync writes haven't been expressed
+ * in grants and need to error with ENOSPC when there isn't room in the
+ * filesystem for them after grants are taken into account.  However,
+ * writeback of the dirty data that was already granted space can write
+ * right on through.  We have no need to stop writes that won't allocate
+ * new space, so we bmap to calculate how much this io is going to consume.
+ *
+ * Caller must hold obd_osfs_lock. */
+static int filter_check_space(struct obd_export *exp, int objcount,
+                              struct fsfilt_objinfo *fso, int niocount,
+                              struct niobuf_remote *rnb,
+                              struct niobuf_local *lnb, obd_size *left,
+                              obd_size *consumed, struct inode *inode)
+{
+        int blocksize = exp->exp_obd->u.filter.fo_sb->s_blocksize;
+        obd_size bytes, ungranted = 0;
+        int i, rc = -ENOSPC, obj, n = 0;
+
+        *consumed = 0;
+
+        for (obj = 0; obj < objcount; obj++) {
+                for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
+                        obd_size tmp;
+
+                        bytes = rnb[n].len;
+                        tmp = rnb[n].offset & (blocksize - 1);
+                        bytes += tmp;
+                        tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
+                        if (tmp)
+                                bytes += blocksize - tmp;
+
+                        if (rnb[n].flags & OBD_BRW_FROM_GRANT) {
+                                *consumed += bytes;
+                                rc = 0;
+                                continue;
+                        }
+                        if (*left - *consumed >= bytes) {
+                                /* if enough space, pretend it was granted */
+                                exp->exp_obd->u.filter.fo_tot_granted += bytes;
+                                exp->exp_filter_data.fed_grant += bytes;
+                                *consumed += bytes;
+                                *left -= bytes;
+                                rc = 0;
+                                continue;
+                        } 
+                        spin_unlock(&exp->exp_obd->obd_osfs_lock);
+                        if (!filter_inode_has_holes(inode,
+                                                   rnb[n].offset >>
+                                                   inode->i_blkbits,
+                                                   rnb[n].len >>
+                                                   inode->i_blkbits)) {
+                                rc = 0;
+                        } else {
+                                rc = lnb[n].rc = -ENOSPC;
+                        }
+                        spin_lock(&exp->exp_obd->obd_osfs_lock);
+                        if (rc)
+                                goto leave;
+                }
+        }
+
+        CDEBUG((*consumed != 0 && ungranted != 0) ? D_ERROR : D_SUPER,
+               "consumed: "LPU64" ungranted: "LPU64"\n", *consumed, ungranted);
+
+        if (*consumed > exp->exp_filter_data.fed_grant)
+                CERROR("request sent from cache, but not enough grant ("LPU64
+                       ","LPU64")\n", *consumed,
+                       exp->exp_filter_data.fed_grant);
+leave:
+        return rc;
+}
+
+/* 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.
+ *
+ * Caller must hold obd_osfs_lock. */
+static void filter_grant(struct obd_export *exp, struct obdo *oa,
+                         obd_size left, obd_size from_grant)
+{
+        struct obd_device *obd = exp->exp_obd;
+        struct filter_export_data *fed = &exp->exp_filter_data;
+        obd_size grant, extra;
+        int blockbits;
+
+        blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
+
+        /* if things go wrong conservatively try to clamp them from
+         * generating more dirty data until things are better on our end */
+        grant = fed->fed_cached;
+
+        extra = min(FILTER_GRANT_CHUNK, left / 2);
+
+        if (grant > fed->fed_grant) {
+                /* If client has screwed up, force basic grant until fixed */
+                CERROR("client %s cached more "LPU64" than granted "LPU64"\n",
+                       exp->exp_client_uuid.uuid, fed->fed_cached,
+                       fed->fed_grant);
+                grant = extra;
+        } else if (fed->fed_grant_waiting) {
+                /* KISS: only one grant change in flight at a time.  We
+                 *       could move it in the "same direction" easily,
+                 *       but changing directions (e.g. grow then shrink
+                 *       before client ACKs) would be bad. */
+                grant = fed->fed_grant_sent;
+        } else {
+                /* grant will shrink or grow as client cache/extra changes */
+                grant = fed->fed_cached + extra;
+        }
+
+        /* If we've granted all we're willing, we have to revoke
+         * the grant covering what the client just wrote. */
+        if (left == 0) {
+                grant -= min(from_grant, grant);
+        }
+
+        if (!fed->fed_grant_waiting && grant + from_grant > left ) {
+                if (from_grant < left)
+                        grant = left - from_grant;
+                else
+                        grant = 0;
+        }
+
+        if (grant != fed->fed_grant) {
+                fed->fed_grant_waiting = 1;
+                fed->fed_grant_sent = grant;
+                if (grant > fed->fed_grant) {
+                        obd->u.filter.fo_tot_granted += grant - fed->fed_grant;
+                        fed->fed_grant = grant;
+                }
+        }
+
+        CDEBUG(D_SUPER,"cli %s cache:"LPU64" grant:"LPU64", granting:"LPU64"\n",
+                        exp->exp_connection->c_remote_uuid.uuid, oa->o_blocks,
+                        oa->o_grant, grant);
+        CDEBUG(D_SUPER, "fed sent:"LPU64" wt:%d grant:"LPU64"\n",
+                        fed->fed_grant_sent, fed->fed_grant_waiting,
+                        fed->fed_grant);
+        CDEBUG(D_SUPER, "tot cached:"LPU64" granted:"LPU64" num_exports: %d\n",
+                        obd->u.filter.fo_tot_cached,
+                        obd->u.filter.fo_tot_granted, obd->obd_num_exports);
+
+        oa->o_valid |= OBD_MD_FLGRANT;
+        oa->o_grant = grant;
+}
+
 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                               int objcount, struct obd_ioobj *obj,
                               int niocount, struct niobuf_remote *nb,
                               struct niobuf_local *res,
                               struct obd_trans_info *oti)
 {
+        struct obd_device *obd = exp->exp_obd;
         struct obd_run_ctxt saved;
         struct obd_ioobj *o;
         struct niobuf_remote *rnb;
@@ -130,7 +411,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
         for (i = 0, o = obj; i < objcount; i++, o++) {
                 LASSERT(o->ioo_bufcnt);
 
-                dentry = filter_oa2dentry(exp->exp_obd, oa);
+                dentry = filter_oa2dentry(obd, oa);
                 if (IS_ERR(dentry))
                         GOTO(cleanup, rc = PTR_ERR(dentry));
 
@@ -151,6 +432,12 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                 CDEBUG(D_INFO, "preprw_read setup: %lu jiffies\n",
                        (jiffies - now));
 
+        if (oa) {
+                spin_lock(&obd->obd_osfs_lock);
+                filter_grant(exp, oa, filter_grant_space_left(exp), 0);
+                spin_unlock(&obd->obd_osfs_lock);
+        }
+
         for (i = 0, o = obj, rnb = nb, lnb = res; i < objcount; i++, o++) {
                 dentry = fso[i].fso_dentry;
                 inode = dentry->d_inode;
@@ -195,8 +482,7 @@ static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
                 CDEBUG(D_INFO, "start_page_read: %lu jiffies\n",
                        (jiffies - now));
 
-        lprocfs_counter_add(exp->exp_obd->obd_stats, LPROC_FILTER_READ_BYTES,
-                            tot_bytes);
+        lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
         while (lnb-- > res) {
                 rc = filter_finish_page_read(lnb);
                 if (rc) {
@@ -264,22 +550,25 @@ 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_device *obd = exp->exp_obd;
         struct obd_run_ctxt saved;
-        struct niobuf_remote *rnb;
-        struct niobuf_local *lnb = NULL;
+        struct niobuf_remote *rnb = nb;
+        struct niobuf_local *lnb = res;
         struct fsfilt_objinfo fso;
         struct dentry *dentry;
         int rc = 0, i, tot_bytes = 0;
+        obd_size consumed = 0, left;
         unsigned long now = jiffies;
         ENTRY;
         LASSERT(objcount == 1);
         LASSERT(obj->ioo_bufcnt > 0);
 
+        filter_grant_incoming(exp, oa);
+
         memset(res, 0, niocount * sizeof(*res));
 
-        push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
-        dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr, 
-                                   obj->ioo_id);
+        push_ctxt(&saved, &obd->obd_ctxt, NULL);
+        dentry = filter_fid2dentry(obd, NULL, obj->ioo_gr, obj->ioo_id);
         if (IS_ERR(dentry))
                 GOTO(cleanup, rc = PTR_ERR(dentry));
 
@@ -299,8 +588,28 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
                 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
                        (jiffies - now));
 
+        spin_lock(&obd->obd_osfs_lock);
+        left = filter_grant_space_left(exp);
+
+        rc = filter_check_space(exp, objcount, &fso, niocount, rnb, lnb,
+                                &left, &consumed, dentry->d_inode);
+        if (oa)
+                filter_grant(exp, oa, left, consumed);
+
+        spin_unlock(&obd->obd_osfs_lock);
+
+        if (rc) {
+                f_dput(dentry);
+                GOTO(cleanup, rc);
+        }
+
         for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
              i++, lnb++, rnb++) {
+
+                /* If there were any granting failures, we should not have
+                   come here */
+                LASSERT (lnb->rc == 0);
+
                 lnb->dentry = dentry;
                 lnb->offset = rnb->offset;
                 lnb->len    = rnb->len;
@@ -326,11 +635,10 @@ static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
                 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
                        (jiffies - now));
 
-        lprocfs_counter_add(exp->exp_obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
-                            tot_bytes);
+        lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_WRITE_BYTES, tot_bytes);
         EXIT;
 cleanup:
-        pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
+        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
         return rc;
 }