From jonny@jonny.eng.br  Mon Jun 28 22:57:35 2004
Return-Path: <jonny@jonny.eng.br>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 93DBF16A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Jun 2004 22:57:35 +0000 (GMT)
Received: from coe.ufrj.br (roma.coe.ufrj.br [146.164.53.65])
	by mx1.FreeBSD.org (Postfix) with ESMTP id D77D343D45
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Jun 2004 22:57:34 +0000 (GMT)
	(envelope-from jonny@jonny.eng.br)
Received: from localhost (localhost [127.0.0.1])
	by coe.ufrj.br (Postfix) with ESMTP id E14BD1A326A8
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Jun 2004 19:57:27 -0300 (BRT)
Received: from coe.ufrj.br ([146.164.53.65])
 by localhost (roma.coe.ufrj.br [127.0.0.1]) (amavisd-new, port 10024)
 with ESMTP id 50165-04 for <FreeBSD-gnats-submit@freebsd.org>;
 Mon, 28 Jun 2004 19:57:25 -0300 (BRT)
Received: by coe.ufrj.br (Postfix, from userid 2000)
	id 2319A1A326A7; Mon, 28 Jun 2004 19:57:25 -0300 (BRT)
Message-Id: <20040628225725.2319A1A326A7@coe.ufrj.br>
Date: Mon, 28 Jun 2004 19:57:25 -0300 (BRT)
From: Joao Carlos Mendes Luis <jonny@jonny.eng.br>
Reply-To: Joao Carlos Mendes Luis <jonny@jonny.eng.br>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Patches to mknod(2) behave more like Linux
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         68459
>Category:       kern
>Synopsis:       [vfs] [patch] Patches to mknod(2) behave more like Linux
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    rwatson
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jun 28 23:00:30 GMT 2004
>Closed-Date:    Sun Aug 03 16:11:07 UTC 2008
>Last-Modified:  Sun Aug 03 16:11:07 UTC 2008
>Originator:     Joao Carlos Mendes Luis
>Release:        FreeBSD 4.10-BETA i386
>Organization:
Barradev Tecnologia Ltda.
>Environment:
System: FreeBSD roma.coe.ufrj.br 4.10-BETA FreeBSD 4.10-BETA #4: Fri Apr 16 03:25:06 BRT 2004 jonny@roma.coe.ufrj.br:/usr/cvsup/RELENG_4/src/sys/compile/ROMA i386


FreeBSD 4-stable VFS subsystem

>Description:

Here a copy of an email sent to -hackers:

    Ive done some changes to the kernel mknod(2) handling to make it more compatible to the Linux behaviour, allowing one to create files of type VREG, VSOCK and VFIFO with mknod(2).  I already know that BSD way is that one should use creat(2), bind(2) and mkfifo(2) functions for these, but, if all these are indeed vnodes, why bother if one use mkfifo(2) or mknod(2) to create fifos?? The only vnode type I think I left out is VDIR, because it needs to be initialized with "." and "..".

    I first came with this idea using rsync for a full backup, noticing it could not copy fifos or sockets.  I could simply fix rsync, but I thought "fixing" FreeBSD would be a little bit funnier!  

    To allow your appreciation, here are my patches.  I am not an expert in VFS coding, so it should probably be checked by an expert before commiting.  All I can say is that it worked for me.

>How-To-Repeat:

Here's a small test program:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

main()
{
  mknod( "test1", S_IFSOCK|0777, 0 );
  perror( "mknod" );
  errno=0;
  mknod( "test2", S_IFIFO|0777, 0 );
  perror( "mknod" );
  errno=0;
  mknod( "test3", S_IFREG|0777, 0 );
  perror( "mknod" );
  errno=0;
  mknod( "test4", S_IFBLK|0777, 0 );
  perror( "mknod" );
  errno=0;
  mknod( "test5", S_IFCHR|0777, 0 );
  perror( "mknod" );
  errno=0;
}

>Fix:

The following patches should be verified by a VFS expert.

A merge to -current should follow, of course.

--- RELENG_4/src/sys/kern/vfs_syscalls.c.orig	Mon Jun 28 18:11:58 2004
+++ RELENG_4/src/sys/kern/vfs_syscalls.c	Mon Jun 28 19:26:31 2004
@@ -1187,14 +1187,20 @@
 	register struct vnode *vp;
 	struct vattr vattr;
 	int error;
-	int whiteout = 0;
+	int vtype;
 	struct nameidata nd;
 
