1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2018 by Delphix. All rights reserved.
25#
26
27. $STF_SUITE/include/libtest.shlib
28
29#
30# DESCRIPTION:
31# Test that we can read from and write to a file in procfs whose contents is
32# backed by a linked list.
33#
34# STRATEGY:
35# 1. Take some snapshots of a filesystem, which will cause some messages to be
36#    written to the zfs dbgmsgs.
37# 2. Read the dbgmsgs via procfs and verify that the expected messages are
38#    present.
39# 3. Write to the dbgmsgs file to clear the messages.
40# 4. Read the dbgmsgs again, and make sure the messages are no longer present.
41#
42
43function cleanup
44{
45	datasetexists $FS && destroy_dataset $FS -r
46}
47
48function count_snap_cmds
49{
50	typeset expected_count=$1
51	count=$(grep -cE "command: (lt-)?zfs snapshot $FS@testsnapshot")
52	log_must [ "$count" -eq "$expected_count" ]
53}
54
55typeset -r ZFS_DBGMSG=/proc/spl/kstat/zfs/dbgmsg
56typeset -r FS=$TESTPOOL/fs
57typeset snap_msgs
58
59log_onexit cleanup
60
61# Clear out old messages
62echo 0 >$ZFS_DBGMSG || log_fail "failed to write to $ZFS_DBGMSG"
63
64log_must zfs create $FS
65for i in {1..20}; do
66	log_must zfs snapshot "$FS@testsnapshot$i"
67done
68sync_pool $TESTPOOL
69
70#
71# Read the debug message file in small chunks to make sure that the read is
72# split up into multiple syscalls. This tests that when a syscall begins we
73# correctly pick up in the list of messages where the previous syscall left
74# off. The size of the read can affect how many bytes the seq_file code has
75# left in its internal buffer, which in turn can affect the relative pos that
76# the seq_file code picks up at when the next read starts. Try a few
77# different size reads to make sure we can handle each case.
78#
79# Check that the file has the right contents by grepping for some of the
80# messages that we expect to be present.
81#
82for chunk_sz in {1,64,256,1024,4096}; do
83	dd if=$ZFS_DBGMSG bs=$chunk_sz | count_snap_cmds 20
84done
85
86# Clear out old messages and check that they really are gone
87echo 0 >$ZFS_DBGMSG || log_fail "failed to write to $ZFS_DBGMSG"
88count_snap_cmds 0 < $ZFS_DBGMSG
89#
90# Even though we don't expect any messages in the file, reading should still
91# succeed.
92#
93log_must cat $ZFS_DBGMSG
94
95log_pass "Basic reading/writing of procfs file backed by linked list successful"
96