Skip to contentSkip to conversation

fs: btrfs: Re-implement btrfs support using the more widely used extent buffer base code

PATCH Superseded Qu Wenruo 4 participants 43 messages Apr 22, 2020 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover fs: btrfs: Re-implement btrfs support using the more widely used extent buffer base code

The branch can be fetched from github:
https://github.com/adam900710/u-boot/tree/btrfs_rebuild

The current btrfs code in U-boot is using a creative way to read on-disk data.
It's pretty simple, involving the least amount of code, but pretty different from btrfs-progs nor kernel, making it pretty hard to sync code between different projects.

This big patchset will rework the btrfs support, to use code mostly from btrfs-progs, thus all existing btrfs developers will feel at home.

During the rework, the following new features are added:
- More hash algorithm support
SHA256 and XXHASH support are added.
BLAKE2 needs more backport, will happen in a separate patchset.

- The ability to read degraded RAID1
Although we still only support one device btrfs, the new code base can choose from different copies already.
As long as new device scan interface is provided, multi-device support is pretty simple.

- More correct handling of compressed extents with offset For compressed extent with offset, we should read the whole compressed extent, decompress them, then copy the referred part.

There are some more features incoming, with the new code base it would be much easier to implement:
- Data checksum support
The check would happen in read_extent_data(), btrfs-progs has the needed facility to locate data csum.

- BLAKE2 support
Need BLAKE2 library cross ported.
For btrfs it's just several lines of modification.

- Multi-device support along wit degraded RAID support We only need an interface to scan one device without opening it. The infrastructure is already provided in this patchset.

These new features would be submitted after the patchset get merged, since the patchset is already large, I don't want to make it more scary.

Although this patchset look horribly large, most of them are code copy from btrfs-progs.
E.g extent-cache.[ch], rbtree-utils.[ch], btrfs_btree.h. And ctree.h has over 1000 lines copied just for various accessors.

While for disk-io.[ch] and volumes-io.[ch], they have some small modifications to adapt the interface of U-boot.
E.g. btrfs_device::fd is replace with blkdev_desc and disk_partition_t.

The new code for U-boot are related to the following functions:
- btrfs_readlink()
- btrfs_lookup_path()
- btrfs_read_extent_inline()
- btrfs_read_extent_reg()
- lookup_data_extent()
- btrfs_file_read()

Thus only the following 5 patches need extra review attention:
- Patch 0017
- Patch 0018
- Patch 0022
- Patch 0023
- Patch 0024

Qu Wenruo (26):
fs: btrfs: Sync btrfs_btree.h from kernel
fs: btrfs: Add More checksum algorithm support to btrfs
fs: btrfs: Cross-port btrfs_read_dev_super() from btrfs-progs
fs: btrfs: Cross-port rbtree-utils from btrfs-progs
fs: btrfs: Cross-port extent-cache.[ch] from btrfs-progs
fs: btrfs: Cross-port extent-io.[ch] from btrfs-progs
fs: btrfs: Cross port structure accessor into ctree.h
fs: btrfs: Cross port volumes.[ch] from btrfs-progs
fs: btrfs: Crossport read_tree_block() from btrfs-progs
fs: btrfs: Rename struct btrfs_path to struct __btrfs_path
fs: btrfs: Rename btrfs_root to __btrfs_root
fs: btrfs: Cross port struct btrfs_root to ctree.h
fs: btrfs: Crossport btrfs_search_slot() from btrfs-progs
fs: btrfs: Crossport btrfs_read_sys_array() and
btrfs_read_chunk_tree()
fs: btrfs: Crossport open_ctree_fs_info()
fs: btrfs: Rename path resolve related functions to avoid name
conflicts
fs: btrfs: Use btrfs_readlink() to implement __btrfs_readlink()
fs: btrfs: Implement btrfs_lookup_path()
fs: btrfs: Use btrfs_iter_dir() to replace btrfs_readdir()
fs: btrfs: Use btrfs_lookup_path() to implement btrfs_exists() and
btrfs_size()
fs: btrfs: Rename btrfs_file_read() and its callees to avoid name
conflicts
fs: btrfs: Introduce btrfs_read_extent_inline() and
btrfs_read_extent_reg()
fs: btrfs: Introduce lookup_data_extent() for later use
fs: btrfs: Implement btrfs_file_read()
fs: btrfs: Cleanup the old implementation
MAINTAINERS: Add btrfs mail list

 MAINTAINERS                         |    1 +
 fs/btrfs/Makefile                   |    5 +-
 fs/btrfs/btrfs.c                    |  311 +++---
 fs/btrfs/btrfs.h                    |   67 +-
 fs/btrfs/btrfs_tree.h               |  766 --------------
 fs/btrfs/chunk-map.c                |  177 ----
 fs/btrfs/common/rbtree-utils.c      |   83 ++
 fs/btrfs/common/rbtree-utils.h      |   53 +
 fs/btrfs/compat.h                   |   80 ++
 fs/btrfs/compression.c              |    2 +-
 fs/btrfs/crypto/hash.c              |   55 +
 fs/btrfs/crypto/hash.h              |   17 +
 fs/btrfs/ctree.c                    |  866 ++++++++++++----
 fs/btrfs/ctree.h                    | 1452 ++++++++++++++++++++++-----
 fs/btrfs/dir-item.c                 |  192 ++--
 fs/btrfs/disk-io.c                  | 1062 ++++++++++++++++++++
 fs/btrfs/disk-io.h                  |   50 +
 fs/btrfs/extent-cache.c             |  318 ++++++
 fs/btrfs/extent-cache.h             |  104 ++
 fs/btrfs/extent-io.c                |  845 ++++++++++++++--
 fs/btrfs/extent-io.h                |  164 +++
 fs/btrfs/hash.c                     |   38 -
 fs/btrfs/inode.c                    |  879 +++++++++++-----
 fs/btrfs/kernel-shared/btrfs_tree.h | 1333 ++++++++++++++++++++++++
 fs/btrfs/root-tree.c                |   47 +
 fs/btrfs/root.c                     |   92 --
 fs/btrfs/subvolume.c                |  130 ---
 fs/btrfs/super.c                    |  246 -----
 fs/btrfs/volumes.c                  | 1173 ++++++++++++++++++++++
 fs/btrfs/volumes.h                  |  204 ++++
 30 files changed, 8308 insertions(+), 2504 deletions(-)
 delete mode 100644 fs/btrfs/btrfs_tree.h
 delete mode 100644 fs/btrfs/chunk-map.c
 create mode 100644 fs/btrfs/common/rbtree-utils.c
 create mode 100644 fs/btrfs/common/rbtree-utils.h
 create mode 100644 fs/btrfs/compat.h
 create mode 100644 fs/btrfs/crypto/hash.c
 create mode 100644 fs/btrfs/crypto/hash.h
 create mode 100644 fs/btrfs/disk-io.c
 create mode 100644 fs/btrfs/disk-io.h
 create mode 100644 fs/btrfs/extent-cache.c
 create mode 100644 fs/btrfs/extent-cache.h
 create mode 100644 fs/btrfs/extent-io.h
 delete mode 100644 fs/btrfs/hash.c
 create mode 100644 fs/btrfs/kernel-shared/btrfs_tree.h
 create mode 100644 fs/btrfs/root-tree.c
 delete mode 100644 fs/btrfs/root.c
 delete mode 100644 fs/btrfs/subvolume.c
 delete mode 100644 fs/btrfs/super.c
 create mode 100644 fs/btrfs/volumes.c
 create mode 100644 fs/btrfs/volumes.h
Signature · 1 line
2.26.0
author
#1 lore raw

PATCH 1/26 fs: btrfs: Sync btrfs_btree.h from kernel

This version includes all needed on-disk format from kernel.

Only need to modify the include headers for u-boot, everything else is untouched.

Also, since u-boot btrfs is using a different endian convert timing (at tree block read time), it needs some forced type conversion before proper cross port.

---
 fs/btrfs/btrfs.c                    |    3 +-
 fs/btrfs/btrfs_tree.h               |  766 ---------------
 fs/btrfs/ctree.h                    |  214 +----
 fs/btrfs/inode.c                    |    5 +-
 fs/btrfs/kernel-shared/btrfs_tree.h | 1333 +++++++++++++++++++++++++++
 fs/btrfs/root.c                     |    2 +-
 fs/btrfs/subvolume.c                |    2 +-
 7 files changed, 1343 insertions(+), 982 deletions(-)
 delete mode 100644 fs/btrfs/btrfs_tree.h
 create mode 100644 fs/btrfs/kernel-shared/btrfs_tree.h
