]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/freevxfs/vxfs_inode.c
freevxfs: implement ->alloc_inode and ->destroy_inode
[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 #ifdef DIAGNOSTIC
46 /*
47  * Dump inode contents (partially).
48  */
49 void
50 vxfs_dumpi(struct vxfs_inode_info *vip, ino_t ino)
51 {
52         printk(KERN_DEBUG "\n\n");
53         if (ino)
54                 printk(KERN_DEBUG "dumping vxfs inode %ld\n", ino);
55         else
56                 printk(KERN_DEBUG "dumping unknown vxfs inode\n");
57
58         printk(KERN_DEBUG "---------------------------\n");
59         printk(KERN_DEBUG "mode is %x\n", vip->vii_mode);
60         printk(KERN_DEBUG "nlink:%u, uid:%u, gid:%u\n",
61                         vip->vii_nlink, vip->vii_uid, vip->vii_gid);
62         printk(KERN_DEBUG "size:%Lx, blocks:%u\n",
63                         vip->vii_size, vip->vii_blocks);
64         printk(KERN_DEBUG "orgtype:%u\n", vip->vii_orgtype);
65 }
66 #endif
67
68 /**
69  * vxfs_transmod - mode for a VxFS inode
70  * @vip:        VxFS inode
71  *
72  * Description:
73  *  vxfs_transmod returns a Linux mode_t for a given
74  *  VxFS inode structure.
75  */
76 static __inline__ umode_t
77 vxfs_transmod(struct vxfs_inode_info *vip)
78 {
79         umode_t                 ret = vip->vii_mode & ~VXFS_TYPE_MASK;
80
81         if (VXFS_ISFIFO(vip))
82                 ret |= S_IFIFO;
83         if (VXFS_ISCHR(vip))
84                 ret |= S_IFCHR;
85         if (VXFS_ISDIR(vip))
86                 ret |= S_IFDIR;
87         if (VXFS_ISBLK(vip))
88                 ret |= S_IFBLK;
89         if (VXFS_ISLNK(vip))
90                 ret |= S_IFLNK;
91         if (VXFS_ISREG(vip))
92                 ret |= S_IFREG;
93         if (VXFS_ISSOC(vip))
94                 ret |= S_IFSOCK;
95
96         return (ret);
97 }
98
99 static inline void dip2vip_cpy(struct vxfs_sb_info *sbi,
100                 struct vxfs_inode_info *vip, struct vxfs_dinode *dip)
101 {
102         struct inode *inode = &vip->vfs_inode;
103
104         vip->vii_mode = fs32_to_cpu(sbi, dip->vdi_mode);
105         vip->vii_nlink = fs32_to_cpu(sbi, dip->vdi_nlink);
106         vip->vii_uid = fs32_to_cpu(sbi, dip->vdi_uid);
107         vip->vii_gid = fs32_to_cpu(sbi, dip->vdi_gid);
108         vip->vii_size = fs64_to_cpu(sbi, dip->vdi_size);
109         vip->vii_atime = fs32_to_cpu(sbi, dip->vdi_atime);
110         vip->vii_autime = fs32_to_cpu(sbi, dip->vdi_autime);
111         vip->vii_mtime = fs32_to_cpu(sbi, dip->vdi_mtime);
112         vip->vii_mutime = fs32_to_cpu(sbi, dip->vdi_mutime);
113         vip->vii_ctime = fs32_to_cpu(sbi, dip->vdi_ctime);
114         vip->vii_cutime = fs32_to_cpu(sbi, dip->vdi_cutime);
115         vip->vii_orgtype = dip->vdi_orgtype;
116
117         vip->vii_blocks = fs32_to_cpu(sbi, dip->vdi_blocks);
118         vip->vii_gen = fs32_to_cpu(sbi, dip->vdi_gen);
119
120         if (VXFS_ISDIR(vip))
121                 vip->vii_dotdot = fs32_to_cpu(sbi, dip->vdi_dotdot);
122         else if (!VXFS_ISREG(vip) && !VXFS_ISLNK(vip))
123                 vip->vii_rdev = fs32_to_cpu(sbi, dip->vdi_rdev);
124
125         /* don't endian swap the fields that differ by orgtype */
126         memcpy(&vip->vii_org, &dip->vdi_org, sizeof(vip->vii_org));
127
128         inode->i_mode = vxfs_transmod(vip);
129         i_uid_write(inode, (uid_t)vip->vii_uid);
130         i_gid_write(inode, (gid_t)vip->vii_gid);
131
132         set_nlink(inode, vip->vii_nlink);
133         inode->i_size = vip->vii_size;
134
135         inode->i_atime.tv_sec = vip->vii_atime;
136         inode->i_ctime.tv_sec = vip->vii_ctime;
137         inode->i_mtime.tv_sec = vip->vii_mtime;
138         inode->i_atime.tv_nsec = 0;
139         inode->i_ctime.tv_nsec = 0;
140         inode->i_mtime.tv_nsec = 0;
141
142         inode->i_blocks = vip->vii_blocks;
143         inode->i_generation = vip->vii_gen;
144 }
145
146 /**
147  * vxfs_blkiget - find inode based on extent #
148  * @sbp:        superblock of the filesystem we search in
149  * @extent:     number of the extent to search
150  * @ino:        inode number to search
151  *
152  * Description:
153  *  vxfs_blkiget searches inode @ino in the filesystem described by
154  *  @sbp in the extent @extent.
155  *  Returns the matching VxFS inode on success, else a NULL pointer.
156  *
157  * NOTE:
158  *  While __vxfs_iget uses the pagecache vxfs_blkiget uses the
159  *  buffercache.  This function should not be used outside the
160  *  read_super() method, otherwise the data may be incoherent.
161  */
162 struct inode *
163 vxfs_blkiget(struct super_block *sbp, u_long extent, ino_t ino)
164 {
165         struct buffer_head              *bp;
166         struct inode                    *inode;
167         u_long                          block, offset;
168
169         inode = new_inode(sbp);
170         if (!inode)
171                 return NULL;
172         inode->i_ino = get_next_ino();
173
174         block = extent + ((ino * VXFS_ISIZE) / sbp->s_blocksize);
175         offset = ((ino % (sbp->s_blocksize / VXFS_ISIZE)) * VXFS_ISIZE);
176         bp = sb_bread(sbp, block);
177
178         if (bp && buffer_mapped(bp)) {
179                 struct vxfs_inode_info  *vip = VXFS_INO(inode);
180                 struct vxfs_dinode      *dip;
181
182                 dip = (struct vxfs_dinode *)(bp->b_data + offset);
183                 dip2vip_cpy(VXFS_SBI(sbp), vip, dip);
184                 vip->vfs_inode.i_mapping->a_ops = &vxfs_aops;
185 #ifdef DIAGNOSTIC
186                 vxfs_dumpi(vip, ino);
187 #endif
188                 brelse(bp);
189                 return inode;
190         }
191
192         printk(KERN_WARNING "vxfs: unable to read block %ld\n", block);
193         brelse(bp);
194         iput(inode);
195         return NULL;
196 }
197
198 /**
199  * __vxfs_iget - generic find inode facility
200  * @ilistp:             inode list
201  * @vip:                VxFS inode to fill in
202  * @ino:                inode number
203  *
204  * Description:
205  *  Search the for inode number @ino in the filesystem
206  *  described by @sbp.  Use the specified inode table (@ilistp).
207  *  Returns the matching inode on success, else an error code.
208  */
209 static int
210 __vxfs_iget(struct inode *ilistp, struct vxfs_inode_info *vip, ino_t ino)
211 {
212         struct page                     *pp;
213         u_long                          offset;
214
215         offset = (ino % (PAGE_SIZE / VXFS_ISIZE)) * VXFS_ISIZE;
216         pp = vxfs_get_page(ilistp->i_mapping, ino * VXFS_ISIZE / PAGE_SIZE);
217
218         if (!IS_ERR(pp)) {
219                 struct vxfs_dinode      *dip;
220                 caddr_t                 kaddr = (char *)page_address(pp);
221
222                 dip = (struct vxfs_dinode *)(kaddr + offset);
223                 dip2vip_cpy(VXFS_SBI(ilistp->i_sb), vip, dip);
224                 vip->vfs_inode.i_mapping->a_ops = &vxfs_aops;
225 #ifdef DIAGNOSTIC
226                 vxfs_dumpi(vip, ino);
227 #endif
228                 vxfs_put_page(pp);
229                 return 0;
230         }
231
232         printk(KERN_WARNING "vxfs: error on page 0x%p for inode %ld\n",
233                 pp, (unsigned long)ino);
234         return PTR_ERR(pp);
235 }
236
237 /**
238  * vxfs_stiget - find inode using the structural inode list
239  * @sbp:        VFS superblock
240  * @ino:        inode #
241  *
242  * Description:
243  *  Find inode @ino in the filesystem described by @sbp using
244  *  the structural inode list.
245  *  Returns the matching inode on success, else a NULL pointer.
246  */
247 struct inode *
248 vxfs_stiget(struct super_block *sbp, ino_t ino)
249 {
250         struct inode *inode;
251         int error;
252
253         inode = new_inode(sbp);
254         if (!inode)
255                 return NULL;
256         inode->i_ino = get_next_ino();
257
258         error = __vxfs_iget(VXFS_SBI(sbp)->vsi_stilist, VXFS_INO(inode), ino);
259         if (error) {
260                 iput(inode);
261                 return NULL;
262         }
263
264         return inode;
265 }
266
267 /**
268  * vxfs_iget - get an inode
269  * @sbp:        the superblock to get the inode for
270  * @ino:        the number of the inode to get
271  *
272  * Description:
273  *  vxfs_read_inode creates an inode, reads the disk inode for @ino and fills
274  *  in all relevant fields in the new inode.
275  */
276 struct inode *
277 vxfs_iget(struct super_block *sbp, ino_t ino)
278 {
279         struct vxfs_inode_info          *vip;
280         const struct address_space_operations   *aops;
281         struct inode *ip;
282         int error;
283
284         ip = iget_locked(sbp, ino);
285         if (!ip)
286                 return ERR_PTR(-ENOMEM);
287         if (!(ip->i_state & I_NEW))
288                 return ip;
289
290         vip = VXFS_INO(ip);
291         error = __vxfs_iget(VXFS_SBI(sbp)->vsi_ilist, vip, ino);
292         if (error) {
293                 iget_failed(ip);
294                 return ERR_PTR(error);
295         }
296
297         if (VXFS_ISIMMED(vip))
298                 aops = &vxfs_immed_aops;
299         else
300                 aops = &vxfs_aops;
301
302         if (S_ISREG(ip->i_mode)) {
303                 ip->i_fop = &generic_ro_fops;
304                 ip->i_mapping->a_ops = aops;
305         } else if (S_ISDIR(ip->i_mode)) {
306                 ip->i_op = &vxfs_dir_inode_ops;
307                 ip->i_fop = &vxfs_dir_operations;
308                 ip->i_mapping->a_ops = aops;
309         } else if (S_ISLNK(ip->i_mode)) {
310                 if (!VXFS_ISIMMED(vip)) {
311                         ip->i_op = &page_symlink_inode_operations;
312                         inode_nohighmem(ip);
313                         ip->i_mapping->a_ops = &vxfs_aops;
314                 } else {
315                         ip->i_op = &simple_symlink_inode_operations;
316                         ip->i_link = vip->vii_immed.vi_immed;
317                         nd_terminate_link(ip->i_link, ip->i_size,
318                                           sizeof(vip->vii_immed.vi_immed) - 1);
319                 }
320         } else
321                 init_special_inode(ip, ip->i_mode, old_decode_dev(vip->vii_rdev));
322
323         unlock_new_inode(ip);
324         return ip;
325 }
326
327 /**
328  * vxfs_evict_inode - remove inode from main memory
329  * @ip:         inode to discard.
330  *
331  * Description:
332  *  vxfs_evict_inode() is called on the final iput and frees the private
333  *  inode area.
334  */
335 void
336 vxfs_evict_inode(struct inode *ip)
337 {
338         truncate_inode_pages_final(&ip->i_data);
339         clear_inode(ip);
340 }