xref: /linux/fs/freevxfs/vxfs_subr.c (revision 1771c8c9e65a20128f93df107353a5f4cb91546a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2000-2001 Christoph Hellwig.
4  */
5 
6 /*
7  * Veritas filesystem driver - shared subroutines.
8  */
9 #include <linux/fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/kernel.h>
12 #include <linux/pagemap.h>
13 
14 #include "vxfs_extern.h"
15 
16 
17 static int		vxfs_read_folio(struct file *, struct folio *);
18 static sector_t		vxfs_bmap(struct address_space *, sector_t);
19 
20 const struct address_space_operations vxfs_aops = {
21 	.read_folio =		vxfs_read_folio,
22 	.bmap =			vxfs_bmap,
23 };
24 
25 inline void
26 vxfs_put_page(struct page *pp)
27 {
28 	kunmap(pp);
29 	put_page(pp);
30 }
31 
32 /**
33  * vxfs_get_page - read a page into memory.
34  * @ip:		inode to read from
35  * @n:		page number
36  *
37  * Description:
38  *   vxfs_get_page reads the @n th page of @ip into the pagecache.
39  *
40  * Returns:
41  *   The wanted page on success, else a NULL pointer.
42  */
43 struct page *
44 vxfs_get_page(struct address_space *mapping, u_long n)
45 {
46 	struct page *			pp;
47 
48 	pp = read_mapping_page(mapping, n, NULL);
49 
50 	if (!IS_ERR(pp)) {
51 		kmap(pp);
52 		/** if (!PageChecked(pp)) **/
53 			/** vxfs_check_page(pp); **/
54 		if (PageError(pp))
55 			goto fail;
56 	}
57 
58 	return (pp);
59 
60 fail:
61 	vxfs_put_page(pp);
62 	return ERR_PTR(-EIO);
63 }
64 
65 /**
66  * vxfs_bread - read buffer for a give inode,block tuple
67  * @ip:		inode
68  * @block:	logical block
69  *
70  * Description:
71  *   The vxfs_bread function reads block no @block  of
72  *   @ip into the buffercache.
73  *
74  * Returns:
75  *   The resulting &struct buffer_head.
76  */
77 struct buffer_head *
78 vxfs_bread(struct inode *ip, int block)
79 {
80 	struct buffer_head	*bp;
81 	daddr_t			pblock;
82 
83 	pblock = vxfs_bmap1(ip, block);
84 	bp = sb_bread(ip->i_sb, pblock);
85 
86 	return (bp);
87 }
88 
89 /**
90  * vxfs_get_block - locate buffer for given inode,block tuple
91  * @ip:		inode
92  * @iblock:	logical block
93  * @bp:		buffer skeleton
94  * @create:	%TRUE if blocks may be newly allocated.
95  *
96  * Description:
97  *   The vxfs_get_block function fills @bp with the right physical
98  *   block and device number to perform a lowlevel read/write on
99  *   it.
100  *
101  * Returns:
102  *   Zero on success, else a negativ error code (-EIO).
103  */
104 static int
105 vxfs_getblk(struct inode *ip, sector_t iblock,
106 	    struct buffer_head *bp, int create)
107 {
108 	daddr_t			pblock;
109 
110 	pblock = vxfs_bmap1(ip, iblock);
111 	if (pblock != 0) {
112 		map_bh(bp, ip->i_sb, pblock);
113 		return 0;
114 	}
115 
116 	return -EIO;
117 }
118 
119 /**
120  * vxfs_read_folio - read one page synchronously into the pagecache
121  * @file:	file context (unused)
122  * @folio:	folio to fill in.
123  *
124  * Description:
125  *   The vxfs_read_folio routine reads @folio synchronously into the
126  *   pagecache.
127  *
128  * Returns:
129  *   Zero on success, else a negative error code.
130  *
131  * Locking status:
132  *   @folio is locked and will be unlocked.
133  */
134 static int vxfs_read_folio(struct file *file, struct folio *folio)
135 {
136 	return block_read_full_folio(folio, vxfs_getblk);
137 }
138 
139 /**
140  * vxfs_bmap - perform logical to physical block mapping
141  * @mapping:	logical to physical mapping to use
142  * @block:	logical block (relative to @mapping).
143  *
144  * Description:
145  *   Vxfs_bmap find out the corresponding phsical block to the
146  *   @mapping, @block pair.
147  *
148  * Returns:
149  *   Physical block number on success, else Zero.
150  *
151  * Locking status:
152  *   We are under the bkl.
153  */
154 static sector_t
155 vxfs_bmap(struct address_space *mapping, sector_t block)
156 {
157 	return generic_block_bmap(mapping, block, vxfs_getblk);
158 }
159