Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[fs/lustre-release.git] / lustre / liblustre / rw.c
index c05a7c9..546ab73 100644 (file)
@@ -29,6 +29,7 @@
 #include <time.h>
 #include <sys/types.h>
 #include <sys/queue.h>
+#include <fcntl.h>
 
 #include <sysio.h>
 #include <fs.h>
 #include <inode.h>
 #include <file.h>
 
-#include "llite_lib.h"
-
-#if 0
-void llu_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
-                               struct ldlm_lock *lock)
-{
-        clear_bit(LLI_F_HAVE_SIZE_LOCK, &(llu_i2info(inode)->lli_flags));
-#if 0
-        struct ldlm_extent *extent = &lock->l_extent;
-        unsigned long start, end, count, skip, i, j;
-        struct page *page;
-        int ret;
-        ENTRY;
-
-        CDEBUG(D_INODE, "obdo %lu inode %p ["LPU64"->"LPU64"] size: %llu\n",
-               inode->i_ino, inode, extent->start, extent->end, inode->i_size);
-
-        start = extent->start >> PAGE_CACHE_SHIFT;
-        count = ~0;
-        skip = 0;
-        end = (extent->end >> PAGE_CACHE_SHIFT) + 1;
-        if ((end << PAGE_CACHE_SHIFT) < extent->end)
-                end = ~0;
-        if (lsm->lsm_stripe_count > 1) {
-                struct {
-                        char name[16];
-                        struct ldlm_lock *lock;
-                        struct lov_stripe_md *lsm;
-                } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
-                __u32 stripe;
-                __u32 vallen = sizeof(stripe);
-                int rc;
-
-                /* get our offset in the lov */
-                rc = obd_get_info(ll_i2obdconn(inode), sizeof(key),
-                                  &key, &vallen, &stripe);
-                if (rc != 0) {
-                        CERROR("obd_get_info: rc = %d\n", rc);
-                        LBUG();
-                }
-                LASSERT(stripe < lsm->lsm_stripe_count);
-
-                count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
-                skip = (lsm->lsm_stripe_count - 1) * count;
-                start += (start/count * skip) + (stripe * count);
-                if (end != ~0)
-                        end += (end/count * skip) + (stripe * count);
-        }
+#undef LIST_HEAD
 
-        i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
-        if (end >= i)
-                clear_bit(LLI_F_HAVE_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
-        if (i < end)
-                end = i;
-
-        CDEBUG(D_INODE, "start: %lu j: %lu count: %lu skip: %lu end: %lu\n",
-               start, start % count, count, skip, end);
-
-        /* start writeback on dirty pages in the extent when its PW */
-        for (i = start, j = start % count;
-                        lock->l_granted_mode == LCK_PW && i < end; j++, i++) {
-                if (j == count) {
-                        i += skip;
-                        j = 0;
-                }
-                /* its unlikely, but give us a chance to bail when we're out */
-                PGCACHE_WRLOCK(inode->i_mapping);
-                if (list_empty(&inode->i_mapping->dirty_pages)) {
-                        CDEBUG(D_INODE, "dirty list empty\n");
-                        PGCACHE_WRUNLOCK(inode->i_mapping);
-                        break;
-                }
-                PGCACHE_WRUNLOCK(inode->i_mapping);
-
-                if (need_resched())
-                        schedule();
-
-        /* always do a getattr for the first person to pop out of lock
-         * acquisition.. the DID_GETATTR flag and semaphore serialize
-         * this initial race.  we used to make a decision based on whether
-         * the lock was matched or acquired, but the matcher could win the
-         * waking race with the first issuer so that was no good..
-         */
-        if (test_bit(LLI_F_DID_GETATTR, &lli->lli_flags))
-                RETURN(ELDLM_OK);
-
-        down(&lli->lli_getattr_sem);
-
-        if (!test_bit(LLI_F_DID_GETATTR, &lli->lli_flags)) {
-                rc = ll_inode_getattr(inode, lsm);
-                if (rc == 0) {
-                        set_bit(LLI_F_DID_GETATTR, &lli->lli_flags);
-                } else {
-                        unlock_page(page);
-                }
-                page_cache_release(page);
-
-        }
-
-        /* our locks are page granular thanks to osc_enqueue, we invalidate the
-         * whole page. */
-        LASSERT((extent->start & ~PAGE_CACHE_MASK) == 0);
-        LASSERT(((extent->end+1) & ~PAGE_CACHE_MASK) == 0);
-        for (i = start, j = start % count ; i < end ; j++, i++) {
-                if ( j == count ) {
-                        i += skip;
-                        j = 0;
-                }
-                PGCACHE_WRLOCK(inode->i_mapping);
-                if (list_empty(&inode->i_mapping->dirty_pages) &&
-                     list_empty(&inode->i_mapping->clean_pages) &&
-                     list_empty(&inode->i_mapping->locked_pages)) {
-                        CDEBUG(D_INODE, "nothing left\n");
-                        PGCACHE_WRUNLOCK(inode->i_mapping);
-                        break;
-                }
-                PGCACHE_WRUNLOCK(inode->i_mapping);
-                if (need_resched())
-                        schedule();
-                page = find_get_page(inode->i_mapping, i);
-                if (page == NULL)
-                        continue;
-                CDEBUG(D_INODE, "dropping page %p at %lu\n", page, page->index);
-                lock_page(page);
-                if (page->mapping) /* might have raced */
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-                        truncate_complete_page(page);
-#else
-                        truncate_complete_page(page->mapping, page);
-#endif                
-                unlock_page(page);
-                page_cache_release(page);
-        }
-        EXIT;
-#endif
-}
+#include "llite_lib.h"
 
-int llu_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
-                      void *data, int flag)
+static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
 {
-        struct inode *inode = data;
         struct llu_inode_info *lli = llu_i2info(inode);
-        struct lustre_handle lockh = {0};
+        struct lov_stripe_md *lsm = lli->lli_smd;
+        struct obd_export *exp = llu_i2obdexp(inode);
+        struct {
+                char name[16];
+                struct ldlm_lock *lock;
+                struct lov_stripe_md *lsm;
+        } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
+        __u32 stripe, vallen = sizeof(stripe);
         int rc;
         ENTRY;
 
-        if (inode == NULL)
-                LBUG();
+        if (lsm->lsm_stripe_count == 1)
+                RETURN(0);
 
-        switch (flag) {
-        case LDLM_CB_BLOCKING:
-                ldlm_lock2handle(lock, &lockh);
-                rc = ldlm_cli_cancel(&lockh);
-                if (rc != ELDLM_OK)
-                        CERROR("ldlm_cli_cancel failed: %d\n", rc);
-                break;
-        case LDLM_CB_CANCELING: {
-                /* FIXME: we could be given 'canceling intents' so that we
-                 * could know to write-back or simply throw away the pages
-                 * based on if the cancel comes from a desire to, say,
-                 * read or truncate.. */
-                llu_pgcache_remove_extent(inode, lli->lli_smd, lock);
-                break;
-        }
-        default:
+        /* get our offset in the lov */
+        rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
+        if (rc != 0) {
+                CERROR("obd_get_info: rc = %d\n", rc);
                 LBUG();
         }
-
-        RETURN(0);
+        LASSERT(stripe < lsm->lsm_stripe_count);
+        RETURN(stripe);
 }
-#endif
 
 static int llu_extent_lock_callback(struct ldlm_lock *lock,
                                     struct ldlm_lock_desc *new, void *data,
@@ -230,25 +90,34 @@ static int llu_extent_lock_callback(struct ldlm_lock *lock,
                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
                 break;
         case LDLM_CB_CANCELING: {
-                struct inode *inode = llu_inode_from_lock(lock);
+                struct inode *inode;
                 struct llu_inode_info *lli;
+                struct lov_stripe_md *lsm;
+                __u32 stripe;
+                __u64 kms;
                 
+                /* This lock wasn't granted, don't try to evict pages */
+                if (lock->l_req_mode != lock->l_granted_mode)
+                        RETURN(0);
+
+                inode = llu_inode_from_lock(lock);
                 if (!inode)
                         RETURN(0);
                 lli= llu_i2info(inode);
-                if (!lli) {
-                        I_RELE(inode);
-                        RETURN(0);
-                }
-                if (!lli->lli_smd) {
-                        I_RELE(inode);
-                        RETURN(0);
-                }
-
-/*
-                ll_pgcache_remove_extent(inode, lli->lli_smd, lock);
-                iput(inode);
-*/
+                if (!lli)
+                        goto iput;
+                if (!lli->lli_smd)
+                        goto iput;
+                lsm = lli->lli_smd;
+
+                stripe = llu_lock_to_stripe_offset(inode, lock);
+                kms = ldlm_extent_shift_kms(lock,
+                                            lsm->lsm_oinfo[stripe].loi_kms);
+                if (lsm->lsm_oinfo[stripe].loi_kms != kms)
+                        LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
+                                   lsm->lsm_oinfo[stripe].loi_kms, kms);
+                lsm->lsm_oinfo[stripe].loi_kms = kms;
+iput:
                 I_RELE(inode);
                 break;
         }
@@ -259,6 +128,120 @@ static int llu_extent_lock_callback(struct ldlm_lock *lock,
         RETURN(0);
 }
 
+static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
+{
+        struct ptlrpc_request *req = reqp;
+        struct inode *inode = llu_inode_from_lock(lock);
+        struct llu_inode_info *lli;
+        struct ost_lvb *lvb;
+        int rc, size = sizeof(*lvb), stripe = 0;
+        ENTRY;
+
+        if (inode == NULL)
+                GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
+        lli = llu_i2info(inode);
+        if (lli == NULL)
+                GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
+        if (lli->lli_smd == NULL)
+                GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
+
+        /* First, find out which stripe index this lock corresponds to. */
+        if (lli->lli_smd->lsm_stripe_count > 1)
+                stripe = llu_lock_to_stripe_offset(inode, lock);
+
+        rc = lustre_pack_reply(req, 1, &size, NULL);
+        if (rc) {
+                CERROR("lustre_pack_reply: %d\n", rc);
+                GOTO(iput, rc);
+        }
+
+        lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
+        lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
+
+        LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
+                   lli->lli_st_size, stripe, lvb->lvb_size);
+ iput:
+        I_RELE(inode);
+ out:
+        /* These errors are normal races, so we don't want to fill the console
+         * with messages by calling ptlrpc_error() */
+        if (rc == -ELDLM_NO_LOCK_DATA)
+                lustre_pack_reply(req, 0, NULL, NULL);
+
+        req->rq_status = rc;
+        return rc;
+}
+
+__u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
+__u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
+
+/* NB: lov_merge_size will prefer locally cached writes if they extend the
+ * file (because it prefers KMS over RSS when larger) */
+int llu_glimpse_size(struct inode *inode, struct ost_lvb *lvb)
+{
+        struct llu_inode_info *lli = llu_i2info(inode);
+        struct llu_sb_info *sbi = llu_i2sbi(inode);
+        ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
+        struct lustre_handle lockh;
+        int rc, flags = LDLM_FL_HAS_INTENT;
+        ENTRY;
+
+        CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", lli->lli_st_ino);
+
+        rc = obd_enqueue(sbi->ll_osc_exp, lli->lli_smd, LDLM_EXTENT, &policy,
+                         LCK_PR, &flags, llu_extent_lock_callback,
+                         ldlm_completion_ast, llu_glimpse_callback, inode,
+                         sizeof(*lvb), lustre_swab_ost_lvb, &lockh);
+        if (rc > 0)
+                RETURN(-EIO);
+
+        lvb->lvb_size = lov_merge_size(lli->lli_smd, 0);
+        //inode->i_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
+
+        CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64"\n", lvb->lvb_size);
+
+        obd_cancel(sbi->ll_osc_exp, lli->lli_smd, LCK_PR, &lockh);
+
+        RETURN(rc);
+}
+
+int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
+                    struct lov_stripe_md *lsm, int mode,
+                    ldlm_policy_data_t *policy, struct lustre_handle *lockh,
+                    int ast_flags)
+{
+        struct llu_sb_info *sbi = llu_i2sbi(inode);
+        struct llu_inode_info *lli = llu_i2info(inode);
+        int rc;
+        ENTRY;
+
+        LASSERT(lockh->cookie == 0);
+
+        /* XXX phil: can we do this?  won't it screw the file size up? */
+        if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
+            (sbi->ll_flags & LL_SBI_NOLCK))
+                RETURN(0);
+
+        CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
+               lli->lli_st_ino, policy->l_extent.start, policy->l_extent.end);
+
+        rc = obd_enqueue(sbi->ll_osc_exp, lsm, LDLM_EXTENT, policy, mode,
+                         &ast_flags, llu_extent_lock_callback,
+                         ldlm_completion_ast, llu_glimpse_callback, inode,
+                         sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
+        if (rc > 0)
+                rc = -EIO;
+
+        if (policy->l_extent.start == 0 &&
+            policy->l_extent.end == OBD_OBJECT_EOF)
+                lli->lli_st_size = lov_merge_size(lsm, 1);
+
+        //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
+
+        RETURN(rc);
+}
+
+#if 0
 int llu_extent_lock_no_validate(struct ll_file_data *fd,
                                 struct inode *inode,
                                 struct lov_stripe_md *lsm,
@@ -274,12 +257,10 @@ int llu_extent_lock_no_validate(struct ll_file_data *fd,
 
         LASSERT(lockh->cookie == 0);
 
-#if 0
         /* XXX phil: can we do this?  won't it screw the file size up? */
         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
             (sbi->ll_flags & LL_SBI_NOLCK))
                 RETURN(0);
-#endif
 
         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
                lli->lli_st_ino, extent->start, extent->end);
