Commit 2de16041 authored by David Howells's avatar David Howells

netfs: Change ->init_request() to return an error code

Change the request initialisation function to return an error code so that
the network filesystem can return a failure (ENOMEM, for example).

This will also allow ceph to abort a ->readahead() op if the server refuses
to give it a cap allowing local caching from within the netfslib framework
(errors aren't passed back through ->readahead(), so returning, say,
-ENOBUFS will cause the op to be aborted).
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
Reviewed-by: default avatarJeff Layton <jlayton@kernel.org>
cc: linux-cachefs@redhat.com

Link: https://lore.kernel.org/r/164678212401.1200972.16537041523832944934.stgit@warthog.procyon.org.uk/ # v2
Link: https://lore.kernel.org/r/164692905398.2099075.5238033621684646524.stgit@warthog.procyon.org.uk/ # v3
parent 663dfb65
...@@ -56,12 +56,13 @@ static void v9fs_issue_read(struct netfs_io_subrequest *subreq) ...@@ -56,12 +56,13 @@ static void v9fs_issue_read(struct netfs_io_subrequest *subreq)
* @rreq: The read request * @rreq: The read request
* @file: The file being read from * @file: The file being read from
*/ */
static void v9fs_init_request(struct netfs_io_request *rreq, struct file *file) static int v9fs_init_request(struct netfs_io_request *rreq, struct file *file)
{ {
struct p9_fid *fid = file->private_data; struct p9_fid *fid = file->private_data;
refcount_inc(&fid->count); refcount_inc(&fid->count);
rreq->netfs_priv = fid; rreq->netfs_priv = fid;
return 0;
} }
/** /**
......
...@@ -359,9 +359,10 @@ static int afs_symlink_readpage(struct file *file, struct page *page) ...@@ -359,9 +359,10 @@ static int afs_symlink_readpage(struct file *file, struct page *page)
return ret; return ret;
} }
static void afs_init_request(struct netfs_io_request *rreq, struct file *file) static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
{ {
rreq->netfs_priv = key_get(afs_file_key(file)); rreq->netfs_priv = key_get(afs_file_key(file));
return 0;
} }
static bool afs_is_cache_enabled(struct inode *inode) static bool afs_is_cache_enabled(struct inode *inode)
......
...@@ -20,27 +20,34 @@ struct netfs_io_request *netfs_alloc_request(struct address_space *mapping, ...@@ -20,27 +20,34 @@ struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
{ {
static atomic_t debug_ids; static atomic_t debug_ids;
struct netfs_io_request *rreq; struct netfs_io_request *rreq;
int ret;
rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL); rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
if (rreq) { if (!rreq)
rreq->start = start; return ERR_PTR(-ENOMEM);
rreq->len = len;
rreq->origin = origin; rreq->start = start;
rreq->netfs_ops = ops; rreq->len = len;
rreq->netfs_priv = netfs_priv; rreq->origin = origin;
rreq->mapping = mapping; rreq->netfs_ops = ops;
rreq->inode = file_inode(file); rreq->netfs_priv = netfs_priv;
rreq->i_size = i_size_read(rreq->inode); rreq->mapping = mapping;
rreq->debug_id = atomic_inc_return(&debug_ids); rreq->inode = file_inode(file);
INIT_LIST_HEAD(&rreq->subrequests); rreq->i_size = i_size_read(rreq->inode);
INIT_WORK(&rreq->work, netfs_rreq_work); rreq->debug_id = atomic_inc_return(&debug_ids);
refcount_set(&rreq->ref, 1); INIT_LIST_HEAD(&rreq->subrequests);
__set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags); INIT_WORK(&rreq->work, netfs_rreq_work);
if (ops->init_request) refcount_set(&rreq->ref, 1);
ops->init_request(rreq, file); __set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
netfs_stat(&netfs_n_rh_rreq); if (rreq->netfs_ops->init_request) {
ret = rreq->netfs_ops->init_request(rreq, file);
if (ret < 0) {
kfree(rreq);
return ERR_PTR(ret);
}
} }
netfs_stat(&netfs_n_rh_rreq);
return rreq; return rreq;
} }
......
...@@ -768,7 +768,7 @@ void netfs_readahead(struct readahead_control *ractl, ...@@ -768,7 +768,7 @@ void netfs_readahead(struct readahead_control *ractl,
readahead_pos(ractl), readahead_pos(ractl),
readahead_length(ractl), readahead_length(ractl),
NETFS_READAHEAD); NETFS_READAHEAD);
if (!rreq) if (IS_ERR(rreq))
goto cleanup; goto cleanup;
if (ops->begin_cache_operation) { if (ops->begin_cache_operation) {
...@@ -842,11 +842,9 @@ int netfs_readpage(struct file *file, ...@@ -842,11 +842,9 @@ int netfs_readpage(struct file *file,
rreq = netfs_alloc_request(folio->mapping, file, ops, netfs_priv, rreq = netfs_alloc_request(folio->mapping, file, ops, netfs_priv,
folio_file_pos(folio), folio_size(folio), folio_file_pos(folio), folio_size(folio),
NETFS_READPAGE); NETFS_READPAGE);
if (!rreq) { if (IS_ERR(rreq)) {
if (netfs_priv) ret = PTR_ERR(rreq);
ops->cleanup(folio_file_mapping(folio), netfs_priv); goto alloc_error;
folio_unlock(folio);
return -ENOMEM;
} }
if (ops->begin_cache_operation) { if (ops->begin_cache_operation) {
...@@ -887,6 +885,11 @@ int netfs_readpage(struct file *file, ...@@ -887,6 +885,11 @@ int netfs_readpage(struct file *file,
out: out:
netfs_put_request(rreq, false, netfs_rreq_trace_put_hold); netfs_put_request(rreq, false, netfs_rreq_trace_put_hold);
return ret; return ret;
alloc_error:
if (netfs_priv)
ops->cleanup(folio_file_mapping(folio), netfs_priv);
folio_unlock(folio);
return ret;
} }
EXPORT_SYMBOL(netfs_readpage); EXPORT_SYMBOL(netfs_readpage);
...@@ -1007,12 +1010,13 @@ int netfs_write_begin(struct file *file, struct address_space *mapping, ...@@ -1007,12 +1010,13 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
goto have_folio_no_wait; goto have_folio_no_wait;
} }
ret = -ENOMEM;
rreq = netfs_alloc_request(mapping, file, ops, netfs_priv, rreq = netfs_alloc_request(mapping, file, ops, netfs_priv,
folio_file_pos(folio), folio_size(folio), folio_file_pos(folio), folio_size(folio),
NETFS_READ_FOR_WRITE); NETFS_READ_FOR_WRITE);
if (!rreq) if (IS_ERR(rreq)) {
ret = PTR_ERR(rreq);
goto error; goto error;
}
rreq->no_unlock_folio = folio_index(folio); rreq->no_unlock_folio = folio_index(folio);
__set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags); __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags);
netfs_priv = NULL; netfs_priv = NULL;
......
...@@ -193,7 +193,7 @@ struct netfs_io_request { ...@@ -193,7 +193,7 @@ struct netfs_io_request {
*/ */
struct netfs_request_ops { struct netfs_request_ops {
bool (*is_cache_enabled)(struct inode *inode); bool (*is_cache_enabled)(struct inode *inode);
void (*init_request)(struct netfs_io_request *rreq, struct file *file); int (*init_request)(struct netfs_io_request *rreq, struct file *file);
int (*begin_cache_operation)(struct netfs_io_request *rreq); int (*begin_cache_operation)(struct netfs_io_request *rreq);
void (*expand_readahead)(struct netfs_io_request *rreq); void (*expand_readahead)(struct netfs_io_request *rreq);
bool (*clamp_length)(struct netfs_io_subrequest *subreq); bool (*clamp_length)(struct netfs_io_subrequest *subreq);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment