blob: fbf55063928fbc40cd32e6073c663c8581aeb000 [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
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000116static int get_name_filldir(void *opaque, const char *name, int length,
117 loff_t offset, u64 inum, unsigned int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118{
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000119 struct get_name_filldir *gnfd = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000121 if (inum != gnfd->inum.no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 return 0;
123
124 memcpy(gnfd->name, name, length);
125 gnfd->name[length] = 0;
126
127 return 1;
128}
129
130static int gfs2_get_name(struct dentry *parent, char *name,
131 struct dentry *child)
132{
133 struct inode *dir = parent->d_inode;
134 struct inode *inode = child->d_inode;
135 struct gfs2_inode *dip, *ip;
136 struct get_name_filldir gnfd;
137 struct gfs2_holder gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400138 u64 offset = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139 int error;
140
141 if (!dir)
142 return -EINVAL;
143
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 if (!S_ISDIR(dir->i_mode) || !inode)
145 return -EINVAL;
146
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400147 dip = GFS2_I(dir);
148 ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149
150 *name = 0;
151 gnfd.inum = ip->i_num;
152 gnfd.name = name;
153
154 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
155 if (error)
156 return error;
157
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500158 error = gfs2_dir_read(dir, &offset, &gnfd, get_name_filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159
160 gfs2_glock_dq_uninit(&gh);
161
162 if (!error && !*name)
163 error = -ENOENT;
164
165 return error;
166}
167
168static struct dentry *gfs2_get_parent(struct dentry *child)
169{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500170 struct qstr dotdot;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 struct inode *inode;
172 struct dentry *dentry;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500174 gfs2_str2qstr(&dotdot, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500175 inode = gfs2_lookupi(child->d_inode, &dotdot, 1, NULL);
176
177 if (!inode)
178 return ERR_PTR(-ENOENT);
Steven Whitehouse7b625362006-09-05 15:56:17 -0400179 /*
180 * In case of an error, @inode carries the error value, and we
181 * have to return that as a(n invalid) pointer to dentry.
182 */
Steven Whitehousec7526662006-03-20 12:30:04 -0500183 if (IS_ERR(inode))
184 return ERR_PTR(PTR_ERR(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 dentry = d_alloc_anon(inode);
187 if (!dentry) {
188 iput(inode);
189 return ERR_PTR(-ENOMEM);
190 }
191
Robert Peterson70831462007-01-11 13:25:00 -0600192 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193 return dentry;
194}
195
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400196static struct dentry *gfs2_get_dentry(struct super_block *sb, void *inum_obj)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500198 struct gfs2_sbd *sdp = sb->s_fs_info;
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400199 struct gfs2_fh_obj *fh_obj = (struct gfs2_fh_obj *)inum_obj;
Al Viro629a21e2006-10-13 22:51:24 -0400200 struct gfs2_inum_host *inum = &fh_obj->this;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201 struct gfs2_holder i_gh, ri_gh, rgd_gh;
202 struct gfs2_rgrpd *rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 struct inode *inode;
204 struct dentry *dentry;
205 int error;
206
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207 /* System files? */
208
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400209 inode = gfs2_ilookup(sb, inum);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 if (inode) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400211 if (GFS2_I(inode)->i_num.no_formal_ino != inum->no_formal_ino) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 iput(inode);
213 return ERR_PTR(-ESTALE);
214 }
215 goto out_inode;
216 }
217
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400218 error = gfs2_glock_nq_num(sdp, inum->no_addr, &gfs2_inode_glops,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000219 LM_ST_SHARED, LM_FLAG_ANY | GL_LOCAL_EXCL,
220 &i_gh);
221 if (error)
222 return ERR_PTR(error);
223
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 error = gfs2_rindex_hold(sdp, &ri_gh);
225 if (error)
226 goto fail;
227
228 error = -EINVAL;
229 rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
230 if (!rgd)
231 goto fail_rindex;
232
233 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
234 if (error)
235 goto fail_rindex;
236
237 error = -ESTALE;
238 if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
239 goto fail_rgd;
240
241 gfs2_glock_dq_uninit(&rgd_gh);
242 gfs2_glock_dq_uninit(&ri_gh);
243
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400244 inode = gfs2_inode_lookup(sb, inum, fh_obj->imode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400245 if (!inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400247 if (IS_ERR(inode)) {
248 error = PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249 goto fail;
250 }
251
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400252 error = gfs2_inode_refresh(GFS2_I(inode));
253 if (error) {
254 iput(inode);
255 goto fail;
256 }
257
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400259 if (GFS2_I(inode)->i_di.di_flags & GFS2_DIF_SYSTEM) {
260 iput(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261 goto fail;
262 }
263
264 gfs2_glock_dq_uninit(&i_gh);
265
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400266out_inode:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267 dentry = d_alloc_anon(inode);
268 if (!dentry) {
269 iput(inode);
270 return ERR_PTR(-ENOMEM);
271 }
272
Robert Peterson70831462007-01-11 13:25:00 -0600273 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274 return dentry;
275
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400276fail_rgd:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 gfs2_glock_dq_uninit(&rgd_gh);
278
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400279fail_rindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 gfs2_glock_dq_uninit(&ri_gh);
281
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400282fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 gfs2_glock_dq_uninit(&i_gh);
284 return ERR_PTR(error);
285}
286
287struct export_operations gfs2_export_ops = {
288 .decode_fh = gfs2_decode_fh,
289 .encode_fh = gfs2_encode_fh,
290 .get_name = gfs2_get_name,
291 .get_parent = gfs2_get_parent,
292 .get_dentry = gfs2_get_dentry,
293};
294