@@ -297,16 +278,19 @@ int llu_extent_lock_no_validate(struct ll_file_data *fd,
  */
 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
                     struct lov_stripe_md *lsm, int mode,
-                    struct ldlm_extent *extent, struct lustre_handle *lockh)
+                    struct ldlm_extent *extent, struct lustre_handle *lockh,
+                    int nonblock)
 {
         struct llu_inode_info *lli = llu_i2info(inode);
         struct obd_export *exp = llu_i2obdexp(inode);
         struct ldlm_extent size_lock;
         struct lustre_handle match_lockh = {0};
         int flags, rc, matched;
+        int astflags = nonblock ? LDLM_FL_BLOCK_NOWAIT : 0;
         ENTRY;
 
-        rc = llu_extent_lock_no_validate(fd, inode, lsm, mode, extent, lockh, 0);
+        rc = llu_extent_lock_no_validate(fd, inode, lsm, mode, extent,
+                                         lockh, astflags);
         if (rc != ELDLM_OK)
                 RETURN(rc);
 
@@ -323,20 +307,22 @@ int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
         size_lock.end = OBD_OBJECT_EOF;
 
         /* XXX I bet we should be checking the lock ignore flags.. */
+        /* FIXME use LDLM_FL_TEST_LOCK instead */
         flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
         matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
                             sizeof(size_lock), LCK_PR, &flags, inode,
                             &match_lockh);
 
