#include "includes.h"
#if defined(F_PREALLOCATE) && defined(HAVE_FSTORE_T)
#define USE_DARWIN_PREALLOCATE
#elif defined (HAVE_STRUCT_FLOCK64) || define(HAVE_XFS_LIBXFS_H)
#define USE_XFS_PREALLOCATE
#endif
#define MODULE "prealloc"
static int module_debug;
#ifdef USE_XFS_PREALLOCATE
#ifdef HAVE_XFS_LIBXFS_H
#include <xfs/libxfs.h>
#define lock_type xfs_flock64_t
#else
#define lock_type struct flock64
#endif
static int preallocate_xfs(int fd, lock_type * fl)
{
#if defined(XFS_IOC_RESVSP64)
return xfsctl(NULL, fd, XFS_IOC_RESVSP64, &fl);
#elif defined(F_RESVSP64)
return fcntl(fd, F_RESVSP64, &fl);
#else
errno = ENOTSUP;
return -1;
#endif
}
#endif
static int preallocate_space(int fd, SMB_OFF_T current, SMB_OFF_T size)
{
#if defined(USE_DARWIN_PREALLOCATE)
fstore_t fst;
#elif defined(USE_XFS_PREALLOCATE)
lock_type fl = {0};
#endif
int err;
if (size <= 0 || current >= size) {
return 0;
}
#if defined(USE_DARWIN_PREALLOCATE)
fst.fst_flags = F_ALLOCATECONTIG;
fst.fst_posmode = F_PEOFPOSMODE;
fst.fst_offset = 0;
fst.fst_length = size - current;
fst.fst_bytesalloc = 0;
err = fcntl(fd, F_PREALLOCATE, &fst);
#elif defined(USE_XFS_PREALLOCATE)
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = size;
err = preallocate_xfs(fd, &fl);
#else
err = -1;
errno = ENOSYS;
#endif
if (err && errno != ENOTSUP) {
DEBUG(module_debug,
("%s: preallocate failed on fd=%d size=%lld: %s\n",
MODULE, fd, (long long)size, strerror(errno)));
}
return err;
}
static int prealloc_connect(
struct vfs_handle_struct * handle,
const char * service,
const char * user)
{
module_debug = lp_parm_int(SNUM(handle->conn),
MODULE, "msglevel", 100);
return SMB_VFS_NEXT_CONNECT(handle, service, user);
}
static int prealloc_open(vfs_handle_struct* handle,
const char * fname,
files_struct * fsp,
int flags,
mode_t mode)
{
int fd;
SMB_OFF_T size = 0;
const char * dot;
char fext[10];
if (!(flags & (O_CREAT|O_TRUNC))) {
goto normal_open;
}
*fext = '\0';
dot = strrchr(fname, '.');
if (dot && *++dot) {
if (strlen(dot) < sizeof(fext)) {
strncpy(fext, dot, sizeof(fext));
strnorm(fext, CASE_LOWER);
}
}
if (*fext == '\0') {
goto normal_open;
}
size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
fext, NULL));
if (size <= 0) {
goto normal_open;
}
fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
if (fd < 0) {
return fd;
}
if ((flags & O_CREAT) || (flags & O_TRUNC)) {
SMB_OFF_T * psize;
psize = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T);
if (psize == NULL || *psize == -1) {
return fd;
}
DEBUG(module_debug,
("%s: preallocating %s (fd=%d) to %lld bytes\n",
MODULE, fname, fd, (long long)size));
*psize = size;
if (preallocate_space(fd, 0, *psize) < 0) {
VFS_REMOVE_FSP_EXTENSION(handle, fsp);
}
}
return fd;
normal_open:
DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
MODULE, fname));
return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
}
static int prealloc_ftruncate(vfs_handle_struct * handle,
files_struct * fsp,
int fd,
SMB_OFF_T offset)
{
SMB_OFF_T *psize;
int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, fd, offset);
if (ret == 0 ) {
if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
preallocate_space(fd, offset, *psize);
}
}
return ret;
}
static vfs_op_tuple prealloc_op_tuples[] = {
{SMB_VFS_OP(prealloc_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
{SMB_VFS_OP(prealloc_ftruncate), SMB_VFS_OP_FTRUNCATE, SMB_VFS_LAYER_TRANSPARENT},
{SMB_VFS_OP(prealloc_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT},
{NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
};
NTSTATUS vfs_prealloc_init(void);
NTSTATUS vfs_prealloc_init(void)
{
return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
MODULE, prealloc_op_tuples);
}