Skip to contentSkip to conversation

reiser4: batch discard support (FITRIM ioctl): initial implementation.

PATCH Superseded Ivan Shapovalov 1 participant 12 messages Dec 12, 2014

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover reiser4: batch discard support (FITRIM ioctl): initial implementation.

This patch series implements FITRIM ioctl support in reiser4.

The FITRIM ioctl is supposed to be applied to any directory inside the target filesystem. At least, this can be deduced from fstrim utility behavior on mainstream filesystems (e. g. ext2/3/4).

The idea of implementation is per Edward's advices: FITRIM handler iteratively grabs a portion of disk space, sequentially allocates extents within grabbed space and commits the resulting atom. This way we avoid starving concurrent processes of the disk space. This process is repeated until the partition has been fully scanned and processed.

Points I'm really uncertain of:
- creation of empty atoms (reiser4_create_atom())
- handling of empty atoms in commit_current_atom()

v2: - fix PATCH 5/7 "reiser4: txnmgr: call reiser4_post_write_back_hook() for empty atoms."
* fix a typo
* release atom spinlock before calling reiser4_post_write_back_hook()

v3: - use bitwise shift instead of integer division by 4 to calculate 25%
- honor "minlen" of ioctl parameters (minimal extent length to consider for discarding)
- fix a stupidity in reiser4_trim_fs() (two local variables holding the same superblock pointer)

v4: - refactor BA_SOME_SPACE implementation, fix calculation of space-to-grab if !BA_RESERVED
- rename reiser4_blocknr_hint's "forward" to "monotonic_forward"
- move free space accounting by pre-commit hook to block_alloc.c
- explain BA_DEFER flag behavior in comments
- fix "minlen" addition to alloc_blocks_forward() (use minlen in both calls)
- refactor (equivalent at this time ;)) reiser4_trim_fs()
- move "capture_count == 0" shortcut in commit_current_atom() below current_atom_complete_writes() in order to finish the atom's flush_queues

Ivan Shapovalov (10):
reiser4: block_alloc: add BA_SOME_SPACE flag for grabbing a fixed amount of space.
reiser4: block_alloc: add a "monotonic_forward" parameter to reiser4_blocknr_hint to allocate blocks only in forward direction.
reiser4: block_alloc: move block accounting by pre-commit hook into block_alloc.c and document BA_DEFER behavior.
reiser4: txnmgr: free allocated but unneeded atom in atom_begin_and_assign_to_txnh().
reiser4: txnmgr: add reiser4_create_atom() which creates an empty atom without capturing any nodes.
reiser4: txnmgr: move "empty atom" shortcut slightly below.
reiser4: batch discard support: add a dummy FITRIM ioctl handler for directories.
reiser4: batch discard support: actually implement the FITRIM ioctl handler.
reiser4: block_alloc: add a "min_len" parameter to reiser4_blocknr_hint to limit allocated extent length from below.
reiser4: batch discard support: honor minimal extent length passed from the userspace.

 fs/reiser4/block_alloc.c         |  66 +++++++++++++++++--
 fs/reiser4/block_alloc.h         |  12 +++-
 fs/reiser4/plugin/dir/dir.h      |   2 +
 fs/reiser4/plugin/file_ops.c     |  62 ++++++++++++++++++
 fs/reiser4/plugin/object.c       |   6 +-
 fs/reiser4/plugin/space/bitmap.c |  53 +++++++---------
 fs/reiser4/super_ops.c           | 134 +++++++++++++++++++++++++++++++++++++++
 fs/reiser4/txnmgr.c              |  57 +++++++++++------
 fs/reiser4/txnmgr.h              |   2 +
 9 files changed, 336 insertions(+), 58 deletions(-)
Signature · 1 line
2.1.3
#1 lore raw

PATCH v4 1/10 reiser4: block_alloc: add BA_SOME_SPACE flag for grabbing a fixed amount of space.

This is used for FITRIM ioctl which will iteratively grab, allocate and trim disk space bit by bit to avoid starving the rest of system.

---
 fs/reiser4/block_alloc.c | 22 ++++++++++++++++++----
 fs/reiser4/block_alloc.h |  5 ++++-
 2 files changed, 22 insertions(+), 5 deletions(-)
