Skip to contentSkip to conversation

PATCH: truncate_inode_pages fix (this time against ac5)

PATCH Juan J. Quintela 1 participant 1 message May 30, 2000

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

Hi
I have just re-diff my patch against 2.4.0-test1-ac5. It is the same that the one that I sent before. The only difference is that I reverse a change from riel patch in shrink_mmap (we need to call __remove_inode_pages with the page cache_lock held.

I am still waiting for comments of the patch for the filesystems people (hint, hint, hint...), before transform everything to support also partial truncates.

I am very interested in *success/failure* reports. The system works stable here, without that patch I get the BUG in inode.c pretty easily here.

Later, Juan.

Note for Alan: if you continue with the riel patch, please apply this
one, it fixes the BUG in inode.c that more people are seeing. At least include the spinlock change.

Quoted 54 lines

Hi
the actual truncate_inode_pages can actually *write* part of
the file that has just been truncated. That makes that things doesn't work. I can trigger the BUG in fs:/inode.c:clear_inode using mmap002 and riel aging patch in 2.4.0-test1. The BUG triggered is:

	if (inode->i_data.nrpages)
		BUG();

The problem is that inode->i_data.nrpages can become negative. I have got here -6 truncating a file of 384MB.

This patch deals with this problem, and try to do truncate_inode_pages safe.

Notice that the function only works for truncate complete files, I have left the old function for the partial case. If people think that this function is OK, I will incorporate the partial case. I have no done it because I want feedback from the MM people and the FS people, and the algorithms for complete files are cleaner.

I would like to now if it is possible to have a locked buffer without the page being locked, i.e. If I can manipulate the buffers in one page without locking them when I have the page lock. (I don't think so, and then I have to lock also buffers).

With this patch things are rock solid here.

This patch does:

- defines a new function: discard_buffer, it will lock the buffer (waiting if needed) and remove the buffer from all the queues. It is like unmap_buffer, but makes sure that we don't do any IO and that we remove the buffer from all the lists.

- defines a new function: block_destroy_buffers: it is a mix of block_flushpage and do_try_to_free_pages. It will make all the buffers in that page disappear calling discard_buffer. Notice the way that we iterate through all the buffer heads. I think that it is not racy, but I would like to hear comments from people than know more about buffer heads handling.

- It changes __remove_inode_pages to check that there are not buffers in that page. Users of the function must make sure about that. It changes all the callers of the function satisfy that requirement.

- I change invalidate_inode_pages (again). Now block_destroy_buffers can wait, then we are *civilized* citizens and drop any lock that we have before call that block_destroy_buffers, and reaquire later.

- It has my *old* rewrite of truncate_inode_pages to use two auxiliary functions and lock the same way for the partial page and for the rest. (That code is wrong anyway, pass to the next function).

Quoted 7 lines

- The new function truncate_inode_pages, that is a copy of invalidate_inodes, but we wait for locked pages.

Comments, please??

I am very interested in the coments from the fs-people. What they think about the locking that I am using in the buffer.c file.

Acknowledgement: This patch would have not been possible without the comments of
arjan, bcrl, davej, riel and RogerL, .... in #kernelnewbies (irc.openprojects.net). They did a lot of review of previous versions of the patch. The errors are mine, but good ideas come from discussions with them :)

fs/buffer.c +86 -17
diff -urN --exclude-from=/home/lfcia/quintela/work/kernel/exclude ac5/fs/buffer.c testing2/fs/buffer.c
@@ -1281,6 +1281,56 @@
 	}
 }
 
+/**
+ * discard_buffer - discard that buffer without doing any IO
+ * @bh: buffer to discard
+ * 
+ * This function removes a buffer from all the queues, without doing
+ * any IO, we are not interested in the contents of the buffer.  This
+ * function can block if the buffer is locked.
+ */
+static struct buffer_head *discard_buffer(struct buffer_head * bh)
+{
+	int index = BUFSIZE_INDEX(bh->b_size);
+	struct buffer_head *next;
+
+	/* grab the lru lock here to block bdflush. */
+	atomic_inc(&bh->b_count);
+	lock_buffer(bh);
+	next = bh->b_this_page;
+	clear_bit(BH_Uptodate, &bh->b_state);
+	clear_bit(BH_Mapped, &bh->b_state);
+	clear_bit(BH_Req, &bh->b_state);
+	clear_bit(BH_New, &bh->b_state);
+
+	spin_lock(&lru_list_lock);
+	write_lock(&hash_table_lock);
+	spin_lock(&free_list[index].lock);
+	spin_lock(&unused_list_lock);
+
+	if (!atomic_dec_and_test(&bh->b_count))
+		BUG();
+
+	__hash_unlink(bh);
+	/* The bunffer can be either on the regular
+	 * queues or on the free list..
+	 */
+	if (bh->b_dev != B_FREE)
+		__remove_from_queues(bh);
+	else
+		__remove_from_free_list(bh, index);
+	__put_unused_buffer_head(bh);	
+	spin_unlock(&unused_list_lock);
+	write_unlock(&hash_table_lock);
+	spin_unlock(&free_list[index].lock);
+	spin_unlock(&lru_list_lock);
+	/* We can unlock the buffer, we have just returned it.
+	 * Ditto for the counter 
+         */
+	return next;
+}
+
+
 /*
  * We don't have to release all buffers here, but
  * we have to be sure that no dirty buffer is left
@@ -1313,24 +1363,43 @@
 		bh = next;
 	} while (bh != head);
 
-	/*
-	 * subtle. We release buffer-heads only if this is
-	 * the 'final' flushpage. We have invalidated the get_block
-	 * cached value unconditionally, so real IO is not
-	 * possible anymore.
-	 *
-	 * If the free doesn't work out, the buffers can be
-	 * left around - they just turn into anonymous buffers
-	 * instead.
-	 */
-	if (!offset) {
-		if (!try_to_free_buffers(page, 0)) {
-			atomic_inc(&buffermem_pages);
-			return 0;
-		}
-	}
-
 	return 1;
