]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
08e0e7c8 | 2 | /* AFS filesystem file handling |
1da177e4 | 3 | * |
08e0e7c8 | 4 | * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. |
1da177e4 | 5 | * Written by David Howells (dhowells@redhat.com) |
1da177e4 LT |
6 | */ |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/init.h> | |
1da177e4 LT |
11 | #include <linux/fs.h> |
12 | #include <linux/pagemap.h> | |
31143d5d | 13 | #include <linux/writeback.h> |
5a0e3ad6 | 14 | #include <linux/gfp.h> |
91b467e0 | 15 | #include <linux/task_io_accounting_ops.h> |
f86196ea | 16 | #include <linux/mm.h> |
5cbf0398 | 17 | #include <linux/netfs.h> |
1da177e4 LT |
18 | #include "internal.h" |
19 | ||
1cf7a151 | 20 | static int afs_file_mmap(struct file *file, struct vm_area_struct *vma); |
416351f2 | 21 | static int afs_readpage(struct file *file, struct page *page); |
75bd228d | 22 | static int afs_symlink_readpage(struct file *file, struct page *page); |
d47992f8 LC |
23 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
24 | unsigned int length); | |
416351f2 | 25 | static int afs_releasepage(struct page *page, gfp_t gfp_flags); |
1da177e4 | 26 | |
5cbf0398 | 27 | static void afs_readahead(struct readahead_control *ractl); |
3978d816 | 28 | static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter); |
6e0e99d5 DH |
29 | static void afs_vm_open(struct vm_area_struct *area); |
30 | static void afs_vm_close(struct vm_area_struct *area); | |
31 | static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); | |
9b3f26c9 | 32 | |
00d3b7a4 DH |
33 | const struct file_operations afs_file_operations = { |
34 | .open = afs_open, | |
35 | .release = afs_release, | |
36 | .llseek = generic_file_llseek, | |
3978d816 | 37 | .read_iter = afs_file_read_iter, |
50b5551d | 38 | .write_iter = afs_file_write, |
1cf7a151 | 39 | .mmap = afs_file_mmap, |
5ffc4ef4 | 40 | .splice_read = generic_file_splice_read, |
06a17bbe | 41 | .splice_write = iter_file_splice_write, |
31143d5d | 42 | .fsync = afs_fsync, |
e8d6c554 DH |
43 | .lock = afs_lock, |
44 | .flock = afs_flock, | |
00d3b7a4 DH |
45 | }; |
46 | ||
754661f1 | 47 | const struct inode_operations afs_file_inode_operations = { |
416351f2 | 48 | .getattr = afs_getattr, |
31143d5d | 49 | .setattr = afs_setattr, |
00d3b7a4 | 50 | .permission = afs_permission, |
1da177e4 LT |
51 | }; |
52 | ||
75bd228d | 53 | const struct address_space_operations afs_file_aops = { |
416351f2 | 54 | .readpage = afs_readpage, |
5cbf0398 | 55 | .readahead = afs_readahead, |
31143d5d DH |
56 | .set_page_dirty = afs_set_page_dirty, |
57 | .launder_page = afs_launder_page, | |
416351f2 DH |
58 | .releasepage = afs_releasepage, |
59 | .invalidatepage = afs_invalidatepage, | |
15b4650e NP |
60 | .write_begin = afs_write_begin, |
61 | .write_end = afs_write_end, | |
31143d5d DH |
62 | .writepage = afs_writepage, |
63 | .writepages = afs_writepages, | |
1da177e4 LT |
64 | }; |
65 | ||
75bd228d DH |
66 | const struct address_space_operations afs_symlink_aops = { |
67 | .readpage = afs_symlink_readpage, | |
68 | .releasepage = afs_releasepage, | |
69 | .invalidatepage = afs_invalidatepage, | |
70 | }; | |
71 | ||
1cf7a151 | 72 | static const struct vm_operations_struct afs_vm_ops = { |
6e0e99d5 DH |
73 | .open = afs_vm_open, |
74 | .close = afs_vm_close, | |
1cf7a151 | 75 | .fault = filemap_fault, |
6e0e99d5 | 76 | .map_pages = afs_vm_map_pages, |
1cf7a151 DH |
77 | .page_mkwrite = afs_page_mkwrite, |
78 | }; | |
79 | ||
4343d008 DH |
80 | /* |
81 | * Discard a pin on a writeback key. | |
82 | */ | |
83 | void afs_put_wb_key(struct afs_wb_key *wbk) | |
84 | { | |
e49c7b2f | 85 | if (wbk && refcount_dec_and_test(&wbk->usage)) { |
4343d008 DH |
86 | key_put(wbk->key); |
87 | kfree(wbk); | |
88 | } | |
89 | } | |
90 | ||
91 | /* | |
92 | * Cache key for writeback. | |
93 | */ | |
94 | int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) | |
95 | { | |
96 | struct afs_wb_key *wbk, *p; | |
97 | ||
98 | wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); | |
99 | if (!wbk) | |
100 | return -ENOMEM; | |
101 | refcount_set(&wbk->usage, 2); | |
102 | wbk->key = af->key; | |
103 | ||
104 | spin_lock(&vnode->wb_lock); | |
105 | list_for_each_entry(p, &vnode->wb_keys, vnode_link) { | |
106 | if (p->key == wbk->key) | |
107 | goto found; | |
108 | } | |
109 | ||
110 | key_get(wbk->key); | |
111 | list_add_tail(&wbk->vnode_link, &vnode->wb_keys); | |
112 | spin_unlock(&vnode->wb_lock); | |
113 | af->wb = wbk; | |
114 | return 0; | |
115 | ||
116 | found: | |
117 | refcount_inc(&p->usage); | |
118 | spin_unlock(&vnode->wb_lock); | |
119 | af->wb = p; | |
120 | kfree(wbk); | |
121 | return 0; | |
122 | } | |
123 | ||
00d3b7a4 DH |
124 | /* |
125 | * open an AFS file or directory and attach a key to it | |
126 | */ | |
127 | int afs_open(struct inode *inode, struct file *file) | |
128 | { | |
129 | struct afs_vnode *vnode = AFS_FS_I(inode); | |
215804a9 | 130 | struct afs_file *af; |
00d3b7a4 | 131 | struct key *key; |
260a9803 | 132 | int ret; |
00d3b7a4 | 133 | |
3b6492df | 134 | _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); |
00d3b7a4 DH |
135 | |
136 | key = afs_request_key(vnode->volume->cell); | |
137 | if (IS_ERR(key)) { | |
215804a9 DH |
138 | ret = PTR_ERR(key); |
139 | goto error; | |
00d3b7a4 DH |
140 | } |
141 | ||
215804a9 DH |
142 | af = kzalloc(sizeof(*af), GFP_KERNEL); |
143 | if (!af) { | |
144 | ret = -ENOMEM; | |
145 | goto error_key; | |
260a9803 | 146 | } |
4343d008 | 147 | af->key = key; |
260a9803 | 148 | |
215804a9 DH |
149 | ret = afs_validate(vnode, key); |
150 | if (ret < 0) | |
151 | goto error_af; | |
152 | ||
4343d008 DH |
153 | if (file->f_mode & FMODE_WRITE) { |
154 | ret = afs_cache_wb_key(vnode, af); | |
155 | if (ret < 0) | |
156 | goto error_af; | |
157 | } | |
5a813276 DH |
158 | |
159 | if (file->f_flags & O_TRUNC) | |
160 | set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); | |
4343d008 | 161 | |
215804a9 | 162 | file->private_data = af; |
00d3b7a4 DH |
163 | _leave(" = 0"); |
164 | return 0; | |
215804a9 DH |
165 | |
166 | error_af: | |
167 | kfree(af); | |
168 | error_key: | |
169 | key_put(key); | |
170 | error: | |
171 | _leave(" = %d", ret); | |
172 | return ret; | |
00d3b7a4 DH |
173 | } |
174 | ||
175 | /* | |
176 | * release an AFS file or directory and discard its key | |
177 | */ | |
178 | int afs_release(struct inode *inode, struct file *file) | |
179 | { | |
180 | struct afs_vnode *vnode = AFS_FS_I(inode); | |
215804a9 | 181 | struct afs_file *af = file->private_data; |
a1b879ee | 182 | int ret = 0; |
00d3b7a4 | 183 | |
3b6492df | 184 | _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); |
00d3b7a4 | 185 | |
5a813276 | 186 | if ((file->f_mode & FMODE_WRITE)) |
a1b879ee | 187 | ret = vfs_fsync(file, 0); |
5a813276 | 188 | |
215804a9 | 189 | file->private_data = NULL; |
4343d008 DH |
190 | if (af->wb) |
191 | afs_put_wb_key(af->wb); | |
215804a9 DH |
192 | key_put(af->key); |
193 | kfree(af); | |
4343d008 | 194 | afs_prune_wb_keys(vnode); |
a1b879ee DH |
195 | _leave(" = %d", ret); |
196 | return ret; | |
00d3b7a4 DH |
197 | } |
198 | ||
c4508464 | 199 | /* |
5cbf0398 | 200 | * Allocate a new read record. |
c4508464 | 201 | */ |
5cbf0398 | 202 | struct afs_read *afs_alloc_read(gfp_t gfp) |
c4508464 | 203 | { |
5cbf0398 | 204 | struct afs_read *req; |
c4508464 | 205 | |
5cbf0398 DH |
206 | req = kzalloc(sizeof(struct afs_read), gfp); |
207 | if (req) | |
208 | refcount_set(&req->usage, 1); | |
c4508464 | 209 | |
5cbf0398 | 210 | return req; |
c4508464 DH |
211 | } |
212 | ||
196ee9cd DH |
213 | /* |
214 | * Dispose of a ref to a read record. | |
215 | */ | |
216 | void afs_put_read(struct afs_read *req) | |
217 | { | |
f3ddee8d | 218 | if (refcount_dec_and_test(&req->usage)) { |
c4508464 DH |
219 | if (req->cleanup) |
220 | req->cleanup(req); | |
c69bf479 | 221 | key_put(req->key); |
196ee9cd DH |
222 | kfree(req); |
223 | } | |
224 | } | |
225 | ||
dc419184 DH |
226 | static void afs_fetch_data_notify(struct afs_operation *op) |
227 | { | |
228 | struct afs_read *req = op->fetch.req; | |
5cbf0398 | 229 | struct netfs_read_subrequest *subreq = req->subreq; |
dc419184 DH |
230 | int error = op->error; |
231 | ||
232 | if (error == -ECONNABORTED) | |
233 | error = afs_abort_to_error(op->ac.abort_code); | |
234 | req->error = error; | |
235 | ||
5cbf0398 DH |
236 | if (subreq) { |
237 | __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); | |
238 | netfs_subreq_terminated(subreq, error ?: req->actual_len, false); | |
239 | req->subreq = NULL; | |
240 | } else if (req->done) { | |
dc419184 | 241 | req->done(req); |
5cbf0398 | 242 | } |
dc419184 DH |
243 | } |
244 | ||
e49c7b2f DH |
245 | static void afs_fetch_data_success(struct afs_operation *op) |
246 | { | |
247 | struct afs_vnode *vnode = op->file[0].vnode; | |
248 | ||
249 | _enter("op=%08x", op->debug_id); | |
e49c7b2f DH |
250 | afs_vnode_commit_status(op, &op->file[0]); |
251 | afs_stat_v(vnode, n_fetches); | |
252 | atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes); | |
dc419184 | 253 | afs_fetch_data_notify(op); |
e49c7b2f DH |
254 | } |
255 | ||
256 | static void afs_fetch_data_put(struct afs_operation *op) | |
257 | { | |
c4508464 | 258 | op->fetch.req->error = op->error; |
e49c7b2f DH |
259 | afs_put_read(op->fetch.req); |
260 | } | |
261 | ||
262 | static const struct afs_operation_ops afs_fetch_data_operation = { | |
263 | .issue_afs_rpc = afs_fs_fetch_data, | |
264 | .issue_yfs_rpc = yfs_fs_fetch_data, | |
265 | .success = afs_fetch_data_success, | |
728279a5 | 266 | .aborted = afs_check_for_remote_deletion, |
dc419184 | 267 | .failed = afs_fetch_data_notify, |
e49c7b2f DH |
268 | .put = afs_fetch_data_put, |
269 | }; | |
270 | ||
d2ddc776 DH |
271 | /* |
272 | * Fetch file data from the volume. | |
273 | */ | |
c69bf479 | 274 | int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req) |
d2ddc776 | 275 | { |
e49c7b2f | 276 | struct afs_operation *op; |
d2ddc776 | 277 | |
3b6492df | 278 | _enter("%s{%llx:%llu.%u},%x,,,", |
d2ddc776 DH |
279 | vnode->volume->name, |
280 | vnode->fid.vid, | |
281 | vnode->fid.vnode, | |
282 | vnode->fid.unique, | |
c69bf479 | 283 | key_serial(req->key)); |
d2ddc776 | 284 | |
c69bf479 | 285 | op = afs_alloc_operation(req->key, vnode->volume); |
5cbf0398 DH |
286 | if (IS_ERR(op)) { |
287 | if (req->subreq) | |
288 | netfs_subreq_terminated(req->subreq, PTR_ERR(op), false); | |
e49c7b2f | 289 | return PTR_ERR(op); |
5cbf0398 | 290 | } |
a58823ac | 291 | |
e49c7b2f | 292 | afs_op_set_vnode(op, 0, vnode); |
d2ddc776 | 293 | |
e49c7b2f DH |
294 | op->fetch.req = afs_get_read(req); |
295 | op->ops = &afs_fetch_data_operation; | |
296 | return afs_do_sync_operation(op); | |
d2ddc776 DH |
297 | } |
298 | ||
5cbf0398 | 299 | static void afs_req_issue_op(struct netfs_read_subrequest *subreq) |
1da177e4 | 300 | { |
5cbf0398 DH |
301 | struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode); |
302 | struct afs_read *fsreq; | |
1da177e4 | 303 | |
5cbf0398 DH |
304 | fsreq = afs_alloc_read(GFP_NOFS); |
305 | if (!fsreq) | |
306 | return netfs_subreq_terminated(subreq, -ENOMEM, false); | |
1da177e4 | 307 | |
5cbf0398 DH |
308 | fsreq->subreq = subreq; |
309 | fsreq->pos = subreq->start + subreq->transferred; | |
310 | fsreq->len = subreq->len - subreq->transferred; | |
345e1ae0 | 311 | fsreq->key = key_get(subreq->rreq->netfs_priv); |
5cbf0398 DH |
312 | fsreq->vnode = vnode; |
313 | fsreq->iter = &fsreq->def_iter; | |
03ffae90 | 314 | |
5cbf0398 DH |
315 | iov_iter_xarray(&fsreq->def_iter, READ, |
316 | &fsreq->vnode->vfs_inode.i_mapping->i_pages, | |
317 | fsreq->pos, fsreq->len); | |
c4508464 | 318 | |
5cbf0398 | 319 | afs_fetch_data(fsreq->vnode, fsreq); |
345e1ae0 | 320 | afs_put_read(fsreq); |
ec26815a | 321 | } |
1da177e4 | 322 | |
75bd228d | 323 | static int afs_symlink_readpage(struct file *file, struct page *page) |
f6d335c0 | 324 | { |
5cbf0398 DH |
325 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
326 | struct afs_read *fsreq; | |
78525c74 | 327 | struct folio *folio = page_folio(page); |
f6d335c0 AV |
328 | int ret; |
329 | ||
5cbf0398 DH |
330 | fsreq = afs_alloc_read(GFP_NOFS); |
331 | if (!fsreq) | |
91b467e0 DH |
332 | return -ENOMEM; |
333 | ||
78525c74 DH |
334 | fsreq->pos = folio_pos(folio); |
335 | fsreq->len = folio_size(folio); | |
5cbf0398 DH |
336 | fsreq->vnode = vnode; |
337 | fsreq->iter = &fsreq->def_iter; | |
338 | iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages, | |
339 | fsreq->pos, fsreq->len); | |
c4508464 | 340 | |
5cbf0398 | 341 | ret = afs_fetch_data(fsreq->vnode, fsreq); |
78525c74 DH |
342 | if (ret == 0) |
343 | SetPageUptodate(page); | |
344 | unlock_page(page); | |
5cbf0398 DH |
345 | return ret; |
346 | } | |
91b467e0 | 347 | |
5cbf0398 DH |
348 | static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file) |
349 | { | |
350 | rreq->netfs_priv = key_get(afs_file_key(file)); | |
351 | } | |
91b467e0 | 352 | |
3003bbd0 DH |
353 | static bool afs_is_cache_enabled(struct inode *inode) |
354 | { | |
2cee6fbb | 355 | return fscache_cookie_enabled(afs_vnode_cache(AFS_FS_I(inode))); |
3003bbd0 DH |
356 | } |
357 | ||
5cbf0398 DH |
358 | static int afs_begin_cache_operation(struct netfs_read_request *rreq) |
359 | { | |
2cee6fbb | 360 | #ifdef CONFIG_AFS_FSCACHE |
5cbf0398 | 361 | struct afs_vnode *vnode = AFS_FS_I(rreq->inode); |
91b467e0 | 362 | |
5cbf0398 | 363 | return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode)); |
2cee6fbb DH |
364 | #else |
365 | return -ENOBUFS; | |
366 | #endif | |
91b467e0 DH |
367 | } |
368 | ||
3003bbd0 | 369 | static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len, |
78525c74 | 370 | struct folio *folio, void **_fsdata) |
3003bbd0 DH |
371 | { |
372 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); | |
373 | ||
374 | return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0; | |
375 | } | |
376 | ||
5cbf0398 | 377 | static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv) |
1da177e4 | 378 | { |
5cbf0398 DH |
379 | key_put(netfs_priv); |
380 | } | |
f6d335c0 | 381 | |
3003bbd0 | 382 | const struct netfs_read_request_ops afs_req_ops = { |
5cbf0398 | 383 | .init_rreq = afs_init_rreq, |
3003bbd0 | 384 | .is_cache_enabled = afs_is_cache_enabled, |
5cbf0398 | 385 | .begin_cache_operation = afs_begin_cache_operation, |
3003bbd0 | 386 | .check_write_begin = afs_check_write_begin, |
5cbf0398 DH |
387 | .issue_op = afs_req_issue_op, |
388 | .cleanup = afs_priv_cleanup, | |
389 | }; | |
1da177e4 | 390 | |
5cbf0398 DH |
391 | static int afs_readpage(struct file *file, struct page *page) |
392 | { | |
78525c74 DH |
393 | struct folio *folio = page_folio(page); |
394 | ||
395 | return netfs_readpage(file, folio, &afs_req_ops, NULL); | |
5cbf0398 | 396 | } |
9b3f26c9 | 397 | |
5cbf0398 DH |
398 | static void afs_readahead(struct readahead_control *ractl) |
399 | { | |
400 | netfs_readahead(ractl, &afs_req_ops, NULL); | |
ec26815a | 401 | } |
1da177e4 | 402 | |
f86726a6 DH |
403 | /* |
404 | * Adjust the dirty region of the page on truncation or full invalidation, | |
405 | * getting rid of the markers altogether if the region is entirely invalidated. | |
406 | */ | |
78525c74 | 407 | static void afs_invalidate_dirty(struct folio *folio, unsigned int offset, |
f86726a6 DH |
408 | unsigned int length) |
409 | { | |
78525c74 | 410 | struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); |
f86726a6 DH |
411 | unsigned long priv; |
412 | unsigned int f, t, end = offset + length; | |
413 | ||
78525c74 | 414 | priv = (unsigned long)folio_get_private(folio); |
f86726a6 DH |
415 | |
416 | /* we clean up only if the entire page is being invalidated */ | |
78525c74 | 417 | if (offset == 0 && length == folio_size(folio)) |
f86726a6 DH |
418 | goto full_invalidate; |
419 | ||
420 | /* If the page was dirtied by page_mkwrite(), the PTE stays writable | |
421 | * and we don't get another notification to tell us to expand it | |
422 | * again. | |
423 | */ | |
78525c74 | 424 | if (afs_is_folio_dirty_mmapped(priv)) |
f86726a6 DH |
425 | return; |
426 | ||
427 | /* We may need to shorten the dirty region */ | |
78525c74 DH |
428 | f = afs_folio_dirty_from(folio, priv); |
429 | t = afs_folio_dirty_to(folio, priv); | |
f86726a6 DH |
430 | |
431 | if (t <= offset || f >= end) | |
432 | return; /* Doesn't overlap */ | |
433 | ||
434 | if (f < offset && t > end) | |
435 | return; /* Splits the dirty region - just absorb it */ | |
436 | ||
437 | if (f >= offset && t <= end) | |
438 | goto undirty; | |
439 | ||
440 | if (f < offset) | |
441 | t = offset; | |
442 | else | |
443 | f = end; | |
444 | if (f == t) | |
445 | goto undirty; | |
446 | ||
78525c74 DH |
447 | priv = afs_folio_dirty(folio, f, t); |
448 | folio_change_private(folio, (void *)priv); | |
449 | trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio); | |
f86726a6 DH |
450 | return; |
451 | ||
452 | undirty: | |
78525c74 DH |
453 | trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio); |
454 | folio_clear_dirty_for_io(folio); | |
f86726a6 | 455 | full_invalidate: |
78525c74 DH |
456 | trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio); |
457 | folio_detach_private(folio); | |
f86726a6 DH |
458 | } |
459 | ||
1da177e4 | 460 | /* |
9b3f26c9 DH |
461 | * invalidate part or all of a page |
462 | * - release a page and clean up its private data if offset is 0 (indicating | |
463 | * the entire page) | |
464 | */ | |
d47992f8 LC |
465 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
466 | unsigned int length) | |
9b3f26c9 | 467 | { |
78525c74 DH |
468 | struct folio *folio = page_folio(page); |
469 | ||
470 | _enter("{%lu},%u,%u", folio_index(folio), offset, length); | |
9b3f26c9 DH |
471 | |
472 | BUG_ON(!PageLocked(page)); | |
473 | ||
f86726a6 | 474 | if (PagePrivate(page)) |
78525c74 | 475 | afs_invalidate_dirty(folio, offset, length); |
9b3f26c9 | 476 | |
78525c74 | 477 | folio_wait_fscache(folio); |
9b3f26c9 DH |
478 | _leave(""); |
479 | } | |
480 | ||
481 | /* | |
482 | * release a page and clean up its private state if it's not busy | |
483 | * - return true if the page can now be released, false if not | |
1da177e4 | 484 | */ |
416351f2 | 485 | static int afs_releasepage(struct page *page, gfp_t gfp_flags) |
1da177e4 | 486 | { |
78525c74 DH |
487 | struct folio *folio = page_folio(page); |
488 | struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); | |
1da177e4 | 489 | |
3b6492df | 490 | _enter("{{%llx:%llu}[%lu],%lx},%x", |
78525c74 | 491 | vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags, |
416351f2 | 492 | gfp_flags); |
1da177e4 | 493 | |
9b3f26c9 DH |
494 | /* deny if page is being written to the cache and the caller hasn't |
495 | * elected to wait */ | |
630f5dda | 496 | #ifdef CONFIG_AFS_FSCACHE |
78525c74 | 497 | if (folio_test_fscache(folio)) { |
630f5dda DH |
498 | if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS)) |
499 | return false; | |
78525c74 | 500 | folio_wait_fscache(folio); |
630f5dda DH |
501 | } |
502 | #endif | |
503 | ||
78525c74 DH |
504 | if (folio_test_private(folio)) { |
505 | trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio); | |
506 | folio_detach_private(folio); | |
1da177e4 LT |
507 | } |
508 | ||
78525c74 | 509 | /* Indicate that the folio can be released */ |
9b3f26c9 | 510 | _leave(" = T"); |
78525c74 | 511 | return true; |
ec26815a | 512 | } |
1cf7a151 | 513 | |
6e0e99d5 DH |
514 | static void afs_add_open_mmap(struct afs_vnode *vnode) |
515 | { | |
516 | if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) { | |
517 | down_write(&vnode->volume->cell->fs_open_mmaps_lock); | |
518 | ||
519 | list_add_tail(&vnode->cb_mmap_link, | |
520 | &vnode->volume->cell->fs_open_mmaps); | |
521 | ||
522 | up_write(&vnode->volume->cell->fs_open_mmaps_lock); | |
523 | } | |
524 | } | |
525 | ||
526 | static void afs_drop_open_mmap(struct afs_vnode *vnode) | |
527 | { | |
528 | if (!atomic_dec_and_test(&vnode->cb_nr_mmap)) | |
529 | return; | |
530 | ||
531 | down_write(&vnode->volume->cell->fs_open_mmaps_lock); | |
532 | ||
533 | if (atomic_read(&vnode->cb_nr_mmap) == 0) | |
534 | list_del_init(&vnode->cb_mmap_link); | |
535 | ||
536 | up_write(&vnode->volume->cell->fs_open_mmaps_lock); | |
537 | flush_work(&vnode->cb_work); | |
538 | } | |
539 | ||
1cf7a151 DH |
540 | /* |
541 | * Handle setting up a memory mapping on an AFS file. | |
542 | */ | |
543 | static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) | |
544 | { | |
6e0e99d5 | 545 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
1cf7a151 DH |
546 | int ret; |
547 | ||
6e0e99d5 DH |
548 | afs_add_open_mmap(vnode); |
549 | ||
1cf7a151 DH |
550 | ret = generic_file_mmap(file, vma); |
551 | if (ret == 0) | |
552 | vma->vm_ops = &afs_vm_ops; | |
6e0e99d5 DH |
553 | else |
554 | afs_drop_open_mmap(vnode); | |
1cf7a151 DH |
555 | return ret; |
556 | } | |
3978d816 | 557 | |
6e0e99d5 DH |
558 | static void afs_vm_open(struct vm_area_struct *vma) |
559 | { | |
560 | afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); | |
561 | } | |
562 | ||
563 | static void afs_vm_close(struct vm_area_struct *vma) | |
564 | { | |
565 | afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); | |
566 | } | |
567 | ||
568 | static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff) | |
569 | { | |
570 | struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file)); | |
571 | struct afs_file *af = vmf->vma->vm_file->private_data; | |
572 | ||
573 | switch (afs_validate(vnode, af->key)) { | |
574 | case 0: | |
575 | return filemap_map_pages(vmf, start_pgoff, end_pgoff); | |
576 | case -ENOMEM: | |
577 | return VM_FAULT_OOM; | |
578 | case -EINTR: | |
579 | case -ERESTARTSYS: | |
580 | return VM_FAULT_RETRY; | |
581 | case -ESTALE: | |
582 | default: | |
583 | return VM_FAULT_SIGBUS; | |
584 | } | |
585 | } | |
586 | ||
3978d816 DH |
587 | static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
588 | { | |
589 | struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); | |
590 | struct afs_file *af = iocb->ki_filp->private_data; | |
591 | int ret; | |
592 | ||
593 | ret = afs_validate(vnode, af->key); | |
594 | if (ret < 0) | |
595 | return ret; | |
596 | ||
597 | return generic_file_read_iter(iocb, iter); | |
598 | } |