1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Storage object read/write
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/file.h>
11 #include <linux/swap.h>
15 * detect wake up events generated by the unlocking of pages in which we're
17 * - we use this to detect read completion of backing pages
18 * - the caller holds the waitqueue lock
20 static int cachefiles_read_waiter(wait_queue_entry_t *wait, unsigned mode,
23 struct cachefiles_one_read *monitor =
24 container_of(wait, struct cachefiles_one_read, monitor);
25 struct cachefiles_object *object;
26 struct fscache_retrieval *op = monitor->op;
27 struct wait_page_key *key = _key;
28 struct folio *folio = wait->private;
32 _enter("{%lu},%u,%d,{%p,%u}",
33 monitor->netfs_page->index, mode, sync,
34 key->folio, key->bit_nr);
36 if (key->folio != folio || key->bit_nr != PG_locked)
39 _debug("--- monitor %p %lx ---", folio, folio->flags);
41 if (!folio_test_uptodate(folio) && !folio_test_error(folio)) {
42 /* unlocked, not uptodate and not erronous? */
43 _debug("page probably truncated");
46 /* remove from the waitqueue */
47 list_del(&wait->entry);
49 /* move onto the action list and queue for FS-Cache thread pool */
52 /* We need to temporarily bump the usage count as we don't own a ref
53 * here otherwise cachefiles_read_copier() may free the op between the
54 * monitor being enqueued on the op->to_do list and the op getting
55 * enqueued on the work queue.
57 fscache_get_retrieval(op);
59 object = container_of(op->op.object, struct cachefiles_object, fscache);
60 spin_lock(&object->work_lock);
61 list_add_tail(&monitor->op_link, &op->to_do);
62 fscache_enqueue_retrieval(op);
63 spin_unlock(&object->work_lock);
65 fscache_put_retrieval(op);
70 * handle a probably truncated page
71 * - check to see if the page is still relevant and reissue the read if
73 * - return -EIO on error, -ENODATA if the page is gone, -EINPROGRESS if we
74 * must wait again and 0 if successful
76 static int cachefiles_read_reissue(struct cachefiles_object *object,
77 struct cachefiles_one_read *monitor)
79 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
80 struct page *backpage = monitor->back_page, *backpage2;
83 _enter("{ino=%lx},{%lx,%lx}",
84 d_backing_inode(object->backer)->i_ino,
85 backpage->index, backpage->flags);
87 /* skip if the page was truncated away completely */
88 if (backpage->mapping != bmapping) {
89 _leave(" = -ENODATA [mapping]");
93 backpage2 = find_get_page(bmapping, backpage->index);
95 _leave(" = -ENODATA [gone]");
99 if (backpage != backpage2) {
101 _leave(" = -ENODATA [different]");
105 /* the page is still there and we already have a ref on it, so we don't
109 INIT_LIST_HEAD(&monitor->op_link);
110 folio_add_wait_queue(page_folio(backpage), &monitor->monitor);
112 if (trylock_page(backpage)) {
114 if (PageError(backpage))
117 if (PageUptodate(backpage))
120 _debug("reissue read");
121 ret = bmapping->a_ops->readpage(NULL, backpage);
126 /* but the page may have been read before the monitor was installed, so
127 * the monitor may miss the event - so we have to ensure that we do get
128 * one in such a case */
129 if (trylock_page(backpage)) {
130 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
131 unlock_page(backpage);
134 /* it'll reappear on the todo list */
135 _leave(" = -EINPROGRESS");
139 unlock_page(backpage);
141 spin_lock_irq(&object->work_lock);
142 list_del(&monitor->op_link);
143 spin_unlock_irq(&object->work_lock);
144 _leave(" = %d", ret);
149 * copy data from backing pages to netfs pages to complete a read operation
150 * - driven by FS-Cache's thread pool
152 static void cachefiles_read_copier(struct fscache_operation *_op)
154 struct cachefiles_one_read *monitor;
155 struct cachefiles_object *object;
156 struct fscache_retrieval *op;
159 op = container_of(_op, struct fscache_retrieval, op);
160 object = container_of(op->op.object,
161 struct cachefiles_object, fscache);
163 _enter("{ino=%lu}", d_backing_inode(object->backer)->i_ino);
166 spin_lock_irq(&object->work_lock);
168 while (!list_empty(&op->to_do)) {
169 monitor = list_entry(op->to_do.next,
170 struct cachefiles_one_read, op_link);
171 list_del(&monitor->op_link);
173 spin_unlock_irq(&object->work_lock);
175 _debug("- copy {%lu}", monitor->back_page->index);
178 if (test_bit(FSCACHE_COOKIE_INVALIDATING,
179 &object->fscache.cookie->flags)) {
181 } else if (PageUptodate(monitor->back_page)) {
182 copy_highpage(monitor->netfs_page, monitor->back_page);
183 fscache_mark_page_cached(monitor->op,
184 monitor->netfs_page);
186 } else if (!PageError(monitor->back_page)) {
187 /* the page has probably been truncated */
188 error = cachefiles_read_reissue(object, monitor);
189 if (error == -EINPROGRESS)
193 cachefiles_io_error_obj(
195 "Readpage failed on backing file %lx",
196 (unsigned long) monitor->back_page->flags);
200 put_page(monitor->back_page);
202 fscache_end_io(op, monitor->netfs_page, error);
203 put_page(monitor->netfs_page);
204 fscache_retrieval_complete(op, 1);
205 fscache_put_retrieval(op);
209 /* let the thread pool have some air occasionally */
211 if (max < 0 || need_resched()) {
212 if (!list_empty(&op->to_do))
213 fscache_enqueue_retrieval(op);
214 _leave(" [maxed out]");
218 spin_lock_irq(&object->work_lock);
221 spin_unlock_irq(&object->work_lock);
226 * read the corresponding page to the given set from the backing file
227 * - an uncertain page is simply discarded, to be tried again another time
229 static int cachefiles_read_backing_file_one(struct cachefiles_object *object,
230 struct fscache_retrieval *op,
231 struct page *netpage)
233 struct cachefiles_one_read *monitor;
234 struct address_space *bmapping;
235 struct page *newpage, *backpage;
240 _debug("read back %p{%lu,%d}",
241 netpage, netpage->index, page_count(netpage));
243 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
247 monitor->netfs_page = netpage;
248 monitor->op = fscache_get_retrieval(op);
250 init_waitqueue_func_entry(&monitor->monitor, cachefiles_read_waiter);
252 /* attempt to get hold of the backing page */
253 bmapping = d_backing_inode(object->backer)->i_mapping;
257 backpage = find_get_page(bmapping, netpage->index);
259 goto backing_page_already_present;
262 newpage = __page_cache_alloc(cachefiles_gfp);
267 ret = add_to_page_cache_lru(newpage, bmapping,
268 netpage->index, cachefiles_gfp);
270 goto installed_new_backing_page;
275 /* we've installed a new backing page, so now we need to start
277 installed_new_backing_page:
278 _debug("- new %p", newpage);
284 ret = bmapping->a_ops->readpage(NULL, backpage);
288 /* set the monitor to transfer the data across */
289 monitor_backing_page:
290 _debug("- monitor add");
292 /* install the monitor */
293 get_page(monitor->netfs_page);
295 monitor->back_page = backpage;
296 monitor->monitor.private = backpage;
297 folio_add_wait_queue(page_folio(backpage), &monitor->monitor);
300 /* but the page may have been read before the monitor was installed, so
301 * the monitor may miss the event - so we have to ensure that we do get
302 * one in such a case */
303 if (trylock_page(backpage)) {
304 _debug("jumpstart %p {%lx}", backpage, backpage->flags);
305 unlock_page(backpage);
309 /* if the backing page is already present, it can be in one of
310 * three states: read in progress, read failed or read okay */
311 backing_page_already_present:
319 if (PageError(backpage))
322 if (PageUptodate(backpage))
323 goto backing_page_already_uptodate;
325 if (!trylock_page(backpage))
326 goto monitor_backing_page;
327 _debug("read %p {%lx}", backpage, backpage->flags);
328 goto read_backing_page;
330 /* the backing page is already up to date, attach the netfs
331 * page to the pagecache and LRU and copy the data across */
332 backing_page_already_uptodate:
333 _debug("- uptodate");
335 fscache_mark_page_cached(op, netpage);
337 copy_highpage(netpage, backpage);
338 fscache_end_io(op, netpage, 0);
339 fscache_retrieval_complete(op, 1);
349 fscache_put_retrieval(monitor->op);
352 _leave(" = %d", ret);
356 _debug("read error %d", ret);
357 if (ret == -ENOMEM) {
358 fscache_retrieval_complete(op, 1);
362 cachefiles_io_error_obj(object, "Page read error on backing file");
363 fscache_retrieval_complete(op, 1);
370 fscache_put_retrieval(monitor->op);
373 fscache_retrieval_complete(op, 1);
374 _leave(" = -ENOMEM");
379 * read a page from the cache or allocate a block in which to store it
380 * - cache withdrawal is prevented by the caller
381 * - returns -EINTR if interrupted
382 * - returns -ENOMEM if ran out of memory
383 * - returns -ENOBUFS if no buffers can be made available
384 * - returns -ENOBUFS if page is beyond EOF
385 * - if the page is backed by a block in the cache:
386 * - a read will be started which will call the callback on completion
387 * - 0 will be returned
388 * - else if the page is unbacked:
389 * - the metadata will be retained
390 * - -ENODATA will be returned
392 int cachefiles_read_or_alloc_page(struct fscache_retrieval *op,
396 struct cachefiles_object *object;
397 struct cachefiles_cache *cache;
403 object = container_of(op->op.object,
404 struct cachefiles_object, fscache);
405 cache = container_of(object->fscache.cache,
406 struct cachefiles_cache, cache);
408 _enter("{%p},{%lx},,,", object, page->index);
413 inode = d_backing_inode(object->backer);
414 ASSERT(S_ISREG(inode->i_mode));
416 /* calculate the shift required to use bmap */
417 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
419 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
420 op->op.flags |= FSCACHE_OP_ASYNC;
421 op->op.processor = cachefiles_read_copier;
423 /* we assume the absence or presence of the first block is a good
424 * enough indication for the page as a whole
425 * - TODO: don't use bmap() for this as it is _not_ actually good
426 * enough for this as it doesn't indicate errors, but it's all we've
432 ret2 = bmap(inode, &block);
435 _debug("%llx -> %llx",
436 (unsigned long long) (page->index << shift),
437 (unsigned long long) block);
440 /* submit the apparently valid page to the backing fs to be
442 ret = cachefiles_read_backing_file_one(object, op, page);
443 } else if (cachefiles_has_space(cache, 0, 1) == 0) {
444 /* there's space in the cache we can use */
445 fscache_mark_page_cached(op, page);
446 fscache_retrieval_complete(op, 1);
452 _leave(" = %d", ret);
456 fscache_retrieval_complete(op, 1);
457 _leave(" = -ENOBUFS");
462 * read the corresponding pages to the given set from the backing file
463 * - any uncertain pages are simply discarded, to be tried again another time
465 static int cachefiles_read_backing_file(struct cachefiles_object *object,
466 struct fscache_retrieval *op,
467 struct list_head *list)
469 struct cachefiles_one_read *monitor = NULL;
470 struct address_space *bmapping = d_backing_inode(object->backer)->i_mapping;
471 struct page *newpage = NULL, *netpage, *_n, *backpage = NULL;
476 list_for_each_entry_safe(netpage, _n, list, lru) {
477 list_del(&netpage->lru);
479 _debug("read back %p{%lu,%d}",
480 netpage, netpage->index, page_count(netpage));
483 monitor = kzalloc(sizeof(*monitor), cachefiles_gfp);
487 monitor->op = fscache_get_retrieval(op);
488 init_waitqueue_func_entry(&monitor->monitor,
489 cachefiles_read_waiter);
493 backpage = find_get_page(bmapping, netpage->index);
495 goto backing_page_already_present;
498 newpage = __page_cache_alloc(cachefiles_gfp);
503 ret = add_to_page_cache_lru(newpage, bmapping,
507 goto installed_new_backing_page;
512 /* we've installed a new backing page, so now we need
513 * to start it reading */
514 installed_new_backing_page:
515 _debug("- new %p", newpage);
521 ret = bmapping->a_ops->readpage(NULL, backpage);
525 /* add the netfs page to the pagecache and LRU, and set the
526 * monitor to transfer the data across */
527 monitor_backing_page:
528 _debug("- monitor add");
530 ret = add_to_page_cache_lru(netpage, op->mapping,
531 netpage->index, cachefiles_gfp);
533 if (ret == -EEXIST) {
538 fscache_retrieval_complete(op, 1);
544 /* install a monitor */
546 monitor->netfs_page = netpage;
549 monitor->back_page = backpage;
550 monitor->monitor.private = backpage;
551 folio_add_wait_queue(page_folio(backpage), &monitor->monitor);
554 /* but the page may have been read before the monitor was
555 * installed, so the monitor may miss the event - so we have to
556 * ensure that we do get one in such a case */
557 if (trylock_page(backpage)) {
558 _debug("2unlock %p {%lx}", backpage, backpage->flags);
559 unlock_page(backpage);
569 /* if the backing page is already present, it can be in one of
570 * three states: read in progress, read failed or read okay */
571 backing_page_already_present:
572 _debug("- present %p", backpage);
574 if (PageError(backpage))
577 if (PageUptodate(backpage))
578 goto backing_page_already_uptodate;
580 _debug("- not ready %p{%lx}", backpage, backpage->flags);
582 if (!trylock_page(backpage))
583 goto monitor_backing_page;
585 if (PageError(backpage)) {
586 _debug("error %lx", backpage->flags);
587 unlock_page(backpage);
591 if (PageUptodate(backpage))
592 goto backing_page_already_uptodate_unlock;
594 /* we've locked a page that's neither up to date nor erroneous,
595 * so we need to attempt to read it again */
596 goto reread_backing_page;
598 /* the backing page is already up to date, attach the netfs
599 * page to the pagecache and LRU and copy the data across */
600 backing_page_already_uptodate_unlock:
601 _debug("uptodate %lx", backpage->flags);
602 unlock_page(backpage);
603 backing_page_already_uptodate:
604 _debug("- uptodate");
606 ret = add_to_page_cache_lru(netpage, op->mapping,
607 netpage->index, cachefiles_gfp);
609 if (ret == -EEXIST) {
614 fscache_retrieval_complete(op, 1);
620 copy_highpage(netpage, backpage);
625 fscache_mark_page_cached(op, netpage);
627 /* the netpage is unlocked and marked up to date here */
628 fscache_end_io(op, netpage, 0);
631 fscache_retrieval_complete(op, 1);
648 fscache_put_retrieval(op);
652 list_for_each_entry_safe(netpage, _n, list, lru) {
653 list_del(&netpage->lru);
655 fscache_retrieval_complete(op, 1);
658 _leave(" = %d", ret);
664 goto record_page_complete;
667 _debug("read error %d", ret);
669 goto record_page_complete;
671 cachefiles_io_error_obj(object, "Page read error on backing file");
673 record_page_complete:
674 fscache_retrieval_complete(op, 1);
679 * read a list of pages from the cache or allocate blocks in which to store
682 int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op,
683 struct list_head *pages,
687 struct cachefiles_object *object;
688 struct cachefiles_cache *cache;
689 struct list_head backpages;
690 struct pagevec pagevec;
692 struct page *page, *_n;
693 unsigned shift, nrbackpages;
694 int ret, ret2, space;
696 object = container_of(op->op.object,
697 struct cachefiles_object, fscache);
698 cache = container_of(object->fscache.cache,
699 struct cachefiles_cache, cache);
701 _enter("{OBJ%x,%d},,%d,,",
702 object->fscache.debug_id, atomic_read(&op->op.usage),
709 if (cachefiles_has_space(cache, 0, *nr_pages) < 0)
712 inode = d_backing_inode(object->backer);
713 ASSERT(S_ISREG(inode->i_mode));
715 /* calculate the shift required to use bmap */
716 shift = PAGE_SHIFT - inode->i_sb->s_blocksize_bits;
718 pagevec_init(&pagevec);
720 op->op.flags &= FSCACHE_OP_KEEP_FLAGS;
721 op->op.flags |= FSCACHE_OP_ASYNC;
722 op->op.processor = cachefiles_read_copier;
724 INIT_LIST_HEAD(&backpages);
727 ret = space ? -ENODATA : -ENOBUFS;
728 list_for_each_entry_safe(page, _n, pages, lru) {
731 /* we assume the absence or presence of the first block is a
732 * good enough indication for the page as a whole
733 * - TODO: don't use bmap() for this as it is _not_ actually
734 * good enough for this as it doesn't indicate errors, but
735 * it's all we've got for the moment
740 ret2 = bmap(inode, &block);
743 _debug("%llx -> %llx",
744 (unsigned long long) (page->index << shift),
745 (unsigned long long) block);
748 /* we have data - add it to the list to give to the
750 list_move(&page->lru, &backpages);
753 } else if (space && pagevec_add(&pagevec, page) == 0) {
754 fscache_mark_pages_cached(op, &pagevec);
755 fscache_retrieval_complete(op, 1);
758 fscache_retrieval_complete(op, 1);
762 if (pagevec_count(&pagevec) > 0)
763 fscache_mark_pages_cached(op, &pagevec);
765 if (list_empty(pages))
768 /* submit the apparently valid pages to the backing fs to be read from
770 if (nrbackpages > 0) {
771 ret2 = cachefiles_read_backing_file(object, op, &backpages);
772 if (ret2 == -ENOMEM || ret2 == -EINTR)
776 _leave(" = %d [nr=%u%s]",
777 ret, *nr_pages, list_empty(pages) ? " empty" : "");
781 fscache_retrieval_complete(op, *nr_pages);
786 * allocate a block in the cache in which to store a page
787 * - cache withdrawal is prevented by the caller
788 * - returns -EINTR if interrupted
789 * - returns -ENOMEM if ran out of memory
790 * - returns -ENOBUFS if no buffers can be made available
791 * - returns -ENOBUFS if page is beyond EOF
793 * - the metadata will be retained
794 * - 0 will be returned
796 int cachefiles_allocate_page(struct fscache_retrieval *op,
800 struct cachefiles_object *object;
801 struct cachefiles_cache *cache;
804 object = container_of(op->op.object,
805 struct cachefiles_object, fscache);
806 cache = container_of(object->fscache.cache,
807 struct cachefiles_cache, cache);
809 _enter("%p,{%lx},", object, page->index);
811 ret = cachefiles_has_space(cache, 0, 1);
813 fscache_mark_page_cached(op, page);
817 fscache_retrieval_complete(op, 1);
818 _leave(" = %d", ret);
823 * allocate blocks in the cache in which to store a set of pages
824 * - cache withdrawal is prevented by the caller
825 * - returns -EINTR if interrupted
826 * - returns -ENOMEM if ran out of memory
827 * - returns -ENOBUFS if some buffers couldn't be made available
828 * - returns -ENOBUFS if some pages are beyond EOF
830 * - -ENODATA will be returned
831 * - metadata will be retained for any page marked
833 int cachefiles_allocate_pages(struct fscache_retrieval *op,
834 struct list_head *pages,
838 struct cachefiles_object *object;
839 struct cachefiles_cache *cache;
840 struct pagevec pagevec;
844 object = container_of(op->op.object,
845 struct cachefiles_object, fscache);
846 cache = container_of(object->fscache.cache,
847 struct cachefiles_cache, cache);
849 _enter("%p,,,%d,", object, *nr_pages);
851 ret = cachefiles_has_space(cache, 0, *nr_pages);
853 pagevec_init(&pagevec);
855 list_for_each_entry(page, pages, lru) {
856 if (pagevec_add(&pagevec, page) == 0)
857 fscache_mark_pages_cached(op, &pagevec);
860 if (pagevec_count(&pagevec) > 0)
861 fscache_mark_pages_cached(op, &pagevec);
867 fscache_retrieval_complete(op, *nr_pages);
868 _leave(" = %d", ret);
873 * request a page be stored in the cache
874 * - cache withdrawal is prevented by the caller
875 * - this request may be ignored if there's no cache block available, in which
876 * case -ENOBUFS will be returned
877 * - if the op is in progress, 0 will be returned
879 int cachefiles_write_page(struct fscache_storage *op, struct page *page)
881 struct cachefiles_object *object;
882 struct cachefiles_cache *cache;
891 ASSERT(page != NULL);
893 object = container_of(op->op.object,
894 struct cachefiles_object, fscache);
896 _enter("%p,%p{%lx},,,", object, page, page->index);
898 if (!object->backer) {
899 _leave(" = -ENOBUFS");
903 ASSERT(d_is_reg(object->backer));
905 cache = container_of(object->fscache.cache,
906 struct cachefiles_cache, cache);
908 pos = (loff_t)page->index << PAGE_SHIFT;
910 /* We mustn't write more data than we have, so we have to beware of a
911 * partial page at EOF.
913 eof = object->fscache.store_limit_l;
917 /* write the page to the backing filesystem and let it store it in its
919 path.mnt = cache->mnt;
920 path.dentry = object->backer;
921 file = dentry_open(&path, O_RDWR | O_LARGEFILE, cache->cache_cred);
928 if (eof & ~PAGE_MASK) {
929 if (eof - pos < PAGE_SIZE) {
930 _debug("cut short %llx to %llx",
933 ASSERTCMP(pos + len, ==, eof);
938 ret = kernel_write(file, data, len, &pos);
951 cachefiles_io_error_obj(object,
952 "Write page to backing file failed");
954 _leave(" = -ENOBUFS [%d]", ret);
959 * detach a backing block from a page
960 * - cache withdrawal is prevented by the caller
962 void cachefiles_uncache_page(struct fscache_object *_object, struct page *page)
963 __releases(&object->fscache.cookie->lock)
965 struct cachefiles_object *object;
967 object = container_of(_object, struct cachefiles_object, fscache);
969 _enter("%p,{%lu}", object, page->index);
971 spin_unlock(&object->fscache.cookie->lock);