xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/statx/statx11.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2023 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Yang Xu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * It is a basic test for STATX_DIOALIGN mask on block device.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - STATX_DIOALIGN   Want stx_dio_mem_align and stx_dio_offset_align value
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * These two values are tightly coupled to the kernel's current DIO
15*49cdfc7eSAndroid Build Coastguard Worker  * restrictions on block devices.
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * Minimum Linux version required is v6.1.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdbool.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/stat.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static char sys_bdev_dma_path[1024], sys_bdev_logical_path[1024];
31*49cdfc7eSAndroid Build Coastguard Worker 
verify_statx(void)32*49cdfc7eSAndroid Build Coastguard Worker static void verify_statx(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	struct statx buf;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS_SILENT(statx(AT_FDCWD, tst_device->dev, 0, STATX_DIOALIGN, &buf),
37*49cdfc7eSAndroid Build Coastguard Worker 		"statx(AT_FDCWD, %s, 0, STATX_DIOALIGN, &buf)", tst_device->dev);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	if (!(buf.stx_mask & STATX_DIOALIGN)) {
40*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "Filesystem does not support STATX_DIOALIGN");
41*49cdfc7eSAndroid Build Coastguard Worker 		return;
42*49cdfc7eSAndroid Build Coastguard Worker 	}
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_STRUCT_STATX_STX_DIO_MEM_ALIGN
45*49cdfc7eSAndroid Build Coastguard Worker 	/*
46*49cdfc7eSAndroid Build Coastguard Worker 	 * This test is tightly coupled to the kernel's current DIO restrictions
47*49cdfc7eSAndroid Build Coastguard Worker 	 * on block devices. The general rule of DIO needing to be aligned to the
48*49cdfc7eSAndroid Build Coastguard Worker 	 * block device's logical block size was relaxed to allow user buffers
49*49cdfc7eSAndroid Build Coastguard Worker 	 * (but not file offsets) aligned to the DMA alignment instead. See v6.0
50*49cdfc7eSAndroid Build Coastguard Worker 	 * commit bf8d08532bc1 ("iomap: add support for dma aligned direct-io") and
51*49cdfc7eSAndroid Build Coastguard Worker 	 * they are subject to further change in the future.
52*49cdfc7eSAndroid Build Coastguard Worker 	 * Also can see commit 2d985f8c6b9 ("vfs: support STATX_DIOALIGN on block devices).
53*49cdfc7eSAndroid Build Coastguard Worker 	 */
54*49cdfc7eSAndroid Build Coastguard Worker 	TST_ASSERT_ULONG(sys_bdev_dma_path, buf.stx_dio_mem_align - 1);
55*49cdfc7eSAndroid Build Coastguard Worker 	TST_ASSERT_ULONG(sys_bdev_logical_path, buf.stx_dio_offset_align);
56*49cdfc7eSAndroid Build Coastguard Worker #else
57*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TCONF, "glibc statx struct miss stx_dio_mem_align field");
58*49cdfc7eSAndroid Build Coastguard Worker #endif
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)61*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	char full_name[256];
64*49cdfc7eSAndroid Build Coastguard Worker 	char *dev_name;
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	strcpy(full_name, tst_device->dev);
67*49cdfc7eSAndroid Build Coastguard Worker 	dev_name = SAFE_BASENAME(full_name);
68*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(sys_bdev_logical_path, "/sys/block/%s/queue/logical_block_size", dev_name);
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	/*
71*49cdfc7eSAndroid Build Coastguard Worker 	 * Since /sys/block/%s/queue doesn't exist for partition, we need to
72*49cdfc7eSAndroid Build Coastguard Worker 	 * use a while to search block device instead of partition.
73*49cdfc7eSAndroid Build Coastguard Worker 	 */
74*49cdfc7eSAndroid Build Coastguard Worker 	while (access(sys_bdev_logical_path, F_OK) != 0) {
75*49cdfc7eSAndroid Build Coastguard Worker 		dev_name[strlen(dev_name)-1] = '\0';
76*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(sys_bdev_logical_path, "/sys/block/%s/queue/logical_block_size", dev_name);
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(sys_bdev_dma_path, "/sys/block/%s/queue/dma_alignment", dev_name);
80*49cdfc7eSAndroid Build Coastguard Worker 	if (access(sys_bdev_dma_path, F_OK) != 0)
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "dma_alignment sysfs file doesn't exist");
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
85*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_statx,
86*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
87*49cdfc7eSAndroid Build Coastguard Worker 	.needs_device = 1,
88*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
89*49cdfc7eSAndroid Build Coastguard Worker };
90