Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2810682pxu; Mon, 7 Dec 2020 16:54:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjDQQW6I5G7LH+il5kU/zC5Vz8nSrL9P9CkVhKV609QkF8vbdhg757JjaJeFNlN3jZP6xx X-Received: by 2002:a17:907:20f1:: with SMTP id rh17mr20871559ejb.147.1607388874359; Mon, 07 Dec 2020 16:54:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607388874; cv=none; d=google.com; s=arc-20160816; b=iHuSUJcWPpG2K1KdLJijxR6zJP/y9TQ5HKtvZNLIrd5Dspvgy7ISua4uhW532uY+lz Uz/k7pbPXm/NnumXZQEPMQ2OSBnPl+xGoH3rA5wHYAlzwaMHHd7MOuJgpNUmadTLD92L pYbyX/lzedkSYmkee73QqXXVnjDXOJ8bdpPGzRmW6PBlQmHyZLo9zpC/w5XEneDPjo7V oGTxaV49x96ogh/0/vsyuwri9EBO5k0mr7gSrUTtVyy0IKeZ11FNIIdUu8Qr+c9xp5OU CaqMWU0MzffuqTDzxDtXaIG2xjju2xcA1/w4IMp2S96Y61daOgbId1lAAYgy68ul2x2q fRaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=G6zS4onHFPZGIgmwS8N5T6yk0LAbilhszwtV7nuYg6c=; b=SJ4nU00cwhfxV1td8J0f3E/kRt7ro6HX+Ke4VHMRAamX9gtC2Nd+YFdOcOwB56Z0f/ oh3zDOhRevvS21WQwZhO/0LN9PxAnkpvkBmQviguU7nh3+leeeNa9TRxYlMxirNYelAr 92KBHO9Lr5S2V/r9A+6fSxogdpJW81takY9YVEYkjbvTM2xfZGhtt5sS4eiBccjBUD4j YOrMjfCYChV6p5/VPTx0ewVd/RKVGMpVpB6vykb4XeBkZ01GtEMsRM5CoXfBTBKHkJ7w 0ss4BihNrLKjzUk6MzbuwRIk+3AJpdcHNgb7zKAWwoNAP/spaSkpfKgM7TGOpJBIaxTp +eGw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n25si9065673edy.231.2020.12.07.16.54.11; Mon, 07 Dec 2020 16:54:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728722AbgLHAcu (ORCPT + 99 others); Mon, 7 Dec 2020 19:32:50 -0500 Received: from bhuna.collabora.co.uk ([46.235.227.227]:56662 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725995AbgLHAcu (ORCPT ); Mon, 7 Dec 2020 19:32:50 -0500 Received: from localhost (unknown [IPv6:2804:14c:132:242d::1001]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: krisman) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id EABE51F44D33; Tue, 8 Dec 2020 00:32:07 +0000 (GMT) From: Gabriel Krisman Bertazi To: dhowells@redhat.com Cc: viro@zeniv.linux.org.uk, tytso@mit.edu, khazhy@google.com, adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, Gabriel Krisman Bertazi , kernel@collabora.com Subject: [PATCH 8/8] samples: watch_queue: Add sample of SB notifications Date: Mon, 7 Dec 2020 21:31:17 -0300 Message-Id: <20201208003117.342047-9-krisman@collabora.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201208003117.342047-1-krisman@collabora.com> References: <20201208003117.342047-1-krisman@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org This sample demonstrates how to use the watch_sb syscall. It exposes notifications like the following: root@host:~# ./watch_sb /mnt read() = 93 NOTIFY[000]: ty=000002 sy=01 i=0300005d SB AT ext4_remount:5636 ERROR: 16 inode=0 block=0 description: Abort forced by user read() = 96 NOTIFY[000]: ty=000002 sy=01 i=03000060 SB AT ext4_lookup:1706 ERROR: 0 inode=13 block=0 description: iget: bogus i_mode (45) Signed-off-by: Gabriel Krisman Bertazi --- samples/watch_queue/Makefile | 2 +- samples/watch_queue/watch_sb.c | 114 +++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 samples/watch_queue/watch_sb.c diff --git a/samples/watch_queue/Makefile b/samples/watch_queue/Makefile index c0db3a6bc524..6067d57a5bb1 100644 --- a/samples/watch_queue/Makefile +++ b/samples/watch_queue/Makefile @@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only -userprogs-always-y += watch_test +userprogs-always-y += watch_test watch_sb userccflags += -I usr/include diff --git a/samples/watch_queue/watch_sb.c b/samples/watch_queue/watch_sb.c new file mode 100644 index 000000000000..51b660334f6b --- /dev/null +++ b/samples/watch_queue/watch_sb.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Use watch_sb to watch for SB notifications. + * + * Copyright (C) 2020 Collabora Ltd. + * Written by Gabriel Krisman Bertazi + * Based on watch_test.c by David Howells (dhowells@redhat.com) + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifndef __NR_watch_sb +# define __NR_watch_sb 441 +#endif + +static void consumer(int fd) +{ + unsigned char buffer[433], *p, *end; + union { + struct watch_notification n; + unsigned char buf1[128]; + struct superblock_error_notification sen; + struct superblock_warning_notification swn; + struct superblock_msg_notification smn; + } n; + ssize_t buf_len; + + for (;;) { + buf_len = read(fd, buffer, sizeof(buffer)); + if (buf_len == -1) + err(1, "read"); + + if (buf_len == 0) { + printf("-- END --\n"); + return; + } + + if (buf_len > sizeof(buffer)) { + err(1, "Read buffer overrun: %zd\n", buf_len); + return; + } + + printf("read() = %zd\n", buf_len); + + p = buffer; + end = buffer + buf_len; + while (p < end) { + size_t largest, len; + + largest = end - p; + if (largest > 128) + largest = 128; + if (largest < sizeof(struct watch_notification)) + err(1, "Short message header: %zu\n", largest); + + memcpy(&n, p, largest); + + printf("NOTIFY[%03zx]: ty=%06x sy=%02x i=%08x\n", + p - buffer, n.n.type, n.n.subtype, n.n.info); + + len = n.n.info & WATCH_INFO_LENGTH; + if (len < sizeof(n.n) || len > largest) + err(1, "Bad message length: %zu/%zu\n", len, largest); + + switch (n.n.subtype) { + case NOTIFY_SUPERBLOCK_ERROR: + printf("\t SB AT %s:%d ERROR: %d inode=%llu block=%llu\n", + n.sen.function, n.sen.line, n.sen.error_number, + n.sen.inode, n.sen.block); + if (len > sizeof(n.sen)) + printf("description: %s\n", n.sen.desc); + break; + case NOTIFY_SUPERBLOCK_MSG: + printf("\t Ext4 MSG: %s\n", n.smn.desc); + break; + case NOTIFY_SUPERBLOCK_WARNING: + printf("\t SB AT %s:%d WARNING inode=%llu block=%llu\n", + n.swn.function, n.swn.line, n.swn.inode, n.swn.block); + if (len > sizeof(n.sen)) + printf("description: %s\n", n.swn.desc); + break; + default: + printf("unknown subtype %c\n", n.n.subtype); + } + p += len; + } + } +} + +int main (int argc, char **argv) +{ + int fd[2]; + + if (argc != 2) + errx(1, "Missing mount point\n"); + + if (syscall(293, fd, O_NOTIFICATION_PIPE) < 0) + err(1, "Failed to open pipe\n"); + + if (ioctl(fd[0], IOC_WATCH_QUEUE_SET_SIZE, 256) < 0) + err(1, "ioctl fail\n"); + + if (syscall(__NR_watch_sb, 0, argv[1], NULL, fd[0], 0x3) < 0) + err(1, "Failed to watch SB\n"); + + consumer(fd[0]); + + return 0; +} -- 2.29.2