]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
mm/filemap: Add folio_mkwrite_check_truncate()
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Thu, 29 Apr 2021 02:30:06 +0000 (22:30 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Mon, 18 Oct 2021 11:49:40 +0000 (07:49 -0400)
This is the folio equivalent of page_mkwrite_check_truncate().

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: David Howells <dhowells@redhat.com>
include/linux/pagemap.h

index 5431d9920dc063dfc896bbb95ce223249e0270e8..4b74d83c5571ccf43927a6bc63224e6fc5c152db 100644 (file)
@@ -1120,6 +1120,34 @@ static inline unsigned long dir_pages(struct inode *inode)
                               PAGE_SHIFT;
 }
 
+/**
+ * folio_mkwrite_check_truncate - check if folio was truncated
+ * @folio: the folio to check
+ * @inode: the inode to check the folio against
+ *
+ * Return: the number of bytes in the folio up to EOF,
+ * or -EFAULT if the folio was truncated.
+ */
+static inline ssize_t folio_mkwrite_check_truncate(struct folio *folio,
+                                             struct inode *inode)
+{
+       loff_t size = i_size_read(inode);
+       pgoff_t index = size >> PAGE_SHIFT;
+       size_t offset = offset_in_folio(folio, size);
+
+       if (!folio->mapping)
+               return -EFAULT;
+
+       /* folio is wholly inside EOF */
+       if (folio_next_index(folio) - 1 < index)
+               return folio_size(folio);
+       /* folio is wholly past EOF */
+       if (folio->index > index || !offset)
+               return -EFAULT;
+       /* folio is partially inside EOF */
+       return offset;
+}
+
 /**
  * page_mkwrite_check_truncate - check if page was truncated
  * @page: the page to check