Beispiel #1
0
static int prealloc_ftruncate(vfs_handle_struct * handle,
			files_struct *	fsp,
			SMB_OFF_T	offset)
{
	SMB_OFF_T *psize;
	int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);

	/* Maintain the allocated space even in the face of truncates. */
	if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
		preallocate_space(fsp->fh->fd, *psize);
	}

	return ret;
}
Beispiel #2
0
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);

	/* Maintain the allocated space even in the face of truncates. If the
	 * truncate succeeded, we know that the current file size is the size
	 * the caller requested.
	 */
	if (ret == 0 ) {
		if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
			preallocate_space(fd, offset, *psize);
		}
	}

	return ret;
}
Beispiel #3
0
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))) {
		/* Caller is not intending to rewrite the file. Let's not mess
		 * with the allocation in this case.
		 */
		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;
	}

	/* Syntax for specifying preallocation size is:
	 *	MODULE: <extension> = <size>
	 * where
	 *	<extension> is the file extension in lower case
	 *	<size> is a size like 10, 10K, 10M
	 */
	size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
						    fext, NULL));
	if (size <= 0) {
		/* No need to preallocate this file. */
		goto normal_open;
	}

	fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
	if (fd < 0) {
		return fd;
	}

	/* Prellocate only if the file is being created or replaced. Note that
	 * Samba won't ever pass down O_TRUNC, which is why we have to handle
	 * truncate calls specially.
	 */
	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:
	/* We are not creating or replacing a file. Skip the
	 * preallocation.
	 */
	DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
		    MODULE, fname));
	return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
}