Path: blob/main/vendor/golang.org/x/sys/unix/dev_linux.go
2880 views
// Copyright 2017 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34// Functions to access/create device major and minor numbers matching the5// encoding used by the Linux kernel and glibc.6//7// The information below is extracted and adapted from bits/sysmacros.h in the8// glibc sources:9//10// dev_t in glibc is 64-bit, with 32-bit major and minor numbers. glibc's11// default encoding is MMMM Mmmm mmmM MMmm, where M is a hex digit of the major12// number and m is a hex digit of the minor number. This is backward compatible13// with legacy systems where dev_t is 16 bits wide, encoded as MMmm. It is also14// backward compatible with the Linux kernel, which for some architectures uses15// 32-bit dev_t, encoded as mmmM MMmm.1617package unix1819// Major returns the major component of a Linux device number.20func Major(dev uint64) uint32 {21major := uint32((dev & 0x00000000000fff00) >> 8)22major |= uint32((dev & 0xfffff00000000000) >> 32)23return major24}2526// Minor returns the minor component of a Linux device number.27func Minor(dev uint64) uint32 {28minor := uint32((dev & 0x00000000000000ff) >> 0)29minor |= uint32((dev & 0x00000ffffff00000) >> 12)30return minor31}3233// Mkdev returns a Linux device number generated from the given major and minor34// components.35func Mkdev(major, minor uint32) uint64 {36dev := (uint64(major) & 0x00000fff) << 837dev |= (uint64(major) & 0xfffff000) << 3238dev |= (uint64(minor) & 0x000000ff) << 039dev |= (uint64(minor) & 0xffffff00) << 1240return dev41}424344