blob: d07230ee5fc00b8b23324408c8333c67382a3fa7 [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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050014#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050015#include <linux/crc32.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020016#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "dir.h"
21#include "glock.h"
22#include "glops.h"
23#include "inode.h"
Robert Peterson70831462007-01-11 13:25:00 -060024#include "ops_dentry.h"
Steven Whitehousebb8d8a62007-06-01 14:11:58 +010025#include "ops_fstype.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
30#define GFS2_LARGE_FH_SIZE 10
31
32struct gfs2_fh_obj {
33 struct gfs2_inum_host this;
34 u32 imode;
35};
36
David Teiglandb3b94fa2006-01-16 16:50:04 +000037static struct dentry *gfs2_decode_fh(struct super_block *sb,
Al Virob44b84d2006-10-14 10:46:30 -040038 __u32 *p,
David Teiglandb3b94fa2006-01-16 16:50:04 +000039 int fh_len,
40 int fh_type,
41 int (*acceptable)(void *context,
42 struct dentry *dentry),
43 void *context)
44{
Al Virob44b84d2006-10-14 10:46:30 -040045 __be32 *fh = (__force __be32 *)p;
Wendy Cheng2eb168c2006-07-11 13:28:53 -040046 struct gfs2_fh_obj fh_obj;
Al Viro629a21e2006-10-13 22:51:24 -040047 struct gfs2_inum_host *this, parent;
David Teiglandb3b94fa2006-01-16 16:50:04 +000048
Wendy Cheng2eb168c2006-07-11 13:28:53 -040049 this = &fh_obj.this;
50 fh_obj.imode = DT_UNKNOWN;
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 memset(&parent, 0, sizeof(struct gfs2_inum));
52
Wendy Chenga7d2b2b2007-02-23 00:21:17 -050053 switch (fh_len) {
Steven Whitehouse5acd3962006-09-04 16:16:45 -040054 case GFS2_LARGE_FH_SIZE:
Steven Whitehousecd915492006-09-04 12:49:07 -040055 parent.no_formal_ino = ((u64)be32_to_cpu(fh[4])) << 32;
David Teiglandb3b94fa2006-01-16 16:50:04 +000056 parent.no_formal_ino |= be32_to_cpu(fh[5]);
Steven Whitehousecd915492006-09-04 12:49:07 -040057 parent.no_addr = ((u64)be32_to_cpu(fh[6])) << 32;
David Teiglandb3b94fa2006-01-16 16:50:04 +000058 parent.no_addr |= be32_to_cpu(fh[7]);
Wendy Cheng2eb168c2006-07-11 13:28:53 -040059 fh_obj.imode = be32_to_cpu(fh[8]);
Steven Whitehouse5acd3962006-09-04 16:16:45 -040060 case GFS2_SMALL_FH_SIZE:
Steven Whitehousecd915492006-09-04 12:49:07 -040061 this->no_formal_ino = ((u64)be32_to_cpu(fh[0])) << 32;
Wendy Cheng2eb168c2006-07-11 13:28:53 -040062 this->no_formal_ino |= be32_to_cpu(fh[1]);
Steven Whitehousecd915492006-09-04 12:49:07 -040063 this->no_addr = ((u64)be32_to_cpu(fh[2])) << 32;
Wendy Cheng2eb168c2006-07-11 13:28:53 -040064 this->no_addr |= be32_to_cpu(fh[3]);
David Teiglandb3b94fa2006-01-16 16:50:04 +000065 break;
66 default:
67 return NULL;
68 }
69
Wendy Cheng2eb168c2006-07-11 13:28:53 -040070 return gfs2_export_ops.find_exported_dentry(sb, &fh_obj, &parent,
David Teiglandb3b94fa2006-01-16 16:50:04 +000071 acceptable, context);
72}
73
Al Virob44b84d2006-10-14 10:46:30 -040074static int gfs2_encode_fh(struct dentry *dentry, __u32 *p, int *len,
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 int connectable)
76{
Al Virob44b84d2006-10-14 10:46:30 -040077 __be32 *fh = (__force __be32 *)p;
David Teiglandb3b94fa2006-01-16 16:50:04 +000078 struct inode *inode = dentry->d_inode;
Steven Whitehousec9fd4302006-03-01 15:31:02 -050079 struct super_block *sb = inode->i_sb;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040080 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000081
Steven Whitehouse5acd3962006-09-04 16:16:45 -040082 if (*len < GFS2_SMALL_FH_SIZE ||
83 (connectable && *len < GFS2_LARGE_FH_SIZE))
David Teiglandb3b94fa2006-01-16 16:50:04 +000084 return 255;
85
Steven Whitehousedbb7cae2007-05-15 15:37:50 +010086 fh[0] = cpu_to_be32(ip->i_no_formal_ino >> 32);
87 fh[1] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
88 fh[2] = cpu_to_be32(ip->i_no_addr >> 32);
89 fh[3] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
Steven Whitehouse5acd3962006-09-04 16:16:45 -040090 *len = GFS2_SMALL_FH_SIZE;
David Teiglandb3b94fa2006-01-16 16:50:04 +000091
Steven Whitehousec9fd4302006-03-01 15:31:02 -050092 if (!connectable || inode == sb->s_root->d_inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +000093 return *len;
94
95 spin_lock(&dentry->d_lock);
96 inode = dentry->d_parent->d_inode;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040097 ip = GFS2_I(inode);
98 igrab(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +000099 spin_unlock(&dentry->d_lock);
100
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100101 fh[4] = cpu_to_be32(ip->i_no_formal_ino >> 32);
102 fh[5] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
103 fh[6] = cpu_to_be32(ip->i_no_addr >> 32);
104 fh[7] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400105
106 fh[8] = cpu_to_be32(inode->i_mode);
107 fh[9] = 0; /* pad to double word */
Steven Whitehouse5acd3962006-09-04 16:16:45 -0400108 *len = GFS2_LARGE_FH_SIZE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400110 iput(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111
112 return *len;
113}
114
115struct get_name_filldir {
Al Viro629a21e2006-10-13 22:51:24 -0400116 struct gfs2_inum_host inum;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117 char *name;
118};
119
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000120static int get_name_filldir(void *opaque, const char *name, int length,
121 loff_t offset, u64 inum, unsigned int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122{
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000123 struct get_name_filldir *gnfd = opaque;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124
Steven Whitehouse3699e3a2007-01-17 15:09:20 +0000125 if (inum != gnfd->inum.no_addr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 return 0;
127
128 memcpy(gnfd->name, name, length);
129 gnfd->name[length] = 0;
130
131 return 1;
132}
133
134static int gfs2_get_name(struct dentry *parent, char *name,
135 struct dentry *child)
136{
137 struct inode *dir = parent->d_inode;
138 struct inode *inode = child->d_inode;
139 struct gfs2_inode *dip, *ip;
140 struct get_name_filldir gnfd;
141 struct gfs2_holder gh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400142 u64 offset = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 int error;
144
145 if (!dir)
146 return -EINVAL;
147
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 if (!S_ISDIR(dir->i_mode) || !inode)
149 return -EINVAL;
150
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400151 dip = GFS2_I(dir);
152 ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153
154 *name = 0;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100155 gnfd.inum.no_addr = ip->i_no_addr;
156 gnfd.inum.no_formal_ino = ip->i_no_formal_ino;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 gnfd.name = name;
158
159 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
160 if (error)
161 return error;
162
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500163 error = gfs2_dir_read(dir, &offset, &gnfd, get_name_filldir);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164
165 gfs2_glock_dq_uninit(&gh);
166
167 if (!error && !*name)
168 error = -ENOENT;
169
170 return error;
171}
172
173static struct dentry *gfs2_get_parent(struct dentry *child)
174{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500175 struct qstr dotdot;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 struct inode *inode;
177 struct dentry *dentry;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000178
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500179 gfs2_str2qstr(&dotdot, "..");
Steven Whitehousec7526662006-03-20 12:30:04 -0500180 inode = gfs2_lookupi(child->d_inode, &dotdot, 1, NULL);
181
182 if (!inode)
183 return ERR_PTR(-ENOENT);
Steven Whitehouse7b625362006-09-05 15:56:17 -0400184 /*
185 * In case of an error, @inode carries the error value, and we
186 * have to return that as a(n invalid) pointer to dentry.
187 */
Steven Whitehousec7526662006-03-20 12:30:04 -0500188 if (IS_ERR(inode))
189 return ERR_PTR(PTR_ERR(inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 dentry = d_alloc_anon(inode);
192 if (!dentry) {
193 iput(inode);
194 return ERR_PTR(-ENOMEM);
195 }
196
Robert Peterson70831462007-01-11 13:25:00 -0600197 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198 return dentry;
199}
200
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400201static struct dentry *gfs2_get_dentry(struct super_block *sb, void *inum_obj)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500203 struct gfs2_sbd *sdp = sb->s_fs_info;
Wendy Cheng2eb168c2006-07-11 13:28:53 -0400204 struct gfs2_fh_obj *fh_obj = (struct gfs2_fh_obj *)inum_obj;
Al Viro629a21e2006-10-13 22:51:24 -0400205 struct gfs2_inum_host *inum = &fh_obj->this;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 struct gfs2_holder i_gh, ri_gh, rgd_gh;
207 struct gfs2_rgrpd *rgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 struct inode *inode;
209 struct dentry *dentry;
210 int error;
211
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 /* System files? */
213
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100214 inode = gfs2_ilookup(sb, inum->no_addr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215 if (inode) {
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100216 if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 iput(inode);
218 return ERR_PTR(-ESTALE);
219 }
220 goto out_inode;
221 }
222
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400223 error = gfs2_glock_nq_num(sdp, inum->no_addr, &gfs2_inode_glops,
Steven Whitehouse1c0f4872007-01-22 12:10:39 -0500224 LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225 if (error)
226 return ERR_PTR(error);
227
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228 error = gfs2_rindex_hold(sdp, &ri_gh);
229 if (error)
230 goto fail;
231
232 error = -EINVAL;
233 rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
234 if (!rgd)
235 goto fail_rindex;
236
237 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
238 if (error)
239 goto fail_rindex;
240
241 error = -ESTALE;
242 if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
243 goto fail_rgd;
244
245 gfs2_glock_dq_uninit(&rgd_gh);
246 gfs2_glock_dq_uninit(&ri_gh);
247
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100248 inode = gfs2_inode_lookup(sb, inum->no_addr, fh_obj->imode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400249 if (!inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 goto fail;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400251 if (IS_ERR(inode)) {
252 error = PTR_ERR(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253 goto fail;
254 }
255
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400256 error = gfs2_inode_refresh(GFS2_I(inode));
257 if (error) {
258 iput(inode);
259 goto fail;
260 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100261 if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
262 iput(inode);
263 goto fail;
264 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400265
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400267 if (GFS2_I(inode)->i_di.di_flags & GFS2_DIF_SYSTEM) {
268 iput(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269 goto fail;
270 }
271
272 gfs2_glock_dq_uninit(&i_gh);
273
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400274out_inode:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275 dentry = d_alloc_anon(inode);
276 if (!dentry) {
277 iput(inode);
278 return ERR_PTR(-ENOMEM);
279 }
280
Robert Peterson70831462007-01-11 13:25:00 -0600281 dentry->d_op = &gfs2_dops;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 return dentry;
283
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400284fail_rgd:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 gfs2_glock_dq_uninit(&rgd_gh);
286
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400287fail_rindex:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288 gfs2_glock_dq_uninit(&ri_gh);
289
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400290fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291 gfs2_glock_dq_uninit(&i_gh);
292 return ERR_PTR(error);
293}
294
295struct export_operations gfs2_export_ops = {
296 .decode_fh = gfs2_decode_fh,
297 .encode_fh = gfs2_encode_fh,
298 .get_name = gfs2_get_name,
299 .get_parent = gfs2_get_parent,
300 .get_dentry = gfs2_get_dentry,
301};
302