Merge tag 'dma-mapping-5.9-1' of git://git.infradead.org/users/hch/dma-mapping
[linux-2.6-microblaze.git] / include / linux / pagemap.h
index a8f7bd8..7de11dc 100644 (file)
@@ -51,7 +51,10 @@ static inline void mapping_set_error(struct address_space *mapping, int error)
                return;
 
        /* Record in wb_err for checkers using errseq_t based tracking */
-       filemap_set_wb_err(mapping, error);
+       __filemap_set_wb_err(mapping, error);
+
+       /* Record it in superblock */
+       errseq_set(&mapping->host->i_sb->s_wb_err, error);
 
        /* Record it in flags for now, for legacy callers */
        if (error == -ENOSPC)
@@ -205,6 +208,43 @@ static inline int page_cache_add_speculative(struct page *page, int count)
        return __page_cache_add_speculative(page, count);
 }
 
+/**
+ * attach_page_private - Attach private data to a page.
+ * @page: Page to attach data to.
+ * @data: Data to attach to page.
+ *
+ * Attaching private data to a page increments the page's reference count.
+ * The data must be detached before the page will be freed.
+ */
+static inline void attach_page_private(struct page *page, void *data)
+{
+       get_page(page);
+       set_page_private(page, (unsigned long)data);
+       SetPagePrivate(page);
+}
+
+/**
+ * detach_page_private - Detach private data from a page.
+ * @page: Page to detach data from.
+ *
+ * Removes the data that was previously attached to the page and decrements
+ * the refcount on the page.
+ *
+ * Return: Data that was attached to the page.
+ */
+static inline void *detach_page_private(struct page *page)
+{
+       void *data = (void *)page_private(page);
+
+       if (!PagePrivate(page))
+               return NULL;
+       ClearPagePrivate(page);
+       set_page_private(page, 0);
+       put_page(page);
+
+       return data;
+}
+
 #ifdef CONFIG_NUMA
 extern struct page *__page_cache_alloc(gfp_t gfp);
 #else
@@ -341,7 +381,7 @@ static inline struct page *find_subpage(struct page *head, pgoff_t index)
        if (PageHuge(head))
                return head;
 
-       return head + (index & (hpage_nr_pages(head) - 1));
+       return head + (index & (thp_nr_pages(head) - 1));
 }
 
 struct page *find_get_entry(struct address_space *mapping, pgoff_t offset);
@@ -456,8 +496,35 @@ static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
        return pgoff;
 }
 
+/* This has the same layout as wait_bit_key - see fs/cachefiles/rdwr.c */
+struct wait_page_key {
+       struct page *page;
+       int bit_nr;
+       int page_match;
+};
+
+struct wait_page_queue {
+       struct page *page;
+       int bit_nr;
+       wait_queue_entry_t wait;
+};
+
+static inline bool wake_page_match(struct wait_page_queue *wait_page,
+                                 struct wait_page_key *key)
+{
+       if (wait_page->page != key->page)
+              return false;
+       key->page_match = 1;
+
+       if (wait_page->bit_nr != key->bit_nr)
+               return false;
+
+       return true;
+}
+
 extern void __lock_page(struct page *page);
 extern int __lock_page_killable(struct page *page);
+extern int __lock_page_async(struct page *page, struct wait_page_queue *wait);
 extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
                                unsigned int flags);
 extern void unlock_page(struct page *page);
@@ -494,11 +561,27 @@ static inline int lock_page_killable(struct page *page)
        return 0;
 }
 
+/*
+ * lock_page_async - Lock the page, unless this would block. If the page
+ * is already locked, then queue a callback when the page becomes unlocked.
+ * This callback can then retry the operation.
+ *
+ * Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page
+ * was already locked and the callback defined in 'wait' was queued.
+ */
+static inline int lock_page_async(struct page *page,
+                                 struct wait_page_queue *wait)
+{
+       if (!trylock_page(page))
+               return __lock_page_async(page, wait);
+       return 0;
+}
+
 /*
  * lock_page_or_retry - Lock the page, unless this would block and the
  * caller indicated that it can handle a retry.
  *
- * Return value and mmap_sem implications depend on flags; see
+ * Return value and mmap_lock implications depend on flags; see
  * __lock_page_or_retry().
  */
 static inline int lock_page_or_retry(struct page *page, struct mm_struct *mm,
@@ -615,6 +698,17 @@ int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask);
 void delete_from_page_cache_batch(struct address_space *mapping,
                                  struct pagevec *pvec);
 
