io_uring: fail links of cancelled timeouts
[linux-2.6-microblaze.git] / fs / afs / file.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS filesystem file handling
3  *
4  * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/fs.h>
12 #include <linux/pagemap.h>
13 #include <linux/writeback.h>
14 #include <linux/gfp.h>
15 #include <linux/task_io_accounting_ops.h>
16 #include <linux/mm.h>
17 #include <linux/netfs.h>
18 #include "internal.h"
19
20 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
21 static int afs_readpage(struct file *file, struct page *page);
22 static void afs_invalidatepage(struct page *page, unsigned int offset,
23                                unsigned int length);
24 static int afs_releasepage(struct page *page, gfp_t gfp_flags);
25
26 static void afs_readahead(struct readahead_control *ractl);
27
28 const struct file_operations afs_file_operations = {
29         .open           = afs_open,
30         .release        = afs_release,
31         .llseek         = generic_file_llseek,
32         .read_iter      = generic_file_read_iter,
33         .write_iter     = afs_file_write,
34         .mmap           = afs_file_mmap,
35         .splice_read    = generic_file_splice_read,
36         .splice_write   = iter_file_splice_write,
37         .fsync          = afs_fsync,
38         .lock           = afs_lock,
39         .flock          = afs_flock,
40 };
41
42 const struct inode_operations afs_file_inode_operations = {
43         .getattr        = afs_getattr,
44         .setattr        = afs_setattr,
45         .permission     = afs_permission,
46 };
47
48 const struct address_space_operations afs_fs_aops = {
49         .readpage       = afs_readpage,
50         .readahead      = afs_readahead,
51         .set_page_dirty = afs_set_page_dirty,
52         .launder_page   = afs_launder_page,
53         .releasepage    = afs_releasepage,
54         .invalidatepage = afs_invalidatepage,
55         .write_begin    = afs_write_begin,
56         .write_end      = afs_write_end,
57         .writepage      = afs_writepage,
58         .writepages     = afs_writepages,
59 };
60
61 static const struct vm_operations_struct afs_vm_ops = {
62         .fault          = filemap_fault,
63         .map_pages      = filemap_map_pages,
64         .page_mkwrite   = afs_page_mkwrite,
65 };
66
67 /*
68  * Discard a pin on a writeback key.
69  */
70 void afs_put_wb_key(struct afs_wb_key *wbk)
71 {
72         if (wbk && refcount_dec_and_test(&wbk->usage)) {
73                 key_put(wbk->key);
74                 kfree(wbk);
75         }
76 }
77
78 /*
79  * Cache key for writeback.
80  */
81 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
82 {
83         struct afs_wb_key *wbk, *p;
84
85         wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
86         if (!wbk)
87                 return -ENOMEM;
88         refcount_set(&wbk->usage, 2);
89         wbk->key = af->key;
90
91         spin_lock(&vnode->wb_lock);
92         list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
93                 if (p->key == wbk->key)
94                         goto found;
95         }
96
97         key_get(wbk->key);
98         list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
99         spin_unlock(&vnode->wb_lock);
100         af->wb = wbk;
101         return 0;
102
103 found:
104         refcount_inc(&p->usage);
105         spin_unlock(&vnode->wb_lock);
106         af->wb = p;
107         kfree(wbk);
108         return 0;
109 }
110
111 /*
112  * open an AFS file or directory and attach a key to it
113  */
114 int afs_open(struct inode *inode, struct file *file)
115 {
116         struct afs_vnode *vnode = AFS_FS_I(inode);
117         struct afs_file *af;
118         struct key *key;
119         int ret;
120
121         _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
122
123         key = afs_request_key(vnode->volume->cell);
124         if (IS_ERR(key)) {
125                 ret = PTR_ERR(key);
126                 goto error;
127         }
128
129         af = kzalloc(sizeof(*af), GFP_KERNEL);
130         if (!af) {
131                 ret = -ENOMEM;
132                 goto error_key;
133         }
134         af->key = key;
135
136         ret = afs_validate(vnode, key);
137         if (ret < 0)
138                 goto error_af;
139
140         if (file->f_mode & FMODE_WRITE) {
141                 ret = afs_cache_wb_key(vnode, af);
142                 if (ret < 0)
143                         goto error_af;
144         }
145
146         if (file->f_flags & O_TRUNC)
147                 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
148         
149         file->private_data = af;
150         _leave(" = 0");
151         return 0;
152
153 error_af:
154         kfree(af);
155 error_key:
156         key_put(key);
157 error:
158         _leave(" = %d", ret);
159         return ret;
160 }
161
162 /*
163  * release an AFS file or directory and discard its key
164  */
165 int afs_release(struct inode *inode, struct file *file)
166 {
167         struct afs_vnode *vnode = AFS_FS_I(inode);
168         struct afs_file *af = file->private_data;
169         int ret = 0;
170
171         _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
172
173         if ((file->f_mode & FMODE_WRITE))
174                 ret = vfs_fsync(file, 0);
175
176         file->private_data = NULL;
177         if (af->wb)
178                 afs_put_wb_key(af->wb);
179         key_put(af->key);
180         kfree(af);
181         afs_prune_wb_keys(vnode);
182         _leave(" = %d", ret);
183         return ret;
184 }
185
186 /*
187  * Allocate a new read record.
188  */
189 struct afs_read *afs_alloc_read(gfp_t gfp)
190 {
191         struct afs_read *req;
192
193         req = kzalloc(sizeof(struct afs_read), gfp);
194         if (req)
195                 refcount_set(&req->usage, 1);
196
197         return req;
198 }
199
200 /*
201  * Dispose of a ref to a read record.
202  */
203 void afs_put_read(struct afs_read *req)
204 {
205         if (refcount_dec_and_test(&req->usage)) {
206                 if (req->cleanup)
207                         req->cleanup(req);
208                 key_put(req->key);
209                 kfree(req);
210         }
211 }
212
213 static void afs_fetch_data_notify(struct afs_operation *op)
214 {
215         struct afs_read *req = op->fetch.req;
216         struct netfs_read_subrequest *subreq = req->subreq;
217         int error = op->error;
218
219         if (error == -ECONNABORTED)
220                 error = afs_abort_to_error(op->ac.abort_code);
221         req->error = error;
222
223         if (subreq) {
224                 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
225                 netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
226                 req->subreq = NULL;
227         } else if (req->done) {
228                 req->done(req);
229         }
230 }
231
232 static void afs_fetch_data_success(struct afs_operation *op)
233 {
234         struct afs_vnode *vnode = op->file[0].vnode;
235
236         _enter("op=%08x", op->debug_id);
237         afs_vnode_commit_status(op, &op->file[0]);
238         afs_stat_v(vnode, n_fetches);
239         atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
240         afs_fetch_data_notify(op);
241 }
242
243 static void afs_fetch_data_put(struct afs_operation *op)
244 {
245         op->fetch.req->error = op->error;
246         afs_put_read(op->fetch.req);
247 }
248
249 static const struct afs_operation_ops afs_fetch_data_operation = {
250         .issue_afs_rpc  = afs_fs_fetch_data,
251         .issue_yfs_rpc  = yfs_fs_fetch_data,
252         .success        = afs_fetch_data_success,
253         .aborted        = afs_check_for_remote_deletion,
254         .failed         = afs_fetch_data_notify,
255         .put            = afs_fetch_data_put,
256 };
257
258 /*
259  * Fetch file data from the volume.
260  */
261 int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
262 {
263         struct afs_operation *op;
264
265         _enter("%s{%llx:%llu.%u},%x,,,",
266                vnode->volume->name,
267                vnode->fid.vid,
268                vnode->fid.vnode,
269                vnode->fid.unique,
270                key_serial(req->key));
271
272         op = afs_alloc_operation(req->key, vnode->volume);
273         if (IS_ERR(op)) {
274                 if (req->subreq)
275                         netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
276                 return PTR_ERR(op);
277         }
278
279         afs_op_set_vnode(op, 0, vnode);
280
281         op->fetch.req   = afs_get_read(req);
282         op->ops         = &afs_fetch_data_operation;
283         return afs_do_sync_operation(op);
284 }
285
286 static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
287 {
288         struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
289         struct afs_read *fsreq;
290
291         fsreq = afs_alloc_read(GFP_NOFS);
292         if (!fsreq)
293                 return netfs_subreq_terminated(subreq, -ENOMEM, false);
294
295         fsreq->subreq   = subreq;
296         fsreq->pos      = subreq->start + subreq->transferred;
297         fsreq->len      = subreq->len   - subreq->transferred;
298         fsreq->key      = subreq->rreq->netfs_priv;
299         fsreq->vnode    = vnode;
300         fsreq->iter     = &fsreq->def_iter;
301
302         iov_iter_xarray(&fsreq->def_iter, READ,
303                         &fsreq->vnode->vfs_inode.i_mapping->i_pages,
304                         fsreq->pos, fsreq->len);
305
306         afs_fetch_data(fsreq->vnode, fsreq);
307 }
308
309 static int afs_symlink_readpage(struct page *page)
310 {
311         struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
312         struct afs_read *fsreq;
313         int ret;
314
315         fsreq = afs_alloc_read(GFP_NOFS);
316         if (!fsreq)
317                 return -ENOMEM;
318
319         fsreq->pos      = page->index * PAGE_SIZE;
320         fsreq->len      = PAGE_SIZE;
321         fsreq->vnode    = vnode;
322         fsreq->iter     = &fsreq->def_iter;
323         iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
324                         fsreq->pos, fsreq->len);
325
326         ret = afs_fetch_data(fsreq->vnode, fsreq);
327         page_endio(page, false, ret);
328         return ret;
329 }
330
331 static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
332 {
333         rreq->netfs_priv = key_get(afs_file_key(file));
334 }
335
336 static bool afs_is_cache_enabled(struct inode *inode)
337 {
338         struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
339
340         return fscache_cookie_enabled(cookie) && !hlist_empty(&cookie->backing_objects);
341 }
342
343 static int afs_begin_cache_operation(struct netfs_read_request *rreq)
344 {
345         struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
346
347         return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode));
348 }
349
350 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
351                                  struct page *page, void **_fsdata)
352 {
353         struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
354
355         return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
356 }
357
358 static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
359 {
360         key_put(netfs_priv);
361 }
362
363 const struct netfs_read_request_ops afs_req_ops = {
364         .init_rreq              = afs_init_rreq,
365         .is_cache_enabled       = afs_is_cache_enabled,
366         .begin_cache_operation  = afs_begin_cache_operation,
367         .check_write_begin      = afs_check_write_begin,
368         .issue_op               = afs_req_issue_op,
369         .cleanup                = afs_priv_cleanup,
370 };
371
372 static int afs_readpage(struct file *file, struct page *page)
373 {
374         if (!file)
375                 return afs_symlink_readpage(page);
376
377         return netfs_readpage(file, page, &afs_req_ops, NULL);
378 }
379
380 static void afs_readahead(struct readahead_control *ractl)
381 {
382         netfs_readahead(ractl, &afs_req_ops, NULL);
383 }
384
385 /*
386  * Adjust the dirty region of the page on truncation or full invalidation,
387  * getting rid of the markers altogether if the region is entirely invalidated.
388  */
389 static void afs_invalidate_dirty(struct page *page, unsigned int offset,
390                                  unsigned int length)
391 {
392         struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
393         unsigned long priv;
394         unsigned int f, t, end = offset + length;
395
396         priv = page_private(page);
397
398         /* we clean up only if the entire page is being invalidated */
399         if (offset == 0 && length == thp_size(page))
400                 goto full_invalidate;
401
402          /* If the page was dirtied by page_mkwrite(), the PTE stays writable
403           * and we don't get another notification to tell us to expand it
404           * again.
405           */
406         if (afs_is_page_dirty_mmapped(priv))
407                 return;
408
409         /* We may need to shorten the dirty region */
410         f = afs_page_dirty_from(page, priv);
411         t = afs_page_dirty_to(page, priv);
412
413         if (t <= offset || f >= end)
414                 return; /* Doesn't overlap */
415
416         if (f < offset && t > end)
417                 return; /* Splits the dirty region - just absorb it */
418
419         if (f >= offset && t <= end)
420                 goto undirty;
421
422         if (f < offset)
423                 t = offset;
424         else
425                 f = end;
426         if (f == t)
427                 goto undirty;
428
429         priv = afs_page_dirty(page, f, t);
430         set_page_private(page, priv);
431         trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page);
432         return;
433
434 undirty:
435         trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page);
436         clear_page_dirty_for_io(page);
437 full_invalidate:
438         trace_afs_page_dirty(vnode, tracepoint_string("inval"), page);
439         detach_page_private(page);
440 }
441
442 /*
443  * invalidate part or all of a page
444  * - release a page and clean up its private data if offset is 0 (indicating
445  *   the entire page)
446  */
447 static void afs_invalidatepage(struct page *page, unsigned int offset,
448                                unsigned int length)
449 {
450         _enter("{%lu},%u,%u", page->index, offset, length);
451
452         BUG_ON(!PageLocked(page));
453
454         if (PagePrivate(page))
455                 afs_invalidate_dirty(page, offset, length);
456
457         wait_on_page_fscache(page);
458         _leave("");
459 }
460
461 /*
462  * release a page and clean up its private state if it's not busy
463  * - return true if the page can now be released, false if not
464  */
465 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
466 {
467         struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
468
469         _enter("{{%llx:%llu}[%lu],%lx},%x",
470                vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
471                gfp_flags);
472
473         /* deny if page is being written to the cache and the caller hasn't
474          * elected to wait */
475 #ifdef CONFIG_AFS_FSCACHE
476         if (PageFsCache(page)) {
477                 if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS))
478                         return false;
479                 wait_on_page_fscache(page);
480         }
481 #endif
482
483         if (PagePrivate(page)) {
484                 trace_afs_page_dirty(vnode, tracepoint_string("rel"), page);
485                 detach_page_private(page);
486         }
487
488         /* indicate that the page can be released */
489         _leave(" = T");
490         return 1;
491 }
492
493 /*
494  * Handle setting up a memory mapping on an AFS file.
495  */
496 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
497 {
498         int ret;
499
500         ret = generic_file_mmap(file, vma);
501         if (ret == 0)
502                 vma->vm_ops = &afs_vm_ops;
503         return ret;
504 }