fs/reiser4/block_alloc.c +18 -4
index 324b11c..14dd378 100644
@@ -264,7 +264,7 @@ int reiser4_check_block_counters(const struct super_block *super)
 static int
 reiser4_grab(reiser4_context * ctx, __u64 count, reiser4_ba_flags_t flags)
 {
-	__u64 free_blocks;
+	__s64 allowed_blocks;
 	int ret = 0, use_reserved = flags & BA_RESERVED;
 	reiser4_super_info_data *sbinfo;
 
@@ -280,10 +280,24 @@ reiser4_grab(reiser4_context * ctx, __u64 count, reiser4_ba_flags_t flags)
 
 	spin_lock_reiser4_super(sbinfo);
 
-	free_blocks = sbinfo->blocks_free;
+	allowed_blocks = use_reserved ? sbinfo->blocks_free
+	                              : sbinfo->blocks_free - sbinfo->blocks_reserved;
 
-	if ((use_reserved && free_blocks < count) ||
-	    (!use_reserved && free_blocks < count + sbinfo->blocks_reserved)) {
+	if (flags & BA_SOME_SPACE) {
+		/* Reserve 25% of all free space */
+		if (allowed_blocks <= 0) {
+			/* No space at all */
+			ret = RETERR(-ENOSPC);
+			goto unlock_and_ret;
+		}
+
+		count = allowed_blocks >> 2;
+		if (count == 0) {
+			/* Less than 4 free blocks */
+			count = allowed_blocks;
+		}
+	} else if (count > allowed_blocks) {
+		/* Not enough space */
 		ret = RETERR(-ENOSPC);
 		goto unlock_and_ret;
 	}
fs/reiser4/block_alloc.h +4 -1
index a4e98af..bfc6be9 100644
@@ -79,7 +79,10 @@ enum reiser4_ba_flags {
 	BA_FORCE = (1 << 5),
 
 	/* use default start value for free blocks search. */
-	BA_USE_DEFAULT_SEARCH_START = (1 << 6)
+	BA_USE_DEFAULT_SEARCH_START = (1 << 6),
+
+	/* reserve some fixed amount of space */
+	BA_SOME_SPACE = (1 << 7),
 };
 
 typedef enum reiser4_ba_flags reiser4_ba_flags_t;
Signature · 1 line
2.1.3
#2 lore raw

PATCH v4 2/10 reiser4: block_alloc: add a "monotonic_forward" parameter to reiser4_blocknr_hint to allocate blocks only in forward direction.

In reply to Ivan Shapovalov
---
 fs/reiser4/block_alloc.c         |  4 ++--
 fs/reiser4/block_alloc.h         |  5 +++--
 fs/reiser4/plugin/space/bitmap.c | 10 ++++++----
 3 files changed, 11 insertions(+), 8 deletions(-)
fs/reiser4/block_alloc.c +2 -2
index 14dd378..56795ca 100644
@@ -264,7 +264,7 @@ int reiser4_check_block_counters(const struct super_block *super)
 static int
 reiser4_grab(reiser4_context * ctx, __u64 count, reiser4_ba_flags_t flags)
 {
-	__s64 allowed_blocks;
+	__u64 allowed_blocks;
 	int ret = 0, use_reserved = flags & BA_RESERVED;
 	reiser4_super_info_data *sbinfo;
 
@@ -285,7 +285,7 @@ reiser4_grab(reiser4_context * ctx, __u64 count, reiser4_ba_flags_t flags)
 
 	if (flags & BA_SOME_SPACE) {
 		/* Reserve 25% of all free space */
-		if (allowed_blocks <= 0) {
+		if (allowed_blocks == 0) {
 			/* No space at all */
 			ret = RETERR(-ENOSPC);
 			goto unlock_and_ret;
fs/reiser4/block_alloc.h +3 -2
index bfc6be9..903bc8f 100644
@@ -51,9 +51,10 @@ struct reiser4_blocknr_hint {
 	/* block allocator assumes that blocks, which will be mapped to disk,
 	   are in this specified block_stage */
 	block_stage_t block_stage;
-	/* If direction = 1 allocate blocks in backward direction from the end
-	 * of disk to the beginning of disk.  */
+	/* allocate blocks in backward direction */
 	unsigned int backward:1;
+	/* allocate blocks only in forward direction starting from blk */
+	unsigned int monotonic_forward:1;
 
 };
 
fs/reiser4/plugin/space/bitmap.c +6 -4
index 3da3f6b..e56c336 100644
@@ -1124,10 +1124,12 @@ static int alloc_blocks_forward(reiser4_blocknr_hint *hint, int needed,
 	actual_len =
 	    bitmap_alloc_forward(&search_start, &search_end, 1, needed);
 
-	/* There is only one bitmap search if max_dist was specified or first
-	   pass was from the beginning of the bitmap. We also do one pass for
-	   scanning bitmap in backward direction. */
-	if (!(actual_len != 0 || hint->max_dist != 0 || search_start == 0)) {
+	/* There is only one bitmap search if max_dist was specified, first
+	   pass was from the beginning of the bitmap, or if the monotonic flag
+	   has been set. Otherwise we also do one pass for scanning bitmap in
+	   backward direction. */
+	if (actual_len == 0 && search_start != 0 &&
+	    hint->max_dist == 0 && !hint->monotonic_forward) {
 		/* next step is a scanning from 0 to search_start */
 		search_end = search_start;
 		search_start = 0;
Signature · 1 line
2.1.3
#3 lore raw

PATCH v4 3/10 reiser4: block_alloc: move block accounting by pre-commit hook into block_alloc.c and document BA_DEFER behavior.

In reply to Ivan Shapovalov

BA_DEFER deallocations ignore target_stage and other flags and use some implied values for these parameters. Document these values and where they do matter.

Also move deallocated block accounting into block_alloc.c because
1) it is allocator-independent;
2) it allows discard code to rely on that accounting is done in pre-commit hook and not later, so that discard code should not use BA_PERMANENT in its allocations.

---
 fs/reiser4/block_alloc.c         | 44 +++++++++++++++++++++++++++++++++++++++-
 fs/reiser4/plugin/space/bitmap.c | 23 ++-------------------
 2 files changed, 45 insertions(+), 22 deletions(-)
fs/reiser4/block_alloc.c +43 -1
index 56795ca..549cc0a 100644
@@ -998,6 +998,19 @@ reiser4_check_blocks(const reiser4_block_nr * start,
 /* BA_FORMATTED bit is only used when BA_DEFER in not present: it is used to
    distinguish blocks allocated for unformatted and formatted nodes */
 
+/* if BA_DEFER is enabled, @target_stage and other @flags are ignored.
+ *
+ * @target_stage is implied to be BLOCK_NOT_COUNTED.
+ * (assumption is used in reiser4_post_write_back_hook() and apply_dset())
+ *
+ * @flags are implied to have BA_PERMANENT.
+ * (assumption is used in reiser4_pre_commit_hook() which counts deallocated
+ *  blocks)
+ * 
+ * That is, if a deferred deallocation is done after reiser4_pre_commit_hook(),
+ * then BA_PERMANENT is implied to be disabled.
+ */
+
 int
 reiser4_dealloc_blocks(const reiser4_block_nr * start,
 		       const reiser4_block_nr * len,
@@ -1089,10 +1102,39 @@ reiser4_dealloc_blocks(const reiser4_block_nr * start,
 	return 0;
 }
 
+/* an actor for counting blocks that are going to be deallocated */
+static int
+count_dset_blocks(txn_atom * atom, const reiser4_block_nr * start,
+                  const reiser4_block_nr * len, void *data)
+{
+	reiser4_block_nr *blocks_freed_p = data;
+
+	if (len != NULL) {
+		(*blocks_freed_p) += *len;
+	} else {
+		(*blocks_freed_p)++;
+	}
+	return 0;
+}
+
 /* wrappers for block allocator plugin methods */
 int reiser4_pre_commit_hook(void)
 {
-	assert("zam-502", get_current_super_private() != NULL);
+	reiser4_block_nr blocks_freed = 0;
+	reiser4_super_info_data *sbinfo = get_current_super_private();
+	txn_atom *atom = get_current_atom_locked();
+
+	assert("zam-502", sbinfo != NULL);
+
+	assert("zam-876", atom->stage == ASTAGE_PRE_COMMIT);
+	spin_unlock_atom(atom);
+
+	atom_dset_deferred_apply(atom, count_dset_blocks, &blocks_freed, 0);
+
+	spin_lock_reiser4_super(sbinfo);
+	sbinfo->blocks_free_committed += blocks_freed - atom->nr_blocks_allocated;
+	spin_unlock_reiser4_super(sbinfo);
+
 	sa_pre_commit_hook();
 	return 0;
 }
fs/reiser4/plugin/space/bitmap.c +2 -21
index e56c336..0ce07da 100644
@@ -1322,15 +1322,13 @@ static void cond_add_to_overwrite_set(txn_atom * atom, jnode * node)
    pages in a single-linked list */
 static int
 apply_dset_to_commit_bmap(txn_atom * atom, const reiser4_block_nr * start,
-			  const reiser4_block_nr * len, void *data)
+			  const reiser4_block_nr * len, void *data UNUSED_ARG)
 {
 
 	bmap_nr_t bmap;
 	bmap_off_t offset;
 	int ret;
 
-	long long *blocks_freed_p = data;
-
 	struct bitmap_node *bnode;
 
 	struct super_block *sb = reiser4_get_current_sb();
@@ -1366,11 +1364,8 @@ apply_dset_to_commit_bmap(txn_atom * atom, const reiser4_block_nr * start,
 		assert("zam-443",
 		       offset + *len <= bmap_bit_count(sb->s_blocksize));
 		reiser4_clear_bits(data, offset, (bmap_off_t) (offset + *len));
-
-		(*blocks_freed_p) += *len;
 	} else {
 		reiser4_clear_bit(offset, data);
-		(*blocks_freed_p)++;
 	}
 
 	bnode_set_commit_crc(bnode, bnode_calc_crc(bnode, sb->s_blocksize));
@@ -1393,8 +1388,6 @@ int reiser4_pre_commit_hook_bitmap(void)
 	struct super_block *super = reiser4_get_current_sb();
 	txn_atom *atom;
 
-	long long blocks_freed = 0;
-
 	atom = get_current_atom_locked();
 	assert("zam-876", atom->stage == ASTAGE_PRE_COMMIT);
 	spin_unlock_atom(atom);
@@ -1460,19 +1453,7 @@ int reiser4_pre_commit_hook_bitmap(void)
 		}
 	}
 
-	atom_dset_deferred_apply(atom, apply_dset_to_commit_bmap, &blocks_freed, 0);
-
-	blocks_freed -= atom->nr_blocks_allocated;
-
-	{
-		reiser4_super_info_data *sbinfo;
-
-		sbinfo = get_super_private(super);
-
-		spin_lock_reiser4_super(sbinfo);
-		sbinfo->blocks_free_committed += blocks_freed;
-		spin_unlock_reiser4_super(sbinfo);
-	}
+	atom_dset_deferred_apply(atom, apply_dset_to_commit_bmap, NULL, 0);
 
 	return 0;
 }
Signature · 1 line
2.1.3
#4 lore raw

PATCH v4 4/10 reiser4: txnmgr: free allocated but unneeded atom in atom_begin_and_assign_to_txnh().

In reply to Ivan Shapovalov

It is unclear why it hasn't been done before, as there was a corresponding notice, and such change apparently does not carry any side effects.

Do this to avoid duplicating code in the next commit.

---
 fs/reiser4/txnmgr.c | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)
fs/reiser4/txnmgr.c +5 -15
index d73ecb9..68070a9 100644
@@ -709,6 +709,9 @@ static int atom_begin_and_assign_to_txnh(txn_atom ** atom_alloc, txn_handle * tx
 			return RETERR(-ENOMEM);
 	}
 
+	atom = *atom_alloc;
+	*atom_alloc = NULL;
+
 	/* and, also, txnmgr spin lock should be taken before jnode and txnh
 	   locks. */
 	mgr = &get_super_private(reiser4_get_current_sb())->tmgr;
@@ -717,18 +720,14 @@ static int atom_begin_and_assign_to_txnh(txn_atom ** atom_alloc, txn_handle * tx
 
 	/* Check whether new atom still needed */
 	if (txnh->atom != NULL) {
-		/* NOTE-NIKITA probably it is rather better to free
-		 * atom_alloc here than thread it up to reiser4_try_capture() */
-
 		spin_unlock_txnh(txnh);
 		spin_unlock_txnmgr(mgr);
 
+		kmem_cache_free(_atom_slab, atom);
+
 		return -E_REPEAT;
 	}
 
-	atom = *atom_alloc;
-	*atom_alloc = NULL;
-
 	atom_init(atom);
 
 	assert("jmacd-17", atom_isclean(atom));
@@ -2031,15 +2030,6 @@ int reiser4_try_capture(jnode *node, znode_lock_mode lock_mode,
 		goto repeat;
 	}
 
-	/* free extra atom object that was possibly allocated by
-	   try_capture_block().
-
-	   Do this before acquiring jnode spin lock to
-	   minimize time spent under lock. --nikita */
-	if (atom_alloc != NULL) {
-		kmem_cache_free(_atom_slab, atom_alloc);
-	}
-
 	if (ret != 0) {
 		if (ret == -E_BLOCK) {
 			assert("nikita-3360",
Signature · 1 line
2.1.3
#5 lore raw

PATCH v4 5/10 reiser4: txnmgr: add reiser4_create_atom() which creates an empty atom without capturing any nodes.

In reply to Ivan Shapovalov
---
 fs/reiser4/txnmgr.c | 24 ++++++++++++++++++++++++
 fs/reiser4/txnmgr.h |  2 ++
 2 files changed, 26 insertions(+)
fs/reiser4/txnmgr.c +24 -0
index 68070a9..2862940 100644
@@ -2050,6 +2050,30 @@ int reiser4_try_capture(jnode *node, znode_lock_mode lock_mode,
 	return ret;
 }
 
+/* This function ensures that the current transcrash has an atom without
+ * capturing anything. If needed, an empty atom is created and assigned.
+ */
+int reiser4_create_atom(void)
+{
+	txn_atom *atom_alloc = NULL;
+	txn_handle *txnh = get_current_context()->trans;
+	int ret;
+
+	do {
+		spin_lock_txnh(txnh);
+		if (txnh->atom == NULL) {
+			spin_unlock_txnh(txnh);
+			/* assign empty atom to the txnh and repeat */
+			ret = atom_begin_and_assign_to_txnh(&atom_alloc, txnh);
+		} else {
+			spin_unlock_txnh(txnh);
+			ret = 0;
+		}
+	} while (ret == -E_REPEAT);
+
+	return ret;
+}
+
 static void release_two_atoms(txn_atom *one, txn_atom *two)
 {
 	spin_unlock_atom(one);
fs/reiser4/txnmgr.h +2 -0
index 72b84a2..50a1f54 100644
@@ -437,6 +437,8 @@ extern void reiser4_uncapture_jnode(jnode *);
 extern int reiser4_capture_inode(struct inode *);
 extern int reiser4_uncapture_inode(struct inode *);
 
+extern int reiser4_create_atom(void);
+
 extern txn_atom *get_current_atom_locked_nocheck(void);
 
 #if REISER4_DEBUG
Signature · 1 line
2.1.3
#6 lore raw

PATCH v4 6/10 reiser4: txnmgr: move "empty atom" shortcut slightly below.

In reply to Ivan Shapovalov

reiser4_trim_fs() generate empty (atom->capture_count == 0) atoms which still have non-empty delete sets. That is, such atoms take a "goto done" shortcut in commit_current_atom().

First, this shortcut must not preclude calling current_atom_complete_writes() to process and detach atom's flush queues (otherwise atom_isclean() will return false).

Second, reiser4_post_write_back_hook() needs to be called explicitly to process atom's delete sets.

---
 fs/reiser4/txnmgr.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/reiser4/txnmgr.c +9 -4
index 2862940..5084722 100644
@@ -1064,9 +1064,6 @@ static int commit_current_atom(long *nr_submitted, txn_atom ** atom)
 		return RETERR(-E_REPEAT);
 	}
 
-	if ((*atom)->capture_count == 0)
-		goto done;
-
 	/* Up to this point we have been flushing and after flush is called we
 	   return -E_REPEAT.  Now we can commit.  We cannot return -E_REPEAT
 	   at this point, commit should be successful. */
@@ -1078,6 +1075,14 @@ static int commit_current_atom(long *nr_submitted, txn_atom ** atom)
 	if (ret)
 		return ret;
 
+	if ((*atom)->capture_count == 0) {
+		/* Process the atom's delete set.
+		 * Even if the atom has no captured nodes, the delete set may
+		 * still be non-empty (see e. g. reiser4_trim_fs()). */
+		reiser4_post_write_back_hook();
+		goto done;
+	}
+
 	assert("zam-906", list_empty(ATOM_WB_LIST(*atom)));
 
 	/* isolate critical code path which should be executed by only one
@@ -1100,8 +1105,8 @@ static int commit_current_atom(long *nr_submitted, txn_atom ** atom)
 	reiser4_invalidate_list(ATOM_WB_LIST(*atom));
 	assert("zam-927", list_empty(&(*atom)->inodes));
 
+done:
 	spin_lock_atom(*atom);
- done:
 	reiser4_atom_set_stage(*atom, ASTAGE_DONE);
 	ON_DEBUG((*atom)->committer = NULL);
 
Signature · 1 line
2.1.3
#7 lore raw

PATCH v4 7/10 reiser4: batch discard support: add a dummy FITRIM ioctl handler for directories.

In reply to Ivan Shapovalov
---
 fs/reiser4/plugin/dir/dir.h  |  2 ++
 fs/reiser4/plugin/file_ops.c | 27 +++++++++++++++++++++++++++
 fs/reiser4/plugin/object.c   |  6 +++++-
 3 files changed, 34 insertions(+), 1 deletion(-)
fs/reiser4/plugin/dir/dir.h +2 -0
index 4a91ebe..5eb7bda 100644
@@ -10,6 +10,8 @@
 
 #include <linux/fs.h>*/
 
+long reiser4_ioctl_dir_common(struct file *file, unsigned int cmd, unsigned long arg);
+
 /* declarations of functions implementing HASHED_DIR_PLUGIN_ID dir plugin */
 
 /* "hashed" directory methods of dir plugin */
fs/reiser4/plugin/file_ops.c +27 -0
index 466da64..65e2b02 100644
@@ -107,6 +107,33 @@ int reiser4_sync_file_common(struct file *file, loff_t start, loff_t end, int da
 	return 0;
 }
 
+/** reiser4_ioctl_dir_common - ioctl of struct file_operations for typical directory
+ */
+long reiser4_ioctl_dir_common(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct inode *inode = file_inode(file);
+	struct super_block *super = inode->i_sb;
+	reiser4_context *ctx;
+	int ret;
+
+	ctx = reiser4_init_context(super);
+	if (IS_ERR(ctx))
+		return PTR_ERR(ctx);
+
+	switch (cmd) {
+	case FITRIM:
+		warning("intelfx-62", "FITRIM ioctl not implemented");
+		/* fall-through to -ENOSYS */
+
+	default:
+		ret = RETERR(-ENOSYS);
+		break;
+	}
+
+	reiser4_exit_context(ctx);
+	return ret;
+}
+
 /*
  * Local variables:
  * c-indentation-style: "K&R"
fs/reiser4/plugin/object.c +5 -1
index 553f1e2..ce09a8a 100644
@@ -251,7 +251,11 @@ static struct file_operations directory_f_ops = {
 	.read = generic_read_dir,
 	.iterate = reiser4_iterate_common,
 	.release = reiser4_release_dir_common,
-	.fsync = reiser4_sync_common
+	.fsync = reiser4_sync_common,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl = reiser4_ioctl_dir_common,
+#endif
+	.unlocked_ioctl = reiser4_ioctl_dir_common
 };
 static struct address_space_operations directory_a_ops = {
 	.writepage = writepage_bugop,
Signature · 1 line
2.1.3
#8 lore raw

PATCH v4 8/10 reiser4: batch discard support: actually implement the FITRIM ioctl handler.

In reply to Ivan Shapovalov

It works in an iterative way, grabbing some fixed amount of space and allocating blocks until grabbed space is exhausted or partition's end is reached.

Blocks are scheduled for discard by allocating them and immediately adding them to the atom's delete set, which is read and processed at atom commit time.

After reaching the allocation stop condition, the atom is force-committed and, if the allocation has been stopped due to grabbed space exhaustion (not due to reaching end of partition), another iteration happens.

---
 fs/reiser4/plugin/file_ops.c |  41 ++++++++++++-
 fs/reiser4/super_ops.c       | 135 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 173 insertions(+), 3 deletions(-)
fs/reiser4/plugin/file_ops.c +38 -3
index 65e2b02..7686406 100644
@@ -8,6 +8,9 @@
 #include "../inode.h"
 #include "object.h"
 
+#include <linux/fs.h>
+#include <linux/blkdev.h>
+
 /* file operations */
 
 /* implementation of vfs's llseek method of struct file_operations for
@@ -20,6 +23,9 @@ loff_t reiser4_llseek_dir_common(struct file *, loff_t, int origin);
 */
 int reiser4_iterate_common(struct file *, struct dir_context *);
 
+/* this function is implemented in super_ops.c */
+int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range);
+
 /**
  * reiser4_release_dir_common - release of struct file_operations
  * @inode: inode of released file
@@ -121,9 +127,38 @@ long reiser4_ioctl_dir_common(struct file *file, unsigned int cmd, unsigned long
 		return PTR_ERR(ctx);
 
 	switch (cmd) {
-	case FITRIM:
-		warning("intelfx-62", "FITRIM ioctl not implemented");
-		/* fall-through to -ENOSYS */
+	case FITRIM: {
+		struct request_queue *q = bdev_get_queue(super->s_bdev);
+		struct fstrim_range range;
+
+		if (!capable(CAP_SYS_ADMIN)) {
+			ret = RETERR(-EPERM);
+			break;
+		}
+
+		if (!blk_queue_discard(q)) {
+			ret = RETERR(-EOPNOTSUPP);
+			break;
+		}
+
+		if (copy_from_user(&range, (struct fstrim_range __user *)arg,
+		    sizeof(range))) {
+			ret = RETERR(-EFAULT);
+			break;
+		}
+
+		range.minlen = max((unsigned int)range.minlen,
+				   q->limits.discard_granularity);
+
+		ret = reiser4_trim_fs(super, &range);
+
+		if (copy_to_user((struct fstrim_range __user *)arg, &range,
+		    sizeof(range))) {
+			ret = RETERR(-EFAULT);
+		}
+
+		break;
+	}
 
 	default:
 		ret = RETERR(-ENOSYS);
fs/reiser4/super_ops.c +135 -0

141 lines not on this page · raw message

Signature · 1 line
2.1.3
#9 lore raw

PATCH v4 9/10 reiser4: block_alloc: add a "min_len" parameter to reiser4_blocknr_hint to limit allocated extent length from below.

In reply to Ivan Shapovalov

The default has been 1, that is, the first encountered free extent with any length from 1 to requested is allocated and returned.

So, allow changing minimal extent length to allocate.

---
 fs/reiser4/block_alloc.h         |  2 ++
 fs/reiser4/plugin/space/bitmap.c | 20 +++++++++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)
fs/reiser4/block_alloc.h +2 -0
index 903bc8f..ac1a747 100644
@@ -45,6 +45,8 @@ struct reiser4_blocknr_hint {
 	reiser4_block_nr blk;
 	/* if not zero, it is a region size we search for free blocks in */
 	reiser4_block_nr max_dist;
+	/* if not zero, minimal length of an extent to allocate */
+	reiser4_block_nr min_len;
 	/* level for allocation, may be useful have branch-level and higher
 	   write-optimized. */
 	tree_level level;
fs/reiser4/plugin/space/bitmap.c +15 -5
index 0ce07da..7c0a1e4 100644
@@ -1101,7 +1101,7 @@ static int alloc_blocks_forward(reiser4_blocknr_hint *hint, int needed,
 				reiser4_block_nr *start, reiser4_block_nr *len)
 {
 	struct super_block *super = get_current_context()->super;
-	int actual_len;
+	int min_len, actual_len;
 
 	reiser4_block_nr search_start;
 	reiser4_block_nr search_end;
@@ -1117,12 +1117,17 @@ static int alloc_blocks_forward(reiser4_blocknr_hint *hint, int needed,
 		    LIMIT(hint->blk + hint->max_dist,
 			  reiser4_block_count(super));
 
+	if (hint->min_len == 0)
+		min_len = 1;
+	else
+		min_len = (int)hint->min_len;
+
 	/* We use @hint -> blk as a search start and search from it to the end
 	   of the disk or in given region if @hint -> max_dist is not zero */
 	search_start = hint->blk;
 
 	actual_len =
-	    bitmap_alloc_forward(&search_start, &search_end, 1, needed);
+	    bitmap_alloc_forward(&search_start, &search_end, min_len, needed);
 
 	/* There is only one bitmap search if max_dist was specified, first
 	   pass was from the beginning of the bitmap, or if the monotonic flag
@@ -1134,7 +1139,7 @@ static int alloc_blocks_forward(reiser4_blocknr_hint *hint, int needed,
 		search_end = search_start;
 		search_start = 0;
 		actual_len =
-		    bitmap_alloc_forward(&search_start, &search_end, 1, needed);
+		    bitmap_alloc_forward(&search_start, &search_end, min_len, needed);
 	}
 	if (actual_len == 0)
 		return RETERR(-ENOSPC);
@@ -1151,7 +1156,7 @@ static int alloc_blocks_backward(reiser4_blocknr_hint * hint, int needed,
 {
 	reiser4_block_nr search_start;
 	reiser4_block_nr search_end;
-	int actual_len;
+	int min_len, actual_len;
 
 	ON_DEBUG(struct super_block *super = reiser4_get_current_sb());
 
@@ -1165,8 +1170,13 @@ static int alloc_blocks_backward(reiser4_blocknr_hint * hint, int needed,
 	else
 		search_end = search_start - hint->max_dist;
 
+	if (hint->min_len == 0)
+		min_len = 1;
+	else
+		min_len = (int)hint->min_len;
+
 	actual_len =
-	    bitmap_alloc_backward(&search_start, &search_end, 1, needed);
+	    bitmap_alloc_backward(&search_start, &search_end, min_len, needed);
 	if (actual_len == 0)
 		return RETERR(-ENOSPC);
 	if (actual_len < 0)
Signature · 1 line
2.1.3
#10 lore raw

PATCH v4 10/10 reiser4: batch discard support: honor minimal extent length passed from the userspace.

In reply to Ivan Shapovalov
---
 fs/reiser4/super_ops.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/reiser4/super_ops.c +2 -3
index ab32ce4..2c98959 100644
@@ -487,7 +487,7 @@ static int reiser4_show_options(struct seq_file *m, struct dentry *dentry)
 int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range)
 {
 	reiser4_blocknr_hint hint;
-	reiser4_block_nr start, end, len, minlen, discarded_count = 0;
+	reiser4_block_nr start, end, len, discarded_count = 0;
 	reiser4_context *ctx;
 	txn_atom *atom;
 	int ret, finished = 0;
@@ -501,11 +501,11 @@ int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range)
 	 */
 	hint.blk = range->start >> super->s_blocksize_bits;
 	hint.max_dist = range->len >> super->s_blocksize_bits;
+	hint.min_len = range->minlen >> super->s_blocksize_bits;
 	hint.block_stage = BLOCK_GRABBED;
 	hint.monotonic_forward = 1;
 
 	end = hint.blk + hint.max_dist;
-	minlen = range->minlen >> super->s_blocksize_bits;
 
 	/*
 	 * We will perform the process in iterations in order not to starve
@@ -538,7 +538,6 @@ int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range)
 		do {
 			/*
 			 * Allocate no more than is grabbed.
-			 * FIXME: use minlen.
 			 *
 			 * NOTE: we do not use BA_PERMANENT in our allocations
 			 * even though we deallocate with BA_DEFER.
Signature · 1 line
2.1.3
#11 lore raw
In reply to Ivan Shapovalov
Quoted 14 lines · Ivan Shapovalov

[...]

Ivan Shapovalov (10):
reiser4: block_alloc: add BA_SOME_SPACE flag for grabbing a fixed amount of space.
reiser4: block_alloc: add a "monotonic_forward" parameter to reiser4_blocknr_hint to allocate blocks only in forward direction.
reiser4: block_alloc: move block accounting by pre-commit hook into block_alloc.c and document BA_DEFER behavior.
reiser4: txnmgr: free allocated but unneeded atom in atom_begin_and_assign_to_txnh().
reiser4: txnmgr: add reiser4_create_atom() which creates an empty atom without capturing any nodes.
reiser4: txnmgr: move "empty atom" shortcut slightly below.
reiser4: batch discard support: add a dummy FITRIM ioctl handler for directories.
reiser4: batch discard support: actually implement the FITRIM ioctl handler.
reiser4: block_alloc: add a "min_len" parameter to reiser4_blocknr_hint to limit allocated extent length from below.
reiser4: batch discard support: honor minimal extent length passed from the userspace.

Quoted 1 line

[...]

This is a diff between v3 and v4, for ease of reviewing.

fs/reiser4/block_alloc.c +59 -8
index 1632f78..549cc0a 100644
@@ -264,7 +264,7 @@ int reiser4_check_block_counters(const struct super_block *super)
 static int
 reiser4_grab(reiser4_context * ctx, __u64 count, reiser4_ba_flags_t flags)
 {
-	__u64 free_blocks;
+	__u64 allowed_blocks;
 	int ret = 0, use_reserved = flags & BA_RESERVED;
 	reiser4_super_info_data *sbinfo;
 
@@ -280,15 +280,24 @@ reiser4_grab(reiser4_context * ctx, __u64 count, reiser4_ba_flags_t flags)
 
 	spin_lock_reiser4_super(sbinfo);
 
-	free_blocks = sbinfo->blocks_free;
+	allowed_blocks = use_reserved ? sbinfo->blocks_free
+	                              : sbinfo->blocks_free - sbinfo->blocks_reserved;
 
 	if (flags & BA_SOME_SPACE) {
-		/* Reserve 25% of all free space. */
-		count = free_blocks >> 2;
-	}
+		/* Reserve 25% of all free space */
+		if (allowed_blocks == 0) {
+			/* No space at all */
+			ret = RETERR(-ENOSPC);
+			goto unlock_and_ret;
+		}
 
-	if ((use_reserved && free_blocks < count) ||
-	    (!use_reserved && free_blocks < count + sbinfo->blocks_reserved)) {
+		count = allowed_blocks >> 2;
+		if (count == 0) {
+			/* Less than 4 free blocks */
+			count = allowed_blocks;
+		}
+	} else if (count > allowed_blocks) {
+		/* Not enough space */
 		ret = RETERR(-ENOSPC);
 		goto unlock_and_ret;
 	}
@@ -989,6 +998,19 @@ reiser4_check_blocks(const reiser4_block_nr * start,
 /* BA_FORMATTED bit is only used when BA_DEFER in not present: it is used to
    distinguish blocks allocated for unformatted and formatted nodes */
 
+/* if BA_DEFER is enabled, @target_stage and other @flags are ignored.
+ *
+ * @target_stage is implied to be BLOCK_NOT_COUNTED.
+ * (assumption is used in reiser4_post_write_back_hook() and apply_dset())
+ *
+ * @flags are implied to have BA_PERMANENT.
+ * (assumption is used in reiser4_pre_commit_hook() which counts deallocated
+ *  blocks)
+ * 
+ * That is, if a deferred deallocation is done after reiser4_pre_commit_hook(),
+ * then BA_PERMANENT is implied to be disabled.
+ */
+
 int
 reiser4_dealloc_blocks(const reiser4_block_nr * start,
 		       const reiser4_block_nr * len,
@@ -1080,10 +1102,39 @@ reiser4_dealloc_blocks(const reiser4_block_nr * start,
 	return 0;
 }
 
+/* an actor for counting blocks that are going to be deallocated */
+static int
+count_dset_blocks(txn_atom * atom, const reiser4_block_nr * start,
+                  const reiser4_block_nr * len, void *data)
+{
+	reiser4_block_nr *blocks_freed_p = data;
+
+	if (len != NULL) {
+		(*blocks_freed_p) += *len;
+	} else {
+		(*blocks_freed_p)++;
+	}
+	return 0;
+}
+
 /* wrappers for block allocator plugin methods */
 int reiser4_pre_commit_hook(void)
 {
-	assert("zam-502", get_current_super_private() != NULL);
+	reiser4_block_nr blocks_freed = 0;
+	reiser4_super_info_data *sbinfo = get_current_super_private();
+	txn_atom *atom = get_current_atom_locked();
+
+	assert("zam-502", sbinfo != NULL);
+
+	assert("zam-876", atom->stage == ASTAGE_PRE_COMMIT);
+	spin_unlock_atom(atom);
+
+	atom_dset_deferred_apply(atom, count_dset_blocks, &blocks_freed, 0);
+
+	spin_lock_reiser4_super(sbinfo);
+	sbinfo->blocks_free_committed += blocks_freed - atom->nr_blocks_allocated;
+	spin_unlock_reiser4_super(sbinfo);
+
 	sa_pre_commit_hook();
 	return 0;
 }
fs/reiser4/block_alloc.h +3 -3
index 16f9810..ac1a747 100644
@@ -53,10 +53,10 @@ struct reiser4_blocknr_hint {
 	/* block allocator assumes that blocks, which will be mapped to disk,
 	   are in this specified block_stage */
 	block_stage_t block_stage;
-	/* Allocate blocks only in backward direction starting from blk. */
+	/* allocate blocks in backward direction */
 	unsigned int backward:1;
-	/* Allocate blocks only in forward direction starting from blk. */
-	unsigned int forward:1;
+	/* allocate blocks only in forward direction starting from blk */
+	unsigned int monotonic_forward:1;
 
 };
 
fs/reiser4/plugin/space/bitmap.c +8 -26
index 8309cf9..7c0a1e4 100644
@@ -1129,16 +1129,17 @@ static int alloc_blocks_forward(reiser4_blocknr_hint *hint, int needed,
 	actual_len =
 	    bitmap_alloc_forward(&search_start, &search_end, min_len, needed);
 
-	/* There is only one bitmap search if max_dist was specified or first
-	   pass was from the beginning of the bitmap. We also do one pass for
-	   scanning bitmap in backward direction. */
+	/* There is only one bitmap search if max_dist was specified, first
+	   pass was from the beginning of the bitmap, or if the monotonic flag
+	   has been set. Otherwise we also do one pass for scanning bitmap in
+	   backward direction. */
 	if (actual_len == 0 && search_start != 0 &&
-	    hint->max_dist == 0 && hint->forward == 0) {
+	    hint->max_dist == 0 && !hint->monotonic_forward) {
 		/* next step is a scanning from 0 to search_start */
 		search_end = search_start;
 		search_start = 0;
 		actual_len =
-		    bitmap_alloc_forward(&search_start, &search_end, 1, needed);
+		    bitmap_alloc_forward(&search_start, &search_end, min_len, needed);
 	}
 	if (actual_len == 0)
 		return RETERR(-ENOSPC);
@@ -1331,15 +1332,13 @@ static void cond_add_to_overwrite_set(txn_atom * atom, jnode * node)
    pages in a single-linked list */
 static int
 apply_dset_to_commit_bmap(txn_atom * atom, const reiser4_block_nr * start,
-			  const reiser4_block_nr * len, void *data)
+			  const reiser4_block_nr * len, void *data UNUSED_ARG)
 {
 
 	bmap_nr_t bmap;
 	bmap_off_t offset;
 	int ret;
 
-	long long *blocks_freed_p = data;
-
 	struct bitmap_node *bnode;
 
 	struct super_block *sb = reiser4_get_current_sb();
@@ -1375,11 +1374,8 @@ apply_dset_to_commit_bmap(txn_atom * atom, const reiser4_block_nr * start,
 		assert("zam-443",
 		       offset + *len <= bmap_bit_count(sb->s_blocksize));
 		reiser4_clear_bits(data, offset, (bmap_off_t) (offset + *len));
-
-		(*blocks_freed_p) += *len;
 	} else {
 		reiser4_clear_bit(offset, data);
-		(*blocks_freed_p)++;
 	}
 
 	bnode_set_commit_crc(bnode, bnode_calc_crc(bnode, sb->s_blocksize));
@@ -1402,8 +1398,6 @@ int reiser4_pre_commit_hook_bitmap(void)
 	struct super_block *super = reiser4_get_current_sb();
 	txn_atom *atom;
 
-	long long blocks_freed = 0;
-
 	atom = get_current_atom_locked();
 	assert("zam-876", atom->stage == ASTAGE_PRE_COMMIT);
 	spin_unlock_atom(atom);
@@ -1469,19 +1463,7 @@ int reiser4_pre_commit_hook_bitmap(void)
 		}
 	}
 
-	atom_dset_deferred_apply(atom, apply_dset_to_commit_bmap, &blocks_freed, 0);
-
-	blocks_freed -= atom->nr_blocks_allocated;
-
-	{
-		reiser4_super_info_data *sbinfo;
-
-		sbinfo = get_super_private(super);
-
-		spin_lock_reiser4_super(sbinfo);
-		sbinfo->blocks_free_committed += blocks_freed;
-		spin_unlock_reiser4_super(sbinfo);
-	}
+	atom_dset_deferred_apply(atom, apply_dset_to_commit_bmap, NULL, 0);
 
 	return 0;
 }
fs/reiser4/super_ops.c +20 -12
index a98b192..2c98959 100644
@@ -503,7 +503,7 @@ int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range)
 	hint.max_dist = range->len >> super->s_blocksize_bits;
 	hint.min_len = range->minlen >> super->s_blocksize_bits;
 	hint.block_stage = BLOCK_GRABBED;
-	hint.forward = 1;
+	hint.monotonic_forward = 1;
 
 	end = hint.blk + hint.max_dist;
 
@@ -535,14 +535,27 @@ int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range)
 		if (ret != 0)
 			goto out;
 
-		while (ctx->grabbed_blocks != 0) {
+		do {
 			/*
 			 * Allocate no more than is grabbed.
+			 *
+			 * NOTE: we do not use BA_PERMANENT in our allocations
+			 * even though we deallocate with BA_DEFER.
+			 * Our atom takes the "shortcut" in commit_current_atom()
+			 * and as such the pre-commit hook is not applied.
 			 */
+			assert("intelfx-75", ctx->grabbed_blocks != 0);
 			len = ctx->grabbed_blocks;
 			ret = reiser4_alloc_blocks(&hint, &start, &len, 0 /* flags */);
-			if (ret == -ENOSPC)
+			if (ret == -ENOSPC) {
+				/*
+				 * We have reached the end of the filesystem --
+				 * no more blocks to discard.
+				 */
+				ret = 0;
+				finished = 1;
 				break;
+			}
 			if (ret != 0)
 				goto out;
 
@@ -557,7 +570,7 @@ int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range)
 			 * Mark the newly allocated extent for deallocation.
 			 * Discard happens on deallocation.
 			 */
-			ret = reiser4_dealloc_blocks(&start, &len, BLOCK_NOT_COUNTED, BA_DEFER);
+			ret = reiser4_dealloc_blocks(&start, &len, 0, BA_DEFER);
 			if (ret != 0)
 				goto out;
 
@@ -571,23 +584,18 @@ int reiser4_trim_fs(struct super_block *super, struct fstrim_range* range)
 			 * space.
 			 */
 			discarded_count += len;
-		}
+		} while (ctx->grabbed_blocks != 0);
 
-		assert("intelfx-64", (ret == 0) || (ret == -ENOSPC));
-
-		if (ret == -ENOSPC) {
-			ret = 0;
-			finished = 1;
-		}
+		assert("intelfx-64", ret == 0);
 
 		/*
 		 * Extents marked for deallocation are discarded here, as part
 		 * of committing current atom.
 		 */
+		all_grabbed2free();
 		atom = get_current_atom_locked();
 		spin_lock_txnh(ctx->trans);
 		force_commit_atom(ctx->trans);
-		all_grabbed2free();
 		grab_space_enable();
 	} while (!finished);
 
fs/reiser4/txnmgr.c +4 -4
index 2efdf85..5084722 100644
@@ -1071,6 +1071,10 @@ static int commit_current_atom(long *nr_submitted, txn_atom ** atom)
 	ON_DEBUG(((*atom)->committer = current));
 	spin_unlock_atom(*atom);
 
+	ret = current_atom_complete_writes();
+	if (ret)
+		return ret;
+
 	if ((*atom)->capture_count == 0) {
 		/* Process the atom's delete set.
 		 * Even if the atom has no captured nodes, the delete set may
@@ -1079,10 +1083,6 @@ static int commit_current_atom(long *nr_submitted, txn_atom ** atom)
 		goto done;
 	}
 
-	ret = current_atom_complete_writes();
-	if (ret)
-		return ret;
-
 	assert("zam-906", list_empty(ATOM_WB_LIST(*atom)));
 
 	/* isolate critical code path which should be executed by only one

Thanks,

Signature · 1 line
Ivan Shapovalov / intelfx /