blob: 6ea979c74b6b9334824fd58e73fc8dc14b5c2401 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050016#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020017#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018
19#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "dir.h"
22#include "glock.h"
23#include "glops.h"
24#include "inode.h"
Robert Peterson70831462007-01-11 13:25:00 -060025#include "ops_dentry.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "ops_export.h"
27#include "rgrp.h"
Steven Whitehousec7526662006-03-20 12:30:04 -050028#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
30static struct dentry *gfs2_decode_fh(struct super_block *sb,
Al Virob44b84d2006-10-14 10:46:30 -040031 __u32 *p,
David Teiglandb3b94fa2006-01-16 16:50:04 +000032 int fh_len,
33 int fh_type,
34 int (*acceptable)(void *context,
35 struct dentry *dentry),
36 void *context)
37{
Al Virob44b84d2006-10-14 10:46:30 -040038 __be32 *fh = (__force __be32 *)p;
Wendy Cheng2eb168c2006-07-11 13:28:53 -040039 struct gfs2_fh_obj fh_obj;
Al Viro629a21e2006-10-13 22:51:24 -040040 struct gfs2_inum_host *this, parent;
David Teiglandb3b94fa2006-01-16 16:50:04 +000041
David Teiglandb3b94fa2006-01-16 16:50:04 +000042 if (fh_type != fh_len)
43 return NULL;
44
Wendy Cheng2eb168c2006-07-11 13:28:53 -040045 this = &fh_obj.this;
46 fh_obj.imode = DT_UNKNOWN;
David Teiglandb3b94fa2006-01-16 16:50:04 +000047 memset(&parent, 0, sizeof(struct gfs2_inum));
48
49 switch (fh_type) {
Steven Whitehouse5acd3962006-09-04 16:16:45 -040050 case GFS2_LARGE_FH_SIZE:
Steven Whitehousecd915492006-09-04 12:49:07 -040051 parent.no_formal_ino = ((u64)be32_to_cpu(fh[4])) << 32;
David Teiglandb3b94fa2006-01-16 16:50:04 +000052 parent.no_formal_ino |= be32_to_cpu(fh[5]);
Steven Whitehousecd915492006-09-04 12:49:07 -040053 parent.no_addr = ((u64)be32_to_cpu(fh[6])) << 32;
David Teiglandb3b94fa2006-01-16 16:50:04 +000054 parent.no_addr |= be32_to_cpu(fh[7]);
Wendy Cheng2eb168c2006-07-11 13:28:53 -040055 fh_obj.imode = be32_to_cpu(fh[8]);
Steven Whitehouse5acd3962006-09-04 16:16:45 -040056 case GFS2_SMALL_FH_SIZE:
Steven Whitehousecd915492006-09-04 12:49:07 -040057 this->no_formal_ino = ((u64)be32_to_cpu(fh[0])) << 32;
Wendy Cheng2eb168c2006-07-11 13:28:53 -040058 this->no_formal_ino |= be32_to_cpu(fh[1]);
Steven Whitehousecd915492006-09-04 12:49:07 -040059 this->no_addr = ((u64)be32_to_cpu(fh[2])) << 32;
Wendy Cheng2eb168c2006-07-11 13:28:53 -040060 this->no_addr |= be32_to_cpu(fh[3]);
David Teiglandb3b94fa2006-01-16 16:50:04 +000061 break;
62 default:
63 return NULL;
64 }
65
Wendy Cheng2eb168c2006-07-11 13:28:53 -040066 return gfs2_export_ops.find_exported_dentry(sb, &fh_obj, &parent,
David Teiglandb3b94fa2006-01-16 16:50:04 +000067 acceptable, context);
68}
69
Al Virob44b84d2006-10-14 10:46:30 -040070static int gfs2_encode_fh(struct dentry *dentry, __u32 *p, int *len,
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 int connectable)
72{
Al Virob44b84d2006-10-14 10:46:30 -040073 __be32 *fh = (__force __be32 *)p;
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 struct inode *inode = dentry->d_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -050075 struct super_block *sb = inode->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040076 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000077
Steven Whitehouse5acd3962006-09-04 16:16:45 -040078 if (*len < GFS2_SMALL_FH_SIZE ||
79 (connectable && *len < GFS2_LARGE_FH_SIZE))
David Teiglandb3b94fa2006-01-16 16:50:04 +000080 return 255;
81
Al Virob44b84d2006-10-14 10:46:30 -040082 fh[0] = cpu_to_be32(ip->i_num.no_formal_ino >> 32);
83 fh[1] = cpu_to_be32(ip->i_num.no_formal_ino & 0xFFFFFFFF);
84 fh[2] = cpu_to_be32(ip->i_num.no_addr >> 32);
85 fh[3] = cpu_to_be32(ip->i_num.no_addr & 0xFFFFFFFF);
Steven Whitehouse5acd3962006-09-04 16:16:45 -040086 *len = GFS2_SMALL_FH_SIZE;
David Teiglandb3b94fa2006-01-16 16:50:04 +000087
Steven Whitehousec9fd4302006-03-01 15:31:02 -050088 if (!connectable || inode == sb->s_root->d_inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +000089 return *len;
90
91 spin_lock(&dentry->d_lock);
92 inode = dentry->d_parent->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040093 ip = GFS2_I(inode);
94 igrab(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000095 spin_unlock(&dentry->d_lock);
96
Al Virob44b84d2006-10-14 10:46:30 -040097 fh[4] = cpu_to_be32(ip->i_num.no_formal_ino >> 32);
98 fh[5] = cpu_to_be32(ip->i_num.no_formal_ino & 0xFFFFFFFF);
99 fh[6] = cpu_to_be32(ip->i_num.no_addr >> 32);
100 fh[7] = cpu_to_be32(ip->i_num.no_addr & 0xFFFFFFFF);
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400101
102 fh[8] = cpu_to_be32(inode->i_mode);
103 fh[9] = 0; /* pad to double word */
Steven Whitehouse5acd3962006-09-04 16:16:45 -0400104 *len = GFS2_LARGE_FH_SIZE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400106 iput(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107
108 return *len;
109}
110
111struct get_name_filldir {
Al Viro629a21e2006-10-13 22:51:24 -0400112 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 char *name;
114};
115
116static int get_name_filldir(void *opaque, const char *name, unsigned int length,
Al Viro629a21e2006-10-13 22:51:24 -0400117 u64 offset, struct gfs2_inum_host *inum,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118 unsigned int type)
119{
120 struct get_name_filldir *gnfd = (struct get_name_filldir *)opaque;
121
122 if (!gfs2_inum_equal(inum, &gnfd->inum))
123 return 0;
124
125 memcpy(gnfd->name, name, length);
126 gnfd->name[length] = 0;
127
128 return 1;
129}
130
131static int gfs2_get_name(struct dentry *parent, char *name,
132 struct dentry *child)
133{
134 struct inode *dir = parent->d_inode;
135 struct inode *inode = child->d_inode;
136 struct gfs2_inode *dip, *ip;
137 struct get_name_filldir gnfd;
138 struct gfs2_holder gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400139 u64 offset = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 int error;
141
142 if (!dir)
143 return -EINVAL;
144
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 if (!S_ISDIR(dir->i_mode) || !inode)
146 return -EINVAL;
147
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400148 dip = GFS2_I(dir);
149 ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150
151 *name = 0;
152 gnfd.inum = ip->i_num;
153 gnfd.name = name;
154
155 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
156 if (error)
157 return error;
158
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500159 error = gfs2_dir_read(dir, &offset, &gnfd, get_name_filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160
161 gfs2_glock_dq_uninit(&gh);
162
163 if (!error && !*name)
164 error = -ENOENT;
165
166 return error;
167}
168
169static struct dentry *gfs2_get_parent(struct dentry *child)
170{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500171 struct qstr dotdot;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 struct inode *inode;
173 struct dentry *dentry;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000174
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500175 gfs2_str2qstr(&dotdot, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500176 inode = gfs2_lookupi(child->d_inode, &dotdot, 1, NULL);
177
178 if (!inode)
179 return ERR_PTR(-ENOENT);
Steven Whitehouse7b625362006-09-05 15:56:17 -0400180 /*
181 * In case of an error, @inode carries the error value, and we
182 * have to return that as a(n invalid) pointer to dentry.
183 */
Steven Whitehousec7526662006-03-20 12:30:04 -0500184 if (IS_ERR(inode))
185 return ERR_PTR(PTR_ERR(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186
David Teiglandb3b94fa2006-01-16 16:50:04 +0000187 dentry = d_alloc_anon(inode);
188 if (!dentry) {
189 iput(inode);
190 return ERR_PTR(-ENOMEM);
191 }
192
Robert Peterson70831462007-01-11 13:25:00 -0600193 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 return dentry;
195}
196
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400197static struct dentry *gfs2_get_dentry(struct super_block *sb, void *inum_obj)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500199 struct gfs2_sbd *sdp = sb->s_fs_info;
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400200 struct gfs2_fh_obj *fh_obj = (struct gfs2_fh_obj *)inum_obj;
Al Viro629a21e2006-10-13 22:51:24 -0400201 struct gfs2_inum_host *inum = &fh_obj->this;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202 struct gfs2_holder i_gh, ri_gh, rgd_gh;
203 struct gfs2_rgrpd *rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 struct inode *inode;
205 struct dentry *dentry;
206 int error;
207
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 /* System files? */
209
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400210 inode = gfs2_ilookup(sb, inum);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 if (inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400212 if (GFS2_I(inode)->i_num.no_formal_ino != inum->no_formal_ino) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 iput(inode);
214 return ERR_PTR(-ESTALE);
215 }
216 goto out_inode;
217 }
218
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400219 error = gfs2_glock_nq_num(sdp, inum->no_addr, &gfs2_inode_glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220 LM_ST_SHARED, LM_FLAG_ANY | GL_LOCAL_EXCL,
221 &i_gh);
222 if (error)
223 return ERR_PTR(error);
224
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225 error = gfs2_rindex_hold(sdp, &ri_gh);
226 if (error)
227 goto fail;
228
229 error = -EINVAL;
230 rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
231 if (!rgd)
232 goto fail_rindex;
233
234 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
235 if (error)
236 goto fail_rindex;
237
238 error = -ESTALE;
239 if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
240 goto fail_rgd;
241
242 gfs2_glock_dq_uninit(&rgd_gh);
243 gfs2_glock_dq_uninit(&ri_gh);
244
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400245 inode = gfs2_inode_lookup(sb, inum, fh_obj->imode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400246 if (!inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400248 if (IS_ERR(inode)) {
249 error = PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 goto fail;
251 }
252
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400253 error = gfs2_inode_refresh(GFS2_I(inode));
254 if (error) {
255 iput(inode);
256 goto fail;
257 }
258
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400260 if (GFS2_I(inode)->i_di.di_flags & GFS2_DIF_SYSTEM) {
261 iput(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262 goto fail;
263 }
264
265 gfs2_glock_dq_uninit(&i_gh);
266
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400267out_inode:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 dentry = d_alloc_anon(inode);
269 if (!dentry) {
270 iput(inode);
271 return ERR_PTR(-ENOMEM);
272 }
273
Robert Peterson70831462007-01-11 13:25:00 -0600274 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275 return dentry;
276
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400277fail_rgd:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 gfs2_glock_dq_uninit(&rgd_gh);
279
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400280fail_rindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 gfs2_glock_dq_uninit(&ri_gh);
282
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400283fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284 gfs2_glock_dq_uninit(&i_gh);
285 return ERR_PTR(error);
286}
287
288struct export_operations gfs2_export_ops = {
289 .decode_fh = gfs2_decode_fh,
290 .encode_fh = gfs2_encode_fh,
291 .get_name = gfs2_get_name,
292 .get_parent = gfs2_get_parent,
293 .get_dentry = gfs2_get_dentry,
294};
295