Skip to contentSkip to conversation

fs: fat: Make the volume label writable when mounted

PATCH In review Martin Kepplinger 4 participants 5 messages Oct 7, 2021 – Oct 10, 2021 VFAT/FAT/msdos filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: fat: Make the volume label writable when mounted

The motivation for this change (probably) has its origin in a whishlist for the kernel that popped up in 2011, see
https://lore.kernel.org/lkml/1319135973.1020.9.camel@mop/

Subsequently the "eudyptula programming challenge", see
http://eudyptula-challenge.org/ included the task of making a mounted FAT filesystems' label writable. That's where it found its way to me in 2014.

During the following years I was asked not to publish this in order to help the challenge be successful. Time has passed and I'm not asked to keep it secret anymore.

As for the API, I'm sending the original implementation as an ioctl() and the example program that I've occasionally used over the years. Based on the previous discussions, it might be less controversal to create a debugfs dir for the fs and make the label a file there. I'm happy for acceptable ideas and all feedback:

We add

	#define VFAT_IOCTL_SET_LABEL _IOW('r', 0x14, __u32)

for users to assign a new FAT volume label while the fat volume is mounted (it works on the mountpoint).

* A pointer to a string as it's attribute changes the label.
* A nullpointer prints the current label strings to the kernel log.
* An empty string clears the label to "NO NAME ". (I at least found this
in the FAT16 spec)

I simply dump a test app here for completeness:

	#include <stdio.h>
	#include <stdint.h>
	#include <sys/ioctl.h>
	#include <fcntl.h>
	#include <unistd.h>
	#include <errno.h>
	#include <linux/msdos_fs.h>
	#include <string.h>
	#include <limits.h>

	#ifndef VFAT_IOCTL_SET_LABEL
	#define VFAT_IOCTL_SET_LABEL _IOW('r', 0x14, __u32)
	#endif

	int main(int argc, char **argv)
	{
		int fd;
		char path[LINE_MAX];
		char mylabel[MSDOS_NAME];
		char *label = mylabel;

		if (argc < 2 || argc > 3) {
			printf("Change label:\n   %s [MOUNTPOINT] [LABEL]\n", argv[0]);
			printf("Show label in kernel logs:\n   %s [MOUNTPOINT]\n\n", argv[0]);
			printf("Available filesystems and their mountpoints are:\n");
			printf("   df --output=source,fstype,target | grep -E 'msdos|fat'\n");
			return -EINVAL;
		}
		if (strlen(argv[1]) >= LINE_MAX) {
			printf("Path to mountpoint too long");
			return -EINVAL;
		}

		strcpy(path, argv[1]);
		if (argc == 3) {
			#pragma GCC diagnostic push
			#pragma GCC diagnostic ignored "-Wstringop-truncation"
			strncpy(label, argv[2], MSDOS_NAME);
			#pragma GCC diagnostic pop
		}
		if (argc == 2)
			label = NULL;

		fd = open(path, O_RDONLY);
		if (fd < 0)
			goto err;

		ioctl(fd, VFAT_IOCTL_SET_LABEL, label);

		close(fd);
	err:
		perror(NULL);
		return errno;
	}
---
 fs/fat/dir.c                  |  22 +++++
 fs/fat/fat.h                  |   3 +
 fs/fat/file.c                 | 164 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/msdos_fs.h |   1 +
 4 files changed, 190 insertions(+)
fs/fat/dir.c +22 -0
index c4a274285858..7c971821dce5 100644
@@ -1412,3 +1412,25 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
 	return err;
 }
 EXPORT_SYMBOL_GPL(fat_add_entries);
+
+/* found at https://lwn.net/Articles/343565/ */
+int fat_get_label_entry(struct inode *root_inode, struct buffer_head **bh,
+			struct msdos_dir_entry **de)
+{
+	loff_t pos;
+
+	if (WARN_ON(root_inode->i_ino != MSDOS_ROOT_INO))
+		return -EINVAL;
+
+	pos = 0;
+	*bh = NULL;
+	while (fat_get_entry(root_inode, &pos, bh, de) >= 0) {
+		/* volume label: note that it is not enough to check only
+		 * whether the ATTR_VOLUME bit is set, since this would yield
+		 * true on any vfat extended entry.
+		 */
+		if ((*de)->attr != ATTR_EXT && ((*de)->attr & ATTR_VOLUME))
+			return 0;
+	}
+	return -ENOENT;
+}
fs/fat/fat.h +3 -0
index 02d4d4234956..efcf509b905d 100644
@@ -336,6 +336,9 @@ extern int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts);
 extern int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
 			   struct fat_slot_info *sinfo);
 extern int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo);
