Merge tag 'iomap-5.8-merge-1' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[sfrench/cifs-2.6.git] / fs / iomap / buffered-io.c
index 17ed7bb92c6c747ab40e9fe7288688699187bab4..bcfc288dba3fb80b9000e716f570da5c2a83965a 100644 (file)
@@ -59,24 +59,19 @@ iomap_page_create(struct inode *inode, struct page *page)
         * migrate_page_move_mapping() assumes that pages with private data have
         * their count elevated by 1.
         */
-       get_page(page);
-       set_page_private(page, (unsigned long)iop);
-       SetPagePrivate(page);
+       attach_page_private(page, iop);
        return iop;
 }
 
 static void
 iomap_page_release(struct page *page)
 {
-       struct iomap_page *iop = to_iomap_page(page);
+       struct iomap_page *iop = detach_page_private(page);
 
        if (!iop)
                return;
        WARN_ON_ONCE(atomic_read(&iop->read_count));
        WARN_ON_ONCE(atomic_read(&iop->write_count));
-       ClearPagePrivate(page);
-       set_page_private(page, 0);
-       put_page(page);
        kfree(iop);
 }
 
@@ -214,9 +209,8 @@ iomap_read_end_io(struct bio *bio)
 struct iomap_readpage_ctx {
        struct page             *cur_page;
        bool                    cur_page_in_bio;
-       bool                    is_readahead;
        struct bio              *bio;
-       struct list_head        *pages;
+       struct readahead_control *rac;
 };
 
 static void
@@ -308,7 +302,7 @@ iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
                if (ctx->bio)
                        submit_bio(ctx->bio);
 
-               if (ctx->is_readahead) /* same as readahead_gfp_mask */
+               if (ctx->rac) /* same as readahead_gfp_mask */
                        gfp |= __GFP_NORETRY | __GFP_NOWARN;
                ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
                /*
@@ -319,7 +313,7 @@ iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
                if (!ctx->bio)
                        ctx->bio = bio_alloc(orig_gfp, 1);
                ctx->bio->bi_opf = REQ_OP_READ;
-               if (ctx->is_readahead)
+               if (ctx->rac)
                        ctx->bio->bi_opf |= REQ_RAHEAD;
                ctx->bio->bi_iter.bi_sector = sector;
                bio_set_dev(ctx->bio, iomap->bdev);
@@ -367,7 +361,7 @@ iomap_readpage(struct page *page, const struct iomap_ops *ops)
        }
 
        /*
-        * Just like mpage_readpages and block_read_full_page we always
+        * Just like mpage_readahead and block_read_full_page we always
         * return 0 and just mark the page as PageError on errors.  This
         * should be cleaned up all through the stack eventually.
         */
@@ -375,36 +369,8 @@ iomap_readpage(struct page *page, const struct iomap_ops *ops)
 }
 EXPORT_SYMBOL_GPL(iomap_readpage);
 
-static struct page *
-iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
-               loff_t length, loff_t *done)
-{
-       while (!list_empty(pages)) {
-               struct page *page = lru_to_page(pages);
-
-               if (page_offset(page) >= (u64)pos + length)
-                       break;
-
-               list_del(&page->lru);
-               if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
-                               GFP_NOFS))
-                       return page;
-
-               /*
-                * If we already have a page in the page cache at index we are
-                * done.  Upper layers don't care if it is uptodate after the
-                * readpages call itself as every page gets checked again once
-                * actually needed.
-                */
-               *done += PAGE_SIZE;
-               put_page(page);
-       }
-
-       return NULL;
-}
-
 static loff_t
-iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
+iomap_readahead_actor(struct inode *inode, loff_t pos, loff_t length,
                void *data, struct iomap *iomap, struct iomap *srcmap)
 {
        struct iomap_readpage_ctx *ctx = data;
@@ -418,10 +384,7 @@ iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
                        ctx->cur_page = NULL;
                }
                if (!ctx->cur_page) {
-                       ctx->cur_page = iomap_next_page(inode, ctx->pages,
-                                       pos, length, &done);
-                       if (!ctx->cur_page)
-                               break;
+                       ctx->cur_page = readahead_page(ctx->rac);
                        ctx->cur_page_in_bio = false;
                }
                ret = iomap_readpage_actor(inode, pos + done, length - done,
@@ -431,32 +394,43 @@ iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
        return done;
 }
 
-int
-iomap_readpages(struct address_space *mapping, struct list_head *pages,
-               unsigned nr_pages, const struct iomap_ops *ops)
+/**
+ * iomap_readahead - Attempt to read pages from a file.
+ * @rac: Describes the pages to be read.
+ * @ops: The operations vector for the filesystem.
+ *
+ * This function is for filesystems to call to implement their readahead
+ * address_space operation.
+ *
+ * Context: The @ops callbacks may submit I/O (eg to read the addresses of
+ * blocks from disc), and may wait for it.  The caller may be trying to
+ * access a different page, and so sleeping excessively should be avoided.
+ * It may allocate memory, but should avoid costly allocations.  This
+ * function is called with memalloc_nofs set, so allocations will not cause
+ * the filesystem to be reentered.
+ */
+void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
 {
+       struct inode *inode = rac->mapping->host;
+       loff_t pos = readahead_pos(rac);
+       loff_t length = readahead_length(rac);
        struct iomap_readpage_ctx ctx = {
-               .pages          = pages,
-               .is_readahead   = true,
+               .rac    = rac,
        };
-       loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
-       loff_t last = page_offset(list_entry(pages->next, struct page, lru));
-       loff_t length = last - pos + PAGE_SIZE, ret = 0;
 
-       trace_iomap_readpages(mapping->host, nr_pages);
+       trace_iomap_readahead(inode, readahead_count(rac));
 
        while (length > 0) {
-               ret = iomap_apply(mapping->host, pos, length, 0, ops,
-                               &ctx, iomap_readpages_actor);
+               loff_t ret = iomap_apply(inode, pos, length, 0, ops,
+                               &ctx, iomap_readahead_actor);
                if (ret <= 0) {
                        WARN_ON_ONCE(ret == 0);
-                       goto done;
+                       break;
                }
                pos += ret;
                length -= ret;
        }
-       ret = 0;
-done:
+
        if (ctx.bio)
                submit_bio(ctx.bio);
        if (ctx.cur_page) {
@@ -464,15 +438,8 @@ done:
                        unlock_page(ctx.cur_page);
                put_page(ctx.cur_page);
        }
-
-       /*
-        * Check that we didn't lose a page due to the arcance calling
-        * conventions..
-        */
-       WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
-       return ret;
 }
-EXPORT_SYMBOL_GPL(iomap_readpages);
+EXPORT_SYMBOL_GPL(iomap_readahead);
 
 /*
  * iomap_is_partially_uptodate checks whether blocks within a page are
@@ -554,14 +521,8 @@ iomap_migrate_page(struct address_space *mapping, struct page *newpage,
        if (ret != MIGRATEPAGE_SUCCESS)
                return ret;
 
-       if (page_has_private(page)) {
-               ClearPagePrivate(page);
-               get_page(newpage);
-               set_page_private(newpage, page_private(page));
-               set_page_private(page, 0);
-               put_page(page);
-               SetPagePrivate(newpage);
-       }
+       if (page_has_private(page))
+               attach_page_private(newpage, detach_page_private(page));
 
        if (mode != MIGRATE_SYNC_NO_COPY)
                migrate_page_copy(newpage, page);