-        /* hey, alright, we hold a size lock that covers the size we 
+        /* hey, alright, we hold a size lock that covers the size we
          * just found, its not going to change for a while.. */
         if (matched == 1) {
                 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
                 obd_cancel(exp, lsm, LCK_PR, &match_lockh);
-        } 
+        }
 
         RETURN(0);
 }
+#endif
 
 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
                 struct lov_stripe_md *lsm, int mode,
@@ -345,12 +331,12 @@ int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
         struct llu_sb_info *sbi = llu_i2sbi(inode);
         int rc;
         ENTRY;
-#if 0
+
         /* XXX phil: can we do this?  won't it screw the file size up? */
         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
             (sbi->ll_flags & LL_SBI_NOLCK))
                 RETURN(0);
-#endif
+
         rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
 
         RETURN(rc);
@@ -432,21 +418,28 @@ static struct obd_async_page_ops llu_async_page_ops = {
 };
 
 static
-struct llu_sysio_cookie* get_sysio_cookie(struct inode *inode, int npages)
+struct llu_sysio_cookie* get_sysio_cookie(struct inode *inode, int maxpages)
 {
         struct llu_sysio_cookie *cookie;
+        int rc;
+
+        OBD_ALLOC(cookie, LLU_SYSIO_COOKIE_SIZE(maxpages));
+        if (cookie == NULL)
+                goto out;
 
-        OBD_ALLOC(cookie, LLU_SYSIO_COOKIE_SIZE(npages));
-        if (cookie) {
-                I_REF(inode);
-                cookie->lsc_inode = inode;
-                cookie->lsc_npages = npages;
-                cookie->lsc_llap = (struct ll_async_page *)(cookie + 1);
-                cookie->lsc_pages = (struct page *) (cookie->lsc_llap + npages);
+        I_REF(inode);
+        cookie->lsc_inode = inode;
+        cookie->lsc_maxpages = maxpages;
+        cookie->lsc_llap = (struct ll_async_page *)(cookie + 1);
+        cookie->lsc_pages = (struct page *) (cookie->lsc_llap + maxpages);
 
-                osic_init(&cookie->lsc_osic);
+        rc = oig_init(&cookie->lsc_oig);
+        if (rc) {
+                OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(maxpages));
+                cookie = NULL;
         }
 
+out:
         return cookie;
 }
 
