All of lore.kernel.org
 help / color / mirror / Atom feed
From: Russell King <rmk+kernel@armlinux.org.uk>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org
Subject: [PATCH 15/41] fs/adfs: dir: add generic copy functions
Date: Mon, 09 Dec 2019 11:09:30 +0000	[thread overview]
Message-ID: <E1ieGuw-0004bH-Gq@rmk-PC.armlinux.org.uk> (raw)
In-Reply-To: <20191209110731.GD25745@shell.armlinux.org.uk>

Directories can span multiple buffers, and we currently open-code
memcpy access to these buffers, including dealing with entries that
are split across multiple buffers.  Such code exists in both
directory format implementations.

Provide common functions to allow data to be copied from/to the
directory buffers as if they were a contiguous set of buffers, and
use them when accessing directories.

Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
---
 fs/adfs/adfs.h      |  4 ++++
 fs/adfs/dir.c       | 50 ++++++++++++++++++++++++++++++++++++++++
 fs/adfs/dir_f.c     | 56 ++++++++-------------------------------------
 fs/adfs/dir_fplus.c | 47 ++++++++++---------------------------
 4 files changed, 75 insertions(+), 82 deletions(-)

diff --git a/fs/adfs/adfs.h b/fs/adfs/adfs.h
index 5f1acee768f5..92cbc4b1d902 100644
--- a/fs/adfs/adfs.h
+++ b/fs/adfs/adfs.h
@@ -165,6 +165,10 @@ extern const struct dentry_operations adfs_dentry_operations;
 extern const struct adfs_dir_ops adfs_f_dir_ops;
 extern const struct adfs_dir_ops adfs_fplus_dir_ops;
 
