ab5a239d43a77bc4c2021fbdc9abfc8989fe9eb9
[xfstests-dev.git] / dmapi / src / suite1 / cmd / make_rt_sparse.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 /*
34  *      Create file with XFS_XFLAG_REALTIME set.
35  */
36
37 #include <sys/types.h>
38 #ifdef __sgi
39 #include <sys/fs/xfs_itable.h>
40 #include <sys/syssgi.h>
41 #include <sys/uuid.h>
42 #include <sys/fs/xfs_fsops.h>
43 #endif
44
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <malloc.h>
48 #include <stdio.h>
49 #include <unistd.h>
50
51
52 /* Note: In order to figure out the filesystem geometry, you have to run this
53    program as root.  Creating the file itself can be done by anyone.
54 */
55
56
57 static  char *  prog;
58
59 static void
60 Usage(void)
61 {
62         fprintf(stderr,"Usage: %s filename\n", prog);
63         exit(1);
64 }
65
66
67 int
68 main(
69         int     argc,
70         char    **argv)
71 {
72         xfs_fsop_geom_t geom;
73         struct  fsxattr fsx;
74         struct  dioattr dio;
75         char    *pathname;
76         u_int   buflen;
77         char    *buf;
78         ssize_t offset;
79         ssize_t count;
80         int     fd;
81         int     i;
82
83         if (prog = strrchr(argv[0], '/')) {
84                 *prog++;
85         } else {
86                 prog = argv[0];
87         }
88
89         if (argc != 2)
90                 Usage();
91         pathname = argv[1];
92
93         /* Create the file. */
94
95         if ((fd = open(pathname, O_RDWR|O_CREAT|O_EXCL|O_DIRECT, 0600)) < 0) {
96                 fprintf(stderr,"%s: Cannot open %s, %s\n", prog,
97                         pathname, strerror(errno));
98                 exit(1);
99         }
100
101         /* Determine the filesystem's realtime partition geometry. */
102
103         if (syssgi(SGI_XFS_FSOPERATIONS, fd, XFS_FS_GEOMETRY, NULL, &geom)) {
104                 fprintf(stderr,"%s: syssgi(,XFS_FS_GEOMETRY) failed, %s\n",
105                         prog, strerror(errno));
106                 exit(1);
107         }
108
109         /* Make the file a realtime file. */
110
111         fsx.fsx_xflags = 1; /*XFS_XFLAG_REALTIME*/
112         fsx.fsx_extsize = 4 * geom.blocksize * geom.rtextsize;
113         if (fcntl(fd, F_FSSETXATTR, &fsx) < 0) {
114                 fprintf(stderr,"%s: fcntl(,F_FSSETXATTR) failed, %s\n", prog,
115                         strerror(errno));
116                 exit(1);
117         }
118
119         /* Obtain the direct I/O parameters. */
120
121         if (fcntl(fd, F_DIOINFO, &dio) < 0) {
122                 fprintf(stderr,"%s: fcntl(,F_DIOINFO) failed,%s\n",
123                         prog, strerror(errno));
124                 exit(1);
125         }
126         fprintf(stdout, "%s: file %s direct io requirements.\n", prog,
127                 pathname);
128         fprintf(stdout, "%7d memory alignment.\n", dio.d_mem);
129         fprintf(stdout, "%7d minimum io size.\n", dio.d_miniosz);
130         fprintf(stdout, "%7d maximum io size.\n", dio.d_maxiosz);
131
132         if (fcntl(fd, F_FSGETXATTR, &fsx) < 0) {
133                 fprintf(stderr,"%s: fcntl(,F_FSGETXATTR) failed, %s\n", prog,
134                         strerror(errno));
135                 exit(1);
136         }
137         fprintf(stdout, "%7d realtime extent size.\n", fsx.fsx_extsize);
138
139         /* Malloc and zero a buffer to use for writes. */
140
141         buflen = dio.d_miniosz;
142         if ((buf = memalign(dio.d_mem, buflen)) == NULL) {
143                 fprintf(stderr,"%s: memalign(%d,%d) returned NULL\n",
144                                 prog, dio.d_mem, buflen);
145                 exit(1);
146         }
147         memset(buf, '\0', buflen);
148
149         for (i = 0; i < 10; i += 2) {
150                 offset = i * fsx.fsx_extsize;
151                 if (lseek(fd, offset, SEEK_SET) < 0) {
152                         fprintf(stderr, "seek to %d failed, %s\n", offset,
153                                 strerror(errno));
154                         exit(1);
155                 }
156                 if ((count = write(fd, buf, buflen)) < 0) {
157                         fprintf(stderr, "write of %d bytes failed at offset "
158                                 "%d, , %s\n", buflen, offset, strerror(errno));
159                         exit(1);
160                 }
161                 if (count != buflen) {
162                         fprintf(stderr, "expected to write %d bytes at offset "
163                                 "%d, actually wrote %d\n", buflen, offset,
164                                 count);
165                         exit(1);
166                 }
167         }
168         exit(0);
169 }