@@ -456,25 +449,114 @@ void put_sysio_cookie(struct llu_sysio_cookie *cookie)
         struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
         struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
         struct ll_async_page *llap = cookie->lsc_llap;
+#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
+        struct page *pages = cookie->lsc_pages;
+#endif
         int i;
 
-        for (i = 0; i< cookie->lsc_npages; i++) {
+        for (i = 0; i< cookie->lsc_maxpages; i++) {
                 if (llap[i].llap_cookie)
                         obd_teardown_async_page(exp, lsm, NULL,
                                                 llap[i].llap_cookie);
+#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
+                if (pages[i]._managed) {
+                        free(pages[i].addr);
+                        pages[i]._managed = 0;
+                }
+#endif
         }
 
         I_RELE(cookie->lsc_inode);
 
-        osic_release(cookie->lsc_osic);
-        OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(cookie->lsc_npages));
+        oig_release(cookie->lsc_oig);
+        OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(cookie->lsc_maxpages));
+}
+
+#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
+/* Note: these code should be removed finally, don't need
+ * more cleanup
+ */
+static
+int prepare_unaligned_write(struct llu_sysio_cookie *cookie)
+{
+        struct inode *inode = cookie->lsc_inode;
+        struct llu_inode_info *lli = llu_i2info(inode);
+        struct lov_stripe_md *lsm = lli->lli_smd;
+        struct obdo oa;
+        struct page *pages = cookie->lsc_pages;
+        int i, pgidx[2] = {0, cookie->lsc_npages-1};
+        int rc;
+        ENTRY;
+
+        for (i = 0; i < 2; i++) {
+                struct page *oldpage = &pages[pgidx[i]];
+                struct page newpage;
+                struct brw_page pg;
+                char *newbuf;
+
+                if (i == 0 && pgidx[0] == pgidx[1])
+                        continue;
+
+                LASSERT(oldpage->_offset + oldpage->_count <= PAGE_CACHE_SIZE);
+
+                if (oldpage->_count == PAGE_CACHE_SIZE)
+                        continue;
+
+                if (oldpage->index << PAGE_CACHE_SHIFT >=
+                    lli->lli_st_size)
+                        continue;
+
+                newbuf = malloc(PAGE_CACHE_SIZE);
+                if (!newbuf)
+                        return -ENOMEM;
+
+                newpage.index = oldpage->index;
+                newpage.addr = newbuf;
+
+                pg.pg = &newpage;
+                pg.off = ((obd_off)newpage.index << PAGE_CACHE_SHIFT);
+                if (pg.off + PAGE_CACHE_SIZE > lli->lli_st_size)
+                        pg.count = lli->lli_st_size % PAGE_CACHE_SIZE;
+                else
+                        pg.count = PAGE_CACHE_SIZE;
+                pg.flag = 0;
+
+                oa.o_id = lsm->lsm_object_id;
+                oa.o_mode = lli->lli_st_mode;
+                oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
+
+                /* issue read */
+                rc = obd_brw(OBD_BRW_READ, llu_i2obdexp(inode), &oa, lsm, 1, &pg, NULL);
+                if (rc) {
+                        free(newbuf);
+                        RETURN(rc);
+                }
+
+                /* copy page content, and reset page params */
+                memcpy(newbuf + oldpage->_offset,
+                       (char*)oldpage->addr + oldpage->_offset,
+                       oldpage->_count);
+
+                oldpage->addr = newbuf;
+                if ((((obd_off)oldpage->index << PAGE_CACHE_SHIFT) +
+                    oldpage->_offset + oldpage->_count) > lli->lli_st_size)
+                        oldpage->_count += oldpage->_offset;
+                else
+                        oldpage->_count = PAGE_CACHE_SIZE;
+                oldpage->_offset = 0;
+                oldpage->_managed = 1;
+        }
+
+        RETURN(0);
 }