+int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset,
+		      size_t len);
+int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src,
+		    size_t len);
 void adfs_dir_relse(struct adfs_dir *dir);
 void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj);
 extern int adfs_dir_update(struct super_block *sb, struct object_info *obj,
diff --git a/fs/adfs/dir.c b/fs/adfs/dir.c
index 16a2639d3ca5..3c303074aa5e 100644
--- a/fs/adfs/dir.c
+++ b/fs/adfs/dir.c
@@ -14,6 +14,56 @@
  */
 static DEFINE_RWLOCK(adfs_dir_lock);
 
+int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset,
+		      size_t len)
+{
+	struct super_block *sb = dir->sb;
+	unsigned int index, remain;
+
+	index = offset >> sb->s_blocksize_bits;
+	offset &= sb->s_blocksize - 1;
+	remain = sb->s_blocksize - offset;
+	if (index + (remain < len) >= dir->nr_buffers)
+		return -EINVAL;
+
+	if (remain < len) {
+		memcpy(dst, dir->bhs[index]->b_data + offset, remain);
+		dst += remain;
+		len -= remain;
+		index += 1;
+		offset = 0;
+	}
+
+	memcpy(dst, dir->bhs[index]->b_data + offset, len);
+
+	return 0;
+}
+
+int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src,
+		    size_t len)
+{
+	struct super_block *sb = dir->sb;
+	unsigned int index, remain;
+
+	index = offset >> sb->s_blocksize_bits;
+	offset &= sb->s_blocksize - 1;
+	remain = sb->s_blocksize - offset;
+	if (index + (remain < len) >= dir->nr_buffers)
+		return -EINVAL;
+
+	if (remain < len) {
+		memcpy(dir->bhs[index]->b_data + offset, src, remain);
+		src += remain;
+		len -= remain;
+		index += 1;
+		offset = 0;
+	}
+
+	memcpy(dir->bhs[index]->b_data + offset, src, len);
+
+	return 0;
+}
+
 void adfs_dir_relse(struct adfs_dir *dir)
 {
 	unsigned int i;
diff --git a/fs/adfs/dir_f.c b/fs/adfs/dir_f.c
index 80ac261b9ec4..3c3b423577d2 100644
--- a/fs/adfs/dir_f.c
+++ b/fs/adfs/dir_f.c
@@ -224,24 +224,12 @@ adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
 static int
 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
 {
-	struct super_block *sb = dir->sb;
 	struct adfs_direntry de;
-	int thissize, buffer, offset;
-
-	buffer = pos >> sb->s_blocksize_bits;
-
-	if (buffer > dir->nr_buffers)
-		return -EINVAL;
-
-	offset = pos & (sb->s_blocksize - 1);
-	thissize = sb->s_blocksize - offset;
-	if (thissize > 26)
-		thissize = 26;
+	int ret;
 
-	memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
-	if (thissize != 26)
-		memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
-		       26 - thissize);
+	ret = adfs_dir_copyfrom(&de, dir, pos, 26);
+	if (ret)
+		return ret;
 
 	if (!de.dirobname[0])
 		return -ENOENT;
@@ -254,42 +242,16 @@ __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
 static int
 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
 {
-	struct super_block *sb = dir->sb;
 	struct adfs_direntry de;
-	int thissize, buffer, offset;
-
-	buffer = pos >> sb->s_blocksize_bits;
-
-	if (buffer > dir->nr_buffers)
-		return -EINVAL;
-
-	offset = pos & (sb->s_blocksize - 1);
-	thissize = sb->s_blocksize - offset;
-	if (thissize > 26)
-		thissize = 26;
+	int ret;
 
-	/*
-	 * Get the entry in total
-	 */
-	memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
-	if (thissize != 26)
-		memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
-		       26 - thissize);
+	ret = adfs_dir_copyfrom(&de, dir, pos, 26);
+	if (ret)
+		return ret;
 
-	/*
-	 * update it
-	 */
 	adfs_obj2dir(&de, obj);
 
-	/*
-	 * Put the new entry back
-	 */
-	memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
-	if (thissize != 26)
-		memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
-		       26 - thissize);
-
-	return 0;
+	return adfs_dir_copyto(dir, pos, &de, 26);
 }
 
 /*
diff --git a/fs/adfs/dir_fplus.c b/fs/adfs/dir_fplus.c
index 1196c8962feb..6a07c0dfcc93 100644
--- a/fs/adfs/dir_fplus.c
+++ b/fs/adfs/dir_fplus.c
@@ -112,34 +112,6 @@ adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos)
 	return ret;
 }
 
-static void
-dir_memcpy(struct adfs_dir *dir, unsigned int offset, void *to, int len)
-{
-	struct super_block *sb = dir->sb;
-	unsigned int buffer, partial, remainder;
-
-	buffer = offset >> sb->s_blocksize_bits;
-	offset &= sb->s_blocksize - 1;
-
-	partial = sb->s_blocksize - offset;
-
-	if (partial >= len)
-		memcpy(to, dir->bhs[buffer]->b_data + offset, len);
-	else {
-		char *c = (char *)to;
-
-		remainder = len - partial;
-
-		memcpy(c,
-			dir->bhs[buffer]->b_data + offset,
-			partial);
-
-		memcpy(c + partial,
-			dir->bhs[buffer + 1]->b_data,
-			remainder);
-	}
-}
-
 static int
 adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
 {
@@ -147,16 +119,19 @@ adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
 		(struct adfs_bigdirheader *) dir->bhs[0]->b_data;
 	struct adfs_bigdirentry bde;
 	unsigned int offset;
-	int ret = -ENOENT;
+	int ret;
 
 	if (dir->pos >= le32_to_cpu(h->bigdirentries))
-		goto out;
+		return -ENOENT;
 
 	offset = offsetof(struct adfs_bigdirheader, bigdirname);
 	offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
 	offset += dir->pos * sizeof(struct adfs_bigdirentry);
 
-	dir_memcpy(dir, offset, &bde, sizeof(struct adfs_bigdirentry));
+	ret = adfs_dir_copyfrom(&bde, dir, offset,
+				sizeof(struct adfs_bigdirentry));
+	if (ret)
+		return ret;
 
 	obj->loadaddr = le32_to_cpu(bde.bigdirload);
 	obj->execaddr = le32_to_cpu(bde.bigdirexec);
@@ -170,13 +145,15 @@ adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
 	offset += le32_to_cpu(h->bigdirentries) * sizeof(struct adfs_bigdirentry);
 	offset += le32_to_cpu(bde.bigdirobnameptr);
 
-	dir_memcpy(dir, offset, obj->name, obj->name_len);
+	ret = adfs_dir_copyfrom(obj->name, dir, offset, obj->name_len);
+	if (ret)
+		return ret;
+
 	adfs_object_fixup(dir, obj);
 
 	dir->pos += 1;
-	ret = 0;
-out:
-	return ret;
+
+	return 0;
 }
 
 const struct adfs_dir_ops adfs_fplus_dir_ops = {
-- 
2.20.1


  parent reply	other threads:[~2019-12-09 11:09 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-09 11:07 [PATCH 00/41] fs/adfs updates for 5.6 Russell King - ARM Linux admin
2019-12-09 11:08 ` [PATCH 01/41] fs/adfs: inode: update timestamps to centisecond precision Russell King
2019-12-09 13:54   ` Vyacheslav Dubeyko
2019-12-09 14:03     ` Russell King - ARM Linux admin
2019-12-09 14:34       ` Vyacheslav Dubeyko
2019-12-09 14:40         ` Russell King - ARM Linux admin
2019-12-09 17:15           ` Matthew Wilcox
2019-12-09 11:08 ` [PATCH 02/41] fs/adfs: inode: fix adfs_mode2atts() Russell King
2019-12-09 11:08 ` [PATCH 03/41] fs/adfs: map: move map reading and validation to map.c Russell King
2019-12-09 11:08 ` [PATCH 04/41] fs/adfs: map: rename adfs_map_free() to adfs_map_statfs() Russell King
2019-12-09 11:08 ` [PATCH 05/41] fs/adfs: map: break up adfs_read_map() Russell King
2019-12-09 11:08 ` [PATCH 06/41] fs/adfs: map: factor out map cleanup Russell King
2019-12-09 11:08 ` [PATCH 07/41] fs/adfs: map: incorporate map offsets into layout Russell King
2019-12-09 11:08 ` [PATCH 08/41] fs/adfs: map: use find_next_bit_le() rather than open coding it Russell King
2019-12-09 11:08 ` [PATCH 09/41] fs/adfs: map: move map-specific sb initialisation to map.c Russell King
2019-12-09 11:09 ` [PATCH 10/41] fs/adfs: map: fix map scanning Russell King
2019-12-09 11:09 ` [PATCH 11/41] fs/adfs: dir: rename bh_fplus to bhs Russell King
2019-12-09 11:09 ` [PATCH 12/41] fs/adfs: dir: add common dir object initialisation Russell King
2019-12-09 11:09 ` [PATCH 13/41] fs/adfs: dir: add common directory buffer release method Russell King
2019-12-09 11:09 ` [PATCH 14/41] fs/adfs: dir: add common directory sync method Russell King
2019-12-09 11:09 ` Russell King [this message]
2019-12-09 11:09 ` [PATCH 16/41] fs/adfs: dir: add generic directory reading Russell King
2019-12-09 11:09 ` [PATCH 17/41] fs/adfs: dir: add helper to read directory using inode Russell King
2019-12-09 11:09 ` [PATCH 18/41] fs/adfs: dir: add helper to mark directory buffers dirty Russell King
2019-12-09 11:09 ` [PATCH 19/41] fs/adfs: dir: update directory locking Russell King
2019-12-09 11:09 ` [PATCH 20/41] fs/adfs: dir: modernise on-disk directory structures Russell King
2019-12-09 11:10 ` [PATCH 21/41] fs/adfs: dir: improve update failure handling Russell King
2019-12-09 11:10 ` [PATCH 22/41] fs/adfs: dir: improve compiler coverage in adfs_dir_update Russell King
2019-12-09 11:10 ` [PATCH 23/41] fs/adfs: dir: switch to iterate_shared method Russell King
2019-12-09 11:10 ` [PATCH 24/41] fs/adfs: dir: add more efficient iterate() per-format method Russell King
2019-12-09 11:10 ` [PATCH 25/41] fs/adfs: dir: use pointers to access directory head/tails Russell King
2019-12-09 11:10 ` [PATCH 26/41] fs/adfs: newdir: factor out directory format validation Russell King
2019-12-09 11:10 ` [PATCH 27/41] fs/adfs: newdir: improve directory validation Russell King
2019-12-09 11:10 ` [PATCH 28/41] fs/adfs: newdir: merge adfs_dir_read() into adfs_f_read() Russell King
2019-12-09 11:10 ` [PATCH 29/41] fs/adfs: newdir: clean up adfs_f_update() Russell King
2019-12-09 11:10 ` [PATCH 30/41] fs/adfs: newdir: split out directory commit from update Russell King
2019-12-09 11:10 ` [PATCH 31/41] fs/adfs: bigdir: factor out directory entry offset calculation Russell King
2019-12-09 11:10 ` [PATCH 32/41] fs/adfs: bigdir: extract directory validation Russell King
2019-12-09 11:11 ` [PATCH 33/41] fs/adfs: bigdir: directory validation strengthening Russell King
2019-12-09 11:11 ` [PATCH 34/41] fs/adfs: bigdir: calculate and validate directory checkbyte Russell King
2019-12-09 11:11 ` [PATCH 35/41] fs/adfs: bigdir: implement directory update support Russell King
2019-12-09 11:11 ` [PATCH 36/41] fs/adfs: super: fix inode dropping Russell King
2019-12-09 11:11 ` [PATCH 37/41] fs/adfs: dir: remove debug in adfs_dir_update() Russell King
2019-12-09 11:11 ` [PATCH 38/41] fs/adfs: super: extract filesystem block probe Russell King
2019-12-09 11:11 ` [PATCH 39/41] fs/adfs: super: add support for E and E+ floppy image formats Russell King
2019-12-09 11:11 ` [PATCH 40/41] fs/adfs: mostly divorse inode number from indirect disc address Russell King
2019-12-09 11:11 ` [PATCH 41/41] Documentation: update adfs filesystem documentation Russell King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1ieGuw-0004bH-Gq@rmk-PC.armlinux.org.uk \
    --to=rmk+kernel@armlinux.org.uk \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.