1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* handling of writes to regular files and writing back to the server
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
8 #include <linux/backing-dev.h>
9 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include <linux/netfs.h>
15 #include <trace/events/netfs.h>
19 * completion of write to server
21 static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len)
23 _enter("{%llx:%llu},{%x @%llx}",
24 vnode->fid.vid, vnode->fid.vnode, len, start);
26 afs_prune_wb_keys(vnode);
31 * Find a key to use for the writeback. We cached the keys used to author the
32 * writes on the vnode. *_wbk will contain the last writeback key used or NULL
33 * and we need to start from there if it's set.
35 static int afs_get_writeback_key(struct afs_vnode *vnode,
36 struct afs_wb_key **_wbk)
38 struct afs_wb_key *wbk = NULL;
40 int ret = -ENOKEY, ret2;
42 spin_lock(&vnode->wb_lock);
44 p = (*_wbk)->vnode_link.next;
46 p = vnode->wb_keys.next;
48 while (p != &vnode->wb_keys) {
49 wbk = list_entry(p, struct afs_wb_key, vnode_link);
50 _debug("wbk %u", key_serial(wbk->key));
51 ret2 = key_validate(wbk->key);
53 refcount_inc(&wbk->usage);
54 _debug("USE WB KEY %u", key_serial(wbk->key));
64 spin_unlock(&vnode->wb_lock);
66 afs_put_wb_key(*_wbk);
71 static void afs_store_data_success(struct afs_operation *op)
73 struct afs_vnode *vnode = op->file[0].vnode;
75 op->ctime = op->file[0].scb.status.mtime_client;
76 afs_vnode_commit_status(op, &op->file[0]);
77 if (!afs_op_error(op)) {
78 if (!op->store.laundering)
79 afs_pages_written_back(vnode, op->store.pos, op->store.size);
80 afs_stat_v(vnode, n_stores);
81 atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes);
85 static const struct afs_operation_ops afs_store_data_operation = {
86 .issue_afs_rpc = afs_fs_store_data,
87 .issue_yfs_rpc = yfs_fs_store_data,
88 .success = afs_store_data_success,
94 static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos,
97 struct afs_operation *op;
98 struct afs_wb_key *wbk = NULL;
99 loff_t size = iov_iter_count(iter);
102 _enter("%s{%llx:%llu.%u},%llx,%llx",
109 ret = afs_get_writeback_key(vnode, &wbk);
111 _leave(" = %d [no keys]", ret);
115 op = afs_alloc_operation(wbk->key, vnode->volume);
121 afs_op_set_vnode(op, 0, vnode);
122 op->file[0].dv_delta = 1;
123 op->file[0].modification = true;
125 op->store.size = size;
126 op->store.laundering = laundering;
127 op->flags |= AFS_OPERATION_UNINTR;
128 op->ops = &afs_store_data_operation;
131 afs_begin_vnode_operation(op);
133 op->store.write_iter = iter;
134 op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
135 op->mtime = inode_get_mtime(&vnode->netfs.inode);
137 afs_wait_for_operation(op);
139 switch (afs_op_error(op)) {
148 ret = afs_get_writeback_key(vnode, &wbk);
151 op->key = key_get(wbk->key);
158 _leave(" = %d", afs_op_error(op));
159 return afs_put_operation(op);
162 static void afs_upload_to_server(struct netfs_io_subrequest *subreq)
164 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
168 subreq->rreq->debug_id, subreq->debug_index, subreq->io_iter.count);
170 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
171 ret = afs_store_data(vnode, &subreq->io_iter, subreq->start,
172 subreq->rreq->origin == NETFS_LAUNDER_WRITE);
173 netfs_write_subrequest_terminated(subreq, ret < 0 ? ret : subreq->len,
177 static void afs_upload_to_server_worker(struct work_struct *work)
179 struct netfs_io_subrequest *subreq =
180 container_of(work, struct netfs_io_subrequest, work);
182 afs_upload_to_server(subreq);
186 * Set up write requests for a writeback slice. We need to add a write request
187 * for each write we want to make.
189 void afs_create_write_requests(struct netfs_io_request *wreq, loff_t start, size_t len)
191 struct netfs_io_subrequest *subreq;
193 _enter("%x,%llx-%llx", wreq->debug_id, start, start + len);
195 subreq = netfs_create_write_request(wreq, NETFS_UPLOAD_TO_SERVER,
196 start, len, afs_upload_to_server_worker);
198 netfs_queue_write_request(subreq);
202 * write some of the pending data back to the server
204 int afs_writepages(struct address_space *mapping, struct writeback_control *wbc)
206 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
209 /* We have to be careful as we can end up racing with setattr()
210 * truncating the pagecache since the caller doesn't take a lock here
213 if (wbc->sync_mode == WB_SYNC_ALL)
214 down_read(&vnode->validate_lock);
215 else if (!down_read_trylock(&vnode->validate_lock))
218 ret = netfs_writepages(mapping, wbc);
219 up_read(&vnode->validate_lock);
224 * flush any dirty pages for this process, and check for write errors.
225 * - the return status from this call provides a reliable indication of
226 * whether any write errors occurred for this process.
228 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
230 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
231 struct afs_file *af = file->private_data;
234 _enter("{%llx:%llu},{n=%pD},%d",
235 vnode->fid.vid, vnode->fid.vnode, file,
238 ret = afs_validate(vnode, af->key);
242 return file_write_and_wait_range(file, start, end);
246 * notification that a previously read-only page is about to become writable
247 * - if it returns an error, the caller will deliver a bus error signal
249 vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
251 struct file *file = vmf->vma->vm_file;
253 if (afs_validate(AFS_FS_I(file_inode(file)), afs_file_key(file)) < 0)
254 return VM_FAULT_SIGBUS;
255 return netfs_page_mkwrite(vmf, NULL);
259 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
261 void afs_prune_wb_keys(struct afs_vnode *vnode)
263 LIST_HEAD(graveyard);
264 struct afs_wb_key *wbk, *tmp;
266 /* Discard unused keys */
267 spin_lock(&vnode->wb_lock);
269 if (!mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
270 !mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_DIRTY)) {
271 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
272 if (refcount_read(&wbk->usage) == 1)
273 list_move(&wbk->vnode_link, &graveyard);
277 spin_unlock(&vnode->wb_lock);
279 while (!list_empty(&graveyard)) {
280 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
281 list_del(&wbk->vnode_link);