]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/ext4/inode.c
ext4: Use page_mkwrite vma_operations to get mmap write notification.
[linux-2.6-omap-h63xx.git] / fs / ext4 / inode.c
index 7cce96a6935ee22ff4da105382ff32a55824146b..034fc544aa66b2d1887555f0302fa23ab101b7f1 100644 (file)
@@ -508,11 +508,12 @@ static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
  *             direct blocks
  */
 static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
-                       ext4_fsblk_t goal, int indirect_blks, int blks,
-                       ext4_fsblk_t new_blocks[4], int *err)
+                               ext4_lblk_t iblock, ext4_fsblk_t goal,
+                               int indirect_blks, int blks,
+                               ext4_fsblk_t new_blocks[4], int *err)
 {
        int target, i;
-       unsigned long count = 0;
+       unsigned long count = 0, blk_allocated = 0;
        int index = 0;
        ext4_fsblk_t current_block = 0;
        int ret = 0;
@@ -525,12 +526,13 @@ static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
         * the first direct block of this branch.  That's the
         * minimum number of blocks need to allocate(required)
         */
-       target = blks + indirect_blks;
-
-       while (1) {
+       /* first we try to allocate the indirect blocks */
+       target = indirect_blks;
+       while (target > 0) {
                count = target;
                /* allocating blocks for indirect blocks and direct blocks */
-               current_block = ext4_new_blocks(handle,inode,goal,&count,err);
+               current_block = ext4_new_meta_blocks(handle, inode,
+                                                       goal, &count, err);
                if (*err)
                        goto failed_out;
 
@@ -540,16 +542,48 @@ static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
                        new_blocks[index++] = current_block++;
                        count--;
                }
-
-               if (count > 0)
+               if (count > 0) {
+                       /*
+                        * save the new block number
+                        * for the first direct block
+                        */
+                       new_blocks[index] = current_block;
+                       printk(KERN_INFO "%s returned more blocks than "
+                                               "requested\n", __func__);
+                       WARN_ON(1);
                        break;
+               }
        }
 
-       /* save the new block number for the first direct block */
-       new_blocks[index] = current_block;
-
+       target = blks - count ;
+       blk_allocated = count;
+       if (!target)
+               goto allocated;
+       /* Now allocate data blocks */
+       count = target;
+       /* allocating blocks for data blocks */
+       current_block = ext4_new_blocks(handle, inode, iblock,
+                                               goal, &count, err);
+       if (*err && (target == blks)) {
+               /*
+                * if the allocation failed and we didn't allocate
+                * any blocks before
+                */
+               goto failed_out;
+       }
+       if (!*err) {
+               if (target == blks) {
+               /*
+                * save the new block number
+                * for the first direct block
+                */
+                       new_blocks[index] = current_block;
+               }
+               blk_allocated += count;
+       }
+allocated:
        /* total number of blocks allocated for direct blocks */
-       ret = count;
+       ret = blk_allocated;
        *err = 0;
        return ret;
 failed_out:
@@ -584,8 +618,9 @@ failed_out:
  *     as described above and return 0.
  */
 static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
-                       int indirect_blks, int *blks, ext4_fsblk_t goal,
-                       ext4_lblk_t *offsets, Indirect *branch)
+                               ext4_lblk_t iblock, int indirect_blks,
+                               int *blks, ext4_fsblk_t goal,
+                               ext4_lblk_t *offsets, Indirect *branch)
 {
        int blocksize = inode->i_sb->s_blocksize;
        int i, n = 0;
@@ -595,7 +630,7 @@ static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
        ext4_fsblk_t new_blocks[4];
        ext4_fsblk_t current_block;
 
-       num = ext4_alloc_blocks(handle, inode, goal, indirect_blks,
+       num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
                                *blks, new_blocks, &err);
        if (err)
                return err;
@@ -855,8 +890,9 @@ int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
        /*
         * Block out ext4_truncate while we alter the tree
         */
-       err = ext4_alloc_branch(handle, inode, indirect_blks, &count, goal,
-                               offsets + (partial - chain), partial);
+       err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
+                                       &count, goal,
+                                       offsets + (partial - chain), partial);
 
        /*
         * The ext4_splice_branch call will free and forget any buffers
@@ -3528,3 +3564,64 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
 
        return err;
 }
+
+static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
+{
+       return !buffer_mapped(bh);
+}
+
+int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page)
+{
+       loff_t size;
+       unsigned long len;
+       int ret = -EINVAL;
+       struct file *file = vma->vm_file;
+       struct inode *inode = file->f_path.dentry->d_inode;
+       struct address_space *mapping = inode->i_mapping;
+
+       /*
+        * Get i_alloc_sem to stop truncates messing with the inode. We cannot
+        * get i_mutex because we are already holding mmap_sem.
+        */
+       down_read(&inode->i_alloc_sem);
+       size = i_size_read(inode);
+       if (page->mapping != mapping || size <= page_offset(page)
+           || !PageUptodate(page)) {
+               /* page got truncated from under us? */
+               goto out_unlock;
+       }
+       ret = 0;
+       if (PageMappedToDisk(page))
+               goto out_unlock;
+
+       if (page->index == size >> PAGE_CACHE_SHIFT)
+               len = size & ~PAGE_CACHE_MASK;
+       else
+               len = PAGE_CACHE_SIZE;
+
+       if (page_has_buffers(page)) {
+               /* return if we have all the buffers mapped */
+               if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
+                                      ext4_bh_unmapped))
+                       goto out_unlock;
+       }
+       /*
+        * OK, we need to fill the hole... Do write_begin write_end
+        * to do block allocation/reservation.We are not holding
+        * inode.i__mutex here. That allow * parallel write_begin,
+        * write_end call. lock_page prevent this from happening
+        * on the same page though
+        */
+       ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
+                       len, AOP_FLAG_UNINTERRUPTIBLE, &page, NULL);
+       if (ret < 0)
+               goto out_unlock;
+       ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
+                       len, len, page, NULL);
+       if (ret < 0)
+               goto out_unlock;
+       ret = 0;
+out_unlock:
+       up_read(&inode->i_alloc_sem);
+       return ret;
+}