+}
+
+/**
+ * block_destroy_buffers - Will destroy the contents of all the
+ * buffers in this page
+ * @page: page to examine the buffers
+ *
+ * This function destroy all the buffers in one page without making
+ * any IO.  The function can block due to the fact that discad_bufferr
+ * can block.
+ */
+void block_destroy_buffers(struct page *page)
+{
+	struct buffer_head  *bh, *head;
+
+	if (!PageLocked(page))
+		BUG();
+	if (!page->buffers)
+		return;
+
+	head = page->buffers;
+	bh = head;
+	do {
+		/* We need to get the next buffer from discard buffer
+		 * because discard buffer can block and anybody else
+		 * can change the buffer list under our feet.
+		 */
+		bh = discard_buffer(bh);
+	}while (bh != head);
+
+	/* Wake up anyone waiting for buffer heads */
+	wake_up(&buffer_wait);
+
+	/* And free the page */
+	page->buffers = NULL;
+	page_cache_release(page);
 }
 
 static void create_empty_buffers(struct page *page, struct inode *inode, unsigned long blocksize)
fs/inode.c +2 -2
diff -urN --exclude-from=/home/lfcia/quintela/work/kernel/exclude ac5/fs/inode.c testing2/fs/inode.c
@@ -322,7 +322,7 @@
 
 		inode = list_entry(inode_entry, struct inode, i_list);
 		if (inode->i_data.nrpages)
-			truncate_inode_pages(&inode->i_data, 0);
+			truncate_all_inode_pages(&inode->i_data);
 		clear_inode(inode);
 		destroy_inode(inode);
 	}
@@ -768,7 +768,7 @@
 				spin_unlock(&inode_lock);
 
 				if (inode->i_data.nrpages)
-					truncate_inode_pages(&inode->i_data, 0);
+					truncate_all_inode_pages(&inode->i_data);
 
 				destroy = 1;
 				if (op && op->delete_inode) {
include/linux/fs.h +1 -0
diff -urN --exclude-from=/home/lfcia/quintela/work/kernel/exclude ac5/include/linux/fs.h testing2/include/linux/fs.h
@@ -1113,6 +1113,7 @@
 
 /* Generic buffer handling for block filesystems.. */
 extern int block_flushpage(struct page *, unsigned long);
+extern void block_destroy_buffers(struct page *);
 extern int block_symlink(struct inode *, const char *, int);
 extern int block_write_full_page(struct page*, get_block_t*);
 extern int block_read_full_page(struct page*, get_block_t*);
include/linux/mm.h +1 -0
diff -urN --exclude-from=/home/lfcia/quintela/work/kernel/exclude ac5/include/linux/mm.h testing2/include/linux/mm.h
@@ -462,6 +462,7 @@
 extern unsigned long page_unuse(struct page *);
 extern int shrink_mmap(int, int);
 extern void truncate_inode_pages(struct address_space *, loff_t);
+extern void truncate_all_inode_pages(struct address_space *);
 
 /* generic vm_area_ops exported for stackable file systems */
 extern int filemap_swapout(struct page * page, struct file *file);
mm/filemap.c +105 -50

237 lines not on this page · raw message

mm/swap_state.c +3 -2
diff -urN --exclude-from=/home/lfcia/quintela/work/kernel/exclude ac5/mm/swap_state.c testing2/mm/swap_state.c
@@ -103,9 +103,10 @@
 	if (!PageLocked(page))
 		BUG();
 
-	if (block_flushpage(page, 0))
-		lru_cache_del(page);
+	if (page->buffers)
+ 		block_destroy_buffers(page);
 
+	lru_cache_del(page);
 	__delete_from_swap_cache(page);
 	page_cache_release(page);
 }
Signature · 6 lines
In theory, practice and theory are the same, but in practice they 
are different -- Larry McVoy
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux.eu.org/Linux-MM/