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 <asm/semaphore.h>
22 #include "ops_export.h"
25 static struct dentry *gfs2_decode_fh(struct super_block *sb,
29 int (*acceptable)(void *context,
30 struct dentry *dentry),
33 struct gfs2_inum this, parent;
35 if (fh_type != fh_len)
38 memset(&parent, 0, sizeof(struct gfs2_inum));
42 parent.no_formal_ino = ((uint64_t)be32_to_cpu(fh[4])) << 32;
43 parent.no_formal_ino |= be32_to_cpu(fh[5]);
44 parent.no_addr = ((uint64_t)be32_to_cpu(fh[6])) << 32;
45 parent.no_addr |= be32_to_cpu(fh[7]);
47 this.no_formal_ino = ((uint64_t)be32_to_cpu(fh[0])) << 32;
48 this.no_formal_ino |= be32_to_cpu(fh[1]);
49 this.no_addr = ((uint64_t)be32_to_cpu(fh[2])) << 32;
50 this.no_addr |= be32_to_cpu(fh[3]);
56 return gfs2_export_ops.find_exported_dentry(sb, &this, &parent,
60 static int gfs2_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
63 struct inode *inode = dentry->d_inode;
64 struct gfs2_inode *ip = get_v2ip(inode);
65 struct gfs2_sbd *sdp = ip->i_sbd;
67 if (*len < 4 || (connectable && *len < 8))
70 fh[0] = ip->i_num.no_formal_ino >> 32;
71 fh[0] = cpu_to_be32(fh[0]);
72 fh[1] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
73 fh[1] = cpu_to_be32(fh[1]);
74 fh[2] = ip->i_num.no_addr >> 32;
75 fh[2] = cpu_to_be32(fh[2]);
76 fh[3] = ip->i_num.no_addr & 0xFFFFFFFF;
77 fh[3] = cpu_to_be32(fh[3]);
80 if (!connectable || ip == get_v2ip(sdp->sd_root_dir))
83 spin_lock(&dentry->d_lock);
84 inode = dentry->d_parent->d_inode;
87 spin_unlock(&dentry->d_lock);
89 fh[4] = ip->i_num.no_formal_ino >> 32;
90 fh[4] = cpu_to_be32(fh[4]);
91 fh[5] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
92 fh[5] = cpu_to_be32(fh[5]);
93 fh[6] = ip->i_num.no_addr >> 32;
94 fh[6] = cpu_to_be32(fh[6]);
95 fh[7] = ip->i_num.no_addr & 0xFFFFFFFF;
96 fh[7] = cpu_to_be32(fh[7]);
104 struct get_name_filldir {
105 struct gfs2_inum inum;
109 static int get_name_filldir(void *opaque, const char *name, unsigned int length,
110 uint64_t offset, struct gfs2_inum *inum,
113 struct get_name_filldir *gnfd = (struct get_name_filldir *)opaque;
115 if (!gfs2_inum_equal(inum, &gnfd->inum))
118 memcpy(gnfd->name, name, length);
119 gnfd->name[length] = 0;
124 static int gfs2_get_name(struct dentry *parent, char *name,
125 struct dentry *child)
127 struct inode *dir = parent->d_inode;
128 struct inode *inode = child->d_inode;
129 struct gfs2_inode *dip, *ip;
130 struct get_name_filldir gnfd;
131 struct gfs2_holder gh;
138 if (!S_ISDIR(dir->i_mode) || !inode)
142 ip = get_v2ip(inode);
145 gnfd.inum = ip->i_num;
148 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
152 error = gfs2_dir_read(dip, &offset, &gnfd, get_name_filldir);
154 gfs2_glock_dq_uninit(&gh);
156 if (!error && !*name)
162 static struct dentry *gfs2_get_parent(struct dentry *child)
164 struct gfs2_inode *dip = get_v2ip(child->d_inode);
165 struct qstr dotdot = { .name = "..", .len = 2 };
167 struct dentry *dentry;
170 error = gfs2_lookupi(child->d_inode, &dotdot, 1, &inode);
172 return ERR_PTR(error);
174 dentry = d_alloc_anon(inode);
177 return ERR_PTR(-ENOMEM);
183 static struct dentry *gfs2_get_dentry(struct super_block *sb, void *inum_p)
185 struct gfs2_sbd *sdp = get_v2sdp(sb);
186 struct gfs2_inum *inum = (struct gfs2_inum *)inum_p;
187 struct gfs2_holder i_gh, ri_gh, rgd_gh;
188 struct gfs2_rgrpd *rgd;
189 struct gfs2_inode *ip;
191 struct dentry *dentry;
196 inode = gfs2_iget(sb, inum);
198 ip = get_v2ip(inode);
199 if (ip->i_num.no_formal_ino != inum->no_formal_ino) {
201 return ERR_PTR(-ESTALE);
206 error = gfs2_glock_nq_num(sdp,
207 inum->no_addr, &gfs2_inode_glops,
208 LM_ST_SHARED, LM_FLAG_ANY | GL_LOCAL_EXCL,
211 return ERR_PTR(error);
213 error = gfs2_inode_get(i_gh.gh_gl, inum, NO_CREATE, &ip);
219 error = gfs2_rindex_hold(sdp, &ri_gh);
224 rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
228 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
233 if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
236 gfs2_glock_dq_uninit(&rgd_gh);
237 gfs2_glock_dq_uninit(&ri_gh);
239 error = gfs2_inode_get(i_gh.gh_gl, inum, CREATE, &ip);
243 error = gfs2_inode_refresh(ip);
251 if (ip->i_di.di_flags & GFS2_DIF_SYSTEM) {
256 gfs2_glock_dq_uninit(&i_gh);
258 inode = gfs2_ip2v(ip);
262 return ERR_PTR(-ENOMEM);
265 dentry = d_alloc_anon(inode);
268 return ERR_PTR(-ENOMEM);
274 gfs2_glock_dq_uninit(&rgd_gh);
277 gfs2_glock_dq_uninit(&ri_gh);
280 gfs2_glock_dq_uninit(&i_gh);
281 return ERR_PTR(error);
284 struct export_operations gfs2_export_ops = {
285 .decode_fh = gfs2_decode_fh,
286 .encode_fh = gfs2_encode_fh,
287 .get_name = gfs2_get_name,
288 .get_parent = gfs2_get_parent,
289 .get_dentry = gfs2_get_dentry,