From nobody Wed Sep 10 01:59:52 2025 Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50207311583 for ; Mon, 8 Sep 2025 16:40:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757349642; cv=none; b=XlQ1smCYIZ0DWaMu2UeEF7nJ4MAXr6B4iRgQzqRHR2Q7UPebCSeznk6IziymAgAQOfrZFcXDIvxMTl3WfmrZd6OW8CP8fln8KOk+c0lBFsjY1KP/0uaBtqnezmiP+vtDHU7SCPdcVZfHMd3hkgdNmcj4NzRwe8+5U/C5YdZ/uMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757349642; c=relaxed/simple; bh=XMY2/xu8PJI7DUrwm4ZXgwInILMf9viF7DT55xJdiWQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vGNWYWw0Kep3QCvfukwxIcdQn6C0IN/Ay/qY9PgpF9Fl3X59T9axKmbJuHaQ3+JQo8Ahh6avToYuhGiCAj5L+Ff8ejzS4eDly933ieHdsgqsVT7xtKS+KXgVawJ5Ub9Yq+/ZxOxgQ2mlVNYzTYIhTkDEAv2Uu0ggiFpR8VxnZn4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zetier.com; spf=pass smtp.mailfrom=zetier.com; dkim=pass (2048-bit key) header.d=zetier.com header.i=@zetier.com header.b=TbLf8pG3; arc=none smtp.client-ip=209.85.219.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zetier.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zetier.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zetier.com header.i=@zetier.com header.b="TbLf8pG3" Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-729c10746edso35088036d6.3 for ; Mon, 08 Sep 2025 09:40:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zetier.com; s=gm; t=1757349639; x=1757954439; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JA+XrAiCtFVnCRuwQJPE61a3h/yFrURoMqW1IT0bnMY=; b=TbLf8pG3UzoNwgRWDk43+cIS/aFz53p7VrgPzOhL8ePjrm3TGYkE7vjyB+jlae6jou jPqCvwMUZ1tSqkxlmXx5LloNa/CtkR3NFWrBZd5RgjpDhqAjGwNUKnfW8t244n8jIpt+ 5OTJwAVMJCp/9PMAHoA7m20QRYf0rUnf4b3/JRHaYWQaaCOHwbnmiMzDfA3Moh8syaYf 9Cn/cYCnBQH79KCimxYJfrZlucbobNDFHZ2DwGsAWZBBDydP7+3VDLRW5KOq6rvXKF+Y ZhFgktZ8XHB1Vbz7dDCkJng5YuXM1QYnRSrGpP68vtTdDeqGL2dWcmr6VOSZayddx2q5 KYOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757349639; x=1757954439; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JA+XrAiCtFVnCRuwQJPE61a3h/yFrURoMqW1IT0bnMY=; b=O997nqGKdCA5AxH+TVtogVR7mp2dVDV7AvDlayTojfhm+gYze7iYhoBNts5aaYZbGg k508Y4DxTKBf39sSlkXdhgVJn412aVdXREqRs+Qp0UjpEA6Zf3eybPopB79TncUFlBlg e74ZZYND0yJnKMzy8z8QzVucyLj72dwuPenvCeKn82ppTPiEZ0u8/JZi2Ew8TpWDWvo2 cnN1CYNLSwYiHz4e2waSPS8Rz64PfIXZEm6jLfPJ05Kfi7hRNArydj3hSM2p0ukmeH8l MMK60dNzWltze0bUA2C1hZd6i2f2iqQW/aeHhCg61l14o98gwjSHsZMR402OmS9yGVak xD6A== X-Forwarded-Encrypted: i=1; AJvYcCX4HEa6ksomF+T8jlBwx60JKtderRcfWWUd4/swLY9BWWpj/sBuu1S2iEQH7n/yj46O+AuiWDvTynr3nHs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3rcnaXXcOftGqqYRcfKSNJTmTzl8WNBXbXFPwENCmJx8G28Py fOdIw8LaZ6gYcWjRHlcitMn8KtqTzl/bRdUUdRKWp6p57qLYKXmKEdd1cX6iE1yWJeA= X-Gm-Gg: ASbGncvSvd4r0CuVKdLN0DH+tm2FUcyzd3UeZZ/+8S+e+VOt4bgYVMFlXkj+BiPx1iV LBbHysB6IY4nFN/IpUZOsGjY7p4mdHFUapFzQnFHPRDlkeAFiwzPP2v1qdPePgyfgg8pl6lXo8j 7EsG00GVnV3U5ZSSjt75nTvGG+hhM6mSFqmATkZ9Ikp47lGiOzGrVYTATeb4CY0lMzluDOe2kx1 wHu7qEzyOQrUJBcDj/B9IsgBXpKjCQsrRWNn6dWMMHP2d9DExuYuq5J0FZkto46AHWtyrwS+cOq yJi20LMMJSoZlDLQpqSluayGAJKSHMVm9IlTuqYjkLcehW8fKT89lxxW28X5l2epZqQz5nYhis5 UDQ7xlwF4e8Dpxp4/dSIDj3UnS29u1/XkLc+7FQ== X-Google-Smtp-Source: AGHT+IFlG4T4xorxAijBMrIX7CGQ5l5ZtLksTzHwaUqTaZf2athPaTXSQBXJSEmdqGRA9WkMK0B0YQ== X-Received: by 2002:a05:6214:20cf:b0:70d:f9d0:de72 with SMTP id 6a1803df08f44-7393d43265fmr72984926d6.61.1757349639110; Mon, 08 Sep 2025 09:40:39 -0700 (PDT) Received: from ethanf.zetier.com ([65.222.209.234]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-72a2d06b9b5sm88751706d6.70.2025.09.08.09.40.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Sep 2025 09:40:38 -0700 (PDT) From: Ethan Ferguson To: linkinjeon@kernel.org, sj1557.seo@samsung.com, yuezhang.mo@sony.com Cc: cpgs@samsung.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Ethan Ferguson Subject: [PATCH v6 1/1] exfat: Add support for FS_IOC_{GET,SET}FSLABEL Date: Mon, 8 Sep 2025 12:40:28 -0400 Message-Id: <20250908164028.31711-2-ethan.ferguson@zetier.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250908164028.31711-1-ethan.ferguson@zetier.com> References: <20250908164028.31711-1-ethan.ferguson@zetier.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for reading / writing to the exfat volume label from the FS_IOC_GETFSLABEL and FS_IOC_SETFSLABEL ioctls Signed-off-by: Ethan Ferguson --- fs/exfat/dir.c | 162 +++++++++++++++++++++++++++++++++++++++++++ fs/exfat/exfat_fs.h | 7 ++ fs/exfat/exfat_raw.h | 6 ++ fs/exfat/file.c | 80 +++++++++++++++++++++ fs/exfat/namei.c | 2 +- 5 files changed, 256 insertions(+), 1 deletion(-) diff --git a/fs/exfat/dir.c b/fs/exfat/dir.c index ee060e26f51d..9223ef687a74 100644 --- a/fs/exfat/dir.c +++ b/fs/exfat/dir.c @@ -1244,3 +1244,165 @@ int exfat_count_dir_entries(struct super_block *sb,= struct exfat_chain *p_dir) =20 return count; } + +static int exfat_get_volume_label_ptrs(struct super_block *sb, + struct buffer_head **out_bh, + struct exfat_dentry **out_ep) +{ + int i, ret; + unsigned int type; + struct exfat_sb_info *sbi =3D EXFAT_SB(sb); + struct exfat_hint_femp *hint_femp =3D &EXFAT_I(sb->s_root->d_inode)->hint= _femp; + struct exfat_chain clu; + struct exfat_dentry *ep; + struct buffer_head *bh; + bool continue_deleted =3D false; + + exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN); + + while (clu.dir !=3D EXFAT_EOF_CLUSTER) { + for (i =3D 0; i < sbi->dentries_per_clu; i++) { + ep =3D exfat_get_dentry(sb, &clu, i, &bh); + + if (!ep) { + ret =3D -EIO; + goto error; + } + + type =3D exfat_get_entry_type(ep); + if (type =3D=3D TYPE_DELETED || type =3D=3D TYPE_UNUSED) { + if (hint_femp->eidx =3D=3D EXFAT_HINT_NONE) { + continue_deleted =3D true; + hint_femp->cur =3D clu; + hint_femp->eidx =3D i; + if (type =3D=3D TYPE_UNUSED) + hint_femp->count =3D sbi->dentries_per_clu - i; + else + hint_femp->count =3D 1; + } else if (continue_deleted) { + hint_femp->count++; + } + } else { + continue_deleted =3D false; + } + + if (type =3D=3D TYPE_UNUSED) { + ret =3D -ENOENT; + brelse(bh); + goto error; + } + + if (type =3D=3D TYPE_VOLUME) { + *out_bh =3D bh; + *out_ep =3D ep; + return 0; + } + + brelse(bh); + } + + if (exfat_get_next_cluster(sb, &(clu.dir))) { + ret =3D -EIO; + goto error; + } + } + + hint_femp->cur =3D clu; + hint_femp->eidx =3D 0; + hint_femp->count =3D 0; + + ret =3D -ENOENT; + +error: + *out_bh =3D NULL; + *out_ep =3D NULL; + return ret; +} + +int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name = *label_out) +{ + int ret, i; + struct exfat_sb_info *sbi =3D EXFAT_SB(sb); + struct buffer_head *bh =3D NULL; + struct exfat_dentry *ep =3D NULL; + + mutex_lock(&sbi->s_lock); + + ret =3D exfat_get_volume_label_ptrs(sb, &bh, &ep); + /* ENOENT signifies that a volume label dentry doesn't exist */ + /* We will treat this as an empty volume label and not fail. */ + if (ret =3D=3D -ENOENT) { + label_out->name[0] =3D 0x0000; + label_out->name_len =3D 0; + ret =3D 0; + } else if (ret < 0) { + goto cleanup; + } else { + for (i =3D 0; i < EXFAT_VOLUME_LABEL_LEN; i++) + label_out->name[i] =3D le16_to_cpu(ep->dentry.volume_label.volume_label= [i]); + label_out->name_len =3D ep->dentry.volume_label.char_count; + } + +cleanup: + mutex_unlock(&sbi->s_lock); + brelse(bh); + return ret; +} + +int exfat_write_volume_label(struct super_block *sb, + struct exfat_uni_name *label) +{ + int ret, i; + struct exfat_sb_info *sbi =3D EXFAT_SB(sb); + struct inode *root_inode =3D sb->s_root->d_inode; + struct exfat_entry_set_cache es =3D {0}; + struct exfat_chain clu; + struct exfat_dentry *ep; + struct buffer_head *bh =3D NULL; + + if (label->name_len > EXFAT_VOLUME_LABEL_LEN) + return -EINVAL; + + mutex_lock(&sbi->s_lock); + + ret =3D exfat_get_volume_label_ptrs(sb, &bh, &ep); + if (ret =3D=3D -ENOENT) { + if (label->name_len =3D=3D 0) { + /* No need to allocate new volume label dentry */ + ret =3D 0; + goto cleanup; + } else { + ret =3D exfat_find_empty_entry(root_inode, &clu, 1, &es); + if (ret < 0) + goto cleanup; + ret =3D 0; + + ep =3D exfat_get_dentry_cached(&es, 0); + memset(ep, 0, sizeof(struct exfat_dentry)); + ep->type =3D EXFAT_VOLUME; + es.modified =3D true; + } + } else if (ret < 0) { + goto cleanup; + } + + for (i =3D 0; i < label->name_len; i++) + ep->dentry.volume_label.volume_label[i] =3D + cpu_to_le16(label->name[i]); + /* Fill the rest of the str with 0x0000 */ + for (; i < EXFAT_VOLUME_LABEL_LEN; i++) + ep->dentry.volume_label.volume_label[i] =3D 0x0000; + + ep->dentry.volume_label.char_count =3D label->name_len; + +cleanup: + if (bh) { + exfat_update_bh(bh, IS_DIRSYNC(root_inode)); + brelse(bh); + } + + exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode)); + + mutex_unlock(&sbi->s_lock); + return ret; +} diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h index f8ead4d47ef0..329697c89d09 100644 --- a/fs/exfat/exfat_fs.h +++ b/fs/exfat/exfat_fs.h @@ -477,6 +477,9 @@ int exfat_force_shutdown(struct super_block *sb, u32 fl= ags); /* namei.c */ extern const struct dentry_operations exfat_dentry_ops; extern const struct dentry_operations exfat_utf8_dentry_ops; +int exfat_find_empty_entry(struct inode *inode, + struct exfat_chain *p_dir, int num_entries, + struct exfat_entry_set_cache *es); =20 /* cache.c */ int exfat_cache_init(void); @@ -517,6 +520,10 @@ int exfat_get_empty_dentry_set(struct exfat_entry_set_= cache *es, unsigned int num_entries); int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync); int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_= dir); +int exfat_read_volume_label(struct super_block *sb, + struct exfat_uni_name *label_out); +int exfat_write_volume_label(struct super_block *sb, + struct exfat_uni_name *label); =20 /* inode.c */ extern const struct inode_operations exfat_file_inode_operations; diff --git a/fs/exfat/exfat_raw.h b/fs/exfat/exfat_raw.h index 971a1ccd0e89..4082fa7b8c14 100644 --- a/fs/exfat/exfat_raw.h +++ b/fs/exfat/exfat_raw.h @@ -80,6 +80,7 @@ #define BOOTSEC_OLDBPB_LEN 53 =20 #define EXFAT_FILE_NAME_LEN 15 +#define EXFAT_VOLUME_LABEL_LEN 11 =20 #define EXFAT_MIN_SECT_SIZE_BITS 9 #define EXFAT_MAX_SECT_SIZE_BITS 12 @@ -159,6 +160,11 @@ struct exfat_dentry { __le32 start_clu; __le64 size; } __packed upcase; /* up-case table directory entry */ + struct { + __u8 char_count; + __le16 volume_label[EXFAT_VOLUME_LABEL_LEN]; + __u8 reserved[8]; + } __packed volume_label; /* volume label directory entry */ struct { __u8 flags; __u8 vendor_guid[16]; diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 538d2b6ac2ec..c44928c02474 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -486,6 +486,82 @@ static int exfat_ioctl_shutdown(struct super_block *sb= , unsigned long arg) return exfat_force_shutdown(sb, flags); } =20 +static int exfat_ioctl_get_volume_label(struct super_block *sb, unsigned l= ong arg) +{ + int ret; + char utf8[FSLABEL_MAX] =3D {0}; + struct exfat_uni_name *uniname; + + uniname =3D kmalloc(sizeof(struct exfat_uni_name), GFP_KERNEL); + if (!uniname) + return -ENOMEM; + + ret =3D exfat_read_volume_label(sb, uniname); + if (ret < 0) + goto cleanup; + + ret =3D exfat_utf16_to_nls(sb, uniname, utf8, FSLABEL_MAX); + if (ret < 0) + goto cleanup; + + if (copy_to_user((char __user *)arg, utf8, FSLABEL_MAX)) { + ret =3D -EFAULT; + goto cleanup; + } + + ret =3D 0; + +cleanup: + kfree(uniname); + return ret; +} + +static int exfat_ioctl_set_volume_label(struct super_block *sb, + unsigned long arg) +{ + int ret, lossy; + char utf8[FSLABEL_MAX]; + struct exfat_uni_name *uniname; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + uniname =3D kmalloc(sizeof(struct exfat_uni_name), GFP_KERNEL); + if (!uniname) + return -ENOMEM; + + if (copy_from_user(utf8, (char __user *)arg, FSLABEL_MAX)) { + ret =3D -EFAULT; + goto cleanup; + } + + if (utf8[0]) { + ret =3D exfat_nls_to_utf16(sb, utf8, strnlen(utf8, FSLABEL_MAX), + uniname, &lossy); + if (ret < 0) { + goto cleanup; + } else if (lossy & NLS_NAME_LOSSY) { + ret =3D -EINVAL; + goto cleanup; + } + } else { + uniname->name[0] =3D 0x0000; + uniname->name_len =3D 0; + } + + if (uniname->name_len > EXFAT_VOLUME_LABEL_LEN) { + exfat_info(sb, "Volume label length too long, truncating"); + uniname->name_len =3D EXFAT_VOLUME_LABEL_LEN; + uniname->name[EXFAT_VOLUME_LABEL_LEN] =3D 0x0000; + } + + ret =3D exfat_write_volume_label(sb, uniname); + +cleanup: + kfree(uniname); + return ret; +} + long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct inode *inode =3D file_inode(filp); @@ -500,6 +576,10 @@ long exfat_ioctl(struct file *filp, unsigned int cmd, = unsigned long arg) return exfat_ioctl_shutdown(inode->i_sb, arg); case FITRIM: return exfat_ioctl_fitrim(inode, arg); + case FS_IOC_GETFSLABEL: + return exfat_ioctl_get_volume_label(inode->i_sb, arg); + case FS_IOC_SETFSLABEL: + return exfat_ioctl_set_volume_label(inode->i_sb, arg); default: return -ENOTTY; } diff --git a/fs/exfat/namei.c b/fs/exfat/namei.c index f5f1c4e8a29f..eaa781d6263c 100644 --- a/fs/exfat/namei.c +++ b/fs/exfat/namei.c @@ -300,7 +300,7 @@ static int exfat_check_max_dentries(struct inode *inode) * the directory entry index in p_dir is returned on succeeds * -error code is returned on failure */ -static int exfat_find_empty_entry(struct inode *inode, +int exfat_find_empty_entry(struct inode *inode, struct exfat_chain *p_dir, int num_entries, struct exfat_entry_set_cache *es) { --=20 2.34.1