linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oleksandr Natalenko <oleksandr@natalenko.name>
To: linux-fsdevel@vger.kernel.org,
	Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Cc: viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org,
	pali@kernel.org, dsterba@suse.cz, aaptel@suse.com,
	willy@infradead.org, rdunlap@infradead.org, joe@perches.com,
	mark@harmstone.com, nborisov@suse.com,
	linux-ntfs-dev@lists.sourceforge.net, anton@tuxera.com,
	dan.carpenter@oracle.com, hch@lst.de, ebiggers@kernel.org,
	andy.lavr@gmail.com, kari.argillander@gmail.com,
	Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Subject: Re: [PATCH v26 04/10] fs/ntfs3: Add file operations and implementation
Date: Mon, 12 Jul 2021 10:28:44 +0200	[thread overview]
Message-ID: <1963819.OlitOqP6fi@spock> (raw)
In-Reply-To: <20210402155347.64594-5-almaz.alexandrovich@paragon-software.com>

Hello.

On pátek 2. dubna 2021 17:53:41 CEST Konstantin Komarov wrote:
> ...
> +/*helper for ntfs_file_write_iter (compressed files)*/
> +static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter
> *from) +{
> +	int err;
> +	struct file *file = iocb->ki_filp;
> +	size_t count = iov_iter_count(from);
> +	loff_t pos = iocb->ki_pos;
> +	struct inode *inode = file_inode(file);
> +	loff_t i_size = inode->i_size;
> +	struct address_space *mapping = inode->i_mapping;
> +	struct ntfs_inode *ni = ntfs_i(inode);
> +	u64 valid = ni->i_valid;
> +	struct ntfs_sb_info *sbi = ni->mi.sbi;
> +	struct page *page, **pages = NULL;
> +	size_t written = 0;
> +	u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
> +	u32 frame_size = 1u << frame_bits;
> +	u32 pages_per_frame = frame_size >> PAGE_SHIFT;
> +	u32 ip, off;
> +	CLST frame;
> +	u64 frame_vbo;
> +	pgoff_t index;
> +	bool frame_uptodate;
> +
> +	if (frame_size < PAGE_SIZE) {
> +		/*
> +		 * frame_size == 8K if cluster 512
> +		 * frame_size == 64K if cluster 4096
> +		 */
> +		ntfs_inode_warn(inode, "page size is bigger than frame size");
> +		return -EOPNOTSUPP;
> +	}
> +
> +	pages = ntfs_malloc(pages_per_frame * sizeof(struct page *));
> +	if (!pages)
> +		return -ENOMEM;
> +
> +	current->backing_dev_info = inode_to_bdi(inode);
> +	err = file_remove_privs(file);
> +	if (err)
> +		goto out;
> +
> +	err = file_update_time(file);
> +	if (err)
> +		goto out;
> +
> +	/* zero range [valid : pos) */
> +	while (valid < pos) {
> +		CLST lcn, clen;
> +
> +		frame = valid >> frame_bits;
> +		frame_vbo = valid & ~(frame_size - 1);
> +		off = valid & (frame_size - 1);
> +
> +		err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 0, 
&lcn,
> +					  &clen, NULL);
> +		if (err)
> +			goto out;
> +
> +		if (lcn == SPARSE_LCN) {
> +			ni->i_valid = valid =
> +				frame_vbo + ((u64)clen << sbi->cluster_bits);
> +			continue;
> +		}
> +
> +		/* Load full frame */
> +		err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
> +					   pages, pages_per_frame,
> +					   &frame_uptodate);
> +		if (err)
> +			goto out;
> +
> +		if (!frame_uptodate && off) {
> +			err = ni_read_frame(ni, frame_vbo, pages,
> +					    pages_per_frame);
> +			if (err) {
> +				for (ip = 0; ip < pages_per_frame; ip++) {
> +					page = pages[ip];
> +					unlock_page(page);
> +					put_page(page);
> +				}
> +				goto out;
> +			}
> +		}
> +
> +		ip = off >> PAGE_SHIFT;
> +		off = offset_in_page(valid);
> +		for (; ip < pages_per_frame; ip++, off = 0) {
> +			page = pages[ip];
> +			zero_user_segment(page, off, PAGE_SIZE);
> +			flush_dcache_page(page);
> +			SetPageUptodate(page);
> +		}
> +
> +		ni_lock(ni);
> +		err = ni_write_frame(ni, pages, pages_per_frame);
> +		ni_unlock(ni);
> +
> +		for (ip = 0; ip < pages_per_frame; ip++) {
> +			page = pages[ip];
> +			SetPageUptodate(page);
> +			unlock_page(page);
> +			put_page(page);
> +		}
> +
> +		if (err)
> +			goto out;
> +
> +		ni->i_valid = valid = frame_vbo + frame_size;
> +	}
> +
> +	/* copy user data [pos : pos + count) */
> +	while (count) {
> +		size_t copied, bytes;
> +
> +		off = pos & (frame_size - 1);
> +		bytes = frame_size - off;
> +		if (bytes > count)
> +			bytes = count;
> +
> +		frame = pos >> frame_bits;
> +		frame_vbo = pos & ~(frame_size - 1);
> +		index = frame_vbo >> PAGE_SHIFT;
> +
> +		if (unlikely(iov_iter_fault_in_readable(from, bytes))) {
> +			err = -EFAULT;
> +			goto out;
> +		}
> +
> +		/* Load full frame */
> +		err = ntfs_get_frame_pages(mapping, index, pages,
> +					   pages_per_frame, &frame_uptodate);
> +		if (err)
> +			goto out;
> +
> +		if (!frame_uptodate) {
> +			loff_t to = pos + bytes;
> +
> +			if (off || (to < i_size && (to & (frame_size - 1)))) {
> +				err = ni_read_frame(ni, frame_vbo, pages,
> +						    pages_per_frame);
> +				if (err) {
> +					for (ip = 0; ip < pages_per_frame;
> +					     ip++) {
> +						page = pages[ip];
> +						unlock_page(page);
> +						put_page(page);
> +					}
> +					goto out;
> +				}
> +			}
> +		}
> +
> +		WARN_ON(!bytes);
> +		copied = 0;
> +		ip = off >> PAGE_SHIFT;
> +		off = offset_in_page(pos);
> +
> +		/* copy user data to pages */
> +		for (;;) {
> +			size_t cp, tail = PAGE_SIZE - off;
> +
> +			page = pages[ip];
> +			cp = iov_iter_copy_from_user_atomic(page, from, off,
> +							    min(tail, bytes));

For 5.14, iov_iter_copy_from_user_atomic() has to be replaced by 
copy_page_from_iter_atomic().

> +			flush_dcache_page(page);
> +			iov_iter_advance(from, cp);

And iov_iter_advance() should be removed then.

Please see upstream commit f0b65f39ac505e8f1dcdaa165aa7b8c0bd6fd454 for 
detailed explanation.

> +			copied += cp;
> +			bytes -= cp;
> +			if (!bytes || !cp)
> +				break;
> +
> +			if (cp < tail) {
> +				off += cp;
> +			} else {
> +				ip++;
> +				off = 0;
> +			}
> +		}
> +
> +		ni_lock(ni);
> +		err = ni_write_frame(ni, pages, pages_per_frame);
> +		ni_unlock(ni);
> +
> +		for (ip = 0; ip < pages_per_frame; ip++) {
> +			page = pages[ip];
> +			ClearPageDirty(page);
> +			SetPageUptodate(page);
> +			unlock_page(page);
> +			put_page(page);
> +		}
> +
> +		if (err)
> +			goto out;
> +
> +		/*
> +		 * We can loop for a long time in here. Be nice and allow
> +		 * us to schedule out to avoid softlocking if preempt
> +		 * is disabled.
> +		 */
> +		cond_resched();
> +
> +		pos += copied;
> +		written += copied;
> +
> +		count = iov_iter_count(from);
> +	}
> +
> +out:
> +	ntfs_free(pages);
> +
> +	current->backing_dev_info = NULL;
> +
> +	if (err < 0)
> +		return err;
> +
> +	iocb->ki_pos += written;
> +	if (iocb->ki_pos > ni->i_valid)
> +		ni->i_valid = iocb->ki_pos;
> +
> +	return written;
> +}
> ...