+#endif
 
 static
 int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
                       char *buf, loff_t pos, size_t count)
 {
-        struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
+        struct llu_inode_info *lli = llu_i2info(cookie->lsc_inode);
+        struct lov_stripe_md *lsm = lli->lli_smd;
         struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
         struct page *pages = cookie->lsc_pages;
         struct ll_async_page *llap = cookie->lsc_llap;
@@ -484,8 +566,6 @@ int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
         if (!exp)
                 RETURN(-EINVAL);
 
-        cookie->lsc_rwcount = count;
-
         /* prepare the pages array */
        do {
                 unsigned long index, offset, bytes;
@@ -496,6 +576,14 @@ int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
                 if (bytes > count)
                         bytes = count;
 
+                /* prevent read beyond file range */
+                if ((cmd == OBD_BRW_READ) &&
+                    (pos + bytes) >= lli->lli_st_size) {
+                        if (pos >= lli->lli_st_size)
+                                break;
+                        bytes = lli->lli_st_size - pos;
+                }
+
                 /* prepare page for this index */
                 pages[npages].index = index;
                 pages[npages].addr = buf - offset;
@@ -507,8 +595,20 @@ int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
                 count -= bytes;
                 pos += bytes;
                 buf += bytes;
+
+                cookie->lsc_rwcount += bytes;
         } while (count);
 
+        cookie->lsc_npages = npages;
+
+#ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
+        if (cmd == OBD_BRW_WRITE) {
+                rc = prepare_unaligned_write(cookie);
+                if (rc)
+                        RETURN(rc);
+        }
+#endif
+
         for (i = 0; i < npages; i++) {
                 llap[i].llap_magic = LLAP_MAGIC;
                 rc = obd_prep_async_page(exp, lsm, NULL, &pages[i],
@@ -526,9 +626,11 @@ int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
                 llap[i].llap_page = &pages[i];
                 llap[i].llap_inode = cookie->lsc_inode;
 
-                rc = obd_queue_sync_io(exp, lsm, NULL, cookie->lsc_osic,
-                                       llap[i].llap_cookie, cmd,
-                                       pages[i]._offset, pages[i]._count, 0);
+                rc = obd_queue_group_io(exp, lsm, NULL, cookie->lsc_oig,
+                                        llap[i].llap_cookie, cmd,
+                                        pages[i]._offset, pages[i]._count, 0,
+                                        ASYNC_READY | ASYNC_URGENT |
+                                        ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
                 if (rc)
                         RETURN(rc);
 
@@ -544,7 +646,7 @@ int llu_start_async_io(struct llu_sysio_cookie *cookie)
         struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
         struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
 
-        return obd_trigger_sync_io(exp, lsm, NULL, cookie->lsc_osic);
+        return obd_trigger_group_io(exp, lsm, NULL, cookie->lsc_oig);
 }
 
 /*
@@ -572,7 +674,7 @@ llu_rw(int cmd, struct inode *inode, char *buf, size_t count, loff_t pos)
                 GOTO(out_cleanup, rc);
 
 /*
-        rc = osic_wait(&osic);
+        rc = oig_wait(&oig);
         if (rc) {
                 CERROR("file i/o error!\n");
                 rw_count = rc;
@@ -585,6 +687,9 @@ out_cleanup:
         RETURN(ERR_PTR(rc));
 }
 
+void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
+                      obd_off size);
+
 struct llu_sysio_callback_args*
 llu_file_write(struct inode *inode, const struct iovec *iovec,
                size_t iovlen, loff_t pos)
@@ -593,9 +698,12 @@ llu_file_write(struct inode *inode, const struct iovec *iovec,
         struct ll_file_data *fd = lli->lli_file_data;
         struct lustre_handle lockh = {0};
         struct lov_stripe_md *lsm = lli->lli_smd;
+        struct obd_export *exp = NULL;
+        ldlm_policy_data_t policy;
         struct llu_sysio_callback_args *lsca;
         struct llu_sysio_cookie *cookie;
-        struct ldlm_extent extent;
+        int astflag = (lli->lli_open_flags & O_NONBLOCK) ?
+                       LDLM_FL_BLOCK_NOWAIT : 0;
         ldlm_error_t err;
         int iovidx;
         ENTRY;
@@ -606,34 +714,31 @@ llu_file_write(struct inode *inode, const struct iovec *iovec,
 
         LASSERT(iovlen <= MAX_IOVEC);
 
+        exp = llu_i2obdexp(inode);
+        if (exp == NULL)
+                RETURN(ERR_PTR(-EINVAL));
+
         OBD_ALLOC(lsca, sizeof(*lsca));
         if (!lsca)
                 RETURN(ERR_PTR(-ENOMEM));
 
         /* FIXME optimize the following extent locking */
         for (iovidx = 0; iovidx < iovlen; iovidx++) {
-                char *buf = iovec[iovidx].iov_base;
+                char *buf = (char*)iovec[iovidx].iov_base;
                 size_t count = iovec[iovidx].iov_len;
 
                 if (count == 0)
                         continue;
 
-                /* FIXME libsysio haven't consider the open flags
-                 * such as O_APPEND */
-#if 0
-                if (!S_ISBLK(lli->lli_st_mode) && file->f_flags & O_APPEND) {
-                        extent.start = 0;
-                        extent.end = OBD_OBJECT_EOF;
-                } else  {
-                        extent.start = *ppos;
-                        extent.end = *ppos + count - 1;
-                }
-#else
-                extent.start = pos;
-                extent.end = pos + count - 1;
-#endif
+                if (pos + count > lli->lli_maxbytes)
+                        GOTO(err_out, err = -ERANGE);
+
+                /* FIXME libsysio haven't handle O_APPEND?? */
+                policy.l_extent.start = pos;
+                policy.l_extent.end = pos + count - 1;
 
-                err = llu_extent_lock(fd, inode, lsm, LCK_PW, &extent, &lockh);
+                err = llu_extent_lock(fd, inode, lsm, LCK_PW, &policy,
+                                      &lockh, astflag);
                 if (err != ELDLM_OK)
                         GOTO(err_out, err = -ENOLCK);
 
@@ -645,12 +750,10 @@ llu_file_write(struct inode *inode, const struct iovec *iovec,
                         /* save cookie */
                         lsca->cookies[lsca->ncookies++] = cookie;
                         pos += count;
-                        /* file size grow. XXX should be done here? */
-                        if (pos > lli->lli_st_size) {
+                        lov_increase_kms(exp, lsm, pos);
+                        /* file size grow */
+                        if (pos > lli->lli_st_size)
                                 lli->lli_st_size = pos;
-                                set_bit(LLI_F_PREFER_EXTENDED_SIZE,
-                                        &lli->lli_flags);
-                        }
                 } else {
                         llu_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
                         GOTO(err_out, err = PTR_ERR(cookie));
@@ -700,15 +803,18 @@ static void llu_update_atime(struct inode *inode)
 
 struct llu_sysio_callback_args*
 llu_file_read(struct inode *inode, const struct iovec *iovec,
-                       size_t iovlen, loff_t pos)
+              size_t iovlen, loff_t pos)
 {
         struct llu_inode_info *lli = llu_i2info(inode);
         struct ll_file_data *fd = lli->lli_file_data;
         struct lov_stripe_md *lsm = lli->lli_smd;
         struct lustre_handle lockh = { 0 };
-        struct ldlm_extent extent;
+        ldlm_policy_data_t policy;
         struct llu_sysio_callback_args *lsca;
         struct llu_sysio_cookie *cookie;
+        int astflag = (lli->lli_open_flags & O_NONBLOCK) ?
+                       LDLM_FL_BLOCK_NOWAIT : 0;
+        __u64 kms;
         int iovidx;
 
         ldlm_error_t err;
@@ -727,15 +833,37 @@ llu_file_read(struct inode *inode, const struct iovec *iovec,
                 if (count == 0)
                         continue;
 
-                extent.start = pos;
-                extent.end = pos + count - 1;
+                policy.l_extent.start = pos;
+                policy.l_extent.end = pos + count - 1;
 
-                err = llu_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
+                err = llu_extent_lock(fd, inode, lsm, LCK_PR, &policy,
+                                      &lockh, astflag);
                 if (err != ELDLM_OK)
                         GOTO(err_out, err = -ENOLCK);
 
-                CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
-                       lli->lli_st_ino, count, pos);
+                kms = lov_merge_size(lsm, 1);
+                if (policy.l_extent.end > kms) {
+                        /* A glimpse is necessary to determine whether we
+                         * return a short read or some zeroes at the end of
+                         * the buffer */
+                        struct ost_lvb lvb;
+                        if (llu_glimpse_size(inode, &lvb)) {
+                                llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
+                                GOTO(err_out, err = -ENOLCK);
+                        }
+                        lli->lli_st_size = lvb.lvb_size;
+                } else {
+                        lli->lli_st_size = kms;
+                }
+
+                CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld, "
+                       "i_size "LPU64"\n", lli->lli_st_ino, count, pos,
+                       lli->lli_st_size);
+
+                if (pos >= lli->lli_st_size) {
+                        llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
+                        break;
+                }
 
                 cookie = llu_rw(OBD_BRW_READ, inode, buf, count, pos);
                 if (!IS_ERR(cookie)) {
@@ -776,15 +904,17 @@ int llu_iop_iodone(struct ioctx *ioctxp)
         ENTRY;
 
         /* write/read(fd, buf, 0) */
-        if (!lsca)
-                return 1;
+        if (!lsca) {
+                ioctxp->ioctx_cc = 0;
+                RETURN(1);
+        }
 
         LASSERT(!IS_ERR(lsca));
 
         for (i = 0; i < lsca->ncookies; i++) {
                 cookie = lsca->cookies[i];
                 if (cookie) {
-                        err = osic_wait(cookie->lsc_osic);
+                        err = oig_wait(cookie->lsc_oig);
                         if (err && !rc)
                                 rc = err;
                         if (!rc)
@@ -793,8 +923,11 @@ int llu_iop_iodone(struct ioctx *ioctxp)
                 }
         }
 
-        if (rc)
-                ioctxp->ioctx_cc = rc;
+        if (rc) {
+                LASSERT(rc < 0);
+                ioctxp->ioctx_cc = -1;
+                ioctxp->ioctx_errno = -rc;
+        }
 
         OBD_FREE(lsca, sizeof(*lsca));
         ioctxp->ioctx_private = NULL;