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 <trace/events/netfs.h>
17 #include "internal.h"
18
19 struct cachefiles_kiocb {
20 struct kiocb iocb;
21 refcount_t ki_refcnt;
22 uoff_t start;
23 union {
24 size_t skipped;
25 size_t len;
26 };
27 struct cachefiles_object *object;
28 netfs_io_terminated_t term_func;
29 void *term_func_priv;
30 bool was_async;
31 unsigned int inval_counter; /* Copy of cookie->inval_counter */
32 u64 b_writing;
33 };
34
35 #define IS_ERR_VALUE_LL(x) unlikely((x) >= (unsigned long long)-MAX_ERRNO)
36
cachefiles_put_kiocb(struct cachefiles_kiocb * ki)37 static inline void cachefiles_put_kiocb(struct cachefiles_kiocb *ki)
38 {
39 if (refcount_dec_and_test(&ki->ki_refcnt)) {
40 cachefiles_put_object(ki->object, cachefiles_obj_put_ioreq);
41 fput(ki->iocb.ki_filp);
42 kfree(ki);
43 }
44 }
45
46 /*
47 * Handle completion of a read from the cache.
48 */
cachefiles_read_complete(struct kiocb * iocb,long ret)49 static void cachefiles_read_complete(struct kiocb *iocb, long ret)
50 {
51 struct cachefiles_kiocb *ki = container_of(iocb, struct cachefiles_kiocb, iocb);
52 struct inode *inode = file_inode(ki->iocb.ki_filp);
53
54 _enter("%ld", ret);
55
56 if (ret < 0)
57 trace_cachefiles_io_error(ki->object, inode, ret,
58 cachefiles_trace_read_error);
59
60 if (ki->term_func) {
61 if (ret >= 0) {
62 if (ki->object->cookie->inval_counter == ki->inval_counter)
63 ki->skipped += ret;
64 else
65 ret = -ESTALE;
66 }
67
68 ki->term_func(ki->term_func_priv, ret);
69 }
70
71 cachefiles_put_kiocb(ki);
72 }
73
74 /*
75 * Initiate a read from the cache.
76 */
cachefiles_read(struct netfs_cache_resources * cres,uoff_t start_pos,struct iov_iter * iter,enum netfs_read_from_hole read_hole,netfs_io_terminated_t term_func,void * term_func_priv)77 static int cachefiles_read(struct netfs_cache_resources *cres,
78 uoff_t start_pos,
79 struct iov_iter *iter,
80 enum netfs_read_from_hole read_hole,
81 netfs_io_terminated_t term_func,
82 void *term_func_priv)
83 {
84 struct cachefiles_object *object;
85 struct cachefiles_kiocb *ki;
86 struct file *file;
87 unsigned int old_nofs;
88 ssize_t ret = -ENOBUFS;
89 size_t len = iov_iter_count(iter), skipped = 0;
90
91 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
92 goto presubmission_error;
93
94 fscache_count_read();
95 object = cachefiles_cres_object(cres);
96 file = cachefiles_cres_file(cres);
97
98 _enter("%pD,%llu,%llx,%zx/%llx",
99 file, file_inode(file)->i_ino, start_pos, len,
100 i_size_read(file_inode(file)));
101
102 /* If the caller asked us to seek for data before doing the read, then
103 * we should do that now. If we find a gap, we fill it with zeros.
104 */
105 if (read_hole != NETFS_READ_HOLE_IGNORE) {
106 loff_t off = start_pos, off2;
107
108 off2 = cachefiles_inject_read_error();
109 if (off2 == 0)
110 off2 = vfs_llseek(file, off, SEEK_DATA);
111 if (off2 < 0 && off2 >= (loff_t)-MAX_ERRNO && off2 != -ENXIO) {
112 skipped = 0;
113 ret = off2;
114 goto presubmission_error;
115 }
116
117 if (off2 == -ENXIO || off2 >= start_pos + len) {
118 /* The region is beyond the EOF or there's no more data
119 * in the region, so clear the rest of the buffer and
120 * return success.
121 */
122 ret = -ENODATA;
123 if (read_hole == NETFS_READ_HOLE_FAIL)
124 goto presubmission_error;
125
126 iov_iter_zero(len, iter);
127 skipped = len;
128 ret = 0;
129 goto presubmission_error;
130 }
131
132 skipped = off2 - off;
133 iov_iter_zero(skipped, iter);
134 }
135
136 ret = -ENOMEM;
137 ki = kzalloc_obj(struct cachefiles_kiocb);
138 if (!ki)
139 goto presubmission_error;
140
141 refcount_set(&ki->ki_refcnt, 2);
142 ki->iocb.ki_filp = file;
143 ki->iocb.ki_pos = start_pos + skipped;
144 ki->iocb.ki_flags = IOCB_DIRECT;
145 ki->iocb.ki_ioprio = get_current_ioprio();
146 ki->skipped = skipped;
147 ki->object = object;
148 ki->inval_counter = cres->inval_counter;
149 ki->term_func = term_func;
150 ki->term_func_priv = term_func_priv;
151 ki->was_async = true;
152
153 if (ki->term_func)
154 ki->iocb.ki_complete = cachefiles_read_complete;
155
156 get_file(ki->iocb.ki_filp);
157 cachefiles_grab_object(object, cachefiles_obj_get_ioreq);
158
159 trace_cachefiles_read(object, file_inode(file), ki->iocb.ki_pos, len - skipped);
160 old_nofs = memalloc_nofs_save();
161 ret = cachefiles_inject_read_error();
162 if (ret == 0)
163 ret = vfs_iocb_iter_read(file, &ki->iocb, iter);
164 memalloc_nofs_restore(old_nofs);
165 switch (ret) {
166 case -EIOCBQUEUED:
167 goto in_progress;
168
169 case -ERESTARTSYS:
170 case -ERESTARTNOINTR:
171 case -ERESTARTNOHAND:
172 case -ERESTART_RESTARTBLOCK:
173 /* There's no easy way to restart the syscall since other AIO's
174 * may be already running. Just fail this IO with EINTR.
175 */
176 ret = -EINTR;
177 fallthrough;
178 default:
179 ki->was_async = false;
180 cachefiles_read_complete(&ki->iocb, ret);
181 if (ret > 0)
182 ret = 0;
183 break;
184 }
185
186 in_progress:
187 cachefiles_put_kiocb(ki);
188 _leave(" = %zd", ret);
189 return ret;
190
191 presubmission_error:
192 if (term_func)
193 term_func(term_func_priv, ret < 0 ? ret : skipped);
194 return ret;
195 }
196
197 /*
198 * Query the occupancy of the cache in a region, returning the extent of the
199 * next two chunks of cached data and the next hole.
200 */
cachefiles_query_occupancy(struct netfs_cache_resources * cres,struct fscache_occupancy * occ)201 static int cachefiles_query_occupancy(struct netfs_cache_resources *cres,
202 struct fscache_occupancy *occ)
203 {
204 struct cachefiles_object *object;
205 struct inode *inode;
206 struct file *file;
207 uoff_t read_limit;
208 loff_t ret;
209 int i;
210
211 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_READ))
212 return -ENOBUFS;
213
214 object = cachefiles_cres_object(cres);
215 file = cachefiles_cres_file(cres);
216 inode = file_inode(file);
217 occ->granularity = object->volume->cache->bsize;
218 /* Read read_limit before content_info. */
219 read_limit = atomic64_read_acquire(&object->read_limit);
220
221 _enter("%pD,%llu,%llx-%llx/%llx",
222 file, inode->i_ino, occ->query_from, occ->query_to, read_limit);
223
224 if (read_limit == 0)
225 goto done;
226
227 switch (READ_ONCE(object->content_info)) {
228 case CACHEFILES_CONTENT_ALL:
229 case CACHEFILES_CONTENT_SINGLE:
230 if (read_limit > occ->query_from) {
231 occ->cached_from[0] = 0;
232 occ->cached_to[0] = read_limit;
233 occ->cached_type[0] = FSCACHE_EXTENT_DATA;
234 occ->query_from = ULLONG_MAX;
235 }
236 goto done;
237 default:
238 break;
239 }
240
241 for (i = 0; i < ARRAY_SIZE(occ->cached_from); i++) {
242 ret = cachefiles_inject_read_error();
243 if (ret == 0)
244 ret = vfs_llseek(file, occ->query_from, SEEK_DATA);
245 if (IS_ERR_VALUE_LL(ret)) {
246 if (ret != -ENXIO)
247 return ret;
248 occ->query_from = ULLONG_MAX;
249 goto done;
250 }
251 occ->cached_type[i] = FSCACHE_EXTENT_DATA;
252 occ->cached_from[i] = ret;
253 occ->query_from = ret;
254
255 ret = cachefiles_inject_read_error();
256 if (ret == 0)
257 ret = vfs_llseek(file, occ->query_from, SEEK_HOLE);
258 if (IS_ERR_VALUE_LL(ret)) {
259 if (ret != -ENXIO)
260 return ret;
261 occ->query_from = ULLONG_MAX;
262 goto done;
263 }
264 occ->cached_to[i] = ret;
265 occ->query_from = ret;
266 if (occ->query_from >= occ->query_to)
267 break;
268 }
269
270 done:
271 _debug("query[0] %llx-%llx", occ->cached_from[0], occ->cached_to[0]);
272 _debug("query[1] %llx-%llx", occ->cached_from[1], occ->cached_to[1]);
273 return 0;
274 }
275
276 /*
277 * Handle completion of a write to the cache.
278 */
cachefiles_write_complete(struct kiocb * iocb,long ret)279 static void cachefiles_write_complete(struct kiocb *iocb, long ret)
280 {
281 struct cachefiles_kiocb *ki = container_of(iocb, struct cachefiles_kiocb, iocb);
282 struct cachefiles_object *object = ki->object;
283 struct inode *inode = file_inode(ki->iocb.ki_filp);
284
285 _enter("%ld", ret);
286
287 if (ki->was_async)
288 kiocb_end_write(iocb);
289
290 if (ret < 0)
291 trace_cachefiles_io_error(object, inode, ret,
292 cachefiles_trace_write_error);
293
294 atomic_long_sub(ki->b_writing, &object->volume->cache->b_writing);
295 set_bit(FSCACHE_COOKIE_HAVE_DATA, &object->cookie->flags);
296 if (ki->term_func)
297 ki->term_func(ki->term_func_priv, ret);
298 cachefiles_put_kiocb(ki);
299 }
300
301 /*
302 * Initiate a write to the cache.
303 */
__cachefiles_write(struct cachefiles_object * object,struct file * file,uoff_t start_pos,struct iov_iter * iter,netfs_io_terminated_t term_func,void * term_func_priv)304 int __cachefiles_write(struct cachefiles_object *object,
305 struct file *file,
306 uoff_t start_pos,
307 struct iov_iter *iter,
308 netfs_io_terminated_t term_func,
309 void *term_func_priv)
310 {
311 struct cachefiles_cache *cache;
312 struct cachefiles_kiocb *ki;
313 unsigned int old_nofs;
314 ssize_t ret;
315 size_t len = iov_iter_count(iter);
316
317 fscache_count_write();
318 cache = object->volume->cache;
319
320 _enter("%pD,%llu,%llx,%zx/%llx",
321 file, file_inode(file)->i_ino, start_pos, len,
322 i_size_read(file_inode(file)));
323
324 ki = kzalloc_obj(struct cachefiles_kiocb);
325 if (!ki) {
326 if (term_func)
327 term_func(term_func_priv, -ENOMEM);
328 return -ENOMEM;
329 }
330
331 refcount_set(&ki->ki_refcnt, 2);
332 ki->iocb.ki_filp = file;
333 ki->iocb.ki_pos = start_pos;
334 ki->iocb.ki_flags = IOCB_DIRECT | IOCB_WRITE;
335 ki->iocb.ki_ioprio = get_current_ioprio();
336 ki->object = object;
337 ki->start = start_pos;
338 ki->len = len;
339 ki->term_func = term_func;
340 ki->term_func_priv = term_func_priv;
341 ki->was_async = true;
342 ki->b_writing = (len + (1 << cache->bshift) - 1) >> cache->bshift;
343
344 if (ki->term_func)
345 ki->iocb.ki_complete = cachefiles_write_complete;
346 atomic_long_add(ki->b_writing, &cache->b_writing);
347
348 get_file(ki->iocb.ki_filp);
349 cachefiles_grab_object(object, cachefiles_obj_get_ioreq);
350
351 trace_cachefiles_write(object, file_inode(file), ki->iocb.ki_pos, len);
352 old_nofs = memalloc_nofs_save();
353 ret = cachefiles_inject_write_error();
354 if (ret == 0)
355 ret = vfs_iocb_iter_write(file, &ki->iocb, iter);
356 memalloc_nofs_restore(old_nofs);
357 switch (ret) {
358 case -EIOCBQUEUED:
359 goto in_progress;
360
361 case -ERESTARTSYS:
362 case -ERESTARTNOINTR:
363 case -ERESTARTNOHAND:
364 case -ERESTART_RESTARTBLOCK:
365 /* There's no easy way to restart the syscall since other AIO's
366 * may be already running. Just fail this IO with EINTR.
367 */
368 ret = -EINTR;
369 fallthrough;
370 default:
371 ki->was_async = false;
372 cachefiles_write_complete(&ki->iocb, ret);
373 break;
374 }
375
376 in_progress:
377 cachefiles_put_kiocb(ki);
378 _leave(" = %zd", ret);
379 return ret;
380 }
381
cachefiles_write(struct netfs_cache_resources * cres,uoff_t start_pos,struct iov_iter * iter,netfs_io_terminated_t term_func,void * term_func_priv)382 static int cachefiles_write(struct netfs_cache_resources *cres,
383 uoff_t start_pos,
384 struct iov_iter *iter,
385 netfs_io_terminated_t term_func,
386 void *term_func_priv)
387 {
388 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE)) {
389 if (term_func)
390 term_func(term_func_priv, -ENOBUFS);
391 trace_netfs_sreq(term_func_priv, netfs_sreq_trace_cache_nowrite);
392 return -ENOBUFS;
393 }
394
395 return __cachefiles_write(cachefiles_cres_object(cres),
396 cachefiles_cres_file(cres),
397 start_pos, iter,
398 term_func, term_func_priv);
399 }
400
401 /*
402 * Prepare for a write to occur.
403 */
__cachefiles_prepare_write(struct cachefiles_object * object,struct file * file,uoff_t * _start,size_t * _len,size_t upper_len,bool no_space_allocated_yet)404 int __cachefiles_prepare_write(struct cachefiles_object *object,
405 struct file *file,
406 uoff_t *_start, size_t *_len, size_t upper_len,
407 bool no_space_allocated_yet)
408 {
409 struct cachefiles_cache *cache = object->volume->cache;
410 loff_t start = *_start, pos;
411 size_t len = *_len;
412 int ret;
413
414 /* Round to DIO size */
415 start = round_down(*_start, cache->bsize);
416 if (start != *_start || *_len > upper_len) {
417 /* Probably asked to cache a streaming write written into the
418 * pagecache when the cookie was temporarily out of service to
419 * culling.
420 */
421 fscache_count_dio_misfit();
422 return -ENOBUFS;
423 }
424
425 *_len = round_up(len, cache->bsize);
426
427 /* We need to work out whether there's sufficient disk space to perform
428 * the write - but we can skip that check if we have space already
429 * allocated.
430 */
431 if (no_space_allocated_yet)
432 goto check_space;
433
434 pos = cachefiles_inject_read_error();
435 if (pos == 0)
436 pos = vfs_llseek(file, start, SEEK_DATA);
437 if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
438 if (pos == -ENXIO)
439 goto check_space; /* Unallocated tail */
440 trace_cachefiles_io_error(object, file_inode(file), pos,
441 cachefiles_trace_seek_error);
442 return pos;
443 }
444 if ((u64)pos >= (u64)start + *_len)
445 goto check_space; /* Unallocated region */
446
447 /* We have a block that's at least partially filled - if we're low on
448 * space, we need to see if it's fully allocated. If it's not, we may
449 * want to cull it.
450 */
451 ret = cachefiles_has_space(cache, 0, *_len / cache->bsize,
452 cachefiles_has_space_check);
453 if (ret == 0)
454 return 0; /* Enough space to simply overwrite the whole block */
455
456 if (ret == -ENOBUFS)
457 trace_cachefiles_no_space(object, cachefiles_trace_write_nospace_2);
458
459 pos = cachefiles_inject_read_error();
460 if (pos == 0)
461 pos = vfs_llseek(file, start, SEEK_HOLE);
462 if (pos < 0 && pos >= (loff_t)-MAX_ERRNO) {
463 trace_cachefiles_io_error(object, file_inode(file), pos,
464 cachefiles_trace_seek_error);
465 return pos;
466 }
467 if ((u64)pos >= (u64)start + *_len)
468 return 0; /* Fully allocated */
469
470 /* Partially allocated, but insufficient space: cull. */
471 fscache_count_no_write_space();
472 ret = cachefiles_inject_remove_error();
473 if (ret == 0)
474 ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
475 start, *_len);
476 if (ret < 0) {
477 trace_cachefiles_io_error(object, file_inode(file), ret,
478 cachefiles_trace_fallocate_error);
479 cachefiles_io_error_obj(object,
480 "CacheFiles: fallocate failed (%d)\n", ret);
481 ret = -EIO;
482 }
483
484 return ret;
485
486 check_space:
487 ret = cachefiles_has_space(cache, 0, *_len / cache->bsize,
488 cachefiles_has_space_for_write);
489 if (ret == -ENOBUFS)
490 trace_cachefiles_no_space(object, cachefiles_trace_write_nospace);
491 return ret;
492 }
493
cachefiles_prepare_write(struct netfs_cache_resources * cres,uoff_t * _start,size_t * _len,size_t upper_len,uoff_t i_size,bool no_space_allocated_yet)494 static int cachefiles_prepare_write(struct netfs_cache_resources *cres,
495 uoff_t *_start, size_t *_len, size_t upper_len,
496 uoff_t i_size, bool no_space_allocated_yet)
497 {
498 struct cachefiles_object *object = cachefiles_cres_object(cres);
499 struct cachefiles_cache *cache = object->volume->cache;
500 const struct cred *saved_cred;
501 int ret;
502
503 if (!cachefiles_cres_file(cres)) {
504 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE))
505 return -ENOBUFS;
506 if (!cachefiles_cres_file(cres))
507 return -ENOBUFS;
508 }
509
510 cachefiles_begin_secure(cache, &saved_cred);
511 ret = __cachefiles_prepare_write(object, cachefiles_cres_file(cres),
512 _start, _len, upper_len,
513 no_space_allocated_yet);
514 cachefiles_end_secure(cache, saved_cred);
515 return ret;
516 }
517
cachefiles_prepare_write_subreq(struct netfs_io_subrequest * subreq)518 static void cachefiles_prepare_write_subreq(struct netfs_io_subrequest *subreq)
519 {
520 struct netfs_io_request *wreq = subreq->rreq;
521 struct netfs_cache_resources *cres = &wreq->cache_resources;
522 struct netfs_io_stream *stream = &wreq->io_streams[subreq->stream_nr];
523
524 _enter("W=%x[%x] %llx", wreq->debug_id, subreq->debug_index, subreq->start);
525
526 stream->sreq_max_len = MAX_RW_COUNT;
527 stream->sreq_max_segs = BIO_MAX_VECS;
528
529 if (!cachefiles_cres_file(cres)) {
530 if (!fscache_wait_for_operation(cres, FSCACHE_WANT_WRITE)) {
531 subreq->error = -ENOBUFS;
532 trace_netfs_sreq(subreq, netfs_sreq_trace_cache_waitfail);
533 return netfs_prepare_write_failed(subreq);
534 }
535 if (!cachefiles_cres_file(cres)) {
536 subreq->error = -ENOBUFS;
537 trace_netfs_sreq(subreq, netfs_sreq_trace_cache_nofile);
538 return netfs_prepare_write_failed(subreq);
539 }
540 }
541 }
542
cachefiles_issue_write(struct netfs_io_subrequest * subreq)543 static void cachefiles_issue_write(struct netfs_io_subrequest *subreq)
544 {
545 struct netfs_io_request *wreq = subreq->rreq;
546 struct netfs_cache_resources *cres = &wreq->cache_resources;
547 struct cachefiles_object *object = cachefiles_cres_object(cres);
548 struct cachefiles_cache *cache = object->volume->cache;
549 struct netfs_io_stream *stream = &wreq->io_streams[subreq->stream_nr];
550 const struct cred *saved_cred;
551 size_t off, pre, post, len = subreq->len;
552 uoff_t start = subreq->start;
553 int ret;
554
555 _enter("W=%x[%x] %llx-%llx",
556 wreq->debug_id, subreq->debug_index, start, start + len - 1);
557
558 /* We need to start on the cache granularity boundary */
559 off = start & (cache->bsize - 1);
560 if (off) {
561 pre = cache->bsize - off;
562 if (pre >= len) {
563 fscache_count_dio_misfit();
564 netfs_write_subrequest_terminated(subreq, len);
565 return;
566 }
567 subreq->transferred += pre;
568 start += pre;
569 len -= pre;
570 iov_iter_advance(&subreq->io_iter, pre);
571 }
572
573 /* We also need to end on the cache granularity boundary */
574 if (start + len == wreq->i_size) {
575 size_t part = len & (cache->bsize - 1);
576 size_t need = cache->bsize - part;
577
578 if (part && stream->submit_extendable_to >= need) {
579 len += need;
580 subreq->len += need;
581 subreq->io_iter.count += need;
582 }
583 }
584
585 post = len & (cache->bsize - 1);
586 if (post) {
587 len -= post;
588 if (len == 0) {
589 fscache_count_dio_misfit();
590 netfs_write_subrequest_terminated(subreq, post);
591 return;
592 }
593 iov_iter_truncate(&subreq->io_iter, len);
594 }
595
596 trace_netfs_sreq(subreq, netfs_sreq_trace_cache_prepare);
597 cachefiles_begin_secure(cache, &saved_cred);
598 ret = __cachefiles_prepare_write(object, cachefiles_cres_file(cres),
599 &start, &len, len, true);
600 cachefiles_end_secure(cache, saved_cred);
601 if (ret < 0) {
602 netfs_write_subrequest_terminated(subreq, ret);
603 return;
604 }
605
606 trace_netfs_sreq(subreq, netfs_sreq_trace_cache_write);
607 cachefiles_write(&subreq->rreq->cache_resources,
608 subreq->start, &subreq->io_iter,
609 netfs_write_subrequest_terminated, subreq);
610 }
611
612 /*
613 * Collect the result of buffered writeback to the cache. This includes
614 * copying a read to the cache. Netfslib collates the results, which might
615 * occur out of order, and delivers them to the cache so that it can update its
616 * content record.
617 *
618 * block_type is one of:
619 * - NETFS_CACHE_COLLECT_WRITE_DATA for a contiguous block of data
620 * - NETFS_CACHE_COLLECT_WRITE_GAP if a discontiguity was skipped
621 * - NETFS_CACHE_COLLECT_WRITE_CANCEL for a hole due to a failed/cancelled write
622 *
623 * The writes we made are all rounded out at both sides to the nearest DIO
624 * block boundary, so if the final block contains the EOF in the middle of it
625 * (rather than at the end), padding will have been written to the file. The
626 * backing file's filesize will have been updated if the write extended the
627 * file; the filesize may still change due to outstanding subreqs.
628 *
629 * The metadata in the cache file xattr records the size of the object we have
630 * stored, but the cache file EOF only goes up to where we've cached data to
631 * and, furthermore, is rounded up to the nearest DIO block boundary.
632 *
633 * Concurrent updates should be protected against by the caller. Netfslib
634 * holds NETFS_ICTX_WB_LOCK as a lock on writeback requests. DIO writes
635 * invalidate the cookie and caching is kept disabled until all users have
636 * unused the cookie.
637 */
cachefiles_collect_write(struct netfs_io_request * wreq,uoff_t start,size_t len,enum netfs_cache_collect block_type)638 static void cachefiles_collect_write(struct netfs_io_request *wreq,
639 uoff_t start, size_t len,
640 enum netfs_cache_collect block_type)
641 {
642 struct netfs_cache_resources *cres = &wreq->cache_resources;
643 struct cachefiles_object *object = cachefiles_cres_object(cres);
644 struct cachefiles_cache *cache = object->volume->cache;
645 struct inode *inode;
646 struct file *file = cachefiles_cres_file(cres);
647 uoff_t read_limit;
648 uoff_t old_size = cres->cache_i_size;
649 uoff_t new_size;
650 uoff_t data_to = object->object_size;
651 uoff_t end = start + len;
652 int ret;
653
654 if (!file)
655 return;
656
657 inode = file_inode(file);
658 new_size = i_size_read(inode);
659
660 _enter("%llx,%zx,%x", start, len, cache->bsize);
661
662 if (WARN_ON(old_size & (cache->bsize - 1)) ||
663 WARN_ON(new_size & (cache->bsize - 1)) ||
664 WARN_ON(start & (cache->bsize - 1)) ||
665 WARN_ON(len & (cache->bsize - 1))) {
666 trace_cachefiles_io_error(object, inode, -EIO,
667 cachefiles_trace_alignment_error);
668 cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
669 return;
670 }
671
672 /* If this is recording a gap, due to discontiguous writes or lack of
673 * cache space, then a hole may have been introduced into the backing
674 * file. Treat it as a zero-length data block.
675 */
676 if (block_type == NETFS_CACHE_COLLECT_WRITE_GAP ||
677 block_type == NETFS_CACHE_COLLECT_WRITE_CANCEL) {
678 start = end;
679 len = 0;
680 }
681
682 /* Zeroth case: Single monolithic files are handled specially.
683 */
684 if (wreq->origin == NETFS_WRITEBACK_SINGLE) {
685 if (block_type == NETFS_CACHE_COLLECT_WRITE_GAP ||
686 block_type == NETFS_CACHE_COLLECT_WRITE_CANCEL) {
687 trace_cachefiles_trunc(object, inode, data_to, 0,
688 cachefiles_trunc_zap);
689 ret = cachefiles_inject_remove_error();
690 if (ret == 0)
691 ret = vfs_truncate(&file->f_path, 0);
692 if (ret < 0) {
693 trace_cachefiles_io_error(object, inode, ret,
694 cachefiles_trace_trunc_error);
695 cachefiles_io_error_obj(object, "truncate failed %d", ret);
696 cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
697 return;
698 }
699
700 object->content_info = CACHEFILES_CONTENT_NO_DATA;
701 read_limit = 0;
702 } else {
703 object->content_info = CACHEFILES_CONTENT_SINGLE;
704 read_limit = len;
705 }
706 goto update_sizes_2;
707 }
708
709 /* First case: The backing file was empty. */
710 if (old_size == 0) {
711 if (start == 0)
712 object->content_info = CACHEFILES_CONTENT_ALL;
713 else
714 object->content_info = CACHEFILES_CONTENT_BACKFS_MAP;
715 goto update_sizes;
716 }
717
718 /* Second case: The backing file is entirely within the old object size
719 * and thus there can be no partial tail block to deal with in the
720 * cache file.
721 */
722 if (old_size <= data_to) {
723 if (start > old_size)
724 goto discontiguous;
725 goto update_sizes;
726 }
727
728 /* Third case: The write happened entirely within the bounds of the
729 * current cache file's size.
730 */
731 if (end <= old_size)
732 goto update_sizes;
733
734 /* Fourth case: The write overwrote the partial tail block and extended
735 * the file. We only need to update the object size because netfslib
736 * rounds out/pads cache writes to whole disk blocks.
737 */
738 if (start < old_size)
739 goto update_sizes;
740
741 /* Fifth case: The write started from the end of the whole tail block
742 * and extended the file. Just extend our notion of the filesize.
743 */
744 if (start == old_size && old_size == data_to)
745 goto update_sizes;
746
747 /* Sixth case: The write continued on from the partial tail block and
748 * extended the file. Need to clear the gap.
749 */
750 if (start == old_size && old_size > data_to)
751 goto clear_gap;
752
753 discontiguous:
754 /* Seventh case: The write was beyond the EOF on the cache file, so now
755 * there's a hole in the file and we can no longer say in the metadata
756 * that we can assume we have it all. We may also need to clear the
757 * end of the partial tail block.
758 */
759 /* TODO: For the moment, we will have to use SEEK_HOLE/SEEK_DATA. */
760 if (object->content_info != CACHEFILES_CONTENT_BACKFS_MAP) {
761 object->content_info = CACHEFILES_CONTENT_BACKFS_MAP;
762 trace_cachefiles_coherency(object, inode->i_ino, data_to, NULL,
763 CACHEFILES_CONTENT_BACKFS_MAP,
764 cachefiles_coherency_discontiguous);
765 }
766
767 clear_gap:
768 /* We need to clear any partial padding that got jumped over. It
769 * *should* be all zeros, but shared-writable mmap exists...
770 */
771 if (old_size > data_to) {
772 trace_cachefiles_trunc(object, inode, data_to, old_size,
773 cachefiles_trunc_clear_padding);
774 ret = cachefiles_inject_write_error();
775 if (ret == 0)
776 ret = vfs_fallocate(file, FALLOC_FL_ZERO_RANGE,
777 data_to, old_size - data_to);
778 if (ret < 0) {
779 trace_cachefiles_io_error(object, inode, ret,
780 cachefiles_trace_fallocate_error);
781 cachefiles_io_error_obj(object, "fallocate zero pad failed %d", ret);
782 cachefiles_remove_object_xattr(cache, object, file->f_path.dentry);
783 return;
784 }
785 }
786
787 update_sizes:
788 read_limit = umax(old_size, end);
789 update_sizes_2:
790 cres->cache_i_size = read_limit;
791
792 /* We need to be careful setting the object_size: we may have written
793 * more to the cache than to the server (due to cache DIO rounding) and
794 * the i_size set on the netfs inode may include unwritten data that
795 * the server doesn't know about yet.
796 */
797 object->object_size = umin(read_limit, wreq->i_size);
798
799 /* Raise the limit at which reads can access the file. */
800 /* Update read_limit after content_info */
801 atomic64_set_release(&object->read_limit, read_limit);
802 }
803
804 /*
805 * Clean up an operation.
806 */
cachefiles_end_operation(struct netfs_cache_resources * cres)807 static void cachefiles_end_operation(struct netfs_cache_resources *cres)
808 {
809 struct file *file = cachefiles_cres_file(cres);
810
811 if (file)
812 fput(file);
813 fscache_end_cookie_access(fscache_cres_cookie(cres), fscache_access_io_end);
814 }
815
816 static const struct netfs_cache_ops cachefiles_netfs_cache_ops = {
817 .end_operation = cachefiles_end_operation,
818 .read = cachefiles_read,
819 .write = cachefiles_write,
820 .issue_write = cachefiles_issue_write,
821 .prepare_write = cachefiles_prepare_write,
822 .prepare_write_subreq = cachefiles_prepare_write_subreq,
823 .query_occupancy = cachefiles_query_occupancy,
824 .collect_write = cachefiles_collect_write,
825 };
826
827 /*
828 * Open the cache file when beginning a cache operation.
829 */
cachefiles_begin_operation(struct netfs_cache_resources * cres,enum fscache_want_state want_state)830 bool cachefiles_begin_operation(struct netfs_cache_resources *cres,
831 enum fscache_want_state want_state)
832 {
833 struct cachefiles_object *object = cachefiles_cres_object(cres);
834 struct file *file;
835
836 cres->dio_size = object->volume->cache->bsize;
837
838 if (!cachefiles_cres_file(cres)) {
839 cres->ops = &cachefiles_netfs_cache_ops;
840 cres->object_id = object->debug_id;
841 if (object->file) {
842 spin_lock(&object->lock);
843 file = object->file;
844 if (!cres->cache_priv2 && file) {
845 cres->cache_priv2 = get_file(file);
846 cres->cache_i_size = i_size_read(file_inode(file));
847 }
848 spin_unlock(&object->lock);
849 }
850 }
851
852 if (!cachefiles_cres_file(cres) && want_state != FSCACHE_WANT_PARAMS) {
853 pr_err("failed to get cres->file\n");
854 return false;
855 }
856
857 return true;
858 }
859