#include "dbwrap/dbwrap.h"
#include "dbwrap/dbwrap_open.h"
#include "source3/lib/xattr_tdb.h"
+#include "lib/util/tevent_unix.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_VFS
{
int ret;
TALLOC_CTX *frame = talloc_stackframe();
- struct smb_filename *smb_fname = NULL;
- NTSTATUS status = create_synthetic_smb_fname_split(frame, path, NULL,
- &smb_fname);
- if (!NT_STATUS_IS_OK(status)) {
- errno = map_errno_from_nt_status(status);
- TALLOC_FREE(frame);
+ struct smb_filename *smb_fname;
+
+ smb_fname = synthetic_smb_fname(frame,
+ path,
+ NULL,
+ NULL,
+ 0,
+ 0);
+ if (smb_fname == NULL) {
+ TALLOC_FREE(frame);
+ errno = ENOMEM;
return -1;
}
}
static ssize_t xattr_tdb_getxattr(struct vfs_handle_struct *handle,
- const char *path, const char *name,
- void *value, size_t size)
+ const struct smb_filename *smb_fname,
+ const char *name,
+ void *value,
+ size_t size)
{
struct file_id id;
struct db_context *db;
TALLOC_FREE(frame); return -1;
});
- ret = xattr_tdb_get_file_id(handle, path, &id);
+ ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
if (ret == -1) {
TALLOC_FREE(frame);
return -1;
TALLOC_FREE(frame);
return -1;
}
+
+ if (size == 0) {
+ TALLOC_FREE(frame);
+ return xattr_size;
+ }
+
if (blob.length > size) {
TALLOC_FREE(frame);
errno = ERANGE;
return xattr_size;
}
+struct xattr_tdb_getxattrat_state {
+ struct vfs_aio_state vfs_aio_state;
+ ssize_t xattr_size;
+ uint8_t *xattr_value;
+};
+
+static struct tevent_req *xattr_tdb_getxattrat_send(
+ TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct vfs_handle_struct *handle,
+ files_struct *dir_fsp,
+ const struct smb_filename *smb_fname,
+ const char *xattr_name,
+ size_t alloc_hint)
+{
+ struct tevent_req *req = NULL;
+ struct xattr_tdb_getxattrat_state *state = NULL;
+ struct smb_filename *cwd = NULL;
+ struct db_context *db = NULL;
+ struct file_id id;
+ int ret;
+ int error;
+ int cwd_ret;
+ DATA_BLOB xattr_blob;
+
+ req = tevent_req_create(mem_ctx, &state,
+ struct xattr_tdb_getxattrat_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->xattr_size = -1;
+
+ SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
+ if (!xattr_tdb_init(-1, state, &db)) {
+ tevent_req_error(req, EIO);
+ return tevent_req_post(req, ev);
+ });
+
+ cwd = SMB_VFS_GETWD(dir_fsp->conn, state);
+ if (tevent_req_nomem(cwd, req)) {
+ return tevent_req_post(req, ev);
+ }
+
+ ret = SMB_VFS_CHDIR(dir_fsp->conn, dir_fsp->fsp_name);
+ if (ret != 0) {
+ tevent_req_error(req, errno);
+ return tevent_req_post(req, ev);
+ }
+
+ ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
+ error = errno;
+
+ cwd_ret = SMB_VFS_CHDIR(dir_fsp->conn, cwd);
+ SMB_ASSERT(cwd_ret == 0);
+
+ if (ret == -1) {
+ tevent_req_error(req, error);
+ return tevent_req_post(req, ev);
+ }
+
+ state->xattr_size = xattr_tdb_getattr(db,
+ state,
+ &id,
+ xattr_name,
+ &xattr_blob);
+ if (state->xattr_size == -1) {
+ tevent_req_error(req, errno);
+ return tevent_req_post(req, ev);
+ }
+
+ if (alloc_hint == 0) {
+ /*
+ * The caller only wants to know the size.
+ */
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
+ }
+
+ if (state->xattr_size == 0) {
+ /*
+ * There's no data.
+ */
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
+ }
+
+ if (xattr_blob.length > alloc_hint) {
+ /*
+ * The data doesn't fit.
+ */
+ state->xattr_size = -1;
+ tevent_req_error(req, ERANGE);
+ return tevent_req_post(req, ev);
+ }
+
+ /*
+ * take the whole blob.
+ */
+ state->xattr_value = xattr_blob.data;
+
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
+}
+
+static ssize_t xattr_tdb_getxattrat_recv(struct tevent_req *req,
+ struct vfs_aio_state *aio_state,
+ TALLOC_CTX *mem_ctx,
+ uint8_t **xattr_value)
+{
+ struct xattr_tdb_getxattrat_state *state = tevent_req_data(
+ req, struct xattr_tdb_getxattrat_state);
+ ssize_t xattr_size;
+
+ if (tevent_req_is_unix_error(req, &aio_state->error)) {
+ tevent_req_received(req);
+ return -1;
+ }
+
+ *aio_state = state->vfs_aio_state;
+ xattr_size = state->xattr_size;
+ if (xattr_value != NULL) {
+ *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
+ }
+
+ tevent_req_received(req);
+ return xattr_size;
+}
+
static ssize_t xattr_tdb_fgetxattr(struct vfs_handle_struct *handle,
struct files_struct *fsp,
const char *name, void *value, size_t size)
TALLOC_FREE(frame);
return -1;
}
+
+ if (size == 0) {
+ TALLOC_FREE(frame);
+ return xattr_size;
+ }
+
if (blob.length > size) {
TALLOC_FREE(frame);
errno = ERANGE;
}
static int xattr_tdb_setxattr(struct vfs_handle_struct *handle,
- const char *path, const char *name,
- const void *value, size_t size, int flags)
+ const struct smb_filename *smb_fname,
+ const char *name,
+ const void *value,
+ size_t size,
+ int flags)
{
struct file_id id;
struct db_context *db;
TALLOC_FREE(frame); return -1;
});
- ret = xattr_tdb_get_file_id(handle, path, &id);
+ ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
if (ret == -1) {
TALLOC_FREE(frame);
return -1;
}
static ssize_t xattr_tdb_listxattr(struct vfs_handle_struct *handle,
- const char *path, char *list, size_t size)
+ const struct smb_filename *smb_fname,
+ char *list,
+ size_t size)
{
struct file_id id;
struct db_context *db;
TALLOC_FREE(frame); return -1;
});
- ret = xattr_tdb_get_file_id(handle, path, &id);
+ ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
if (ret == -1) {
TALLOC_FREE(frame);
return -1;
}
static int xattr_tdb_removexattr(struct vfs_handle_struct *handle,
- const char *path, const char *name)
+ const struct smb_filename *smb_fname,
+ const char *name)
{
struct file_id id;
struct db_context *db;
TALLOC_FREE(frame); return -1;
});
- ret = xattr_tdb_get_file_id(handle, path, &id);
+ ret = xattr_tdb_get_file_id(handle, smb_fname->base_name, &id);
if (ret == -1) {
TALLOC_FREE(frame);
return ret;
const char *dbname;
char *def_dbname;
- def_dbname = state_path("xattr.tdb");
+ def_dbname = state_path(talloc_tos(), "xattr.tdb");
if (def_dbname == NULL) {
errno = ENOSYS;
return false;
become_root();
db = db_open(NULL, dbname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
- DBWRAP_LOCK_ORDER_2);
+ DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
unbecome_root();
if (db == NULL) {
return true;
}
+static int xattr_tdb_openat(struct vfs_handle_struct *handle,
+ const struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ struct files_struct *fsp,
+ int flags,
+ mode_t mode)
+{
+ struct db_context *db = NULL;
+ TALLOC_CTX *frame = NULL;
+ SMB_STRUCT_STAT sbuf;
+ int ret;
+
+ fsp->fh->fd = SMB_VFS_NEXT_OPENAT(handle,
+ dirfsp,
+ smb_fname,
+ fsp,
+ flags,
+ mode);
+
+ if (fsp->fh->fd < 0) {
+ return fsp->fh->fd;
+ }
+
+ if ((flags & (O_CREAT|O_EXCL)) != (O_CREAT|O_EXCL)) {
+ return fsp->fh->fd;
+ }
+
+ /*
+ * We know we used O_CREAT|O_EXCL and it worked.
+ * We must have created the file.
+ */
+
+ ret = SMB_VFS_FSTAT(fsp, &sbuf);
+ if (ret == -1) {
+ /* Can't happen... */
+ DBG_WARNING("SMB_VFS_FSTAT failed on file %s (%s)\n",
+ smb_fname_str_dbg(smb_fname),
+ strerror(errno));
+ return -1;
+ }
+
+ fsp->file_id = SMB_VFS_FILE_ID_CREATE(fsp->conn, &sbuf);
+
+ frame = talloc_stackframe();
+
+ SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
+ if (!xattr_tdb_init(-1, frame, &db))
+ {
+ TALLOC_FREE(frame); return -1;
+ });
+
+ xattr_tdb_remove_all_attrs(db, &fsp->file_id);
+
+ TALLOC_FREE(frame);
+ return fsp->fh->fd;
+}
+
+static int xattr_tdb_mkdirat(vfs_handle_struct *handle,
+ struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ mode_t mode)
+{
+ struct db_context *db = NULL;
+ TALLOC_CTX *frame = NULL;
+ struct file_id fileid;
+ int ret;
+ struct smb_filename *smb_fname_tmp = NULL;
+
+ ret = SMB_VFS_NEXT_MKDIRAT(handle,
+ dirfsp,
+ smb_fname,
+ mode);
+ if (ret < 0) {
+ return ret;
+ }
+
+ frame = talloc_stackframe();
+ smb_fname_tmp = cp_smb_filename(frame, smb_fname);
+ if (smb_fname_tmp == NULL) {
+ TALLOC_FREE(frame);
+ errno = ENOMEM;
+ return -1;
+ }
+
+ /* Always use LSTAT here - we just created the directory. */
+ ret = SMB_VFS_LSTAT(handle->conn, smb_fname_tmp);
+ if (ret == -1) {
+ /* Rename race. Let upper level take care of it. */
+ TALLOC_FREE(frame);
+ return -1;
+ }
+ if (!S_ISDIR(smb_fname_tmp->st.st_ex_mode)) {
+ /* Rename race. Let upper level take care of it. */
+ TALLOC_FREE(frame);
+ return -1;
+ }
+
+ fileid = SMB_VFS_FILE_ID_CREATE(handle->conn, &smb_fname_tmp->st);
+
+ SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
+ if (!xattr_tdb_init(-1, frame, &db))
+ {
+ TALLOC_FREE(frame); return -1;
+ });
+
+ xattr_tdb_remove_all_attrs(db, &fileid);
+ TALLOC_FREE(frame);
+ return 0;
+}
+
/*
* On unlink we need to delete the tdb record
*/
-static int xattr_tdb_unlink(vfs_handle_struct *handle,
- const struct smb_filename *smb_fname)
+static int xattr_tdb_unlinkat(vfs_handle_struct *handle,
+ struct files_struct *dirfsp,
+ const struct smb_filename *smb_fname,
+ int flags)
{
struct smb_filename *smb_fname_tmp = NULL;
struct file_id id;
return -1;
}
- if (lp_posix_pathnames()) {
+ if (smb_fname_tmp->flags & SMB_FILENAME_POSIX_PATH) {
ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_tmp);
} else {
ret = SMB_VFS_NEXT_STAT(handle, smb_fname_tmp);
goto out;
}
- if (smb_fname_tmp->st.st_ex_nlink == 1) {
- /* Only remove record on last link to file. */
+ if (flags & AT_REMOVEDIR) {
+ /* Always remove record when removing a directory succeeds. */
remove_record = true;
+ } else {
+ if (smb_fname_tmp->st.st_ex_nlink == 1) {
+ /* Only remove record on last link to file. */
+ remove_record = true;
+ }
}
- ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
+ ret = SMB_VFS_NEXT_UNLINKAT(handle,
+ dirfsp,
+ smb_fname_tmp,
+ flags);
if (ret == -1) {
goto out;
return ret;
}
-/*
- * On rmdir we need to delete the tdb record
- */
-static int xattr_tdb_rmdir(vfs_handle_struct *handle, const char *path)
-{
- SMB_STRUCT_STAT sbuf;
- struct file_id id;
- struct db_context *db;
- int ret;
- TALLOC_CTX *frame = talloc_stackframe();
-
- SMB_VFS_HANDLE_GET_DATA(handle, db, struct db_context,
- if (!xattr_tdb_init(-1, frame, &db))
- {
- TALLOC_FREE(frame); return -1;
- });
-
- if (vfs_stat_smb_fname(handle->conn, path, &sbuf) == -1) {
- TALLOC_FREE(frame);
- return -1;
- }
-
- ret = SMB_VFS_NEXT_RMDIR(handle, path);
-
- if (ret == -1) {
- TALLOC_FREE(frame);
- return -1;
- }
-
- id = SMB_VFS_NEXT_FILE_ID_CREATE(handle, &sbuf);
-
- xattr_tdb_remove_all_attrs(db, &id);
-
- TALLOC_FREE(frame);
- return 0;
-}
-
/*
* Destructor for the VFS private data
*/
static struct vfs_fn_pointers vfs_xattr_tdb_fns = {
.getxattr_fn = xattr_tdb_getxattr,
+ .getxattrat_send_fn = xattr_tdb_getxattrat_send,
+ .getxattrat_recv_fn = xattr_tdb_getxattrat_recv,
.fgetxattr_fn = xattr_tdb_fgetxattr,
.setxattr_fn = xattr_tdb_setxattr,
.fsetxattr_fn = xattr_tdb_fsetxattr,
.flistxattr_fn = xattr_tdb_flistxattr,
.removexattr_fn = xattr_tdb_removexattr,
.fremovexattr_fn = xattr_tdb_fremovexattr,
- .unlink_fn = xattr_tdb_unlink,
- .rmdir_fn = xattr_tdb_rmdir,
+ .openat_fn = xattr_tdb_openat,
+ .mkdirat_fn = xattr_tdb_mkdirat,
+ .unlinkat_fn = xattr_tdb_unlinkat,
.connect_fn = xattr_tdb_connect,
};
-NTSTATUS vfs_xattr_tdb_init(void);
-NTSTATUS vfs_xattr_tdb_init(void)
+static_decl_vfs;
+NTSTATUS vfs_xattr_tdb_init(TALLOC_CTX *ctx)
{
return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "xattr_tdb",
&vfs_xattr_tdb_fns);