From 348f0c61efc0f35aedcd0e44bc9fa7bf7f067942 Mon Sep 17 00:00:00 2001 From: Tobias Geerinckx-Rice Date: Sun, 12 Sep 2021 18:07:54 +0200 Subject: syscalls: Deduplicate device number conversion. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * guix/cpio.scm (device-number, device->major+minor): Move to, and subsequently import from, … * guix/build/syscalls.scm (device-number, device-number->major+minor): …here. Note the slight name change. (mounts): Replace 16-bit open code with a DEVICE-NUMBER call. * gnu/build/linux-boot.scm (device-number): Remove duplicate 16-bit implementation in favour of the one above. (resume-if-hibernated): Reuse DEVICE-NUMBER->MAJOR+MINOR. --- guix/build/syscalls.scm | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'guix/build') diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm index ac1b0c2eea..99a3b45004 100644 --- a/guix/build/syscalls.scm +++ b/guix/build/syscalls.scm @@ -7,6 +7,7 @@ ;;; Copyright © 2020 Julien Lepiller ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen ;;; Copyright © 2021 Chris Marusich +;;; Copyright © 2021 Tobias Geerinckx-Rice ;;; ;;; This file is part of GNU Guix. ;;; @@ -56,6 +57,9 @@ (define-module (guix build syscalls) restart-on-EINTR + device-number + device-number->major+minor + mount? mount-device-number mount-source @@ -448,6 +452,29 @@ (define-as-needed proc (lambda* (args ...) body ...))) (module-define! (current-module) 'variable value) (module-export! (current-module) '(variable))))))) + +;;; +;;; Block devices. +;;; + +;; Convert between major:minor pairs and packed ‘device number’ representation. +;; XXX These aren't syscalls, but if you squint very hard they are part of the +;; FFI or however you want to justify me not finding a better fit… :-) +(define (device-number major minor) ; see glibc's + "Return the device number for the device with MAJOR and MINOR, for use as +the last argument of `mknod'." + (logior (ash (logand #x00000fff major) 8) + (ash (logand #xfffff000 major) 32) + (logand #x000000ff minor) + (ash (logand #xffffff00 minor) 12))) + +(define (device-number->major+minor device) ; see glibc's + "Return two values: the major and minor device numbers that make up DEVICE." + (values (logior (ash (logand #x00000000000fff00 device) -8) + (ash (logand #xfffff00000000000 device) -32)) + (logior (logand #x00000000000000ff device) + (ash (logand #x00000ffffff00000 device) -12)))) + ;;; ;;; File systems. @@ -628,7 +655,7 @@ (define (mounts) (define (string->device-number str) (match (string-split str #\:) (((= string->number major) (= string->number minor)) - (+ (* major 256) minor)))) + (device-number major minor)))) (call-with-input-file "/proc/self/mountinfo" (lambda (port) -- cgit v1.2.3