root/branches/cu-security-branch/test/io/description/test-zero-fill.c @ 8330

Revision 8330, 15.4 KB (checked in by nlmills, 3 years ago)

revert cu-security-branch to before the attempted merge with Orange-Branch

Line 
1
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include <stdarg.h>
6#include <time.h>
7#include <unistd.h>
8#include <assert.h>
9
10#include "pvfs2-request.h"
11#include "pvfs2-sysint.h"
12#include "pvfs2-util.h"
13#include "pvfs2-dist-simple-stripe.h"
14
15char buff[1000];
16char check_buff[1000];
17char * dashes = "--------------------------------------------------------";
18char * spaces = "                                                        ";
19char * as = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
20char * bs = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
21char * cs = "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
22char * xs = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
23
24int verbose = 0;
25
26void reset_buff(void);
27void print_buff(int padding, int len);
28
29int do_smallmem_noncontig_read(
30    PVFS_object_ref ref, PVFS_credential * cred,
31    int memsize,
32    int chunks,
33    ...);
34
35int do_noncontig_read(
36    PVFS_object_ref ref, PVFS_credential * cred,
37    int chunks,
38    ...);
39
40int do_contig_read(
41    PVFS_object_ref ref,
42    PVFS_credential * cred,
43    PVFS_offset offset,
44    int length,
45    PVFS_Request freq);
46
47int do_write(PVFS_object_ref ref, PVFS_credential * cred,
48             PVFS_offset offset, PVFS_size size, char * buff);
49
50int check_results(
51    PVFS_offset offset,
52    int total_read,
53    PVFS_offset * offsets,
54    int32_t * sizes,
55    int count);
56
57int check_smallmem_results(
58    PVFS_offset offset,
59    int total_read,
60    int memsize,
61    PVFS_offset * offsets,
62    int32_t * sizes,
63    int count);
64
65void reset_buff(void)
66{
67    memset(buff, 'X', 1000);
68}
69
70void print_buff(int padding, int len)
71{
72    int i = 0;
73   
74    fprintf(stdout, "RESULT: \t");
75    fprintf(stdout, "%.*s", padding, spaces);
76
77    for(; i < 50; ++i)
78    {
79        fprintf(stdout, "%c", (buff[i] == 0 ? '-' : buff[i]));
80    }
81    fprintf(stdout, "\t (%d bytes)\n\n", len);
82}
83
84int check_smallmem_results(
85    PVFS_offset offset,
86    int total_read,
87    int memsize,
88    PVFS_offset * offsets,
89    int32_t * sizes,
90    int count)
91{
92    int stop_size;
93    int total_size = 0;
94    int i = 0;
95    int res = 0;
96
97    stop_size = (memsize > total_read) ? total_read : memsize;
98
99    for(; i < count; ++i)
100    {
101        if(total_size + sizes[i] > stop_size)
102        {
103            sizes[i] = stop_size - total_size;
104        }
105
106        res = memcmp(check_buff + offset + offsets[i],
107                     buff + total_size, sizes[i]);
108        if(res != 0)
109        {
110            if(verbose)
111            {
112                printf("Invalid result: offset: %d, size: %d\n",
113                       (int32_t)offsets[i], (int32_t)sizes[i]);
114            }
115            return res;
116        }
117        total_size += sizes[i];
118    }
119
120    return 0;
121
122}
123
124int check_results(
125    PVFS_offset offset,
126    int total_read,
127    PVFS_offset * offsets,
128    int32_t * sizes,
129    int count)
130{
131    int total_size = 0;
132    int i = 0;
133    int res;
134
135    for(; i < count; ++i)
136    {
137        if(total_read < (total_size + sizes[i]))
138        {
139            sizes[i] = (total_read - total_size);
140        }
141           
142        res = memcmp(
143            check_buff + offset + offsets[i], buff + total_size, sizes[i]);
144        if(res != 0)
145        {
146            if(verbose)
147            {
148                printf("Invalid result: offset: %u, size: %d\n",
149                       (int32_t)offsets[i], (int32_t)sizes[i]);
150            }
151            return res;
152        }
153        total_size += sizes[i];
154    }
155
156    return 0;
157}
158           
159
160int do_smallmem_noncontig_read(
161    PVFS_object_ref ref, PVFS_credential * cred,
162    int memsize,
163    int chunks,
164    ...)
165{
166    int i;
167    int res;
168    PVFS_Request memreq, readreq;
169    PVFS_sysresp_io io_resp;
170    PVFS_size * offsets;
171    int32_t * sizes;
172    va_list args_list;
173    int total = 0;
174   
175    if(verbose)
176    {
177        fprintf(stdout,
178            "READING:\t");
179    }
180 
181    sizes = malloc(sizeof(int32_t) * chunks);
182    offsets = malloc(sizeof(PVFS_size) * chunks);
183
184    va_start(args_list, chunks);
185
186    for(i = 0; i < chunks; ++i)
187    {
188        offsets[i] = (PVFS_size)va_arg(args_list, int);
189        sizes[i] = (int32_t)va_arg(args_list, int);
190
191        if(verbose)
192        {
193            total += fprintf(stdout,
194                             "%.*s<%.*s>",
195                             (int) (offsets[i] - total), spaces,
196                             (int) (sizes[i] - 2), spaces);
197        }
198
199    }
200
201    va_end(args_list);
202
203    if(verbose)
204    {
205        fprintf(stdout, "    (memsize = %d)\n", memsize);
206    }
207
208    PVFS_Request_indexed(chunks, sizes, offsets, PVFS_BYTE, &readreq);
209    PVFS_Request_contiguous(memsize, PVFS_BYTE, &memreq);
210
211    reset_buff();
212
213    res = PVFS_sys_read(
214        ref, readreq, 0, buff, memreq, cred, &io_resp);
215    if(res < 0)
216    {
217        PVFS_perror("read failed with errcode", res);
218        return res;
219    }
220
221    if(verbose)
222    {
223        print_buff(0, io_resp.total_completed);
224    }
225   
226    res = check_smallmem_results(
227        0, io_resp.total_completed, memsize, offsets, sizes, chunks);
228
229    free(offsets);
230    free(sizes);
231
232    return res;
233}
234
235int do_noncontig_read(
236    PVFS_object_ref ref, PVFS_credential * cred,
237    int chunks,
238    ...)
239{
240    int i;
241    int res;
242    PVFS_Request memreq, readreq;
243    PVFS_sysresp_io io_resp;
244    PVFS_size * offsets;
245    int32_t * sizes;
246    PVFS_size readreq_size;
247    va_list args_list;
248    int total = 0;
249   
250    if(verbose)
251    {
252        fprintf(stdout,
253                "READING:\t");
254    }
255
256    sizes = malloc(sizeof(int32_t) * chunks);
257    offsets = malloc(sizeof(PVFS_size) * chunks);
258
259    va_start(args_list, chunks);
260
261    for(i = 0; i < chunks; ++i)
262    {
263        offsets[i] = (PVFS_size)va_arg(args_list, int);
264        sizes[i] = (int32_t)va_arg(args_list, int);
265
266        if(verbose)
267        {
268            total += fprintf(stdout,
269                             "%.*s<%.*s>",
270                             (int) (offsets[i] - total), spaces,
271                             (int) (sizes[i] - 2), spaces);
272        }
273    }
274   
275    va_end(args_list);
276
277    if(verbose)
278    {
279        fprintf(stdout, "\n");
280    }
281
282    PVFS_Request_indexed(chunks, sizes, offsets, PVFS_BYTE, &readreq);
283    PVFS_Request_size(readreq, &readreq_size);
284    PVFS_Request_contiguous(readreq_size, PVFS_BYTE, &memreq);
285
286    reset_buff();
287
288    res = PVFS_sys_read(
289        ref, readreq, 0, buff, memreq, cred, &io_resp);
290    if(res < 0)
291    {
292        PVFS_perror("read failed with errcode", res);
293        return res;
294    }
295
296    if(verbose)
297    {
298        print_buff(0, io_resp.total_completed);
299    }
300
301    res = check_results(0, io_resp.total_completed, offsets, sizes, chunks);
302
303    free(offsets);
304    free(sizes);
305
306    return res;
307}
308
309int do_contig_read(
310    PVFS_object_ref ref,
311    PVFS_credential * cred,
312    PVFS_offset offset,
313    int length,
314    PVFS_Request freq)
315{
316    int res;
317    PVFS_sysresp_io     io_resp;
318    PVFS_Request memreq, readreq;
319
320    if(verbose)
321    {
322        fprintf(stdout,
323                "READING:\t"
324                "%.*s<%.*s>\n",
325                (int)offset, spaces,
326                length - 2, spaces);
327    }
328
329    res = PVFS_Request_contiguous(length, PVFS_BYTE, &memreq);
330    if(res < 0)
331    {
332        PVFS_perror("request contig for memory failed with errcode", res);
333        return res;
334    }
335
336    if(freq)
337    {
338        readreq = freq;
339    }
340    else
341    {
342        res = PVFS_Request_contiguous(length, PVFS_BYTE, &readreq);
343        if(res < 0)
344        {
345            PVFS_perror("request contig for file failed with errcode", res);
346            return res;
347        }
348    }
349
350    reset_buff();
351
352    res = PVFS_sys_read(
353        ref, readreq, offset, buff, memreq, cred, &io_resp);
354    if(res < 0)
355    {
356        PVFS_perror("read failed with errcode", res);
357        return res;
358    }
359
360    if(verbose)
361    {
362        print_buff(offset, io_resp.total_completed);
363    }
364
365    res = check_results(0, io_resp.total_completed,
366                        (PVFS_offset *)&offset, &length, 1);
367
368    return res;
369}
370
371int do_write(PVFS_object_ref ref, PVFS_credential * cred,
372             PVFS_offset offset, PVFS_size size, char * buff)
373{
374    PVFS_Request filereq;
375    PVFS_Request memreq;
376    PVFS_sysresp_io     io_resp;
377    int res;
378
379    /* setup check_buff */
380    memcpy(check_buff + offset, buff, size);
381
382    if(verbose)
383    {
384        fprintf(stdout,
385                "WRITING:\t"
386                "%.*s%.*s\n\n",
387                (int32_t)offset, spaces,
388                (int32_t)size, buff);
389    }
390
391    res = PVFS_Request_contiguous(size, PVFS_BYTE, &filereq);
392    if(res < 0)
393    {
394        PVFS_perror("request contig for memory failed with errcode", res);
395        return -1;
396    }
397
398    res = PVFS_Request_contiguous(size, PVFS_BYTE, &memreq);
399    if(res < 0)
400    {
401        PVFS_perror("request contig for memory failed with errcode", res);
402        return -1;
403    }
404
405    res = PVFS_sys_write(
406        ref, filereq,
407        offset, buff, memreq, cred, &io_resp);
408    if(res < 0)
409    {
410        PVFS_perror("write failed with errcode", res);
411        return -1;
412    }
413
414    return 0;
415}
416
417static void usage(void)
418{
419    printf("usage: test-zero-fill [<OPTIONS>...]\n");
420    printf("\n<OPTIONS> is one of\n");
421    printf("-v\t\tverbose mode.  prints out read results\n");
422    printf("-s <strip size>\tstrip size to use when creating the file\n");
423    printf("-h\t\tprint this help\n");
424}
425
426#define ZEROFILL_FILENAME "test-zerofill"
427
428extern char *optarg;
429extern int optind, opterr, optopt;
430
431int main(int argc, char * argv[])
432{
433    PVFS_sysresp_create create_resp;
434    PVFS_sysresp_lookup lookup_resp;
435    PVFS_fs_id curfs;
436    PVFS_sys_attr attr;
437    PVFS_credential *cred;
438    PVFS_sys_dist * dist;
439    int strip_size = 9;
440    int half_strip;
441    int before_len, after_len;
442    char zerofill_fname[100];
443    PVFS_simple_stripe_params params;
444    int32_t res, realres;
445    char c;
446
447    memset(check_buff, 0, 1000);
448
449    while((c = getopt(argc, argv, "vs:")) != EOF)
450    {
451        switch(c)
452        {
453            case 'v':
454                verbose = 1;
455                break;
456            case 's':
457                strip_size = atoi(optarg);
458                break;
459            case 'h':
460                usage();
461                exit(0);
462            case '?':
463                usage();
464                exit(1);
465            default:
466                break;
467        }
468    }
469
470    half_strip = (int) strip_size / 2;
471
472    res = PVFS_util_init_defaults();
473    if(res < 0)
474    {
475        PVFS_perror("PVFS_util_init_defaults", res);
476        return (-1);
477    }
478
479    res = PVFS_util_get_default_fsid(&curfs);
480    if(res < 0)
481    {
482        PVFS_perror("PVFS_util_get_default_fsid", res);
483        return (-1);
484    }
485 
486    before_len = half_strip - 1;
487    after_len = half_strip + (strip_size % 2 == 0 ? 0 : 1) - 2;
488
489    if(verbose)
490    {
491
492        fprintf(stdout,
493                "unset  = XXXXX\n"
494                "zeroed = -----\n\n"
495                "DISTRIBUTION (strip size == %d):\n"
496                "        \t|%.*s0%.*s||%.*s1%.*s||%.*s2%.*s|"
497                "|%.*s0%.*s||%.*s1%.*s||%.*s2%.*s|\n",
498                strip_size,
499                before_len, dashes, after_len, dashes,
500                before_len, dashes, after_len, dashes,
501                before_len, dashes, after_len, dashes,
502                before_len, dashes, after_len, dashes,
503                before_len, dashes, after_len, dashes,
504                before_len, dashes, after_len, dashes);
505    }
506   
507    cred = PVFS_util_gen_fake_credential();
508    assert(cred);
509
510    res = PVFS_sys_lookup(curfs, "/", cred, &lookup_resp, 0);
511    if(res < 0)
512    {
513        PVFS_perror("lookup failed with errcode", res);
514    }
515   
516    dist = PVFS_sys_dist_lookup("simple_stripe");
517    params.strip_size = strip_size;
518
519    res = PVFS_sys_dist_setparam(dist, "strip_size", (void *)&params);
520    if(res < 0)
521    {
522        PVFS_perror("dist setparam failed with errcode", res);
523    }
524
525    attr.mask = PVFS_ATTR_SYS_ALL_SETABLE;
526    attr.owner = cred->userid;
527    attr.group = cred->group_array[0];
528    attr.perms = 1877;
529    attr.atime = attr.ctime = attr.mtime = time(NULL);
530
531    sprintf(zerofill_fname, "%s-%d", ZEROFILL_FILENAME, rand());
532
533    if(verbose)
534    {
535        fprintf(stdout, "filename: %s\n", zerofill_fname);
536    }
537
538    res = PVFS_sys_create(
539        zerofill_fname, lookup_resp.ref, attr, cred, dist, NULL, &create_resp);
540    if(res < 0)
541    {
542        PVFS_perror("create failed with errcode", res);
543        return -1;
544    }
545
546    half_strip = strip_size / 2;
547
548    do_write(create_resp.ref, cred, 0, half_strip, as);
549
550    do_write(create_resp.ref, cred, strip_size * 2, half_strip, bs);
551
552    res = do_contig_read(
553        create_resp.ref, cred, 0, strip_size + half_strip, NULL);
554    if(res < 0)
555    {
556        goto exit;
557    }
558       
559    res = do_contig_read(
560        create_resp.ref, cred, 0, strip_size + half_strip, PVFS_BYTE);
561    if(res < 0)
562    {
563        goto exit;
564    }
565
566    res = do_contig_read(
567        create_resp.ref, cred,
568        (strip_size + half_strip), (strip_size + half_strip), NULL);
569    if(res < 0)
570    {
571        goto exit;
572    }
573
574    res = do_contig_read(
575        create_resp.ref, cred,
576        (strip_size + half_strip), (strip_size + half_strip), PVFS_BYTE);
577    if(res < 0)
578    {
579        goto exit;
580    }
581
582    res = do_contig_read(
583        create_resp.ref, cred, 0, (strip_size * 3), NULL);
584    if(res < 0)
585    {
586        goto exit;
587    }
588
589    res = do_contig_read(
590        create_resp.ref, cred, 0, (strip_size * 3), PVFS_BYTE);
591    if(res < 0)
592    {
593        goto exit;
594    }
595
596    res = do_contig_read(
597        create_resp.ref, cred, 0, half_strip + 2, NULL);
598    if(res < 0)
599    {
600        goto exit;
601    }
602
603    res = do_contig_read(
604        create_resp.ref, cred, 0, half_strip + 2, PVFS_BYTE);
605    if(res < 0)
606    {
607        goto exit;
608    }
609
610    res = do_contig_read(
611        create_resp.ref, cred, half_strip + 2, strip_size, NULL);
612    if(res < 0)
613    {
614        goto exit;
615    }
616
617    res = do_contig_read(
618        create_resp.ref, cred, half_strip + 2, strip_size, PVFS_BYTE);
619    if(res < 0)
620    {
621        goto exit;
622    }
623
624    res = do_contig_read(
625        create_resp.ref, cred, strip_size + 1, 3, NULL);
626    if(res < 0)
627    {
628        goto exit;
629    }
630
631    res = do_contig_read(
632        create_resp.ref, cred, strip_size + 1, 3, PVFS_BYTE);
633    if(res < 0)
634    {
635        goto exit;
636    }
637
638    res = do_contig_read(
639        create_resp.ref, cred, 0, 2, NULL);
640    if(res < 0)
641    {
642        goto exit;
643    }
644
645    res = do_contig_read(
646        create_resp.ref, cred, 0, 2, PVFS_BYTE);
647    if(res < 0)
648    {
649        goto exit;
650    }
651
652    do_write(create_resp.ref, cred, strip_size * 4, half_strip, cs);
653
654    res = do_contig_read(
655        create_resp.ref, cred,
656        (strip_size * 3 + half_strip), (strip_size + half_strip), NULL);
657    if(res < 0)
658    {
659        goto exit;
660    }
661
662    res = do_contig_read(
663        create_resp.ref, cred,
664        (strip_size * 3 + half_strip), (strip_size + half_strip), PVFS_BYTE);
665    if(res < 0)
666    {
667        goto exit;
668    }
669
670    res = do_contig_read(
671        create_resp.ref, cred,
672        0, (strip_size * 5), NULL);
673    if(res < 0)
674    {
675        goto exit;
676    }
677
678    res = do_contig_read(
679        create_resp.ref, cred,
680        0, (strip_size * 5), PVFS_BYTE);
681    if(res < 0)
682    {
683        goto exit;
684    }
685
686    res = do_contig_read(
687        create_resp.ref, cred,
688        (strip_size + half_strip), (strip_size + half_strip), NULL);
689    if(res < 0)
690    {
691        goto exit;
692    }
693
694    res = do_contig_read(
695        create_resp.ref, cred,
696        (strip_size + half_strip), (strip_size + half_strip), PVFS_BYTE);
697    if(res < 0)
698    {
699        goto exit;
700    }
701
702    res = do_contig_read(
703        create_resp.ref, cred,
704        0, (strip_size * 3), NULL);
705    if(res < 0)
706    {
707        goto exit;
708    }
709
710    res = do_contig_read(
711        create_resp.ref, cred,
712        0, (strip_size * 3), PVFS_BYTE);
713    if(res < 0)
714    {
715        goto exit;
716    }
717
718    res = do_noncontig_read(
719        create_resp.ref, cred,
720        2,
721        0, strip_size + half_strip,
722        strip_size * 4, half_strip);
723    if(res < 0)
724    {
725        goto exit;
726    }
727   
728    res = do_noncontig_read(
729        create_resp.ref, cred,
730        2,
731        0, strip_size,
732        strip_size * 5, half_strip);
733    if(res < 0)
734    {
735        goto exit;
736    }
737
738    res = do_noncontig_read(
739        create_resp.ref, cred,
740        2,
741        0, strip_size,
742        strip_size * 3, half_strip);
743    if(res < 0)
744    {
745        goto exit;
746    }
747
748    res = do_noncontig_read(
749        create_resp.ref, cred,
750        2,
751        strip_size, strip_size,
752        strip_size * 4 + 2, strip_size);
753    if(res < 0)
754    {
755        goto exit;
756    }
757
758    res = do_noncontig_read(
759        create_resp.ref, cred,
760        9,
761        2, 2,
762        8, 2,
763        14, 2,
764        20, 2,
765        26, 2,
766        32, 2,
767        38, 2,
768        44, 2,
769        50, 2);
770    if(res < 0)
771    {
772        goto exit;
773    }
774
775    res = do_smallmem_noncontig_read(
776        create_resp.ref, cred,
777        strip_size,
778        4,
779        strip_size, half_strip,
780        strip_size * 2, half_strip,
781        strip_size * 3, half_strip,
782        strip_size * 4, half_strip);
783    if(res < 0)
784    {
785        goto exit;
786    }
787   
788    res = do_smallmem_noncontig_read(
789        create_resp.ref, cred,
790        strip_size,
791        2,
792        strip_size, 2,
793        strip_size * 4, strip_size);
794    if(res < 0)
795    {
796        goto exit;
797    }
798   
799    PVFS_sys_remove(
800        zerofill_fname,
801        lookup_resp.ref,
802        cred);
803
804exit:
805   
806    realres = res;
807   
808    res = PVFS_sys_finalize();
809    if(res < 0)
810    {
811        printf("finalizing sysint failed with errcode = %d\n", res);
812        realres = res;
813    }
814
815    return realres;
816}
817
Note: See TracBrowser for help on using the browser.