Whamcloud - gitweb
Fix eric's extremely well-spotted locking bug. It's not clear that we even
[fs/lustre-release.git] / lustre / llite / rw.c
index 0200ca6..823d18a 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Lustre Lite I/O Page Cache
  *
- * Copyright (C) 2002 Cluster File Systems, Inc. 
+ * Copyright (C) 2002 Cluster File Systems, Inc.
  */
 
 #include <linux/config.h>
 #include <linux/lustre_lite.h>
 #include <linux/lustre_lib.h>
 
+
 /* SYNCHRONOUS I/O to object storage for an inode */
 static int ll_brw(int rw, struct inode *inode, struct page *page, int create)
 {
-        struct ll_inode_info *lii = ll_i2info(inode);
-        struct lov_stripe_md *md = lii->lli_smd;
-        obd_size         count = PAGE_SIZE;
-        obd_off          offset = ((obd_off)page->index) << PAGE_SHIFT;
-        obd_flag         flags = create ? OBD_BRW_CREATE : 0;
-        int              err;
+        struct ll_inode_info *lli = ll_i2info(inode);
+        struct lov_stripe_md *md = lli->lli_smd;
+        struct brw_page pg;
+        int err;
+        struct io_cb_data *cbd = ll_init_cb();
         ENTRY;
 
-        err = obd_brw(rw, ll_i2obdconn(inode), md, 1,
-                      &page, &count, &offset, &flags, NULL, NULL);
+        if (!cbd)
+                RETURN(-ENOMEM);
+
+        pg.pg = page;
+        pg.count = PAGE_SIZE;
+        pg.off = ((obd_off)page->index) << PAGE_SHIFT;
+        pg.flag = create ? OBD_BRW_CREATE : 0;
+
+        err = obd_brw(rw, ll_i2obdconn(inode), md, 1, &pg, ll_sync_io_cb, cbd);
+
         RETURN(err);
 } /* ll_brw */
 
