2 * Copyright (c) 2000-2001 Christoph Hellwig.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * Alternatively, this software may be distributed under the terms of the
15 * GNU General Public License ("GPL").
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
31 * Veritas filesystem driver - inode routines.
34 #include <linux/buffer_head.h>
35 #include <linux/pagemap.h>
36 #include <linux/kernel.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
41 #include "vxfs_inode.h"
42 #include "vxfs_extern.h"
45 struct kmem_cache *vxfs_inode_cachep;
47 static struct inode * vxfs_get_fake_inode(struct super_block *,
48 struct vxfs_inode_info *);
52 * Dump inode contents (partially).
55 vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
57 printk(KERN_DEBUG "\n\n");
59 printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
61 printk(KERN_DEBUG "dumping unknown vxfs inode\n");
63 printk(KERN_DEBUG "---------------------------\n");
64 printk(KERN_DEBUG "mode is %x\n", vip->vii_mode);
65 printk(KERN_DEBUG "nlink:%u, uid:%u, gid:%u\n",
66 vip->vii_nlink, vip->vii_uid, vip->vii_gid);
67 printk(KERN_DEBUG "size:%Lx, blocks:%u\n",
68 vip->vii_size, vip->vii_blocks);
69 printk(KERN_DEBUG "orgtype:%u\n", vip->vii_orgtype);
73 static inline void dip2vip_cpy(struct vxfs_sb_info *sbi,
74 struct vxfs_inode_info *vip, struct vxfs_dinode *dip)
76 vip->vii_mode = fs32_to_cpu(sbi, dip->vdi_mode);
77 vip->vii_nlink = fs32_to_cpu(sbi, dip->vdi_nlink);
78 vip->vii_uid = fs32_to_cpu(sbi, dip->vdi_uid);
79 vip->vii_gid = fs32_to_cpu(sbi, dip->vdi_gid);
80 vip->vii_size = fs64_to_cpu(sbi, dip->vdi_size);
81 vip->vii_atime = fs32_to_cpu(sbi, dip->vdi_atime);
82 vip->vii_autime = fs32_to_cpu(sbi, dip->vdi_autime);
83 vip->vii_mtime = fs32_to_cpu(sbi, dip->vdi_mtime);
84 vip->vii_mutime = fs32_to_cpu(sbi, dip->vdi_mutime);
85 vip->vii_ctime = fs32_to_cpu(sbi, dip->vdi_ctime);
86 vip->vii_cutime = fs32_to_cpu(sbi, dip->vdi_cutime);
87 vip->vii_orgtype = dip->vdi_orgtype;
89 vip->vii_blocks = fs32_to_cpu(sbi, dip->vdi_blocks);
90 vip->vii_gen = fs32_to_cpu(sbi, dip->vdi_gen);
93 vip->vii_dotdot = fs32_to_cpu(sbi, dip->vdi_dotdot);
94 else if (!VXFS_ISREG(vip) && !VXFS_ISLNK(vip))
95 vip->vii_rdev = fs32_to_cpu(sbi, dip->vdi_rdev);
97 /* don't endian swap the fields that differ by orgtype */
98 memcpy(&vip->vii_org, &dip->vdi_org, sizeof(vip->vii_org));
102 * vxfs_blkiget - find inode based on extent #
103 * @sbp: superblock of the filesystem we search in
104 * @extent: number of the extent to search
105 * @ino: inode number to search
108 * vxfs_blkiget searches inode @ino in the filesystem described by
109 * @sbp in the extent @extent.
110 * Returns the matching VxFS inode on success, else a NULL pointer.
113 * While __vxfs_iget uses the pagecache vxfs_blkiget uses the
114 * buffercache. This function should not be used outside the
115 * read_super() method, otherwise the data may be incoherent.
118 vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
120 struct buffer_head *bp;
122 u_long block, offset;
124 block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
125 offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
126 bp = sb_bread(sbp, block);
128 if (bp && buffer_mapped(bp)) {
129 struct vxfs_inode_info *vip;
130 struct vxfs_dinode *dip;
132 if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
134 dip = (struct vxfs_dinode *)(bp->b_data + offset);
135 dip2vip_cpy(VXFS_SBI(sbp), vip, dip);
137 vxfs_dumpi(vip, ino);
141 inode = vxfs_get_fake_inode(sbp, vip);
143 kmem_cache_free(vxfs_inode_cachep, vip);
148 printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
154 * __vxfs_iget - generic find inode facility
155 * @sbp: VFS superblock
157 * @ilistp: inode list
160 * Search the for inode number @ino in the filesystem
161 * described by @sbp. Use the specified inode table (@ilistp).
162 * Returns the matching inode on success, else an error code.
164 static struct vxfs_inode_info *
165 __vxfs_iget(ino_t ino, struct inode *ilistp)
170 offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
171 pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
174 struct vxfs_inode_info *vip;
175 struct vxfs_dinode *dip;
176 caddr_t kaddr = (char *)page_address(pp);
178 if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
180 dip = (struct vxfs_dinode *)(kaddr + offset);
181 dip2vip_cpy(VXFS_SBI(ilistp->i_sb), vip, dip);
183 vxfs_dumpi(vip, ino);
189 printk(KERN_WARNING "vxfs: error on page %p\n", pp);
193 printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
195 return ERR_PTR(-ENOMEM);
199 * vxfs_stiget - find inode using the structural inode list
200 * @sbp: VFS superblock
204 * Find inode @ino in the filesystem described by @sbp using
205 * the structural inode list.
206 * Returns the matching inode on success, else a NULL pointer.
209 vxfs_stiget(struct super_block *sbp, ino_t ino)
211 struct vxfs_inode_info *vip;
214 vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
217 inode = vxfs_get_fake_inode(sbp, vip);
219 kmem_cache_free(vxfs_inode_cachep, vip);
224 * vxfs_transmod - mode for a VxFS inode
228 * vxfs_transmod returns a Linux mode_t for a given
229 * VxFS inode structure.
231 static __inline__ umode_t
232 vxfs_transmod(struct vxfs_inode_info *vip)
234 umode_t ret = vip->vii_mode & ~VXFS_TYPE_MASK;
236 if (VXFS_ISFIFO(vip))
255 * vxfs_iinit- helper to fill inode fields
260 * vxfs_instino is a helper function to fill in all relevant
261 * fields in @ip from @vip.
264 vxfs_iinit(struct inode *ip, struct vxfs_inode_info *vip)
267 ip->i_mode = vxfs_transmod(vip);
268 i_uid_write(ip, (uid_t)vip->vii_uid);
269 i_gid_write(ip, (gid_t)vip->vii_gid);
271 set_nlink(ip, vip->vii_nlink);
272 ip->i_size = vip->vii_size;
274 ip->i_atime.tv_sec = vip->vii_atime;
275 ip->i_ctime.tv_sec = vip->vii_ctime;
276 ip->i_mtime.tv_sec = vip->vii_mtime;
277 ip->i_atime.tv_nsec = 0;
278 ip->i_ctime.tv_nsec = 0;
279 ip->i_mtime.tv_nsec = 0;
281 ip->i_blocks = vip->vii_blocks;
282 ip->i_generation = vip->vii_gen;
289 * vxfs_get_fake_inode - get fake inode structure
290 * @sbp: filesystem superblock
294 * vxfs_fake_inode gets a fake inode (not in the inode hash) for a
295 * superblock, vxfs_inode pair.
296 * Returns the filled VFS inode.
298 static struct inode *
299 vxfs_get_fake_inode(struct super_block *sbp, struct vxfs_inode_info *vip)
301 struct inode *ip = NULL;
303 if ((ip = new_inode(sbp))) {
304 ip->i_ino = get_next_ino();
306 ip->i_mapping->a_ops = &vxfs_aops;
312 * vxfs_iget - get an inode
313 * @sbp: the superblock to get the inode for
314 * @ino: the number of the inode to get
317 * vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
318 * in all relevant fields in the new inode.
321 vxfs_iget(struct super_block *sbp, ino_t ino)
323 struct vxfs_inode_info *vip;
324 const struct address_space_operations *aops;
327 ip = iget_locked(sbp, ino);
329 return ERR_PTR(-ENOMEM);
330 if (!(ip->i_state & I_NEW))
333 vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist);
336 return ERR_CAST(vip);
341 if (VXFS_ISIMMED(vip))
342 aops = &vxfs_immed_aops;
346 if (S_ISREG(ip->i_mode)) {
347 ip->i_fop = &generic_ro_fops;
348 ip->i_mapping->a_ops = aops;
349 } else if (S_ISDIR(ip->i_mode)) {
350 ip->i_op = &vxfs_dir_inode_ops;
351 ip->i_fop = &vxfs_dir_operations;
352 ip->i_mapping->a_ops = aops;
353 } else if (S_ISLNK(ip->i_mode)) {
354 if (!VXFS_ISIMMED(vip)) {
355 ip->i_op = &page_symlink_inode_operations;
357 ip->i_mapping->a_ops = &vxfs_aops;
359 ip->i_op = &simple_symlink_inode_operations;
360 ip->i_link = vip->vii_immed.vi_immed;
361 nd_terminate_link(ip->i_link, ip->i_size,
362 sizeof(vip->vii_immed.vi_immed) - 1);
365 init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
367 unlock_new_inode(ip);
371 static void vxfs_i_callback(struct rcu_head *head)
373 struct inode *inode = container_of(head, struct inode, i_rcu);
374 kmem_cache_free(vxfs_inode_cachep, inode->i_private);
378 * vxfs_evict_inode - remove inode from main memory
379 * @ip: inode to discard.
382 * vxfs_evict_inode() is called on the final iput and frees the private
386 vxfs_evict_inode(struct inode *ip)
388 truncate_inode_pages_final(&ip->i_data);
390 call_rcu(&ip->i_rcu, vxfs_i_callback);