xref: /openbmc/linux/fs/freevxfs/vxfs_inode.c (revision a1e58bbd)
1 /*
2  * Copyright (c) 2000-2001 Christoph Hellwig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * Alternatively, this software may be distributed under the terms of the
15  * GNU General Public License ("GPL").
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Veritas filesystem driver - inode routines.
32  */
33 #include <linux/fs.h>
34 #include <linux/buffer_head.h>
35 #include <linux/pagemap.h>
36 #include <linux/kernel.h>
37 #include <linux/slab.h>
38 
39 #include "vxfs.h"
40 #include "vxfs_inode.h"
41 #include "vxfs_extern.h"
42 
43 
44 extern const struct address_space_operations vxfs_aops;
45 extern const struct address_space_operations vxfs_immed_aops;
46 
47 extern const struct inode_operations vxfs_immed_symlink_iops;
48 
49 struct kmem_cache		*vxfs_inode_cachep;
50 
51 
52 #ifdef DIAGNOSTIC
53 /*
54  * Dump inode contents (partially).
55  */
56 void
57 vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
58 {
59 	printk(KERN_DEBUG "\n\n");
60 	if (ino)
61 		printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
62 	else
63 		printk(KERN_DEBUG "dumping unknown vxfs inode\n");
64 
65 	printk(KERN_DEBUG "---------------------------\n");
66 	printk(KERN_DEBUG "mode is %x\n", vip->vii_mode);
67 	printk(KERN_DEBUG "nlink:%u, uid:%u, gid:%u\n",
68 			vip->vii_nlink, vip->vii_uid, vip->vii_gid);
69 	printk(KERN_DEBUG "size:%Lx, blocks:%u\n",
70 			vip->vii_size, vip->vii_blocks);
71 	printk(KERN_DEBUG "orgtype:%u\n", vip->vii_orgtype);
72 }
73 #endif
74 
75 
76 /**
77  * vxfs_blkiget - find inode based on extent #
78  * @sbp:	superblock of the filesystem we search in
79  * @extent:	number of the extent to search
80  * @ino:	inode number to search
81  *
82  * Description:
83  *  vxfs_blkiget searches inode @ino in the filesystem described by
84  *  @sbp in the extent @extent.
85  *  Returns the matching VxFS inode on success, else a NULL pointer.
86  *
87  * NOTE:
88  *  While __vxfs_iget uses the pagecache vxfs_blkiget uses the
89  *  buffercache.  This function should not be used outside the
90  *  read_super() method, otherwise the data may be incoherent.
91  */
92 struct vxfs_inode_info *
93 vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
94 {
95 	struct buffer_head		*bp;
96 	u_long				block, offset;
97 
98 	block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
99 	offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
100 	bp = sb_bread(sbp, block);
101 
102 	if (bp && buffer_mapped(bp)) {
103 		struct vxfs_inode_info	*vip;
104 		struct vxfs_dinode	*dip;
105 
106 		if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
107 			goto fail;
108 		dip = (struct vxfs_dinode *)(bp->b_data + offset);
109 		memcpy(vip, dip, sizeof(*vip));
110 #ifdef DIAGNOSTIC
111 		vxfs_dumpi(vip, ino);
112 #endif
113 		brelse(bp);
114 		return (vip);
115 	}
116 
117 fail:
118 	printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
119 	brelse(bp);
120 	return NULL;
121 }
122 
123 /**
124  * __vxfs_iget - generic find inode facility
125  * @sbp:		VFS superblock
126  * @ino:		inode number
127  * @ilistp:		inode list
128  *
129  * Description:
130  *  Search the for inode number @ino in the filesystem
131  *  described by @sbp.  Use the specified inode table (@ilistp).
132  *  Returns the matching VxFS inode on success, else an error code.
133  */
134 static struct vxfs_inode_info *
135 __vxfs_iget(ino_t ino, struct inode *ilistp)
136 {
137 	struct page			*pp;
138 	u_long				offset;
139 
140 	offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
141 	pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
142 
143 	if (!IS_ERR(pp)) {
144 		struct vxfs_inode_info	*vip;
145 		struct vxfs_dinode	*dip;
146 		caddr_t			kaddr = (char *)page_address(pp);
147 
148 		if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
149 			goto fail;
150 		dip = (struct vxfs_dinode *)(kaddr + offset);
151 		memcpy(vip, dip, sizeof(*vip));
152 #ifdef DIAGNOSTIC
153 		vxfs_dumpi(vip, ino);
154 #endif
155 		vxfs_put_page(pp);
156 		return (vip);
157 	}
158 
159 	printk(KERN_WARNING "vxfs: error on page %p\n", pp);
160 	return ERR_CAST(pp);
161 
162 fail:
163 	printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
164 	vxfs_put_page(pp);
165 	return ERR_PTR(-ENOMEM);
166 }
167 
168 /**
169  * vxfs_stiget - find inode using the structural inode list
170  * @sbp:	VFS superblock
171  * @ino:	inode #
172  *
173  * Description:
174  *  Find inode @ino in the filesystem described by @sbp using
175  *  the structural inode list.
176  *  Returns the matching VxFS inode on success, else a NULL pointer.
177  */
178 struct vxfs_inode_info *
179 vxfs_stiget(struct super_block *sbp, ino_t ino)
180 {
181 	struct vxfs_inode_info *vip;
182 
183 	vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
184 	return IS_ERR(vip) ? NULL : vip;
185 }
186 
187 /**
188  * vxfs_transmod - mode for a VxFS inode
189  * @vip:	VxFS inode
190  *
191  * Description:
192  *  vxfs_transmod returns a Linux mode_t for a given
193  *  VxFS inode structure.
194  */
195 static __inline__ mode_t
196 vxfs_transmod(struct vxfs_inode_info *vip)
197 {
198 	mode_t			ret = vip->vii_mode & ~VXFS_TYPE_MASK;
199 
200 	if (VXFS_ISFIFO(vip))
201 		ret |= S_IFIFO;
202 	if (VXFS_ISCHR(vip))
203 		ret |= S_IFCHR;
204 	if (VXFS_ISDIR(vip))
205 		ret |= S_IFDIR;
206 	if (VXFS_ISBLK(vip))
207 		ret |= S_IFBLK;
208 	if (VXFS_ISLNK(vip))
209 		ret |= S_IFLNK;
210 	if (VXFS_ISREG(vip))
211 		ret |= S_IFREG;
212 	if (VXFS_ISSOC(vip))
213 		ret |= S_IFSOCK;
214 
215 	return (ret);
216 }
217 
218 /**
219  * vxfs_iinit- helper to fill inode fields
220  * @ip:		VFS inode
221  * @vip:	VxFS inode
222  *
223  * Description:
224  *  vxfs_instino is a helper function to fill in all relevant
225  *  fields in @ip from @vip.
226  */
227 static void
228 vxfs_iinit(struct inode *ip, struct vxfs_inode_info *vip)
229 {
230 
231 	ip->i_mode = vxfs_transmod(vip);
232 	ip->i_uid = (uid_t)vip->vii_uid;
233 	ip->i_gid = (gid_t)vip->vii_gid;
234 
235 	ip->i_nlink = vip->vii_nlink;
236 	ip->i_size = vip->vii_size;
237 
238 	ip->i_atime.tv_sec = vip->vii_atime;
239 	ip->i_ctime.tv_sec = vip->vii_ctime;
240 	ip->i_mtime.tv_sec = vip->vii_mtime;
241 	ip->i_atime.tv_nsec = 0;
242 	ip->i_ctime.tv_nsec = 0;
243 	ip->i_mtime.tv_nsec = 0;
244 
245 	ip->i_blocks = vip->vii_blocks;
246 	ip->i_generation = vip->vii_gen;
247 
248 	ip->i_private = vip;
249 
250 }
251 
252 /**
253  * vxfs_get_fake_inode - get fake inode structure
254  * @sbp:		filesystem superblock
255  * @vip:		fspriv inode
256  *
257  * Description:
258  *  vxfs_fake_inode gets a fake inode (not in the inode hash) for a
259  *  superblock, vxfs_inode pair.
260  *  Returns the filled VFS inode.
261  */
262 struct inode *
263 vxfs_get_fake_inode(struct super_block *sbp, struct vxfs_inode_info *vip)
264 {
265 	struct inode			*ip = NULL;
266 
267 	if ((ip = new_inode(sbp))) {
268 		vxfs_iinit(ip, vip);
269 		ip->i_mapping->a_ops = &vxfs_aops;
270 	}
271 	return (ip);
272 }
273 
274 /**
275  * vxfs_put_fake_inode - free faked inode
276  * *ip:			VFS inode
277  *
278  * Description:
279  *  vxfs_put_fake_inode frees all data asssociated with @ip.
280  */
281 void
282 vxfs_put_fake_inode(struct inode *ip)
283 {
284 	iput(ip);
285 }
286 
287 /**
288  * vxfs_iget - get an inode
289  * @sbp:	the superblock to get the inode for
290  * @ino:	the number of the inode to get
291  *
292  * Description:
293  *  vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
294  *  in all relevant fields in the new inode.
295  */
296 struct inode *
297 vxfs_iget(struct super_block *sbp, ino_t ino)
298 {
299 	struct vxfs_inode_info		*vip;
300 	const struct address_space_operations	*aops;
301 	struct inode *ip;
302 
303 	ip = iget_locked(sbp, ino);
304 	if (!ip)
305 		return ERR_PTR(-ENOMEM);
306 	if (!(ip->i_state & I_NEW))
307 		return ip;
308 
309 	vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist);
310 	if (IS_ERR(vip)) {
311 		iget_failed(ip);
312 		return ERR_CAST(vip);
313 	}
314 
315 	vxfs_iinit(ip, vip);
316 
317 	if (VXFS_ISIMMED(vip))
318 		aops = &vxfs_immed_aops;
319 	else
320 		aops = &vxfs_aops;
321 
322 	if (S_ISREG(ip->i_mode)) {
323 		ip->i_fop = &generic_ro_fops;
324 		ip->i_mapping->a_ops = aops;
325 	} else if (S_ISDIR(ip->i_mode)) {
326 		ip->i_op = &vxfs_dir_inode_ops;
327 		ip->i_fop = &vxfs_dir_operations;
328 		ip->i_mapping->a_ops = aops;
329 	} else if (S_ISLNK(ip->i_mode)) {
330 		if (!VXFS_ISIMMED(vip)) {
331 			ip->i_op = &page_symlink_inode_operations;
332 			ip->i_mapping->a_ops = &vxfs_aops;
333 		} else
334 			ip->i_op = &vxfs_immed_symlink_iops;
335 	} else
336 		init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
337 
338 	unlock_new_inode(ip);
339 	return ip;
340 }
341 
342 /**
343  * vxfs_clear_inode - remove inode from main memory
344  * @ip:		inode to discard.
345  *
346  * Description:
347  *  vxfs_clear_inode() is called on the final iput and frees the private
348  *  inode area.
349  */
350 void
351 vxfs_clear_inode(struct inode *ip)
352 {
353 	kmem_cache_free(vxfs_inode_cachep, ip->i_private);
354 }
355