Re: [PATCH] Initial implementation of posix_fallocate(2)

2015-09-13 Thread Dimitris Papastamos
Hi,

A slightly updated patch.  Removed some checks that were
redundant.

Index: kern/init_sysent.c
===
RCS file: /cvs/src/sys/kern/init_sysent.c,v
retrieving revision 1.171
diff -u -p -r1.171 init_sysent.c
--- kern/init_sysent.c  9 Sep 2015 17:57:57 -   1.171
+++ kern/init_sysent.c  13 Sep 2015 15:47:11 -
@@ -1,4 +1,4 @@
-/* $OpenBSD: init_sysent.c,v 1.171 2015/09/09 17:57:57 deraadt Exp $   
*/
+/* $OpenBSD$   */
 
 /*
  * System call switch table.
@@ -751,5 +751,7 @@ struct sysent sysent[] = {
sys___set_tcb },/* 329 = __set_tcb */
{ 0, 0, SY_NOLOCK | 0,
sys___get_tcb },/* 330 = __get_tcb */
+   { 3, s(struct sys_posix_fallocate_args), 0,
+   sys_posix_fallocate },  /* 331 = posix_fallocate */
 };
 
Index: kern/syscalls.c
===
RCS file: /cvs/src/sys/kern/syscalls.c,v
retrieving revision 1.170
diff -u -p -r1.170 syscalls.c
--- kern/syscalls.c 9 Sep 2015 17:57:57 -   1.170
+++ kern/syscalls.c 13 Sep 2015 15:47:11 -
@@ -1,4 +1,4 @@
-/* $OpenBSD: syscalls.c,v 1.170 2015/09/09 17:57:57 deraadt Exp $  */
+/* $OpenBSD$   */
 
 /*
  * System call names.
@@ -393,4 +393,5 @@ char *syscallnames[] = {
"#328 (obsolete __tfork51)",/* 328 = obsolete __tfork51 */
"__set_tcb",/* 329 = __set_tcb */
"__get_tcb",/* 330 = __get_tcb */
+   "posix_fallocate",  /* 331 = posix_fallocate */
 };
Index: kern/syscalls.master
===
RCS file: /cvs/src/sys/kern/syscalls.master,v
retrieving revision 1.158
diff -u -p -r1.158 syscalls.master
--- kern/syscalls.master9 Sep 2015 17:56:59 -   1.158
+++ kern/syscalls.master13 Sep 2015 15:47:12 -
@@ -561,3 +561,4 @@
 328OBSOL   __tfork51
 329STD NOLOCK  { void sys___set_tcb(void *tcb); }
 330STD NOLOCK  { void *sys___get_tcb(void); }
+331STD { int sys_posix_fallocate(int fd, off_t offset, off_t 
len); }
Index: kern/vfs_syscalls.c
===
RCS file: /cvs/src/sys/kern/vfs_syscalls.c,v
retrieving revision 1.227
diff -u -p -r1.227 vfs_syscalls.c
--- kern/vfs_syscalls.c 31 Aug 2015 16:13:11 -  1.227
+++ kern/vfs_syscalls.c 13 Sep 2015 15:47:12 -
@@ -3023,3 +3023,40 @@ sys_pwritev(struct proc *p, void *v, reg
1, , retval));
 }
 
+int
+sys_posix_fallocate(struct proc *p, void *v, register_t *retval)
+{
+   struct sys_posix_fallocate_args /* {
+   syscallarg(int) fd;
+   syscallarg(off_t) offset;
+   syscallarg(off_t) len;
+   } */ *uap = v;
+   struct vnode *vp;
+   struct file *fp;
+   int error;
+
+   if (SCARG(uap, offset) < 0 || SCARG(uap, len) < 0)
+   return (EINVAL);
+   if (SCARG(uap, offset) > LLONG_MAX - SCARG(uap, len))
+   return (EFBIG);
+
+   if ((error = getvnode(p, SCARG(uap, fd), )) != 0)
+   return (error);
+   if ((fp->f_flag & FWRITE) == 0) {
+   error = EBADF;
+   goto bad;
+   }
+   vp = fp->f_data;
+   vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
+   if (vp->v_type == VFIFO)
+   error = ESPIPE;
+   else if (vp->v_type != VREG)
+   error = ENODEV;
+   else if ((error = vn_writechk(vp)) == 0)
+   error = vn_fallocate(vp, SCARG(uap, offset),
+SCARG(uap, len), p);
+   VOP_UNLOCK(vp, 0, p);
+bad:
+   FRELE(fp, p);
+   return (error);
+}
Index: kern/vfs_vnops.c
===
RCS file: /cvs/src/sys/kern/vfs_vnops.c,v
retrieving revision 1.82
diff -u -p -r1.82 vfs_vnops.c
--- kern/vfs_vnops.c1 May 2015 01:30:58 -   1.82
+++ kern/vfs_vnops.c13 Sep 2015 15:47:12 -
@@ -573,3 +573,70 @@ vn_isunder(struct vnode *lvp, struct vno
 
return (0);
 }
+
+int
+vn_fallocate(struct vnode *vp, off_t offset, off_t len, struct proc *p)
+{
+   struct vattr va;
+   uint8_t *buf;
+   off_t cur, fsize;
+   long blksize;
+   size_t resid;
+   int error;
+
+   if ((error = VOP_GETATTR(vp, , p->p_ucred, p)) != 0)
+   return (error);
+   fsize = va.va_size;
+   blksize = va.va_blocksize;
+
+   if (offset + len > fsize) {
+   /*
+* Check if the filesystem supports the resulting
+* filesystem size.
+*/
+   VATTR_NULL();
+   va.va_size = offset + len;
+   if ((error = VOP_SETATTR(vp, , p->p_ucred, p)) != 0)
+   return (error);
+
+   /* 

Re: [PATCH] Initial implementation of posix_fallocate(2)

2015-09-12 Thread Dimitris Papastamos
On Sat, Sep 12, 2015 at 06:57:48PM +0100, Dimitris Papastamos wrote:
> + switch (fp->f_type) {
> + case DTYPE_VNODE:
> + break;
> + case DTYPE_PIPE:
> + case VFIFO:

VFIFO shouldn't be here, I have moved it further down after vn_lock
to check it against v_type.