Skip to contentSkip to conversation

Improved hfsplus in the kernel

PATCH In review Pranjal Prasad 2 participants 2 messages Feb 25, 2025 Hfsplus filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improved hfsplus in the kernel

Hi,

Please find attached the patch to improve the HFS+ filesystem support in the kernel. I have not done much work, but as I require HFS, HFS+, and APFS in Linux, I decided to maintain it. Please allow me to be the maintainer of HFS and HFS+ in the kernel.

Best regards,
Pranjal Prasad

From 201b5513650ec86b87a64fa584013da8080e9ee3 Mon Sep 17 00:00:00 2001
From: Pranjal Prasad <[email protected]>
Date: Wed, 26 Feb 2025 01:51:09 +0530
Subject: [PATCH] Improved hfsplus in the kernel

---
 fs/hfsplus/attributes.c     | 113 +++++++++++++-----
 fs/hfsplus/bfind.c          | 153 ++++++++++++++++---------
 fs/hfsplus/btree.c          | 223 +++++++++++++++++++++---------------
 fs/hfsplus/xattr_security.c |  50 +++++---
 4 files changed, 353 insertions(+), 186 deletions(-)
fs/hfsplus/attributes.c +87 -26
index eeebe80c6be4..81d64944b145 100644
@@ -5,6 +5,9 @@
  * Vyacheslav Dubeyko <[email protected]>
  *
  * Handling of records in attributes tree
+ *
+ * Pranjal Prasad <[email protected]>
+ * Improved and added comments
  */
 
 #include "hfsplus_fs.h"
@@ -79,91 +82,149 @@ int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key,
 
 	return 0;
 }
-
+/*
+ * Allocate memory for an attribute entry from the attribute tree cache.
+ * This function uses the kernel's memory cache system to allocate memory
+ * for an attribute entry.
+ */
 hfsplus_attr_entry *hfsplus_alloc_attr_entry(void)
 {
-	return kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);
+	return kmem_cache_alloc(hfsplus_attr_tree_cachep, GFP_KERNEL);  // Allocates memory from the cache.
 }
 
+/*
+ * Free an attribute entry back to the attribute tree cache.
+ * This function releases memory allocated for an attribute entry back to
+ * the kernel's memory cache system.
+ */
 void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry)
 {
-	if (entry)
-		kmem_cache_free(hfsplus_attr_tree_cachep, entry);
+	if (entry)  // Check if the entry is valid (non-NULL).
+		kmem_cache_free(hfsplus_attr_tree_cachep, entry);  // Frees the entry back to the cache.
 }
 
+/*
+ * Constant for invalid attribute record.
+ * Used when the record type is invalid or when an error occurs during record creation.
+ */
 #define HFSPLUS_INVALID_ATTR_RECORD -1
 
+/*
+ * Builds an attribute record in the provided entry based on the record type.
+ * Depending on the record type, this function either initializes the entry for
+ * a specific attribute type or returns an error code if the type is unsupported.
+ *
+ * record_type: The type of the record (e.g., fork data, extents, inline data).
+ * cnid: The CNID of the entry (used for file identification).
+ * value: The value to store in the entry (if applicable).
+ * size: The size of the value.
+ *
+ * Returns: The size of the record, or HFSPLUS_INVALID_ATTR_RECORD on error.
+ */
 static int hfsplus_attr_build_record(hfsplus_attr_entry *entry, int record_type,
-				u32 cnid, const void *value, size_t size)
+				     u32 cnid, const void *value, size_t size)
 {
+	// Handle fork data attributes (Mac OS X supports only inline data for forks)
 	if (record_type == HFSPLUS_ATTR_FORK_DATA) {
 		/*
-		 * Mac OS X supports only inline data attributes.
-		 * Do nothing
+		 * Mac OS X supports only inline data attributes for forks.
+		 * No action needed here, just initialize the entry with zeros.
 		 */
 		memset(entry, 0, sizeof(*entry));
-		return sizeof(struct hfsplus_attr_fork_data);
+		return sizeof(struct hfsplus_attr_fork_data);  // Return the size of the fork data record.
+
+		// Handle extents attributes (again, only inline data is supported)
 	} else if (record_type == HFSPLUS_ATTR_EXTENTS) {
 		/*
-		 * Mac OS X supports only inline data attributes.
-		 * Do nothing.
+		 * Mac OS X supports only inline data attributes for extents.
+		 * No action needed here, just initialize the entry with zeros.
 		 */
 		memset(entry, 0, sizeof(*entry));
-		return sizeof(struct hfsplus_attr_extents);
+		return sizeof(struct hfsplus_attr_extents);  // Return the size of the extents record.
+
+		// Handle inline data attributes
 	} else if (record_type == HFSPLUS_ATTR_INLINE_DATA) {
 		u16 len;
 
+		// Initialize the entry for inline data attributes
 		memset(entry, 0, sizeof(struct hfsplus_attr_inline_data));
-		entry->inline_data.record_type = cpu_to_be32(record_type);
+		entry->inline_data.record_type = cpu_to_be32(record_type);  // Set the record type in CPU-to-BE format.
+
+		// If the size is within the allowed range for inline data, proceed to set the length.
 		if (size <= HFSPLUS_MAX_INLINE_DATA_SIZE)
 			len = size;
 		else
-			return HFSPLUS_INVALID_ATTR_RECORD;
-		entry->inline_data.length = cpu_to_be16(len);
+			return HFSPLUS_INVALID_ATTR_RECORD;  // Return an error if the size exceeds the limit.
+
+			// Set the length of the inline data attribute (in CPU-to-BE format)
+			entry->inline_data.length = cpu_to_be16(len);
+
+		// Copy the raw bytes of the data into the entry (memory location for the data).
 		memcpy(entry->inline_data.raw_bytes, value, len);
-		/*
-		 * Align len on two-byte boundary.
-		 * It needs to add pad byte if we have odd len.
-		 */
+
+		// Ensure the length is aligned to a two-byte boundary. If the length is odd, we add a padding byte.
 		len = round_up(len, 2);
-		return offsetof(struct hfsplus_attr_inline_data, raw_bytes) +
-					len;
-	} else /* invalid input */
-		memset(entry, 0, sizeof(*entry));
 
-	return HFSPLUS_INVALID_ATTR_RECORD;
+		// Return the size of the record, considering the padding applied to the raw bytes.
+		return offsetof(struct hfsplus_attr_inline_data, raw_bytes) + len;
+	} else {
+		// If the record type is invalid, clear the entry and return the invalid record constant.
+		memset(entry, 0, sizeof(*entry));
+		return HFSPLUS_INVALID_ATTR_RECORD;
+	}
 }
 
