diff --git a/configure.ac b/configure.ac index 0eebcda..79e6138 100644 --- a/configure.ac +++ b/configure.ac @@ -186,6 +186,9 @@ AC_CONFIG_FILES([Makefile xlators/mgmt/Makefile xlators/mgmt/glusterd/Makefile xlators/mgmt/glusterd/src/Makefile + xlators/experimental/Makefile + xlators/experimental/create-restrictions/Makefile + xlators/experimental/create-restrictions/src/Makefile cli/Makefile cli/src/Makefile doc/Makefile diff --git a/xlators/Makefile.am b/xlators/Makefile.am index 2b66201..6b40fa4 100644 --- a/xlators/Makefile.am +++ b/xlators/Makefile.am @@ -1,5 +1,5 @@ SUBDIRS = cluster storage protocol performance debug features encryption mount nfs mgmt system \ - playground meta + playground meta experimental EXTRA_DIST = xlator.sym diff --git a/xlators/experimental/Makefile.am b/xlators/experimental/Makefile.am new file mode 100644 index 0000000..8a11eb4 --- /dev/null +++ b/xlators/experimental/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = create-restrictions + +CLEANFILES = diff --git a/xlators/experimental/create-restrictions/Makefile.am b/xlators/experimental/create-restrictions/Makefile.am new file mode 100644 index 0000000..a985f42 --- /dev/null +++ b/xlators/experimental/create-restrictions/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = src + +CLEANFILES = diff --git a/xlators/experimental/create-restrictions/src/Makefile.am b/xlators/experimental/create-restrictions/src/Makefile.am new file mode 100644 index 0000000..78fac8b --- /dev/null +++ b/xlators/experimental/create-restrictions/src/Makefile.am @@ -0,0 +1,16 @@ +xlator_LTLIBRARIES = create-restrictions.la +xlatordir = $(libdir)/glusterfs/$(PACKAGE_VERSION)/xlator/experimental + +create_restrictions_la_LDFLAGS = -module $(GF_XLATOR_DEFAULT_LDFLAGS) + +create_restrictions_la_SOURCES = create-restrictions.c +create_restrictions_la_LIBADD = $(top_builddir)/libglusterfs/src/libglusterfs.la + +noinst_HEADERS = create-restrictions.h + +AM_CPPFLAGS = $(GF_CPPFLAGS) -I$(top_srcdir)/libglusterfs/src \ + -I$(top_srcdir)/rpc/xdr/src -I$(top_builddir)/rpc/xdr/src + +AM_CFLAGS = -Wall $(GF_CFLAGS) + +CLEANFILES = diff --git a/xlators/experimental/create-restrictions/src/create-restrictions.c b/xlators/experimental/create-restrictions/src/create-restrictions.c new file mode 100644 index 0000000..9aefa91 --- /dev/null +++ b/xlators/experimental/create-restrictions/src/create-restrictions.c @@ -0,0 +1,262 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. + */ + +#include "create-restrictions.h" + +int32_t +_crt_restrict_create_task (void *data) +{ + loc_t tmp_loc = {0,}; + int ret = 0; + call_frame_t *frame = NULL; + xlator_t *this = NULL; + xlator_t *subvol = NULL; + dict_t *in_xdata = NULL; + crt_restrict_local_t *local = NULL; + double percent = 0; + double i_percent = 0; + struct statvfs statvfs = {0,}; + + frame = data; + this = THIS; + local = (crt_restrict_local_t*) frame->local; + + /* make it root gfid */ + tmp_loc.gfid[15] = 1; + in_xdata = dict_new (); + if (!in_xdata) { + ret = -1; + gf_log (this->name, GF_LOG_ERROR, + "Failed to allocate dictionary"); + goto out; + } + + ret = dict_set_int8 (in_xdata, GF_INTERNAL_IGNORE_DEEM_STATFS, 1); + if (ret) { + gf_log (this->name, GF_LOG_ERROR, + "Failed to set " + GF_INTERNAL_IGNORE_DEEM_STATFS" in dict"); + ret = -1; + goto out; + } + + ret = syncop_statfs (FIRST_CHILD (this), &tmp_loc, + (struct statvfs*) &statvfs, in_xdata, NULL); + if (ret) { + gf_log (this->name, GF_LOG_ERROR, + "Failed to create synctask"); + ret = -1; + goto out; + } + + if (statvfs.f_blocks) + percent = (statvfs.f_bavail * 100) / statvfs.f_blocks; + + if (statvfs.f_files) + i_percent = (statvfs.f_ffree * 100) / statvfs.f_files; + + /* 60 is JUST for testing */ + /* TODO: configurable value for threshold */ + if (percent < 60 || i_percent < 60) { + gf_log (this->name, GF_LOG_DEBUG, + "create-restrictions is enabled. " + "free blocks: %.2f%%, free inodes: %.2f%%", + percent, i_percent); + ret = -1; + + CRT_RESTRICT_STACK_UNWIND (create, frame, -1, ENOSPC, local->fd, + NULL, NULL, NULL, NULL, NULL); + }else{ + subvol = local->subvol; + STACK_WIND_COOKIE (frame, crt_restrict_create_cbk, subvol, + subvol, subvol->fops->create, &local->loc, + local->flags, local->mode, local->umask, + local->fd, local->xattr_req); + } +out: + return ret; +} + +int32_t +_crt_restrict_create_complete (int op_ret, call_frame_t *frame, void *data) +{ + return 0; +} + +int32_t +_crt_restrict_keep_meta (call_frame_t *frame, + xlator_t *this, + loc_t *loc, + int32_t flags, + mode_t mode, + mode_t umask, + fd_t *fd, + dict_t *xdata) +{ + crt_restrict_local_t *local = NULL; + + local = (crt_restrict_local_t *) frame->local; + local->subvol = FIRST_CHILD(this); + + local->flags = flags; + local->mode = mode; + local->umask = umask; + + if (fd) + local->fd = fd_ref (fd); + + if (loc) + loc_copy (&local->loc, loc); + + if (xdata) + local->xattr_req = dict_ref(xdata); + + return 0; +} + +int32_t +crt_restrict_create_cbk (call_frame_t *frame, void *cookie, xlator_t *this, + int op_ret, int op_errno, + fd_t *fd, inode_t *inode, struct iatt *stbuf, + struct iatt *preparent, struct iatt *postparent, dict_t *xdata) +{ + CRT_RESTRICT_STACK_UNWIND (create, frame, op_ret, op_errno, fd, inode, + stbuf, preparent, postparent, xdata); + + return 0; +} + +int32_t +crt_restrict_create (call_frame_t *frame, xlator_t *this, + loc_t *loc, int32_t flags, mode_t mode, + mode_t umask, fd_t *fd, dict_t *xdata) +{ + int ret = 0; + crt_restrict_private_t *priv = (crt_restrict_private_t *) this->private; + crt_restrict_local_t *local = NULL; + + if (!strncmp(priv->is_active, "on", 2)) { + frame->local = mem_get0 (this->local_pool); + local = (crt_restrict_local_t *) frame->local; + if (!local) + goto out; + + _crt_restrict_keep_meta (frame, this, loc, flags, mode, + umask, fd, xdata); + + ret = synctask_new (this->ctx->env, + _crt_restrict_create_task, + _crt_restrict_create_complete, + frame, frame); + if (ret) { + gf_log (this->name, GF_LOG_ERROR, + "Failed to create synctask"); + ret = -1; + goto out; + } + }else{ + STACK_WIND (frame, + crt_restrict_create_cbk, + FIRST_CHILD (this), + FIRST_CHILD (this)->fops->create, + loc, flags, mode, umask, fd, xdata); + } +out: + return -1; +} + +void +crt_restrict_local_wipe (xlator_t *this, crt_restrict_local_t *local) +{ + if (!local) + return; + + if (local->xattr_req) + dict_unref (local->xattr_req); + + if (local->fd) { + fd_unref (local->fd); + local->fd = NULL; + } + + mem_put (local); +} + +int32_t +init (xlator_t *this) +{ + data_t *data = NULL; + crt_restrict_private_t *priv = NULL; + + if (!this->children || this->children->next) { + gf_log ("crt_restrict", GF_LOG_ERROR, + "FATAL: crt_restrict should have exactly one child"); + return -1; + } + + if (!this->parents) { + gf_log (this->name, GF_LOG_WARNING, + "dangling volume. check volfile "); + } + + priv = GF_CALLOC (sizeof (crt_restrict_private_t), 1, 0); + if (!priv) + return -1; + + data = dict_get (this->options, "create-restrictions-active"); + if (data) { + priv->is_active = data->data; + }else { + priv->is_active = "off"; + } + + this->local_pool = mem_pool_new (crt_restrict_local_t, 512); + if (!this->local_pool) { + gf_log (this->name, GF_LOG_ERROR, + " crt_restrict initialisation failed. " + "failed to create local_t's memory pool"); + goto out; + } + + this->private = priv; + gf_log ("crt_restrict", GF_LOG_DEBUG, "crt_restrict xlator loaded"); + +out: + return 0; +} + +int32_t +fini (xlator_t *this) +{ + crt_restrict_private_t *priv = this->private; + + if (!priv) + return -1; + + this->private = NULL; + GF_FREE (priv); + + return 0; +} + +struct xlator_fops fops = { + .create = crt_restrict_create, +}; + +struct xlator_cbks cbks; + +struct volume_options options[] = { + { .key = {"create-restrictions-active" }, + .type = GF_OPTION_TYPE_STR, + .description = "create-restrictions-active description", + .value = { "on", "off", "" } + }, + { .key = {NULL} }, +}; diff --git a/xlators/experimental/create-restrictions/src/create-restrictions.h b/xlators/experimental/create-restrictions/src/create-restrictions.h new file mode 100644 index 0000000..b8be328 --- /dev/null +++ b/xlators/experimental/create-restrictions/src/create-restrictions.h @@ -0,0 +1,62 @@ +/* + Copyright (c) 2006-2012 Red Hat, Inc. + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ +#ifndef __CRT_RESTRICT_H__ +#define __CRT_RESTRICT_H__ + +#include +#include "glusterfs.h" +#include "xlator.h" +#include "logging.h" +#include "dict.h" +#include "syncop.h" + +#define CRT_RESTRICT_STACK_UNWIND(fop, frame, params ...) do { \ + crt_restrict_local_t *__local = NULL; \ + xlator_t *__xl = NULL; \ + if (frame) { \ + __xl = frame->this; \ + __local = frame->local; \ + frame->local = NULL; \ + } \ + STACK_UNWIND_STRICT (fop, frame, params); \ + crt_restrict_local_wipe (__xl, __local); \ + } while (0) + + +typedef struct { + char *is_active; +} crt_restrict_private_t; + +typedef struct { + /* to resume io */ + xlator_t *subvol; + loc_t loc; + int32_t flags; + mode_t mode; + mode_t umask; + fd_t *fd; + dict_t *xattr_req; +} crt_restrict_local_t; + +int32_t +crt_restrict_create_cbk (call_frame_t *frame, void *cookie, xlator_t *this, + int op_ret, int op_errno, + fd_t *fd, inode_t *inode, struct iatt *stbuf, + struct iatt *preparent, struct iatt *postparent, dict_t *xdata); + +int32_t +crt_restrict_create (call_frame_t *frame, xlator_t *this, + loc_t *loc, int32_t flags, mode_t mode, + mode_t umask, fd_t *fd, dict_t *params); + +void +crt_restrict_local_wipe (xlator_t *this, crt_restrict_local_t *local); + +#endif /* __CRT_RESTRICT_H__ */