1047e8742e5c0b168775f466d43be54bdceaf729
[xfstests-dev.git] / dmapi / src / suite1 / cmd / make_rt_sparse.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  *      Create file with XFS_XFLAG_REALTIME set.
21  */
22
23 #include <sys/types.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <malloc.h>
27 #include <stdio.h>
28 #include <unistd.h>
29
30
31 /* Note: In order to figure out the filesystem geometry, you have to run this
32    program as root.  Creating the file itself can be done by anyone.
33 */
34
35
36 static  char *  prog;
37
38 static void
39 Usage(void)
40 {
41         fprintf(stderr,"Usage: %s filename\n", prog);
42         exit(1);
43 }
44
45
46 int
47 main(
48         int     argc,
49         char    **argv)
50 {
51         xfs_fsop_geom_t geom;
52         struct  fsxattr fsx;
53         struct  dioattr dio;
54         char    *pathname;
55         u_int   buflen;
56         char    *buf;
57         ssize_t offset;
58         ssize_t count;
59         int     fd;
60         int     i;
61
62         if (prog = strrchr(argv[0], '/')) {
63                 *prog++;
64         } else {
65                 prog = argv[0];
66         }
67
68         if (argc != 2)
69                 Usage();
70         pathname = argv[1];
71
72         /* Create the file. */
73
74         if ((fd = open(pathname, O_RDWR|O_CREAT|O_EXCL|O_DIRECT, 0600)) < 0) {
75                 fprintf(stderr,"%s: Cannot open %s, %s\n", prog,
76                         pathname, strerror(errno));
77                 exit(1);
78         }
79
80         /* Determine the filesystem's realtime partition geometry. */
81
82         if (syssgi(SGI_XFS_FSOPERATIONS, fd, XFS_FS_GEOMETRY, NULL, &geom)) {
83                 fprintf(stderr,"%s: syssgi(,XFS_FS_GEOMETRY) failed, %s\n",
84                         prog, strerror(errno));
85                 exit(1);
86         }
87
88         /* Make the file a realtime file. */
89
90         fsx.fsx_xflags = 1; /*XFS_XFLAG_REALTIME*/
91         fsx.fsx_extsize = 4 * geom.blocksize * geom.rtextsize;
92         if (fcntl(fd, F_FSSETXATTR, &fsx) < 0) {
93                 fprintf(stderr,"%s: fcntl(,F_FSSETXATTR) failed, %s\n", prog,
94                         strerror(errno));
95                 exit(1);
96         }
97
98         /* Obtain the direct I/O parameters. */
99
100         if (fcntl(fd, F_DIOINFO, &dio) < 0) {
101                 fprintf(stderr,"%s: fcntl(,F_DIOINFO) failed,%s\n",
102                         prog, strerror(errno));
103                 exit(1);
104         }
105         fprintf(stdout, "%s: file %s direct io requirements.\n", prog,
106                 pathname);
107         fprintf(stdout, "%7d memory alignment.\n", dio.d_mem);
108         fprintf(stdout, "%7d minimum io size.\n", dio.d_miniosz);
109         fprintf(stdout, "%7d maximum io size.\n", dio.d_maxiosz);
110
111         if (fcntl(fd, F_FSGETXATTR, &fsx) < 0) {
112                 fprintf(stderr,"%s: fcntl(,F_FSGETXATTR) failed, %s\n", prog,
113                         strerror(errno));
114                 exit(1);
115         }
116         fprintf(stdout, "%7d realtime extent size.\n", fsx.fsx_extsize);
117
118         /* Malloc and zero a buffer to use for writes. */
119
120         buflen = dio.d_miniosz;
121         if ((buf = memalign(dio.d_mem, buflen)) == NULL) {
122                 fprintf(stderr,"%s: memalign(%d,%d) returned NULL\n",
123                                 prog, dio.d_mem, buflen);
124                 exit(1);
125         }
126         memset(buf, '\0', buflen);
127
128         for (i = 0; i < 10; i += 2) {
129                 offset = i * fsx.fsx_extsize;
130                 if (lseek(fd, offset, SEEK_SET) < 0) {
131                         fprintf(stderr, "seek to %d failed, %s\n", offset,
132                                 strerror(errno));
133                         exit(1);
134                 }
135                 if ((count = write(fd, buf, buflen)) < 0) {
136                         fprintf(stderr, "write of %d bytes failed at offset "
137                                 "%d, , %s\n", buflen, offset, strerror(errno));
138                         exit(1);
139                 }
140                 if (count != buflen) {
141                         fprintf(stderr, "expected to write %d bytes at offset "
142                                 "%d, actually wrote %d\n", buflen, offset,
143                                 count);
144                         exit(1);
145                 }
146         }
147         exit(0);
148 }