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 58cdf10..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>
 /* 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;
-        struct brw_page pg; 
-        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;
-        if (!cbd) 
-                RETURN(-ENOMEM); 
+
+        if (!cbd)
+                RETURN(-ENOMEM);
 
         pg.pg = page;
         pg.count = PAGE_SIZE;
@@ -52,6 +53,7 @@ static int ll_brw(int rw, struct inode *inode, struct page *page, int create)
         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 */
 
@@ -59,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);
@@ -91,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();
@@ -103,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);
 
@@ -141,7 +144,7 @@ static int ll_writepage(struct page *page)
         } else {
                 CERROR("ll_brw failure %d\n", err);
         }
-        UnlockPage(page); 
+        unlock_page(page);
         RETURN(err);
 }
 
@@ -153,11 +156,11 @@ 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;
-        struct brw_page pg; 
-        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;
@@ -166,8 +169,8 @@ static int ll_commit_write(struct file *file, struct page *page,
         pg.flag = create ? OBD_BRW_CREATE : 0;
 
         ENTRY;
-        if (!cbd) 
-                RETURN(-ENOMEM); 
+        if (!cbd)
+                RETURN(-ENOMEM);
 
         SetPageUptodate(page);
 
@@ -181,19 +184,10 @@ static int ll_commit_write(struct file *file, struct page *page,
                       1, &pg, ll_sync_io_cb, cbd);
         kunmap(page);
 
-        iattr.ia_size = pg.off + pg.count;
-        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 */
@@ -202,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. */
@@ -223,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;
@@ -237,8 +241,8 @@ 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;
+        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;