2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <asm/semaphore.h>
19 #include "lm_interface.h"
25 #include "ops_export.h"
28 static struct dentry *gfs2_decode_fh(struct super_block *sb,
32 int (*acceptable)(void *context,
33 struct dentry *dentry),
36 struct gfs2_inum this, parent;
38 if (fh_type != fh_len)
41 memset(&parent, 0, sizeof(struct gfs2_inum));
45 parent.no_formal_ino = ((uint64_t)be32_to_cpu(fh[4])) << 32;
46 parent.no_formal_ino |= be32_to_cpu(fh[5]);
47 parent.no_addr = ((uint64_t)be32_to_cpu(fh[6])) << 32;
48 parent.no_addr |= be32_to_cpu(fh[7]);
50 this.no_formal_ino = ((uint64_t)be32_to_cpu(fh[0])) << 32;
51 this.no_formal_ino |= be32_to_cpu(fh[1]);
52 this.no_addr = ((uint64_t)be32_to_cpu(fh[2])) << 32;
53 this.no_addr |= be32_to_cpu(fh[3]);
59 return gfs2_export_ops.find_exported_dentry(sb, &this, &parent,
63 static int gfs2_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
66 struct inode *inode = dentry->d_inode;
67 struct super_block *sb = inode->i_sb;
68 struct gfs2_inode *ip = inode->u.generic_ip;
70 if (*len < 4 || (connectable && *len < 8))
73 fh[0] = ip->i_num.no_formal_ino >> 32;
74 fh[0] = cpu_to_be32(fh[0]);
75 fh[1] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
76 fh[1] = cpu_to_be32(fh[1]);
77 fh[2] = ip->i_num.no_addr >> 32;
78 fh[2] = cpu_to_be32(fh[2]);
79 fh[3] = ip->i_num.no_addr & 0xFFFFFFFF;
80 fh[3] = cpu_to_be32(fh[3]);
83 if (!connectable || inode == sb->s_root->d_inode)
86 spin_lock(&dentry->d_lock);
87 inode = dentry->d_parent->d_inode;
88 ip = inode->u.generic_ip;
90 spin_unlock(&dentry->d_lock);
92 fh[4] = ip->i_num.no_formal_ino >> 32;
93 fh[4] = cpu_to_be32(fh[4]);
94 fh[5] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
95 fh[5] = cpu_to_be32(fh[5]);
96 fh[6] = ip->i_num.no_addr >> 32;
97 fh[6] = cpu_to_be32(fh[6]);
98 fh[7] = ip->i_num.no_addr & 0xFFFFFFFF;
99 fh[7] = cpu_to_be32(fh[7]);
107 struct get_name_filldir {
108 struct gfs2_inum inum;
112 static int get_name_filldir(void *opaque, const char *name, unsigned int length,
113 uint64_t offset, struct gfs2_inum *inum,
116 struct get_name_filldir *gnfd = (struct get_name_filldir *)opaque;
118 if (!gfs2_inum_equal(inum, &gnfd->inum))
121 memcpy(gnfd->name, name, length);
122 gnfd->name[length] = 0;
127 static int gfs2_get_name(struct dentry *parent, char *name,
128 struct dentry *child)
130 struct inode *dir = parent->d_inode;
131 struct inode *inode = child->d_inode;
132 struct gfs2_inode *dip, *ip;
133 struct get_name_filldir gnfd;
134 struct gfs2_holder gh;
141 if (!S_ISDIR(dir->i_mode) || !inode)
144 dip = dir->u.generic_ip;
145 ip = inode->u.generic_ip;
148 gnfd.inum = ip->i_num;
151 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
155 error = gfs2_dir_read(dip, &offset, &gnfd, get_name_filldir);
157 gfs2_glock_dq_uninit(&gh);
159 if (!error && !*name)
165 static struct dentry *gfs2_get_parent(struct dentry *child)
167 struct qstr dotdot = { .name = "..", .len = 2 };
169 struct dentry *dentry;
172 error = gfs2_lookupi(child->d_inode, &dotdot, 1, &inode);
174 return ERR_PTR(error);
176 dentry = d_alloc_anon(inode);
179 return ERR_PTR(-ENOMEM);
185 static struct dentry *gfs2_get_dentry(struct super_block *sb, void *inum_p)
187 struct gfs2_sbd *sdp = sb->s_fs_info;
188 struct gfs2_inum *inum = (struct gfs2_inum *)inum_p;
189 struct gfs2_holder i_gh, ri_gh, rgd_gh;
190 struct gfs2_rgrpd *rgd;
191 struct gfs2_inode *ip;
193 struct dentry *dentry;
198 inode = gfs2_iget(sb, inum);
200 ip = inode->u.generic_ip;
201 if (ip->i_num.no_formal_ino != inum->no_formal_ino) {
203 return ERR_PTR(-ESTALE);
208 error = gfs2_glock_nq_num(sdp,
209 inum->no_addr, &gfs2_inode_glops,
210 LM_ST_SHARED, LM_FLAG_ANY | GL_LOCAL_EXCL,
213 return ERR_PTR(error);
215 error = gfs2_inode_get(i_gh.gh_gl, inum, NO_CREATE, &ip);
221 error = gfs2_rindex_hold(sdp, &ri_gh);
226 rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
230 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
235 if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
238 gfs2_glock_dq_uninit(&rgd_gh);
239 gfs2_glock_dq_uninit(&ri_gh);
241 error = gfs2_inode_get(i_gh.gh_gl, inum, CREATE, &ip);
245 error = gfs2_inode_refresh(ip);
253 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM) {
258 gfs2_glock_dq_uninit(&i_gh);
260 inode = gfs2_ip2v(ip);
264 return ERR_PTR(-ENOMEM);
267 dentry = d_alloc_anon(inode);
270 return ERR_PTR(-ENOMEM);
276 gfs2_glock_dq_uninit(&rgd_gh);
279 gfs2_glock_dq_uninit(&ri_gh);
282 gfs2_glock_dq_uninit(&i_gh);
283 return ERR_PTR(error);
286 struct export_operations gfs2_export_ops = {
287 .decode_fh = gfs2_decode_fh,
288 .encode_fh = gfs2_encode_fh,
289 .get_name = gfs2_get_name,
290 .get_parent = gfs2_get_parent,
291 .get_dentry = gfs2_get_dentry,