@@ -53,13 +61,14 @@ static int ll_brw(int rw, struct inode *inode, struct page *page, int create)
 static int ll_readpage(struct file *file, struct page *page)
 {
         struct inode *inode = page->mapping->host;
+        obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
         int rc = 0;
         ENTRY;
 
         if (!PageLocked(page))
                 LBUG();
 
-        if (((inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT) <= page->index) {
+        if (inode->i_size <= offset) {
                 memset(kmap(page), 0, PAGE_SIZE);
                 kunmap(page);
                 GOTO(readpage_out, rc);
@@ -85,11 +94,11 @@ static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
                             unsigned to)
 {
         struct inode *inode = page->mapping->host;
-        //obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
+        obd_off offset = ((obd_off)page->index) << PAGE_SHIFT;
         int rc = 0;
         char *addr;
-        ENTRY; 
-        
+        ENTRY;
+
         addr = kmap(page);
         if (!PageLocked(page))
                 LBUG();
@@ -97,16 +106,16 @@ static int ll_prepare_write(struct file *file, struct page *page, unsigned from,
         if (Page_Uptodate(page))
                 GOTO(prepare_done, rc);
 
-        memset(addr, 0, PAGE_SIZE);
-
         /* We're completely overwriting an existing page, so _don't_ set it up
          * to date until commit_write */
         if (from == 0 && to == PAGE_SIZE)
                 RETURN(0);
-        
-        /* prepare write should not read what lies beyond the end of
-           the file */
 
+        /* We are writing to a new page, no need to read old data */
+        if (inode->i_size <= offset) {
+                memset(addr, 0, PAGE_SIZE);
+                goto prepare_done;
+        }
 
         rc = ll_brw(OBD_BRW_READ, inode, page, 0);
 
@@ -135,10 +144,11 @@ static int ll_writepage(struct page *page)
         } else {
                 CERROR("ll_brw failure %d\n", err);
         }
-        UnlockPage(page); 
+        unlock_page(page);
         RETURN(err);
 }
 
+
 /* SYNCHRONOUS I/O to object storage for an inode -- object attr will be updated
  * too */
 static int ll_commit_write(struct file *file, struct page *page,
@@ -146,15 +156,21 @@ static int ll_commit_write(struct file *file, struct page *page,
 {
         int create = 1;
         struct inode *inode = page->mapping->host;
-        struct ll_inode_info *lii = ll_i2info(inode);
-        struct lov_stripe_md *md = lii->lli_smd;
-        obd_size         count = to;
-        obd_off          offset = (((obd_off)page->index) << PAGE_SHIFT);
-        obd_flag         flags = create ? OBD_BRW_CREATE : 0;
-        int              err;
-        struct iattr     iattr;
+        struct ll_inode_info *lli = ll_i2info(inode);
+        struct lov_stripe_md *md = lli->lli_smd;
+        struct brw_page pg;
+        int err;
+        loff_t size;
+        struct io_cb_data *cbd = ll_init_cb();
+
+        pg.pg = page;
+        pg.count = to;
+        pg.off = (((obd_off)page->index) << PAGE_SHIFT);
+        pg.flag = create ? OBD_BRW_CREATE : 0;
 
         ENTRY;
+        if (!cbd)
+                RETURN(-ENOMEM);
 
         SetPageUptodate(page);
 
@@ -162,25 +178,16 @@ static int ll_commit_write(struct file *file, struct page *page,
                 LBUG();
 
         CDEBUG(D_INODE, "commit_page writing (at %d) to %d, count %Ld\n",
-               from, to, (unsigned long long)count);
+               from, to, (unsigned long long)pg.count);
 
         err = obd_brw(OBD_BRW_WRITE, ll_i2obdconn(inode), md,
-                      1, &page, &count, &offset, &flags, NULL, NULL);
+                      1, &pg, ll_sync_io_cb, cbd);
         kunmap(page);
 
-        iattr.ia_size = offset + to;
-        if (iattr.ia_size > inode->i_size) {
-                /* do NOT truncate when writing in the middle of a file */
-                inode->i_size = iattr.ia_size;
-                iattr.ia_valid = ATTR_SIZE;
-#if 0
-                err = ll_inode_setattr(inode, &iattr, 0);
-                if (err) {
-                        CERROR("failed - %d.\n", err);
-                        err = -EIO;
-                }
-#endif
-        }
+        size = pg.off + pg.count;
+        /* do NOT truncate when writing in the middle of a file */
+        if (size > inode->i_size)
+                inode->i_size = size;
 
         RETURN(err);
 } /* ll_commit_write */
@@ -189,19 +196,29 @@ void ll_truncate(struct inode *inode)
 {
         struct obdo oa = {0};
         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
+        struct lustre_handle *lockhs = NULL;
         int err;
         ENTRY;
 
-        if (!md) { 
+        if (!md) {
                 /* object not yet allocated */
                 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
                 return;
         }
 
+        oa.o_id = md->lmd_object_id;
+        oa.o_size = inode->i_size;
+
         CDEBUG(D_INFO, "calling punch for %ld (all bytes after %Ld)\n",
                (long)oa.o_id, (unsigned long long)oa.o_size);
 
-        oa.o_id = md->lmd_object_id;
+        err = ll_size_lock(inode, md, oa.o_size, LCK_PW, &lockhs);
+        if (err) {
+                CERROR("ll_size_lock failed: %d\n", err);
+                /* FIXME: What to do here?  It's too late to back out... */
+                LBUG();
+        }
+
         oa.o_valid = OBD_MD_FLID;
         /* truncate == punch to/from start from/to end:
            set end to -1 for that. */
@@ -210,11 +227,11 @@ void ll_truncate(struct inode *inode)
         if (err)
                 CERROR("obd_truncate fails (%d)\n", err);
         else
-                /* This is done for us at the OST and MDS, but the
-                 * updated timestamps are not sent back to us.
-                 * Needed for POSIX.
-                 */
-                inode->i_mtime = inode->i_ctime = CURRENT_TIME;
+                obdo_to_inode(inode, &oa, oa.o_valid);
+
+        err = ll_size_unlock(inode, md, LCK_PW, lockhs);
+        if (err)
+                CERROR("ll_size_unlock failed: %d\n", err);
 
         EXIT;
         return;
@@ -224,49 +241,47 @@ int ll_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
                  unsigned long blocknr, int blocksize)
 {
         obd_count        bufs_per_obdo = iobuf->nr_pages;
-        struct ll_inode_info *lii = ll_i2info(inode);
-        struct lov_stripe_md *md = lii->lli_smd;
-        obd_size         *count = NULL;
-        obd_off          *offset = NULL;
-        obd_flag         *flags = NULL;
+        struct ll_inode_info *lli = ll_i2info(inode);
+        struct lov_stripe_md *md = lli->lli_smd;
+        struct brw_page *pga;
         int              rc = 0;
         int i;
+        struct io_cb_data *cbd = ll_init_cb();
 
         ENTRY;
+        if (!cbd)
+                RETURN(-ENOMEM);
 
         if (blocksize != PAGE_SIZE) {
                 CERROR("direct_IO blocksize != PAGE_SIZE\n");
                 return -EINVAL;
         }
 
-        OBD_ALLOC(count, sizeof(*count) * bufs_per_obdo);
-        OBD_ALLOC(offset, sizeof(*offset) * bufs_per_obdo);
-        OBD_ALLOC(flags, sizeof(*flags) * bufs_per_obdo);
-        if (!count || !offset || !flags)
+        OBD_ALLOC(pga, sizeof(*pga) * bufs_per_obdo);
+        if (!pga)
                 GOTO(out, rc = -ENOMEM);
 
         /* NB: we can't use iobuf->maplist[i]->index for the offset
          * instead of "blocknr" because ->index contains garbage.
          */
         for (i = 0; i < bufs_per_obdo; i++, blocknr++) {
-                count[i] = PAGE_SIZE;
-                offset[i] = (obd_off)blocknr << PAGE_SHIFT;
-                flags[i] = OBD_BRW_CREATE;
+                pga[i].pg = iobuf->maplist[i];
+                pga[i].count = PAGE_SIZE;
+                pga[i].off = (obd_off)blocknr << PAGE_SHIFT;
+                pga[i].flag = OBD_BRW_CREATE;
         }
 
         if (!md || !md->lmd_object_id)
                 GOTO(out, rc = -ENOMEM);
 
         rc = obd_brw(rw == WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
-                     ll_i2obdconn(inode), md, bufs_per_obdo,
-                     iobuf->maplist, count, offset, flags, NULL, NULL);
+                     ll_i2obdconn(inode), md, bufs_per_obdo, pga,
+                     ll_sync_io_cb, cbd);
         if (rc == 0)
                 rc = bufs_per_obdo * PAGE_SIZE;
 
 out:
-        OBD_FREE(flags, sizeof(obd_flag) * bufs_per_obdo);
-        OBD_FREE(count, sizeof(obd_count) * bufs_per_obdo);
-        OBD_FREE(offset, sizeof(obd_off) * bufs_per_obdo);
+        OBD_FREE(pga, sizeof(*pga) * bufs_per_obdo);
         RETURN(rc);
 }