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
);
282 #elif defined(HAVE_3ARGS_VFS_GETATTR)
283 rc
= vfs_getattr(filp
->f_path
.mnt
, filp
->f_dentry
, &stat
);
285 #error "No available vfs_getattr()"
290 zfattr
->zfa_size
= stat
.size
;
291 zfattr
->zfa_mode
= stat
.mode
;
299 * filp - file pointer
300 * flags - O_SYNC and or O_DSYNC
302 * Returns 0 on success or error code of underlying sync call on failure.
305 zfs_file_fsync(zfs_file_t
*filp
, int flags
)
315 * May enter XFS which generates a warning when PF_FSTRANS is set.
316 * To avoid this the flag is cleared over vfs_sync() and then reset.
318 fstrans
= __spl_pf_fstrans_check();
320 current
->flags
&= ~(__SPL_PF_FSTRANS
);
322 error
= -vfs_fsync(filp
, datasync
);
325 current
->flags
|= __SPL_PF_FSTRANS
;
331 * fallocate - allocate or free space on disk
334 * mode (non-standard options for hole punching etc)
335 * offset - offset to start allocating or freeing from
336 * len - length to free / allocate
341 zfs_file_fallocate(zfs_file_t
*fp
, int mode
, loff_t offset
, loff_t len
)
344 * May enter XFS which generates a warning when PF_FSTRANS is set.
345 * To avoid this the flag is cleared over vfs_sync() and then reset.
347 int fstrans
= __spl_pf_fstrans_check();
349 current
->flags
&= ~(__SPL_PF_FSTRANS
);
352 * When supported by the underlying file system preferentially
353 * use the fallocate() callback to preallocate the space.
355 int error
= EOPNOTSUPP
;
356 if (fp
->f_op
->fallocate
)
357 error
= fp
->f_op
->fallocate(fp
, mode
, offset
, len
);
360 current
->flags
|= __SPL_PF_FSTRANS
;
366 * Request current file pointer offset
368 * fp - pointer to file
370 * Returns current file offset.
373 zfs_file_off(zfs_file_t
*fp
)
379 * Request file pointer private data
381 * fp - pointer to file
383 * Returns pointer to file private data.
386 zfs_file_private(zfs_file_t
*fp
)
388 return (fp
->private_data
);
394 * path - fully qualified file path
396 * Returns 0 on success.
401 zfs_file_unlink(const char *path
)
407 * Get reference to file pointer
409 * fd - input file descriptor
411 * Returns pointer to file struct or NULL
420 * Drop reference to file pointer
422 * fp - input file struct pointer
425 zfs_file_put(zfs_file_t
*fp
)