From: Vladimir Saveliev <[EMAIL PROTECTED]>

[EMAIL PROTECTED]: can't quite get rid of prepare_write/commit_write callbacks
 yet! (xattrs and ioctl uses them), but this is a good start]

Signed-off-by: Vladimir Saveliev <[EMAIL PROTECTED]>
Signed-off-by: Nick Piggin <[EMAIL PROTECTED]>

---
Index: linux-2.6/fs/reiserfs/inode.c
===================================================================
--- linux-2.6.orig/fs/reiserfs/inode.c
+++ linux-2.6/fs/reiserfs/inode.c
@@ -16,6 +16,7 @@
 #include <linux/mpage.h>
 #include <linux/writeback.h>
 #include <linux/quotaops.h>
+#include <linux/swap.h>
 
 static int reiserfs_commit_write(struct file *f, struct page *page,
                                 unsigned from, unsigned to);
@@ -2549,6 +2550,69 @@ static int reiserfs_writepage(struct pag
        return reiserfs_write_full_page(page, wbc);
 }
 
+static int reiserfs_write_begin(struct file *file,
+                               struct address_space *mapping,
+                               loff_t pos, unsigned len, unsigned flags,
+                               struct page **pagep, void **fsdata)
+{
+       struct inode *inode;
+       struct page *page;
+       pgoff_t index;
+       int ret;
+       int old_ref = 0;
+
+       index = pos >> PAGE_CACHE_SHIFT;
+       page = __grab_cache_page(mapping, index);
+       if (!page)
+               return -ENOMEM;
+       *pagep = page;
+
+       inode = mapping->host;
+       reiserfs_wait_on_write_block(inode->i_sb);
+       fix_tail_page_for_writing(page);
+       if (reiserfs_transaction_running(inode->i_sb)) {
+               struct reiserfs_transaction_handle *th;
+               th = (struct reiserfs_transaction_handle *)current->
+                   journal_info;
+               BUG_ON(!th->t_refcount);
+               BUG_ON(!th->t_trans_id);
+               old_ref = th->t_refcount;
+               th->t_refcount++;
+       }
+       ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
+                               reiserfs_get_block);
+       if (ret && reiserfs_transaction_running(inode->i_sb)) {
+               struct reiserfs_transaction_handle *th = current->journal_info;
+               /* this gets a little ugly.  If reiserfs_get_block returned an
+                * error and left a transacstion running, we've got to close it,
+                * and we've got to free handle if it was a persistent 
transaction.
+                *
+                * But, if we had nested into an existing transaction, we need
+                * to just drop the ref count on the handle.
+                *
+                * If old_ref == 0, the transaction is from reiserfs_get_block,
+                * and it was a persistent trans.  Otherwise, it was nested 
above.
+                */
+               if (th->t_refcount > old_ref) {
+                       if (old_ref)
+                               th->t_refcount--;
+                       else {
+                               int err;
+                               reiserfs_write_lock(inode->i_sb);
+                               err = reiserfs_end_persistent_transaction(th);
+                               reiserfs_write_unlock(inode->i_sb);
+                               if (err)
+                                       ret = err;
+                       }
+               }
+       }
+       if (ret) {
+               unlock_page(page);
+               page_cache_release(page);
+       }
+       return ret;
+}
+
 static int reiserfs_prepare_write(struct file *f, struct page *page,
                                  unsigned from, unsigned to)
 {
@@ -2603,6 +2667,99 @@ static sector_t reiserfs_aop_bmap(struct
        return generic_block_bmap(as, block, reiserfs_bmap);
 }
 
+static int reiserfs_write_end(struct file *file, struct address_space *mapping,
+                             loff_t pos, unsigned len, unsigned copied,
+                             struct page *page, void *fsdata)
+{
+       struct inode *inode = page->mapping->host;
+       int ret = 0;
+       int update_sd = 0;
+       struct reiserfs_transaction_handle *th;
+       unsigned start;
+
+
+       reiserfs_wait_on_write_block(inode->i_sb);
+       if (reiserfs_transaction_running(inode->i_sb))
+               th = current->journal_info;
+       else
+               th = NULL;
+
+       start = pos & (PAGE_CACHE_SIZE - 1);
+       if (unlikely(copied < len)) {
+               if (!PageUptodate(page))
+                       copied = 0;
+
+               page_zero_new_buffers(page, start + copied, start + len);
+       }
+       flush_dcache_page(page);
+
+       reiserfs_commit_page(inode, page, start, start + copied);
+       unlock_page(page);
+       mark_page_accessed(page);
+       page_cache_release(page);
+
+       /* generic_commit_write does this for us, but does not update the
+        ** transaction tracking stuff when the size changes.  So, we have
+        ** to do the i_size updates here.
+        */
+       pos += copied;
+       if (pos > inode->i_size) {
+               struct reiserfs_transaction_handle myth;
+               reiserfs_write_lock(inode->i_sb);
+               /* If the file have grown beyond the border where it
+                  can have a tail, unmark it as needing a tail
+                  packing */
+               if ((have_large_tails(inode->i_sb)
+                    && inode->i_size > i_block_size(inode) * 4)
+                   || (have_small_tails(inode->i_sb)
+                       && inode->i_size > i_block_size(inode)))
+                       REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
+
+               ret = journal_begin(&myth, inode->i_sb, 1);
+               if (ret) {
+                       reiserfs_write_unlock(inode->i_sb);
+                       goto journal_error;
+               }
+               reiserfs_update_inode_transaction(inode);
+               inode->i_size = pos;
+               /*
+                * this will just nest into our transaction.  It's important
+                * to use mark_inode_dirty so the inode gets pushed around on 
the
+                * dirty lists, and so that O_SYNC works as expected
+                */
+               mark_inode_dirty(inode);
+               reiserfs_update_sd(&myth, inode);
+               update_sd = 1;
+               ret = journal_end(&myth, inode->i_sb, 1);
+               reiserfs_write_unlock(inode->i_sb);
+               if (ret)
+                       goto journal_error;
+       }
+       if (th) {
+               reiserfs_write_lock(inode->i_sb);
+               if (!update_sd)
+                       mark_inode_dirty(inode);
+               ret = reiserfs_end_persistent_transaction(th);
+               reiserfs_write_unlock(inode->i_sb);
+               if (ret)
+                       goto out;
+       }
+
+      out:
+       return ret == 0 ? copied : ret;
+
+      journal_error:
+       if (th) {
+               reiserfs_write_lock(inode->i_sb);
+               if (!update_sd)
+                       reiserfs_update_sd(th, inode);
+               ret = reiserfs_end_persistent_transaction(th);
+               reiserfs_write_unlock(inode->i_sb);
+       }
+
+       return ret;
+}
+
 static int reiserfs_commit_write(struct file *f, struct page *page,
                                 unsigned from, unsigned to)
 {

-- 

-
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to