Thanks.

-- 
Oleksandr Natalenko (post-factum)



  reply	other threads:[~2021-07-12  8:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-02 15:53 [PATCH v26 00/10] NTFS read-write driver GPL implementation by Paragon Software Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 01/10] fs/ntfs3: Add headers and misc files Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 02/10] fs/ntfs3: Add initialization of super block Konstantin Komarov
2021-05-20 16:51   ` Darrick J. Wong
2021-04-02 15:53 ` [PATCH v26 03/10] fs/ntfs3: Add bitmap Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 04/10] fs/ntfs3: Add file operations and implementation Konstantin Komarov
2021-07-12  8:28   ` Oleksandr Natalenko [this message]
2021-04-02 15:53 ` [PATCH v26 05/10] fs/ntfs3: Add attrib operations Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 06/10] fs/ntfs3: Add compression Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 07/10] fs/ntfs3: Add NTFS journal Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 08/10] fs/ntfs3: Add Kconfig, Makefile and doc Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 09/10] fs/ntfs3: Add NTFS3 in fs/Kconfig and fs/Makefile Konstantin Komarov
2021-04-02 15:53 ` [PATCH v26 10/10] fs/ntfs3: Add MAINTAINERS Konstantin Komarov
2021-05-19  3:47 ` [PATCH v26 00/10] NTFS read-write driver GPL implementation by Paragon Software Neal Gompa
2021-05-19  3:51   ` Neal Gompa
2021-05-20 16:13 ` Darrick J. Wong
2021-07-17 16:36 ` Pali Rohár
2021-07-25 17:02 ` Yan Pas

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=1963819.OlitOqP6fi@spock \
    --to=oleksandr@natalenko.name \
    --cc=aaptel@suse.com \
    --cc=almaz.alexandrovich@paragon-software.com \
    --cc=andy.lavr@gmail.com \
    --cc=anton@tuxera.com \
    --cc=dan.carpenter@oracle.com \
    --cc=dsterba@suse.cz \
    --cc=ebiggers@kernel.org \
    --cc=hch@lst.de \
    --cc=joe@perches.com \
    --cc=kari.argillander@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-ntfs-dev@lists.sourceforge.net \
    --cc=mark@harmstone.com \
    --cc=nborisov@suse.com \
    --cc=pali@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /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).