Files
OrangePi_CM5/fs/ceph/export.c
T

250 lines
5.8 KiB
C
Raw Normal View History

#include <linux/ceph/ceph_debug.h>
2009-10-06 11:31:13 -07:00
#include <linux/exportfs.h>
#include <linux/slab.h>
2009-10-06 11:31:13 -07:00
#include <asm/unaligned.h>
#include "super.h"
#include "mds_client.h"
2009-10-06 11:31:13 -07:00
/*
* Basic fh
*/
struct ceph_nfs_fh {
u64 ino;
} __attribute__ ((packed));
/*
2014-03-01 18:05:41 +08:00
* Larger fh that includes parent ino.
2009-10-06 11:31:13 -07:00
*/
struct ceph_nfs_confh {
u64 ino, parent_ino;
} __attribute__ ((packed));
2012-04-05 12:07:36 -07:00
static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
struct inode *parent_inode)
2009-10-06 11:31:13 -07:00
{
int type;
2009-10-06 11:31:13 -07:00
struct ceph_nfs_fh *fh = (void *)rawfh;
struct ceph_nfs_confh *cfh = (void *)rawfh;
int connected_handle_length = sizeof(*cfh)/4;
int handle_length = sizeof(*fh)/4;
2009-10-06 11:31:13 -07:00
/* don't re-export snaps */
if (ceph_snap(inode) != CEPH_NOSNAP)
return -EINVAL;
2014-03-01 18:05:41 +08:00
if (parent_inode && (*max_len < connected_handle_length)) {
*max_len = connected_handle_length;
return FILEID_INVALID;
} else if (*max_len < handle_length) {
*max_len = handle_length;
return FILEID_INVALID;
}
2014-03-01 18:05:41 +08:00
if (parent_inode) {
dout("encode_fh %llx with parent %llx\n",
ceph_ino(inode), ceph_ino(parent_inode));
2012-04-05 12:07:36 -07:00
cfh->ino = ceph_ino(inode);
2014-03-01 18:05:41 +08:00
cfh->parent_ino = ceph_ino(parent_inode);
*max_len = connected_handle_length;
2014-03-01 18:05:41 +08:00
type = FILEID_INO32_GEN_PARENT;
2009-10-06 11:31:13 -07:00
} else {
2014-03-01 18:05:41 +08:00
dout("encode_fh %llx\n", ceph_ino(inode));
fh->ino = ceph_ino(inode);
*max_len = handle_length;
2014-03-01 18:05:41 +08:00
type = FILEID_INO32_GEN;
2009-10-06 11:31:13 -07:00
}
return type;
}
2014-03-01 18:05:41 +08:00
static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)
2009-10-06 11:31:13 -07:00
{
struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
2009-10-06 11:31:13 -07:00
struct inode *inode;
struct dentry *dentry;
struct ceph_vino vino;
int err;
2014-03-01 18:05:41 +08:00
vino.ino = ino;
2009-10-06 11:31:13 -07:00
vino.snap = CEPH_NOSNAP;
inode = ceph_find_inode(sb, vino);
if (!inode) {
struct ceph_mds_request *req;
req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
USE_ANY_MDS);
if (IS_ERR(req))
return ERR_CAST(req);
req->r_ino1 = vino;
req->r_num_caps = 1;
err = ceph_mdsc_do_request(mdsc, NULL, req);
inode = req->r_target_inode;
if (inode)
ihold(inode);
ceph_mdsc_put_request(req);
if (!inode)
return ERR_PTR(-ESTALE);
}
2009-10-06 11:31:13 -07:00
dentry = d_obtain_alias(inode);
2010-04-21 12:31:13 +02:00
if (IS_ERR(dentry)) {
2009-10-06 11:31:13 -07:00
iput(inode);
2010-04-21 12:31:13 +02:00
return dentry;
2009-10-06 11:31:13 -07:00
}
err = ceph_init_dentry(dentry);
if (err < 0) {
2014-03-01 18:05:41 +08:00
dput(dentry);
2009-10-06 11:31:13 -07:00
return ERR_PTR(err);
}
2014-03-01 18:05:41 +08:00
dout("__fh_to_dentry %llx %p dentry %p\n", ino, inode, dentry);
2009-10-06 11:31:13 -07:00
return dentry;
}
/*
2014-03-01 18:05:41 +08:00
* convert regular fh to dentry
2009-10-06 11:31:13 -07:00
*/
2014-03-01 18:05:41 +08:00
static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
struct fid *fid,
int fh_len, int fh_type)
2009-10-06 11:31:13 -07:00
{
2014-03-01 18:05:41 +08:00
struct ceph_nfs_fh *fh = (void *)fid->raw;
2009-10-06 11:31:13 -07:00
2014-03-01 18:05:41 +08:00
if (fh_type != FILEID_INO32_GEN &&
fh_type != FILEID_INO32_GEN_PARENT)
return NULL;
if (fh_len < sizeof(*fh) / 4)
return NULL;
2009-10-06 11:31:13 -07:00
2014-03-01 18:05:41 +08:00
dout("fh_to_dentry %llx\n", fh->ino);
return __fh_to_dentry(sb, fh->ino);
2009-10-06 11:31:13 -07:00
}
2014-03-01 22:11:45 +08:00
static struct dentry *__get_parent(struct super_block *sb,
struct dentry *child, u64 ino)
{
struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
struct ceph_mds_request *req;
struct inode *inode;
struct dentry *dentry;
int err;
req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
USE_ANY_MDS);
if (IS_ERR(req))
return ERR_CAST(req);
if (child) {
req->r_inode = d_inode(child);
ihold(d_inode(child));
2014-03-01 22:11:45 +08:00
} else {
req->r_ino1 = (struct ceph_vino) {
.ino = ino,
.snap = CEPH_NOSNAP,
};
}
req->r_num_caps = 1;
err = ceph_mdsc_do_request(mdsc, NULL, req);
inode = req->r_target_inode;
if (inode)
ihold(inode);
ceph_mdsc_put_request(req);
if (!inode)
return ERR_PTR(-ENOENT);
dentry = d_obtain_alias(inode);
if (IS_ERR(dentry)) {
iput(inode);
return dentry;
}
err = ceph_init_dentry(dentry);
if (err < 0) {
dput(dentry);
return ERR_PTR(err);
}
dout("__get_parent ino %llx parent %p ino %llx.%llx\n",
child ? ceph_ino(d_inode(child)) : ino,
2014-03-01 22:11:45 +08:00
dentry, ceph_vinop(inode));
return dentry;
}
2014-04-09 14:11:31 +08:00
static struct dentry *ceph_get_parent(struct dentry *child)
2014-03-01 22:11:45 +08:00
{
/* don't re-export snaps */
if (ceph_snap(d_inode(child)) != CEPH_NOSNAP)
2014-03-01 22:11:45 +08:00
return ERR_PTR(-EINVAL);
dout("get_parent %p ino %llx.%llx\n",
child, ceph_vinop(d_inode(child)));
2014-03-01 22:11:45 +08:00
return __get_parent(child->d_sb, child, 0);
}
2009-10-06 11:31:13 -07:00
/*
2014-03-01 23:09:05 +08:00
* convert regular fh to parent
2009-10-06 11:31:13 -07:00
*/
static struct dentry *ceph_fh_to_parent(struct super_block *sb,
2014-03-01 23:09:05 +08:00
struct fid *fid,
2009-10-06 11:31:13 -07:00
int fh_len, int fh_type)
{
struct ceph_nfs_confh *cfh = (void *)fid->raw;
struct dentry *dentry;
2014-03-01 23:09:05 +08:00
if (fh_type != FILEID_INO32_GEN_PARENT)
return NULL;
if (fh_len < sizeof(*cfh) / 4)
2014-03-01 23:09:05 +08:00
return NULL;
2009-10-06 11:31:13 -07:00
2014-03-01 23:09:05 +08:00
dout("fh_to_parent %llx\n", cfh->parent_ino);
dentry = __get_parent(sb, NULL, cfh->ino);
if (IS_ERR(dentry) && PTR_ERR(dentry) == -ENOENT)
dentry = __fh_to_dentry(sb, cfh->parent_ino);
2009-10-06 11:31:13 -07:00
return dentry;
}
2014-03-06 16:40:32 +08:00
static int ceph_get_name(struct dentry *parent, char *name,
struct dentry *child)
{
struct ceph_mds_client *mdsc;
struct ceph_mds_request *req;
int err;
mdsc = ceph_inode_to_client(d_inode(child))->mdsc;
2014-03-06 16:40:32 +08:00
req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
USE_ANY_MDS);
if (IS_ERR(req))
return PTR_ERR(req);
mutex_lock(&d_inode(parent)->i_mutex);
2014-03-06 16:40:32 +08:00
req->r_inode = d_inode(child);
ihold(d_inode(child));
req->r_ino2 = ceph_vino(d_inode(parent));
req->r_locked_dir = d_inode(parent);
2014-03-06 16:40:32 +08:00
req->r_num_caps = 2;
err = ceph_mdsc_do_request(mdsc, NULL, req);
mutex_unlock(&d_inode(parent)->i_mutex);
2014-03-06 16:40:32 +08:00
if (!err) {
struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
memcpy(name, rinfo->dname, rinfo->dname_len);
name[rinfo->dname_len] = 0;
dout("get_name %p ino %llx.%llx name %s\n",
child, ceph_vinop(d_inode(child)), name);
2014-03-06 16:40:32 +08:00
} else {
dout("get_name %p ino %llx.%llx err %d\n",
child, ceph_vinop(d_inode(child)), err);
2014-03-06 16:40:32 +08:00
}
ceph_mdsc_put_request(req);
return err;
}
2009-10-06 11:31:13 -07:00
const struct export_operations ceph_export_ops = {
.encode_fh = ceph_encode_fh,
.fh_to_dentry = ceph_fh_to_dentry,
.fh_to_parent = ceph_fh_to_parent,
2014-03-01 22:11:45 +08:00
.get_parent = ceph_get_parent,
2014-03-06 16:40:32 +08:00
.get_name = ceph_get_name,
2009-10-06 11:31:13 -07:00
};