+extern int fat_get_label_entry(struct inode *root_inode,
+			       struct buffer_head **bh,
+			       struct msdos_dir_entry **de);
 
 /* fat/fatent.c */
 struct fat_entry {
fs/fat/file.c +164 -0

176 lines not on this page · raw message

include/uapi/linux/msdos_fs.h +1 -0
index a5773899f4d9..b666bca09238 100644
@@ -104,6 +104,7 @@ struct __fat_dirent {
 #define FAT_IOCTL_SET_ATTRIBUTES	_IOW('r', 0x11, __u32)
 /*Android kernel has used 0x12, so we use 0x13*/
 #define FAT_IOCTL_GET_VOLUME_ID		_IOR('r', 0x13, __u32)
+#define VFAT_IOCTL_SET_LABEL		_IOW('r', 0x14, __u32)
 
 struct fat_boot_sector {
 	__u8	ignored[3];	/* Boot strap short or near jump */
Signature · 1 line
2.30.2
maintainer
#2 lore raw
In reply to Martin Kepplinger
Quoted 3 lines · Martin Kepplinger

The motivation for this change (probably) has its origin in a whishlist for the kernel that popped up in 2011, see
https://lore.kernel.org/lkml/1319135973.1020.9.camel@mop/

I'm ok this patch if some userspace wants to use in real world.

The whole point of this should be to provide access without any race, however this looks like broken many ways.

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 157

Quoted 2 lines
fs/fat/file.c +2 -0
+/* protect access to the FAT fs label */
+DEFINE_MUTEX(label_mutex);

nothing reason to be global lock (iow, should be per-sb)?

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 184

Quoted 9 lines
fs/fat/file.c +9 -0
+	/* code found in inode.c */
+	fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
+	bh = sb_bread(sb, blocknr);
+	if (!bh) {
+		fat_msg(sb, KERN_ERR,
+			"unable to read inode block for updating (i_pos %lld)",
+			i_pos);
+		return -EIO;
+	}

reading dir block manually, this can read full of dir blocks for fat32?

Quoted 5 lines
fs/fat/file.c +5 -0
+	de = NULL;
+	err = fat_get_label_entry(root_inode, &bh, &de);
+	if (err)
+		return err;
+

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 200

Quoted 10 lines
fs/fat/file.c +10 -0
+	if (label) {
+		fat_msg(sb, KERN_INFO,
+			"rename directory label from %.11s to %.11s",
+			de->name, label);
+		mutex_lock(&label_mutex);
+		strncpy(de->name, label, MSDOS_NAME);
+		mark_buffer_dirty(bh);
+		sync_dirty_buffer(bh);
+		brelse(bh);
+		mutex_unlock(&label_mutex);

race with normal dir operations?

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 227

Quoted 5 lines
fs/fat/file.c +5 -0
+	if (sbi->fat_bits == 32) {
+		if (label) {
+			fat_msg(sb, KERN_INFO,
+				"rename partition label from %.11s to %.11s",
+				b->fat32.vol_label, label);

nothing reason to output message for user input.

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 237

Quoted 9 lines
fs/fat/file.c +9 -0
+			mutex_lock(&MSDOS_SB(sb)->s_lock);
+			b->fat32.state |= FAT_STATE_DIRTY;
+			memcpy(b->fat32.vol_label, label, MSDOS_NAME);
+			mutex_unlock(&MSDOS_SB(sb)->s_lock);
+		} else {
+			b->fat32.state &= ~FAT_STATE_DIRTY;
+			fat_msg(sb, KERN_INFO, "partition label is %.11s",
+				b->fat32.vol_label);
+		}

why do you touch dirty state?

Quoted 6 lines
fs/fat/file.c +6 -0
+	} else {
+		if (label) {
+			fat_msg(sb, KERN_INFO,
+				"rename partition label from %.11s to %.11s ",
+				b->fat16.vol_label, label);
+

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 253

Quoted 10 lines
fs/fat/file.c +10 -0
+			mutex_lock(&MSDOS_SB(sb)->s_lock);
+			b->fat32.state |= FAT_STATE_DIRTY;
+			memcpy(b->fat32.vol_label, label, MSDOS_NAME);
+			mutex_unlock(&MSDOS_SB(sb)->s_lock);
+		} else {
+			b->fat32.state &= ~FAT_STATE_DIRTY;
+			fat_msg(sb, KERN_INFO, "partition label is %.11s",
+				b->fat16.vol_label);
+		}
+	}

ditto

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 199

Quoted 3 lines
fs/fat/file.c +3 -0
+	mark_buffer_dirty(bh);
+	sync_dirty_buffer(bh);
+	brelse(bh);

why need to sync here?

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 274

Quoted 3 lines
fs/fat/file.c +3 -0
+	if (!user_attr) {
+		label = NULL;
+		goto print_only;

This outputs only label to dmesg, too strange syscall design. Probably, we would be better to delete label or such for NULL.

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 278

Quoted 2 lines
fs/fat/file.c +2 -0
+	label = newlabel;
+	err = copy_from_user(label, (void *)user_attr, MSDOS_NAME);

do we need cast here?

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 282

Quoted 4 lines
fs/fat/file.c +4 -0
+	if (err) {
+		fat_msg(sb, KERN_ERR,
+			"copy_from_user failed %d bytes not copied\n", err);
+		return -EFAULT;

user fault should not output message basically.

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 294

Quoted 5 lines
fs/fat/file.c +5 -0
+		for (i = 0; i < len; i++) {
+			c = label[i];
+			if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) {
+				if ((c < '0' || c > '9') && c != 0x20)
+					return -EINVAL;

IIRC, label allows much more characters.

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 303

Quoted 4 lines
fs/fat/file.c +4 -0
+	err = fat_set_directory_volume_label(file, label);
+	if (err == -ENOENT) {
+		fat_msg(sb, KERN_ERR, "no label entry. please reformat\n");
+		strncpy(label, "NO NAME    ", MSDOS_NAME);

no label entry should be normal state, and should not output message

fs/fat/file.c static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) · line 313

Quoted 10 lines
fs/fat/file.c +10 -0
+	} else if (err) {
+		fat_msg(sb, KERN_ERR, "error setting directory label\n");
+		return err;
+	}
+
+	err = fat_set_partition_volume_label(file, label);
+	if (err) {
+		fat_msg(sb, KERN_ERR, "error setting partition label\n");
+		return err;
+	}

strange messages, how interacting with the user should be basically the job of userspace tools.

include/uapi/linux/msdos_fs.h struct __fat_dirent { · line 107

Quoted 9 lines
include/uapi/linux/msdos_fs.h +1 -0
index a5773899f4d9..b666bca09238 100644
@@ -104,6 +104,7 @@ struct __fat_dirent {
 #define FAT_IOCTL_SET_ATTRIBUTES	_IOW('r', 0x11, __u32)
 /*Android kernel has used 0x12, so we use 0x13*/
 #define FAT_IOCTL_GET_VOLUME_ID		_IOR('r', 0x13, __u32)
+#define VFAT_IOCTL_SET_LABEL		_IOW('r', 0x14, __u32)

maybe FAT_IOCTL_SET_LABEL is better.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#3 lore raw

Hello!

Quoted 11 lines · On Sunday 10 October 2021 21:06:52 OGAWA Hirofumi wrote:

include/uapi/linux/msdos_fs.h struct __fat_dirent { · line 107

Quoted 9 lines
include/uapi/linux/msdos_fs.h +1 -0
index a5773899f4d9..b666bca09238 100644
@@ -104,6 +104,7 @@ struct __fat_dirent {
 #define FAT_IOCTL_SET_ATTRIBUTES	_IOW('r', 0x11, __u32)
 /*Android kernel has used 0x12, so we use 0x13*/
 #define FAT_IOCTL_GET_VOLUME_ID		_IOR('r', 0x13, __u32)
+#define VFAT_IOCTL_SET_LABEL		_IOW('r', 0x14, __u32)

maybe FAT_IOCTL_SET_LABEL is better.

Please do not introduce a new fs specific ioctls. There is already vfs-agnostic FS_IOC_SETFSLABEL ioctl, look at manpage:
https://man7.org/linux/man-pages/man2/ioctl_fslabel.2.html

I have WIP patches which adds support for FS_IOC_SETFSLABEL ioctl into kernel vfat driver. But it is not possible to implement this ioctl properly until issues with encoding are fixed in vfat driver. Some of dependency patches I have sent to ML as RFC series:
https://lore.kernel.org/lkml/[email protected]/

If you have a time and motivation, I can send you my unfinished WIP patches which implements FS_IOC_SETFSLABEL ioctl for vfat.

#4 lore raw
Quoted 19 lines · On Sunday 10 October 2021 14:18:16 Pali Rohár wrote:

Hello!

Quoted 11 lines · On Sunday 10 October 2021 21:06:52 OGAWA Hirofumi wrote:

include/uapi/linux/msdos_fs.h struct __fat_dirent { · line 107

Quoted 9 lines
include/uapi/linux/msdos_fs.h +1 -0
index a5773899f4d9..b666bca09238 100644
@@ -104,6 +104,7 @@ struct __fat_dirent {
 #define FAT_IOCTL_SET_ATTRIBUTES	_IOW('r', 0x11, __u32)
 /*Android kernel has used 0x12, so we use 0x13*/
 #define FAT_IOCTL_GET_VOLUME_ID		_IOR('r', 0x13, __u32)
+#define VFAT_IOCTL_SET_LABEL		_IOW('r', 0x14, __u32)

maybe FAT_IOCTL_SET_LABEL is better.

Please do not introduce a new fs specific ioctls. There is already vfs-agnostic FS_IOC_SETFSLABEL ioctl, look at manpage:
https://man7.org/linux/man-pages/man2/ioctl_fslabel.2.html

Quoted 5 lines

I have WIP patches which adds support for FS_IOC_SETFSLABEL ioctl into kernel vfat driver. But it is not possible to implement this ioctl properly until issues with encoding are fixed in vfat driver. Some of dependency patches I have sent to ML as RFC series:
https://lore.kernel.org/lkml/[email protected]/

Also look at important details about encoding in discussion:
https://lore.kernel.org/lkml/20200107231522.GC472641@magnolia/

Quoted 2 lines

If you have a time and motivation, I can send you my unfinished WIP patches which implements FS_IOC_SETFSLABEL ioctl for vfat.