Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

samples/vfs: add STATMOUNT_MNT_{G,U}IDMAP

Illustrate how to use STATMOUNT_MNT_{G,U}IDMAP.

Link: https://lore.kernel.org/r/20250204-work-mnt_idmap-statmount-v2-4-007720f39f2e@kernel.org
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>

+37 -3
+13 -1
samples/vfs/samples-vfs.h
··· 42 42 __u32 opt_array; /* [str] Array of nul terminated fs options */ 43 43 __u32 opt_sec_num; /* Number of security options */ 44 44 __u32 opt_sec_array; /* [str] Array of nul terminated security options */ 45 - __u64 __spare2[46]; 45 + __u32 mnt_uidmap_num; /* Number of uid mappings */ 46 + __u32 mnt_uidmap; /* [str] Array of uid mappings */ 47 + __u32 mnt_gidmap_num; /* Number of gid mappings */ 48 + __u32 mnt_gidmap; /* [str] Array of gid mappings */ 49 + __u64 __spare2[44]; 46 50 char str[]; /* Variable size part containing strings */ 47 51 }; 48 52 ··· 160 156 161 157 #ifndef STATX_MNT_ID_UNIQUE 162 158 #define STATX_MNT_ID_UNIQUE 0x00004000U /* Want/got extended stx_mount_id */ 159 + #endif 160 + 161 + #ifndef STATMOUNT_MNT_UIDMAP 162 + #define STATMOUNT_MNT_UIDMAP 0x00002000U /* Want/got uidmap... */ 163 + #endif 164 + 165 + #ifndef STATMOUNT_MNT_GIDMAP 166 + #define STATMOUNT_MNT_GIDMAP 0x00004000U /* Want/got gidmap... */ 163 167 #endif 164 168 165 169 #ifndef MOUNT_ATTR_RDONLY
+24 -2
samples/vfs/test-list-all-mounts.c
··· 128 128 STATMOUNT_MNT_POINT | 129 129 STATMOUNT_MNT_NS_ID | 130 130 STATMOUNT_MNT_OPTS | 131 - STATMOUNT_FS_TYPE, 0); 131 + STATMOUNT_FS_TYPE | 132 + STATMOUNT_MNT_UIDMAP | 133 + STATMOUNT_MNT_GIDMAP, 0); 132 134 if (!stmnt) { 133 135 printf("Failed to statmount(%" PRIu64 ") in mount namespace(%" PRIu64 ")\n", 134 136 (uint64_t)last_mnt_id, (uint64_t)info.mnt_ns_id); 135 137 continue; 136 138 } 137 139 138 - printf("mnt_id:\t\t%" PRIu64 "\nmnt_parent_id:\t%" PRIu64 "\nfs_type:\t%s\nmnt_root:\t%s\nmnt_point:\t%s\nmnt_opts:\t%s\n\n", 140 + printf("mnt_id:\t\t%" PRIu64 "\nmnt_parent_id:\t%" PRIu64 "\nfs_type:\t%s\nmnt_root:\t%s\nmnt_point:\t%s\nmnt_opts:\t%s\n", 139 141 (uint64_t)stmnt->mnt_id, 140 142 (uint64_t)stmnt->mnt_parent_id, 141 143 (stmnt->mask & STATMOUNT_FS_TYPE) ? stmnt->str + stmnt->fs_type : "", 142 144 (stmnt->mask & STATMOUNT_MNT_ROOT) ? stmnt->str + stmnt->mnt_root : "", 143 145 (stmnt->mask & STATMOUNT_MNT_POINT) ? stmnt->str + stmnt->mnt_point : "", 144 146 (stmnt->mask & STATMOUNT_MNT_OPTS) ? stmnt->str + stmnt->mnt_opts : ""); 147 + 148 + if (stmnt->mask & STATMOUNT_MNT_UIDMAP) { 149 + const char *idmap = stmnt->str + stmnt->mnt_uidmap; 150 + 151 + for (size_t idx = 0; idx < stmnt->mnt_uidmap_num; idx++) { 152 + printf("mnt_uidmap[%lu]:\t%s\n", idx, idmap); 153 + idmap += strlen(idmap) + 1; 154 + } 155 + } 156 + 157 + if (stmnt->mask & STATMOUNT_MNT_GIDMAP) { 158 + const char *idmap = stmnt->str + stmnt->mnt_gidmap; 159 + 160 + for (size_t idx = 0; idx < stmnt->mnt_gidmap_num; idx++) { 161 + printf("mnt_gidmap[%lu]:\t%s\n", idx, idmap); 162 + idmap += strlen(idmap) + 1; 163 + } 164 + } 165 + 166 + printf("\n"); 145 167 146 168 free(stmnt); 147 169 }