00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "includes.h"
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifdef HAVE_XFS_LIBXFS_H
00045 #include <xfs/libxfs.h>
00046 #define lock_type xfs_flock64_t
00047 #else
00048 #define lock_type struct flock64
00049 #endif
00050
00051 #define MODULE "prealloc"
00052 static int module_debug;
00053
00054 static int preallocate_space(int fd, SMB_OFF_T size)
00055 {
00056 lock_type fl = {0};
00057 int err;
00058
00059 if (size <= 0) {
00060 return 0;
00061 }
00062
00063 fl.l_whence = SEEK_SET;
00064 fl.l_start = 0;
00065 fl.l_len = size;
00066
00067
00068
00069
00070
00071
00072 #if defined(XFS_IOC_RESVSP64)
00073
00074 err = xfsctl(NULL, fd, XFS_IOC_RESVSP64, &fl);
00075 #elif defined(F_RESVSP64)
00076
00077 err = fcntl(fd, F_RESVSP64, &fl);
00078 #else
00079 err = -1;
00080 errno = ENOSYS;
00081 #endif
00082
00083 if (err) {
00084 DEBUG(module_debug,
00085 ("%s: preallocate failed on fd=%d size=%lld: %s\n",
00086 MODULE, fd, (long long)size, strerror(errno)));
00087 }
00088
00089 return err;
00090 }
00091
00092 static int prealloc_connect(
00093 struct vfs_handle_struct * handle,
00094 const char * service,
00095 const char * user)
00096 {
00097 module_debug = lp_parm_int(SNUM(handle->conn),
00098 MODULE, "debug", 100);
00099
00100 return SMB_VFS_NEXT_CONNECT(handle, service, user);
00101 }
00102
00103 static int prealloc_open(vfs_handle_struct* handle,
00104 const char * fname,
00105 files_struct * fsp,
00106 int flags,
00107 mode_t mode)
00108 {
00109 int fd;
00110 off64_t size = 0;
00111
00112 const char * dot;
00113 char fext[10];
00114
00115 if (!(flags & (O_CREAT|O_TRUNC))) {
00116
00117
00118
00119 goto normal_open;
00120 }
00121
00122 *fext = '\0';
00123 dot = strrchr(fname, '.');
00124 if (dot && *++dot) {
00125 if (strlen(dot) < sizeof(fext)) {
00126 strncpy(fext, dot, sizeof(fext));
00127 strnorm(fext, CASE_LOWER);
00128 }
00129 }
00130
00131 if (*fext == '\0') {
00132 goto normal_open;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
00142 fext, NULL));
00143 if (size <= 0) {
00144
00145 goto normal_open;
00146 }
00147
00148 fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
00149 if (fd < 0) {
00150 return fd;
00151 }
00152
00153
00154
00155
00156
00157 if ((flags & O_CREAT) || (flags & O_TRUNC)) {
00158 SMB_OFF_T * psize;
00159
00160 psize = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T);
00161 if (psize == NULL || *psize == -1) {
00162 return fd;
00163 }
00164
00165 DEBUG(module_debug,
00166 ("%s: preallocating %s (fd=%d) to %lld bytes\n",
00167 MODULE, fname, fd, (long long)size));
00168
00169 *psize = size;
00170 if (preallocate_space(fd, *psize) < 0) {
00171 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
00172 }
00173 }
00174
00175 return fd;
00176
00177 normal_open:
00178
00179
00180
00181 DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
00182 MODULE, fname));
00183 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
00184 }
00185
00186 static int prealloc_ftruncate(vfs_handle_struct * handle,
00187 files_struct * fsp,
00188 int fd,
00189 SMB_OFF_T offset)
00190 {
00191 SMB_OFF_T *psize;
00192 int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, fd, offset);
00193
00194
00195 if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
00196 preallocate_space(fd, *psize);
00197 }
00198
00199 return ret;
00200 }
00201
00202 static vfs_op_tuple prealloc_op_tuples[] = {
00203 {SMB_VFS_OP(prealloc_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
00204 {SMB_VFS_OP(prealloc_ftruncate), SMB_VFS_OP_FTRUNCATE, SMB_VFS_LAYER_TRANSPARENT},
00205 {SMB_VFS_OP(prealloc_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT},
00206 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
00207 };
00208
00209 NTSTATUS vfs_prealloc_init(void);
00210 NTSTATUS vfs_prealloc_init(void)
00211 {
00212 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
00213 MODULE, prealloc_op_tuples);
00214 }
00215