7b5de83405f216f7bd8c02cec30b8619039c8d51
[cascardo/linux.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 #include <linux/namei.h>
39
40 #include "vxfs.h"
41 #include "vxfs_inode.h"
42 #include "vxfs_extern.h"
43
44
45 struct kmem_cache               *vxfs_inode_cachep;
46
47 static struct inode *           vxfs_get_fake_inode(struct super_block *,
48                                         struct vxfs_inode_info *);
49
50 #ifdef DIAGNOSTIC
51 /*
52  * Dump inode contents (partially).
53  */
54 void
55 vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
56 {
57         printk(KERN_DEBUG "\n\n");
58         if (ino)
59                 printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
60         else
61                 printk(KERN_DEBUG "dumping unknown vxfs inode\n");
62
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);
70 }
71 #endif
72
73 static inline void dip2vip_cpy(struct vxfs_sb_info *sbi,
74                 struct vxfs_inode_info *vip, struct vxfs_dinode *dip)
75 {
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;
88
89         vip->vii_blocks = fs32_to_cpu(sbi, dip->vdi_blocks);
90         vip->vii_gen = fs32_to_cpu(sbi, dip->vdi_gen);
91
92         if (VXFS_ISDIR(vip))
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);
96
97         /* don't endian swap the fields that differ by orgtype */
98         memcpy(&vip->vii_org, &dip->vdi_org, sizeof(vip->vii_org));
99 }
100
101 /**
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
106  *
107  * Description:
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.
111  *
112  * NOTE:
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.
116  */
117 struct inode *
118 vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
119 {
120         struct buffer_head              *bp;
121         struct inode                    *inode;
122         u_long                          block, offset;
123
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);
127
128         if (bp && buffer_mapped(bp)) {
129                 struct vxfs_inode_info  *vip;
130                 struct vxfs_dinode      *dip;
131
132                 if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
133                         goto fail;
134                 dip = (struct vxfs_dinode *)(bp->b_data + offset);
135                 dip2vip_cpy(VXFS_SBI(sbp), vip, dip);
136 #ifdef DIAGNOSTIC
137                 vxfs_dumpi(vip, ino);
138 #endif
139                 brelse(bp);
140
141                 inode = vxfs_get_fake_inode(sbp, vip);
142                 if (!inode)
143                         kmem_cache_free(vxfs_inode_cachep, vip);
144                 return inode;
145         }
146
147 fail:
148         printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
149         brelse(bp);
150         return NULL;
151 }
152
153 /**
154  * __vxfs_iget - generic find inode facility
155  * @sbp:                VFS superblock
156  * @ino:                inode number
157  * @ilistp:             inode list
158  *
159  * Description:
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.
163  */
164 static struct vxfs_inode_info *
165 __vxfs_iget(ino_t ino, struct inode *ilistp)
166 {
167         struct page                     *pp;
168         u_long                          offset;
169
170         offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
171         pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
172
173         if (!IS_ERR(pp)) {
174                 struct vxfs_inode_info  *vip;
175                 struct vxfs_dinode      *dip;
176                 caddr_t                 kaddr = (char *)page_address(pp);
177
178                 if (!(vip = kmem_cache_alloc(vxfs_inode_cachep, GFP_KERNEL)))
179                         goto fail;
180                 dip = (struct vxfs_dinode *)(kaddr + offset);
181                 dip2vip_cpy(VXFS_SBI(ilistp->i_sb), vip, dip);
182 #ifdef DIAGNOSTIC
183                 vxfs_dumpi(vip, ino);
184 #endif
185                 vxfs_put_page(pp);
186                 return (vip);
187         }
188
189         printk(KERN_WARNING "vxfs: error on page %p\n", pp);
190         return ERR_CAST(pp);
191
192 fail:
193         printk(KERN_WARNING "vxfs: unable to read inode %ld\n", (unsigned long)ino);
194         vxfs_put_page(pp);
195         return ERR_PTR(-ENOMEM);
196 }
197
198 /**
199  * vxfs_stiget - find inode using the structural inode list
200  * @sbp:        VFS superblock
201  * @ino:        inode #
202  *
203  * Description:
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.
207  */
208 struct inode *
209 vxfs_stiget(struct super_block *sbp, ino_t ino)
210 {
211         struct vxfs_inode_info *vip;
212         struct inode *inode;
213
214         vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_stilist);
215         if (IS_ERR(vip))
216                 return NULL;
217         inode = vxfs_get_fake_inode(sbp, vip);
218         if (!inode)
219                 kmem_cache_free(vxfs_inode_cachep, vip);
220         return inode;
221 }
222
223 /**
224  * vxfs_transmod - mode for a VxFS inode
225  * @vip:        VxFS inode
226  *
227  * Description:
228  *  vxfs_transmod returns a Linux mode_t for a given
229  *  VxFS inode structure.
230  */
231 static __inline__ umode_t
232 vxfs_transmod(struct vxfs_inode_info *vip)
233 {
234         umode_t                 ret = vip->vii_mode & ~VXFS_TYPE_MASK;
235
236         if (VXFS_ISFIFO(vip))
237                 ret |= S_IFIFO;
238         if (VXFS_ISCHR(vip))
239                 ret |= S_IFCHR;
240         if (VXFS_ISDIR(vip))
241                 ret |= S_IFDIR;
242         if (VXFS_ISBLK(vip))
243                 ret |= S_IFBLK;
244         if (VXFS_ISLNK(vip))
245                 ret |= S_IFLNK;
246         if (VXFS_ISREG(vip))
247                 ret |= S_IFREG;
248         if (VXFS_ISSOC(vip))
249                 ret |= S_IFSOCK;
250
251         return (ret);
252 }
253
254 /**
255  * vxfs_iinit- helper to fill inode fields
256  * @ip:         VFS inode
257  * @vip:        VxFS inode
258  *
259  * Description:
260  *  vxfs_instino is a helper function to fill in all relevant
261  *  fields in @ip from @vip.
262  */
263 static void
264 vxfs_iinit(struct inode *ip, struct vxfs_inode_info *vip)
265 {
266
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);
270
271         set_nlink(ip, vip->vii_nlink);
272         ip->i_size = vip->vii_size;
273
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;
280
281         ip->i_blocks = vip->vii_blocks;
282         ip->i_generation = vip->vii_gen;
283
284         ip->i_private = vip;
285         
286 }
287
288 /**
289  * vxfs_get_fake_inode - get fake inode structure
290  * @sbp:                filesystem superblock
291  * @vip:                fspriv inode
292  *
293  * Description:
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.
297  */
298 static struct inode *
299 vxfs_get_fake_inode(struct super_block *sbp, struct vxfs_inode_info *vip)
300 {
301         struct inode                    *ip = NULL;
302
303         if ((ip = new_inode(sbp))) {
304                 ip->i_ino = get_next_ino();
305                 vxfs_iinit(ip, vip);
306                 ip->i_mapping->a_ops = &vxfs_aops;
307         }
308         return (ip);
309 }
310
311 /**
312  * vxfs_iget - get an inode
313  * @sbp:        the superblock to get the inode for
314  * @ino:        the number of the inode to get
315  *
316  * Description:
317  *  vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
318  *  in all relevant fields in the new inode.
319  */
320 struct inode *
321 vxfs_iget(struct super_block *sbp, ino_t ino)
322 {
323         struct vxfs_inode_info          *vip;
324         const struct address_space_operations   *aops;
325         struct inode *ip;
326
327         ip = iget_locked(sbp, ino);
328         if (!ip)
329                 return ERR_PTR(-ENOMEM);
330         if (!(ip->i_state & I_NEW))
331                 return ip;
332
333         vip = __vxfs_iget(ino, VXFS_SBI(sbp)->vsi_ilist);
334         if (IS_ERR(vip)) {
335                 iget_failed(ip);
336                 return ERR_CAST(vip);
337         }
338
339         vxfs_iinit(ip, vip);
340
341         if (VXFS_ISIMMED(vip))
342                 aops = &vxfs_immed_aops;
343         else
344                 aops = &vxfs_aops;
345
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;
356                         inode_nohighmem(ip);
357                         ip->i_mapping->a_ops = &vxfs_aops;
358                 } else {
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);
363                 }
364         } else
365                 init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
366
367         unlock_new_inode(ip);
368         return ip;
369 }
370
371 static void vxfs_i_callback(struct rcu_head *head)
372 {
373         struct inode *inode = container_of(head, struct inode, i_rcu);
374         kmem_cache_free(vxfs_inode_cachep, inode->i_private);
375 }
376
377 /**
378  * vxfs_evict_inode - remove inode from main memory
379  * @ip:         inode to discard.
380  *
381  * Description:
382  *  vxfs_evict_inode() is called on the final iput and frees the private
383  *  inode area.
384  */
385 void
386 vxfs_evict_inode(struct inode *ip)
387 {
388         truncate_inode_pages_final(&ip->i_data);
389         clear_inode(ip);
390         call_rcu(&ip->i_rcu, vxfs_i_callback);
391 }