+#define VM_READAHEAD_PAGES     (SZ_128K / PAGE_SIZE)
+
+void page_cache_sync_readahead(struct address_space *, struct file_ra_state *,
+               struct file *, pgoff_t index, unsigned long req_count);
+void page_cache_async_readahead(struct address_space *, struct file_ra_state *,
+               struct file *, struct page *, pgoff_t index,
+               unsigned long req_count);
+void page_cache_readahead_unbounded(struct address_space *, struct file *,
+               pgoff_t index, unsigned long nr_to_read,
+               unsigned long lookahead_count);
+
 /*
  * Like add_to_page_cache_locked, but used to add newly allocated pages:
  * the page is new, so we can just run __SetPageLocked() against it.
@@ -631,6 +725,146 @@ static inline int add_to_page_cache(struct page *page,
        return error;
 }
 
+/**
+ * struct readahead_control - Describes a readahead request.
+ *
+ * A readahead request is for consecutive pages.  Filesystems which
+ * implement the ->readahead method should call readahead_page() or
+ * readahead_page_batch() in a loop and attempt to start I/O against
+ * each page in the request.
+ *
+ * Most of the fields in this struct are private and should be accessed
+ * by the functions below.
+ *
+ * @file: The file, used primarily by network filesystems for authentication.
+ *       May be NULL if invoked internally by the filesystem.
+ * @mapping: Readahead this filesystem object.
+ */
+struct readahead_control {
+       struct file *file;
+       struct address_space *mapping;
+/* private: use the readahead_* accessors instead */
+       pgoff_t _index;
+       unsigned int _nr_pages;
+       unsigned int _batch_count;
+};
+
+/**
+ * readahead_page - Get the next page to read.
+ * @rac: The current readahead request.
+ *
+ * Context: The page is locked and has an elevated refcount.  The caller
+ * should decreases the refcount once the page has been submitted for I/O
+ * and unlock the page once all I/O to that page has completed.
+ * Return: A pointer to the next page, or %NULL if we are done.
+ */
+static inline struct page *readahead_page(struct readahead_control *rac)
+{
+       struct page *page;
+
+       BUG_ON(rac->_batch_count > rac->_nr_pages);
+       rac->_nr_pages -= rac->_batch_count;
+       rac->_index += rac->_batch_count;
+
+       if (!rac->_nr_pages) {
+               rac->_batch_count = 0;
+               return NULL;
+       }
+
+       page = xa_load(&rac->mapping->i_pages, rac->_index);
+       VM_BUG_ON_PAGE(!PageLocked(page), page);
+       rac->_batch_count = thp_nr_pages(page);
+
+       return page;
+}
+
+static inline unsigned int __readahead_batch(struct readahead_control *rac,
+               struct page **array, unsigned int array_sz)
+{
+       unsigned int i = 0;
+       XA_STATE(xas, &rac->mapping->i_pages, 0);
+       struct page *page;
+
+       BUG_ON(rac->_batch_count > rac->_nr_pages);
+       rac->_nr_pages -= rac->_batch_count;
+       rac->_index += rac->_batch_count;
+       rac->_batch_count = 0;
+
+       xas_set(&xas, rac->_index);
+       rcu_read_lock();
+       xas_for_each(&xas, page, rac->_index + rac->_nr_pages - 1) {
+               VM_BUG_ON_PAGE(!PageLocked(page), page);
+               VM_BUG_ON_PAGE(PageTail(page), page);
+               array[i++] = page;
+               rac->_batch_count += thp_nr_pages(page);
+
+               /*
+                * The page cache isn't using multi-index entries yet,
+                * so the xas cursor needs to be manually moved to the
+                * next index.  This can be removed once the page cache
+                * is converted.
+                */
+               if (PageHead(page))
+                       xas_set(&xas, rac->_index + rac->_batch_count);
+
+               if (i == array_sz)
+                       break;
+       }
+       rcu_read_unlock();
+
+       return i;
+}
+
+/**
+ * readahead_page_batch - Get a batch of pages to read.
+ * @rac: The current readahead request.
+ * @array: An array of pointers to struct page.
+ *
+ * Context: The pages are locked and have an elevated refcount.  The caller
+ * should decreases the refcount once the page has been submitted for I/O
+ * and unlock the page once all I/O to that page has completed.
+ * Return: The number of pages placed in the array.  0 indicates the request
+ * is complete.
+ */
+#define readahead_page_batch(rac, array)                               \
+       __readahead_batch(rac, array, ARRAY_SIZE(array))
+
+/**
+ * readahead_pos - The byte offset into the file of this readahead request.
+ * @rac: The readahead request.
+ */
+static inline loff_t readahead_pos(struct readahead_control *rac)
+{
+       return (loff_t)rac->_index * PAGE_SIZE;
+}
+
+/**
+ * readahead_length - The number of bytes in this readahead request.
+ * @rac: The readahead request.
+ */
+static inline loff_t readahead_length(struct readahead_control *rac)
+{
+       return (loff_t)rac->_nr_pages * PAGE_SIZE;
+}
+
+/**
+ * readahead_index - The index of the first page in this readahead request.
+ * @rac: The readahead request.
+ */
+static inline pgoff_t readahead_index(struct readahead_control *rac)
+{
+       return rac->_index;
+}
+
+/**
+ * readahead_count - The number of pages in this readahead request.
+ * @rac: The readahead request.
+ */
+static inline unsigned int readahead_count(struct readahead_control *rac)
+{
+       return rac->_nr_pages;
+}
+
 static inline unsigned long dir_pages(struct inode *inode)
 {
        return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>