Whamcloud - gitweb
Merge b_md into HEAD
[fs/lustre-release.git] / lustre / utils / lstripe.c
1 #include <ctype.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/ioctl.h>
8 #include <unistd.h>
9
10
11 /****************** Custom includes ********************/
12 #include <linux/lustre_lite.h>
13 #include <linux/lustre_idl.h>
14
15
16 /******************  Functions ******************/
17
18 void usage(char *prog)
19 {
20         fprintf(stderr, "usage: %s <filename> <stripe size> <stripe start> "
21                         "<stripe count>\n", prog);
22
23         fprintf(stderr,
24                 "\tstripe size: number of bytes in each stripe (0 default)\n");
25         fprintf(stderr,
26                 "\tstripe start: OST index of first stripe (-1 default)\n");
27         fprintf(stderr,
28                 "\tstripe count: number of OSTs to stripe over (0 default)\n");
29 }
30
31 int create_file(char *name, long stripe_size, int stripe_offset,
32                 int stripe_count)
33 {
34         struct lov_mds_md a_striping;
35         int fd, result = 0;
36
37         /*  Initialize IOCTL striping pattern structure  */
38         a_striping.lmm_magic = LOV_MAGIC;
39         a_striping.lmm_stripe_size = stripe_size;
40         a_striping.lmm_stripe_offset = stripe_offset;
41         a_striping.lmm_stripe_count = stripe_count;
42
43         fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
44         if (fd < 0) {
45                 fprintf(stderr, "\nUnable to open '%s': %s\n",
46                         name, strerror(errno));
47                 result = -errno;
48         } else if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &a_striping)) {
49                 fprintf(stderr, "\nError on ioctl for '%s' (%d): %s\n",
50                         name, fd, strerror(errno));
51                 result = -errno;
52         } else if (close(fd) < 0) {
53                 fprintf(stderr, "\nError on close for '%s' (%d): %s\n",
54                         name, fd, strerror(errno));
55                 result = -errno;
56         }
57
58         return result;
59 }
60
61 int main(int argc, char *argv[])
62 {
63         int result;
64         long st_size;
65         int  st_offset,
66              st_count;
67         char *end;
68
69         /*  Check to make sure we have enough parameters  */
70         if (argc != 5) {
71                 usage(argv[0]);
72                 return 1;
73         }
74
75         /* Get the stripe size */
76         st_size = strtoul(argv[2], &end, 0);
77         if (*end != '\0') {
78                 fprintf(stderr, "bad stripe size '%s'\n", argv[2]);
79                 usage(argv[0]);
80                 return 2;
81         }
82
83         /*
84         if (st_size & 4095) {
85                 fprintf(stderr, "stripe size must be multiple of page size\n");
86                 usage(argv[0]);
87                 return 3;
88         }
89         */
90
91         /* Get the stripe offset*/
92         st_offset = strtoul(argv[3], &end, 0);
93         if (*end != '\0') {
94                 fprintf(stderr, "bad stripe offset '%s'\n", argv[3]);
95                 usage(argv[0]);
96                 return 4;
97         }
98
99         /* Get the stripe count */
100         st_count = strtoul(argv[4], &end, 0);
101         if (*end != '\0') {
102                 fprintf(stderr, "bad stripe count '%s'\n", argv[4]);
103                 usage(argv[0]);
104                 return 5;
105         }
106
107         /*  Create the file, as specified.  Return and display any errors.  */
108         result = create_file(argv[1], st_size, st_offset, st_count);
109
110         return result;
111 }