From: Lukas Czerner <lczerner@redhat.com>
To: Andreas Dilger <adilger@dilger.ca>
Cc: linux-ext4@vger.kernel.org, tytso@mit.edu
Subject: Re: [PATCH v2] ext4: implement support for get/set fs label
Date: Mon, 29 Nov 2021 21:49:57 +0100 [thread overview]
Message-ID: <20211129204957.u43dc5lesun32noq@work> (raw)
In-Reply-To: <5E8B9CB8-9EEE-4CB2-8DB6-DE995103B513@dilger.ca>
On Mon, Nov 29, 2021 at 01:28:09PM -0700, Andreas Dilger wrote:
> On Nov 12, 2021, at 1:20 AM, Lukas Czerner <lczerner@redhat.com> wrote:
> >
> > Implement support for FS_IOC_GETFSLABEL and FS_IOC_SETFSLABEL ioctls for
> > online reading and setting of file system label.
> >
> > ext4_ioctl_getlabel() is simple, just get the label from the primary
> > superblock bh. This might not be the first sb on the file system if
> > 'sb=' mount option is used.
> >
> > In ext4_ioctl_setlabel() we update what ext4 currently views as a
> > primary superblock and then proceed to update backup superblocks. There
> > are two caveats:
> > - the primary superblock might not be the first superblock and so it
> > might not be the one used by userspace tools if read directly
> > off the disk.
> > - because the primary superblock might not be the first superblock we
> > potentialy have to update it as part of backup superblock update.
> > However the first sb location is a bit more complicated than the rest
> > so we have to account for that.
> >
> > Tested with generic/492 with various configurations. I also checked the
> > behavior with 'sb=' mount options, including very large file systems
> > with and without sparse_super/sparse_super2.
> >
> > Signed-off-by: Lukas Czerner <lczerner@redhat.com>
> > ---
>
> One minor issue/question inline.
>
> > +static int ext4_ioctl_setlabel(struct file *filp, const char __user *user_label)
> > +{
> > + size_t len;
> > + handle_t *handle;
> > + ext4_group_t ngroups;
> > + ext4_fsblk_t sb_block;
> > + struct buffer_head *bh;
> > + int ret = 0, ret2, grp;
> > + unsigned long offset = 0;
> > + char new_label[EXT4_LABEL_MAX + 1];
> > + struct super_block *sb = file_inode(filp)->i_sb;
> > + struct ext4_sb_info *sbi = EXT4_SB(sb);
> > + struct ext4_super_block *es = sbi->s_es;
> > +
> > + /* Sanity check, this should never happen */
> > + BUILD_BUG_ON(sizeof(es->s_volume_name) < EXT4_LABEL_MAX);
> > +
> > + if (!capable(CAP_SYS_ADMIN))
> > + return -EPERM;
> > + /*
> > + * Copy the maximum length allowed for ext4 label with one more to
> > + * find the required terminating null byte in order to test the
> > + * label length. The on disk label doesn't need to be null terminated.
> > + */
> > + if (copy_from_user(new_label, user_label, EXT4_LABEL_MAX + 1))
> > + return -EFAULT;
> > +
> > + len = strnlen(new_label, EXT4_LABEL_MAX + 1);
> > + if (len > EXT4_LABEL_MAX)
> > + return -EINVAL;
> > +
> > + ret = mnt_want_write_file(filp);
> > + if (ret)
> > + return ret;
> > +
> > + handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, EXT4_MAX_TRANS_DATA);
> > + if (IS_ERR(handle)) {
> > + ret = PTR_ERR(handle);
> > + goto err_out;
> > + }
> > + /* Update the primary superblock first */
> > + ret = ext4_journal_get_write_access(handle, sb,
> > + sbi->s_sbh,
> > + EXT4_JTR_NONE);
> > + if (ret)
> > + goto err_journal;
> > +
> > + lock_buffer(sbi->s_sbh);
> > + memset(es->s_volume_name, 0, sizeof(es->s_volume_name));
> > + memcpy(es->s_volume_name, new_label, len);
>
> (minor) this introduces a very small window where s_volume_name is unset.
> Since "new_label" is already a temporary buffer of the correct size, it
> would be better IMHO to zero it out, copy the new label from userspace
> into it, and then copy EXT4_LABEL_MAX bytes of new_label to s_volume_name.
>
> It still isn't perfect, but reduces the window significantly.
Very good point, I'll fix that in the next version.
Thanks!
-Lukas
>
> > + /* Update backup superblocks */
> > + ngroups = ext4_get_groups_count(sb);
> > + for (grp = 0; grp < ngroups; grp++) {
>
> :
> :
>
> > + ext4_debug("update backup superblock %llu\n", sb_block);
> > + BUFFER_TRACE(bh, "get_write_access");
> > + ret = ext4_journal_get_write_access(handle, sb,
> > + bh,
> > + EXT4_JTR_NONE);
> > + if (ret) {
> > + brelse(bh);
> > + break;
> > + }
> > +
> > + es = (struct ext4_super_block *) (bh->b_data + offset);
> > + lock_buffer(bh);
> > + if (ext4_has_metadata_csum(sb) &&
> > + es->s_checksum != ext4_superblock_csum(sb, es)) {
> > + ext4_msg(sb, KERN_ERR, "Invalid checksum for backup "
> > + "superblock %llu\n", sb_block);
> > + unlock_buffer(bh);
> > + brelse(bh);
> > + ret = -EFSBADCRC;
> > + break;
> > + }
> > + memset(es->s_volume_name, 0, sizeof(es->s_volume_name));
> > + memcpy(es->s_volume_name, new_label, len);
>
> Same here.
>
> The rest looks fine.
>
> Cheers, Andreas
>
>
>
>
>
next prev parent reply other threads:[~2021-11-29 20:52 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-11 21:59 [PATCH] ext4: implement support for get/set fs label Lukas Czerner
2021-11-12 8:20 ` [PATCH v2] " Lukas Czerner
2021-11-29 20:28 ` Andreas Dilger
2021-11-29 20:49 ` Lukas Czerner [this message]
2021-11-30 3:00 ` Theodore Y. Ts'o
2021-11-30 9:49 ` Lukas Czerner
2021-12-01 14:39 ` Theodore Y. Ts'o
2021-12-10 15:16 ` [PATCH v3] " Lukas Czerner
2021-12-10 15:22 ` Lukas Czerner
2021-12-10 23:05 ` Theodore Y. Ts'o
2021-12-13 9:44 ` Lukas Czerner
2021-12-10 18:48 ` [PATCH v4] " Lukas Czerner
2021-12-13 13:56 ` [PATCH v5] " Lukas Czerner
2022-01-05 3:14 ` Theodore Ts'o
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211129204957.u43dc5lesun32noq@work \
--to=lczerner@redhat.com \
--cc=adilger@dilger.ca \
--cc=linux-ext4@vger.kernel.org \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).