blob: 3a9b9b43834444ac59eef1cce15774ffdefe95c3 [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
David Teiglandb3b94fa2006-01-16 16:50:04 +000010#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070014#include <linux/exportfs.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"
Steven Whitehouseb2760582008-10-14 16:05:55 +010025#include "super.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "rgrp.h"
Steven Whitehousec7526662006-03-20 12:30:04 -050027#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010029#define GFS2_SMALL_FH_SIZE 4
Wendy Cheng35dcc522007-06-27 17:07:53 -040030#define GFS2_LARGE_FH_SIZE 8
Steven Whitehouse3ebf4492007-07-10 12:28:27 +010031#define GFS2_OLD_FH_SIZE 10
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010032
Al Virob44b84d2006-10-14 10:46:30 -040033static int gfs2_encode_fh(struct dentry *dentry, __u32 *p, int *len,
David Teiglandb3b94fa2006-01-16 16:50:04 +000034 int connectable)
35{
Al Virob44b84d2006-10-14 10:46:30 -040036 __be32 *fh = (__force __be32 *)p;
David Teiglandb3b94fa2006-01-16 16:50:04 +000037 struct inode *inode = dentry->d_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -050038 struct super_block *sb = inode->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040039 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
Steven Whitehouse5acd3962006-09-04 16:16:45 -040041 if (*len < GFS2_SMALL_FH_SIZE ||
42 (connectable && *len < GFS2_LARGE_FH_SIZE))
David Teiglandb3b94fa2006-01-16 16:50:04 +000043 return 255;
44
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010045 fh[0] = cpu_to_be32(ip->i_no_formal_ino >> 32);
46 fh[1] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
47 fh[2] = cpu_to_be32(ip->i_no_addr >> 32);
48 fh[3] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
Steven Whitehouse5acd3962006-09-04 16:16:45 -040049 *len = GFS2_SMALL_FH_SIZE;
David Teiglandb3b94fa2006-01-16 16:50:04 +000050
Steven Whitehousec9fd4302006-03-01 15:31:02 -050051 if (!connectable || inode == sb->s_root->d_inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +000052 return *len;
53
54 spin_lock(&dentry->d_lock);
55 inode = dentry->d_parent->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040056 ip = GFS2_I(inode);
57 igrab(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000058 spin_unlock(&dentry->d_lock);
59
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010060 fh[4] = cpu_to_be32(ip->i_no_formal_ino >> 32);
61 fh[5] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
62 fh[6] = cpu_to_be32(ip->i_no_addr >> 32);
63 fh[7] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
Steven Whitehouse5acd3962006-09-04 16:16:45 -040064 *len = GFS2_LARGE_FH_SIZE;
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040066 iput(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000067
68 return *len;
69}
70
71struct get_name_filldir {
Al Viro629a21e2006-10-13 22:51:24 -040072 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +000073 char *name;
74};
75
Steven Whitehouse3699e3a2007-01-17 15:09:20 +000076static int get_name_filldir(void *opaque, const char *name, int length,
77 loff_t offset, u64 inum, unsigned int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +000078{
Steven Whitehouse3699e3a2007-01-17 15:09:20 +000079 struct get_name_filldir *gnfd = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +000080
Steven Whitehouse3699e3a2007-01-17 15:09:20 +000081 if (inum != gnfd->inum.no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +000082 return 0;
83
84 memcpy(gnfd->name, name, length);
85 gnfd->name[length] = 0;
86
87 return 1;
88}
89
90static int gfs2_get_name(struct dentry *parent, char *name,
91 struct dentry *child)
92{
93 struct inode *dir = parent->d_inode;
94 struct inode *inode = child->d_inode;
95 struct gfs2_inode *dip, *ip;
96 struct get_name_filldir gnfd;
97 struct gfs2_holder gh;
Steven Whitehousecd915492006-09-04 12:49:07 -040098 u64 offset = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +000099 int error;
100
101 if (!dir)
102 return -EINVAL;
103
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104 if (!S_ISDIR(dir->i_mode) || !inode)
105 return -EINVAL;
106
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400107 dip = GFS2_I(dir);
108 ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
110 *name = 0;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100111 gnfd.inum.no_addr = ip->i_no_addr;
112 gnfd.inum.no_formal_ino = ip->i_no_formal_ino;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 gnfd.name = name;
114
115 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
116 if (error)
117 return error;
118
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500119 error = gfs2_dir_read(dir, &offset, &gnfd, get_name_filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120
121 gfs2_glock_dq_uninit(&gh);
122
123 if (!error && !*name)
124 error = -ENOENT;
125
126 return error;
127}
128
129static struct dentry *gfs2_get_parent(struct dentry *child)
130{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500131 struct qstr dotdot;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132 struct dentry *dentry;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133
Steven Whitehouse7b625362006-09-05 15:56:17 -0400134 /*
Christoph Hellwig44003722008-08-11 15:49:04 +0200135 * XXX(hch): it would be a good idea to keep this around as a
136 * static variable.
Steven Whitehouse7b625362006-09-05 15:56:17 -0400137 */
Christoph Hellwig44003722008-08-11 15:49:04 +0200138 gfs2_str2qstr(&dotdot, "..");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139
Christoph Hellwig44003722008-08-11 15:49:04 +0200140 dentry = d_obtain_alias(gfs2_lookupi(child->d_inode, &dotdot, 1));
141 if (!IS_ERR(dentry))
142 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 return dentry;
144}
145
Christoph Hellwig34c0d152007-10-21 16:42:14 -0700146static struct dentry *gfs2_get_dentry(struct super_block *sb,
147 struct gfs2_inum_host *inum)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500149 struct gfs2_sbd *sdp = sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 struct gfs2_holder i_gh, ri_gh, rgd_gh;
151 struct gfs2_rgrpd *rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 struct inode *inode;
153 struct dentry *dentry;
154 int error;
155
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 /* System files? */
157
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100158 inode = gfs2_ilookup(sb, inum->no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159 if (inode) {
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100160 if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 iput(inode);
162 return ERR_PTR(-ESTALE);
163 }
164 goto out_inode;
165 }
166
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400167 error = gfs2_glock_nq_num(sdp, inum->no_addr, &gfs2_inode_glops,
Steven Whitehouse1c0f4872007-01-22 12:10:39 -0500168 LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 if (error)
170 return ERR_PTR(error);
171
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 error = gfs2_rindex_hold(sdp, &ri_gh);
173 if (error)
174 goto fail;
175
176 error = -EINVAL;
177 rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
178 if (!rgd)
179 goto fail_rindex;
180
181 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
182 if (error)
183 goto fail_rindex;
184
185 error = -ESTALE;
186 if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
187 goto fail_rgd;
188
189 gfs2_glock_dq_uninit(&rgd_gh);
190 gfs2_glock_dq_uninit(&ri_gh);
191
Wendy Cheng35dcc522007-06-27 17:07:53 -0400192 inode = gfs2_inode_lookup(sb, DT_UNKNOWN,
Wendy Chengbb9bcf02007-06-27 17:07:08 -0400193 inum->no_addr,
Benjamin Marzinski7a9f53b2007-09-18 13:33:18 -0500194 0, 0);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400195 if (IS_ERR(inode)) {
196 error = PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 goto fail;
198 }
199
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400200 error = gfs2_inode_refresh(GFS2_I(inode));
201 if (error) {
202 iput(inode);
203 goto fail;
204 }
Wendy Cheng35dcc522007-06-27 17:07:53 -0400205
206 /* Pick up the works we bypass in gfs2_inode_lookup */
207 if (inode->i_state & I_NEW)
208 gfs2_set_iop(inode);
209
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100210 if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
211 iput(inode);
212 goto fail;
213 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400214
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400216 if (GFS2_I(inode)->i_di.di_flags & GFS2_DIF_SYSTEM) {
217 iput(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218 goto fail;
219 }
220
221 gfs2_glock_dq_uninit(&i_gh);
222
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400223out_inode:
Christoph Hellwig44003722008-08-11 15:49:04 +0200224 dentry = d_obtain_alias(inode);
225 if (!IS_ERR(dentry))
226 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227 return dentry;
228
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229fail_rgd:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 gfs2_glock_dq_uninit(&rgd_gh);
231
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400232fail_rindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233 gfs2_glock_dq_uninit(&ri_gh);
234
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400235fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 gfs2_glock_dq_uninit(&i_gh);
237 return ERR_PTR(error);
238}
239
Christoph Hellwig34c0d152007-10-21 16:42:14 -0700240static struct dentry *gfs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
241 int fh_len, int fh_type)
242{
243 struct gfs2_inum_host this;
244 __be32 *fh = (__force __be32 *)fid->raw;
245
246 switch (fh_type) {
247 case GFS2_SMALL_FH_SIZE:
248 case GFS2_LARGE_FH_SIZE:
249 case GFS2_OLD_FH_SIZE:
250 this.no_formal_ino = ((u64)be32_to_cpu(fh[0])) << 32;
251 this.no_formal_ino |= be32_to_cpu(fh[1]);
252 this.no_addr = ((u64)be32_to_cpu(fh[2])) << 32;
253 this.no_addr |= be32_to_cpu(fh[3]);
254 return gfs2_get_dentry(sb, &this);
255 default:
256 return NULL;
257 }
258}
259
260static struct dentry *gfs2_fh_to_parent(struct super_block *sb, struct fid *fid,
261 int fh_len, int fh_type)
262{
263 struct gfs2_inum_host parent;
264 __be32 *fh = (__force __be32 *)fid->raw;
265
266 switch (fh_type) {
267 case GFS2_LARGE_FH_SIZE:
268 case GFS2_OLD_FH_SIZE:
269 parent.no_formal_ino = ((u64)be32_to_cpu(fh[4])) << 32;
270 parent.no_formal_ino |= be32_to_cpu(fh[5]);
271 parent.no_addr = ((u64)be32_to_cpu(fh[6])) << 32;
272 parent.no_addr |= be32_to_cpu(fh[7]);
273 return gfs2_get_dentry(sb, &parent);
274 default:
275 return NULL;
276 }
277}
278
Christoph Hellwig39655162007-10-21 16:42:17 -0700279const struct export_operations gfs2_export_ops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 .encode_fh = gfs2_encode_fh,
Christoph Hellwig34c0d152007-10-21 16:42:14 -0700281 .fh_to_dentry = gfs2_fh_to_dentry,
282 .fh_to_parent = gfs2_fh_to_parent,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 .get_name = gfs2_get_name,
284 .get_parent = gfs2_get_parent,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285};
286