a issue about getting a devfs node's fullpath

Zhichao1.Li at dell.com Zhichao1.Li at dell.com
Fri Oct 11 02:23:24 UTC 2019


Hello
Thank you for your time
Unfortunately it does not work, how about this way?

--- a/src/sys/fs/devfs/devfs_vnops.c
+++ b/src/sys/fs/devfs/devfs_vnops.c
@@ -293,7 +293,14 @@ devfs_vptocnp(struct vop_vptocnp_args *ap)
                }
                bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
                    strlen(dd->de_cdp->cdp_c.si_name));
-               de = dd->de_dir;
+               /*
+                * when dealing with VCHR
+                * the element 'si_name' already
+                * holds the full path string
+                * except rootdir, so just go
+                * to the rootdir
+                */
+               de = dmp->dm_rootdir;
        } else if (vp->v_type == VDIR) {
                if (dd == dmp->dm_rootdir) {
                        *dvp = vp;
@@ -307,13 +314,17 @@ devfs_vptocnp(struct vop_vptocnp_args *ap)
                }
                bcopy(dd->de_dirent->d_name, buf + i,
                    dd->de_dirent->d_namlen);
-               de = dd;
+               /*
+                * when dealing with VDIR
+                * get its parent
+                */
+               de = devfs_parent_dirent(dd);
        } else {
                error = ENOENT;
                goto finished;
        }
        *buflen = i;
-       de = devfs_parent_dirent(de);
+       /*no need to get another parent*/
        if (de == NULL) {
                error = ENOENT;
                goto finished;

another thing is this func is called by many modules, I want to ensure modifying the func this way will not impact other modules and not bring some bugs, would you be so kind to help me out?

thank you 

-----Original Message-----
From: Konstantin Belousov <kib at freebsd.org> 
Sent: Thursday, October 10, 2019 11:40 PM
To: Li, Zhichao1 <Zhichao_Li1 at Dell.com>
Cc: freebsd-fs at freebsd.org; freebsd-drivers at freebsd.org; Hu, Shunchao <Shunchao_Hu at Dell.com>
Subject: Re: a issue about getting a devfs node's fullpath


[EXTERNAL EMAIL] 

On Thu, Oct 10, 2019 at 05:49:55AM +0000, Zhichao1.Li at dell.com wrote:
> Dear freebsd developers
> I know you're swamped, so I'll be brief.
> I am trying to get a node's full under /dev by calling the function 'vn_fullpath', when dealing with things like '/dev/null' or '/dev/usb/1.0.1', it works well.
> However when dealing a node under more than 2 sub directories (e.g. /dev/bus/usb/001/002)  which I made by calling 'make_dev_s' , it goes a little bit different, I got a string "/dev/bus/usb/bus/usb/001/002"
> And I found the function 'devfs_vptocnp' gets the string wrongly when 
> dealing multi slashes path string, I have remark the code as followed and put some comments would you please take a look, and tell me what the purpose this func pass the string that way?

Try this.  Not tested.

diff --git a/sys/fs/devfs/devfs_vnops.c b/sys/fs/devfs/devfs_vnops.c index 3b80c68e309..f20b466d88b 100644
--- a/sys/fs/devfs/devfs_vnops.c
+++ b/sys/fs/devfs/devfs_vnops.c
@@ -284,38 +284,27 @@ devfs_vptocnp(struct vop_vptocnp_args *ap)
 	if (error != 0)
 		return (error);
 
-	i = *buflen;
+	if (vp->v_type != VCHR && vp->v_type != VDIR) {
+		error = ENOENT;
+		goto finished;
+	}
+
 	dd = vp->v_data;
+	if (vp->v_type == VDIR && dd == dmp->dm_rootdir) {
+		*dvp = vp;
+		vref(*dvp);
+		goto finished;
+	}
 
-	if (vp->v_type == VCHR) {
-		i -= strlen(dd->de_cdp->cdp_c.si_name);
-		if (i < 0) {
-			error = ENOMEM;
-			goto finished;
-		}
-		bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
-		    strlen(dd->de_cdp->cdp_c.si_name));
-		de = dd->de_dir;
-	} else if (vp->v_type == VDIR) {
-		if (dd == dmp->dm_rootdir) {
-			*dvp = vp;
-			vref(*dvp);
-			goto finished;
-		}
-		i -= dd->de_dirent->d_namlen;
-		if (i < 0) {
-			error = ENOMEM;
-			goto finished;
-		}
-		bcopy(dd->de_dirent->d_name, buf + i,
-		    dd->de_dirent->d_namlen);
-		de = dd;
-	} else {
-		error = ENOENT;
+	i = *buflen;
+	i -= dd->de_dirent->d_namlen;
+	if (i < 0) {
+		error = ENOMEM;
 		goto finished;
 	}
+	bcopy(dd->de_dirent->d_name, buf + i, dd->de_dirent->d_namlen);
 	*buflen = i;
-	de = devfs_parent_dirent(de);
+	de = devfs_parent_dirent(dd);
 	if (de == NULL) {
 		error = ENOENT;
 		goto finished;


More information about the freebsd-fs mailing list