ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / freevxfs / vxfs_inode.c
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 struct address_space_operations vxfs_aops;
45 extern struct address_space_operations vxfs_immed_aops;
46
47 extern struct inode_operations vxfs_immed_symlink_iops;
48
49 static struct file_operations vxfs_file_operations = {
50         .open =                 generic_file_open,
51         .llseek =               generic_file_llseek,
52         .read =                 generic_file_read,
53         .mmap =                 generic_file_mmap,
54         .sendfile =             generic_file_sendfile,
55 };
56
57
58 kmem_cache_t            *vxfs_inode_cachep;
59
60
61 #ifdef DIAGNOSTIC
62 /*
63  * Dump inode contents (partially).
64  */
65 void
66 vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
67 {
68         printk(KERN_DEBUG "\n\n");
69         if (ino)
70                 printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
71         else
72                 printk(KERN_DEBUG "dumping unknown vxfs inode\n");
73
74         printk(KERN_DEBUG "---------------------------\n");
75         printk(KERN_DEBUG "mode is %x\n", vip->vii_mode);
76         printk(KERN_DEBUG "nlink:%u, uid:%u, gid:%u\n",
77                         vip->vii_nlink, vip->vii_uid, vip->vii_gid);
78         printk(KERN_DEBUG "size:%Lx, blocks:%u\n",
79                         vip->vii_size, vip->vii_blocks);
80         printk(KERN_DEBUG "orgtype:%u\n", vip->vii_orgtype);
81 }
82 #endif
83
84
85 /**
86  * vxfs_blkiget - find inode based on extent #
87  * @sbp:        superblock of the filesystem we search in
88  * @extent:     number of the extent to search
89  * @ino:        inode number to search
90  *
91  * Description:
92  *  vxfs_blkiget searches inode @ino in the filesystem described by
93  *  @sbp in the extent @extent.
94  *  Returns the matching VxFS inode on success, else a NULL pointer.
95  *
96  * NOTE:
97  *  While __vxfs_iget uses the pagecache vxfs_blkiget uses the
98  *  buffercache.  This function should not be used outside the
99  *  read_super() method, otherwise the data may be incoherent.
100  */
101 struct vxfs_inode_info *
102 vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
103 {
104         struct buffer_head              *bp;
105         u_long                          block, offset;
106
107         block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
108         offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
109         bp = sb_bread(sbp, block);
110
111         if (buffer_mapped(bp)) {
112                 struct vxfs_inode_info  *vip;
113                 struct vxfs_dinode      *dip;
114
115                 if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, SLAB_KERNEL)))
116                         goto fail;
117                 dip = (struct vxfs_dinode *)(bp->b_data + offset);
118                 memcpy(vip, dip, sizeof(*vip));
119 #ifdef DIAGNOSTIC
120                 vxfs_dumpi(vip, ino);
121 #endif
122                 brelse(bp);
123                 return (vip);
124         }
125
126 fail:
127         printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
128         brelse(bp);
129         return NULL;
130 }
131
132 /**
133  * __vxfs_iget - generic find inode facility
134  * @sbp:                VFS superblock
135  * @ino:                inode number
136  * @ilistp:             inode list
137  *
138  * Description:
139  *  Search the for inode number @ino in the filesystem
140  *  described by @sbp.  Use the specified inode table (@ilistp).
141  *  Returns the matching VxFS inode on success, else a NULL pointer.
142  */
143 static struct vxfs_inode_info *
144 __vxfs_iget(ino_t ino, struct inode *ilistp)
145 {
146         struct page                     *pp;
147         u_long                          offset;
148
149         offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
150         pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
151
152         if (!IS_ERR(pp)) {
153                 struct vxfs_inode_info  *vip;
154                 struct vxfs_dinode      *dip;
155                 caddr_t                 kaddr = (char *)page_address(pp);
156
157                 if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, SLAB_KERNEL)))
158                         goto fail;
159                 dip = (struct vxfs_dinode *)(kaddr + offset);
160                 memcpy(vip, dip, sizeof(*vip));
161 #ifdef DIAGNOSTIC
162                 vxfs_dumpi(vip, ino);
163 #endif
164                 vxfs_put_page(pp);
165                 return (vip);
166         }
167
168         printk(KERN_WARNING "vxfs: error on page %p\n", pp);
169         return NULL;
170
171 fail:
172         printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
173         vxfs_put_page(pp);
174         return NULL;
175 }
176
177 /**
178  * vxfs_stiget - find inode using the structural inode list
179  * @sbp:        VFS superblock
180  * @ino:        inode #
181  *
182  * Description:
183  *  Find inode @ino in the filesystem described by @sbp using
184  *  the structural inode list.
185  *  Returns the matching VxFS inode on success, else a NULL pointer.
186  */
187 struct vxfs_inode_info *
188 vxfs_stiget(struct super_block *sbp, ino_t ino)
189 {
190         return __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
191 }
192
193 /**
194  * vxfs_transmod - mode for a VxFS inode
195  * @vip:        VxFS inode
196  *
197  * Description:
198  *  vxfs_transmod returns a Linux mode_t for a given
199  *  VxFS inode structure.
200  */
201 static __inline__ mode_t
202 vxfs_transmod(struct vxfs_inode_info *vip)
203 {
204         mode_t                  ret = vip->vii_mode & ~VXFS_TYPE_MASK;
205
206         if (VXFS_ISFIFO(vip))
207                 ret |= S_IFIFO;
208         if (VXFS_ISCHR(vip))
209                 ret |= S_IFCHR;
210         if (VXFS_ISDIR(vip))
211                 ret |= S_IFDIR;
212         if (VXFS_ISBLK(vip))
213                 ret |= S_IFBLK;
214         if (VXFS_ISLNK(vip))
215                 ret |= S_IFLNK;
216         if (VXFS_ISREG(vip))
217                 ret |= S_IFREG;
218         if (VXFS_ISSOC(vip))
219                 ret |= S_IFSOCK;
220
221         return (ret);
222 }
223
224 /**
225  * vxfs_iinit- helper to fill inode fields
226  * @ip:         VFS inode
227  * @vip:        VxFS inode
228  *
229  * Description:
230  *  vxfs_instino is a helper function to fill in all relevant
231  *  fields in @ip from @vip.
232  */
233 static void
234 vxfs_iinit(struct inode *ip, struct vxfs_inode_info *vip)
235 {
236
237         ip->i_mode = vxfs_transmod(vip);
238         ip->i_uid = (uid_t)vip->vii_uid;
239         ip->i_gid = (gid_t)vip->vii_gid;
240
241         ip->i_nlink = vip->vii_nlink;
242         ip->i_size = vip->vii_size;
243
244         ip->i_atime.tv_sec = vip->vii_atime;
245         ip->i_ctime.tv_sec = vip->vii_ctime;
246         ip->i_mtime.tv_sec = vip->vii_mtime;
247         ip->i_atime.tv_nsec = 0;
248         ip->i_ctime.tv_nsec = 0;
249         ip->i_mtime.tv_nsec = 0;
250
251         ip->i_blksize = PAGE_SIZE;
252         ip->i_blocks = vip->vii_blocks;
253         ip->i_generation = vip->vii_gen;
254
255         ip->u.generic_ip = (void *)vip;
256         
257 }
258
259 /**
260  * vxfs_get_fake_inode - get fake inode structure
261  * @sbp:                filesystem superblock
262  * @vip:                fspriv inode
263  *
264  * Description:
265  *  vxfs_fake_inode gets a fake inode (not in the inode hash) for a
266  *  superblock, vxfs_inode pair.
267  *  Returns the filled VFS inode.
268  */
269 struct inode *
270 vxfs_get_fake_inode(struct super_block *sbp, struct vxfs_inode_info *vip)
271 {
272         struct inode                    *ip = NULL;
273
274         if ((ip = new_inode(sbp))) {
275                 vxfs_iinit(ip, vip);
276                 ip->i_mapping->a_ops = &vxfs_aops;
277         }
278         return (ip);
279 }
280
281 /**
282  * vxfs_put_fake_inode - free faked inode
283  * *ip:                 VFS inode
284  *
285  * Description:
286  *  vxfs_put_fake_inode frees all data asssociated with @ip.
287  */
288 void
289 vxfs_put_fake_inode(struct inode *ip)
290 {
291         iput(ip);
292 }
293
294 /**
295  * vxfs_read_inode - fill in inode information
296  * @ip:         inode pointer to fill
297  *
298  * Description:
299  *  vxfs_read_inode reads the disk inode for @ip and fills
300  *  in all relevant fields in @ip.
301  */
302 void
303 vxfs_read_inode(struct inode *ip)
304 {
305         struct super_block              *sbp = ip->i_sb;
306         struct vxfs_inode_info          *vip;
307         struct address_space_operations *aops;
308         ino_t                           ino = ip->i_ino;
309
310         if (!(vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist)))
311                 return;
312
313         vxfs_iinit(ip, vip);
314
315         if (VXFS_ISIMMED(vip))
316                 aops = &vxfs_immed_aops;
317         else
318                 aops = &vxfs_aops;
319
320         if (S_ISREG(ip->i_mode)) {
321                 ip->i_fop = &vxfs_file_operations;
322                 ip->i_mapping->a_ops = aops;
323         } else if (S_ISDIR(ip->i_mode)) {
324                 ip->i_op = &vxfs_dir_inode_ops;
325                 ip->i_fop = &vxfs_dir_operations;
326                 ip->i_mapping->a_ops = aops;
327         } else if (S_ISLNK(ip->i_mode)) {
328                 if (!VXFS_ISIMMED(vip)) {
329                         ip->i_op = &page_symlink_inode_operations;
330                         ip->i_mapping->a_ops = &vxfs_aops;
331                 } else
332                         ip->i_op = &vxfs_immed_symlink_iops;
333         } else
334                 init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
335
336         return;
337 }
338
339 /**
340  * vxfs_put_inode - remove inode from main memory
341  * @ip:         inode to discard.
342  *
343  * Description:
344  *  vxfs_put_inode() is called on each iput.  If we are the last
345  *  link in memory, free the fspriv inode area.
346  */
347 void
348 vxfs_put_inode(struct inode *ip)
349 {
350         if (atomic_read(&ip->i_count) == 1)
351                 kmem_cache_free(vxfs_inode_cachep, ip->u.generic_ip);
352 }