-	switch (SCARG(uap, mode) & S_IFMT) {
+	vtype = SCARG(uap, mode) & S_IFMT;
+	switch ( vtype ) {
 	case S_IFCHR:
 	case S_IFBLK:
 		error = suser(p);
 		break;
+	case S_IFREG:
+	case S_IFIFO:
+	case S_IFSOCK:
+		error = 0;
+		break;
 	default:
 		error = suser_xxx(0, p, PRISON_ROOT);
 		break;
@@ -1202,7 +1208,7 @@
 	if (error)
 		return (error);
 	bwillwrite();
-	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
+	NDINIT(&nd, CREATE, LOCKPARENT, (vtype==S_IFSOCK)?UIO_SYSSPACE:UIO_USERSPACE, SCARG(uap, path), p);
 	if ((error = namei(&nd)) != 0)
 		return (error);
 	vp = nd.ni_vp;
@@ -1212,9 +1218,8 @@
 		VATTR_NULL(&vattr);
 		vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
 		vattr.va_rdev = SCARG(uap, dev);
-		whiteout = 0;
 
-		switch (SCARG(uap, mode) & S_IFMT) {
+		switch ( vtype ) {
 		case S_IFMT:	/* used by badsect to flag bad sectors */
 			vattr.va_type = VBAD;
 			break;
@@ -1224,8 +1229,17 @@
 		case S_IFBLK:
 			vattr.va_type = VBLK;
 			break;
+		case S_IFREG:
+			vattr.va_type = VREG;
+			break;
+		case S_IFSOCK:
+			vattr.va_type = VSOCK;
+			break;
+		case S_IFIFO:
+			vattr.va_type = VFIFO;
+			break;
 		case S_IFWHT:
-			whiteout = 1;
+			/* NOTHING */
 			break;
 		default:
 			error = EINVAL;
@@ -1234,9 +1248,18 @@
 	}
 	if (!error) {
 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
-		if (whiteout)
+		switch ( vtype ) {
+		    case S_IFWHT:
 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
-		else {
+			break;
+		    case S_IFREG:
+		    case S_IFSOCK:
+			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
+						&nd.ni_cnd, &vattr);
+			if (error == 0)
+				vput(nd.ni_vp);
+		        break;
+		    default:
 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
 						&nd.ni_cnd, &vattr);
 			if (error == 0)


>Release-Note:
>Audit-Trail:

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/68459: commit references a PR
Date: Sun, 22 Jun 2008 21:51:49 +0000 (UTC)

 rwatson     2008-06-22 21:51:32 UTC
 
   FreeBSD src repository
 
   Modified files:
     sys/kern             vfs_syscalls.c 
   Log:
   SVN rev 179936 on 2008-06-22 21:51:32Z by rwatson
   
   If S_IFIFO is passed to mknod(2), invoke kern_mkfifoat(9) to create a
   FIFO, as required by SUSv3.  No specific privilege check is performed
   in this case, as FIFOs may be created by unprivileged processes
   (subject to the normal file system name space restrictions that may be
   in place).
   
   Unlike the Apple implementation, we reject requests to create a FIFO
   using mknod(2) if there is a non-zero dev argument to the system call,
   which is permitted by the Open Group specification ("... undefined
   ...").  We might want to revise this if we find it causes
   compatibility problems for applications in practice.
   
   PR:             kern/74242, kern/68459
   Obtained from:  Apple, Inc.
   MFC after:      3 weeks
   
   Revision  Changes    Path
   1.454     +4 -0      src/sys/kern/vfs_syscalls.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: gavin 
State-Changed-When: Thu Jun 26 10:41:40 UTC 2008 
State-Changed-Why:  
Fixed in HEAD 


Responsible-Changed-From-To: freebsd-bugs->rwatson 
Responsible-Changed-By: gavin 
Responsible-Changed-When: Thu Jun 26 10:41:40 UTC 2008 
Responsible-Changed-Why:  
rwatson committed the fix 

http://www.freebsd.org/cgi/query-pr.cgi?pr=68459 

From: Robert Watson <rwatson@FreeBSD.org>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/68459: [vfs] [patch] Patches to mknod(2) behave more like
 Linux
Date: Thu, 26 Jun 2008 14:55:45 +0100 (BST)

 On Thu, 26 Jun 2008, gavin@FreeBSD.org wrote:
 
 > Synopsis: [vfs] [patch] Patches to mknod(2) behave more like Linux
 >
 > State-Changed-From-To: open->patched
 > State-Changed-By: gavin
 > State-Changed-When: Thu Jun 26 10:41:40 UTC 2008
 > State-Changed-Why:
 > Fixed in HEAD
 >
 > Responsible-Changed-From-To: freebsd-bugs->rwatson
 > Responsible-Changed-By: gavin
 > Responsible-Changed-When: Thu Jun 26 10:41:40 UTC 2008
 > Responsible-Changed-Why:
 > rwatson committed the fix
 >
 > http://www.freebsd.org/cgi/query-pr.cgi?pr=68459
 
 Notice that this only addresses the report with respect to fifos, not sockets, 
 whiteout, and regular files.
 
 Robert N M Watson
 Computer Laboratory
 University of Cambridge
State-Changed-From-To: patched->closed 
State-Changed-By: rwatson 
State-Changed-When: Sun Aug 3 16:09:30 UTC 2008 
State-Changed-Why:  
While a fix to some portion of this has been committed, close rather than 
MFC while we decide if it sticks.  With the patch we now offer 
compatibility with Mac OS X, but in excess of the requirements of SUSv3, 
so we might want to revert this change for reasons of cleanliness. 
Thanks for the report! 


http://www.freebsd.org/cgi/query-pr.cgi?pr=68459 
>Unformatted:
