4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 #include <sys/zfs_context.h>
23 #include <sys/zfs_file.h>
26 #include <linux/falloc.h>
28 #include <linux/uaccess.h>
29 #ifdef HAVE_FDTABLE_HEADER
30 #include <linux/fdtable.h>
36 * path - fully qualified path to file
37 * flags - file attributes O_READ / O_WRITE / O_EXCL
38 * fpp - pointer to return file pointer
40 * Returns 0 on success underlying error on failure.
43 zfs_file_open(const char *path, int flags, int mode, zfs_file_t **fpp)
48 if (!(flags & O_CREAT) && (flags & O_WRONLY))
52 saved_umask = xchg(¤t->fs->umask, 0);
54 filp = filp_open(path, flags, mode);
57 (void) xchg(¤t->fs->umask, saved_umask);
60 return (-PTR_ERR(filp));
67 zfs_file_close(zfs_file_t *fp)
73 zfs_file_write_impl(zfs_file_t *fp, const void *buf, size_t count, loff_t *off)
75 #if defined(HAVE_KERNEL_WRITE_PPOS)
76 return (kernel_write(fp, buf, count, off));
78 mm_segment_t saved_fs;
84 rc = vfs_write(fp, (__force const char __user __user *)buf, count, off);
93 * Stateful write - use os internal file pointer to determine where to
94 * write and update on successful completion.
96 * fp - pointer to file (pipe, socket, etc) to write to
97 * buf - buffer to write
98 * count - # of bytes to write
99 * resid - pointer to count of unwritten bytes (if short write)
101 * Returns 0 on success errno on failure.
104 zfs_file_write(zfs_file_t *fp, const void *buf, size_t count, ssize_t *resid)
106 loff_t off = fp->f_pos;
109 rc = zfs_file_write_impl(fp, buf, count, &off);
117 } else if (rc != count) {
125 * Stateless write - os internal file pointer is not updated.
127 * fp - pointer to file (pipe, socket, etc) to write to
128 * buf - buffer to write
129 * count - # of bytes to write
130 * off - file offset to write to (only valid for seekable types)
131 * resid - pointer to count of unwritten bytes
133 * Returns 0 on success errno on failure.
136 zfs_file_pwrite(zfs_file_t *fp, const void *buf, size_t count, loff_t off,
141 rc = zfs_file_write_impl(fp, buf, count, &off);
147 } else if (rc != count) {
155 zfs_file_read_impl(zfs_file_t *fp, void *buf, size_t count, loff_t *off)
157 #if defined(HAVE_KERNEL_READ_PPOS)
158 return (kernel_read(fp, buf, count, off));
160 mm_segment_t saved_fs;
166 rc = vfs_read(fp, (void __user *)buf, count, off);
174 * Stateful read - use os internal file pointer to determine where to
175 * read and update on successful completion.
177 * fp - pointer to file (pipe, socket, etc) to read from
178 * buf - buffer to write
179 * count - # of bytes to read
180 * resid - pointer to count of unread bytes (if short read)
182 * Returns 0 on success errno on failure.
185 zfs_file_read(zfs_file_t *fp, void *buf, size_t count, ssize_t *resid)
187 loff_t off = fp->f_pos;
190 rc = zfs_file_read_impl(fp, buf, count, &off);
198 } else if (rc != count) {
206 * Stateless read - os internal file pointer is not updated.
208 * fp - pointer to file (pipe, socket, etc) to read from
209 * buf - buffer to write
210 * count - # of bytes to write
211 * off - file offset to read from (only valid for seekable types)
212 * resid - pointer to count of unwritten bytes (if short write)
214 * Returns 0 on success errno on failure.
217 zfs_file_pread(zfs_file_t *fp, void *buf, size_t count, loff_t off,
222 rc = zfs_file_read_impl(fp, buf, count, &off);
228 } else if (rc != count) {
236 * lseek - set / get file pointer
238 * fp - pointer to file (pipe, socket, etc) to read from
239 * offp - value to seek to, returns current value plus passed offset
240 * whence - see man pages for standard lseek whence values
242 * Returns 0 on success errno on failure (ESPIPE for non seekable types)
245 zfs_file_seek(zfs_file_t *fp, loff_t *offp, int whence)
249 if (*offp < 0 || *offp > MAXOFFSET_T)
252 rc = vfs_llseek(fp, *offp, whence);
262 * Get file attributes
264 * filp - file pointer
265 * zfattr - pointer to file attr structure
267 * Currently only used for fetching size and file mode.
269 * Returns 0 on success or error code of underlying getattr call on failure.
272 zfs_file_getattr(zfs_file_t *filp, zfs_file_attr_t *zfattr)
277 #if defined(HAVE_4ARGS_VFS_GETATTR)
278 rc = vfs_getattr(&filp->f_path, &stat, STATX_BASIC_STATS,
279 AT_STATX_SYNC_AS_STAT);
280 #elif defined(HAVE_2ARGS_VFS_GETATTR)
281 rc = vfs_getattr(&filp->f_path, &stat);
283 rc = vfs_getattr(filp->f_path.mnt, filp->f_dentry, &stat);
288 zfattr->zfa_size = stat.size;
289 zfattr->zfa_mode = stat.mode;
297 * filp - file pointer
298 * flags - O_SYNC and or O_DSYNC
300 * Returns 0 on success or error code of underlying sync call on failure.
303 zfs_file_fsync(zfs_file_t *filp, int flags)
313 * May enter XFS which generates a warning when PF_FSTRANS is set.
314 * To avoid this the flag is cleared over vfs_sync() and then reset.
316 fstrans = __spl_pf_fstrans_check();
318 current->flags &= ~(__SPL_PF_FSTRANS);
320 error = -vfs_fsync(filp, datasync);
323 current->flags |= __SPL_PF_FSTRANS;
329 * fallocate - allocate or free space on disk
332 * mode (non-standard options for hole punching etc)
333 * offset - offset to start allocating or freeing from
334 * len - length to free / allocate
339 zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
342 * May enter XFS which generates a warning when PF_FSTRANS is set.
343 * To avoid this the flag is cleared over vfs_sync() and then reset.
345 int fstrans = __spl_pf_fstrans_check();
347 current->flags &= ~(__SPL_PF_FSTRANS);
350 * When supported by the underlying file system preferentially
351 * use the fallocate() callback to preallocate the space.
353 int error = EOPNOTSUPP;
354 if (fp->f_op->fallocate)
355 error = fp->f_op->fallocate(fp, mode, offset, len);
358 current->flags |= __SPL_PF_FSTRANS;
364 * Request current file pointer offset
366 * fp - pointer to file
368 * Returns current file offset.
371 zfs_file_off(zfs_file_t *fp)
377 * Request file pointer private data
379 * fp - pointer to file
381 * Returns pointer to file private data.
384 zfs_file_private(zfs_file_t *fp)
386 return (fp->private_data);
392 * path - fully qualified file path
394 * Returns 0 on success.
399 zfs_file_unlink(const char *path)
405 * Get reference to file pointer
407 * fd - input file descriptor
408 * fpp - pointer to file pointer
410 * Returns 0 on success EBADF on failure.
413 zfs_file_get(int fd, zfs_file_t **fpp)
427 * Drop reference to file pointer
429 * fd - input file descriptor
436 if ((fp = fget(fd)) != NULL) {