Skip to contentSkip to conversation

: ufs: truncate should allocate block for last byte

PATCH In review Evgeniy Dushistov 2 participants 4 messages Jun 28, 2006 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : ufs: truncate should allocate block for last byte

This patch fixes buggy behaviour of UFS
in such kind of scenario:
open(, O_TRUNC...)
ftruncate(, 1024)
ftruncate(, 0)

such scenario causes ufs_panic and remount read-only. This happen because of according to specification UFS should always allocate block for last byte, and many parts of our implementation rely on this, but `ufs_truncate' doesn't care about this.

To make possible return error code and
to know about old size, this patch removes `truncate' from ufs inode_operations and
uses `setattr' method to call ufs_truncate.

---

fs/ufs/balloc.c +2 -46
Index: linux-2.6.17-mm2/fs/ufs/balloc.c
===================================================================
@@ -217,48 +217,6 @@ failed:
 	return;
 }
 
-static struct page *ufs_get_locked_page(struct address_space *mapping,
-				  unsigned long index)
-{
-	struct page *page;
-
-try_again:
-	page = find_lock_page(mapping, index);
-	if (!page) {
-		page = read_cache_page(mapping, index,
-				       (filler_t*)mapping->a_ops->readpage,
-				       NULL);
-		if (IS_ERR(page)) {
-			printk(KERN_ERR "ufs_change_blocknr: "
-			       "read_cache_page error: ino %lu, index: %lu\n",
-			       mapping->host->i_ino, index);
-			goto out;
-		}
-
-		lock_page(page);
-
-		if (!PageUptodate(page) || PageError(page)) {
-			unlock_page(page);
-			page_cache_release(page);
-
-			printk(KERN_ERR "ufs_change_blocknr: "
-			       "can not read page: ino %lu, index: %lu\n",
-			       mapping->host->i_ino, index);
-
-			page = ERR_PTR(-EIO);
-			goto out;
-		}
-	}
-
-	if (unlikely(!page->mapping || !page_has_buffers(page))) {
-		unlock_page(page);
-		page_cache_release(page);
-		goto try_again;/*we really need these buffers*/
-	}
-out:
-	return page;
-}
-
 /*
  * Modify inode page cache in such way:
  * have - blocks with b_blocknr equal to oldb...oldb+count-1
@@ -311,10 +269,8 @@ static void ufs_change_blocknr(struct in
 
 		set_page_dirty(page);
 
-		if (likely(cur_index != index)) {
-			unlock_page(page);
-			page_cache_release(page);
-		}
+		if (likely(cur_index != index))
+			ufs_put_locked_page(page);
  	}
 	UFSD("EXIT\n");
 }
fs/ufs/truncate.c +135 -13

168 lines not on this page · raw message

fs/ufs/util.c +54 -0

57 lines not on this page · raw message

fs/ufs/util.h +8 -0
Index: linux-2.6.17-mm2/fs/ufs/util.h
===================================================================
@@ -251,6 +251,14 @@ extern void _ubh_ubhcpymem_(struct ufs_s
 #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
 extern void _ubh_memcpyubh_(struct ufs_sb_private_info *, struct ufs_buffer_head *, unsigned char *, unsigned);
 
+/* This functions works with cache pages*/
+extern struct page *ufs_get_locked_page(struct address_space *mapping,
+					pgoff_t index);
+static inline void ufs_put_locked_page(struct page *page)
+{
+       unlock_page(page);
+       page_cache_release(page);
+}
 
 
 /*
fs/ufs/file.c +0 -4
Index: linux-2.6.17-mm2/fs/ufs/file.c
===================================================================
@@ -60,7 +60,3 @@ const struct file_operations ufs_file_op
 	.fsync		= ufs_sync_file,
 	.sendfile	= generic_file_sendfile,
 };
-
-struct inode_operations ufs_file_inode_operations = {
-	.truncate	= ufs_truncate,
-};
fs/ufs/inode.c +5 -2
Index: linux-2.6.17-mm2/fs/ufs/inode.c
===================================================================
@@ -841,14 +841,17 @@ int ufs_sync_inode (struct inode *inode)
 
 void ufs_delete_inode (struct inode * inode)
 {
+	loff_t old_i_size;
+
 	truncate_inode_pages(&inode->i_data, 0);
 	/*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
 	lock_kernel();
 	mark_inode_dirty(inode);
 	ufs_update_inode(inode, IS_SYNC(inode));
+	old_i_size = inode->i_size;
 	inode->i_size = 0;
-	if (inode->i_blocks)
-		ufs_truncate (inode);
+	if (inode->i_blocks && ufs_truncate(inode, old_i_size))
+		ufs_warning(inode->i_sb, __FUNCTION__, "ufs_truncate failed\n");
 	ufs_free_inode (inode);
 	unlock_kernel();
 }
include/linux/ufs_fs.h +1 -1
Index: linux-2.6.17-mm2/include/linux/ufs_fs.h
===================================================================
@@ -993,7 +993,7 @@ extern void ufs_panic (struct super_bloc
 extern struct inode_operations ufs_fast_symlink_inode_operations;
 
 /* truncate.c */
-extern void ufs_truncate (struct inode *);
+extern int ufs_truncate (struct inode *, loff_t);
 
 static inline struct ufs_sb_info *UFS_SB(struct super_block *sb)
 {
Signature · 1 line
/Evgeniy
#1 lore raw
Quoted 31 lines · On Wed, 28 Jun 2006 13:38:51 +0400 Evgeniy Dushistov <[email protected]> wrote:
fs/ufs/util.c +31 -0
+struct page *ufs_get_locked_page(struct address_space *mapping,
+				 pgoff_t index)
+{
+	struct page *page;
+
+try_again:
+	page = find_lock_page(mapping, index);
+	if (!page) {
+		page = read_cache_page(mapping, index,
+				       (filler_t*)mapping->a_ops->readpage,
+				       NULL);
+		if (IS_ERR(page)) {
+			printk(KERN_ERR "ufs_change_blocknr: "
+			       "read_cache_page error: ino %lu, index: %lu\n",
+			       mapping->host->i_ino, index);
+			goto out;
+		}
+
+		lock_page(page);
+
+		if (!PageUptodate(page) || PageError(page)) {
+			unlock_page(page);
+			page_cache_release(page);
+
+			printk(KERN_ERR "ufs_change_blocknr: "
+			       "can not read page: ino %lu, index: %lu\n",
+			       mapping->host->i_ino, index);
+
+			page = ERR_PTR(-EIO);
+			goto out;
+		}

fs/ufs/util.c ufs_set_inode_dev(struct super_block *sb · line 289

Quoted 10 lines
fs/ufs/util.c +10 -0
+	}
+
+	if (unlikely(!page->mapping || !page_has_buffers(page))) {
+		unlock_page(page);
+		page_cache_release(page);
+		goto try_again;/*we really need these buffers*/
+	}
+out:
+	return page;
+}

I think there's a (preexisting) problem here. When one thread is executing ufs_get_locked_page() while a second thread is running truncate().

If truncate got to the page first, truncate_complete_page() will mark the page !uptodate and will later unlock it. Now this function gets the page lock and emits a printk (bad) and assumes -EIO (worse).

That scenario might not be possible because of i_mutex coverage, dunno.

But if it _is_ possible, it can be simply fixed by doing

	lock_page(page);

+ if (page->mapping == NULL) {
+ /* truncate() got there first */
+ page_cache_release(page);
+ goto try_again;
+ }

That's if it is appropriate to re-instantiate the page at a place which is now outside i_size...

#2 lore raw
Quoted 13 lines · Andrew Morton

On Wed, 28 Jun 2006 13:38:51 +0400

fs/ufs/util.c ufs_set_inode_dev(struct super_block *sb · line 289

Quoted 8 lines
fs/ufs/util.c +8 -0
+	if (unlikely(!page->mapping || !page_has_buffers(page))) {
+		unlock_page(page);
+		page_cache_release(page);
+		goto try_again;/*we really need these buffers*/
+	}
+out:
+	return page;
+}

I think there's a (preexisting) problem here. When one thread is executing ufs_get_locked_page() while a second thread is running truncate().

Quoted 6 lines

If truncate got to the page first, truncate_complete_page() will mark the page !uptodate and will later unlock it. Now this function gets the page lock and emits a printk (bad) and assumes -EIO (worse).

That scenario might not be possible because of i_mutex coverage, dunno.

I suppose this is possible because of
a)page may be mapped to hole
b)sys_msync doesn't use i_mutex
c)in case of block allocation we can call ufs_get_locked_page

Quoted 2 lines

But if it _is_ possible, it can be simply fixed by doing

But you added such check "!page->mapping" into ufs_get_locked_page, is it not enough?

Quoted 9 lines
	lock_page(page);
(quoted diff) +5 -0
+	if (page->mapping == NULL) {
+		/* truncate() got there first */
+		page_cache_release(page);
+		goto try_again;
+	}

That's if it is appropriate to re-instantiate the page at a place which is now outside i_size...

Signature · 1 line
/Evgeniy
#3 lore raw
Quoted 18 lines · On Wed, 28 Jun 2006 19:24:50 +0400 Evgeniy Dushistov <[email protected]> wrote:
Quoted 17 lines · Andrew Morton

On Wed, 28 Jun 2006 13:38:51 +0400

fs/ufs/util.c ufs_set_inode_dev(struct super_block *sb · line 289

Quoted 8 lines
fs/ufs/util.c +8 -0
+	if (unlikely(!page->mapping || !page_has_buffers(page))) {
+		unlock_page(page);
+		page_cache_release(page);
+		goto try_again;/*we really need these buffers*/
+	}
+out:
+	return page;
+}

I think there's a (preexisting) problem here. When one thread is executing ufs_get_locked_page() while a second thread is running truncate().

If truncate got to the page first, truncate_complete_page() will mark the page !uptodate and will later unlock it. Now this function gets the page lock and emits a printk (bad) and assumes -EIO (worse).

Quoted 6 lines
Quoted 2 lines

That scenario might not be possible because of i_mutex coverage, dunno.

I suppose this is possible because of
a)page may be mapped to hole
b)sys_msync doesn't use i_mutex
c)in case of block allocation we can call ufs_get_locked_page

OK.

Quoted 4 lines
Quoted 2 lines

But if it _is_ possible, it can be simply fixed by doing

But you added such check "!page->mapping" into ufs_get_locked_page, is it not enough?

That is what I was proposing, here:

Quoted 6 lines
Quoted 6 lines
	lock_page(page);
(quoted diff) +5 -0
+	if (page->mapping == NULL) {
+		/* truncate() got there first */
+		page_cache_release(page);
+		goto try_again;
+	}