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