Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* kiocb-using read/write
3 *
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/mount.h>
9#include <linux/slab.h>
10#include <linux/file.h>
11#include <linux/uio.h>
12#include <linux/falloc.h>
13#include <linux/sched/mm.h>
14#include <trace/events/fscache.h>
15#include "internal.h"
16
17struct cachefiles_kiocb {
18 struct kiocb iocb;
19 refcount_t ki_refcnt;
20 loff_t start;
21 union {
22 size_t skipped;
23 size_t len;
24 };
25 struct cachefiles_object *object;
26 netfs_io_terminated_t term_func;
27 void *term_func_priv;
28 bool was_async;
29 unsigned int inval_counter; /* Copy of cookie->inval_counter */
30 u64 b_writing;
31};
32
33static inline void cachefiles_put_kiocb(struct cachefiles_kiocb *ki)
34{
35 if (refcount_dec_and_test(&ki->ki_refcnt)) {
36 cachefiles_put_object(ki->object, cachefiles_obj_put_ioreq);
37 fput(ki->iocb.ki_filp);
38 kfree(ki);
39 }
40}
41
42/*
43 * Handle completion of a read from the cache.
44 */
45static void cachefiles_read_complete(struct kiocb *iocb, long ret)
46{
47 struct cachefiles_kiocb *ki = container_of(iocb, struct cachefiles_kiocb, iocb);
48 struct inode *inode = file_inode(ki->iocb.ki_filp);
49
50 _enter("%ld", ret);
51
52 if (ret < 0)
53 trace_cachefiles_io_error(ki->object, inode, ret,
54 cachefiles_trace_read_error);
55
56 if (ki->term_func) {
57 if (ret >= 0) {
58 if (ki->object->cookie->inval_counter == ki->inval_counter)
59 ki->skipped += ret;
60 else
61 ret = -ESTALE;
62 }
63
64 ki->term_func(ki->term_func_priv, ret, ki->was_async);
65 }
66
67 cachefiles_put_kiocb(ki);
68}
69
70/*
71 * Initiate a read from the cache.
72 */
73static int cachefiles_read(struct netfs_cache_resources *cres,
74 loff_t start_pos,
75 struct iov_iter *iter,
76 enum netfs_read_from_hole read_hole,
77 netfs_io_terminated_t term_func,
78 void *term_func_priv)
79{
80 struct cachefiles_object *object;
81 struct cachefiles_kiocb *ki;
82 struct file *file;
83 unsigned int old_nofs;
84 ssize_t ret = -ENOBUFS;
85 size_t len = iov_iter_count(iter), skipped = 0;
86
87 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
88 goto presubmission_error;
89
90 fscache_count_read();
91 object = cachefiles_cres_object(cres);
92 file = cachefiles_cres_file(cres);
93
94 _enter("%pD,%li,%llx,%zx/%llx",
95 file, file_inode(file)->i_ino, start_pos, len,
96 i_size_read(file_inode(file)));
97
98 /* If the caller asked us to seek for data before doing the read, then
99 * we should do that now. If we find a gap, we fill it with zeros.
100 */
101 if (read_hole != NETFS_READ_HOLE_IGNORE) {
102 loff_t off = start_pos, off2;
103
104 off2 = cachefiles_inject_read_error();
105 if (off2 == 0)
106 off2 = vfs_llseek(file, off, SEEK_DATA);
107 if (off2 < 0 && off2 >= (loff_t)-MAX_ERRNO && off2 != -ENXIO) {
108 skipped = 0;
109 ret = off2;
110 goto presubmission_error;
111 }
112
113 if (off2 == -ENXIO || off2 >= start_pos + len) {
114 /* The region is beyond the EOF or there's no more data
115 * in the region, so clear the rest of the buffer and
116 * return success.
117 */
118 ret = -ENODATA;
119 if (read_hole == NETFS_READ_HOLE_FAIL)
120 goto presubmission_error;
121
122 iov_iter_zero(len, iter);
123 skipped = len;
124 ret = 0;
125 goto presubmission_error;
126 }
127
128 skipped = off2 - off;
129 iov_iter_zero(skipped, iter);
130 }
131
132 ret = -ENOMEM;
133 ki = kzalloc(sizeof(struct cachefiles_kiocb), GFP_KERNEL);
134 if (!ki)
135 goto presubmission_error;
136
137 refcount_set(&ki->ki_refcnt, 2);
138 ki->iocb.ki_filp = file;
139 ki->iocb.ki_pos = start_pos + skipped;
140 ki->iocb.ki_flags = IOCB_DIRECT;
141 ki->iocb.ki_ioprio = get_current_ioprio();
142 ki->skipped = skipped;
143 ki->object = object;
144 ki->inval_counter = cres->inval_counter;
145 ki->term_func = term_func;
146 ki->term_func_priv = term_func_priv;
147 ki->was_async = true;
148
149 if (ki->term_func)
150 ki->iocb.ki_complete = cachefiles_read_complete;
151
152 get_file(ki->iocb.ki_filp);
153 cachefiles_grab_object(object, cachefiles_obj_get_ioreq);
154
155 trace_cachefiles_read(object, file_inode(file), ki->iocb.ki_pos, len - skipped);
156 old_nofs = memalloc_nofs_save();
157 ret = cachefiles_inject_read_error();
158 if (ret == 0)
159 ret = vfs_iocb_iter_read(file, &ki->iocb, iter);
160 memalloc_nofs_restore(old_nofs);
161 switch (ret) {
162 case -EIOCBQUEUED:
163 goto in_progress;
164
165 case -ERESTARTSYS:
166 case -ERESTARTNOINTR:
167 case -ERESTARTNOHAND:
168 case -ERESTART_RESTARTBLOCK:
169 /* There's no easy way to restart the syscall since other AIO's
170 * may be already running. Just fail this IO with EINTR.
171 */
172 ret = -EINTR;
173 fallthrough;
174 default:
175 ki->was_async = false;
176 cachefiles_read_complete(&ki->iocb, ret);
177 if (ret > 0)
178 ret = 0;
179 break;
180 }
181
182in_progress:
183 cachefiles_put_kiocb(ki);
184 _leave(" = %zd", ret);
185 return ret;
186
187presubmission_error:
188 if (term_func)
189 term_func(term_func_priv, ret < 0 ? ret : skipped, false);
190 return ret;
191}
192
193/*
194 * Query the occupancy of the cache in a region, returning where the next chunk
195 * of data starts and how long it is.
196 */
197static int cachefiles_query_occupancy(struct netfs_cache_resources *cres,
198 loff_t start, size_t len, size_t granularity,
199 loff_t *_data_start, size_t *_data_len)
200{
201 struct cachefiles_object *object;
202 struct file *file;
203 loff_t off, off2;
204
205 *_data_start = -1;
206 *_data_len = 0;
207
208 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
209 return -ENOBUFS;
210
211 object = cachefiles_cres_object(cres);
212 file = cachefiles_cres_file(cres);
213 granularity = max_t(size_t, object->volume->cache->bsize, granularity);
214
215 _enter("%pD,%li,%llx,%zx/%llx",
216 file, file_inode(file)->i_ino, start, len,
217 i_size_read(file_inode(file)));
218
219 off = cachefiles_inject_read_error();
220 if (off == 0)
221 off = vfs_llseek(file, start, SEEK_DATA);
222 if (off == -ENXIO)
223 return -ENODATA; /* Beyond EOF */
224 if (off < 0 && off >= (loff_t)-MAX_ERRNO)
225 return -ENOBUFS; /* Error. */
226 if (round_up(off, granularity) >= start + len)
227 return -ENODATA; /* No data in range */
228
229 off2 = cachefiles_inject_read_error();
230 if (off2 == 0)
231 off2 = vfs_llseek(file, off, SEEK_HOLE);
232 if (off2 == -ENXIO)
233 return -ENODATA; /* Beyond EOF */
234 if (off2 < 0 && off2 >= (loff_t)-MAX_ERRNO)
235 return -ENOBUFS; /* Error. */
236
237 /* Round away partial blocks */
238 off = round_up(off, granularity);
239 off2 = round_down(off2, granularity);
240 if (off2 <= off)
241 return -ENODATA;
242
243 *_data_start = off;
244 if (off2 > start + len)
245 *_data_len = len;
246 else
247 *_data_len = off2 - off;
248 return 0;
249}
250
251/*
252 * Handle completion of a write to the cache.
253 */
254static void cachefiles_write_complete(struct kiocb *iocb, long ret)
255{
256 struct cachefiles_kiocb *ki = container_of(iocb, struct cachefiles_kiocb, iocb);
257 struct cachefiles_object *object = ki->object;
258 struct inode *inode = file_inode(ki->iocb.ki_filp);
259
260 _enter("%ld", ret);
261
262 if (ki->was_async)
263 kiocb_end_write(iocb);
264
265 if (ret < 0)
266 trace_cachefiles_io_error(object, inode, ret,
267 cachefiles_trace_write_error);
268
269 atomic_long_sub(ki->b_writing, &object->volume->cache->b_writing);
270 set_bit(FSCACHE_COOKIE_HAVE_DATA, &object->cookie->flags);
271 if (ki->term_func)
272 ki->term_func(ki->term_func_priv, ret, ki->was_async);
273 cachefiles_put_kiocb(ki);
274}
275
276/*
277 * Initiate a write to the cache.
278 */
279int __cachefiles_write(struct cachefiles_object *object,
280 struct file *file,
281 loff_t start_pos,
282 struct iov_iter *iter,
283 netfs_io_terminated_t term_func,
284 void *term_func_priv)
285{
286 struct cachefiles_cache *cache;
287 struct cachefiles_kiocb *ki;
288 unsigned int old_nofs;
289 ssize_t ret;
290 size_t len = iov_iter_count(iter);
291
292 fscache_count_write();
293 cache = object->volume->cache;
294
295 _enter("%pD,%li,%llx,%zx/%llx",
296 file, file_inode(file)->i_ino, start_pos, len,
297 i_size_read(file_inode(file)));
298
299 ki = kzalloc(sizeof(struct cachefiles_kiocb), GFP_KERNEL);
300 if (!ki) {
301 if (term_func)
302 term_func(term_func_priv, -ENOMEM, false);
303 return -ENOMEM;
304 }
305
306 refcount_set(&ki->ki_refcnt, 2);
307 ki->iocb.ki_filp = file;
308 ki->iocb.ki_pos = start_pos;
309 ki->iocb.ki_flags = IOCB_DIRECT | IOCB_WRITE;
310 ki->iocb.ki_ioprio = get_current_ioprio();
311 ki->object = object;
312 ki->start = start_pos;
313 ki->len = len;
314 ki->term_func = term_func;
315 ki->term_func_priv = term_func_priv;
316 ki->was_async = true;
317 ki->b_writing = (len + (1 << cache->bshift) - 1) >> cache->bshift;
318
319 if (ki->term_func)
320 ki->iocb.ki_complete = cachefiles_write_complete;
321 atomic_long_add(ki->b_writing, &cache->b_writing);
322
323 get_file(ki->iocb.ki_filp);
324 cachefiles_grab_object(object, cachefiles_obj_get_ioreq);
325
326 trace_cachefiles_write(object, file_inode(file), ki->iocb.ki_pos, len);
327 old_nofs = memalloc_nofs_save();
328 ret = cachefiles_inject_write_error();
329 if (ret == 0)
330 ret = vfs_iocb_iter_write(file, &ki->iocb, iter);
331 memalloc_nofs_restore(old_nofs);
332 switch (ret) {
333 case -EIOCBQUEUED:
334 goto in_progress;
335
336 case -ERESTARTSYS:
337 case -ERESTARTNOINTR:
338 case -ERESTARTNOHAND:
339 case -ERESTART_RESTARTBLOCK:
340 /* There's no easy way to restart the syscall since other AIO's
341 * may be already running. Just fail this IO with EINTR.
342 */
343 ret = -EINTR;
344 fallthrough;
345 default:
346 ki->was_async = false;
347 cachefiles_write_complete(&ki->iocb, ret);
348 if (ret > 0)
349 ret = 0;
350 break;
351 }
352
353in_progress:
354 cachefiles_put_kiocb(ki);
355 _leave(" = %zd", ret);
356 return ret;
357}
358
359static int cachefiles_write(struct netfs_cache_resources *cres,
360 loff_t start_pos,
361 struct iov_iter *iter,
362 netfs_io_terminated_t term_func,
363 void *term_func_priv)
364{
365 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE)) {
366 if (term_func)
367 term_func(term_func_priv, -ENOBUFS, false);
368 return -ENOBUFS;
369 }
370
371 return __cachefiles_write(cachefiles_cres_object(cres),
372 cachefiles_cres_file(cres),
373 start_pos, iter,
374 term_func, term_func_priv);
375}
376
377static inline enum netfs_io_source
378cachefiles_do_prepare_read(struct netfs_cache_resources *cres,
379 loff_t start, size_t *_len, loff_t i_size,
380 unsigned long *_flags, ino_t netfs_ino)
381{
382 enum cachefiles_prepare_read_trace why;
383 struct cachefiles_object *object = NULL;
384 struct cachefiles_cache *cache;
385 struct fscache_cookie *cookie = fscache_cres_cookie(cres);
386 const struct cred *saved_cred;
387 struct file *file = cachefiles_cres_file(cres);
388 enum netfs_io_source ret = NETFS_DOWNLOAD_FROM_SERVER;
389 size_t len = *_len;
390 loff_t off, to;
391 ino_t ino = file ? file_inode(file)->i_ino : 0;
392 int rc;
393
394 _enter("%zx @%llx/%llx", len, start, i_size);
395
396 if (start >= i_size) {
397 ret = NETFS_FILL_WITH_ZEROES;
398 why = cachefiles_trace_read_after_eof;
399 goto out_no_object;
400 }
401
402 if (test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags)) {
403 __set_bit(NETFS_SREQ_COPY_TO_CACHE, _flags);
404 why = cachefiles_trace_read_no_data;
405 if (!test_bit(NETFS_SREQ_ONDEMAND, _flags))
406 goto out_no_object;
407 }
408
409 /* The object and the file may be being created in the background. */
410 if (!file) {
411 why = cachefiles_trace_read_no_file;
412 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
413 goto out_no_object;
414 file = cachefiles_cres_file(cres);
415 if (!file)
416 goto out_no_object;
417 ino = file_inode(file)->i_ino;
418 }
419
420 object = cachefiles_cres_object(cres);
421 cache = object->volume->cache;
422 cachefiles_begin_secure(cache, &saved_cred);
423retry:
424 off = cachefiles_inject_read_error();
425 if (off == 0)
426 off = vfs_llseek(file, start, SEEK_DATA);
427 if (off < 0 && off >= (loff_t)-MAX_ERRNO) {
428 if (off == (loff_t)-ENXIO) {
429 why = cachefiles_trace_read_seek_nxio;
430 goto download_and_store;
431 }
432 trace_cachefiles_io_error(object, file_inode(file), off,
433 cachefiles_trace_seek_error);
434 why = cachefiles_trace_read_seek_error;
435 goto out;
436 }
437
438 if (off >= start + len) {
439 why = cachefiles_trace_read_found_hole;
440 goto download_and_store;
441 }
442
443 if (off > start) {
444 off = round_up(off, cache->bsize);
445 len = off - start;
446 *_len = len;
447 why = cachefiles_trace_read_found_part;
448 goto download_and_store;
449 }
450
451 to = cachefiles_inject_read_error();
452 if (to == 0)
453 to = vfs_llseek(file, start, SEEK_HOLE);
454 if (to < 0 && to >= (loff_t)-MAX_ERRNO) {
455 trace_cachefiles_io_error(object, file_inode(file), to,
456 cachefiles_trace_seek_error);
457 why = cachefiles_trace_read_seek_error;
458 goto out;
459 }
460
461 if (to < start + len) {
462 if (start + len >= i_size)
463 to = round_up(to, cache->bsize);
464 else
465 to = round_down(to, cache->bsize);
466 len = to - start;
467 *_len = len;
468 }
469
470 why = cachefiles_trace_read_have_data;
471 ret = NETFS_READ_FROM_CACHE;
472 goto out;
473
474download_and_store:
475 __set_bit(NETFS_SREQ_COPY_TO_CACHE, _flags);
476 if (test_bit(NETFS_SREQ_ONDEMAND, _flags)) {
477 rc = cachefiles_ondemand_read(object, start, len);
478 if (!rc) {
479 __clear_bit(NETFS_SREQ_ONDEMAND, _flags);
480 goto retry;
481 }
482 ret = NETFS_INVALID_READ;
483 }
484out:
485 cachefiles_end_secure(cache, saved_cred);
486out_no_object:
487 trace_cachefiles_prep_read(object, start, len, *_flags, ret, why, ino, netfs_ino);
488 return ret;
489}
490
491/*
492 * Prepare a read operation, shortening it to a cached/uncached
493 * boundary as appropriate.
494 */
495static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *subreq,
496 loff_t i_size)
497{
498 return cachefiles_do_prepare_read(&subreq->rreq->cache_resources,
499 subreq->start, &subreq->len, i_size,
500 &subreq->flags, subreq->rreq->inode->i_ino);
501}
502
503/*
504 * Prepare an on-demand read operation, shortening it to a cached/uncached
505 * boundary as appropriate.
506 */
507static enum netfs_io_source
508cachefiles_prepare_ondemand_read(struct netfs_cache_resources *cres,
509 loff_t start, size_t *_len, loff_t i_size,
510 unsigned long *_flags, ino_t ino)
511{
512 return cachefiles_do_prepare_read(cres, start, _len, i_size, _flags, ino);
513}
514
515/*
516 * Prepare for a write to occur.
517 */
518int __cachefiles_prepare_write(struct cachefiles_object *object,
519 struct file *file,
520 loff_t *_start, size_t *_len, size_t upper_len,
521 bool no_space_allocated_yet)
522{
523 struct cachefiles_cache *cache = object->volume->cache;
524 loff_t start = *_start, pos;
525 size_t len = *_len;
526 int ret;
527
528 /* Round to DIO size */
529 start = round_down(*_start, PAGE_SIZE);
530 if (start != *_start || *_len > upper_len) {
531 /* Probably asked to cache a streaming write written into the
532 * pagecache when the cookie was temporarily out of service to
533 * culling.
534 */
535 fscache_count_dio_misfit();
536 return -ENOBUFS;
537 }
538
539 *_len = round_up(len, PAGE_SIZE);
540
541 /* We need to work out whether there's sufficient disk space to perform
542 * the write - but we can skip that check if we have space already
543 * allocated.
544 */
545 if (no_space_allocated_yet)
546 goto check_space;
547
548 pos = cachefiles_inject_read_error();
549 if (pos == 0)
550 pos = vfs_llseek(file, start, SEEK_DATA);
551 if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
552 if (pos == -ENXIO)
553 goto check_space; /* Unallocated tail */
554 trace_cachefiles_io_error(object, file_inode(file), pos,
555 cachefiles_trace_seek_error);
556 return pos;
557 }
558 if ((u64)pos >= (u64)start + *_len)
559 goto check_space; /* Unallocated region */
560
561 /* We have a block that's at least partially filled - if we're low on
562 * space, we need to see if it's fully allocated. If it's not, we may
563 * want to cull it.
564 */
565 if (cachefiles_has_space(cache, 0, *_len / PAGE_SIZE,
566 cachefiles_has_space_check) == 0)
567 return 0; /* Enough space to simply overwrite the whole block */
568
569 pos = cachefiles_inject_read_error();
570 if (pos == 0)
571 pos = vfs_llseek(file, start, SEEK_HOLE);
572 if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
573 trace_cachefiles_io_error(object, file_inode(file), pos,
574 cachefiles_trace_seek_error);
575 return pos;
576 }
577 if ((u64)pos >= (u64)start + *_len)
578 return 0; /* Fully allocated */
579
580 /* Partially allocated, but insufficient space: cull. */
581 fscache_count_no_write_space();
582 ret = cachefiles_inject_remove_error();
583 if (ret == 0)
584 ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
585 start, *_len);
586 if (ret < 0) {
587 trace_cachefiles_io_error(object, file_inode(file), ret,
588 cachefiles_trace_fallocate_error);
589 cachefiles_io_error_obj(object,
590 "CacheFiles: fallocate failed (%d)\n", ret);
591 ret = -EIO;
592 }
593
594 return ret;
595
596check_space:
597 return cachefiles_has_space(cache, 0, *_len / PAGE_SIZE,
598 cachefiles_has_space_for_write);
599}
600
601static int cachefiles_prepare_write(struct netfs_cache_resources *cres,
602 loff_t *_start, size_t *_len, size_t upper_len,
603 loff_t i_size, bool no_space_allocated_yet)
604{
605 struct cachefiles_object *object = cachefiles_cres_object(cres);
606 struct cachefiles_cache *cache = object->volume->cache;
607 const struct cred *saved_cred;
608 int ret;
609
610 if (!cachefiles_cres_file(cres)) {
611 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE))
612 return -ENOBUFS;
613 if (!cachefiles_cres_file(cres))
614 return -ENOBUFS;
615 }
616
617 cachefiles_begin_secure(cache, &saved_cred);
618 ret = __cachefiles_prepare_write(object, cachefiles_cres_file(cres),
619 _start, _len, upper_len,
620 no_space_allocated_yet);
621 cachefiles_end_secure(cache, saved_cred);
622 return ret;
623}
624
625/*
626 * Clean up an operation.
627 */
628static void cachefiles_end_operation(struct netfs_cache_resources *cres)
629{
630 struct file *file = cachefiles_cres_file(cres);
631
632 if (file)
633 fput(file);
634 fscache_end_cookie_access(fscache_cres_cookie(cres), fscache_access_io_end);
635}
636
637static const struct netfs_cache_ops cachefiles_netfs_cache_ops = {
638 .end_operation = cachefiles_end_operation,
639 .read = cachefiles_read,
640 .write = cachefiles_write,
641 .prepare_read = cachefiles_prepare_read,
642 .prepare_write = cachefiles_prepare_write,
643 .prepare_ondemand_read = cachefiles_prepare_ondemand_read,
644 .query_occupancy = cachefiles_query_occupancy,
645};
646
647/*
648 * Open the cache file when beginning a cache operation.
649 */
650bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
651 enum fscache_want_state want_state)
652{
653 struct cachefiles_object *object = cachefiles_cres_object(cres);
654
655 if (!cachefiles_cres_file(cres)) {
656 cres->ops = &cachefiles_netfs_cache_ops;
657 if (object->file) {
658 spin_lock(&object->lock);
659 if (!cres->cache_priv2 && object->file)
660 cres->cache_priv2 = get_file(object->file);
661 spin_unlock(&object->lock);
662 }
663 }
664
665 if (!cachefiles_cres_file(cres) && want_state != FSCACHE_WANT_PARAMS) {
666 pr_err("failed to get cres->file\n");
667 return false;
668 }
669
670 return true;
671}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* kiocb-using read/write
3 *
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/mount.h>
9#include <linux/slab.h>
10#include <linux/file.h>
11#include <linux/uio.h>
12#include <linux/bio.h>
13#include <linux/falloc.h>
14#include <linux/sched/mm.h>
15#include <trace/events/fscache.h>
16#include "internal.h"
17
18struct cachefiles_kiocb {
19 struct kiocb iocb;
20 refcount_t ki_refcnt;
21 loff_t start;
22 union {
23 size_t skipped;
24 size_t len;
25 };
26 struct cachefiles_object *object;
27 netfs_io_terminated_t term_func;
28 void *term_func_priv;
29 bool was_async;
30 unsigned int inval_counter; /* Copy of cookie->inval_counter */
31 u64 b_writing;
32};
33
34static inline void cachefiles_put_kiocb(struct cachefiles_kiocb *ki)
35{
36 if (refcount_dec_and_test(&ki->ki_refcnt)) {
37 cachefiles_put_object(ki->object, cachefiles_obj_put_ioreq);
38 fput(ki->iocb.ki_filp);
39 kfree(ki);
40 }
41}
42
43/*
44 * Handle completion of a read from the cache.
45 */
46static void cachefiles_read_complete(struct kiocb *iocb, long ret)
47{
48 struct cachefiles_kiocb *ki = container_of(iocb, struct cachefiles_kiocb, iocb);
49 struct inode *inode = file_inode(ki->iocb.ki_filp);
50
51 _enter("%ld", ret);
52
53 if (ret < 0)
54 trace_cachefiles_io_error(ki->object, inode, ret,
55 cachefiles_trace_read_error);
56
57 if (ki->term_func) {
58 if (ret >= 0) {
59 if (ki->object->cookie->inval_counter == ki->inval_counter)
60 ki->skipped += ret;
61 else
62 ret = -ESTALE;
63 }
64
65 ki->term_func(ki->term_func_priv, ret, ki->was_async);
66 }
67
68 cachefiles_put_kiocb(ki);
69}
70
71/*
72 * Initiate a read from the cache.
73 */
74static int cachefiles_read(struct netfs_cache_resources *cres,
75 loff_t start_pos,
76 struct iov_iter *iter,
77 enum netfs_read_from_hole read_hole,
78 netfs_io_terminated_t term_func,
79 void *term_func_priv)
80{
81 struct cachefiles_object *object;
82 struct cachefiles_kiocb *ki;
83 struct file *file;
84 unsigned int old_nofs;
85 ssize_t ret = -ENOBUFS;
86 size_t len = iov_iter_count(iter), skipped = 0;
87
88 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
89 goto presubmission_error;
90
91 fscache_count_read();
92 object = cachefiles_cres_object(cres);
93 file = cachefiles_cres_file(cres);
94
95 _enter("%pD,%li,%llx,%zx/%llx",
96 file, file_inode(file)->i_ino, start_pos, len,
97 i_size_read(file_inode(file)));
98
99 /* If the caller asked us to seek for data before doing the read, then
100 * we should do that now. If we find a gap, we fill it with zeros.
101 */
102 if (read_hole != NETFS_READ_HOLE_IGNORE) {
103 loff_t off = start_pos, off2;
104
105 off2 = cachefiles_inject_read_error();
106 if (off2 == 0)
107 off2 = vfs_llseek(file, off, SEEK_DATA);
108 if (off2 < 0 && off2 >= (loff_t)-MAX_ERRNO && off2 != -ENXIO) {
109 skipped = 0;
110 ret = off2;
111 goto presubmission_error;
112 }
113
114 if (off2 == -ENXIO || off2 >= start_pos + len) {
115 /* The region is beyond the EOF or there's no more data
116 * in the region, so clear the rest of the buffer and
117 * return success.
118 */
119 ret = -ENODATA;
120 if (read_hole == NETFS_READ_HOLE_FAIL)
121 goto presubmission_error;
122
123 iov_iter_zero(len, iter);
124 skipped = len;
125 ret = 0;
126 goto presubmission_error;
127 }
128
129 skipped = off2 - off;
130 iov_iter_zero(skipped, iter);
131 }
132
133 ret = -ENOMEM;
134 ki = kzalloc(sizeof(struct cachefiles_kiocb), GFP_KERNEL);
135 if (!ki)
136 goto presubmission_error;
137
138 refcount_set(&ki->ki_refcnt, 2);
139 ki->iocb.ki_filp = file;
140 ki->iocb.ki_pos = start_pos + skipped;
141 ki->iocb.ki_flags = IOCB_DIRECT;
142 ki->iocb.ki_ioprio = get_current_ioprio();
143 ki->skipped = skipped;
144 ki->object = object;
145 ki->inval_counter = cres->inval_counter;
146 ki->term_func = term_func;
147 ki->term_func_priv = term_func_priv;
148 ki->was_async = true;
149
150 if (ki->term_func)
151 ki->iocb.ki_complete = cachefiles_read_complete;
152
153 get_file(ki->iocb.ki_filp);
154 cachefiles_grab_object(object, cachefiles_obj_get_ioreq);
155
156 trace_cachefiles_read(object, file_inode(file), ki->iocb.ki_pos, len - skipped);
157 old_nofs = memalloc_nofs_save();
158 ret = cachefiles_inject_read_error();
159 if (ret == 0)
160 ret = vfs_iocb_iter_read(file, &ki->iocb, iter);
161 memalloc_nofs_restore(old_nofs);
162 switch (ret) {
163 case -EIOCBQUEUED:
164 goto in_progress;
165
166 case -ERESTARTSYS:
167 case -ERESTARTNOINTR:
168 case -ERESTARTNOHAND:
169 case -ERESTART_RESTARTBLOCK:
170 /* There's no easy way to restart the syscall since other AIO's
171 * may be already running. Just fail this IO with EINTR.
172 */
173 ret = -EINTR;
174 fallthrough;
175 default:
176 ki->was_async = false;
177 cachefiles_read_complete(&ki->iocb, ret);
178 if (ret > 0)
179 ret = 0;
180 break;
181 }
182
183in_progress:
184 cachefiles_put_kiocb(ki);
185 _leave(" = %zd", ret);
186 return ret;
187
188presubmission_error:
189 if (term_func)
190 term_func(term_func_priv, ret < 0 ? ret : skipped, false);
191 return ret;
192}
193
194/*
195 * Query the occupancy of the cache in a region, returning where the next chunk
196 * of data starts and how long it is.
197 */
198static int cachefiles_query_occupancy(struct netfs_cache_resources *cres,
199 loff_t start, size_t len, size_t granularity,
200 loff_t *_data_start, size_t *_data_len)
201{
202 struct cachefiles_object *object;
203 struct file *file;
204 loff_t off, off2;
205
206 *_data_start = -1;
207 *_data_len = 0;
208
209 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
210 return -ENOBUFS;
211
212 object = cachefiles_cres_object(cres);
213 file = cachefiles_cres_file(cres);
214 granularity = max_t(size_t, object->volume->cache->bsize, granularity);
215
216 _enter("%pD,%li,%llx,%zx/%llx",
217 file, file_inode(file)->i_ino, start, len,
218 i_size_read(file_inode(file)));
219
220 off = cachefiles_inject_read_error();
221 if (off == 0)
222 off = vfs_llseek(file, start, SEEK_DATA);
223 if (off == -ENXIO)
224 return -ENODATA; /* Beyond EOF */
225 if (off < 0 && off >= (loff_t)-MAX_ERRNO)
226 return -ENOBUFS; /* Error. */
227 if (round_up(off, granularity) >= start + len)
228 return -ENODATA; /* No data in range */
229
230 off2 = cachefiles_inject_read_error();
231 if (off2 == 0)
232 off2 = vfs_llseek(file, off, SEEK_HOLE);
233 if (off2 == -ENXIO)
234 return -ENODATA; /* Beyond EOF */
235 if (off2 < 0 && off2 >= (loff_t)-MAX_ERRNO)
236 return -ENOBUFS; /* Error. */
237
238 /* Round away partial blocks */
239 off = round_up(off, granularity);
240 off2 = round_down(off2, granularity);
241 if (off2 <= off)
242 return -ENODATA;
243
244 *_data_start = off;
245 if (off2 > start + len)
246 *_data_len = len;
247 else
248 *_data_len = off2 - off;
249 return 0;
250}
251
252/*
253 * Handle completion of a write to the cache.
254 */
255static void cachefiles_write_complete(struct kiocb *iocb, long ret)
256{
257 struct cachefiles_kiocb *ki = container_of(iocb, struct cachefiles_kiocb, iocb);
258 struct cachefiles_object *object = ki->object;
259 struct inode *inode = file_inode(ki->iocb.ki_filp);
260
261 _enter("%ld", ret);
262
263 if (ki->was_async)
264 kiocb_end_write(iocb);
265
266 if (ret < 0)
267 trace_cachefiles_io_error(object, inode, ret,
268 cachefiles_trace_write_error);
269
270 atomic_long_sub(ki->b_writing, &object->volume->cache->b_writing);
271 set_bit(FSCACHE_COOKIE_HAVE_DATA, &object->cookie->flags);
272 if (ki->term_func)
273 ki->term_func(ki->term_func_priv, ret, ki->was_async);
274 cachefiles_put_kiocb(ki);
275}
276
277/*
278 * Initiate a write to the cache.
279 */
280int __cachefiles_write(struct cachefiles_object *object,
281 struct file *file,
282 loff_t start_pos,
283 struct iov_iter *iter,
284 netfs_io_terminated_t term_func,
285 void *term_func_priv)
286{
287 struct cachefiles_cache *cache;
288 struct cachefiles_kiocb *ki;
289 unsigned int old_nofs;
290 ssize_t ret;
291 size_t len = iov_iter_count(iter);
292
293 fscache_count_write();
294 cache = object->volume->cache;
295
296 _enter("%pD,%li,%llx,%zx/%llx",
297 file, file_inode(file)->i_ino, start_pos, len,
298 i_size_read(file_inode(file)));
299
300 ki = kzalloc(sizeof(struct cachefiles_kiocb), GFP_KERNEL);
301 if (!ki) {
302 if (term_func)
303 term_func(term_func_priv, -ENOMEM, false);
304 return -ENOMEM;
305 }
306
307 refcount_set(&ki->ki_refcnt, 2);
308 ki->iocb.ki_filp = file;
309 ki->iocb.ki_pos = start_pos;
310 ki->iocb.ki_flags = IOCB_DIRECT | IOCB_WRITE;
311 ki->iocb.ki_ioprio = get_current_ioprio();
312 ki->object = object;
313 ki->start = start_pos;
314 ki->len = len;
315 ki->term_func = term_func;
316 ki->term_func_priv = term_func_priv;
317 ki->was_async = true;
318 ki->b_writing = (len + (1 << cache->bshift) - 1) >> cache->bshift;
319
320 if (ki->term_func)
321 ki->iocb.ki_complete = cachefiles_write_complete;
322 atomic_long_add(ki->b_writing, &cache->b_writing);
323
324 get_file(ki->iocb.ki_filp);
325 cachefiles_grab_object(object, cachefiles_obj_get_ioreq);
326
327 trace_cachefiles_write(object, file_inode(file), ki->iocb.ki_pos, len);
328 old_nofs = memalloc_nofs_save();
329 ret = cachefiles_inject_write_error();
330 if (ret == 0)
331 ret = vfs_iocb_iter_write(file, &ki->iocb, iter);
332 memalloc_nofs_restore(old_nofs);
333 switch (ret) {
334 case -EIOCBQUEUED:
335 goto in_progress;
336
337 case -ERESTARTSYS:
338 case -ERESTARTNOINTR:
339 case -ERESTARTNOHAND:
340 case -ERESTART_RESTARTBLOCK:
341 /* There's no easy way to restart the syscall since other AIO's
342 * may be already running. Just fail this IO with EINTR.
343 */
344 ret = -EINTR;
345 fallthrough;
346 default:
347 ki->was_async = false;
348 cachefiles_write_complete(&ki->iocb, ret);
349 if (ret > 0)
350 ret = 0;
351 break;
352 }
353
354in_progress:
355 cachefiles_put_kiocb(ki);
356 _leave(" = %zd", ret);
357 return ret;
358}
359
360static int cachefiles_write(struct netfs_cache_resources *cres,
361 loff_t start_pos,
362 struct iov_iter *iter,
363 netfs_io_terminated_t term_func,
364 void *term_func_priv)
365{
366 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE)) {
367 if (term_func)
368 term_func(term_func_priv, -ENOBUFS, false);
369 return -ENOBUFS;
370 }
371
372 return __cachefiles_write(cachefiles_cres_object(cres),
373 cachefiles_cres_file(cres),
374 start_pos, iter,
375 term_func, term_func_priv);
376}
377
378static inline enum netfs_io_source
379cachefiles_do_prepare_read(struct netfs_cache_resources *cres,
380 loff_t start, size_t *_len, loff_t i_size,
381 unsigned long *_flags, ino_t netfs_ino)
382{
383 enum cachefiles_prepare_read_trace why;
384 struct cachefiles_object *object = NULL;
385 struct cachefiles_cache *cache;
386 struct fscache_cookie *cookie = fscache_cres_cookie(cres);
387 const struct cred *saved_cred;
388 struct file *file = cachefiles_cres_file(cres);
389 enum netfs_io_source ret = NETFS_DOWNLOAD_FROM_SERVER;
390 size_t len = *_len;
391 loff_t off, to;
392 ino_t ino = file ? file_inode(file)->i_ino : 0;
393 int rc;
394
395 _enter("%zx @%llx/%llx", len, start, i_size);
396
397 if (start >= i_size) {
398 ret = NETFS_FILL_WITH_ZEROES;
399 why = cachefiles_trace_read_after_eof;
400 goto out_no_object;
401 }
402
403 if (test_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags)) {
404 __set_bit(NETFS_SREQ_COPY_TO_CACHE, _flags);
405 why = cachefiles_trace_read_no_data;
406 if (!test_bit(NETFS_SREQ_ONDEMAND, _flags))
407 goto out_no_object;
408 }
409
410 /* The object and the file may be being created in the background. */
411 if (!file) {
412 why = cachefiles_trace_read_no_file;
413 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
414 goto out_no_object;
415 file = cachefiles_cres_file(cres);
416 if (!file)
417 goto out_no_object;
418 ino = file_inode(file)->i_ino;
419 }
420
421 object = cachefiles_cres_object(cres);
422 cache = object->volume->cache;
423 cachefiles_begin_secure(cache, &saved_cred);
424retry:
425 off = cachefiles_inject_read_error();
426 if (off == 0)
427 off = vfs_llseek(file, start, SEEK_DATA);
428 if (off < 0 && off >= (loff_t)-MAX_ERRNO) {
429 if (off == (loff_t)-ENXIO) {
430 why = cachefiles_trace_read_seek_nxio;
431 goto download_and_store;
432 }
433 trace_cachefiles_io_error(object, file_inode(file), off,
434 cachefiles_trace_seek_error);
435 why = cachefiles_trace_read_seek_error;
436 goto out;
437 }
438
439 if (off >= start + len) {
440 why = cachefiles_trace_read_found_hole;
441 goto download_and_store;
442 }
443
444 if (off > start) {
445 off = round_up(off, cache->bsize);
446 len = off - start;
447 *_len = len;
448 why = cachefiles_trace_read_found_part;
449 goto download_and_store;
450 }
451
452 to = cachefiles_inject_read_error();
453 if (to == 0)
454 to = vfs_llseek(file, start, SEEK_HOLE);
455 if (to < 0 && to >= (loff_t)-MAX_ERRNO) {
456 trace_cachefiles_io_error(object, file_inode(file), to,
457 cachefiles_trace_seek_error);
458 why = cachefiles_trace_read_seek_error;
459 goto out;
460 }
461
462 if (to < start + len) {
463 if (start + len >= i_size)
464 to = round_up(to, cache->bsize);
465 else
466 to = round_down(to, cache->bsize);
467 len = to - start;
468 *_len = len;
469 }
470
471 why = cachefiles_trace_read_have_data;
472 ret = NETFS_READ_FROM_CACHE;
473 goto out;
474
475download_and_store:
476 __set_bit(NETFS_SREQ_COPY_TO_CACHE, _flags);
477 if (test_bit(NETFS_SREQ_ONDEMAND, _flags)) {
478 rc = cachefiles_ondemand_read(object, start, len);
479 if (!rc) {
480 __clear_bit(NETFS_SREQ_ONDEMAND, _flags);
481 goto retry;
482 }
483 ret = NETFS_INVALID_READ;
484 }
485out:
486 cachefiles_end_secure(cache, saved_cred);
487out_no_object:
488 trace_cachefiles_prep_read(object, start, len, *_flags, ret, why, ino, netfs_ino);
489 return ret;
490}
491
492/*
493 * Prepare a read operation, shortening it to a cached/uncached
494 * boundary as appropriate.
495 */
496static enum netfs_io_source cachefiles_prepare_read(struct netfs_io_subrequest *subreq,
497 unsigned long long i_size)
498{
499 return cachefiles_do_prepare_read(&subreq->rreq->cache_resources,
500 subreq->start, &subreq->len, i_size,
501 &subreq->flags, subreq->rreq->inode->i_ino);
502}
503
504/*
505 * Prepare an on-demand read operation, shortening it to a cached/uncached
506 * boundary as appropriate.
507 */
508static enum netfs_io_source
509cachefiles_prepare_ondemand_read(struct netfs_cache_resources *cres,
510 loff_t start, size_t *_len, loff_t i_size,
511 unsigned long *_flags, ino_t ino)
512{
513 return cachefiles_do_prepare_read(cres, start, _len, i_size, _flags, ino);
514}
515
516/*
517 * Prepare for a write to occur.
518 */
519int __cachefiles_prepare_write(struct cachefiles_object *object,
520 struct file *file,
521 loff_t *_start, size_t *_len, size_t upper_len,
522 bool no_space_allocated_yet)
523{
524 struct cachefiles_cache *cache = object->volume->cache;
525 loff_t start = *_start, pos;
526 size_t len = *_len;
527 int ret;
528
529 /* Round to DIO size */
530 start = round_down(*_start, PAGE_SIZE);
531 if (start != *_start || *_len > upper_len) {
532 /* Probably asked to cache a streaming write written into the
533 * pagecache when the cookie was temporarily out of service to
534 * culling.
535 */
536 fscache_count_dio_misfit();
537 return -ENOBUFS;
538 }
539
540 *_len = round_up(len, PAGE_SIZE);
541
542 /* We need to work out whether there's sufficient disk space to perform
543 * the write - but we can skip that check if we have space already
544 * allocated.
545 */
546 if (no_space_allocated_yet)
547 goto check_space;
548
549 pos = cachefiles_inject_read_error();
550 if (pos == 0)
551 pos = vfs_llseek(file, start, SEEK_DATA);
552 if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
553 if (pos == -ENXIO)
554 goto check_space; /* Unallocated tail */
555 trace_cachefiles_io_error(object, file_inode(file), pos,
556 cachefiles_trace_seek_error);
557 return pos;
558 }
559 if ((u64)pos >= (u64)start + *_len)
560 goto check_space; /* Unallocated region */
561
562 /* We have a block that's at least partially filled - if we're low on
563 * space, we need to see if it's fully allocated. If it's not, we may
564 * want to cull it.
565 */
566 if (cachefiles_has_space(cache, 0, *_len / PAGE_SIZE,
567 cachefiles_has_space_check) == 0)
568 return 0; /* Enough space to simply overwrite the whole block */
569
570 pos = cachefiles_inject_read_error();
571 if (pos == 0)
572 pos = vfs_llseek(file, start, SEEK_HOLE);
573 if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
574 trace_cachefiles_io_error(object, file_inode(file), pos,
575 cachefiles_trace_seek_error);
576 return pos;
577 }
578 if ((u64)pos >= (u64)start + *_len)
579 return 0; /* Fully allocated */
580
581 /* Partially allocated, but insufficient space: cull. */
582 fscache_count_no_write_space();
583 ret = cachefiles_inject_remove_error();
584 if (ret == 0)
585 ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
586 start, *_len);
587 if (ret < 0) {
588 trace_cachefiles_io_error(object, file_inode(file), ret,
589 cachefiles_trace_fallocate_error);
590 cachefiles_io_error_obj(object,
591 "CacheFiles: fallocate failed (%d)\n", ret);
592 ret = -EIO;
593 }
594
595 return ret;
596
597check_space:
598 return cachefiles_has_space(cache, 0, *_len / PAGE_SIZE,
599 cachefiles_has_space_for_write);
600}
601
602static int cachefiles_prepare_write(struct netfs_cache_resources *cres,
603 loff_t *_start, size_t *_len, size_t upper_len,
604 loff_t i_size, bool no_space_allocated_yet)
605{
606 struct cachefiles_object *object = cachefiles_cres_object(cres);
607 struct cachefiles_cache *cache = object->volume->cache;
608 const struct cred *saved_cred;
609 int ret;
610
611 if (!cachefiles_cres_file(cres)) {
612 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE))
613 return -ENOBUFS;
614 if (!cachefiles_cres_file(cres))
615 return -ENOBUFS;
616 }
617
618 cachefiles_begin_secure(cache, &saved_cred);
619 ret = __cachefiles_prepare_write(object, cachefiles_cres_file(cres),
620 _start, _len, upper_len,
621 no_space_allocated_yet);
622 cachefiles_end_secure(cache, saved_cred);
623 return ret;
624}
625
626static void cachefiles_prepare_write_subreq(struct netfs_io_subrequest *subreq)
627{
628 struct netfs_io_request *wreq = subreq->rreq;
629 struct netfs_cache_resources *cres = &wreq->cache_resources;
630 struct netfs_io_stream *stream = &wreq->io_streams[subreq->stream_nr];
631
632 _enter("W=%x[%x] %llx", wreq->debug_id, subreq->debug_index, subreq->start);
633
634 stream->sreq_max_len = MAX_RW_COUNT;
635 stream->sreq_max_segs = BIO_MAX_VECS;
636
637 if (!cachefiles_cres_file(cres)) {
638 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE))
639 return netfs_prepare_write_failed(subreq);
640 if (!cachefiles_cres_file(cres))
641 return netfs_prepare_write_failed(subreq);
642 }
643}
644
645static void cachefiles_issue_write(struct netfs_io_subrequest *subreq)
646{
647 struct netfs_io_request *wreq = subreq->rreq;
648 struct netfs_cache_resources *cres = &wreq->cache_resources;
649 struct cachefiles_object *object = cachefiles_cres_object(cres);
650 struct cachefiles_cache *cache = object->volume->cache;
651 struct netfs_io_stream *stream = &wreq->io_streams[subreq->stream_nr];
652 const struct cred *saved_cred;
653 size_t off, pre, post, len = subreq->len;
654 loff_t start = subreq->start;
655 int ret;
656
657 _enter("W=%x[%x] %llx-%llx",
658 wreq->debug_id, subreq->debug_index, start, start + len - 1);
659
660 /* We need to start on the cache granularity boundary */
661 off = start & (CACHEFILES_DIO_BLOCK_SIZE - 1);
662 if (off) {
663 pre = CACHEFILES_DIO_BLOCK_SIZE - off;
664 if (pre >= len) {
665 fscache_count_dio_misfit();
666 netfs_write_subrequest_terminated(subreq, len, false);
667 return;
668 }
669 subreq->transferred += pre;
670 start += pre;
671 len -= pre;
672 iov_iter_advance(&subreq->io_iter, pre);
673 }
674
675 /* We also need to end on the cache granularity boundary */
676 if (start + len == wreq->i_size) {
677 size_t part = len % CACHEFILES_DIO_BLOCK_SIZE;
678 size_t need = CACHEFILES_DIO_BLOCK_SIZE - part;
679
680 if (part && stream->submit_extendable_to >= need) {
681 len += need;
682 subreq->len += need;
683 subreq->io_iter.count += need;
684 }
685 }
686
687 post = len & (CACHEFILES_DIO_BLOCK_SIZE - 1);
688 if (post) {
689 len -= post;
690 if (len == 0) {
691 fscache_count_dio_misfit();
692 netfs_write_subrequest_terminated(subreq, post, false);
693 return;
694 }
695 iov_iter_truncate(&subreq->io_iter, len);
696 }
697
698 cachefiles_begin_secure(cache, &saved_cred);
699 ret = __cachefiles_prepare_write(object, cachefiles_cres_file(cres),
700 &start, &len, len, true);
701 cachefiles_end_secure(cache, saved_cred);
702 if (ret < 0) {
703 netfs_write_subrequest_terminated(subreq, ret, false);
704 return;
705 }
706
707 cachefiles_write(&subreq->rreq->cache_resources,
708 subreq->start, &subreq->io_iter,
709 netfs_write_subrequest_terminated, subreq);
710}
711
712/*
713 * Clean up an operation.
714 */
715static void cachefiles_end_operation(struct netfs_cache_resources *cres)
716{
717 struct file *file = cachefiles_cres_file(cres);
718
719 if (file)
720 fput(file);
721 fscache_end_cookie_access(fscache_cres_cookie(cres), fscache_access_io_end);
722}
723
724static const struct netfs_cache_ops cachefiles_netfs_cache_ops = {
725 .end_operation = cachefiles_end_operation,
726 .read = cachefiles_read,
727 .write = cachefiles_write,
728 .issue_write = cachefiles_issue_write,
729 .prepare_read = cachefiles_prepare_read,
730 .prepare_write = cachefiles_prepare_write,
731 .prepare_write_subreq = cachefiles_prepare_write_subreq,
732 .prepare_ondemand_read = cachefiles_prepare_ondemand_read,
733 .query_occupancy = cachefiles_query_occupancy,
734};
735
736/*
737 * Open the cache file when beginning a cache operation.
738 */
739bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
740 enum fscache_want_state want_state)
741{
742 struct cachefiles_object *object = cachefiles_cres_object(cres);
743
744 if (!cachefiles_cres_file(cres)) {
745 cres->ops = &cachefiles_netfs_cache_ops;
746 if (object->file) {
747 spin_lock(&object->lock);
748 if (!cres->cache_priv2 && object->file)
749 cres->cache_priv2 = get_file(object->file);
750 spin_unlock(&object->lock);
751 }
752 }
753
754 if (!cachefiles_cres_file(cres) && want_state != FSCACHE_WANT_PARAMS) {
755 pr_err("failed to get cres->file\n");
756 return false;
757 }
758
759 return true;
760}