xfs: test fallocate ops when rt extent size is and isn't a power of 2
[xfstests-dev.git] / dmapi / src / common / cmd / write_invis.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <lib/hsm.h>
8
9 #include <unistd.h>
10 #include <string.h>
11 #include <malloc.h>
12 #include <getopt.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16
17 /*---------------------------------------------------------------------------
18
19 Test program used to test the DMAPI function dm_write_invis().  The
20 command line is:
21
22         write_invis [-c char] [-o offset] [-l length] [-s sid] \
23                 [-S storefile] {pathname|handle}
24
25 where:
26 'char' is the character to use as a repeated pattern ('X' is the default),
27 'offset' is the offset of the start of the write (0 is the default),
28 'length' is the length of the write in bytes (1 is the default),
29 'sid' is the session ID whose events you you are interested in.
30 'pathname' is the name of the file to be written.
31
32 DM_WRITE_SYNC is is not supported.
33
34 ----------------------------------------------------------------------------*/
35
36 #ifndef linux
37 extern  char    *sys_errlist[];
38 #endif
39 extern  int     optind;
40 extern  char    *optarg;
41
42
43 char    *Progname;
44
45
46 static void
47 usage(void)
48 {
49         fprintf(stderr, "usage:\t%s [-c char] [-o offset] [-l length] "
50                 "[-s sid] [-S storefile] {pathname|handle}\n", Progname);
51         exit(1);
52 }
53
54
55 int
56 main(
57         int     argc, 
58         char    **argv)
59 {
60         dm_sessid_t     sid = DM_NO_SESSION;
61         char            *object = NULL;
62         dm_off_t        offset = 0;
63         long long       lltemp;
64         dm_size_t       length = 1;
65         unsigned long long      ulltemp;
66         u_char          ch = 'X';
67         void            *bufp = NULL;
68         void            *hanp;
69         size_t          hlen;
70         dm_ssize_t      rc;
71         char            *name;
72         int             opt;
73         char            *storefile = NULL;
74         int             storefd;
75         int             exit_status = 0;
76
77         Progname = strrchr(argv[0], '/');
78         if (Progname) {
79                 Progname++;
80         } else {
81                 Progname = argv[0];
82         }
83
84         /* Crack and validate the command line options. */
85
86         while ((opt = getopt(argc, argv, "c:o:l:s:S:")) != EOF) {
87                 switch (opt) {
88                 case 'c':
89                         ch = *optarg;
90                         break;
91                 case 'o':
92                         sscanf(optarg, "%lld", &lltemp);
93                         offset = (dm_off_t) offset;
94                         break;
95                 case 'l':
96                         sscanf(optarg, "%llu", &ulltemp);
97                         length = (dm_size_t) ulltemp;
98                         break;
99                 case 's':
100                         sid = atol(optarg);
101                         break;
102                 case 'S':
103                         storefile = optarg;
104                         break;
105                 case '?':
106                         usage();
107                 }
108         }
109         if (optind + 1 != argc)
110                 usage();
111         object = argv[optind];
112
113         if (dm_init_service(&name) == -1)  {
114                 fprintf(stderr, "Can't initialize the DMAPI\n");
115                 exit(1);
116         }
117         if (sid == DM_NO_SESSION)
118                 find_test_session(&sid);
119
120         /* Get the file's handle. */
121
122         if (opaque_to_handle(object, &hanp, &hlen)) {
123                 fprintf(stderr, "can't get handle for %s\n", object);
124                 exit(1);
125         }
126
127         if (length > 0) {
128                 /* In case it is a realtime file, align the buffer on a
129                    sufficiently big boundary.
130                 */
131                 if ((bufp = memalign(4096, length)) == NULL) {
132                         fprintf(stderr, "malloc of %llu bytes failed\n",
133                                 (unsigned long long) length);
134                         exit(1);
135                 }
136                 memset(bufp, ch, length);
137         }
138
139         if (storefile) {
140                 ssize_t sret;
141                 size_t len;
142
143                 if ((storefd = open(storefile, O_RDONLY)) == -1) {
144                         fprintf(stderr, "unable to open store file for read (%s), errno = %d\n", storefile, errno);
145                         exit(1);
146                 }
147
148                 len = length;
149                 sret = read(storefd, bufp, len);
150                 if (sret < 0) {
151                         fprintf(stderr, "unable to read store file (%s), errno = %d\n", storefile, errno);
152                         exit(1);
153                 }
154                 else if (sret != length) {
155                         fprintf(stderr, "read(%s) returned %lld, expected %lld\n",
156                                 storefile, (long long)sret, (long long)length);
157                         exit(1);
158                 }
159                 close(storefd);
160         }
161
162         rc = dm_write_invis(sid, hanp, hlen, DM_NO_TOKEN, 0, offset, length, bufp);
163
164         if (rc < 0) {
165                 fprintf(stderr, "dm_write_invis failed, %s\n", strerror(errno));
166                 exit_status++;
167         } else if (rc != length) {
168                 fprintf(stderr, "dm_write_invis expected to write %llu bytes, "
169                         "actually wrote %lld\n",
170                         (unsigned long long) length, (long long)rc);
171                 exit_status++;
172         }
173         dm_handle_free(hanp, hlen);
174         exit(exit_status);
175 }