+/*
+ * Find an attribute by its CNID and name in the attribute tree.
+ * This function searches for an attribute based on the CNID and name provided.
+ * If the name is NULL, it searches for the first attribute matching the CNID.
+ *
+ * sb: The superblock containing the filesystem.
+ * cnid: The CNID of the entry to search for.
+ * name: The name of the attribute to search for (optional).
+ * fd: The structure to store the search results.
+ *
+ * Returns: 0 on success, or an error code on failure.
+ */
 int hfsplus_find_attr(struct super_block *sb, u32 cnid,
-			const char *name, struct hfs_find_data *fd)
+		      const char *name, struct hfs_find_data *fd)
 {
 	int err = 0;
 
+	// Debugging output: log the search for the attribute.
 	hfs_dbg(ATTR_MOD, "find_attr: %s,%d\n", name ? name : NULL, cnid);
 
+	// If the attribute tree does not exist, log an error and return failure.
 	if (!HFSPLUS_SB(sb)->attr_tree) {
 		pr_err("attributes file doesn't exist\n");
 		return -EINVAL;
 	}
 
+	// Search by name if it is provided.
 	if (name) {
+		// Build the search key based on the CNID and the name.
 		err = hfsplus_attr_build_key(sb, fd->search_key, cnid, name);
 		if (err)
 			goto failed_find_attr;
+
+		// Perform the search by key in the attribute record tree.
 		err = hfs_brec_find(fd, hfs_find_rec_by_key);
 		if (err)
 			goto failed_find_attr;
 	} else {
+		// If no name is provided, search by CNID alone.
 		err = hfsplus_attr_build_key(sb, fd->search_key, cnid, NULL);
 		if (err)
 			goto failed_find_attr;
+
+		// Perform the search for the first record matching the CNID.
 		err = hfs_brec_find(fd, hfs_find_1st_rec_by_cnid);
 		if (err)
 			goto failed_find_attr;
 	}
 
-failed_find_attr:
-	return err;
+	failed_find_attr:
+	return err;  // Return the result of the attribute search (0 for success, error code on failure).
 }
 
 int hfsplus_attr_exists(struct inode *inode, const char *name)
fs/hfsplus/bfind.c +102 -51

341 lines not on this page · raw message

fs/hfsplus/btree.c +130 -93

328 lines not on this page · raw message

fs/hfsplus/xattr_security.c +34 -16

91 lines not on this page · raw message

Signature · 1 line
2.48.1
#1 lore raw
Quoted 6 lines · Pranjal Prasad

Hi,

Please find attached the patch to improve the HFS+ filesystem support in the kernel. I have not done much work, but as I require HFS, HFS+, and APFS in Linux, I decided to maintain it. Please allow me to be the maintainer of HFS and HFS+ in the kernel.

Umm... "Improved" is not a description - it's marketdroidese. And comments are supposed to help understanding why we are doing something, not retelling what's being done in every line. Being more verbose is no virtue - not when it adds no information.

What's more worrying, though... are there actual changes in that pile of added comments? Your "improved and added comments" would seem to imply that these had been two distinct things, so it sounds like there are actual modifications involved.

In such case you really should separate those rather than burying them.

And this "every line must come with a comment, no matter how useful it is" is a habit best unlearnt. I realize that a bunch of places teach that, but it's a really, really bad idea.