Whamcloud - gitweb
65055a5b3eacb4b518da715031182b3c2cf63c10
[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 *pgm)
19 {
20         fprintf(stderr, "\nIncorrect parameters!  Correct usage:\n\n" );
21         fprintf(stderr, "%s <output filename> <stripe size> <OST #> <stripe #>\n", pgm);
22
23         fprintf(stderr, "\n\nArgument explanations:\n---------------------\n\n");
24         fprintf(stderr, "<output filename> = the full name and path of the output file to create\n");
25         fprintf(stderr, "<stripe size> = the number of bytes to have in each stripe.\n");
26         fprintf(stderr, "<OST #> = the OST number to start the striping on.\n");
27         fprintf(stderr, "<stripe #> = the number of stripes to use.\n");
28
29         fprintf(stderr, "\n\nExamples:\n---------\n\n");
30
31         fprintf(stderr, "%s /mnt/lustre/ost1 131072 0 1\n", pgm);
32         fprintf(stderr, "\t\tcreates a file only on ost1.\n\n");
33
34         fprintf(stderr, "%s /mnt/lustre/ost2 131072 1 1\n", pgm);
35         fprintf(stderr, "\t\tcreates a file only on ost2.\n\n");
36
37         fprintf(stderr, "%s /mnt/lustre/ost1and2 131072 0 2\n", pgm);
38         fprintf(stderr, "\t\tcreates a 128k file with 2 stripes, on ost1 and ost2.\n");
39
40         fprintf(stderr, "%s /mnt/lustre/ost1and2 131072 1 2\n", pgm);
41         fprintf(stderr, "\t\tcreates a 128k file with 2 stripes, on ost2 and ost1.\n");
42 }
43
44 int create_file(char *name, long stripe_size, int stripe_offset,
45                 int stripe_count)
46 {
47         struct lov_mds_md a_striping;
48         int fd, result = 0;
49
50         /*  Initialize IOCTL striping pattern structure  */
51         a_striping.lmm_magic = LOV_MAGIC;
52         a_striping.lmm_stripe_pattern = 0;
53         a_striping.lmm_stripe_size = stripe_size;
54         a_striping.lmm_stripe_offset = stripe_offset;
55         a_striping.lmm_stripe_count = stripe_count;
56
57         fd = open(name, O_CREAT | O_RDWR | O_LOV_DELAY_CREATE, 0644);
58         if (fd < 0) {
59                 fprintf(stderr, "\nUnable to open '%s': %s\n",
60                         name, strerror(errno));
61                 result = -errno;
62         } else if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &a_striping)) {
63                 fprintf(stderr, "\nError on ioctl for '%s' (%d): %s\n",
64                         name, fd, strerror(errno));
65                 result = -errno;
66         } else if (close(fd) < 0) {
67                 fprintf(stderr, "\nError on close for '%s' (%d): %s\n",
68                         name, fd, strerror(errno));
69                 result = -errno;
70         }
71
72         return result;
73 }
74
75 int main(int argc, char *argv[])
76 {
77         int result;
78         long st_size;
79         int  st_offset,
80              st_count;
81
82         /*  Check to make sure we have enough parameters  */
83         if (argc != 5) {
84                 usage(argv[0]);
85                 return(-1);
86         }
87
88         /* Get the stripe size */
89         st_size = atol(argv[2]);
90
91         /* Get the stripe offset*/
92         st_offset = atoi(argv[3]);
93
94         /* Get the stripe count */
95         st_count = atoi(argv[4]);
96
97         /*  Create the file, as specified.  Return and display any errors.  */
98         result = create_file(argv[1], st_size, st_offset, st_count);
99
100         return result;
101 }