fs/btrfs/btrfs.c +2 -1
index cb7e18274221..5d3ddd5931f0 100644
@@ -31,7 +31,8 @@ static int readdir_callback(const struct btrfs_root *root,
 	char filetime[32], *target = NULL;
 	time_t mtime;
 
-	if (btrfs_lookup_inode(root, &item->location, &inode, NULL)) {
+	if (btrfs_lookup_inode(root, (struct btrfs_key *)&item->location,
+			       &inode, NULL)) {
 		printf("%s: Cannot find inode item for directory entry %.*s!\n",
 		       __func__, item->name_len, name);
 		return 0;
fs/btrfs/btrfs_tree.h deleted +0 -766

766 lines not on this page · raw message

fs/btrfs/ctree.h +3 -211

253 lines not on this page · raw message

fs/btrfs/inode.c +3 -2
index 991c2f68c3b7..d88ae67217b1 100644
@@ -29,7 +29,7 @@ u64 btrfs_lookup_inode_ref(struct btrfs_root *root, u64 inr,
 		*refp = *ref;
 
 	if (name) {
-		if (ref->name_len > BTRFS_NAME_MAX) {
+		if (ref->name_len > BTRFS_NAME_LEN) {
 			printf("%s: inode name too long: %u\n", __func__,
 			        ref->name_len);
 			goto out;
@@ -255,7 +255,8 @@ u64 btrfs_lookup_path(struct btrfs_root *root, u64 inr, const char *path,
 
 		type = item.type;
 		have_inode = 1;
-		if (btrfs_lookup_inode(root, &item.location, &inode_item, root))
+		if (btrfs_lookup_inode(root, (struct btrfs_key *)&item.location,
+					&inode_item, root))
 			return -1ULL;
 
 		if (item.type == BTRFS_FT_SYMLINK && symlink_limit >= 0) {
fs/btrfs/kernel-shared/btrfs_tree.h new file +1333 -0

1,333 lines not on this page · raw message

fs/btrfs/root.c +1 -1
index 127b67fd1c89..61155e8918b8 100644
@@ -75,7 +75,7 @@ u64 btrfs_lookup_root_ref(u64 subvolid, struct btrfs_root_ref *refp, char *name)
 		*refp = *ref;
 
 	if (name) {
-		if (ref->name_len > BTRFS_VOL_NAME_MAX) {
+		if (ref->name_len > BTRFS_NAME_LEN) {
 			printf("%s: volume name too long: %u\n", __func__,
 			       ref->name_len);
 			goto out;
fs/btrfs/subvolume.c +1 -1
index 06e54f331098..dbe92d13cb81 100644
@@ -14,7 +14,7 @@ static int get_subvol_name(u64 subvolid, char *name, int max_len)
 	struct btrfs_inode_ref iref;
 	struct btrfs_root root;
 	u64 dir;
-	char tmp[max(BTRFS_VOL_NAME_MAX, BTRFS_NAME_MAX)];
+	char tmp[BTRFS_NAME_LEN];
 	char *ptr;
 
 	ptr = name + max_len - 1;
Signature · 1 line
2.26.0
author
#2 lore raw

PATCH 2/26 fs: btrfs: Add More checksum algorithm support to btrfs

In reply to Qu Wenruo

This would mostly backport crypto/hash.[ch] from btrfs-progs.

The differences are:
- No blake2 support
No blake2 related library in u-boot yet.

- Use uboot xxhash/sha256 directly
No need to implement the code as U-boot has already provided the interface.

This adds the support for the following csums:
- SHA256
- XXHASH

---
 fs/btrfs/Makefile      |  2 +-
 fs/btrfs/btrfs.c       |  3 ++-
 fs/btrfs/btrfs.h       | 11 ---------
 fs/btrfs/crypto/hash.c | 55 ++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/crypto/hash.h | 17 +++++++++++++
 fs/btrfs/dir-item.c    |  1 +
 fs/btrfs/disk-io.c     | 22 +++++++++++++++++
 fs/btrfs/disk-io.h     | 20 +++++++++++++++
 fs/btrfs/hash.c        | 38 -----------------------------
 fs/btrfs/super.c       | 23 +++++++++---------
 10 files changed, 130 insertions(+), 62 deletions(-)
 create mode 100644 fs/btrfs/crypto/hash.c
 create mode 100644 fs/btrfs/crypto/hash.h
 create mode 100644 fs/btrfs/disk-io.c
 create mode 100644 fs/btrfs/disk-io.h
 delete mode 100644 fs/btrfs/hash.c
fs/btrfs/Makefile +1 -1
index 9b3159296fff..84b3b5ec0e8a 100644
@@ -3,4 +3,4 @@
 # 2017 Marek Behun, CZ.NIC, marek.behun at nic.cz
 
 obj-y := btrfs.o chunk-map.o compression.o ctree.o dev.o dir-item.o \
-	extent-io.o hash.o inode.o root.o subvolume.o super.o
+	extent-io.o inode.o root.o subvolume.o super.o crypto/hash.o disk-io.o
fs/btrfs/btrfs.c +2 -1
index 5d3ddd5931f0..012d8afa2f53 100644
@@ -5,10 +5,11 @@
  * 2017 Marek Behun, CZ.NIC, marek.behun at nic.cz
  */
 
-#include "btrfs.h"
 #include <config.h>
 #include <malloc.h>
 #include <linux/time.h>
+#include "btrfs.h"
+#include "crypto/hash.h"
 
 struct btrfs_info btrfs_info;
 
fs/btrfs/btrfs.h +0 -11
index 986d07867978..baceea2edb90 100644
@@ -23,17 +23,6 @@ struct btrfs_info {
 
 extern struct btrfs_info btrfs_info;
 
-/* hash.c */
-void btrfs_hash_init(void);
-u32 btrfs_crc32c(u32, const void *, size_t);
-u32 btrfs_csum_data(char *, u32, size_t);
-void btrfs_csum_final(u32, void *);
-
-static inline u64 btrfs_name_hash(const char *name, int len)
-{
-	return btrfs_crc32c((u32) ~1, name, len);
-}
-
 /* dev.c */
 extern struct blk_desc *btrfs_blk_desc;
 extern disk_partition_t *btrfs_part_info;
fs/btrfs/crypto/hash.c new file +55 -0

55 lines not on this page · raw message

fs/btrfs/crypto/hash.h new file +17 -0
new file mode 100644
index 000000000000..d1ba1fa374e3
@@ -0,0 +1,17 @@
+#ifndef CRYPTO_HASH_H
+#define CRYPTO_HASH_H
+
+#include <linux/types.h>
+
+#define CRYPTO_HASH_SIZE_MAX	32
+
+void btrfs_hash_init(void);
+int hash_crc32c(const u8 *buf, size_t length, u8 *out);
+int hash_xxhash(const u8 *buf, size_t length, u8 *out);
+int hash_sha256(const u8 *buf, size_t length, u8 *out);
+
+u32 crc32c(u32 seed, const void * data, size_t len);
+
+/* Blake2B is not yet supported due to lack of library */
+
+#endif
fs/btrfs/dir-item.c +1 -0
index 63b5bf0a860f..1ff446a45a85 100644
@@ -6,6 +6,7 @@
  */
 
 #include "btrfs.h"
+#include "disk-io.h"
 
 static int verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total)
 {
fs/btrfs/disk-io.c new file +22 -0
new file mode 100644
index 000000000000..58c32b548e50
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0+
+#include <common.h>
+#include <fs_internal.h>
+#include "disk-io.h"
+#include "crypto/hash.h"
+
+int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len)
+{
+	memset(out, 0, BTRFS_CSUM_SIZE);
+
+	switch (csum_type) {
+	case BTRFS_CSUM_TYPE_CRC32:
+		return hash_crc32c(data, len, out);
+	case BTRFS_CSUM_TYPE_XXHASH:
+		return hash_xxhash(data, len, out);
+	case BTRFS_CSUM_TYPE_SHA256:
+		return hash_sha256(data, len, out);
+	default:
+		printf("Unknown csum type %d\n", csum_type);
+		return -EINVAL;
+	}
+}
fs/btrfs/disk-io.h new file +20 -0
new file mode 100644
index 000000000000..efb715828cce
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0+
+#ifndef __BTRFS_DISK_IO_H__
+#define __BTRFS_DISK_IO_H__
+
+#include "crypto/hash.h"
+#include "ctree.h"
+#include "disk-io.h"
+
+static inline u64 btrfs_name_hash(const char *name, int len)
+{
+	u32 crc;
+
+	crc = crc32c((u32)~1, (unsigned char *)name, len);
+
+	return (u64)crc;
+}
+
+int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len);
+
+#endif
fs/btrfs/hash.c deleted +0 -38
deleted file mode 100644
index 52a8ceaf0c8b..000000000000
@@ -1,38 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * BTRFS filesystem implementation for U-Boot
- *
- * 2017 Marek Behun, CZ.NIC, marek.behun at nic.cz
- */
-
-#include "btrfs.h"
-#include <u-boot/crc.h>
-#include <asm/unaligned.h>
-
-static u32 btrfs_crc32c_table[256];
-
-void btrfs_hash_init(void)
-{
-	static int inited = 0;
-
-	if (!inited) {
-		crc32c_init(btrfs_crc32c_table, 0x82F63B78);
-		inited = 1;
-	}
-}
-
-u32 btrfs_crc32c(u32 crc, const void *data, size_t length)
-{
-	return crc32c_cal(crc, (const char *) data, length,
-			  btrfs_crc32c_table);
-}
-
-u32 btrfs_csum_data(char *data, u32 seed, size_t len)
-{
-	return btrfs_crc32c(seed, data, len);
-}
-
-void btrfs_csum_final(u32 crc, void *result)
-{
-	put_unaligned(cpu_to_le32(~crc), (u32 *)result);
-}
fs/btrfs/super.c +12 -11
index 2dc4a6fcd7a3..7891d84ed369 100644
@@ -6,6 +6,7 @@
  */
 
 #include "btrfs.h"
+#include "disk-io.h"
 #include <memalign.h>
 
 #define BTRFS_SUPER_FLAG_SUPP	(BTRFS_HEADER_FLAG_WRITTEN	\
@@ -57,19 +58,19 @@ static int btrfs_check_super_csum(char *raw_disk_sb)
 		(struct btrfs_super_block *) raw_disk_sb;
 	u16 csum_type = le16_to_cpu(disk_sb->csum_type);
 
-	if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
-		u32 crc = ~(u32) 0;
-		const int csum_size = sizeof(crc);
-		char result[csum_size];
+	if (csum_type == BTRFS_CSUM_TYPE_CRC32 ||
+	    csum_type == BTRFS_CSUM_TYPE_SHA256 ||
+	    csum_type == BTRFS_CSUM_TYPE_XXHASH) {
+		u8 result[BTRFS_CSUM_SIZE];
 
-		crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE, crc,
-				      BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
-		btrfs_csum_final(crc, result);
+		btrfs_csum_data(csum_type, (u8 *)raw_disk_sb + BTRFS_CSUM_SIZE,
+			result, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
 
-		if (memcmp(raw_disk_sb, result, csum_size))
-			return -1;
-	} else {
-		return -1;
+		if (memcmp(raw_disk_sb, result, BTRFS_CSUM_SIZE))
+			return -EIO;
+	} else if (csum_type == BTRFS_CSUM_TYPE_BLAKE2){
+		printf("Blake2 csum type is not supported yet\n");
+		return -ENOTSUPP;
 	}
 
 	return 0;
Signature · 1 line
2.26.0
author
#3 lore raw

PATCH 3/26 fs: btrfs: Cross-port btrfs_read_dev_super() from btrfs-progs

In reply to Qu Wenruo

This patch will use generic code shared from btrfs-progs to read one super block from block device.

To support the btrfs-progs style code, the following codes are also cross-ported:
- BTRFS_SETGET_FUNC for btrfs_super_block
- btrfs_check_super() function
- Move btrfs_read_superblock() to disk-io.[ch]
Since super.c only contains pretty small amount of code, and the extra check will be covered in later root read patches.

The different between this implementation from btrfs-progs are:
- No sbflags/sb_bytenr support
Since we only need to read the primary super block (like kernel), thus sbflags/sb_bytenr used by super block recovery is not needed.

Also, this changes the following behavior of U-boot btrfs:
- Only reads the primary super block
The old implementation will read all 3 super blocks, with one non-existing backup super.
This is not correct, especially for fs created after btrfs.

Just like kernel, we only check the primary super block.

---
 fs/btrfs/Makefile  |   2 +-
 fs/btrfs/btrfs.c   |   1 +
 fs/btrfs/compat.h  |  56 ++++++++++
 fs/btrfs/ctree.c   |  32 ++++++
 fs/btrfs/ctree.h   |  67 ++++++++++++
 fs/btrfs/disk-io.c | 213 ++++++++++++++++++++++++++++++++++++++
 fs/btrfs/disk-io.h |   7 ++
 fs/btrfs/super.c   | 247 ---------------------------------------------
 8 files changed, 377 insertions(+), 248 deletions(-)
 create mode 100644 fs/btrfs/compat.h
 delete mode 100644 fs/btrfs/super.c
fs/btrfs/Makefile +1 -1
index 84b3b5ec0e8a..bd4b848d1b1f 100644
@@ -3,4 +3,4 @@
 # 2017 Marek Behun, CZ.NIC, marek.behun at nic.cz
 
 obj-y := btrfs.o chunk-map.o compression.o ctree.o dev.o dir-item.o \
-	extent-io.o inode.o root.o subvolume.o super.o crypto/hash.o disk-io.o
+	extent-io.o inode.o root.o subvolume.o crypto/hash.o disk-io.o
fs/btrfs/btrfs.c +1 -0
index 012d8afa2f53..ec993a193106 100644
@@ -10,6 +10,7 @@
 #include <linux/time.h>
 #include "btrfs.h"
 #include "crypto/hash.h"
+#include "disk-io.h"
 
 struct btrfs_info btrfs_info;
 
fs/btrfs/compat.h new file +56 -0

56 lines not on this page · raw message

fs/btrfs/ctree.c +32 -0
index 7fae383f1509..bdf8e18fd3ee 100644
@@ -9,6 +9,38 @@
 #include <malloc.h>
 #include <memalign.h>
 
+static const struct btrfs_csum {
+	u16 size;
+	const char name[14];
+} btrfs_csums[] = {
+	[BTRFS_CSUM_TYPE_CRC32]		= {  4, "crc32c" },
+	[BTRFS_CSUM_TYPE_XXHASH]	= {  8, "xxhash64" },
+	[BTRFS_CSUM_TYPE_SHA256]	= { 32, "sha256" },
+	[BTRFS_CSUM_TYPE_BLAKE2]	= { 32, "blake2" },
+};
+
+u16 btrfs_super_csum_size(const struct btrfs_super_block *sb)
+{
+	const u16 csum_type = btrfs_super_csum_type(sb);
+
+	return btrfs_csums[csum_type].size;
+}
+
+const char *btrfs_super_csum_name(u16 csum_type)
+{
+	return btrfs_csums[csum_type].name;
+}
+
+size_t btrfs_super_num_csums(void)
+{
+	return ARRAY_SIZE(btrfs_csums);
+}
+
+u16 btrfs_csum_type_size(u16 csum_type)
+{
+	return btrfs_csums[csum_type].size;
+}
+
 int btrfs_comp_keys(struct btrfs_key *a, struct btrfs_key *b)
 {
 	if (a->objectid > b->objectid)
fs/btrfs/ctree.h +67 -0

83 lines not on this page · raw message

fs/btrfs/disk-io.c +213 -0

223 lines not on this page · raw message

fs/btrfs/disk-io.h +7 -0
index efb715828cce..671f1ac55c68 100644
@@ -2,10 +2,14 @@
 #ifndef __BTRFS_DISK_IO_H__
 #define __BTRFS_DISK_IO_H__
 
+#include <linux/sizes.h>
+#include <fs_internal.h>
 #include "crypto/hash.h"
 #include "ctree.h"
 #include "disk-io.h"
 
+#define BTRFS_SUPER_INFO_OFFSET SZ_64K
+#define BTRFS_SUPER_INFO_SIZE	SZ_4K
 static inline u64 btrfs_name_hash(const char *name, int len)
 {
 	u32 crc;
@@ -17,4 +21,7 @@ static inline u64 btrfs_name_hash(const char *name, int len)
 
 int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len);
 
+int btrfs_read_dev_super(struct blk_desc *desc, disk_partition_t *part,
+			 struct btrfs_super_block *sb);
+int btrfs_read_superblock(void);
 #endif
fs/btrfs/super.c deleted +0 -247

247 lines not on this page · raw message

Signature · 1 line
2.26.0
#4 lore raw

Re: [PATCH U-BOOT 03/26] fs: btrfs: Cross-port btrfs_read_dev_super() from btrfs-progs

fs/btrfs/compat.h line 13

Quoted 2 lines · On Wed, 22 Apr 2020 14:49:46 +0800 Qu Wenruo <[email protected]> wrote:
fs/btrfs/compat.h new file +2 -0
+/* A simple wraper to for error() from btrfs-progs */
+#define error(...) { printf(__VA_ARGS__); printf("\n"); }

Is this from btrfs-progs?
I don't like these macros much, I prefer to use static inline functions.

static inline void error(const char *fmt, ...)

{
	printf(fmt, __builtin_va_arg_pack());
	printf("\n");
}

Attribute for printf can be added to check printf specifiers:
__attribute__((format (__printf__, 1, 2)))

It is possible that this won't compile when optimizations are disabled. In that case more attributes are needed
__always_inline __attribute__((__gnu_inline__))
These could be defined as a macro in include/linux/compiler-gcc.h
#define __gnu_inline __always_inline __attribute__((__gnu_inline__))

Marek

#5 lore raw

Re: [PATCH U-BOOT 03/26] fs: btrfs: Cross-port btrfs_read_dev_super() from btrfs-progs

Quoted 7 lines · Marek Behun

fs/btrfs/compat.h line 13

Quoted 2 lines · On Wed, 22 Apr 2020 14:49:46 +0800 Qu Wenruo <[email protected]> wrote:
fs/btrfs/compat.h new file +2 -0
+/* A simple wraper to for error() from btrfs-progs */
+#define error(...) { printf(__VA_ARGS__); printf("\n"); }

Is this from btrfs-progs?

Nope, anything in compat.h is mostly for different projects to share the same code.

Quoted 7 lines

I don't like these macros much, I prefer to use static inline functions.

static inline void error(const char *fmt, ...)

{
	printf(fmt, __builtin_va_arg_pack());
	printf("\n");
}

That looks much better, especially we can tell compiler that we want printf check on the parameters.

Thanks,
Qu

Quoted 4 lines

Attribute for printf can be added to check printf specifiers:
__attribute__((format (__printf__, 1, 2)))

Quoted 8 lines

It is possible that this won't compile when optimizations are disabled. In that case more attributes are needed
__always_inline __attribute__((__gnu_inline__))
These could be defined as a macro in include/linux/compiler-gcc.h
#define __gnu_inline __always_inline __attribute__((__gnu_inline__))

Marek

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200422/c21dd025/attachment.sig>

author
#6 lore raw

PATCH 4/26 fs: btrfs: Cross-port rbtree-utils from btrfs-progs

In reply to Qu Wenruo

This is needed for incoming extent-cache infrastructure.

---
 fs/btrfs/Makefile              |  3 +-
 fs/btrfs/common/rbtree-utils.c | 83 ++++++++++++++++++++++++++++++++++
 fs/btrfs/common/rbtree-utils.h | 53 ++++++++++++++++++++++
 3 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 fs/btrfs/common/rbtree-utils.c
 create mode 100644 fs/btrfs/common/rbtree-utils.h
fs/btrfs/Makefile +2 -1
index bd4b848d1b1f..53be6e8835f2 100644
@@ -3,4 +3,5 @@
 # 2017 Marek Behun, CZ.NIC, marek.behun at nic.cz
 
 obj-y := btrfs.o chunk-map.o compression.o ctree.o dev.o dir-item.o \
-	extent-io.o inode.o root.o subvolume.o crypto/hash.o disk-io.o
+	extent-io.o inode.o root.o subvolume.o crypto/hash.o disk-io.o \
+	common/rbtree-utils.o extent-cache.o
fs/btrfs/common/rbtree-utils.c new file +83 -0

83 lines not on this page · raw message

fs/btrfs/common/rbtree-utils.h new file +53 -0

53 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#7 lore raw

PATCH 5/26 fs: btrfs: Cross-port extent-cache.[ch] from btrfs-progs

In reply to Qu Wenruo

This patch implements an infrastructure to insert/search/merge an extent range (with variable length).

This provides the basis for later extent buffer cache used in btrfs.

---
 fs/btrfs/extent-cache.c | 318 ++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/extent-cache.h | 104 +++++++++++++
 2 files changed, 422 insertions(+)
 create mode 100644 fs/btrfs/extent-cache.c
 create mode 100644 fs/btrfs/extent-cache.h
fs/btrfs/extent-cache.c new file +318 -0
new file mode 100644
index 000000000000..e7b055f55422
@@ -0,0 +1,318 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+/*
+ * Cross-ported from the same named file of btrfs-progs.
+ *
+ * Minor modification to include headers.
+ */
+#include <linux/kernel.h>
+#include <linux/rbtree.h>
+#include <linux/errno.h>
+#include <linux/bug.h>
+#include <stdlib.h>
+#include "extent-cache.h"
+#include "common/rbtree-utils.h"
+
+struct cache_extent_search_range {
+	u64 objectid;
+	u64 start;
+	u64 size;
+};
+
+static int cache_tree_comp_range(struct rb_node *node, void *data)
+{
+	struct cache_extent *entry;
+	struct cache_extent_search_range *range;
+
+	range = (struct cache_extent_search_range *)data;
+	entry = rb_entry(node, struct cache_extent, rb_node);
+
+	if (entry->start + entry->size <= range->start)
+		return 1;
+	else if (range->start + range->size <= entry->start)
+		return -1;
+	else
+		return 0;
+}
+
+static int cache_tree_comp_nodes(struct rb_node *node1, struct rb_node *node2)
+{
+	struct cache_extent *entry;
+	struct cache_extent_search_range range;
+
+	entry = rb_entry(node2, struct cache_extent, rb_node);
+	range.start = entry->start;
+	range.size = entry->size;
+
+	return cache_tree_comp_range(node1, (void *)&range);
+}
+
+static int cache_tree_comp_range2(struct rb_node *node, void *data)
+{
+	struct cache_extent *entry;
+	struct cache_extent_search_range *range;
+
+	range = (struct cache_extent_search_range *)data;
+	entry = rb_entry(node, struct cache_extent, rb_node);
+
+	if (entry->objectid < range->objectid)
+		return 1;
+	else if (entry->objectid > range->objectid)
+		return -1;
+	else if (entry->start + entry->size <= range->start)
+		return 1;
+	else if (range->start + range->size <= entry->start)
+		return -1;
+	else
+		return 0;
+}
+
+static int cache_tree_comp_nodes2(struct rb_node *node1, struct rb_node *node2)
+{
+	struct cache_extent *entry;
+	struct cache_extent_search_range range;
+
+	entry = rb_entry(node2, struct cache_extent, rb_node);
+	range.objectid = entry->objectid;
+	range.start = entry->start;
+	range.size = entry->size;
+
+	return cache_tree_comp_range2(node1, (void *)&range);
+}
+
+void cache_tree_init(struct cache_tree *tree)
+{
+	tree->root = RB_ROOT;
+}
+
+static struct cache_extent *alloc_cache_extent(u64 start, u64 size)
+{
+	struct cache_extent *pe = malloc(sizeof(*pe));
+
+	if (!pe)
+		return pe;
+
+	pe->objectid = 0;
+	pe->start = start;
+	pe->size = size;
+	return pe;
+}
+
+int add_cache_extent(struct cache_tree *tree, u64 start, u64 size)
+{
+	struct cache_extent *pe = alloc_cache_extent(start, size);
+	int ret;
+
+	if (!pe)
+		return -ENOMEM;
+
+	ret = insert_cache_extent(tree, pe);
+	if (ret)
+		free(pe);
+
+	return ret;
+}
+
+int insert_cache_extent(struct cache_tree *tree, struct cache_extent *pe)
+{
+	return rb_insert(&tree->root, &pe->rb_node, cache_tree_comp_nodes);
+}
+
+int insert_cache_extent2(struct cache_tree *tree, struct cache_extent *pe)
+{
+	return rb_insert(&tree->root, &pe->rb_node, cache_tree_comp_nodes2);
+}
+
+struct cache_extent *lookup_cache_extent(struct cache_tree *tree,
+					 u64 start, u64 size)
+{
+	struct rb_node *node;
+	struct cache_extent *entry;
+	struct cache_extent_search_range range;
+
+	range.start = start;
+	range.size = size;
+	node = rb_search(&tree->root, &range, cache_tree_comp_range, NULL);
+	if (!node)
+		return NULL;
+
+	entry = rb_entry(node, struct cache_extent, rb_node);
+	return entry;
+}
+
+struct cache_extent *lookup_cache_extent2(struct cache_tree *tree,
+					 u64 objectid, u64 start, u64 size)
+{
+	struct rb_node *node;
+	struct cache_extent *entry;
+	struct cache_extent_search_range range;
+
+	range.objectid = objectid;
+	range.start = start;
+	range.size = size;
+	node = rb_search(&tree->root, &range, cache_tree_comp_range2, NULL);
+	if (!node)
+		return NULL;
+
+	entry = rb_entry(node, struct cache_extent, rb_node);
+	return entry;
+}
+
+struct cache_extent *search_cache_extent(struct cache_tree *tree, u64 start)
+{
+	struct rb_node *next;
+	struct rb_node *node;
+	struct cache_extent *entry;
+	struct cache_extent_search_range range;
+
+	range.start = start;
+	range.size = 1;
+	node = rb_search(&tree->root, &range, cache_tree_comp_range, &next);
+	if (!node)
+		node = next;
+	if (!node)
+		return NULL;
+
+	entry = rb_entry(node, struct cache_extent, rb_node);
+	return entry;
+}
+
+struct cache_extent *search_cache_extent2(struct cache_tree *tree,
+					 u64 objectid, u64 start)
+{
+	struct rb_node *next;
+	struct rb_node *node;
+	struct cache_extent *entry;
+	struct cache_extent_search_range range;
+
+	range.objectid = objectid;
+	range.start = start;
+	range.size = 1;
+	node = rb_search(&tree->root, &range, cache_tree_comp_range2, &next);
+	if (!node)
+		node = next;
+	if (!node)
+		return NULL;
+
+	entry = rb_entry(node, struct cache_extent, rb_node);
+	return entry;
+}
+
+struct cache_extent *first_cache_extent(struct cache_tree *tree)
+{
+	struct rb_node *node = rb_first(&tree->root);
+
+	if (!node)
+		return NULL;
+	return rb_entry(node, struct cache_extent, rb_node);
+}
+
+struct cache_extent *last_cache_extent(struct cache_tree *tree)
+{
+	struct rb_node *node = rb_last(&tree->root);
+
+	if (!node)
+		return NULL;
+	return rb_entry(node, struct cache_extent, rb_node);
+}
+
+struct cache_extent *prev_cache_extent(struct cache_extent *pe)
+{
+	struct rb_node *node = rb_prev(&pe->rb_node);
+
+	if (!node)
+		return NULL;
+	return rb_entry(node, struct cache_extent, rb_node);
+}
+
+struct cache_extent *next_cache_extent(struct cache_extent *pe)
+{
+	struct rb_node *node = rb_next(&pe->rb_node);
+
+	if (!node)
+		return NULL;
+	return rb_entry(node, struct cache_extent, rb_node);
+}
+
+void remove_cache_extent(struct cache_tree *tree, struct cache_extent *pe)
+{
+	rb_erase(&pe->rb_node, &tree->root);
+}
+
+void cache_tree_free_extents(struct cache_tree *tree,
+			     free_cache_extent free_func)
+{
+	struct cache_extent *ce;
+
+	while ((ce = first_cache_extent(tree))) {
+		remove_cache_extent(tree, ce);
+		free_func(ce);
+	}
+}
+
+static void free_extent_cache(struct cache_extent *pe)
+{
+	free(pe);
+}
+
+void free_extent_cache_tree(struct cache_tree *tree)
+{
+	cache_tree_free_extents(tree, free_extent_cache);
+}
+
+int add_merge_cache_extent(struct cache_tree *tree, u64 start, u64 size)
+{
+	struct cache_extent *cache;
+	struct cache_extent *next = NULL;
+	struct cache_extent *prev = NULL;
+	int next_merged = 0;
+	int prev_merged = 0;
+	int ret = 0;
+
+	if (cache_tree_empty(tree))
+		goto insert;
+
+	cache = search_cache_extent(tree, start);
+	if (!cache) {
+		/*
+		 * Either the tree is completely empty, or the no range after
+		 * start.
+		 * Either way, the last cache_extent should be prev.
+		 */
+		prev = last_cache_extent(tree);
+	} else if (start <= cache->start) {
+		next = cache;
+		prev = prev_cache_extent(cache);
+	} else {
+		prev = cache;
+		next = next_cache_extent(cache);
+	}
+
+	/*
+	 * Ensure the range to be inserted won't cover with existings
+	 * Or we will need extra loop to do merge
+	 */
+	BUG_ON(next && start + size > next->start);
+	BUG_ON(prev && prev->start + prev->size > start);
+
+	if (next && start + size == next->start) {
+		next_merged = 1;
+		next->size = next->start + next->size - start;
+		next->start = start;
+	}
+	if (prev && prev->start + prev->size == start) {
+		prev_merged = 1;
+		if (next_merged) {
+			next->size = next->start + next->size - prev->start;
+			next->start = prev->start;
+			remove_cache_extent(tree, prev);
+			free(prev);
+		} else {
+			prev->size = start + size - prev->start;
+		}
+	}
+insert:
+	if (!prev_merged && !next_merged)
+		ret = add_cache_extent(tree, start, size);
+	return ret;
+}
fs/btrfs/extent-cache.h new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#8 lore raw

PATCH 6/26 fs: btrfs: Cross-port extent-io.[ch] from btrfs-progs

In reply to Qu Wenruo

This cross port brings the extent_io_tree infrastructure, with that we could finally bring in proper btrfs_fs_info structure to ctree.h.

Also with read/write_extent_buffer() implemented, also backport read/write_eb_member() to ctree.h.

---
 fs/btrfs/Makefile    |   2 +-
 fs/btrfs/ctree.h     |  55 +++
 fs/btrfs/extent-io.c | 801 ++++++++++++++++++++++++++++++++++++++++++-
 fs/btrfs/extent-io.h | 164 +++++++++
 4 files changed, 1020 insertions(+), 2 deletions(-)
 create mode 100644 fs/btrfs/extent-io.h
fs/btrfs/Makefile +1 -1
index 53be6e8835f2..6dac3a7e239b 100644
@@ -4,4 +4,4 @@
 
 obj-y := btrfs.o chunk-map.o compression.o ctree.o dev.o dir-item.o \
 	extent-io.o inode.o root.o subvolume.o crypto/hash.o disk-io.o \
-	common/rbtree-utils.o extent-cache.o
+	common/rbtree-utils.o extent-cache.o extent-io.o
fs/btrfs/ctree.h +55 -0

75 lines not on this page · raw message

fs/btrfs/extent-io.c +800 -1

812 lines not on this page · raw message

fs/btrfs/extent-io.h new file +164 -0

164 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#9 lore raw

PATCH 7/26 fs: btrfs: Cross port structure accessor into ctree.h

In reply to Qu Wenruo

This will include all structure accessor from btrfs-progs/ctree.h, which is also the same as kernel ctree.h.

All these accessor handles the endian convert at runtime, and since all of them are defined as static inline functions, even we don't use them they won't take extra text space.

---
 fs/btrfs/compat.h  |   14 +
 fs/btrfs/ctree.h   | 1136 ++++++++++++++++++++++++++++++++++++++++----
 fs/btrfs/disk-io.h |    9 -
 3 files changed, 1064 insertions(+), 95 deletions(-)
fs/btrfs/compat.h +14 -0
index 376a035cd6db..64e8b8ab3dfb 100644
@@ -23,6 +23,20 @@
 #define cpu_to_le8(v) (v)
 #define __le8 u8
 
+/*
+ * Macros to generate set/get funcs for the struct fields
+ * assume there is a lefoo_to_cpu for every type, so lets make a simple
+ * one for u8:
+ */
+#define le8_to_cpu(v) (v)
+#define cpu_to_le8(v) (v)
+#define __le8 u8
+
+#define get_unaligned_le8(p) (*((u8 *)(p)))
+#define get_unaligned_8(p) (*((u8 *)(p)))
+#define put_unaligned_le8(val,p) ((*((u8 *)(p))) = (val))
+#define put_unaligned_8(val,p) ((*((u8 *)(p))) = (val))
+
 /*
  * Read data from device specified by @desc and @part
  *
fs/btrfs/ctree.h +1050 -86

1,205 lines not on this page · raw message

fs/btrfs/disk-io.h +0 -9
index 671f1ac55c68..8190abc920ad 100644
@@ -4,20 +4,11 @@
 
 #include <linux/sizes.h>
 #include <fs_internal.h>
-#include "crypto/hash.h"
 #include "ctree.h"
 #include "disk-io.h"
 
 #define BTRFS_SUPER_INFO_OFFSET SZ_64K
 #define BTRFS_SUPER_INFO_SIZE	SZ_4K
-static inline u64 btrfs_name_hash(const char *name, int len)
-{
-	u32 crc;
-
-	crc = crc32c((u32)~1, (unsigned char *)name, len);
-
-	return (u64)crc;
-}
 
 int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len);
 
Signature · 1 line
2.26.0
author
#10 lore raw

PATCH 8/26 fs: btrfs: Cross port volumes.[ch] from btrfs-progs

In reply to Qu Wenruo

This patch cross ports volumes.[ch] from btrfs-progs, including:
- btrfs_map_block()
The core mechanism to map btrfs logical address to physical address. This version includes multi-device support, along with RAID56 support.

- btrfs_scan_one_device()
This is the function to register one btrfs device to the list. This is the main part of the multi-device btrfs assembling process. Although we're not going to support multiple devices until U-boot allows us to scan one device without actually opening it.

---
 fs/btrfs/Makefile  |   2 +-
 fs/btrfs/compat.h  |   5 +
 fs/btrfs/volumes.c | 872 +++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/volumes.h | 202 +++++++++++
 4 files changed, 1080 insertions(+), 1 deletion(-)
 create mode 100644 fs/btrfs/volumes.c
 create mode 100644 fs/btrfs/volumes.h
fs/btrfs/Makefile +1 -1
index 6dac3a7e239b..573bb7514fce 100644
@@ -4,4 +4,4 @@
 
 obj-y := btrfs.o chunk-map.o compression.o ctree.o dev.o dir-item.o \
 	extent-io.o inode.o root.o subvolume.o crypto/hash.o disk-io.o \
-	common/rbtree-utils.o extent-cache.o extent-io.o
+	common/rbtree-utils.o extent-cache.o extent-io.o volumes.o
fs/btrfs/compat.h +5 -0
index 64e8b8ab3dfb..8840e509f32c 100644
@@ -67,4 +67,9 @@ static inline void uuid_unparse(const u8 *uuid, char *out)
 	return uuid_bin_to_str((unsigned char *)uuid, out, 0);
 }
 
+static inline int is_power_of_2(unsigned long n)
+{
+	return (n != 0 && ((n & (n - 1)) == 0));
+}
+
 #endif
fs/btrfs/volumes.c new file +872 -0

872 lines not on this page · raw message

fs/btrfs/volumes.h new file +202 -0

202 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#11 lore raw

PATCH 9/26 fs: btrfs: Crossport read_tree_block() from btrfs-progs

In reply to Qu Wenruo

This is the one of the base stone function for btrfs, which will:
- Do the chunk mapping resolve
- Read data from disk
- Do various sanity check

WIth read_tree_block(), we can finally cross port needed btrfs btree operations to U-boot.

---
 fs/btrfs/ctree.c   | 188 +++++++++++++++++++++++-
 fs/btrfs/ctree.h   |  50 ++++++-
 fs/btrfs/disk-io.c | 348 +++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/disk-io.h |  18 ++-
 fs/btrfs/inode.c   |   6 +-
 5 files changed, 602 insertions(+), 8 deletions(-)
fs/btrfs/ctree.c +186 -2
index bdf8e18fd3ee..4e7f213f71ed 100644
@@ -41,7 +41,7 @@ u16 btrfs_csum_type_size(u16 csum_type)
 	return btrfs_csums[csum_type].size;
 }
 
-int btrfs_comp_keys(struct btrfs_key *a, struct btrfs_key *b)
+int __btrfs_comp_keys(struct btrfs_key *a, struct btrfs_key *b)
 {
 	if (a->objectid > b->objectid)
 		return 1;
@@ -81,7 +81,7 @@ static int generic_bin_search(void *addr, int item_size, struct btrfs_key *key,
 		mid = (low + high) / 2;
 
 		tmp = (struct btrfs_key *) ((u8 *) addr + mid*item_size);
-		ret = btrfs_comp_keys(tmp, key);
+		ret = __btrfs_comp_keys(tmp, key);
 
 		if (ret < 0) {
 			low = mid + 1;
@@ -320,3 +320,187 @@ int btrfs_next_slot(struct btrfs_path *p)
 	p->slots[0]++;
 	return 0;
 }
+
+int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
+{
+	if (k1->objectid > k2->objectid)
+		return 1;
+	if (k1->objectid < k2->objectid)
+		return -1;
+	if (k1->type > k2->type)
+		return 1;
+	if (k1->type < k2->type)
+		return -1;
+	if (k1->offset > k2->offset)
+		return 1;
+	if (k1->offset < k2->offset)
+		return -1;
+	return 0;
+}
+
+static int btrfs_comp_keys(struct btrfs_disk_key *disk,
+			     const struct btrfs_key *k2)
+{
+	struct btrfs_key k1;
+
+	btrfs_disk_key_to_cpu(&k1, disk);
+	return btrfs_comp_cpu_keys(&k1, k2);
+}
+
+enum btrfs_tree_block_status
+btrfs_check_node(struct btrfs_fs_info *fs_info,
+		 struct btrfs_disk_key *parent_key, struct extent_buffer *buf)
+{
+	int i;
+	struct btrfs_key cpukey;
+	struct btrfs_disk_key key;
+	u32 nritems = btrfs_header_nritems(buf);
+	enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
+
+	if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(fs_info))
+		goto fail;
+
+	ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
+	if (parent_key && parent_key->type) {
+		btrfs_node_key(buf, &key, 0);
+		if (memcmp(parent_key, &key, sizeof(key)))
+			goto fail;
+	}
+	ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
+	for (i = 0; nritems > 1 && i < nritems - 2; i++) {
+		btrfs_node_key(buf, &key, i);
+		btrfs_node_key_to_cpu(buf, &cpukey, i + 1);
+		if (btrfs_comp_keys(&key, &cpukey) >= 0)
+			goto fail;
+	}
+	return BTRFS_TREE_BLOCK_CLEAN;
+fail:
+	return ret;
+}
+
+enum btrfs_tree_block_status
+btrfs_check_leaf(struct btrfs_fs_info *fs_info,
+		 struct btrfs_disk_key *parent_key, struct extent_buffer *buf)
+{
+	int i;
+	struct btrfs_key cpukey;
+	struct btrfs_disk_key key;
+	u32 nritems = btrfs_header_nritems(buf);
+	enum btrfs_tree_block_status ret = BTRFS_TREE_BLOCK_INVALID_NRITEMS;
+
+	if (nritems * sizeof(struct btrfs_item) > buf->len)  {
+		fprintf(stderr, "invalid number of items %llu\n",
+			(unsigned long long)buf->start);
+		goto fail;
+	}
+
+	if (btrfs_header_level(buf) != 0) {
+		ret = BTRFS_TREE_BLOCK_INVALID_LEVEL;
+		fprintf(stderr, "leaf is not a leaf %llu\n",
+		       (unsigned long long)btrfs_header_bytenr(buf));
+		goto fail;
+	}
+	if (btrfs_leaf_free_space(buf) < 0) {
+		ret = BTRFS_TREE_BLOCK_INVALID_FREE_SPACE;
+		fprintf(stderr, "leaf free space incorrect %llu %d\n",
+			(unsigned long long)btrfs_header_bytenr(buf),
+			btrfs_leaf_free_space(buf));
+		goto fail;
+	}
+
+	if (nritems == 0)
+		return BTRFS_TREE_BLOCK_CLEAN;
+
+	btrfs_item_key(buf, &key, 0);
+	if (parent_key && parent_key->type &&
+	    memcmp(parent_key, &key, sizeof(key))) {
+		ret = BTRFS_TREE_BLOCK_INVALID_PARENT_KEY;
+		fprintf(stderr, "leaf parent key incorrect %llu\n",
+		       (unsigned long long)btrfs_header_bytenr(buf));
+		goto fail;
+	}
+	for (i = 0; nritems > 1 && i < nritems - 1; i++) {
+		btrfs_item_key(buf, &key, i);
+		btrfs_item_key_to_cpu(buf, &cpukey, i + 1);
+		if (btrfs_comp_keys(&key, &cpukey) >= 0) {
+			ret = BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
+			fprintf(stderr, "bad key ordering %d %d\n", i, i+1);
+			goto fail;
+		}
+		if (btrfs_item_offset_nr(buf, i) !=
+			btrfs_item_end_nr(buf, i + 1)) {
+			ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
+			fprintf(stderr, "incorrect offsets %u %u\n",
+				btrfs_item_offset_nr(buf, i),
+				btrfs_item_end_nr(buf, i + 1));
+			goto fail;
+		}
+		if (i == 0 && btrfs_item_end_nr(buf, i) !=
+		    BTRFS_LEAF_DATA_SIZE(fs_info)) {
+			ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
+			fprintf(stderr, "bad item end %u wanted %u\n",
+				btrfs_item_end_nr(buf, i),
+				(unsigned)BTRFS_LEAF_DATA_SIZE(fs_info));
+			goto fail;
+		}
+	}
+
+	for (i = 0; i < nritems; i++) {
+		if (btrfs_item_end_nr(buf, i) >
+				BTRFS_LEAF_DATA_SIZE(fs_info)) {
+			btrfs_item_key(buf, &key, 0);
+			ret = BTRFS_TREE_BLOCK_INVALID_OFFSETS;
+			fprintf(stderr, "slot end outside of leaf %llu > %llu\n",
+				(unsigned long long)btrfs_item_end_nr(buf, i),
+				(unsigned long long)BTRFS_LEAF_DATA_SIZE(
+					fs_info));
+			goto fail;
+		}
+	}
+
+	return BTRFS_TREE_BLOCK_CLEAN;
+fail:
+	return ret;
+}
+
+/*
+ * how many bytes are required to store the items in a leaf.  start
+ * and nr indicate which items in the leaf to check.  This totals up the
+ * space used both by the item structs and the item data
+ */
+static int leaf_space_used(struct extent_buffer *l, int start, int nr)
+{
+	int data_len;
+	int nritems = btrfs_header_nritems(l);
+	int end = min(nritems, start + nr) - 1;
+
+	if (!nr)
+		return 0;
+	data_len = btrfs_item_end_nr(l, start);
+	data_len = data_len - btrfs_item_offset_nr(l, end);
+	data_len += sizeof(struct btrfs_item) * nr;
+	WARN_ON(data_len < 0);
+	return data_len;
+}
+
+/*
+ * The space between the end of the leaf items and
+ * the start of the leaf data.  IOW, how much room
+ * the leaf has left for both items and data
+ */
+int btrfs_leaf_free_space(struct extent_buffer *leaf)
+{
+	int nritems = btrfs_header_nritems(leaf);
+	u32 leaf_data_size;
+	int ret;
+
+	BUG_ON(leaf->fs_info && leaf->fs_info->nodesize != leaf->len);
+	leaf_data_size = __BTRFS_LEAF_DATA_SIZE(leaf->len);
+	ret = leaf_data_size - leaf_space_used(leaf, 0 ,nritems);
+	if (ret < 0) {
+		printk("leaf free space ret %d, leaf data size %u, used %d nritems %d\n",
+		       ret, leaf_data_size, leaf_space_used(leaf, 0, nritems),
+		       nritems);
+	}
+	return ret;
+}
fs/btrfs/ctree.h +49 -1

72 lines not on this page · raw message

fs/btrfs/disk-io.c +348 -0

359 lines not on this page · raw message

fs/btrfs/disk-io.h +16 -2
index 8190abc920ad..9d593374c096 100644
@@ -10,9 +10,23 @@
 #define BTRFS_SUPER_INFO_OFFSET SZ_64K
 #define BTRFS_SUPER_INFO_SIZE	SZ_4K
 
-int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len);
+/* U-boot specific */
+int btrfs_read_superblock(void);
+
+/* From btrfs-progs */
+int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror);
+struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
+		u64 parent_transid);
 
+struct extent_buffer* btrfs_find_create_tree_block(
+		struct btrfs_fs_info *fs_info, u64 bytenr);
+struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
+					    u64 bytenr, u32 blocksize);
 int btrfs_read_dev_super(struct blk_desc *desc, disk_partition_t *part,
 			 struct btrfs_super_block *sb);
-int btrfs_read_superblock(void);
+int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid);
+int btrfs_set_buffer_uptodate(struct extent_buffer *buf);
+int btrfs_csum_data(u16 csum_type, const u8 *data, u8 *out, size_t len);
+int csum_tree_block_size(struct extent_buffer *buf, u16 csum_sectorsize,
+			 int verify, u16 csum_type);
 #endif
fs/btrfs/inode.c +3 -3
index d88ae67217b1..55f431a28900 100644
@@ -65,7 +65,7 @@ int btrfs_lookup_inode(const struct btrfs_root *root,
 	if (btrfs_search_tree(&tmp_root, location, &path))
 		return res;
 
-	if (btrfs_comp_keys(location, btrfs_path_leaf_key(&path)))
+	if (__btrfs_comp_keys(location, btrfs_path_leaf_key(&path)))
 		goto out;
 
 	if (item) {
@@ -98,7 +98,7 @@ int btrfs_readlink(const struct btrfs_root *root, u64 inr, char *target)
 	if (btrfs_search_tree(root, &key, &path))
 		return -1;
 
-	if (btrfs_comp_keys(&key, btrfs_path_leaf_key(&path)))
+	if (__btrfs_comp_keys(&key, btrfs_path_leaf_key(&path)))
 		goto out;
 
 	extent = btrfs_path_item_ptr(&path, struct btrfs_file_extent_item);
@@ -333,7 +333,7 @@ u64 btrfs_file_read(const struct btrfs_root *root, u64 inr, u64 offset,
 	if (btrfs_search_tree(root, &key, &path))
 		return -1ULL;
 
-	if (btrfs_comp_keys(&key, btrfs_path_leaf_key(&path)) < 0) {
+	if (__btrfs_comp_keys(&key, btrfs_path_leaf_key(&path)) < 0) {
 		if (btrfs_prev_slot(&path))
 			goto out;
 
Signature · 1 line
2.26.0
author
#12 lore raw

PATCH 10/26 fs: btrfs: Rename struct btrfs_path to struct __btrfs_path

In reply to Qu Wenruo

To avoid name conflicting between the extent buffer based btrfs_path from btrfs-progs.

Also rename btrfs_free_path() to __btrfs_free_path() to avoid conflicts.

---
 fs/btrfs/btrfs.h     |  4 ++--
 fs/btrfs/chunk-map.c |  4 ++--
 fs/btrfs/ctree.c     | 16 ++++++++--------
 fs/btrfs/ctree.h     | 20 ++++++++++----------
 fs/btrfs/dir-item.c  | 10 +++++-----
 fs/btrfs/extent-io.c |  4 ++--
 fs/btrfs/inode.c     | 16 ++++++++--------
 fs/btrfs/root.c      | 10 +++++-----
 fs/btrfs/subvolume.c |  4 ++--
 9 files changed, 44 insertions(+), 44 deletions(-)
fs/btrfs/btrfs.h +2 -2
index baceea2edb90..8b8f40d29388 100644
@@ -67,10 +67,10 @@ u64 btrfs_file_read(const struct btrfs_root *, u64, u64, u64, char *);
 u64 btrfs_get_default_subvol_objectid(void);
 
 /* extent-io.c */
-u64 btrfs_read_extent_inline(struct btrfs_path *,
+u64 btrfs_read_extent_inline(struct __btrfs_path *,
 			      struct btrfs_file_extent_item *, u64, u64,
 			      char *);
-u64 btrfs_read_extent_reg(struct btrfs_path *, struct btrfs_file_extent_item *,
+u64 btrfs_read_extent_reg(struct __btrfs_path *, struct btrfs_file_extent_item *,
 			   u64, u64, char *);
 
 #endif /* !__BTRFS_BTRFS_H__ */
fs/btrfs/chunk-map.c +2 -2
index 0c9a659f8fff..85c3efc63e16 100644
@@ -143,7 +143,7 @@ int btrfs_chunk_map_init(void)
 
 int btrfs_read_chunk_tree(void)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_key key, *found_key;
 	struct btrfs_chunk *chunk;
 	int res = 0;
@@ -168,7 +168,7 @@ int btrfs_read_chunk_tree(void)
 		}
 	} while (!(res = btrfs_next_slot(&path)));
 
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 
 	if (res < 0)
 		return -1;
fs/btrfs/ctree.c +8 -8
index 4e7f213f71ed..25a69000dc4f 100644
@@ -114,7 +114,7 @@ int btrfs_bin_search(union btrfs_tree_node *p, struct btrfs_key *key,
 	return generic_bin_search(addr, size, key, p->header.nritems, slot);
 }
 
-static void clear_path(struct btrfs_path *p)
+static void clear_path(struct __btrfs_path *p)
 {
 	int i;
 
@@ -124,7 +124,7 @@ static void clear_path(struct btrfs_path *p)
 	}
 }
 
-void btrfs_free_path(struct btrfs_path *p)
+void __btrfs_free_path(struct __btrfs_path *p)
 {
 	int i;
 
@@ -181,7 +181,7 @@ static int read_tree_node(u64 physical, union btrfs_tree_node **buf)
 }
 
 int btrfs_search_tree(const struct btrfs_root *root, struct btrfs_key *key,
-		      struct btrfs_path *p)
+		      struct __btrfs_path *p)
 {
 	u8 lvl, prev_lvl;
 	int i, slot, ret;
@@ -235,13 +235,13 @@ int btrfs_search_tree(const struct btrfs_root *root, struct btrfs_key *key,
 
 	return 0;
 err:
-	btrfs_free_path(p);
+	__btrfs_free_path(p);
 	return -1;
 }
 
-static int jump_leaf(struct btrfs_path *path, int dir)
+static int jump_leaf(struct __btrfs_path *path, int dir)
 {
-	struct btrfs_path p;
+	struct __btrfs_path p;
 	u32 slot;
 	int level = 1, from_level, i;
 
@@ -301,7 +301,7 @@ err:
 	return -1;
 }
 
-int btrfs_prev_slot(struct btrfs_path *p)
+int btrfs_prev_slot(struct __btrfs_path *p)
 {
 	if (!p->slots[0])
 		return jump_leaf(p, -1);
@@ -310,7 +310,7 @@ int btrfs_prev_slot(struct btrfs_path *p)
 	return 0;
 }
 
-int btrfs_next_slot(struct btrfs_path *p)
+int btrfs_next_slot(struct __btrfs_path *p)
 {
 	struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
 
fs/btrfs/ctree.h +10 -10
index 52ebce6cded4..b98e5f8ac0a1 100644
@@ -1176,7 +1176,7 @@ union btrfs_tree_node {
 	struct btrfs_node node;
 };
 
-struct btrfs_path {
+struct __btrfs_path {
 	union btrfs_tree_node *nodes[BTRFS_MAX_LEVEL];
 	u32 slots[BTRFS_MAX_LEVEL];
 };
@@ -1190,20 +1190,20 @@ struct btrfs_root {
 int __btrfs_comp_keys(struct btrfs_key *, struct btrfs_key *);
 int btrfs_comp_keys_type(struct btrfs_key *, struct btrfs_key *);
 int btrfs_bin_search(union btrfs_tree_node *, struct btrfs_key *, int *);
-void btrfs_free_path(struct btrfs_path *);
+void __btrfs_free_path(struct __btrfs_path *);
 int btrfs_search_tree(const struct btrfs_root *, struct btrfs_key *,
-		      struct btrfs_path *);
-int btrfs_prev_slot(struct btrfs_path *);
-int btrfs_next_slot(struct btrfs_path *);
+		      struct __btrfs_path *);
+int btrfs_prev_slot(struct __btrfs_path *);
+int btrfs_next_slot(struct __btrfs_path *);
 
-static inline struct btrfs_key *btrfs_path_leaf_key(struct btrfs_path *p) {
+static inline struct btrfs_key *btrfs_path_leaf_key(struct __btrfs_path *p) {
 	/* At tree read time we have converted the endian for btrfs_disk_key */
 	return (struct btrfs_key *)&p->nodes[0]->leaf.items[p->slots[0]].key;
 }
 
 static inline struct btrfs_key *
 btrfs_search_tree_key_type(const struct btrfs_root *root, u64 objectid,
-			   u8 type, struct btrfs_path *path)
+			   u8 type, struct __btrfs_path *path)
 {
 	struct btrfs_key key, *res;
 
@@ -1216,14 +1216,14 @@ btrfs_search_tree_key_type(const struct btrfs_root *root, u64 objectid,
 
 	res = btrfs_path_leaf_key(path);
 	if (btrfs_comp_keys_type(&key, res)) {
-		btrfs_free_path(path);
+		__btrfs_free_path(path);
 		return NULL;
 	}
 
 	return res;
 }
 
-static inline u32 btrfs_path_item_size(struct btrfs_path *p)
+static inline u32 btrfs_path_item_size(struct __btrfs_path *p)
 {
 	return p->nodes[0]->leaf.items[p->slots[0]].size;
 }
@@ -1234,7 +1234,7 @@ static inline void *__btrfs_leaf_data(struct btrfs_leaf *leaf, u32 slot)
 	       + leaf->items[slot].offset;
 }
 
-static inline void *btrfs_path_leaf_data(struct btrfs_path *p)
+static inline void *btrfs_path_leaf_data(struct __btrfs_path *p)
 {
 	return __btrfs_leaf_data(&p->nodes[0]->leaf, p->slots[0]);
 }
fs/btrfs/dir-item.c +5 -5
index 1ff446a45a85..5595542dabaa 100644
@@ -32,7 +32,7 @@ static int verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total)
 }
 
 static struct btrfs_dir_item *
-btrfs_match_dir_item_name(struct btrfs_path *path, const char *name,
+btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name,
 			  int name_len)
 {
 	struct btrfs_dir_item *item;
@@ -65,7 +65,7 @@ int btrfs_lookup_dir_item(const struct btrfs_root *root, u64 dir,
 			  const char *name, int name_len,
 			  struct btrfs_dir_item *item)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_key key;
 	struct btrfs_dir_item *res = NULL;
 
@@ -83,14 +83,14 @@ int btrfs_lookup_dir_item(const struct btrfs_root *root, u64 dir,
 	if (res)
 		*item = *res;
 out:
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 	return res ? 0 : -1;
 }
 
 int btrfs_readdir(const struct btrfs_root *root, u64 dir,
 		  btrfs_readdir_callback_t callback)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_key key, *found_key;
 	struct btrfs_dir_item *item;
 	int res = 0;
@@ -119,7 +119,7 @@ int btrfs_readdir(const struct btrfs_root *root, u64 dir,
 			break;
 	} while (!(res = btrfs_next_slot(&path)));
 
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 
 	return res < 0 ? -1 : 0;
 }
fs/btrfs/extent-io.c +2 -2
index dc0832c8645f..456b8776ef67 100644
@@ -13,7 +13,7 @@
 #include "extent-io.h"
 #include "disk-io.h"
 
-u64 btrfs_read_extent_inline(struct btrfs_path *path,
+u64 btrfs_read_extent_inline(struct __btrfs_path *path,
 			     struct btrfs_file_extent_item *extent, u64 offset,
 			     u64 size, char *out)
 {
@@ -65,7 +65,7 @@ err:
 	return -1ULL;
 }
 
-u64 btrfs_read_extent_reg(struct btrfs_path *path,
+u64 btrfs_read_extent_reg(struct __btrfs_path *path,
 			  struct btrfs_file_extent_item *extent, u64 offset,
 			  u64 size, char *out)
 {
fs/btrfs/inode.c +8 -8
index 55f431a28900..c0778726a0ec 100644
@@ -11,7 +11,7 @@
 u64 btrfs_lookup_inode_ref(struct btrfs_root *root, u64 inr,
 			   struct btrfs_inode_ref *refp, char *name)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_key *key;
 	struct btrfs_inode_ref *ref;
 	u64 res = -1ULL;
@@ -40,7 +40,7 @@ u64 btrfs_lookup_inode_ref(struct btrfs_root *root, u64 inr,
 
 	res = key->offset;
 out:
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 	return res;
 }
 
@@ -50,7 +50,7 @@ int btrfs_lookup_inode(const struct btrfs_root *root,
 		       struct btrfs_root *new_root)
 {
 	struct btrfs_root tmp_root = *root;
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	int res = -1;
 
 	if (location->type == BTRFS_ROOT_ITEM_KEY) {
@@ -79,13 +79,13 @@ int btrfs_lookup_inode(const struct btrfs_root *root,
 	res = 0;
 
 out:
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 	return res;
 }
 
 int btrfs_readlink(const struct btrfs_root *root, u64 inr, char *target)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_key key;
 	struct btrfs_file_extent_item *extent;
 	const char *data_ptr;
@@ -131,7 +131,7 @@ int btrfs_readlink(const struct btrfs_root *root, u64 inr, char *target)
 	target[extent->ram_bytes] = '\0';
 	res = 0;
 out:
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 	return res;
 }
 
@@ -320,7 +320,7 @@ u64 btrfs_lookup_path(struct btrfs_root *root, u64 inr, const char *path,
 u64 btrfs_file_read(const struct btrfs_root *root, u64 inr, u64 offset,
 		    u64 size, char *buf)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_key key;
 	struct btrfs_file_extent_item *extent;
 	int res = 0;
@@ -379,6 +379,6 @@ u64 btrfs_file_read(const struct btrfs_root *root, u64 inr, u64 offset,
 		return -1ULL;
 
 out:
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 	return rd_all;
 }
fs/btrfs/root.c +5 -5
index 61155e8918b8..9b5f645015ae 100644
@@ -7,7 +7,7 @@
 
 #include "btrfs.h"
 
-static void read_root_item(struct btrfs_path *p, struct btrfs_root_item *item)
+static void read_root_item(struct __btrfs_path *p, struct btrfs_root_item *item)
 {
 	u32 len;
 	int reset = 0;
@@ -34,7 +34,7 @@ static void read_root_item(struct btrfs_path *p, struct btrfs_root_item *item)
 int btrfs_find_root(u64 objectid, struct btrfs_root *root,
 		    struct btrfs_root_item *root_item)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_root_item my_root_item;
 
 	if (!btrfs_search_tree_key_type(&btrfs_info.tree_root, objectid,
@@ -51,13 +51,13 @@ int btrfs_find_root(u64 objectid, struct btrfs_root *root,
 		root->root_dirid = root_item->root_dirid;
 	}
 
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 	return 0;
 }
 
 u64 btrfs_lookup_root_ref(u64 subvolid, struct btrfs_root_ref *refp, char *name)
 {
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_key *key;
 	struct btrfs_root_ref *ref;
 	u64 res = -1ULL;
@@ -86,7 +86,7 @@ u64 btrfs_lookup_root_ref(u64 subvolid, struct btrfs_root_ref *refp, char *name)
 
 	res = key->offset;
 out:
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 	return res;
 }
 
fs/btrfs/subvolume.c +2 -2
index dbe92d13cb81..fa5ef1e0acb7 100644
@@ -81,7 +81,7 @@ u64 btrfs_get_default_subvol_objectid(void)
 static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level)
 {
 	struct btrfs_key key, *found_key;
-	struct btrfs_path path;
+	struct __btrfs_path path;
 	struct btrfs_root_ref *ref;
 	int res;
 
@@ -116,7 +116,7 @@ static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level)
 			       "subvolumes\n", __func__);
 	} while (!(res = btrfs_next_slot(&path)));
 
-	btrfs_free_path(&path);
+	__btrfs_free_path(&path);
 }
 
 void btrfs_list_subvols(void)
Signature · 1 line
2.26.0
author
#13 lore raw

PATCH 11/26 fs: btrfs: Rename btrfs_root to __btrfs_root

In reply to Qu Wenruo

This is to avoid naming conflicts between extent buffer based btrfs_root.

---
 fs/btrfs/btrfs.c     | 10 +++++-----
 fs/btrfs/btrfs.h     | 26 +++++++++++++-------------
 fs/btrfs/ctree.c     |  2 +-
 fs/btrfs/ctree.h     |  6 +++---
 fs/btrfs/dir-item.c  |  4 ++--
 fs/btrfs/inode.c     | 16 ++++++++--------
 fs/btrfs/root.c      |  2 +-
 fs/btrfs/subvolume.c |  2 +-
 8 files changed, 34 insertions(+), 34 deletions(-)
fs/btrfs/btrfs.c +5 -5
index ec993a193106..1cb56ea0124d 100644
@@ -14,7 +14,7 @@
 
 struct btrfs_info btrfs_info;
 
-static int readdir_callback(const struct btrfs_root *root,
+static int readdir_callback(const struct __btrfs_root *root,
 			    struct btrfs_dir_item *item)
 {
 	static const char typestr[BTRFS_FT_MAX][4] = {
@@ -114,7 +114,7 @@ int btrfs_probe(struct blk_desc *fs_dev_desc, disk_partition_t *fs_partition)
 
 int btrfs_ls(const char *path)
 {
-	struct btrfs_root root = btrfs_info.fs_root;
+	struct __btrfs_root root = btrfs_info.fs_root;
 	u64 inr;
 	u8 type;
 
@@ -140,7 +140,7 @@ int btrfs_ls(const char *path)
 
 int btrfs_exists(const char *file)
 {
-	struct btrfs_root root = btrfs_info.fs_root;
+	struct __btrfs_root root = btrfs_info.fs_root;
 	u64 inr;
 	u8 type;
 
@@ -151,7 +151,7 @@ int btrfs_exists(const char *file)
 
 int btrfs_size(const char *file, loff_t *size)
 {
-	struct btrfs_root root = btrfs_info.fs_root;
+	struct __btrfs_root root = btrfs_info.fs_root;
 	struct btrfs_inode_item inode;
 	u64 inr;
 	u8 type;
@@ -176,7 +176,7 @@ int btrfs_size(const char *file, loff_t *size)
 int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
 	       loff_t *actread)
 {
-	struct btrfs_root root = btrfs_info.fs_root;
+	struct __btrfs_root root = btrfs_info.fs_root;
 	struct btrfs_inode_item inode;
 	u64 inr, rd;
 	u8 type;
fs/btrfs/btrfs.h +13 -13
index 8b8f40d29388..57cdb5aad58c 100644
@@ -14,9 +14,9 @@
 struct btrfs_info {
 	struct btrfs_super_block sb;
 
-	struct btrfs_root tree_root;
-	struct btrfs_root fs_root;
-	struct btrfs_root chunk_root;
+	struct __btrfs_root tree_root;
+	struct __btrfs_root fs_root;
+	struct __btrfs_root chunk_root;
 
 	struct rb_root chunks_root;
 };
@@ -42,26 +42,26 @@ u32 btrfs_decompress(u8 type, const char *, u32, char *, u32);
 int btrfs_read_superblock(void);
 
 /* dir-item.c */
-typedef int (*btrfs_readdir_callback_t)(const struct btrfs_root *,
+typedef int (*btrfs_readdir_callback_t)(const struct __btrfs_root *,
 					struct btrfs_dir_item *);
 
-int btrfs_lookup_dir_item(const struct btrfs_root *, u64, const char *, int,
+int btrfs_lookup_dir_item(const struct __btrfs_root *, u64, const char *, int,
 			   struct btrfs_dir_item *);
-int btrfs_readdir(const struct btrfs_root *, u64, btrfs_readdir_callback_t);
+int btrfs_readdir(const struct __btrfs_root *, u64, btrfs_readdir_callback_t);
 
 /* root.c */
-int btrfs_find_root(u64, struct btrfs_root *, struct btrfs_root_item *);
+int btrfs_find_root(u64, struct __btrfs_root *, struct btrfs_root_item *);
 u64 btrfs_lookup_root_ref(u64, struct btrfs_root_ref *, char *);
 
 /* inode.c */
-u64 btrfs_lookup_inode_ref(struct btrfs_root *, u64, struct btrfs_inode_ref *,
+u64 btrfs_lookup_inode_ref(struct __btrfs_root *, u64, struct btrfs_inode_ref *,
 			    char *);
-int btrfs_lookup_inode(const struct btrfs_root *, struct btrfs_key *,
-		        struct btrfs_inode_item *, struct btrfs_root *);
-int btrfs_readlink(const struct btrfs_root *, u64, char *);
-u64 btrfs_lookup_path(struct btrfs_root *, u64, const char *, u8 *,
+int btrfs_lookup_inode(const struct __btrfs_root *, struct btrfs_key *,
+		        struct btrfs_inode_item *, struct __btrfs_root *);
+int btrfs_readlink(const struct __btrfs_root *, u64, char *);
+u64 btrfs_lookup_path(struct __btrfs_root *, u64, const char *, u8 *,
 		       struct btrfs_inode_item *, int);
-u64 btrfs_file_read(const struct btrfs_root *, u64, u64, u64, char *);
+u64 btrfs_file_read(const struct __btrfs_root *, u64, u64, u64, char *);
 
 /* subvolume.c */
 u64 btrfs_get_default_subvol_objectid(void);
fs/btrfs/ctree.c +1 -1
index 25a69000dc4f..68f38a97b80e 100644
@@ -180,7 +180,7 @@ static int read_tree_node(u64 physical, union btrfs_tree_node **buf)
 	return 0;
 }
 
-int btrfs_search_tree(const struct btrfs_root *root, struct btrfs_key *key,
+int btrfs_search_tree(const struct __btrfs_root *root, struct btrfs_key *key,
 		      struct __btrfs_path *p)
 {
 	u8 lvl, prev_lvl;
fs/btrfs/ctree.h +3 -3
index b98e5f8ac0a1..f068db6505e0 100644
@@ -1181,7 +1181,7 @@ struct __btrfs_path {
 	u32 slots[BTRFS_MAX_LEVEL];
 };
 
-struct btrfs_root {
+struct __btrfs_root {
 	u64 objectid;
 	u64 bytenr;
 	u64 root_dirid;
@@ -1191,7 +1191,7 @@ int __btrfs_comp_keys(struct btrfs_key *, struct btrfs_key *);
 int btrfs_comp_keys_type(struct btrfs_key *, struct btrfs_key *);
 int btrfs_bin_search(union btrfs_tree_node *, struct btrfs_key *, int *);
 void __btrfs_free_path(struct __btrfs_path *);
-int btrfs_search_tree(const struct btrfs_root *, struct btrfs_key *,
+int btrfs_search_tree(const struct __btrfs_root *, struct btrfs_key *,
 		      struct __btrfs_path *);
 int btrfs_prev_slot(struct __btrfs_path *);
 int btrfs_next_slot(struct __btrfs_path *);
@@ -1202,7 +1202,7 @@ static inline struct btrfs_key *btrfs_path_leaf_key(struct __btrfs_path *p) {
 }
 
 static inline struct btrfs_key *
-btrfs_search_tree_key_type(const struct btrfs_root *root, u64 objectid,
+btrfs_search_tree_key_type(const struct __btrfs_root *root, u64 objectid,
 			   u8 type, struct __btrfs_path *path)
 {
 	struct btrfs_key key, *res;
fs/btrfs/dir-item.c +2 -2
index 5595542dabaa..756918df2e13 100644
@@ -61,7 +61,7 @@ btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name,
 	return NULL;
 }
 
-int btrfs_lookup_dir_item(const struct btrfs_root *root, u64 dir,
+int btrfs_lookup_dir_item(const struct __btrfs_root *root, u64 dir,
 			  const char *name, int name_len,
 			  struct btrfs_dir_item *item)
 {
@@ -87,7 +87,7 @@ out:
 	return res ? 0 : -1;
 }
 
-int btrfs_readdir(const struct btrfs_root *root, u64 dir,
+int btrfs_readdir(const struct __btrfs_root *root, u64 dir,
 		  btrfs_readdir_callback_t callback)
 {
 	struct __btrfs_path path;
fs/btrfs/inode.c +8 -8
index c0778726a0ec..be385d8a3f98 100644
@@ -8,7 +8,7 @@
 #include "btrfs.h"
 #include <malloc.h>
 
-u64 btrfs_lookup_inode_ref(struct btrfs_root *root, u64 inr,
+u64 btrfs_lookup_inode_ref(struct __btrfs_root *root, u64 inr,
 			   struct btrfs_inode_ref *refp, char *name)
 {
 	struct __btrfs_path path;
@@ -44,12 +44,12 @@ out:
 	return res;
 }
 
-int btrfs_lookup_inode(const struct btrfs_root *root,
+int btrfs_lookup_inode(const struct __btrfs_root *root,
 		       struct btrfs_key *location,
 		       struct btrfs_inode_item *item,
-		       struct btrfs_root *new_root)
+		       struct __btrfs_root *new_root)
 {
-	struct btrfs_root tmp_root = *root;
+	struct __btrfs_root tmp_root = *root;
 	struct __btrfs_path path;
 	int res = -1;
 
@@ -83,7 +83,7 @@ out:
 	return res;
 }
 
-int btrfs_readlink(const struct btrfs_root *root, u64 inr, char *target)
+int btrfs_readlink(const struct __btrfs_root *root, u64 inr, char *target)
 {
 	struct __btrfs_path path;
 	struct btrfs_key key;
@@ -137,7 +137,7 @@ out:
 
 /* inr must be a directory (for regular files with multiple hard links this
    function returns only one of the parents of the file) */
-static u64 get_parent_inode(struct btrfs_root *root, u64 inr,
+static u64 get_parent_inode(struct __btrfs_root *root, u64 inr,
 			    struct btrfs_inode_item *inode_item)
 {
 	struct btrfs_key key;
@@ -209,7 +209,7 @@ static inline const char *skip_current_directories(const char *cur)
 	return cur;
 }
 
-u64 btrfs_lookup_path(struct btrfs_root *root, u64 inr, const char *path,
+u64 btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 		      u8 *type_p, struct btrfs_inode_item *inode_item_p,
 		      int symlink_limit)
 {
@@ -317,7 +317,7 @@ u64 btrfs_lookup_path(struct btrfs_root *root, u64 inr, const char *path,
 	return inr;
 }
 
-u64 btrfs_file_read(const struct btrfs_root *root, u64 inr, u64 offset,
+u64 btrfs_file_read(const struct __btrfs_root *root, u64 inr, u64 offset,
 		    u64 size, char *buf)
 {
 	struct __btrfs_path path;
fs/btrfs/root.c +1 -1
index 9b5f645015ae..a424966df61e 100644
@@ -31,7 +31,7 @@ static void read_root_item(struct __btrfs_path *p, struct btrfs_root_item *item)
 	}
 }
 
-int btrfs_find_root(u64 objectid, struct btrfs_root *root,
+int btrfs_find_root(u64 objectid, struct __btrfs_root *root,
 		    struct btrfs_root_item *root_item)
 {
 	struct __btrfs_path path;
fs/btrfs/subvolume.c +1 -1
index fa5ef1e0acb7..72b847b940da 100644
@@ -12,7 +12,7 @@ static int get_subvol_name(u64 subvolid, char *name, int max_len)
 {
 	struct btrfs_root_ref rref;
 	struct btrfs_inode_ref iref;
-	struct btrfs_root root;
+	struct __btrfs_root root;
 	u64 dir;
 	char tmp[BTRFS_NAME_LEN];
 	char *ptr;
Signature · 1 line
2.26.0
author
#14 lore raw

PATCH 12/26 fs: btrfs: Cross port struct btrfs_root to ctree.h

In reply to Qu Wenruo

With write related members deleted.

---
 fs/btrfs/ctree.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
fs/btrfs/ctree.h +17 -0
index f068db6505e0..e8dcab1a0b5d 100644
@@ -68,6 +68,23 @@ enum btrfs_tree_block_status {
 	BTRFS_TREE_BLOCK_INVALID_OFFSETS,
 };
 
+struct btrfs_root {
+	struct extent_buffer *node;
+	struct btrfs_root_item root_item;
+	struct btrfs_key root_key;
+	struct btrfs_fs_info *fs_info;
+	u64 objectid;
+	u64 last_trans;
+
+	int ref_cows;
+	int track_dirty;
+
+	u32 type;
+	u64 last_inode_alloc;
+
+	struct rb_node rb_node;
+};
+
 struct btrfs_device;
 struct btrfs_fs_devices;
 struct btrfs_fs_info {
Signature · 1 line
2.26.0
author
#15 lore raw

PATCH 13/26 fs: btrfs: Crossport btrfs_search_slot() from btrfs-progs

In reply to Qu Wenruo

This patch copies the core function, btrfs_search_slot() from btrfs-progs.

This version has the following function removed:
- The ability to COW tree block
Related code is commented, as we may still need this ability later.

- The readahead functionality
That readahead is abused in kernel. Remove it completely.

With the core function in place, btrfs developers should feel at home now.

This also crossports supportive codes like btrfs_previous_item() to ctree.[ch].

---
 fs/btrfs/ctree.c | 536 ++++++++++++++++++++++++++++++++++++++++++++++-
 fs/btrfs/ctree.h |  61 +++++-
 2 files changed, 591 insertions(+), 6 deletions(-)
fs/btrfs/ctree.c +531 -5
index 68f38a97b80e..20c68a154d14 100644
@@ -5,9 +5,11 @@
  * 2017 Marek Behun, CZ.NIC, marek.behun at nic.cz
  */
 
-#include "btrfs.h"
+#include <linux/kernel.h>
 #include <malloc.h>
 #include <memalign.h>
+#include "btrfs.h"
+#include "disk-io.h"
 
 static const struct btrfs_csum {
 	u16 size;
@@ -41,6 +43,32 @@ u16 btrfs_csum_type_size(u16 csum_type)
 	return btrfs_csums[csum_type].size;
 }
 
+struct btrfs_path *btrfs_alloc_path(void)
+{
+	struct btrfs_path *path;
+	path = kzalloc(sizeof(struct btrfs_path), GFP_NOFS);
+	return path;
+}
+
+void btrfs_free_path(struct btrfs_path *p)
+{
+	if (!p)
+		return;
+	btrfs_release_path(p);
+	kfree(p);
+}
+
+void btrfs_release_path(struct btrfs_path *p)
+{
+	int i;
+	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
+		if (!p->nodes[i])
+			continue;
+		free_extent_buffer(p->nodes[i]);
+	}
+	memset(p, 0, sizeof(*p));
+}
+
 int __btrfs_comp_keys(struct btrfs_key *a, struct btrfs_key *b)
 {
 	if (a->objectid > b->objectid)
@@ -71,7 +99,17 @@ int btrfs_comp_keys_type(struct btrfs_key *a, struct btrfs_key *b)
 	return 0;
 }
 
-static int generic_bin_search(void *addr, int item_size, struct btrfs_key *key,
+/*
+ * search for key in the extent_buffer.  The items start at offset p,
+ * and they are item_size apart.  There are 'max' items in p.
+ *
+ * the slot in the array is returned via slot, and it points to
+ * the place where you would insert key if it is not found in
+ * the array.
+ *
+ * slot may point to max if the key is bigger than all of the keys
+ */
+static int __generic_bin_search(void *addr, int item_size, struct btrfs_key *key,
 			      int max, int *slot)
 {
 	int low = 0, high = max, mid, ret;
@@ -97,7 +135,7 @@ static int generic_bin_search(void *addr, int item_size, struct btrfs_key *key,
 	return 1;
 }
 
-int btrfs_bin_search(union btrfs_tree_node *p, struct btrfs_key *key,
+int __btrfs_bin_search(union btrfs_tree_node *p, struct btrfs_key *key,
 		     int *slot)
 {
 	void *addr;
@@ -111,7 +149,7 @@ int btrfs_bin_search(union btrfs_tree_node *p, struct btrfs_key *key,
 		size = sizeof(struct btrfs_item);
 	}
 
-	return generic_bin_search(addr, size, key, p->header.nritems, slot);
+	return __generic_bin_search(addr, size, key, p->header.nritems, slot);
 }
 
 static void clear_path(struct __btrfs_path *p)
@@ -208,7 +246,7 @@ int btrfs_search_tree(const struct __btrfs_root *root, struct btrfs_key *key,
 		}
 		prev_lvl = lvl;
 
-		ret = btrfs_bin_search(buf, key, &slot);
+		ret = __btrfs_bin_search(buf, key, &slot);
 		if (ret < 0)
 			goto err;
 		if (ret && slot > 0 && lvl)
@@ -463,6 +501,350 @@ fail:
 	return ret;
 }
 
+static int noinline check_block(struct btrfs_fs_info *fs_info,
+				struct btrfs_path *path, int level)
+{
+	struct btrfs_disk_key key;
+	struct btrfs_disk_key *key_ptr = NULL;
+	struct extent_buffer *parent;
+	enum btrfs_tree_block_status ret;
+
+	if (path->nodes[level + 1]) {
+		parent = path->nodes[level + 1];
+		btrfs_node_key(parent, &key, path->slots[level + 1]);
+		key_ptr = &key;
+	}
+	if (level == 0)
+		ret = btrfs_check_leaf(fs_info, key_ptr, path->nodes[0]);
+	else
+		ret = btrfs_check_node(fs_info, key_ptr, path->nodes[level]);
+	if (ret == BTRFS_TREE_BLOCK_CLEAN)
+		return 0;
+	return -EIO;
+}
+
+/*
+ * search for key in the extent_buffer.  The items start at offset p,
+ * and they are item_size apart.  There are 'max' items in p.
+ *
+ * the slot in the array is returned via slot, and it points to
+ * the place where you would insert key if it is not found in
+ * the array.
+ *
+ * slot may point to max if the key is bigger than all of the keys
+ */
+static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
+			      int item_size, const struct btrfs_key *key,
+			      int max, int *slot)
+{
+	int low = 0;
+	int high = max;
+	int mid;
+	int ret;
+	unsigned long offset;
+	struct btrfs_disk_key *tmp;
+
+	while(low < high) {
+		mid = (low + high) / 2;
+		offset = p + mid * item_size;
+
+		tmp = (struct btrfs_disk_key *)(eb->data + offset);
+		ret = btrfs_comp_keys(tmp, key);
+
+		if (ret < 0)
+			low = mid + 1;
+		else if (ret > 0)
+			high = mid;
+		else {
+			*slot = mid;
+			return 0;
+		}
+	}
+	*slot = low;
+	return 1;
+}
+
+/*
+ * simple bin_search frontend that does the right thing for
+ * leaves vs nodes
+ */
+int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
+		     int *slot)
+{
+	if (btrfs_header_level(eb) == 0)
+		return generic_bin_search(eb,
+					  offsetof(struct btrfs_leaf, items),
+					  sizeof(struct btrfs_item),
+					  key, btrfs_header_nritems(eb),
+					  slot);
+	else
+		return generic_bin_search(eb,
+					  offsetof(struct btrfs_node, ptrs),
+					  sizeof(struct btrfs_key_ptr),
+					  key, btrfs_header_nritems(eb),
+					  slot);
+}
+
+struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
+				   struct extent_buffer *parent, int slot)
+{
+	struct extent_buffer *ret;
+	int level = btrfs_header_level(parent);
+
+	if (slot < 0)
+		return NULL;
+	if (slot >= btrfs_header_nritems(parent))
+		return NULL;
+
+	if (level == 0)
+		return NULL;
+
+	ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
+		       btrfs_node_ptr_generation(parent, slot));
+	if (!extent_buffer_uptodate(ret))
+		return ERR_PTR(-EIO);
+
+	if (btrfs_header_level(ret) != level - 1) {
+		error(
+"child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
+		      btrfs_header_bytenr(parent), slot,
+		      btrfs_header_level(parent), btrfs_header_level(ret));
+		free_extent_buffer(ret);
+		return ERR_PTR(-EIO);
+	}
+	return ret;
+}
+
+int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
+		u64 iobjectid, u64 ioff, u8 key_type,
+		struct btrfs_key *found_key)
+{
+	int ret;
+	struct btrfs_key key;
+	struct extent_buffer *eb;
+	struct btrfs_path *path;
+
+	key.type = key_type;
+	key.objectid = iobjectid;
+	key.offset = ioff;
+
+	if (found_path == NULL) {
+		path = btrfs_alloc_path();
+		if (!path)
+			return -ENOMEM;
+	} else
+		path = found_path;
+
+	ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
+	if ((ret < 0) || (found_key == NULL))
+		goto out;
+
+	eb = path->nodes[0];
+	if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
+		ret = btrfs_next_leaf(fs_root, path);
+		if (ret)
+			goto out;
+		eb = path->nodes[0];
+	}
+
+	btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
+	if (found_key->type != key.type ||
+			found_key->objectid != key.objectid) {
+		ret = 1;
+		goto out;
+	}
+
+out:
+	if (path != found_path)
+		btrfs_free_path(path);
+	return ret;
+}
+
+/*
+ * look for key in the tree.  path is filled in with nodes along the way
+ * if key is found, we return zero and you can find the item in the leaf
+ * level of the path (level 0)
+ *
+ * If the key isn't found, the path points to the slot where it should
+ * be inserted, and 1 is returned.  If there are other errors during the
+ * search a negative error number is returned.
+ *
+ * if ins_len > 0, nodes and leaves will be split as we walk down the
+ * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
+ * possible)
+ *
+ * NOTE: This version has no COW ability, thus we expect trans == NULL,
+ * ins_len == 0 and cow == 0.
+ */
+int btrfs_search_slot(struct btrfs_trans_handle *trans,
+		struct btrfs_root *root, const struct btrfs_key *key,
+		struct btrfs_path *p, int ins_len, int cow)
+{
+	struct extent_buffer *b;
+	int slot;
+	int ret;
+	int level;
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	u8 lowest_level = 0;
+
+	assert(trans == NULL && ins_len == 0 && cow == 0);
+	lowest_level = p->lowest_level;
+	WARN_ON(lowest_level && ins_len > 0);
+	WARN_ON(p->nodes[0] != NULL);
+
+	b = root->node;
+	extent_buffer_get(b);
+	while (b) {
+		level = btrfs_header_level(b);
+		/*
+		if (cow) {
+			int wret;
+			wret = btrfs_cow_block(trans, root, b,
+					       p->nodes[level + 1],
+					       p->slots[level + 1],
+					       &b);
+			if (wret) {
+				free_extent_buffer(b);
+				return wret;
+			}
+		}
+		*/
+		BUG_ON(!cow && ins_len);
+		if (level != btrfs_header_level(b))
+			WARN_ON(1);
+		level = btrfs_header_level(b);
+		p->nodes[level] = b;
+		ret = check_block(fs_info, p, level);
+		if (ret)
+			return -1;
+		ret = btrfs_bin_search(b, key, &slot);
+		if (level != 0) {
+			if (ret && slot > 0)
+				slot -= 1;
+			p->slots[level] = slot;
+			/*
+			if ((p->search_for_split || ins_len > 0) &&
+			    btrfs_header_nritems(b) >=
+			    BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
+				int sret = split_node(trans, root, p, level);
+				BUG_ON(sret > 0);
+				if (sret)
+					return sret;
+				b = p->nodes[level];
+				slot = p->slots[level];
+			} else if (ins_len < 0) {
+				int sret = balance_level(trans, root, p,
+							 level);
+				if (sret)
+					return sret;
+				b = p->nodes[level];
+				if (!b) {
+					btrfs_release_path(p);
+					goto again;
+				}
+				slot = p->slots[level];
+				BUG_ON(btrfs_header_nritems(b) == 1);
+			}
+			*/
+			/* this is only true while dropping a snapshot */
+			if (level == lowest_level)
+				break;
+
+			b = read_node_slot(fs_info, b, slot);
+			if (!extent_buffer_uptodate(b))
+				return -EIO;
+		} else {
+			p->slots[level] = slot;
+			/*
+			if (ins_len > 0 &&
+			    ins_len > btrfs_leaf_free_space(b)) {
+				int sret = split_leaf(trans, root, key,
+						      p, ins_len, ret == 0);
+				BUG_ON(sret > 0);
+				if (sret)
+					return sret;
+			}
+			*/
+			return ret;
+		}
+	}
+	return 1;
+}
+
+/*
+ * Helper to use instead of search slot if no exact match is needed but
+ * instead the next or previous item should be returned.
+ * When find_higher is true, the next higher item is returned, the next lower
+ * otherwise.
+ * When return_any and find_higher are both true, and no higher item is found,
+ * return the next lower instead.
+ * When return_any is true and find_higher is false, and no lower item is found,
+ * return the next higher instead.
+ * It returns 0 if any item is found, 1 if none is found (tree empty), and
+ * < 0 on error
+ */
+int btrfs_search_slot_for_read(struct btrfs_root *root,
+                               const struct btrfs_key *key,
+                               struct btrfs_path *p, int find_higher,
+                               int return_any)
+{
+        int ret;
+        struct extent_buffer *leaf;
+
+again:
+        ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
+        if (ret <= 0)
+                return ret;
+        /*
+	 * A return value of 1 means the path is at the position where the item
+	 * should be inserted. Normally this is the next bigger item, but in
+	 * case the previous item is the last in a leaf, path points to the
+	 * first free slot in the previous leaf, i.e.@an invalid item.
+         */
+        leaf = p->nodes[0];
+
+        if (find_higher) {
+                if (p->slots[0] >= btrfs_header_nritems(leaf)) {
+                        ret = btrfs_next_leaf(root, p);
+                        if (ret <= 0)
+                                return ret;
+                        if (!return_any)
+                                return 1;
+                        /*
+			 * No higher item found, return the next lower instead
+                         */
+                        return_any = 0;
+                        find_higher = 0;
+                        btrfs_release_path(p);
+                        goto again;
+                }
+        } else {
+                if (p->slots[0] == 0) {
+                        ret = btrfs_prev_leaf(root, p);
+                        if (ret < 0)
+                                return ret;
+                        if (!ret) {
+                                leaf = p->nodes[0];
+                                if (p->slots[0] == btrfs_header_nritems(leaf))
+                                        p->slots[0]--;
+                                return 0;
+                        }
+                        if (!return_any)
+                                return 1;
+                        /*
+			 * No lower item found, return the next higher instead
+                         */
+                        return_any = 0;
+                        find_higher = 1;
+                        btrfs_release_path(p);
+                        goto again;
+                } else {
+                        --p->slots[0];
+                }
+        }
+        return 0;
+}
+
 /*
  * how many bytes are required to store the items in a leaf.  start
  * and nr indicate which items in the leaf to check.  This totals up the
@@ -504,3 +886,147 @@ int btrfs_leaf_free_space(struct extent_buffer *leaf)
 	}
 	return ret;
 }
+
+/*
+ * walk up the tree as far as required to find the previous leaf.
+ * returns 0 if it found something or 1 if there are no lesser leaves.
+ * returns < 0 on io errors.
+ */
+int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
+{
+	int slot;
+	int level = 1;
+	struct extent_buffer *c;
+	struct extent_buffer *next = NULL;
+	struct btrfs_fs_info *fs_info = root->fs_info;
+
+	while(level < BTRFS_MAX_LEVEL) {
+		if (!path->nodes[level])
+			return 1;
+
+		slot = path->slots[level];
+		c = path->nodes[level];
+		if (slot == 0) {
+			level++;
+			if (level == BTRFS_MAX_LEVEL)
+				return 1;
+			continue;
+		}
+		slot--;
+
+		next = read_node_slot(fs_info, c, slot);
+		if (!extent_buffer_uptodate(next)) {
+			if (IS_ERR(next))
+				return PTR_ERR(next);
+			return -EIO;
+		}
+		break;
+	}
+	path->slots[level] = slot;
+	while(1) {
+		level--;
+		c = path->nodes[level];
+		free_extent_buffer(c);
+		slot = btrfs_header_nritems(next);
+		if (slot != 0)
+			slot--;
+		path->nodes[level] = next;
+		path->slots[level] = slot;
+		if (!level)
+			break;
+		next = read_node_slot(fs_info, next, slot);
+		if (!extent_buffer_uptodate(next)) {
+			if (IS_ERR(next))
+				return PTR_ERR(next);
+			return -EIO;
+		}
+	}
+	return 0;
+}
+
+/*
+ * Walk up the tree as far as necessary to find the next sibling tree block.
+ * More generic version of btrfs_next_leaf(), as it could find sibling nodes
+ * if @path->lowest_level is not 0.
+ *
+ * returns 0 if it found something or 1 if there are no greater leaves.
+ * returns < 0 on io errors.
+ */
+int btrfs_next_sibling_tree_block(struct btrfs_fs_info *fs_info,
+				  struct btrfs_path *path)
+{
+	int slot;
+	int level = path->lowest_level + 1;
+	struct extent_buffer *c;
+	struct extent_buffer *next = NULL;
+
+	BUG_ON(path->lowest_level + 1 >= BTRFS_MAX_LEVEL);
+	do {
+		if (!path->nodes[level])
+			return 1;
+
+		slot = path->slots[level] + 1;
+		c = path->nodes[level];
+		if (slot >= btrfs_header_nritems(c)) {
+			level++;
+			if (level == BTRFS_MAX_LEVEL)
+				return 1;
+			continue;
+		}
+
+		next = read_node_slot(fs_info, c, slot);
+		if (!extent_buffer_uptodate(next))
+			return -EIO;
+		break;
+	} while (level < BTRFS_MAX_LEVEL);
+	path->slots[level] = slot;
+	while(1) {
+		level--;
+		c = path->nodes[level];
+		free_extent_buffer(c);
+		path->nodes[level] = next;
+		path->slots[level] = 0;
+		if (level == path->lowest_level)
+			break;
+		next = read_node_slot(fs_info, next, 0);
+		if (!extent_buffer_uptodate(next))
+			return -EIO;
+	}
+	return 0;
+}
+
+int btrfs_previous_item(struct btrfs_root *root,
+			struct btrfs_path *path, u64 min_objectid,
+			int type)
+{
+	struct btrfs_key found_key;
+	struct extent_buffer *leaf;
+	u32 nritems;
+	int ret;
+
+	while(1) {
+		if (path->slots[0] == 0) {
+			ret = btrfs_prev_leaf(root, path);
+			if (ret != 0)
+				return ret;
+		} else {
+			path->slots[0]--;
+		}
+		leaf = path->nodes[0];
+		nritems = btrfs_header_nritems(leaf);
+		if (nritems == 0)
+			return 1;
+		if (path->slots[0] == nritems)
+			path->slots[0]--;
+
+		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+		if (found_key.objectid < min_objectid)
+			break;
+		if (found_key.type == type)
+			return 0;
+		if (found_key.objectid == min_objectid &&
+		    found_key.type < type)
+			break;
+	}
+	return 1;
+}
fs/btrfs/ctree.h +60 -1

84 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#16 lore raw

PATCH 14/26 fs: btrfs: Crossport btrfs_read_sys_array() and btrfs_read_chunk_tree()

In reply to Qu Wenruo

These two functions play a big role in btrfs bootstrap.

The following function is removed:
- Seed device support

Although in theory we can still support multiple devices, we don't have a facility in U-boot to do device scan without opening them.

---
 fs/btrfs/btrfs.c     |   2 +-
 fs/btrfs/btrfs.h     |   2 +-
 fs/btrfs/chunk-map.c |   2 +-
 fs/btrfs/volumes.c   | 301 +++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/volumes.h   |   2 +
 5 files changed, 306 insertions(+), 3 deletions(-)
fs/btrfs/btrfs.c +1 -1
index 1cb56ea0124d..4f2fa2fe6c9f 100644
@@ -98,7 +98,7 @@ int btrfs_probe(struct blk_desc *fs_dev_desc, disk_partition_t *fs_partition)
 	btrfs_info.chunk_root.objectid = 0;
 	btrfs_info.chunk_root.bytenr = btrfs_info.sb.chunk_root;
 
-	if (btrfs_read_chunk_tree()) {
+	if (__btrfs_read_chunk_tree()) {
 		printf("%s: failed to read chunk tree\n", __func__);
 		return -1;
 	}
fs/btrfs/btrfs.h +1 -1
index 57cdb5aad58c..037492fa06a8 100644
@@ -33,7 +33,7 @@ int btrfs_devread(u64, int, void *);
 u64 btrfs_map_logical_to_physical(u64);
 int btrfs_chunk_map_init(void);
 void btrfs_chunk_map_exit(void);
-int btrfs_read_chunk_tree(void);
+int __btrfs_read_chunk_tree(void);
 
 /* compression.c */
 u32 btrfs_decompress(u8 type, const char *, u32, char *, u32);
fs/btrfs/chunk-map.c +1 -1
index 85c3efc63e16..49d8835e14e9 100644
@@ -141,7 +141,7 @@ int btrfs_chunk_map_init(void)
 	return 0;
 }
 
-int btrfs_read_chunk_tree(void)
+int __btrfs_read_chunk_tree(void)
 {
 	struct __btrfs_path path;
 	struct btrfs_key key, *found_key;
fs/btrfs/volumes.c +301 -0

319 lines not on this page · raw message

fs/btrfs/volumes.h +2 -0
index 4383b4f9c034..d0c09d831c74 100644
@@ -182,6 +182,8 @@ static inline int btrfs_next_bg_system(struct btrfs_fs_info *fs_info,
 	return btrfs_next_bg(fs_info, logical, size,
 			BTRFS_BLOCK_GROUP_SYSTEM);
 }
+int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
+int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices);
 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
 void btrfs_close_all_devices(void);
Signature · 1 line
2.26.0
author
#17 lore raw

PATCH 15/26 fs: btrfs: Crossport open_ctree_fs_info()

In reply to Qu Wenruo

open_ctree_fs_info() is the main entry point to open a btrfs.

This version is a simplfied version of __open_ctree_fd() of btrfs-progs, the main differences are:
- Parameters on how to specify a block device
Instead of @fd and @path, uboot uses blk_desc and disk_partition_t.

- Remove open_ctree flags
There will not be multiple open ctree modes in uboot.

Otherwise the function structure are all kept the same.

With open_ctree_fs_info() implemented, also introduce the global current_fs_info pointer to show the current opened btrfs.

---
 fs/btrfs/Makefile    |   2 +-
 fs/btrfs/btrfs.c     |  15 +-
 fs/btrfs/btrfs.h     |   1 +
 fs/btrfs/compat.h    |   2 +
 fs/btrfs/ctree.h     |   4 +
 fs/btrfs/disk-io.c   | 509 +++++++++++++++++++++++++++++++++++++++++--
 fs/btrfs/disk-io.h   |  19 ++
 fs/btrfs/root-tree.c |  47 ++++
 8 files changed, 579 insertions(+), 20 deletions(-)
 create mode 100644 fs/btrfs/root-tree.c
fs/btrfs/Makefile +1 -1
index 573bb7514fce..3cf8a716f8c0 100644
@@ -4,4 +4,4 @@
 
 obj-y := btrfs.o chunk-map.o compression.o ctree.o dev.o dir-item.o \
 	extent-io.o inode.o root.o subvolume.o crypto/hash.o disk-io.o \
-	common/rbtree-utils.o extent-cache.o extent-io.o volumes.o
+	common/rbtree-utils.o extent-cache.o extent-io.o volumes.o root-tree.o
fs/btrfs/btrfs.c +14 -1
index 4f2fa2fe6c9f..b86e5cc50b9e 100644
@@ -13,6 +13,7 @@
 #include "disk-io.h"
 
 struct btrfs_info btrfs_info;
+struct btrfs_fs_info *current_fs_info;
 
 static int readdir_callback(const struct __btrfs_root *root,
 			    struct btrfs_dir_item *item)
@@ -79,6 +80,9 @@ static int readdir_callback(const struct __btrfs_root *root,
 
 int btrfs_probe(struct blk_desc *fs_dev_desc, disk_partition_t *fs_partition)
 {
+	struct btrfs_fs_info *fs_info;
+	int ret = -1;
+
 	btrfs_blk_desc = fs_dev_desc;
 	btrfs_part_info = fs_partition;
 
@@ -109,7 +113,12 @@ int btrfs_probe(struct blk_desc *fs_dev_desc, disk_partition_t *fs_partition)
 		return -1;
 	}
 
-	return 0;
+	fs_info = open_ctree_fs_info(fs_dev_desc, fs_partition);
+	if (fs_info) {
+		current_fs_info = fs_info;
+		ret = 0;
+	}
+	return ret;
 }
 
 int btrfs_ls(const char *path)
@@ -213,6 +222,10 @@ int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
 void btrfs_close(void)
 {
 	btrfs_chunk_map_exit();
+	if (current_fs_info) {
+		close_ctree_fs_info(current_fs_info);
+		current_fs_info = NULL;
+	}
 }
 
 int btrfs_uuid(char *uuid_str)
fs/btrfs/btrfs.h +1 -0
index 037492fa06a8..3d20d2bf06bb 100644
@@ -22,6 +22,7 @@ struct btrfs_info {
 };
 
 extern struct btrfs_info btrfs_info;
+extern struct btrfs_fs_info *current_fs_info;
 
 /* dev.c */
 extern struct blk_desc *btrfs_blk_desc;
fs/btrfs/compat.h +2 -0
index 8840e509f32c..64c343f4169c 100644
@@ -12,6 +12,8 @@
 /* A simple wraper to for error() from btrfs-progs */
 #define error(...) { printf(__VA_ARGS__); printf("\n"); }
 
+#define ASSERT(c) assert(c)
+
 #define BTRFS_UUID_UNPARSED_SIZE	37
 
 /*
fs/btrfs/ctree.h +4 -0
index 1fe7a4544022..8699cef689ef 100644
@@ -1274,6 +1274,10 @@ const char *btrfs_super_csum_name(u16 csum_type);
 u16 btrfs_csum_type_size(u16 csum_type);
 size_t btrfs_super_num_csums(void);
 
+/* root-tree.c */
+int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
+			struct btrfs_root_item *item, struct btrfs_key *key);
+
 /* ctree.c */
 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
 enum btrfs_tree_block_status
fs/btrfs/disk-io.c +491 -18

524 lines not on this page · raw message

fs/btrfs/disk-io.h +19 -0
index 9d593374c096..d8f6f827550a 100644
@@ -22,6 +22,25 @@ struct extent_buffer* btrfs_find_create_tree_block(
 		struct btrfs_fs_info *fs_info, u64 bytenr);
 struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
 					    u64 bytenr, u32 blocksize);
+struct btrfs_root *btrfs_read_fs_root_no_cache(struct btrfs_fs_info *fs_info,
+					       struct btrfs_key *location);
+struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
+				      struct btrfs_key *location);
+
+void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
+		      u64 objectid);
+
+void btrfs_free_fs_info(struct btrfs_fs_info *fs_info);
+struct btrfs_fs_info *btrfs_new_fs_info(void);
+int btrfs_check_fs_compatibility(struct btrfs_super_block *sb);
+int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info);
+void btrfs_release_all_roots(struct btrfs_fs_info *fs_info);
+void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info);
+
+struct btrfs_fs_info *open_ctree_fs_info(struct blk_desc *desc,
+					 disk_partition_t *part);
+int close_ctree_fs_info(struct btrfs_fs_info *fs_info);
+
 int btrfs_read_dev_super(struct blk_desc *desc, disk_partition_t *part,
 			 struct btrfs_super_block *sb);
 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid);
fs/btrfs/root-tree.c new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#18 lore raw

PATCH 16/26 fs: btrfs: Rename path resolve related functions to avoid name conflicts

In reply to Qu Wenruo

Since the old code are all using __btrfs_path/__btrfs_root other than the regular extent buffer based one, adding "__" prefix for them to avoid name conflicts in the incoming cross ports.

---
 fs/btrfs/btrfs.c     | 12 ++++++------
 fs/btrfs/btrfs.h     | 10 +++++-----
 fs/btrfs/compat.h    |  3 +++
 fs/btrfs/ctree.h     |  7 +++++++
 fs/btrfs/dir-item.c  | 12 ++++++------
 fs/btrfs/inode.c     | 28 ++++++++++++++--------------
 fs/btrfs/subvolume.c |  4 ++--
 7 files changed, 43 insertions(+), 33 deletions(-)
fs/btrfs/btrfs.c +6 -6
index b86e5cc50b9e..7e6c7a26e4cf 100644
@@ -34,7 +34,7 @@ static int readdir_callback(const struct __btrfs_root *root,
 	char filetime[32], *target = NULL;
 	time_t mtime;
 
-	if (btrfs_lookup_inode(root, (struct btrfs_key *)&item->location,
+	if (__btrfs_lookup_inode(root, (struct btrfs_key *)&item->location,
 			       &inode, NULL)) {
 		printf("%s: Cannot find inode item for directory entry %.*s!\n",
 		       __func__, item->name_len, name);
@@ -48,7 +48,7 @@ static int readdir_callback(const struct __btrfs_root *root,
 		target = malloc(min(inode.size + 1,
 				    (u64) btrfs_info.sb.sectorsize));
 
-		if (target && btrfs_readlink(root, item->location.objectid,
+		if (target && __btrfs_readlink(root, item->location.objectid,
 					     target)) {
 			free(target);
 			target = NULL;
@@ -127,7 +127,7 @@ int btrfs_ls(const char *path)
 	u64 inr;
 	u8 type;
 
-	inr = btrfs_lookup_path(&root, root.root_dirid, path, &type, NULL, 40);
+	inr = __btrfs_lookup_path(&root, root.root_dirid, path, &type, NULL, 40);
 
 	if (inr == -1ULL) {
 		printf("Cannot lookup path %s\n", path);
@@ -153,7 +153,7 @@ int btrfs_exists(const char *file)
 	u64 inr;
 	u8 type;
 
-	inr = btrfs_lookup_path(&root, root.root_dirid, file, &type, NULL, 40);
+	inr = __btrfs_lookup_path(&root, root.root_dirid, file, &type, NULL, 40);
 
 	return (inr != -1ULL && type == BTRFS_FT_REG_FILE);
 }
@@ -165,7 +165,7 @@ int btrfs_size(const char *file, loff_t *size)
 	u64 inr;
 	u8 type;
 
-	inr = btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
+	inr = __btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
 				40);
 
 	if (inr == -1ULL) {
@@ -190,7 +190,7 @@ int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
 	u64 inr, rd;
 	u8 type;
 
-	inr = btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
+	inr = __btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
 				40);
 
 	if (inr == -1ULL) {
fs/btrfs/btrfs.h +5 -5
index 3d20d2bf06bb..e561d27944fc 100644
@@ -46,7 +46,7 @@ int btrfs_read_superblock(void);
 typedef int (*btrfs_readdir_callback_t)(const struct __btrfs_root *,
 					struct btrfs_dir_item *);
 
-int btrfs_lookup_dir_item(const struct __btrfs_root *, u64, const char *, int,
+int __btrfs_lookup_dir_item(const struct __btrfs_root *, u64, const char *, int,
 			   struct btrfs_dir_item *);
 int btrfs_readdir(const struct __btrfs_root *, u64, btrfs_readdir_callback_t);
 
@@ -55,12 +55,12 @@ int btrfs_find_root(u64, struct __btrfs_root *, struct btrfs_root_item *);
 u64 btrfs_lookup_root_ref(u64, struct btrfs_root_ref *, char *);
 
 /* inode.c */
-u64 btrfs_lookup_inode_ref(struct __btrfs_root *, u64, struct btrfs_inode_ref *,
+u64 __btrfs_lookup_inode_ref(struct __btrfs_root *, u64, struct btrfs_inode_ref *,
 			    char *);
-int btrfs_lookup_inode(const struct __btrfs_root *, struct btrfs_key *,
+int __btrfs_lookup_inode(const struct __btrfs_root *, struct btrfs_key *,
 		        struct btrfs_inode_item *, struct __btrfs_root *);
-int btrfs_readlink(const struct __btrfs_root *, u64, char *);
-u64 btrfs_lookup_path(struct __btrfs_root *, u64, const char *, u8 *,
+int __btrfs_readlink(const struct __btrfs_root *, u64, char *);
+u64 __btrfs_lookup_path(struct __btrfs_root *, u64, const char *, u8 *,
 		       struct btrfs_inode_item *, int);
 u64 btrfs_file_read(const struct __btrfs_root *, u64, u64, u64, char *);
 
fs/btrfs/compat.h +3 -0
index 64c343f4169c..535db2685220 100644
@@ -16,6 +16,9 @@
 
 #define BTRFS_UUID_UNPARSED_SIZE	37
 
+/* No <linux/limits.h> so have to define it here */
+#define XATTR_NAME_MAX		255
+
 /*
  * Macros to generate set/get funcs for the struct fields
  * assume there is a lefoo_to_cpu for every type, so lets make a simple
fs/btrfs/ctree.h +7 -0
index 8699cef689ef..f702eaff293c 100644
@@ -1278,6 +1278,13 @@ size_t btrfs_super_num_csums(void);
 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
 			struct btrfs_root_item *item, struct btrfs_key *key);
 
+/* dir-item.c */
+struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
+					     struct btrfs_root *root,
+					     struct btrfs_path *path, u64 dir,
+					     const char *name, int name_len,
+					     int mod);
+
 /* ctree.c */
 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
 enum btrfs_tree_block_status
fs/btrfs/dir-item.c +6 -6
index 756918df2e13..aea621c72bb3 100644
@@ -8,7 +8,7 @@
 #include "btrfs.h"
 #include "disk-io.h"
 
-static int verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total)
+static int __verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total)
 {
 	u16 max_len = BTRFS_NAME_LEN;
 	u32 end;
@@ -32,7 +32,7 @@ static int verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total)
 }
 
 static struct btrfs_dir_item *
-btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name,
+__btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name,
 			  int name_len)
 {
 	struct btrfs_dir_item *item;
@@ -48,7 +48,7 @@ btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name,
 		this_len = sizeof(*item) + item->name_len + item->data_len;
 		name_ptr = (const char *) (item + 1);
 
-		if (verify_dir_item(item, cur, total_len))
+		if (__verify_dir_item(item, cur, total_len))
 			return NULL;
 		if (item->name_len == name_len && !memcmp(name_ptr, name,
 							  name_len))
@@ -61,7 +61,7 @@ btrfs_match_dir_item_name(struct __btrfs_path *path, const char *name,
 	return NULL;
 }
 
-int btrfs_lookup_dir_item(const struct __btrfs_root *root, u64 dir,
+int __btrfs_lookup_dir_item(const struct __btrfs_root *root, u64 dir,
 			  const char *name, int name_len,
 			  struct btrfs_dir_item *item)
 {
@@ -79,7 +79,7 @@ int btrfs_lookup_dir_item(const struct __btrfs_root *root, u64 dir,
 	if (btrfs_comp_keys_type(&key, btrfs_path_leaf_key(&path)))
 		goto out;
 
-	res = btrfs_match_dir_item_name(&path, name, name_len);
+	res = __btrfs_match_dir_item_name(&path, name, name_len);
 	if (res)
 		*item = *res;
 out:
@@ -110,7 +110,7 @@ int btrfs_readdir(const struct __btrfs_root *root, u64 dir,
 		item = btrfs_path_item_ptr(&path, struct btrfs_dir_item);
 		btrfs_dir_item_to_cpu(item);
 
-		if (verify_dir_item(item, 0, sizeof(*item) + item->name_len))
+		if (__verify_dir_item(item, 0, sizeof(*item) + item->name_len))
 			continue;
 		if (item->type == BTRFS_FT_XATTR)
 			continue;
fs/btrfs/inode.c +14 -14
index be385d8a3f98..eb34f546b57f 100644
@@ -8,7 +8,7 @@
 #include "btrfs.h"
 #include <malloc.h>
 
-u64 btrfs_lookup_inode_ref(struct __btrfs_root *root, u64 inr,
+u64 __btrfs_lookup_inode_ref(struct __btrfs_root *root, u64 inr,
 			   struct btrfs_inode_ref *refp, char *name)
 {
 	struct __btrfs_path path;
@@ -44,7 +44,7 @@ out:
 	return res;
 }
 
-int btrfs_lookup_inode(const struct __btrfs_root *root,
+int __btrfs_lookup_inode(const struct __btrfs_root *root,
 		       struct btrfs_key *location,
 		       struct btrfs_inode_item *item,
 		       struct __btrfs_root *new_root)
@@ -83,7 +83,7 @@ out:
 	return res;
 }
 
-int btrfs_readlink(const struct __btrfs_root *root, u64 inr, char *target)
+int __btrfs_readlink(const struct __btrfs_root *root, u64 inr, char *target)
 {
 	struct __btrfs_path path;
 	struct btrfs_key key;
@@ -137,7 +137,7 @@ out:
 
 /* inr must be a directory (for regular files with multiple hard links this
    function returns only one of the parents of the file) */
-static u64 get_parent_inode(struct __btrfs_root *root, u64 inr,
+static u64 __get_parent_inode(struct __btrfs_root *root, u64 inr,
 			    struct btrfs_inode_item *inode_item)
 {
 	struct btrfs_key key;
@@ -164,14 +164,14 @@ static u64 get_parent_inode(struct __btrfs_root *root, u64 inr,
 			key.type = BTRFS_INODE_ITEM_KEY;
 			key.offset = 0;
 
-			if (btrfs_lookup_inode(root, &key, inode_item, NULL))
+			if (__btrfs_lookup_inode(root, &key, inode_item, NULL))
 				return -1ULL;
 		}
 
 		return inr;
 	}
 
-	res = btrfs_lookup_inode_ref(root, inr, NULL, NULL);
+	res = __btrfs_lookup_inode_ref(root, inr, NULL, NULL);
 	if (res == -1ULL)
 		return -1ULL;
 
@@ -180,7 +180,7 @@ static u64 get_parent_inode(struct __btrfs_root *root, u64 inr,
 		key.type = BTRFS_INODE_ITEM_KEY;
 		key.offset = 0;
 
-		if (btrfs_lookup_inode(root, &key, inode_item, NULL))
+		if (__btrfs_lookup_inode(root, &key, inode_item, NULL))
 			return -1ULL;
 	}
 
@@ -209,7 +209,7 @@ static inline const char *skip_current_directories(const char *cur)
 	return cur;
 }
 
-u64 btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
+u64 __btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 		      u8 *type_p, struct btrfs_inode_item *inode_item_p,
 		      int symlink_limit)
 {
@@ -239,7 +239,7 @@ u64 btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 
 		if (len == 2 && cur[0] == '.' && cur[1] == '.') {
 			cur += 2;
-			inr = get_parent_inode(root, inr, &inode_item);
+			inr = __get_parent_inode(root, inr, &inode_item);
 			if (inr == -1ULL)
 				return -1ULL;
 
@@ -250,12 +250,12 @@ u64 btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 		if (!*cur)
 			break;
 		
-		if (btrfs_lookup_dir_item(root, inr, cur, len, &item))
+		if (__btrfs_lookup_dir_item(root, inr, cur, len, &item))
 			return -1ULL;
 
 		type = item.type;
 		have_inode = 1;
-		if (btrfs_lookup_inode(root, (struct btrfs_key *)&item.location,
+		if (__btrfs_lookup_inode(root, (struct btrfs_key *)&item.location,
 					&inode_item, root))
 			return -1ULL;
 
@@ -272,13 +272,13 @@ u64 btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 			if (!target)
 				return -1ULL;
 
-			if (btrfs_readlink(root, item.location.objectid,
+			if (__btrfs_readlink(root, item.location.objectid,
 					   target)) {
 				free(target);
 				return -1ULL;
 			}
 
-			inr = btrfs_lookup_path(root, inr, target, &type,
+			inr = __btrfs_lookup_path(root, inr, target, &type,
 						&inode_item, symlink_limit - 1);
 
 			free(target);
@@ -307,7 +307,7 @@ u64 btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 			key.type = BTRFS_INODE_ITEM_KEY;
 			key.offset = 0;
 
-			if (btrfs_lookup_inode(root, &key, &inode_item, NULL))
+			if (__btrfs_lookup_inode(root, &key, &inode_item, NULL))
 				return -1ULL;
 		}
 
fs/btrfs/subvolume.c +2 -2
index 72b847b940da..0e72577d0df5 100644
@@ -39,7 +39,7 @@ static int get_subvol_name(u64 subvolid, char *name, int max_len)
 		dir = rref.dirid;
 
 		while (dir != BTRFS_FIRST_FREE_OBJECTID) {
-			dir = btrfs_lookup_inode_ref(&root, dir, &iref, tmp);
+			dir = __btrfs_lookup_inode_ref(&root, dir, &iref, tmp);
 
 			if (dir == -1ULL)
 				return -1;
@@ -71,7 +71,7 @@ u64 btrfs_get_default_subvol_objectid(void)
 {
 	struct btrfs_dir_item item;
 
-	if (btrfs_lookup_dir_item(&btrfs_info.tree_root,
+	if (__btrfs_lookup_dir_item(&btrfs_info.tree_root,
 				  btrfs_info.sb.root_dir_objectid, "default", 7,
 				  &item))
 		return BTRFS_FS_TREE_OBJECTID;
Signature · 1 line
2.26.0
author
#19 lore raw

PATCH 17/26 fs: btrfs: Use btrfs_readlink() to implement __btrfs_readlink()

In reply to Qu Wenruo

The existing __btrfs_readlink() can be easily re-implemented using the extent buffer based btrfs_readlink().

This should be the first step to re-implement u-boot btrfs code.

---
 fs/btrfs/btrfs.h |   1 +
 fs/btrfs/inode.c | 100 +++++++++++++++++++++++++++++------------------
 2 files changed, 64 insertions(+), 37 deletions(-)
fs/btrfs/btrfs.h +1 -0
index e561d27944fc..9057f2476526 100644
@@ -60,6 +60,7 @@ u64 __btrfs_lookup_inode_ref(struct __btrfs_root *, u64, struct btrfs_inode_ref
 int __btrfs_lookup_inode(const struct __btrfs_root *, struct btrfs_key *,
 		        struct btrfs_inode_item *, struct __btrfs_root *);
 int __btrfs_readlink(const struct __btrfs_root *, u64, char *);
+int btrfs_readlink(struct btrfs_root *root, u64 ino, char *target);
 u64 __btrfs_lookup_path(struct __btrfs_root *, u64, const char *, u8 *,
 		       struct btrfs_inode_item *, int);
 u64 btrfs_file_read(const struct __btrfs_root *, u64, u64, u64, char *);
fs/btrfs/inode.c +63 -37

127 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#20 lore raw

PATCH 18/26 fs: btrfs: Implement btrfs_lookup_path()

In reply to Qu Wenruo

This is the extent buffer based path lookup routine.

To implement this, btrfs_lookup_dir_item() is cross ported from btrfs-progs, and implement btrfs_lookup_path() from scratch.

Unlike the existing __btrfs_lookup_path(), since btrfs_read_fs_root() will check whether a root is orphan at read time, there is no need to check root backref, this make the code a little easier to read.

---
 fs/btrfs/ctree.h    |   4 +
 fs/btrfs/dir-item.c | 106 +++++++++++++++++++
 fs/btrfs/inode.c    | 245 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 355 insertions(+)
fs/btrfs/ctree.h +4 -0
index f702eaff293c..78f7746d1f32 100644
@@ -1284,6 +1284,10 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
 					     struct btrfs_path *path, u64 dir,
 					     const char *name, int name_len,
 					     int mod);
+/* inode.c */
+int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
+			struct btrfs_root **root_ret, u64 *ino_ret,
+			u8 *type_ret, int symlink_limit);
 
 /* ctree.c */
 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
fs/btrfs/dir-item.c +106 -0

112 lines not on this page · raw message

fs/btrfs/inode.c +245 -0

257 lines not on this page · raw message

Signature · 1 line
2.26.0
#21 lore raw

PATCH 18/26 fs: btrfs: Implement btrfs_lookup_path()

Quoted 352 lines · Qu Wenruo

This is the extent buffer based path lookup routine.

To implement this, btrfs_lookup_dir_item() is cross ported from btrfs-progs, and implement btrfs_lookup_path() from scratch.

Unlike the existing __btrfs_lookup_path(), since btrfs_read_fs_root()
will check whether a root is orphan at read time, there is no need to
check root backref, this make the code a little easier to read.

---
 fs/btrfs/ctree.h    |   4 +
 fs/btrfs/dir-item.c | 106 +++++++++++++++++++
 fs/btrfs/inode.c    | 245
 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 355 insertions(+)
fs/btrfs/ctree.h +4 -0
index f702eaff293c..78f7746d1f32 100644
@@ -1284,6 +1284,10 @@ struct btrfs_dir_item
 *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
 					     struct btrfs_path *path, u64 dir,
 					     const char *name, int name_len,
 					     int mod);
+/* inode.c */
+int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const
 char *filename,
+			struct btrfs_root **root_ret, u64 *ino_ret,
+			u8 *type_ret, int symlink_limit);
 

/* ctree.c */
int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);

fs/btrfs/dir-item.c +59 -0
index aea621c72bb3..4bf45c2fa925 100644
@@ -8,6 +8,112 @@
 #include "btrfs.h"
 #include "disk-io.h"
 
+static int verify_dir_item(struct btrfs_root *root,
+		    struct extent_buffer *leaf,
+		    struct btrfs_dir_item *dir_item)
+{
+	u16 namelen = BTRFS_NAME_LEN;
+	u8 type = btrfs_dir_type(leaf, dir_item);
+
+	if (type == BTRFS_FT_XATTR)
+		namelen = XATTR_NAME_MAX;
+
+	if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
+		fprintf(stderr, "invalid dir item name len: %u\n",
+		       (unsigned)btrfs_dir_data_len(leaf, dir_item));
+		return 1;
+	}
+
+	/* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
+	if ((btrfs_dir_data_len(leaf, dir_item) +
+	     btrfs_dir_name_len(leaf, dir_item)) >
+			BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
+		fprintf(stderr, "invalid dir item name + data len: %u +
 %u\n",
+		       (unsigned)btrfs_dir_name_len(leaf, dir_item),
+		       (unsigned)btrfs_dir_data_len(leaf, dir_item));
+		return 1;
+	}
+
+	return 0;
+}
+
+struct btrfs_dir_item *btrfs_match_dir_item_name(struct
 btrfs_root *root,
+			      struct btrfs_path *path,
+			      const char *name, int name_len)
+{
+	struct btrfs_dir_item *dir_item;
+	unsigned long name_ptr;
+	u32 total_len;
+	u32 cur = 0;
+	u32 this_len;
+	struct extent_buffer *leaf;
+
+	leaf = path->nodes[0];
+	dir_item = btrfs_item_ptr(leaf, path->slots[0], struct
 btrfs_dir_item);
+	total_len = btrfs_item_size_nr(leaf, path->slots[0]);
+	if (verify_dir_item(root, leaf, dir_item))
+		return NULL;
+
+	while(cur < total_len) {
+		this_len = sizeof(*dir_item) +
+			btrfs_dir_name_len(leaf, dir_item) +
+			btrfs_dir_data_len(leaf, dir_item);
+		if (this_len > (total_len - cur)) {
+			fprintf(stderr, "invalid dir item size\n");
+			return NULL;
+		}
+
+		name_ptr = (unsigned long)(dir_item + 1);
+
+		if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
+		    memcmp_extent_buffer(leaf, name, name_ptr, name_len)

== 0)

fs/btrfs/dir-item.c +10 -0
+			return dir_item;
+
+		cur += this_len;
+		dir_item = (struct btrfs_dir_item *)((char *)dir_item +
+						     this_len);
+	}
+	return NULL;
+}
+
+struct btrfs_dir_item *btrfs_lookup_dir_item(struct

btrfs_trans_handle *trans,

fs/btrfs/dir-item.c +18 -0
+					     struct btrfs_root *root,
+					     struct btrfs_path *path, u64 dir,
+					     const char *name, int name_len,
+					     int mod)
+{
+	int ret;
+	struct btrfs_key key;
+	int ins_len = mod < 0 ? -1 : 0;
+	int cow = mod != 0;
+	struct btrfs_key found_key;
+	struct extent_buffer *leaf;
+
+	key.objectid = dir;
+	key.type = BTRFS_DIR_ITEM_KEY;
+
+	key.offset = btrfs_name_hash(name, name_len);
+
+	ret = btrfs_search_slot(trans, root, &key, path, ins_len,

cow);

fs/btrfs/dir-item.c +19 -0
+	if (ret < 0)
+		return ERR_PTR(ret);
+	if (ret > 0) {
+		if (path->slots[0] == 0)
+			return NULL;
+		path->slots[0]--;
+	}
+
+	leaf = path->nodes[0];
+	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+	if (found_key.objectid != dir ||
+	    found_key.type != BTRFS_DIR_ITEM_KEY ||
+	    found_key.offset != key.offset)
+		return NULL;
+
+	return btrfs_match_dir_item_name(root, path, name, name_len);
+}
+
 static int __verify_dir_item(struct btrfs_dir_item *item, u32
 start, u32 total)
 {
 	u16 max_len = BTRFS_NAME_LEN;
fs/btrfs/inode.c +45 -0
index df2f6590bb40..af4f30bbd50c 100644
@@ -161,6 +161,115 @@ int __btrfs_readlink(const struct
 __btrfs_root *root, u64 inr, char *target)
 	return 0;
 }
 
+static int lookup_root_ref(struct btrfs_fs_info *fs_info,
+			   u64 rootid, u64 *root_ret, u64 *dir_ret)
+{
+	struct btrfs_root *root = fs_info->tree_root;
+	struct btrfs_root_ref *root_ref;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	btrfs_init_path(&path);
+	key.objectid = rootid;
+	key.type = BTRFS_ROOT_BACKREF_KEY;
+	key.offset = (u64)-1;
+
+	ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
+	if (ret < 0)
+		return ret;
+	/* Should not happen */
+	if (ret == 0) {
+		ret = -EUCLEAN;
+		goto out;
+	}
+	ret = btrfs_previous_item(root, &path, rootid,
 BTRFS_ROOT_BACKREF_KEY);
+	if (ret < 0)
+		goto out;
+	if (ret > 0) {
+		ret = -ENOENT;
+		goto out;
+	}
+	btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+	root_ref = btrfs_item_ptr(path.nodes[0], path.slots[0],
+				  struct btrfs_root_ref);
+	*root_ret = key.offset;
+	*dir_ret = btrfs_root_ref_dirid(path.nodes[0], root_ref);
+out:
+	btrfs_release_path(&path);
+	return ret;
+}
+
+/*
+ * To get the parent inode of @ino of @root.
+ *
+ * @root_ret and @ino_ret will be filled.
+ *
+ * NOTE: This function is not reliable. It can only get one

parent inode.
+ * The get the proper parent inode, we need a full VFS inodes stack to
+ * resolve properly.
+ */

fs/btrfs/inode.c +47 -0
+static int get_parent_inode(struct btrfs_root *root, u64 ino,
+			    struct btrfs_root **root_ret, u64 *ino_ret)
+{
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	if (ino == BTRFS_FIRST_FREE_OBJECTID) {
+		u64 parent_root;
+
+		/* It's top level already, no more parent */
+		if (root->root_key.objectid == BTRFS_FS_TREE_OBJECTID) {
+			*root_ret = fs_info->fs_root;
+			*ino_ret = BTRFS_FIRST_FREE_OBJECTID;
+			return 0;
+		}
+
+		ret = lookup_root_ref(fs_info, root->root_key.objectid,
+				      &parent_root, ino_ret);
+		if (ret < 0)
+			return ret;
+
+		key.objectid = parent_root;
+		key.type = BTRFS_ROOT_ITEM_KEY;
+		key.offset = (u64)-1;
+		*root_ret = btrfs_read_fs_root(fs_info, &key);
+		if (IS_ERR(*root_ret))
+			return PTR_ERR(*root_ret);
+
+		return 0;
+	}
+
+	btrfs_init_path(&path);
+	key.objectid = ino;
+	key.type = BTRFS_INODE_REF_KEY;
+	key.offset = (u64)-1;
+
+	ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
+	if (ret < 0)
+		return ret;
+	/* Should not happen */
+	if (ret == 0) {
+		ret = -EUCLEAN;
+		goto out;
+	}
+	ret = btrfs_previous_item(root, &path, ino,

BTRFS_INODE_REF_KEY);

fs/btrfs/inode.c +49 -0
+	if (ret < 0)
+		goto out;
+	if (ret > 0) {
+		ret = -ENOENT;
+		goto out;
+	}
+	btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+	*root_ret = root;
+	*ino_ret = key.offset;
+out:
+	btrfs_release_path(&path);
+	return ret;
+}
+
 /* inr must be a directory (for regular files with multiple
 hard links this
    function returns only one of the parents of the file) */
 static u64 __get_parent_inode(struct __btrfs_root *root, u64
 inr,
@@ -235,6 +344,142 @@ static inline const char
 *skip_current_directories(const char *cur)
 	return cur;
 }
 
+/*
+ * Resolve one filename of @ino of @root.
+ *
+ * key_ret:	The child key (either INODE_ITEM or ROOT_ITEM
 type)
+ * type_ret:	BTRFS_FT_* of the child inode.
+ *
+ * Return 0 with above members filled.
+ * Return <0 for error.
+ */
+static int resolve_one_filename(struct btrfs_root *root, u64
 ino,
+				const char *name, int namelen,
+				struct btrfs_key *key_ret, u8 *type_ret)
+{
+	struct btrfs_dir_item *dir_item;
+	struct btrfs_path path;
+	int ret = 0;
+
+	btrfs_init_path(&path);
+
+	dir_item = btrfs_lookup_dir_item(NULL, root, &path, ino, name,
+					 namelen, 0);
+	if (IS_ERR(dir_item)) {
+		ret = PTR_ERR(dir_item);
+		goto out;
+	}
+
+	btrfs_dir_item_key_to_cpu(path.nodes[0], dir_item, key_ret);
+	*type_ret = btrfs_dir_type(path.nodes[0], dir_item);
+out:
+	btrfs_release_path(&path);
+	return ret;
+}
+
+/*
+ * Resolve a full path @filename. The start point is @ino of

@root.
+ *
+ * The result will be filled into @root_ret, @ino_ret and @type_ret.
+ */
+int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,

fs/btrfs/inode.c +15 -0
+			struct btrfs_root **root_ret, u64 *ino_ret,
+			u8 *type_ret, int symlink_limit)
+{
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	struct btrfs_root *next_root;
+	struct btrfs_key key;
+	const char *cur = filename;
+	u64 next_ino;
+	u8 next_type;
+	u8 type;
+	int len;
+	int ret = 0;
+
+	/* If the path is absolute path, also search from fs root */
+	if (*cur == '/') {

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 410

Quoted 10 lines
fs/btrfs/inode.c +10 -0
+		root = fs_info->fs_root;
+		ino = btrfs_root_dirid(&root->root_item);
+		type = BTRFS_FT_DIR;
+	}
+
+	while (*cur != '\0') {
+
+		cur = skip_current_directories(cur);
+		len = next_length(cur);
+		if (len > BTRFS_NAME_LEN) {

next_length() promises @len <= BTRFS_NAME_LEN, so the check is trivial.

Quoted 40 lines
fs/btrfs/inode.c +9 -0
+			printf("%s: Name too long at \"%.*s\"\n", __func__,
+			       BTRFS_NAME_LEN, cur);
+			return -ENAMETOOLONG;
+		}
+		if (len == 1 && cur[0] == '.')
+			break;
+		/* Go one level up */
+		if (len == 2 && cur[0] == '.' && cur[1] == '.') {
+			ret = get_parent_inode(root, ino, &next_root,

&next_ino);

fs/btrfs/inode.c +10 -0
+			if (ret < 0)
+				return ret;
+			root = next_root;
+			ino = next_ino;
+			goto next;
+		}
+		if (!*cur)
+			break;
+
+		ret = resolve_one_filename(root, ino, cur, len, &key,

&type);

fs/btrfs/inode.c +19 -0
+		if (ret < 0)
+			return ret;
+		/* Child inode is a subvolume */
+		if (key.type == BTRFS_ROOT_ITEM_KEY) {
+
+			next_root = btrfs_read_fs_root(fs_info, &key);
+			if (IS_ERR(next_root))
+				return PTR_ERR(next_root);
+			root = next_root;
+			ino = btrfs_root_dirid(&root->root_item);
+			goto next;
+		}
+		/* Child inode is a symlink */
+		if (type == BTRFS_FT_SYMLINK && symlink_limit >= 0) {
+			char *target;
+
+			if (symlink_limit == 0) {
+				printf("%s: Too much symlinks!\n", __func__);
+				return -EMLINK;

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 458

Quoted 10 lines
fs/btrfs/inode.c +10 -0
+			}
+			target = malloc(fs_info->sectorsize);
+			if (!target)
+				return -ENOMEM;
+			ret = btrfs_readlink(root, ino, target);
+			if (ret < 0) {
+				free(target);
+				return ret;
+			}
+			target[ret] = '\0';

It was done in btrfs_readlink() already.

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 462

Quoted 4 lines
fs/btrfs/inode.c +4 -0
+
+			ret = btrfs_lookup_path(root, ino, target, &next_root,
+						&next_ino, &next_type,
+						symlink_limit);

Just notify gentlely this is a recursive call here. I don't know whether uboot cares about stack things. But, recursion makes coding simpler :).

--
Su

Quoted 24 lines
fs/btrfs/inode.c +20 -0
+			if (ret < 0)
+				return ret;
+			root = next_root;
+			ino = next_ino;
+			type = next_type;
+			goto next;
+		}
+		/* Child inode is an inode */
+		ino = key.objectid;
+next:
+		cur += len;
+	}
+	if (!ret) {
+		*root_ret = root;
+		*ino_ret = ino;
+		*type_ret = type;
+	}
+	return ret;
+}
+
 u64 __btrfs_lookup_path(struct __btrfs_root *root, u64 inr,
 const char *path,
 		      u8 *type_p, struct btrfs_inode_item *inode_item_p,
 		      int symlink_limit)
#22 lore raw

Re: [PATCH U-BOOT 18/26] fs: btrfs: Implement btrfs_lookup_path()

In reply to Qu Wenruo
Quoted 326 lines · Qu Wenruo

This is the extent buffer based path lookup routine.

To implement this, btrfs_lookup_dir_item() is cross ported from btrfs-progs, and implement btrfs_lookup_path() from scratch.

Unlike the existing __btrfs_lookup_path(), since btrfs_read_fs_root() will check whether a root is orphan at read time, there is no need to check root backref, this make the code a little easier to read.

---
 fs/btrfs/ctree.h    |   4 +
 fs/btrfs/dir-item.c | 106 +++++++++++++++++++
 fs/btrfs/inode.c    | 245 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 355 insertions(+)
fs/btrfs/ctree.h +4 -0
index f702eaff293c..78f7746d1f32 100644
@@ -1284,6 +1284,10 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
 					     struct btrfs_path *path, u64 dir,
 					     const char *name, int name_len,
 					     int mod);
+/* inode.c */
+int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
+			struct btrfs_root **root_ret, u64 *ino_ret,
+			u8 *type_ret, int symlink_limit);
 
 /* ctree.c */
 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
fs/btrfs/dir-item.c +106 -0
index aea621c72bb3..4bf45c2fa925 100644
@@ -8,6 +8,112 @@
 #include "btrfs.h"
 #include "disk-io.h"
 
+static int verify_dir_item(struct btrfs_root *root,
+		    struct extent_buffer *leaf,
+		    struct btrfs_dir_item *dir_item)
+{
+	u16 namelen = BTRFS_NAME_LEN;
+	u8 type = btrfs_dir_type(leaf, dir_item);
+
+	if (type == BTRFS_FT_XATTR)
+		namelen = XATTR_NAME_MAX;
+
+	if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
+		fprintf(stderr, "invalid dir item name len: %u\n",
+		       (unsigned)btrfs_dir_data_len(leaf, dir_item));
+		return 1;
+	}
+
+	/* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
+	if ((btrfs_dir_data_len(leaf, dir_item) +
+	     btrfs_dir_name_len(leaf, dir_item)) >
+			BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
+		fprintf(stderr, "invalid dir item name + data len: %u + %u\n",
+		       (unsigned)btrfs_dir_name_len(leaf, dir_item),
+		       (unsigned)btrfs_dir_data_len(leaf, dir_item));
+		return 1;
+	}
+
+	return 0;
+}
+
+struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
+			      struct btrfs_path *path,
+			      const char *name, int name_len)
+{
+	struct btrfs_dir_item *dir_item;
+	unsigned long name_ptr;
+	u32 total_len;
+	u32 cur = 0;
+	u32 this_len;
+	struct extent_buffer *leaf;
+
+	leaf = path->nodes[0];
+	dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
+	total_len = btrfs_item_size_nr(leaf, path->slots[0]);
+	if (verify_dir_item(root, leaf, dir_item))
+		return NULL;
+
+	while(cur < total_len) {
+		this_len = sizeof(*dir_item) +
+			btrfs_dir_name_len(leaf, dir_item) +
+			btrfs_dir_data_len(leaf, dir_item);
+		if (this_len > (total_len - cur)) {
+			fprintf(stderr, "invalid dir item size\n");
+			return NULL;
+		}
+
+		name_ptr = (unsigned long)(dir_item + 1);
+
+		if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
+		    memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
+			return dir_item;
+
+		cur += this_len;
+		dir_item = (struct btrfs_dir_item *)((char *)dir_item +
+						     this_len);
+	}
+	return NULL;
+}
+
+struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
+					     struct btrfs_root *root,
+					     struct btrfs_path *path, u64 dir,
+					     const char *name, int name_len,
+					     int mod)
+{
+	int ret;
+	struct btrfs_key key;
+	int ins_len = mod < 0 ? -1 : 0;
+	int cow = mod != 0;
+	struct btrfs_key found_key;
+	struct extent_buffer *leaf;
+
+	key.objectid = dir;
+	key.type = BTRFS_DIR_ITEM_KEY;
+
+	key.offset = btrfs_name_hash(name, name_len);
+
+	ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
+	if (ret < 0)
+		return ERR_PTR(ret);
+	if (ret > 0) {
+		if (path->slots[0] == 0)
+			return NULL;
+		path->slots[0]--;
+	}
+
+	leaf = path->nodes[0];
+	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+	if (found_key.objectid != dir ||
+	    found_key.type != BTRFS_DIR_ITEM_KEY ||
+	    found_key.offset != key.offset)
+		return NULL;
+
+	return btrfs_match_dir_item_name(root, path, name, name_len);
+}
+
 static int __verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total)
 {
 	u16 max_len = BTRFS_NAME_LEN;
fs/btrfs/inode.c +163 -0
index df2f6590bb40..af4f30bbd50c 100644
@@ -161,6 +161,115 @@ int __btrfs_readlink(const struct __btrfs_root *root, u64 inr, char *target)
 	return 0;
 }
 
+static int lookup_root_ref(struct btrfs_fs_info *fs_info,
+			   u64 rootid, u64 *root_ret, u64 *dir_ret)
+{
+	struct btrfs_root *root = fs_info->tree_root;
+	struct btrfs_root_ref *root_ref;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	btrfs_init_path(&path);
+	key.objectid = rootid;
+	key.type = BTRFS_ROOT_BACKREF_KEY;
+	key.offset = (u64)-1;
+
+	ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
+	if (ret < 0)
+		return ret;
+	/* Should not happen */
+	if (ret == 0) {
+		ret = -EUCLEAN;
+		goto out;
+	}
+	ret = btrfs_previous_item(root, &path, rootid, BTRFS_ROOT_BACKREF_KEY);
+	if (ret < 0)
+		goto out;
+	if (ret > 0) {
+		ret = -ENOENT;
+		goto out;
+	}
+	btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+	root_ref = btrfs_item_ptr(path.nodes[0], path.slots[0],
+				  struct btrfs_root_ref);
+	*root_ret = key.offset;
+	*dir_ret = btrfs_root_ref_dirid(path.nodes[0], root_ref);
+out:
+	btrfs_release_path(&path);
+	return ret;
+}
+
+/*
+ * To get the parent inode of @ino of @root.
+ *
+ * @root_ret and @ino_ret will be filled.
+ *
+ * NOTE: This function is not reliable. It can only get one parent inode.
+ * The get the proper parent inode, we need a full VFS inodes stack to
+ * resolve properly.
+ */
+static int get_parent_inode(struct btrfs_root *root, u64 ino,
+			    struct btrfs_root **root_ret, u64 *ino_ret)
+{
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	if (ino == BTRFS_FIRST_FREE_OBJECTID) {
+		u64 parent_root;
+
+		/* It's top level already, no more parent */
+		if (root->root_key.objectid == BTRFS_FS_TREE_OBJECTID) {
+			*root_ret = fs_info->fs_root;
+			*ino_ret = BTRFS_FIRST_FREE_OBJECTID;
+			return 0;
+		}
+
+		ret = lookup_root_ref(fs_info, root->root_key.objectid,
+				      &parent_root, ino_ret);
+		if (ret < 0)
+			return ret;
+
+		key.objectid = parent_root;
+		key.type = BTRFS_ROOT_ITEM_KEY;
+		key.offset = (u64)-1;
+		*root_ret = btrfs_read_fs_root(fs_info, &key);
+		if (IS_ERR(*root_ret))
+			return PTR_ERR(*root_ret);
+
+		return 0;
+	}
+
+	btrfs_init_path(&path);
+	key.objectid = ino;
+	key.type = BTRFS_INODE_REF_KEY;
+	key.offset = (u64)-1;
+
+	ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
+	if (ret < 0)
+		return ret;
+	/* Should not happen */
+	if (ret == 0) {
+		ret = -EUCLEAN;
+		goto out;
+	}
+	ret = btrfs_previous_item(root, &path, ino, BTRFS_INODE_REF_KEY);
+	if (ret < 0)
+		goto out;
+	if (ret > 0) {
+		ret = -ENOENT;
+		goto out;
+	}
+	btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+	*root_ret = root;
+	*ino_ret = key.offset;
+out:
+	btrfs_release_path(&path);
+	return ret;
+}
+
 /* inr must be a directory (for regular files with multiple hard links this
    function returns only one of the parents of the file) */
 static u64 __get_parent_inode(struct __btrfs_root *root, u64 inr,
@@ -235,6 +344,142 @@ static inline const char *skip_current_directories(const char *cur)
 	return cur;
 }
 
+/*
+ * Resolve one filename of @ino of @root.
+ *
+ * key_ret:	The child key (either INODE_ITEM or ROOT_ITEM type)
+ * type_ret:	BTRFS_FT_* of the child inode.
+ *
+ * Return 0 with above members filled.
+ * Return <0 for error.
+ */
+static int resolve_one_filename(struct btrfs_root *root, u64 ino,
+				const char *name, int namelen,
+				struct btrfs_key *key_ret, u8 *type_ret)
+{
+	struct btrfs_dir_item *dir_item;
+	struct btrfs_path path;
+	int ret = 0;
+
+	btrfs_init_path(&path);
+
+	dir_item = btrfs_lookup_dir_item(NULL, root, &path, ino, name,
+					 namelen, 0);
+	if (IS_ERR(dir_item)) {
+		ret = PTR_ERR(dir_item);
+		goto out;
+	}
+
+	btrfs_dir_item_key_to_cpu(path.nodes[0], dir_item, key_ret);
+	*type_ret = btrfs_dir_type(path.nodes[0], dir_item);
+out:
+	btrfs_release_path(&path);
+	return ret;
+}
+
+/*
+ * Resolve a full path @filename. The start point is @ino of @root.
+ *
+ * The result will be filled into @root_ret, @ino_ret and @type_ret.
+ */
+int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
+			struct btrfs_root **root_ret, u64 *ino_ret,
+			u8 *type_ret, int symlink_limit)
+{
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	struct btrfs_root *next_root;
+	struct btrfs_key key;
+	const char *cur = filename;
+	u64 next_ino;
+	u8 next_type;
+	u8 type;
+	int len;
+	int ret = 0;
+
+	/* If the path is absolute path, also search from fs root */
+	if (*cur == '/') {

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 410

Quoted 10 lines
fs/btrfs/inode.c +10 -0
+		root = fs_info->fs_root;
+		ino = btrfs_root_dirid(&root->root_item);
+		type = BTRFS_FT_DIR;
+	}
+
+	while (*cur != '\0') {
+
+		cur = skip_current_directories(cur);
+		len = next_length(cur);
+		if (len > BTRFS_NAME_LEN) {

next_length() promises @len <= BTRFS_NAME_LEN, so the check is trivial.

Quoted 38 lines
fs/btrfs/inode.c +38 -0
+			printf("%s: Name too long at \"%.*s\"\n", __func__,
+			       BTRFS_NAME_LEN, cur);
+			return -ENAMETOOLONG;
+		}
+		if (len == 1 && cur[0] == '.')
+			break;
+		/* Go one level up */
+		if (len == 2 && cur[0] == '.' && cur[1] == '.') {
+			ret = get_parent_inode(root, ino, &next_root, &next_ino);
+			if (ret < 0)
+				return ret;
+			root = next_root;
+			ino = next_ino;
+			goto next;
+		}
+		if (!*cur)
+			break;
+
+		ret = resolve_one_filename(root, ino, cur, len, &key, &type);
+		if (ret < 0)
+			return ret;
+		/* Child inode is a subvolume */
+		if (key.type == BTRFS_ROOT_ITEM_KEY) {
+
+			next_root = btrfs_read_fs_root(fs_info, &key);
+			if (IS_ERR(next_root))
+				return PTR_ERR(next_root);
+			root = next_root;
+			ino = btrfs_root_dirid(&root->root_item);
+			goto next;
+		}
+		/* Child inode is a symlink */
+		if (type == BTRFS_FT_SYMLINK && symlink_limit >= 0) {
+			char *target;
+
+			if (symlink_limit == 0) {
+				printf("%s: Too much symlinks!\n", __func__);
+				return -EMLINK;

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 458

Quoted 10 lines
fs/btrfs/inode.c +10 -0
+			}
+			target = malloc(fs_info->sectorsize);
+			if (!target)
+				return -ENOMEM;
+			ret = btrfs_readlink(root, ino, target);
+			if (ret < 0) {
+				free(target);
+				return ret;
+			}
+			target[ret] = '\0';

It was done in btrfs_readlink() already.

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 462

Quoted 4 lines
fs/btrfs/inode.c +4 -0
+
+			ret = btrfs_lookup_path(root, ino, target, &next_root,
+						&next_ino, &next_type,
+						symlink_limit);

Just notify gentlely this is a recursive call here. I don't know whether uboot cares about stack things. But, recursion makes coding simpler :).

--
Su

Quoted 23 lines
fs/btrfs/inode.c +20 -0
+			if (ret < 0)
+				return ret;
+			root = next_root;
+			ino = next_ino;
+			type = next_type;
+			goto next;
+		}
+		/* Child inode is an inode */
+		ino = key.objectid;
+next:
+		cur += len;
+	}
+	if (!ret) {
+		*root_ret = root;
+		*ino_ret = ino;
+		*type_ret = type;
+	}
+	return ret;
+}
+
 u64 __btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 		      u8 *type_p, struct btrfs_inode_item *inode_item_p,
 		      int symlink_limit)
#23 lore raw

Re: [PATCH U-BOOT 18/26] fs: btrfs: Implement btrfs_lookup_path()

Quoted 7 lines · On Wed, 22 Apr 2020 17:46:25 +0800 Su Yue <[email protected]> wrote:

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 410

Quoted 5 lines
fs/btrfs/inode.c +5 -0
+	while (*cur != '\0') {
+
+		cur = skip_current_directories(cur);
+		len = next_length(cur);
+		if (len > BTRFS_NAME_LEN) {  

next_length() promises @len <= BTRFS_NAME_LEN, so the check is trivial.

Hmm. This is a bug in next_length. I meant for next_length to return len > BTRFS_NAME_LEN in case of too long name. Thanks for noticing.

Quoted 8 lines

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 458

Quoted 6 lines
fs/btrfs/inode.c +6 -0
+			ret = btrfs_readlink(root, ino, target);
+			if (ret < 0) {
+				free(target);
+				return ret;
+			}
+			target[ret] = '\0';  

It was done in btrfs_readlink() already.

It is in old btrfs_readlink, but is it even after this patches? I don't see it in the new implementation.

Quoted 7 lines

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 462

Quoted 4 lines
fs/btrfs/inode.c +4 -0
+
+			ret = btrfs_lookup_path(root, ino, target, &next_root,
+						&next_ino, &next_type,
+						symlink_limit);  

Just notify gentlely this is a recursive call here. I don't know whether uboot cares about stack things. But, recursion makes coding simpler :).

It is limited by symlink_limit. Until somebody complains about stack issues I would like to keep it simple.

#24 lore raw

Re: [PATCH U-BOOT 18/26] fs: btrfs: Implement btrfs_lookup_path()

Quoted 9 lines · Marek Behun
Quoted 5 lines · On Wed, 22 Apr 2020 17:46:25 +0800 Su Yue <[email protected]> wrote:
Quoted 3 lines
(quoted diff) +1 -0
+	while (*cur != '\0') { + +		cur =

skip_current_directories(cur); + len =
next_length(cur); + if (len > BTRFS_NAME_LEN) {

next_length() promises @len <= BTRFS_NAME_LEN, so the check is
trivial.

Okay.

Quoted 4 lines

Hmm. This is a bug in next_length. I meant for next_length to return len > BTRFS_NAME_LEN in case of too long name. Thanks for noticing.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
(quoted diff) +1 -0
+			ret = btrfs_readlink(root, ino, target); +

if (ret < 0) { + free(target); +
return ret; + } + target[ret] = '\0';

It was done in btrfs_readlink() already.

It is in old btrfs_readlink, but is it even after this patches? I don't see it in the new implementation.

You are right. The thing changed in the 17th patch. btrfs_readlink()
doesn't set the null byte now.

--
Su

Quoted 10 lines
Quoted 7 lines

fs/btrfs/inode.c static inline const char *skip_current_directories(const char *cur) · line 462

Quoted 4 lines
fs/btrfs/inode.c +4 -0
+
+			ret = btrfs_lookup_path(root, ino, target, &next_root,
+						&next_ino, &next_type,
+						symlink_limit);

Just notify gentlely this is a recursive call here. I don't know whether uboot cares about stack things. But, recursion makes coding simpler :).

It is limited by symlink_limit. Until somebody complains about stack issues I would like to keep it simple.

#25 lore raw

Re: [PATCH U-BOOT 18/26] fs: btrfs: Implement btrfs_lookup_path()

In reply to Qu Wenruo
Quoted 199 lines · Qu Wenruo

This is the extent buffer based path lookup routine.

To implement this, btrfs_lookup_dir_item() is cross ported from btrfs-progs, and implement btrfs_lookup_path() from scratch.

Unlike the existing __btrfs_lookup_path(), since btrfs_read_fs_root() will check whether a root is orphan at read time, there is no need to check root backref, this make the code a little easier to read.

 fs/btrfs/ctree.h    |   4 + fs/btrfs/dir-item.c | 106
 +++++++++++++++++++ fs/btrfs/inode.c    | 245

++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 355 insertions(+)

fs/btrfs/ctree.h index renamed from fs/btrfs/ctree.h +0 -0

f702eaff293c..78f7746d1f32 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -1284,6 +1284,10 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,

fs/btrfs/ctree.h index renamed from fs/btrfs/ctree.h +1 -0
 					     struct btrfs_path *path, u64 dir, const
 char *name, int name_len, int mod);
+/* inode.c */ +int btrfs_lookup_path(struct btrfs_root *root,

u64 ino, const char *filename, + struct btrfs_root
**root_ret, u64 *ino_ret, + u8 *type_ret, int
symlink_limit);
/* ctree.c */ int btrfs_comp_cpu_keys(const struct btrfs_key
*k1, const struct btrfs_key *k2);

fs/btrfs/dir-item.c index renamed from fs/btrfs/dir-item.c +0 -0

aea621c72bb3..4bf45c2fa925 100644 --- a/fs/btrfs/dir-item.c +++ b/fs/btrfs/dir-item.c @@ -8,6 +8,112 @@
#include "btrfs.h" #include "disk-io.h"
+static int verify_dir_item(struct btrfs_root *root, +
struct extent_buffer *leaf, + struct btrfs_dir_item
*dir_item) +{ + u16 namelen = BTRFS_NAME_LEN; + u8 type =
btrfs_dir_type(leaf, dir_item); + + if (type ==
BTRFS_FT_XATTR) + namelen = XATTR_NAME_MAX; + + if
(btrfs_dir_name_len(leaf, dir_item) > namelen) { + fprintf(stderr, "invalid dir item name len: %u\n", +
(unsigned)btrfs_dir_data_len(leaf, dir_item)); + return 1;

fs/btrfs/dir-item.c index renamed from fs/btrfs/dir-item.c +1 -0
+	} + +	/* BTRFS_MAX_XATTR_SIZE is the same for all dir items

*/ + if ((btrfs_dir_data_len(leaf, dir_item) + +
btrfs_dir_name_len(leaf, dir_item)) > +
BTRFS_MAX_XATTR_SIZE(root->fs_info)) { + fprintf(stderr,
"invalid dir item name + data len: %u + %u\n", +
(unsigned)btrfs_dir_name_len(leaf, dir_item), +
(unsigned)btrfs_dir_data_len(leaf, dir_item)); + return 1;

fs/btrfs/dir-item.c index renamed from fs/btrfs/dir-item.c +1 -0
+	} + +	return 0; +} + +struct btrfs_dir_item

*btrfs_match_dir_item_name(struct btrfs_root *root, +
struct btrfs_path *path, + const char *name,
int name_len) +{ + struct btrfs_dir_item *dir_item; +
unsigned long name_ptr; + u32 total_len; + u32 cur = 0; +
u32 this_len; + struct extent_buffer *leaf; + + leaf =
path->nodes[0]; + dir_item = btrfs_item_ptr(leaf,
path->slots[0], struct btrfs_dir_item); + total_len =
btrfs_item_size_nr(leaf, path->slots[0]); + if
(verify_dir_item(root, leaf, dir_item)) + return NULL; + +
while(cur < total_len) { + this_len = sizeof(*dir_item) +

fs/btrfs/dir-item.c index renamed from fs/btrfs/dir-item.c +1 -0
+			btrfs_dir_name_len(leaf, dir_item) + +

btrfs_dir_data_len(leaf, dir_item); + if (this_len >
(total_len - cur)) { + fprintf(stderr, "invalid dir
item size\n"); + return NULL; + } + +
name_ptr = (unsigned long)(dir_item + 1); + + if
(btrfs_dir_name_len(leaf, dir_item) == name_len && + memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) +
return dir_item; + + cur += this_len; + dir_item =
(struct btrfs_dir_item *)((char *)dir_item + +
this_len); + } + return NULL; +} + +struct btrfs_dir_item
*btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, +
struct btrfs_root *root, + struct
btrfs_path *path, u64 dir, + const char
*name, int name_len, + int mod) +{ +
int ret; + struct btrfs_key key; + int ins_len = mod < 0 ? -1
: 0; + int cow = mod != 0; + struct btrfs_key found_key; +
struct extent_buffer *leaf; + + key.objectid = dir; +
key.type = BTRFS_DIR_ITEM_KEY; + + key.offset =
btrfs_name_hash(name, name_len); + + ret =
btrfs_search_slot(trans, root, &key, path, ins_len, cow); + if
(ret < 0) + return ERR_PTR(ret); + if (ret > 0) { +
if (path->slots[0] == 0) + return NULL; +
path->slots[0]--; + } + + leaf = path->nodes[0]; +
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); + + if
(found_key.objectid != dir || + found_key.type !=
BTRFS_DIR_ITEM_KEY || + found_key.offset != key.offset) +
return NULL; + + return btrfs_match_dir_item_name(root, path,
name, name_len); +} +
static int __verify_dir_item(struct btrfs_dir_item *item, u32 start, u32 total) { u16 max_len = BTRFS_NAME_LEN;

fs/btrfs/inode.c index renamed from fs/btrfs/inode.c +0 -0

df2f6590bb40..af4f30bbd50c 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -161,6 +161,115 @@ int __btrfs_readlink(const struct __btrfs_root *root, u64 inr, char *target)

fs/btrfs/inode.c index renamed from fs/btrfs/inode.c +1 -0
 	return 0; }
+static int lookup_root_ref(struct btrfs_fs_info *fs_info, +

u64 rootid, u64 *root_ret, u64 *dir_ret) +{ + struct btrfs_root
*root = fs_info->tree_root; + struct btrfs_root_ref *root_ref; +
struct btrfs_path path; + struct btrfs_key key; + int ret; + +
btrfs_init_path(&path); + key.objectid = rootid; + key.type =
BTRFS_ROOT_BACKREF_KEY; + key.offset = (u64)-1; + + ret =
btrfs_search_slot(NULL, root, &key, &path, 0, 0); + if (ret <
0) + return ret; + /* Should not happen */ + if (ret ==
0) { + ret = -EUCLEAN; + goto out; + } + ret =
btrfs_previous_item(root, &path, rootid,
BTRFS_ROOT_BACKREF_KEY); + if (ret < 0) + goto out; + if
(ret > 0) { + ret = -ENOENT; + goto out; + } +
btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + root_ref = btrfs_item_ptr(path.nodes[0], path.slots[0], +
struct btrfs_root_ref); + *root_ret = key.offset; + *dir_ret =
btrfs_root_ref_dirid(path.nodes[0], root_ref); +out: +
btrfs_release_path(&path); + return ret; +} + +/* + * To get
the parent inode of @ino of @root. + * + * @root_ret and @ino_ret will be filled. + * + * NOTE: This function is not reliable. It can only get one parent inode. + * The get the proper parent inode, we need a full VFS inodes stack to + * resolve properly. + */ +static int get_parent_inode(struct
btrfs_root *root, u64 ino, + struct btrfs_root
**root_ret, u64 *ino_ret) +{ + struct btrfs_fs_info *fs_info
= root->fs_info; + struct btrfs_path path; + struct
btrfs_key key; + int ret; + + if (ino ==
BTRFS_FIRST_FREE_OBJECTID) { + u64 parent_root; + +
/* It's top level already, no more parent */ + if
(root->root_key.objectid == BTRFS_FS_TREE_OBJECTID) { +
*root_ret = fs_info->fs_root; + *ino_ret =
BTRFS_FIRST_FREE_OBJECTID; + return 0; + } + +
ret = lookup_root_ref(fs_info, root->root_key.objectid, +
&parent_root, ino_ret); + if (ret < 0) + return
ret; + + key.objectid = parent_root; + key.type =
BTRFS_ROOT_ITEM_KEY; + key.offset = (u64)-1; +
*root_ret = btrfs_read_fs_root(fs_info, &key); + if
(IS_ERR(*root_ret)) + return PTR_ERR(*root_ret); + +
return 0; + } + + btrfs_init_path(&path); + key.objectid =
ino; + key.type = BTRFS_INODE_REF_KEY; + key.offset =
(u64)-1; + + ret = btrfs_search_slot(NULL, root, &key, &path,
0, 0); + if (ret < 0) + return ret; + /* Should not
happen */ + if (ret == 0) { + ret = -EUCLEAN; +
goto out; + } + ret = btrfs_previous_item(root, &path, ino,
BTRFS_INODE_REF_KEY); + if (ret < 0) + goto out; + if
(ret > 0) { + ret = -ENOENT; + goto out; + } +
btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); +
*root_ret = root; + *ino_ret = key.offset; +out: +
btrfs_release_path(&path); + return ret; +} +
/* inr must be a directory (for regular files with multiple hard links this
function returns only one of the parents of the file) */
static u64 __get_parent_inode(struct __btrfs_root *root, u64 inr,

fs/btrfs/inode.c index renamed from fs/btrfs/inode.c +1 -0
@@ -235,6 +344,142 @@ static inline const char
 *skip_current_directories(const char *cur)
 	return cur; }
+/* + * Resolve one filename of @ino of @root.  + * + * key_ret:

The child key (either INODE_ITEM or ROOT_ITEM type) + *
type_ret: BTRFS_FT_* of the child inode. + * + * Return 0 with
above members filled. + * Return <0 for error. + */ +static int resolve_one_filename(struct btrfs_root *root, u64 ino, +
const char *name, int namelen, + struct btrfs_key
*key_ret, u8 *type_ret) +{ + struct btrfs_dir_item *dir_item; +
struct btrfs_path path; + int ret = 0; + +
btrfs_init_path(&path); + + dir_item =
btrfs_lookup_dir_item(NULL, root, &path, ino, name, +
namelen, 0); + if (IS_ERR(dir_item)) { + ret =
PTR_ERR(dir_item); + goto out; + } + +
btrfs_dir_item_key_to_cpu(path.nodes[0], dir_item, key_ret); + *type_ret = btrfs_dir_type(path.nodes[0], dir_item); +out: +
btrfs_release_path(&path); + return ret; +} + +/* + * Resolve a
full path @filename. The start point is @ino of @root. + * + * The result will be filled into @root_ret, @ino_ret and @type_ret. + */ +int btrfs_lookup_path(struct btrfs_root *root,
u64 ino, const char *filename, + struct btrfs_root
**root_ret, u64 *ino_ret, + u8 *type_ret, int
symlink_limit) +{ + struct btrfs_fs_info *fs_info =
root->fs_info; + struct btrfs_root *next_root; + struct
btrfs_key key; + const char *cur = filename; + u64 next_ino;

fs/btrfs/inode.c index renamed from fs/btrfs/inode.c +1 -0
+	u8 next_type; +	u8 type; +	int len; +	int ret = 0; + +	/*

If the path is absolute path, also search from fs root */ + if
(*cur == '/') { + root = fs_info->fs_root; + ino =
btrfs_root_dirid(&root->root_item); + type = BTRFS_FT_DIR; +
} + + while (*cur != '\0') { + + cur =
skip_current_directories(cur); + len = next_length(cur); +
if (len > BTRFS_NAME_LEN) { + printf("%s: Name too long
at \"%.*s\"\n", __func__, + BTRFS_NAME_LEN,
cur); + return -ENAMETOOLONG; + } + if (len ==
1 && cur[0] == '.') + break; + /* Go one
level up */ + if (len == 2 && cur[0] == '.' && cur[1] ==
'.') { + ret = get_parent_inode(root, ino, &next_root,
&next_ino); + if (ret < 0) + return ret; +
root = next_root; + ino = next_ino; + goto
next; + } + if (!*cur) + break; + +
ret = resolve_one_filename(root, ino, cur, len, &key, &type); +
if (ret < 0) + return ret; + /* Child inode is
a subvolume */ + if (key.type == BTRFS_ROOT_ITEM_KEY) { + +
next_root = btrfs_read_fs_root(fs_info, &key); + if
(IS_ERR(next_root)) + return PTR_ERR(next_root); +
root = next_root; + ino =
btrfs_root_dirid(&root->root_item); + goto next; +
} + /* Child inode is a symlink */ + if (type ==
BTRFS_FT_SYMLINK && symlink_limit >= 0) { + char
*target; + + if (symlink_limit == 0) { +
printf("%s: Too much symlinks!\n", __func__); +

Quoted 8 lines

return -EMLINK; + } + target =
malloc(fs_info->sectorsize); + if (!target) +
return -ENOMEM; + ret = btrfs_readlink(root, ino,
target); + if (ret < 0) { +
free(target); + return ret; + } +
target[ret] = '\0'; + + ret = btrfs_lookup_path(root,
ino, target, &next_root, + &next_ino,
&next_type, + symlink_limit);

Looked through older codes, should be "symlink_limit - 1"?

--
Su

Quoted 23 lines
fs/btrfs/inode.c index renamed from fs/btrfs/inode.c +20 -0
+			if (ret < 0)
+				return ret;
+			root = next_root;
+			ino = next_ino;
+			type = next_type;
+			goto next;
+		}
+		/* Child inode is an inode */
+		ino = key.objectid;
+next:
+		cur += len;
+	}
+	if (!ret) {
+		*root_ret = root;
+		*ino_ret = ino;
+		*type_ret = type;
+	}
+	return ret;
+}
+
 u64 __btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 		      u8 *type_p, struct btrfs_inode_item *inode_item_p,
 		      int symlink_limit)
#26 lore raw

Re: [PATCH U-BOOT 18/26] fs: btrfs: Implement btrfs_lookup_path()

Quoted 1 line · On Wed, 22 Apr 2020 22:44:43 +0800 Su Yue <[email protected]> wrote:

Looked through older codes, should be "symlink_limit - 1"?

OMG yes :) without this it can break :)

author
#27 lore raw

PATCH 19/26 fs: btrfs: Use btrfs_iter_dir() to replace btrfs_readdir()

In reply to Qu Wenruo

Use extent buffer based infrastructure to re-implement btrfs_readdir().

Along this rework, some small corner cases fixed:
- Subvolume tree mtime
Mtime of a subvolume tree is recorded in its root item, since there is no INODE_ITEM for it.
This needs extra search from tree root.

- Output the unknown type
If the DIR_ITEM is corrupted, at least don't try to access the memory out of bounday.

---
 fs/btrfs/btrfs.c    | 157 +++++++++++++++++++++++++++-----------------
 fs/btrfs/btrfs.h    |   5 --
 fs/btrfs/ctree.h    |   5 ++
 fs/btrfs/dir-item.c |  68 +++++++++++--------
 4 files changed, 144 insertions(+), 91 deletions(-)
fs/btrfs/btrfs.c +97 -60
index 7e6c7a26e4cf..e7c7ddcfc551 100644
@@ -15,67 +15,102 @@
 struct btrfs_info btrfs_info;
 struct btrfs_fs_info *current_fs_info;
 
-static int readdir_callback(const struct __btrfs_root *root,
-			    struct btrfs_dir_item *item)
+static int show_dir(struct btrfs_root *root, struct extent_buffer *eb,
+		    struct btrfs_dir_item *di)
 {
-	static const char typestr[BTRFS_FT_MAX][4] = {
-		[BTRFS_FT_UNKNOWN]  = " ? ",
-		[BTRFS_FT_REG_FILE] = "   ",
-		[BTRFS_FT_DIR]      = "DIR",
-		[BTRFS_FT_CHRDEV]   = "CHR",
-		[BTRFS_FT_BLKDEV]   = "BLK",
-		[BTRFS_FT_FIFO]     = "FIF",
-		[BTRFS_FT_SOCK]     = "SCK",
-		[BTRFS_FT_SYMLINK]  = "SYM",
-		[BTRFS_FT_XATTR]    = " ? ",
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	struct btrfs_inode_item ii;
+	struct btrfs_key key;
+	static const char* dir_item_str[] = {
+		[BTRFS_FT_REG_FILE]	= "FILE",
+		[BTRFS_FT_DIR] 		= "DIR",
+		[BTRFS_FT_CHRDEV]	= "CHRDEV",
+		[BTRFS_FT_BLKDEV]	= "BLKDEV",
+		[BTRFS_FT_FIFO]		= "FIFO",
+		[BTRFS_FT_SOCK]		= "SOCK",
+		[BTRFS_FT_SYMLINK]	= "SYMLINK",
+		[BTRFS_FT_XATTR]	= "XATTR"
 	};
-	struct btrfs_inode_item inode;
-	const char *name = (const char *) (item + 1);
-	char filetime[32], *target = NULL;
+	u8 type = btrfs_dir_type(eb, di);
+	char namebuf[BTRFS_NAME_LEN];
+	char *target = NULL;
+	char filetime[32];
 	time_t mtime;
+	int ret;
 
-	if (__btrfs_lookup_inode(root, (struct btrfs_key *)&item->location,
-			       &inode, NULL)) {
-		printf("%s: Cannot find inode item for directory entry %.*s!\n",
-		       __func__, item->name_len, name);
-		return 0;
-	}
-
-	mtime = inode.mtime.sec;
-	ctime_r(&mtime, filetime);
+	btrfs_dir_item_key_to_cpu(eb, di, &key);
 
-	if (item->type == BTRFS_FT_SYMLINK) {
-		target = malloc(min(inode.size + 1,
-				    (u64) btrfs_info.sb.sectorsize));
+	if (key.type == BTRFS_ROOT_ITEM_KEY) {
+		struct btrfs_root *subvol;
 
-		if (target && __btrfs_readlink(root, item->location.objectid,
-					     target)) {
-			free(target);
-			target = NULL;
+		/* It's a subvolume, get its mtime from root item */
+		subvol = btrfs_read_fs_root(fs_info, &key);
+		if (IS_ERR(subvol)) {
+			ret = PTR_ERR(subvol);
+			error("Can't find root %llu", key.objectid);
+			return ret;
 		}
+		mtime = btrfs_stack_timespec_sec(&subvol->root_item.otime);
+	} else {
+		struct btrfs_path path;
+
+		/* It's regular inode, get its mtime from inode item */
+		btrfs_init_path(&path);
+		ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
+		if (ret > 0)
+			ret = -ENOENT;
+		if (ret < 0) {
+			error("Can't find inode %llu", key.objectid);
+			btrfs_release_path(&path);
+			return ret;
+		}
+		read_extent_buffer(path.nodes[0], &ii,
+			btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
+			sizeof(ii));
+		btrfs_release_path(&path);
+		mtime = btrfs_stack_timespec_sec(&ii.mtime);
+	}
+	ctime_r(&mtime, filetime);
 
-		if (!target)
-			printf("%s: Cannot read symlink target!\n", __func__);
+	if (type == BTRFS_FT_SYMLINK) {
+		target = malloc(fs_info->sectorsize);
+		if (!target) {
+			error("Can't alloc memory for symlink %llu",
+				key.objectid);
+			return -ENOMEM;
+		}
+		ret = btrfs_readlink(root, key.objectid, target);
+		if (ret < 0) {
+			error("Failed to read symlink %llu", key.objectid);
+			goto out;
+		}
+		target[ret] = '\0';
 	}
 
-	printf("<%s> ", typestr[item->type]);
-	if (item->type == BTRFS_FT_CHRDEV || item->type == BTRFS_FT_BLKDEV)
-		printf("%4u,%5u  ", (unsigned int) (inode.rdev >> 20),
-			(unsigned int) (inode.rdev & 0xfffff));
+	if (type < ARRAY_SIZE(dir_item_str) && dir_item_str[type])
+		printf("<%s> ", dir_item_str[type]);
 	else
-		printf("%10llu  ", inode.size);
-
-	printf("%24.24s  %.*s", filetime, item->name_len, name);
-
-	if (item->type == BTRFS_FT_SYMLINK) {
-		printf(" -> %s", target ? target : "?");
-		if (target)
-			free(target);
+		printf("DIR_ITEM.%u", type);
+	if (type == BTRFS_FT_CHRDEV || type == BTRFS_FT_BLKDEV) {
+		ASSERT(key.type == BTRFS_INODE_ITEM_KEY);
+		printf("%4llu,%5llu  ", btrfs_stack_inode_rdev(&ii) >> 20,
+				btrfs_stack_inode_rdev(&ii) & 0xfffff);
+	} else {
+		if (key.type == BTRFS_INODE_ITEM_KEY)
+			printf("%10llu  ", btrfs_stack_inode_size(&ii));
+		else
+			printf("%10llu  ", 0ULL);
 	}
 
+	read_extent_buffer(eb, namebuf, (unsigned long)(di + 1),
+			   btrfs_dir_name_len(eb, di));
+	printf("%24.24s  %.*s", filetime, btrfs_dir_name_len(eb, di), namebuf);
+	if (type == BTRFS_FT_SYMLINK)
+		printf(" -> %s", target ? target : "?");
 	printf("\n");
-
-	return 0;
+out:
+	free(target);
+	return ret;
 }
 
 int btrfs_probe(struct blk_desc *fs_dev_desc, disk_partition_t *fs_partition)
@@ -123,27 +158,29 @@ int btrfs_probe(struct blk_desc *fs_dev_desc, disk_partition_t *fs_partition)
 
 int btrfs_ls(const char *path)
 {
-	struct __btrfs_root root = btrfs_info.fs_root;
-	u64 inr;
+	struct btrfs_fs_info *fs_info = current_fs_info;
+	struct btrfs_root *root = fs_info->fs_root;
+	u64 ino = BTRFS_FIRST_FREE_OBJECTID;
 	u8 type;
+	int ret;
 
-	inr = __btrfs_lookup_path(&root, root.root_dirid, path, &type, NULL, 40);
-
-	if (inr == -1ULL) {
+	ASSERT(fs_info);
+	ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
+				path, &root, &ino, &type, 40);
+	if (ret < 0) {
 		printf("Cannot lookup path %s\n", path);
-		return -1;
+		return ret;
 	}
 
 	if (type != BTRFS_FT_DIR) {
-		printf("Not a directory: %s\n", path);
-		return -1;
+		error("Not a directory: %s", path);
+		return -ENOENT;
 	}
-
-	if (btrfs_readdir(&root, inr, readdir_callback)) {
-		printf("An error occured while listing directory %s\n", path);
-		return -1;
+	ret = btrfs_iter_dir(root, ino, show_dir);
+	if (ret < 0) {
+		error("An error occured while listing directory %s", path);
+		return ret;
 	}
-
 	return 0;
 }
 
fs/btrfs/btrfs.h +0 -5
index 9057f2476526..eb32eb2006a8 100644
@@ -43,13 +43,8 @@ u32 btrfs_decompress(u8 type, const char *, u32, char *, u32);
 int btrfs_read_superblock(void);
 
 /* dir-item.c */
-typedef int (*btrfs_readdir_callback_t)(const struct __btrfs_root *,
-					struct btrfs_dir_item *);
-
 int __btrfs_lookup_dir_item(const struct __btrfs_root *, u64, const char *, int,
 			   struct btrfs_dir_item *);
-int btrfs_readdir(const struct __btrfs_root *, u64, btrfs_readdir_callback_t);
-
 /* root.c */
 int btrfs_find_root(u64, struct __btrfs_root *, struct btrfs_root_item *);
 u64 btrfs_lookup_root_ref(u64, struct btrfs_root_ref *, char *);
fs/btrfs/ctree.h +5 -0
index 78f7746d1f32..fe6966e896a1 100644
@@ -1284,6 +1284,11 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
 					     struct btrfs_path *path, u64 dir,
 					     const char *name, int name_len,
 					     int mod);
+typedef int (*btrfs_iter_dir_callback_t)(struct btrfs_root *root,
+					 struct extent_buffer *eb,
+					 struct btrfs_dir_item *di);
+int btrfs_iter_dir(struct btrfs_root *root, u64 ino,
+		   btrfs_iter_dir_callback_t callback);
 /* inode.c */
 int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
 			struct btrfs_root **root_ret, u64 *ino_ret,
fs/btrfs/dir-item.c +42 -26

81 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#28 lore raw

PATCH 20/26 fs: btrfs: Use btrfs_lookup_path() to implement btrfs_exists() and btrfs_size()

In reply to Qu Wenruo

The remaining function that still utilize __btrfs_lookup_path() is btrfs_read().

---
 fs/btrfs/btrfs.c | 65 +++++++++++++++++++++++++++++++++++-------------
 fs/btrfs/inode.c |  1 -
 2 files changed, 48 insertions(+), 18 deletions(-)
fs/btrfs/btrfs.c +48 -17
index e7c7ddcfc551..8d1dee2f73c4 100644
@@ -186,37 +186,66 @@ int btrfs_ls(const char *path)
 
 int btrfs_exists(const char *file)
 {
-	struct __btrfs_root root = btrfs_info.fs_root;
-	u64 inr;
+	struct btrfs_fs_info *fs_info = current_fs_info;
+	struct btrfs_root *root;
+	u64 ino;
 	u8 type;
+	int ret;
+
+	ASSERT(fs_info);
 
-	inr = __btrfs_lookup_path(&root, root.root_dirid, file, &type, NULL, 40);
+	ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
+				file, &root, &ino, &type, 40);
+	if (ret < 0)
+		return 0;
 
-	return (inr != -1ULL && type == BTRFS_FT_REG_FILE);
+	if (type == BTRFS_FT_REG_FILE)
+		return 1;
+	return 0;
 }
 
 int btrfs_size(const char *file, loff_t *size)
 {
-	struct __btrfs_root root = btrfs_info.fs_root;
-	struct btrfs_inode_item inode;
-	u64 inr;
+	struct btrfs_fs_info *fs_info = current_fs_info;
+	struct btrfs_inode_item *ii;
+	struct btrfs_root *root;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	u64 ino;
 	u8 type;
+	int ret;
 
-	inr = __btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
-				40);
-
-	if (inr == -1ULL) {
+	ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
+				file, &root, &ino, &type, 40);
+	if (ret < 0) {
 		printf("Cannot lookup file %s\n", file);
-		return -1;
+		return ret;
 	}
-
 	if (type != BTRFS_FT_REG_FILE) {
 		printf("Not a regular file: %s\n", file);
-		return -1;
+		return -ENOENT;
 	}
+	btrfs_init_path(&path);
+	key.objectid = ino;
+	key.type = BTRFS_INODE_ITEM_KEY;
+	key.offset = 0;
 
-	*size = inode.size;
-	return 0;
+	ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
+	if (ret < 0) {
+		printf("Cannot lookup ino %llu\n", ino);
+		return ret;
+	}
+	if (ret > 0) {
+		printf("Ino %llu does not exist\n", ino);
+		ret = -ENOENT;
+		goto out;
+	}
+	ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
+			    struct btrfs_inode_item);
+	*size = btrfs_inode_size(path.nodes[0], ii);
+out:
+	btrfs_release_path(&path);
+	return ret;
 }
 
 int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
@@ -268,7 +297,9 @@ void btrfs_close(void)
 int btrfs_uuid(char *uuid_str)
 {
 #ifdef CONFIG_LIB_UUID
-	uuid_bin_to_str(btrfs_info.sb.fsid, uuid_str, UUID_STR_FORMAT_STD);
+	if (current_fs_info)
+		uuid_bin_to_str(current_fs_info->super_copy->fsid, uuid_str,
+				UUID_STR_FORMAT_STD);
 	return 0;
 #endif
 	return -ENOSYS;
fs/btrfs/inode.c +0 -1
index af4f30bbd50c..ff411d5251e9 100644
@@ -404,7 +404,6 @@ int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
 	}
 
 	while (*cur != '\0') {
-
 		cur = skip_current_directories(cur);
 		len = next_length(cur);
 		if (len > BTRFS_NAME_LEN) {
Signature · 1 line
2.26.0
author
#29 lore raw

PATCH 21/26 fs: btrfs: Rename btrfs_file_read() and its callees to avoid name conflicts

In reply to Qu Wenruo
---
 fs/btrfs/btrfs.c     | 2 +-
 fs/btrfs/btrfs.h     | 6 +++---
 fs/btrfs/extent-io.c | 4 ++--
 fs/btrfs/inode.c     | 6 +++---
 4 files changed, 9 insertions(+), 9 deletions(-)
fs/btrfs/btrfs.c +1 -1
index 8d1dee2f73c4..3916696ab909 100644
@@ -275,7 +275,7 @@ int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
 	if (len > inode.size - offset)
 		len = inode.size - offset;
 
-	rd = btrfs_file_read(&root, inr, offset, len, buf);
+	rd = __btrfs_file_read(&root, inr, offset, len, buf);
 	if (rd == -1ULL) {
 		printf("An error occured while reading file %s\n", file);
 		return -1;
fs/btrfs/btrfs.h +3 -3
index eb32eb2006a8..025cf4007503 100644
@@ -58,16 +58,16 @@ int __btrfs_readlink(const struct __btrfs_root *, u64, char *);
 int btrfs_readlink(struct btrfs_root *root, u64 ino, char *target);
 u64 __btrfs_lookup_path(struct __btrfs_root *, u64, const char *, u8 *,
 		       struct btrfs_inode_item *, int);
-u64 btrfs_file_read(const struct __btrfs_root *, u64, u64, u64, char *);
+u64 __btrfs_file_read(const struct __btrfs_root *, u64, u64, u64, char *);
 
 /* subvolume.c */
 u64 btrfs_get_default_subvol_objectid(void);
 
 /* extent-io.c */
-u64 btrfs_read_extent_inline(struct __btrfs_path *,
+u64 __btrfs_read_extent_inline(struct __btrfs_path *,
 			      struct btrfs_file_extent_item *, u64, u64,
 			      char *);
-u64 btrfs_read_extent_reg(struct __btrfs_path *, struct btrfs_file_extent_item *,
+u64 __btrfs_read_extent_reg(struct __btrfs_path *, struct btrfs_file_extent_item *,
 			   u64, u64, char *);
 
 #endif /* !__BTRFS_BTRFS_H__ */
fs/btrfs/extent-io.c +2 -2
index 456b8776ef67..2af710f0dd32 100644
@@ -13,7 +13,7 @@
 #include "extent-io.h"
 #include "disk-io.h"
 
-u64 btrfs_read_extent_inline(struct __btrfs_path *path,
+u64 __btrfs_read_extent_inline(struct __btrfs_path *path,
 			     struct btrfs_file_extent_item *extent, u64 offset,
 			     u64 size, char *out)
 {
@@ -65,7 +65,7 @@ err:
 	return -1ULL;
 }
 
-u64 btrfs_read_extent_reg(struct __btrfs_path *path,
+u64 __btrfs_read_extent_reg(struct __btrfs_path *path,
 			  struct btrfs_file_extent_item *extent, u64 offset,
 			  u64 size, char *out)
 {
fs/btrfs/inode.c +3 -3
index ff411d5251e9..797063449ca6 100644
@@ -587,7 +587,7 @@ u64 __btrfs_lookup_path(struct __btrfs_root *root, u64 inr, const char *path,
 	return inr;
 }
 
-u64 btrfs_file_read(const struct __btrfs_root *root, u64 inr, u64 offset,
+u64 __btrfs_file_read(const struct __btrfs_root *root, u64 inr, u64 offset,
 		    u64 size, char *buf)
 {
 	struct __btrfs_path path;
@@ -622,11 +622,11 @@ u64 btrfs_file_read(const struct __btrfs_root *root, u64 inr, u64 offset,
 
 		if (extent->type == BTRFS_FILE_EXTENT_INLINE) {
 			btrfs_file_extent_item_to_cpu_inl(extent);
-			rd = btrfs_read_extent_inline(&path, extent, offset,
+			rd = __btrfs_read_extent_inline(&path, extent, offset,
 						      size, buf);
 		} else {
 			btrfs_file_extent_item_to_cpu(extent);
-			rd = btrfs_read_extent_reg(&path, extent, offset, size,
+			rd = __btrfs_read_extent_reg(&path, extent, offset, size,
 						   buf);
 		}
 
Signature · 1 line
2.26.0
author
#30 lore raw

PATCH 22/26 fs: btrfs: Introduce btrfs_read_extent_inline() and btrfs_read_extent_reg()

In reply to Qu Wenruo

These two functions are used to do sector aligned read, which will be later used to implement btrfs_file_read().

---
 fs/btrfs/ctree.h   |   5 ++
 fs/btrfs/disk-io.c |  36 ++++++++++
 fs/btrfs/disk-io.h |   2 +
 fs/btrfs/inode.c   | 162 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 205 insertions(+)
fs/btrfs/ctree.h +5 -0
index fe6966e896a1..602f99b5e593 100644
@@ -1293,6 +1293,11 @@ int btrfs_iter_dir(struct btrfs_root *root, u64 ino,
 int btrfs_lookup_path(struct btrfs_root *root, u64 ino, const char *filename,
 			struct btrfs_root **root_ret, u64 *ino_ret,
 			u8 *type_ret, int symlink_limit);
+int btrfs_read_extent_inline(struct btrfs_path *path,
+			     struct btrfs_file_extent_item *fi, char *dest);
+int btrfs_read_extent_reg(struct btrfs_path *path,
+			  struct btrfs_file_extent_item *fi, u64 offset,
+			  int len, char *dest);
 
 /* ctree.c */
 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
fs/btrfs/disk-io.c +36 -0
index 8dc816f7a8c9..be2c26d323f6 100644
@@ -565,6 +565,42 @@ struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
 	return ERR_PTR(ret);
 }
 
+int read_extent_data(struct btrfs_fs_info *fs_info, char *data, u64 logical,
+		     u64 *len, int mirror)
+{
+	u64 offset = 0;
+	struct btrfs_multi_bio *multi = NULL;
+	struct btrfs_device *device;
+	int ret = 0;
+	u64 max_len = *len;
+
+	ret = btrfs_map_block(fs_info, READ, logical, len, &multi, mirror,
+			      NULL);
+	if (ret) {
+		fprintf(stderr, "Couldn't map the block %llu\n",
+				logical + offset);
+		goto err;
+	}
+	device = multi->stripes[0].dev;
+
+	if (*len > max_len)
+		*len = max_len;
+	if (!device->desc || !device->part) {
+		ret = -EIO;
+		goto err;
+	}
+
+	ret = __btrfs_devread(device->desc, device->part, data, *len,
+			      multi->stripes[0].physical);
+	if (ret != *len)
+		ret = -EIO;
+	else
+		ret = 0;
+err:
+	kfree(multi);
+	return ret;
+}
+
 void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
 		      u64 objectid)
 {
fs/btrfs/disk-io.h +2 -0
index d8f6f827550a..7c321d842561 100644
@@ -18,6 +18,8 @@ int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirr
 struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
 		u64 parent_transid);
 
+int read_extent_data(struct btrfs_fs_info *fs_info, char *data, u64 logical,
+		     u64 *len, int mirror);
 struct extent_buffer* btrfs_find_create_tree_block(
 		struct btrfs_fs_info *fs_info, u64 bytenr);
 struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
fs/btrfs/inode.c +162 -0

174 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#31 lore raw

PATCH 23/26 fs: btrfs: Introduce lookup_data_extent() for later use

In reply to Qu Wenruo
---
 fs/btrfs/inode.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
fs/btrfs/inode.c +101 -0
index 986b9347b012..3332055ff840 100644
@@ -814,3 +814,104 @@ out:
 	free(dbuf);
 	return ret;
 }
+
+/*
+ * Get the first file extent that covers bytenr @file_offset.
+ *
+ * @file_offset must be aligned to sectorsize.
+ *
+ * return 0 for found, and path points to the file extent.
+ * return >0 for not found, and fill @next_offset.
+ * @next_offset can be 0 if there is no next file extent.
+ * return <0 for error.
+ */
+static int lookup_data_extent(struct btrfs_root *root, struct btrfs_path *path,
+			      u64 ino, u64 file_offset, u64 *next_offset)
+{
+	struct btrfs_key key;
+	struct btrfs_file_extent_item *fi;
+	u8 extent_type;
+	int ret = 0;
+
+	ASSERT(IS_ALIGNED(file_offset, root->fs_info->sectorsize));
+	key.objectid = ino;
+	key.type = BTRFS_EXTENT_DATA_KEY;
+	key.offset = file_offset;
+
+	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	/* Error or we're already at the file extent */
+	if (ret <= 0)
+		return ret;
+	if (ret > 0) {
+		/* Check previous file extent */
+		ret = btrfs_previous_item(root, path, ino,
+					  BTRFS_EXTENT_DATA_KEY);
+		if (ret < 0)
+			return ret;
+		if (ret > 0)
+			goto check_next;
+	}
+	/* Now the key.offset must be smaller than @file_offset */
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+	if (key.objectid != ino ||
+	    key.type != BTRFS_EXTENT_DATA_KEY)
+		goto check_next;
+
+	fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
+			    struct btrfs_file_extent_item);
+	extent_type = btrfs_file_extent_type(path->nodes[0], fi);
+	if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
+		if (file_offset == 0)
+			return 0;
+		/* Inline extent should be the only extent, no next extent. */
+		*next_offset = 0;
+		return 1;
+	}
+
+	/* This file extent covers @file_offset */
+	if (key.offset <= file_offset && key.offset +
+	    btrfs_file_extent_num_bytes(path->nodes[0], fi) > file_offset)
+		return 0;
+check_next:
+	ret = btrfs_next_item(root, path);
+	if (ret < 0)
+		return ret;
+	if (ret > 0) {
+		*next_offset = 0;
+		return 1;
+	}
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+	fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
+			    struct btrfs_file_extent_item);
+	/* Next next data extent */
+	if (key.objectid != ino ||
+	    key.type != BTRFS_EXTENT_DATA_KEY) {
+		*next_offset = 0;
+		return 1;
+	}
+	/* Current file extent already beyond @file_offset */
+	if (key.offset > file_offset) {
+		*next_offset = key.offset;
+		return 1;
+	}
+	/* This file extent covers @file_offset */
+	if (key.offset <= file_offset && key.offset +
+	    btrfs_file_extent_num_bytes(path->nodes[0], fi) > file_offset)
+		return 0;
+	/* This file extent ends before @file_offset, check next */
+	ret = btrfs_next_item(root, path);
+	if (ret < 0)
+		return ret;
+	if (ret > 0) {
+		*next_offset = 0;
+		return 1;
+	}
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+	if (key.type != BTRFS_EXTENT_DATA_KEY || key.objectid != ino) {
+		*next_offset = 0;
+		return 1;
+	}
+	*next_offset = key.offset;
+	return 1;
+}
Signature · 1 line
2.26.0
author
#32 lore raw

PATCH 24/26 fs: btrfs: Implement btrfs_file_read()

In reply to Qu Wenruo

This version of btrfs_file_read() will have the following new features:
- Try all mirrors
- More hanlding on unaligned size
- Better compressed extent handling
The old implementation doesn't handle compressed extent with offset properly.
As we need to read out the whole compressed extent, then decompress the whole extent, then only copy the referred part.

---
 fs/btrfs/btrfs.c |  42 ++++++++------
 fs/btrfs/btrfs.h |   2 +
 fs/btrfs/inode.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 173 insertions(+), 17 deletions(-)
fs/btrfs/btrfs.c +25 -17
index 3916696ab909..023268152319 100644
@@ -251,37 +251,45 @@ out:
 int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
 	       loff_t *actread)
 {
-	struct __btrfs_root root = btrfs_info.fs_root;
-	struct btrfs_inode_item inode;
-	u64 inr, rd;
+	struct btrfs_fs_info *fs_info = current_fs_info;
+	struct btrfs_root *root;
+	loff_t real_size;
+	u64 ino;
 	u8 type;
+	int ret;
 
-	inr = __btrfs_lookup_path(&root, root.root_dirid, file, &type, &inode,
-				40);
-
-	if (inr == -1ULL) {
+	ASSERT(fs_info);
+	ret = btrfs_lookup_path(fs_info->fs_root, BTRFS_FIRST_FREE_OBJECTID,
+				file, &root, &ino, &type, 40);
+	if (ret < 0) {
 		printf("Cannot lookup file %s\n", file);
-		return -1;
+		return ret;
 	}
 
 	if (type != BTRFS_FT_REG_FILE) {
 		printf("Not a regular file: %s\n", file);
-		return -1;
+		return -EINVAL;
 	}
 
-	if (!len)
-		len = inode.size;
+	if (!len) {
+		ret = btrfs_size(file, &real_size);
+		if (ret < 0) {
+			printf("Failed to get inode size: %s\n", file);
+			return ret;
+		}
+		len = real_size;
+	}
 
-	if (len > inode.size - offset)
-		len = inode.size - offset;
+	if (len > real_size - offset)
+		len = real_size - offset;
 
-	rd = __btrfs_file_read(&root, inr, offset, len, buf);
-	if (rd == -1ULL) {
+	ret = btrfs_file_read(root, ino, offset, len, buf);
+	if (ret < 0) {
 		printf("An error occured while reading file %s\n", file);
-		return -1;
+		return ret;
 	}
 
-	*actread = rd;
+	*actread = len;
 	return 0;
 }
 
fs/btrfs/btrfs.h +2 -0
index 025cf4007503..6963e5ac447e 100644
@@ -59,6 +59,8 @@ int btrfs_readlink(struct btrfs_root *root, u64 ino, char *target);
 u64 __btrfs_lookup_path(struct __btrfs_root *, u64, const char *, u8 *,
 		       struct btrfs_inode_item *, int);
 u64 __btrfs_file_read(const struct __btrfs_root *, u64, u64, u64, char *);
+int btrfs_file_read(struct btrfs_root *root, u64 ino, u64 file_offset, u64 len,
+		    char *dest);
 
 /* subvolume.c */
 u64 btrfs_get_default_subvol_objectid(void);
fs/btrfs/inode.c +146 -0

149 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#33 lore raw

PATCH 25/26 fs: btrfs: Cleanup the old implementation

In reply to Qu Wenruo
---
 fs/btrfs/Makefile      |   4 +-
 fs/btrfs/btrfs.c       |  31 ----
 fs/btrfs/btrfs.h       |  47 ------
 fs/btrfs/chunk-map.c   | 177 ----------------------
 fs/btrfs/compression.c |   2 +-
 fs/btrfs/ctree.c       | 290 ------------------------------------
 fs/btrfs/ctree.h       |  63 --------
 fs/btrfs/dir-item.c    |  79 ----------
 fs/btrfs/disk-io.c     |  30 ----
 fs/btrfs/disk-io.h     |   3 -
 fs/btrfs/extent-io.c   | 110 --------------
 fs/btrfs/inode.c       | 323 -----------------------------------------
 fs/btrfs/root.c        |  92 ------------
 fs/btrfs/subvolume.c   | 130 -----------------
 14 files changed, 3 insertions(+), 1378 deletions(-)
 delete mode 100644 fs/btrfs/chunk-map.c
 delete mode 100644 fs/btrfs/root.c
 delete mode 100644 fs/btrfs/subvolume.c
fs/btrfs/Makefile +2 -2
index 3cf8a716f8c0..be03bf11efe1 100644
@@ -2,6 +2,6 @@
 #
 # 2017 Marek Behun, CZ.NIC, marek.behun at nic.cz
 
-obj-y := btrfs.o chunk-map.o compression.o ctree.o dev.o dir-item.o \
-	extent-io.o inode.o root.o subvolume.o crypto/hash.o disk-io.o \
+obj-y := btrfs.o compression.o ctree.o dev.o dir-item.o \
+	extent-io.o inode.o crypto/hash.o disk-io.o \
 	common/rbtree-utils.o extent-cache.o extent-io.o volumes.o root-tree.o
fs/btrfs/btrfs.c +0 -31
index 023268152319..f924d08b1e67 100644
@@ -12,7 +12,6 @@
 #include "crypto/hash.h"
 #include "disk-io.h"
 
-struct btrfs_info btrfs_info;
 struct btrfs_fs_info *current_fs_info;
 
 static int show_dir(struct btrfs_root *root, struct extent_buffer *eb,
@@ -118,36 +117,7 @@ int btrfs_probe(struct blk_desc *fs_dev_desc, disk_partition_t *fs_partition)
 	struct btrfs_fs_info *fs_info;
 	int ret = -1;
 
-	btrfs_blk_desc = fs_dev_desc;
-	btrfs_part_info = fs_partition;
-
-	memset(&btrfs_info, 0, sizeof(btrfs_info));
-
 	btrfs_hash_init();
-	if (btrfs_read_superblock())
-		return -1;
-
-	if (btrfs_chunk_map_init()) {
-		printf("%s: failed to init chunk map\n", __func__);
-		return -1;
-	}
-
-	btrfs_info.tree_root.objectid = 0;
-	btrfs_info.tree_root.bytenr = btrfs_info.sb.root;
-	btrfs_info.chunk_root.objectid = 0;
-	btrfs_info.chunk_root.bytenr = btrfs_info.sb.chunk_root;
-
-	if (__btrfs_read_chunk_tree()) {
-		printf("%s: failed to read chunk tree\n", __func__);
-		return -1;
-	}
-
-	if (btrfs_find_root(btrfs_get_default_subvol_objectid(),
-			    &btrfs_info.fs_root, NULL)) {
-		printf("%s: failed to find default subvolume\n", __func__);
-		return -1;
-	}
-
 	fs_info = open_ctree_fs_info(fs_dev_desc, fs_partition);
 	if (fs_info) {
 		current_fs_info = fs_info;
@@ -295,7 +265,6 @@ int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
 
 void btrfs_close(void)
 {
-	btrfs_chunk_map_exit();
 	if (current_fs_info) {
 		close_ctree_fs_info(current_fs_info);
 		current_fs_info = NULL;
fs/btrfs/btrfs.h +0 -47

65 lines not on this page · raw message

fs/btrfs/chunk-map.c deleted +0 -177

177 lines not on this page · raw message

fs/btrfs/compression.c +1 -1
index 346875d45a1b..987299639060 100644
@@ -94,7 +94,7 @@ static u32 decompress_zlib(const u8 *_cbuf, u32 clen, u8 *dbuf, u32 dlen)
 	while (stream.total_in < clen) {
 		stream.next_in = cbuf + stream.total_in;
 		stream.avail_in = min((u32) (clen - stream.total_in),
-				      (u32) btrfs_info.sb.sectorsize);
+					current_fs_info->sectorsize);
 
 		ret = inflate(&stream, Z_NO_FLUSH);
 		if (ret != Z_OK)
fs/btrfs/ctree.c +0 -290

296 lines not on this page · raw message

fs/btrfs/ctree.h +0 -63

69 lines not on this page · raw message

fs/btrfs/dir-item.c +0 -79

85 lines not on this page · raw message

fs/btrfs/disk-io.c +0 -30
index be2c26d323f6..48d4fb56d954 100644
@@ -310,36 +310,6 @@ int btrfs_read_dev_super(struct blk_desc *desc, disk_partition_t *part,
 	return 0;
 }
 
-int btrfs_read_superblock(void)
-{
-	ALLOC_CACHE_ALIGN_BUFFER(char, raw_sb, BTRFS_SUPER_INFO_SIZE);
-	struct btrfs_super_block *sb = (struct btrfs_super_block *) raw_sb;
-	int ret;
-
-
-	btrfs_info.sb.generation = 0;
-
-	ret = btrfs_read_dev_super(btrfs_blk_desc, btrfs_part_info, sb);
-	if (ret < 0) {
-		debug("%s: No valid BTRFS superblock found!\n", __func__);
-		return ret;
-	}
-	btrfs_super_block_to_cpu(sb);
-	memcpy(&btrfs_info.sb, sb, sizeof(*sb));
-
-	if (btrfs_info.sb.num_devices != 1) {
-		printf("%s: Unsupported number of devices (%lli). This driver "
-		       "only supports filesystem on one device.\n", __func__,
-		       btrfs_info.sb.num_devices);
-		return -1;
-	}
-
-	debug("Chosen superblock with generation = %llu\n",
-	      btrfs_info.sb.generation);
-
-	return 0;
-}
-
 static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
 				  int verify, int silent, u16 csum_type)
 {
fs/btrfs/disk-io.h +0 -3
index 7c321d842561..c3eae240b47c 100644
@@ -10,9 +10,6 @@
 #define BTRFS_SUPER_INFO_OFFSET SZ_64K
 #define BTRFS_SUPER_INFO_SIZE	SZ_4K
 
-/* U-boot specific */
-int btrfs_read_superblock(void);
-
 /* From btrfs-progs */
 int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirror);
 struct extent_buffer* read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
fs/btrfs/extent-io.c +0 -110

116 lines not on this page · raw message

fs/btrfs/inode.c +0 -323

347 lines not on this page · raw message

fs/btrfs/root.c deleted +0 -92

92 lines not on this page · raw message

fs/btrfs/subvolume.c deleted +0 -130

130 lines not on this page · raw message

Signature · 1 line
2.26.0
author
#34 lore raw

PATCH 26/26 MAINTAINERS: Add btrfs mail list

In reply to Qu Wenruo

Since the current code base is mostly from btrfs-progs, anyone contributing to U-boot btrfs code could also help us to improve btrfs-progs and btrfs kernel module.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index b50652bd85d0..2bb69aeb72eb 100644
@@ -529,6 +529,7 @@ F:	tools/binman/
 
 BTRFS
 M:	Marek Behun <[email protected]>
+L:	linux-btrfs at vger.kernel.org
 S:	Maintained
 F:	cmd/btrfs.c
 F:	fs/btrfs/
Signature · 1 line
2.26.0
#35 lore raw
In reply to Qu Wenruo

On Wed, 22 Apr 2020 14:49:43 +0800
Qu Wenruo <[email protected]> wrote:

Hi Qu,

Quoted 5 lines

The current btrfs code in U-boot is using a creative way to read on-disk data.
It's pretty simple, involving the least amount of code, but pretty different from btrfs-progs nor kernel, making it pretty hard to sync code between different projects.

do you think maybe btrfs-progs / kernel would be interested if I tried to convert their code to this "simpler to use" implementation of conversion functions?

Quoted 6 lines

Thus only the following 5 patches need extra review attention:
- Patch 0017
- Patch 0018
- Patch 0022
- Patch 0023
- Patch 0024

Anyway, this patch series does not apply cleanly on u-boot/master. I tried with the first 3 patches and then gave up :(
Sorry about this but I will review and test if you send a series that applies cleanly.

Marek

#36 lore raw
Quoted 11 lines · Marek Behun

On Wed, 22 Apr 2020 14:49:43 +0800
Qu Wenruo <[email protected]> wrote:

Hi Qu,

Quoted 5 lines

The current btrfs code in U-boot is using a creative way to read on-disk data.
It's pretty simple, involving the least amount of code, but pretty different from btrfs-progs nor kernel, making it pretty hard to sync code between different projects.

Quoted 3 lines

do you think maybe btrfs-progs / kernel would be interested if I tried to convert their code to this "simpler to use" implementation of conversion functions?

I don't think so, the problem is kernel and btrfs-progs all need to modify extent buffer, which make the read time conversion become a burden in write path.

Quoted 8 lines
Quoted 7 lines

Thus only the following 5 patches need extra review attention:
- Patch 0017
- Patch 0018
- Patch 0022
- Patch 0023
- Patch 0024

Quoted 4 lines

Anyway, this patch series does not apply cleanly on u-boot/master. I tried with the first 3 patches and then gave up :(
Sorry about this but I will review and test if you send a series that applies cleanly.

That's strange, the branch is originally based on an older master, as you can find in the github.

I guess there are some more btrfs related code change since then, I'll rebase them to latest master, and update the github repo then.

Thanks,
Qu

Quoted 3 lines

Marek

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200422/64879fc6/attachment.sig>

#37 lore raw
Quoted 35 lines · Qu Wenruo
Quoted 11 lines · Marek Behun

On Wed, 22 Apr 2020 14:49:43 +0800
Qu Wenruo <[email protected]> wrote:

Hi Qu,

Quoted 5 lines

The current btrfs code in U-boot is using a creative way to read on-disk data.
It's pretty simple, involving the least amount of code, but pretty different from btrfs-progs nor kernel, making it pretty hard to sync code between different projects.

Quoted 3 lines

do you think maybe btrfs-progs / kernel would be interested if I tried to convert their code to this "simpler to use" implementation of conversion functions?

I don't think so, the problem is kernel and btrfs-progs all need to modify extent buffer, which make the read time conversion become a burden in write path.

Quoted 8 lines
Quoted 7 lines

Thus only the following 5 patches need extra review attention:
- Patch 0017
- Patch 0018
- Patch 0022
- Patch 0023
- Patch 0024

Quoted 4 lines

Anyway, this patch series does not apply cleanly on u-boot/master. I tried with the first 3 patches and then gave up :(
Sorry about this but I will review and test if you send a series that applies cleanly.

Quoted 5 lines

That's strange, the branch is originally based on an older master, as you can find in the github.

I guess there are some more btrfs related code change since then, I'll rebase them to latest master, and update the github repo then.

Git repo updated.
You can fetch that branch.

Only two small conflicts during rebase, and since it compiles I haven't re-done the full test, but it looks pretty OK.

Thanks,
Qu

Quoted 8 lines

Thanks,
Qu

Quoted 3 lines

Marek

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200422/382d0616/attachment.sig>

#38 lore raw
In reply to Qu Wenruo
Quoted 7 lines · On Wed, 22 Apr 2020 15:52:25 +0800 Qu Wenruo <[email protected]> wrote:
Quoted 3 lines

do you think maybe btrfs-progs / kernel would be interested if I tried to convert their code to this "simpler to use" implementation of conversion functions?

I don't think so, the problem is kernel and btrfs-progs all need to modify extent buffer, which make the read time conversion become a burden in write path.

:(

#39 lore raw
In reply to Qu Wenruo

I am testing compilation from the github repository.

Another problem is that in the cleanup patch you have removed btrfs_list_subvols functoin, which is used by cmd/btrfs.c to list subvolumes. So Turris Mox and Turris Omnia with their default defconfig do not compile now.

Marek

#40 lore raw
In reply to Qu Wenruo

Also there are some warnings when compiling for Mox and Omnia:

fs/btrfs/inode.c: In function ?btrfs_lookup_path?:
fs/btrfs/inode.c:141:16: warning: ?parent_root? may be used uninitialized in this function [-Wmaybe-uninitialized]

  141 |   key.objectid = parent_root;
      |   ~~~~~~~~~~~~~^~~~~~~~~~~~~
fs/btrfs/inode.c:127:7: note: ?parent_root? was declared here
  127 |   u64 parent_root;
      |       ^~~~~~~~~~~
  CC      cmd/nvedit.o
In file included from fs/btrfs/ctree.h:19,
                 from fs/btrfs/disk-io.h:7,
                 from fs/btrfs/disk-io.c:8:
fs/btrfs/disk-io.c: In function ?btrfs_scan_fs_devices?:
fs/btrfs/disk-io.c:881:9: warning: format ?%llu? expects argument of type ?long long unsigned int?, but argument 3 has type ?lbaint_t? {aka ?long unsigned int?} [-Wformat=]
  881 |   error("superblock end %u is larger than device size %llu",
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  882 |     BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
  883 |     part->size << desc->log2blksz);
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                |
      |                lbaint_t {aka long unsigned int}
fs/btrfs/compat.h:13:29: note: in definition of macro ?error?
   13 | #define error(...) { printf(__VA_ARGS__); printf("\n"); }
      |                             ^~~~~~~~~~~
fs/btrfs/disk-io.c:881:58: note: format string is defined here
  881 |   error("superblock end %u is larger than device size %llu",
      |                                                       ~~~^
      |                                                          |
      |                                                          long long unsigned int
      |                                                       %lu

In file included from fs/btrfs/ctree.h:19,
from fs/btrfs/volumes.c:5:
fs/btrfs/volumes.c: In function ?btrfs_check_chunk_valid?:
fs/btrfs/volumes.c:404:9: warning: format ?%lu? expects argument of type ?long unsigned int?, but argument 4 has type ?u32? {aka ?unsigned int?} [-Wformat=]
404 | error("invalid chunk item size, have %u expect [%zu, %lu)",

      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/btrfs/compat.h:13:29: note: in definition of macro ?error?
   13 | #define error(...) { printf(__VA_ARGS__); printf("\n"); }
      |                             ^~~~~~~~~~~
fs/btrfs/volumes.c:404:58: note: format string is defined here
  404 |   error("invalid chunk item size, have %u expect [%zu, %lu)",
      |                                                        ~~^
      |                                                          |
      |                                                          long unsigned int
      |                                                        %u
#41 lore raw
Quoted 45 lines · Marek Behun

Also there are some warnings when compiling for Mox and Omnia:

fs/btrfs/inode.c: In function ?btrfs_lookup_path?:
fs/btrfs/inode.c:141:16: warning: ?parent_root? may be used uninitialized in this function [-Wmaybe-uninitialized]

  141 |   key.objectid = parent_root;
      |   ~~~~~~~~~~~~~^~~~~~~~~~~~~
fs/btrfs/inode.c:127:7: note: ?parent_root? was declared here
  127 |   u64 parent_root;
      |       ^~~~~~~~~~~
  CC      cmd/nvedit.o
In file included from fs/btrfs/ctree.h:19,
                 from fs/btrfs/disk-io.h:7,
                 from fs/btrfs/disk-io.c:8:
fs/btrfs/disk-io.c: In function ?btrfs_scan_fs_devices?:
fs/btrfs/disk-io.c:881:9: warning: format ?%llu? expects argument of type ?long long unsigned int?, but argument 3 has type ?lbaint_t? {aka ?long unsigned int?} [-Wformat=]
  881 |   error("superblock end %u is larger than device size %llu",
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  882 |     BTRFS_SUPER_INFO_SIZE + BTRFS_SUPER_INFO_OFFSET,
  883 |     part->size << desc->log2blksz);
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                |
      |                lbaint_t {aka long unsigned int}
fs/btrfs/compat.h:13:29: note: in definition of macro ?error?
   13 | #define error(...) { printf(__VA_ARGS__); printf("\n"); }
      |                             ^~~~~~~~~~~
fs/btrfs/disk-io.c:881:58: note: format string is defined here
  881 |   error("superblock end %u is larger than device size %llu",
      |                                                       ~~~^
      |                                                          |
      |                                                          long long unsigned int
      |                                                       %lu

In file included from fs/btrfs/ctree.h:19,
from fs/btrfs/volumes.c:5:
fs/btrfs/volumes.c: In function ?btrfs_check_chunk_valid?:
fs/btrfs/volumes.c:404:9: warning: format ?%lu? expects argument of type ?long unsigned int?, but argument 4 has type ?u32? {aka ?unsigned int?} [-Wformat=]
404 | error("invalid chunk item size, have %u expect [%zu, %lu)",

Quoted 11 lines
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/btrfs/compat.h:13:29: note: in definition of macro ?error?
   13 | #define error(...) { printf(__VA_ARGS__); printf("\n"); }
      |                             ^~~~~~~~~~~
fs/btrfs/volumes.c:404:58: note: format string is defined here
  404 |   error("invalid chunk item size, have %u expect [%zu, %lu)",
      |                                                        ~~^
      |                                                          |
      |                                                          long unsigned int
      |                                                        %u

I also hit those with GCC 9.2, but in GCC 9.3 they just disappear, so I'm not sure whether they are bugs from GCC or something else...

Thanks,
Qu

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200422/5cce0114/attachment.sig>

#42 lore raw
In reply to Qu Wenruo

Just for informational purposes for others:

I tested compiling with and without this patches to see how much it increases binary size.

		Omnia (armv7a)		Mox (aarch64)

with old btrfs 52900 (51.7 KB) 55456 (54.2 KB)
with new btrfs 76176 (74.4 KB) 75480 (73.7 KB)
diff 23276 (22.7 KB) 20024 (19.5 KB)
increase driver 44% 36%
increase u-boot 3.46% 2.81%

(I don't actually care much, this is not too much, this is something that can maybe be reduced by using some optimizations which u-boot does not support yet and also there should be enough storage for u-boot on Mox and Omnia. But maybe someone else will care enough.)