4. 构造新Xlator
这里我们通过构造一个称为NULL的Xlator来梳理构造新xlator的基本方法。NULL Xlator本身不实现具体的功能,仅作为类似Proxy的中转,用于演示构造xlator的结构和方法。NULL Xlator实现包括四个文件,null.h, null.c, null_fops.h, null_fops.c,其中null_fops.h, null_fops.c与defaults.h, defaults.c完全相同。Null.h内容如下:
#ifndef __NULL_H__
#define __NULL_H__
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "mem-types.h"
typedef struct {
xlator_t *target;
} null_private_t;
enum gf_null_mem_types_ {
gf_null_mt_priv_t = gf_common_mt_end + 1,
gf_null_mt_end
};
#endif /* __NULL_H__ */
其中,自定义了私有数据结构体null_private_t和内部数据类型。Null.c内容如下:
#include <ctype.h>
#include <sys/uio.h>
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "glusterfs.h"
#include "call-stub.h"
#include "defaults.h"
#include "logging.h"
#include "xlator.h"
#include "null.h"
#include "null_fops.h"
int32_t
init (xlator_t *this)
{
xlator_t *tgt_xl = NULL;
null_private_t *priv = NULL;
if (!this->children || this->children->next) {
gf_log (this->name, GF_LOG_ERROR,
"FATAL: null should have exactly one child");
return -1;
}
priv = GF_CALLOC (1, sizeof (null_private_t), gf_null_mt_priv_t);
if (!priv)
return -1;
/* Init priv here */
priv->target = tgt_xl;
gf_log (this->name, GF_LOG_DEBUG, "null xlator loaded");
return 0;
}
void
fini (xlator_t *this)
{
null_private_t *priv = this->private;
if (!priv)
return;
this->private = NULL;
GF_FREE (priv);
return;
}
struct xlator_fops fops = {
.lookup = null_lookup,
.stat = null_stat,
.fstat = null_fstat,
.truncate = null_truncate,
.ftruncate = null_ftruncate,
.access = null_access,
.readlink = null_readlink,
.mknod = null_mknod,
.mkdir = null_mkdir,
.unlink = null_unlink,
.rmdir = null_rmdir,
.symlink = null_symlink,
.rename = null_rename,
.link = null_link,
.create = null_create,
.open = null_open,
.readv = null_readv,
.writev = null_writev,
.flush = null_flush,
.fsync = null_fsync,
.opendir = null_opendir,
.readdir = null_readdir,
.readdirp = null_readdirp,
.fsyncdir = null_fsyncdir,
.statfs = null_statfs,
.setxattr = null_setxattr,
.getxattr = null_getxattr,
.fsetxattr = null_fsetxattr,
.fgetxattr = null_fgetxattr,
.removexattr = null_removexattr,
.lk = null_lk,
.inodelk = null_inodelk,
.finodelk = null_finodelk,
.entrylk = null_entrylk,
.fentrylk = null_fentrylk,
.rchecksum = null_rchecksum,
.xattrop = null_xattrop,
.fxattrop = null_fxattrop,
.setattr = null_setattr,
.fsetattr = null_fsetattr,
.getspec = null_getspec,
};
struct xlator_cbks cbks = {
.forget = null_forget,
.release = null_release,
.releasedir = null_releasedir,
};
struct volume_options options[] = {
{ .key = {NULL} },
};
这其中主要实现了上面提到的init和fini函数,fops调用函数指针和cbks回调函数指针,以及卷参数选项options。一个新xlator的代码基本框架就是如此,这里因为没有实现具体功能,各种结构体、变量和函数实现都相对非常简单。如果要实现特定功能的xlator,可以以此为模块进行扩展。Glusterfs源码中xlator都是很好的例子,但有些很复杂,不适合初学者,可以先从简单的rot-13, read-only, bypass, negative-lookup等xlator开始研究,然后构造出自己所需功能的xlator。
