2024-07-02 00:40:22 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/* Read with PG_private_2 [DEPRECATED].
|
|
|
|
*
|
|
|
|
* Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/export.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/task_io_accounting_ops.h>
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [DEPRECATED] Copy a folio to the cache with PG_private_2 set.
|
|
|
|
*/
|
2024-12-16 20:41:17 +00:00
|
|
|
static void netfs_pgpriv2_copy_folio(struct netfs_io_request *creq, struct folio *folio)
|
2024-07-02 00:40:22 +01:00
|
|
|
{
|
2024-12-16 20:41:17 +00:00
|
|
|
struct netfs_io_stream *cache = &creq->io_streams[1];
|
2024-07-02 00:40:22 +01:00
|
|
|
size_t fsize = folio_size(folio), flen = fsize;
|
|
|
|
loff_t fpos = folio_pos(folio), i_size;
|
|
|
|
bool to_eof = false;
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
|
|
|
/* netfs_perform_write() may shift i_size around the page or from out
|
|
|
|
* of the page to beyond it, but cannot move i_size into or through the
|
|
|
|
* page since we have it locked.
|
|
|
|
*/
|
2024-12-16 20:41:17 +00:00
|
|
|
i_size = i_size_read(creq->inode);
|
2024-07-02 00:40:22 +01:00
|
|
|
|
|
|
|
if (fpos >= i_size) {
|
|
|
|
/* mmap beyond eof. */
|
|
|
|
_debug("beyond eof");
|
|
|
|
folio_end_private_2(folio);
|
2024-12-16 20:41:17 +00:00
|
|
|
return;
|
2024-07-02 00:40:22 +01:00
|
|
|
}
|
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
if (fpos + fsize > creq->i_size)
|
|
|
|
creq->i_size = i_size;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
|
|
|
if (flen > i_size - fpos) {
|
|
|
|
flen = i_size - fpos;
|
|
|
|
to_eof = true;
|
|
|
|
} else if (flen == i_size - fpos) {
|
|
|
|
to_eof = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_debug("folio %zx %zx", flen, fsize);
|
|
|
|
|
|
|
|
trace_netfs_folio(folio, netfs_folio_trace_store_copy);
|
|
|
|
|
|
|
|
/* Attach the folio to the rolling buffer. */
|
2024-12-16 20:41:17 +00:00
|
|
|
if (rolling_buffer_append(&creq->buffer, folio, 0) < 0) {
|
|
|
|
clear_bit(NETFS_RREQ_FOLIO_COPY_TO_CACHE, &creq->flags);
|
|
|
|
return;
|
|
|
|
}
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-07-12 12:44:30 +01:00
|
|
|
cache->submit_extendable_to = fsize;
|
2024-07-02 00:40:22 +01:00
|
|
|
cache->submit_off = 0;
|
|
|
|
cache->submit_len = flen;
|
|
|
|
|
|
|
|
/* Attach the folio to one or more subrequests. For a big folio, we
|
|
|
|
* could end up with thousands of subrequests if the wsize is small -
|
|
|
|
* but we might need to wait during the creation of subrequests for
|
|
|
|
* network resources (eg. SMB credits).
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
ssize_t part;
|
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
creq->buffer.iter.iov_offset = cache->submit_off;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
atomic64_set(&creq->issued_to, fpos + cache->submit_off);
|
2024-07-12 12:44:30 +01:00
|
|
|
cache->submit_extendable_to = fsize - cache->submit_off;
|
2024-12-16 20:41:17 +00:00
|
|
|
part = netfs_advance_write(creq, cache, fpos + cache->submit_off,
|
2024-07-02 00:40:22 +01:00
|
|
|
cache->submit_len, to_eof);
|
|
|
|
cache->submit_off += part;
|
|
|
|
if (part > cache->submit_len)
|
|
|
|
cache->submit_len = 0;
|
|
|
|
else
|
|
|
|
cache->submit_len -= part;
|
|
|
|
} while (cache->submit_len > 0);
|
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
creq->buffer.iter.iov_offset = 0;
|
|
|
|
rolling_buffer_advance(&creq->buffer, fsize);
|
|
|
|
atomic64_set(&creq->issued_to, fpos + fsize);
|
2024-07-02 00:40:22 +01:00
|
|
|
|
|
|
|
if (flen < fsize)
|
2024-12-16 20:41:17 +00:00
|
|
|
netfs_issue_write(creq, cache);
|
2024-07-02 00:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-12-16 20:41:17 +00:00
|
|
|
* [DEPRECATED] Set up copying to the cache.
|
2024-07-02 00:40:22 +01:00
|
|
|
*/
|
2024-12-16 20:41:17 +00:00
|
|
|
static struct netfs_io_request *netfs_pgpriv2_begin_copy_to_cache(
|
|
|
|
struct netfs_io_request *rreq, struct folio *folio)
|
2024-07-02 00:40:22 +01:00
|
|
|
{
|
2024-12-16 20:41:17 +00:00
|
|
|
struct netfs_io_request *creq;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
|
|
|
if (!fscache_resources_valid(&rreq->cache_resources))
|
2024-12-16 20:41:17 +00:00
|
|
|
goto cancel;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
creq = netfs_create_write_req(rreq->mapping, NULL, folio_pos(folio),
|
2024-07-02 00:40:22 +01:00
|
|
|
NETFS_PGPRIV2_COPY_TO_CACHE);
|
2024-12-16 20:41:17 +00:00
|
|
|
if (IS_ERR(creq))
|
|
|
|
goto cancel;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
if (!creq->io_streams[1].avail)
|
|
|
|
goto cancel_put;
|
|
|
|
|
|
|
|
trace_netfs_write(creq, netfs_write_trace_copy_to_cache);
|
2024-07-02 00:40:22 +01:00
|
|
|
netfs_stat(&netfs_n_wh_copy_to_cache);
|
2024-12-16 20:41:17 +00:00
|
|
|
rreq->copy_to_cache = creq;
|
|
|
|
return creq;
|
|
|
|
|
|
|
|
cancel_put:
|
|
|
|
netfs_put_request(creq, false, netfs_rreq_trace_put_return);
|
|
|
|
cancel:
|
|
|
|
rreq->copy_to_cache = ERR_PTR(-ENOBUFS);
|
|
|
|
clear_bit(NETFS_RREQ_FOLIO_COPY_TO_CACHE, &rreq->flags);
|
|
|
|
return ERR_PTR(-ENOBUFS);
|
|
|
|
}
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
/*
|
|
|
|
* [DEPRECATED] Mark page as requiring copy-to-cache using PG_private_2 and add
|
|
|
|
* it to the copy write request.
|
|
|
|
*/
|
|
|
|
void netfs_pgpriv2_copy_to_cache(struct netfs_io_request *rreq, struct folio *folio)
|
|
|
|
{
|
|
|
|
struct netfs_io_request *creq = rreq->copy_to_cache;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
if (!creq)
|
|
|
|
creq = netfs_pgpriv2_begin_copy_to_cache(rreq, folio);
|
|
|
|
if (IS_ERR(creq))
|
|
|
|
return;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
trace_netfs_folio(folio, netfs_folio_trace_copy_to_cache);
|
|
|
|
folio_start_private_2(folio);
|
|
|
|
netfs_pgpriv2_copy_folio(creq, folio);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [DEPRECATED] End writing to the cache, flushing out any outstanding writes.
|
|
|
|
*/
|
|
|
|
void netfs_pgpriv2_end_copy_to_cache(struct netfs_io_request *rreq)
|
|
|
|
{
|
|
|
|
struct netfs_io_request *creq = rreq->copy_to_cache;
|
|
|
|
|
|
|
|
if (IS_ERR_OR_NULL(creq))
|
|
|
|
return;
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
netfs_issue_write(creq, &creq->io_streams[1]);
|
2024-07-02 00:40:22 +01:00
|
|
|
smp_wmb(); /* Write lists before ALL_QUEUED. */
|
2024-12-16 20:41:17 +00:00
|
|
|
set_bit(NETFS_RREQ_ALL_QUEUED, &creq->flags);
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
netfs_put_request(creq, false, netfs_rreq_trace_put_return);
|
|
|
|
creq->copy_to_cache = NULL;
|
2024-07-02 00:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [DEPRECATED] Remove the PG_private_2 mark from any folios we've finished
|
|
|
|
* copying.
|
|
|
|
*/
|
2024-12-16 20:41:17 +00:00
|
|
|
bool netfs_pgpriv2_unlock_copied_folios(struct netfs_io_request *creq)
|
2024-07-02 00:40:22 +01:00
|
|
|
{
|
2024-12-16 20:41:17 +00:00
|
|
|
struct folio_queue *folioq = creq->buffer.tail;
|
|
|
|
unsigned long long collected_to = creq->collected_to;
|
|
|
|
unsigned int slot = creq->buffer.first_tail_slot;
|
2024-07-02 00:40:22 +01:00
|
|
|
bool made_progress = false;
|
|
|
|
|
|
|
|
if (slot >= folioq_nr_slots(folioq)) {
|
2024-12-16 20:41:17 +00:00
|
|
|
folioq = rolling_buffer_delete_spent(&creq->buffer);
|
2024-07-02 00:40:22 +01:00
|
|
|
slot = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct folio *folio;
|
|
|
|
unsigned long long fpos, fend;
|
|
|
|
size_t fsize, flen;
|
|
|
|
|
|
|
|
folio = folioq_folio(folioq, slot);
|
|
|
|
if (WARN_ONCE(!folio_test_private_2(folio),
|
|
|
|
"R=%08x: folio %lx is not marked private_2\n",
|
2024-12-16 20:41:17 +00:00
|
|
|
creq->debug_id, folio->index))
|
2024-07-02 00:40:22 +01:00
|
|
|
trace_netfs_folio(folio, netfs_folio_trace_not_under_wback);
|
|
|
|
|
|
|
|
fpos = folio_pos(folio);
|
|
|
|
fsize = folio_size(folio);
|
|
|
|
flen = fsize;
|
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
fend = min_t(unsigned long long, fpos + flen, creq->i_size);
|
2024-07-02 00:40:22 +01:00
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
trace_netfs_collect_folio(creq, folio, fend, collected_to);
|
2024-07-02 00:40:22 +01:00
|
|
|
|
|
|
|
/* Unlock any folio we've transferred all of. */
|
|
|
|
if (collected_to < fend)
|
|
|
|
break;
|
|
|
|
|
|
|
|
trace_netfs_folio(folio, netfs_folio_trace_end_copy);
|
|
|
|
folio_end_private_2(folio);
|
2024-12-16 20:41:17 +00:00
|
|
|
creq->cleaned_to = fpos + fsize;
|
2024-07-02 00:40:22 +01:00
|
|
|
made_progress = true;
|
|
|
|
|
|
|
|
/* Clean up the head folioq. If we clear an entire folioq, then
|
|
|
|
* we can get rid of it provided it's not also the tail folioq
|
|
|
|
* being filled by the issuer.
|
|
|
|
*/
|
|
|
|
folioq_clear(folioq, slot);
|
|
|
|
slot++;
|
|
|
|
if (slot >= folioq_nr_slots(folioq)) {
|
2024-12-16 20:41:17 +00:00
|
|
|
folioq = rolling_buffer_delete_spent(&creq->buffer);
|
2024-12-16 20:40:55 +00:00
|
|
|
if (!folioq)
|
|
|
|
goto done;
|
2024-07-02 00:40:22 +01:00
|
|
|
slot = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fpos + fsize >= collected_to)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-12-16 20:41:17 +00:00
|
|
|
creq->buffer.tail = folioq;
|
2024-12-16 20:40:55 +00:00
|
|
|
done:
|
2024-12-16 20:41:17 +00:00
|
|
|
creq->buffer.first_tail_slot = slot;
|
2024-07-02 00:40:22 +01:00
|
|
|
return made_progress;
|
|
|
|
}
|