From owner-freebsd-hackers@freebsd.org Mon Feb 19 21:55:43 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 10295F01DC1 for ; Mon, 19 Feb 2018 21:55:43 +0000 (UTC) (envelope-from wjw@digiware.nl) Received: from smtp.digiware.nl (smtp.digiware.nl [176.74.240.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 9B3FB6D823 for ; Mon, 19 Feb 2018 21:55:42 +0000 (UTC) (envelope-from wjw@digiware.nl) Received: from router.digiware.nl (localhost.digiware.nl [127.0.0.1]) by smtp.digiware.nl (Postfix) with ESMTP id 9AF492EE9E; Mon, 19 Feb 2018 22:55:40 +0100 (CET) X-Virus-Scanned: amavisd-new at digiware.com Received: from smtp.digiware.nl ([127.0.0.1]) by router.digiware.nl (router.digiware.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3YE4JaVnocVI; Mon, 19 Feb 2018 22:55:40 +0100 (CET) Received: from [192.168.10.67] (opteron [192.168.10.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.digiware.nl (Postfix) with ESMTPSA id 0D6E52EE9D; Mon, 19 Feb 2018 22:55:40 +0100 (CET) Subject: Re: Using fstatfs on a ZFS disk To: "Rodney W. Grimes" , rb@gid.co.uk Cc: FreeBSD Hackers References: <201802191833.w1JIXhVL078022@pdx.rh.CN85.dnsmgr.net> From: Willem Jan Withagen Message-ID: <82ef8d5d-ef62-f746-f415-728e34257924@digiware.nl> Date: Mon, 19 Feb 2018 22:55:37 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <201802191833.w1JIXhVL078022@pdx.rh.CN85.dnsmgr.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: nl Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Feb 2018 21:55:43 -0000 On 19-2-2018 19:33, Rodney W. Grimes wrote: >> Hi, >> >>> On 19 Feb 2018, at 15:50, Willem Jan Withagen wrote: >>> >>> On 19-2-2018 16:00, Bob Bishop wrote: >>>> Hi, >>>>> On 19 Feb 2018, at 14:48, Willem Jan Withagen wrote: >>>>> >>>>> Hi, >>>>> >>>>> I'm trying to find the values of the returned f_type for ZFS >>>>> in the fstatfs call when a file is on ZFS.... >>>>> >>>>> But I have not yet found the definitions of the ENUMS that >>>>> would fill that value... Let alone the value for ZFS. >>>> I chased this particular wild goose myself recently. It?s FS_... in /usr/include/sys/disklabel,h that you want. >>> >>> Hi Bob, >>> >>> I grepped on MAGIC and FS, but the combo did not deliver anything useful. So this is already more that I found. >>> I did get: >>> /usr/include/ufs/ffs/fs.h:#define FS_UFS1_MAGIC 0x011954 /* UFS1 fast filesystem magic number */ >>> /usr/include/ufs/ffs/fs.h:#define FS_UFS2_MAGIC 0x19540119 /* UFS2 fast filesystem magic number */ >>> /usr/include/ufs/ffs/fs.h:#define FS_BAD_MAGIC 0x19960408 /* UFS incomplete newfs magic number */ >> >> Those I believe are magic numbers for UFS superblocks... >> >>> So I was looking for something like: FS_ZFS_MAGIC >> >> ... so you won?t find that. >> >>> disklabel.h contains: >>> #ifdef FSTYPENAMES >>> static const char *fstypenames[] = { >>> >>> And further search: >>> /usr/include/sys/disk/bsd.h:#define FS_ZFS 27 /* Sun's ZFS */ >>> >>> Running: >>> #include "stdio.h" >>> >>> #include >>> #include >>> >>> int main() { >>> struct statfs fstr; >>> char * str; >>> >>> str = "/tmp"; >>> statfs(str, &fstr); >>> printf("%s, ftype: 0x%x.\n", str, fstr.f_type); >>> } >>> results in: >>> /tmp, ftype: 0xde. >>> >>> Now 0xde != 27, so the question is, where is this 0xde specified. >>> And more important is this f_type constant over all FreeBSD ZFS filesystems? >> >> You got me. And a quick look at sys/kern/vfs_syscalls.c doesn?t help except to imply that the type is set when the filesystem is mounted. I have no idea where 0xde comes from. > > Could that 0xde be the start of 0xdeadcode? > > 0xde is 222 decimal, that does not ring a bell for me either. Well the VSTAT node gets setup during vfs_mount_alloc() which copies data from: /* * Filesystem configuration information. One of these exists for each * type of filesystem supported by the kernel. These are searched at * mount time to identify the requested filesystem. * * XXX: Never change the first two arguments! */ struct vfsconf { u_int vfc_version; /* ABI version number */ char vfc_name[MFSNAMELEN]; /* filesystem type name */ struct vfsops *vfc_vfsops; /* filesystem operations vector */ int vfc_typenum; /* historic filesystem type number */ int vfc_refcount; /* number mounted of this type */ int vfc_flags; /* permanent flags */ struct vfsoptdecl *vfc_opts; /* mount options */ TAILQ_ENTRY(vfsconf) vfc_list; /* list of vfscons */ }; And the "historic filesystem type number" is a worrying comment. As it turns out the type is calculated/hashed in vfs_init: ==== if (vfs_typenumhash != 0) { /* * Calculate a hash on vfc_name to use for vfc_typenum. Unless * all of 1<->255 are assigned, it is limited to 8bits since * that is what ZFS uses from vfc_typenum and is also the * preferred range for vfs_getnewfsid(). */ hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT); hashval &= 0xff; secondpass = 0; do { /* Look for and fix any collision. */ TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) { if (hashval == tvfc->vfc_typenum) { if (hashval == 255 && secondpass == 0) { hashval = 1; secondpass = 1; } else hashval++; break; } } } while (tvfc != NULL); vfc->vfc_typenum = hashval; if (vfc->vfc_typenum >= maxvfsconf) maxvfsconf = vfc->vfc_typenum + 1; So the f_type is sort of stable over time, unless een new type creates a collision and things start moving. Made a small test program: /tmp, ftype: 0xde, name: zfs. /home/wjw, ftype: 0x3a, name: nfs. /home/wjw/.tcshrc, ftype: 0x3a, name: nfs. /dev/random, ftype: 0x71, name: devfs. So I guess I'm going to '#define FS_ZFS_TYPE 0xde' --WjW