remove a debug message
[amitay/dbench.git] / smb.c
1 /* 
2    Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2009
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8    
9    This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #undef _GNU_SOURCE
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <stdint.h>
25 #include <libsmbclient.h>
26 #include "dbench.h"
27
28 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
29
30 static char *smb_domain;
31 static char *smb_user;
32 static char *smb_password;
33 static char *smb_server;
34 static char *smb_share;
35
36 typedef struct _data_t {
37         const char *dptr;
38         int dsize;
39 } data_t;
40
41 typedef struct _smb_handle_t {
42         int fd;
43 } smb_handle_t;
44
45 /* a tree to store all open handles, indexed by path */
46 typedef struct _tree_t {
47         data_t path;
48         smb_handle_t handle;
49         struct _tree_t *parent;
50         struct _tree_t *left;
51         struct _tree_t *right;
52 } tree_t;
53
54 static tree_t *open_files;
55
56
57 static tree_t *find_path(tree_t *tree, const char *path)
58 {
59         int i;
60
61         if (tree == NULL) {
62                 return NULL;
63         }
64
65         i = strcmp(path, tree->path.dptr);
66         if (i == 0) {
67                 return tree;
68         }
69         if (i < 0) {
70                 return find_path(tree->left, path);
71         }
72
73         return find_path(tree->right, path);
74 }
75
76 static smb_handle_t *lookup_path(const char *path)
77 {
78         tree_t *t;
79
80         t = find_path(open_files, path);
81         if (t == NULL) {
82                 return NULL;
83         }
84
85         return &t->handle;
86 }
87
88 static void free_node(tree_t *t)
89 {
90         free(discard_const(t->path.dptr));
91         free(t);
92 }
93
94 static void delete_path(const char *path)
95 {
96         tree_t *t;
97
98         t = find_path(open_files, path);
99         if (t == NULL) {
100                 return;
101         }
102
103         /* we have a left child */
104         if (t->left) {
105                 tree_t *tmp_tree;
106
107                 for(tmp_tree=t->left;tmp_tree->right;tmp_tree=tmp_tree->right)
108                         ;
109                 tmp_tree->right = t->right;
110                 if (t->right) {
111                         t->right->parent = tmp_tree;
112                 }
113
114                 if (t->parent == NULL) {
115                         open_files = tmp_tree;
116                         tmp_tree->parent = NULL;
117                         free_node(t);
118                         return;
119                 }
120
121                 if (t->parent->left == t) {
122                         t->parent->left = t->left;
123                         if (t->left) {
124                                 t->left->parent = t->parent;
125                         }
126                         free_node(t);
127                         return;
128                 }
129
130                 t->parent->right = t->left;
131                 if (t->left) {
132                         t->left->parent = t->parent;
133                 }
134                 free_node(t);
135                 return;
136         }
137
138         /* we only have a right child */
139         if (t->right) {
140                 tree_t *tmp_tree;
141
142                 for(tmp_tree=t->right;tmp_tree->left;tmp_tree=tmp_tree->left)
143                         ;
144                 tmp_tree->left = t->left;
145                 if (t->left) {
146                         t->left->parent = tmp_tree;
147                 }
148
149                 if (t->parent == NULL) {
150                         open_files = tmp_tree;
151                         tmp_tree->parent = NULL;
152                         free_node(t);
153                         return;
154                 }
155
156                 if (t->parent->left == t) {
157                         t->parent->left = t->right;
158                         if (t->right) {
159                                 t->right->parent = t->parent;
160                         }
161                         free_node(t);
162                         return;
163                 }
164
165                 t->parent->right = t->right;
166                 if (t->right) {
167                         t->right->parent = t->parent;
168                 }
169                 free_node(t);
170                 return;
171         }
172
173         /* we are a leaf node */
174         if (t->parent == NULL) {
175                 open_files = NULL;
176         } else {
177                 if (t->parent->left == t) {
178                         t->parent->left = NULL;
179                 } else {
180                         t->parent->right = NULL;
181                 }
182         }
183         free_node(t);
184         return;
185 }
186
187 static void insert_path(const char *path, smb_handle_t *hnd)
188 {
189         tree_t *tmp_t;
190         tree_t *t;
191         int i;
192
193         tmp_t = find_path(open_files, path);
194         if (tmp_t != NULL) {
195                 delete_path(path);
196         }
197
198         t = malloc(sizeof(tree_t));
199         if (t == NULL) {
200                 fprintf(stderr, "MALLOC failed to allocate tree_t in insert_fhandle\n");
201                 exit(10);
202         }
203
204         t->path.dptr = strdup(path);
205         if (t->path.dptr == NULL) {
206                 fprintf(stderr, "STRDUP failed to allocate key in insert_fhandle\n");
207                 exit(10);
208         }
209         t->path.dsize = strlen(path);
210
211         t->handle = *hnd;
212
213         t->left   = NULL;
214         t->right  = NULL;
215         t->parent = NULL;
216
217         if (open_files == NULL) {
218                 open_files = t;
219                 return;
220         }
221
222         tmp_t = open_files;
223 again:
224         i = strcmp(t->path.dptr, tmp_t->path.dptr);
225         if (i == 0) {
226                 tmp_t->handle = t->handle;
227                 free(discard_const(t->path.dptr));
228                 free(t);
229                 return;
230         }
231         if (i < 0) {
232                 if (tmp_t->left == NULL) {
233                         tmp_t->left = t;
234                         t->parent = tmp_t;
235                         return;
236                 }
237                 tmp_t = tmp_t->left;
238                 goto again;
239         }
240         if (tmp_t->right == NULL) {
241                 tmp_t->right = t;
242                 t->parent = tmp_t;
243                 return;
244         }
245         tmp_t = tmp_t->right;
246         goto again;
247 }
248
249
250
251
252
253 struct smb_child {
254         SMBCCTX *ctx;
255 };
256
257 static void failed(struct child_struct *child)
258 {
259         child->failed = 1;
260         fprintf(stderr, "ERROR: child %d failed at line %d\n", child->id, child->line);
261         exit(1);
262 }
263
264 static int check_status(int ret, const char *status)
265 {
266         if (!strcmp(status, "*")) {
267                 return 0;
268         }
269
270         if ((!strcmp(status, "SUCCESS")) && (ret == 0)) {
271                 return 0;
272         }
273
274         if ((!strcmp(status, "ERROR")) && (ret != 0)) {
275                 return 0;
276         }
277
278         return 1;
279 }
280
281
282 void smb_auth_fn(const char *server, const char *share, char *wrkgrp, int wrkgrplen, char *user, int userlen, char *passwd, int passwdlen)
283 {
284         (void)server;
285         (void)share;
286
287         if (smb_domain != NULL) {
288                 strncpy(wrkgrp, smb_domain, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
289         }
290         strncpy(user, smb_user, userlen - 1); user[userlen - 1] = 0;
291         strncpy(passwd, smb_password, passwdlen - 1); passwd[passwdlen - 1] = 0;
292 }
293
294 static int smb_init(void)
295 {
296         SMBCCTX *ctx;
297         char *tmp;
298         int ret;
299         char *str;
300
301         if (options.smb_share == NULL) {
302                 fprintf(stderr, "You must specify --smb-share=<share> with the \"smb\" backend.\n");
303                 return 1;
304         }
305         if (options.smb_share[0] != '/' || options.smb_share[1] != '/') {
306                 fprintf(stderr, "--smb-share Must be of the form //SERVER/SHARE[/PATH]\n");
307                 return 1;
308         }
309         smb_server = strdup(options.smb_share+2);
310         tmp = index(smb_server, '/');
311         if (tmp == NULL) {
312                 fprintf(stderr, "--smb-share Must be of the form //SERVER/SHARE[/PATH]\n");
313                 return 1;
314         }
315         *tmp = '\0';
316         smb_share = tmp+1;              
317
318
319         if (options.smb_user == NULL) {
320                 fprintf(stderr, "You must specify --smb-user=[<domain>/]<user>%%<password> with the \"smb\" backend.\n");
321                 return 1;
322         }
323
324         smb_domain = strdup(options.smb_user);
325         tmp = index(smb_domain, '/');
326         if (tmp == NULL) {
327                 smb_user = smb_domain;
328                 smb_domain = NULL;
329         } else {
330                 smb_user = tmp+1;
331                 *tmp = '\0';
332         }
333         tmp = index(smb_user, '%');
334         if (tmp == NULL) {
335                 smb_password = NULL;
336         } else {
337                 smb_password = tmp+1;
338                 *tmp = '\0';
339         }
340
341         ctx = smbc_new_context();
342         if (ctx == NULL) {
343                 fprintf(stderr, "Could not allocate SMB Context\n");
344                 return 1;
345         }
346
347         smbc_setDebug(ctx, 0);
348         smbc_setFunctionAuthData(ctx, smb_auth_fn);
349
350         if (!smbc_init_context(ctx)) {
351                 smbc_free_context(ctx, 0);
352                 fprintf(stderr, "failed to initialize context\n");
353                 return 1;
354         }
355         smbc_set_context(ctx);
356
357         asprintf(&str, "smb://%s/%s", smb_server, smb_share);
358         ret = smbc_opendir(str);
359         free(str);
360
361         if (ret == -1) {
362                 fprintf(stderr, "Failed to access //%s/%s\n", smb_server, smb_share);
363                 return 1;
364         }
365
366         smbc_free_context(ctx, 1);
367         return 0;
368 }
369
370 static void smb_setup(struct child_struct *child)
371 {
372         struct smb_child *ctx;
373
374         ctx = malloc(sizeof(struct smb_child));
375         if (ctx == NULL) {
376                 fprintf(stderr, "Failed to malloc child ctx\n");
377                 exit(10);
378         }
379         child->private =ctx;
380
381         ctx->ctx = smbc_new_context();
382         if (ctx->ctx == NULL) {
383                 fprintf(stderr, "Could not allocate SMB Context\n");
384                 exit(10);
385         }
386
387         smbc_setDebug(ctx->ctx, 0);
388         smbc_setFunctionAuthData(ctx->ctx, smb_auth_fn);
389
390         if (!smbc_init_context(ctx->ctx)) {
391                 smbc_free_context(ctx->ctx, 0);
392                 fprintf(stderr, "failed to initialize context\n");
393                 exit(10);
394         }
395         smbc_setOptionUrlEncodeReaddirEntries(ctx->ctx, True);
396         smbc_set_context(ctx->ctx);
397 }
398
399 static void smb_mkdir(struct dbench_op *op)
400 {
401         char *str;
402         const char *dir;
403         int ret;
404
405         dir = op->fname + 2;
406
407         asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, dir);
408
409         ret = smbc_mkdir(str, 0777);
410         free(str);
411
412         if (check_status(ret, op->status)) {
413                 fprintf(stderr, "[%d] MKDIR \"%s\" failed - expected %s, got %d\n", op->child->line, dir, op->status, ret);
414                 failed(op->child);
415         }
416 }
417
418 static void smb_rmdir(struct dbench_op *op)
419 {
420         char *str;
421         const char *dir;
422         int ret;
423
424         dir = op->fname + 2;
425         asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, dir);
426         ret = smbc_rmdir(str);
427         free(str);
428
429         if (check_status(ret, op->status)) {
430                 fprintf(stderr, "[%d] RMDIR \"%s\" failed - expected %s, got %d\n", op->child->line, dir, op->status, ret);
431                 failed(op->child);
432         }
433 }
434
435 static void smb_open(struct dbench_op *op)
436 {
437         const char *file;
438         char *str;
439         int flags = 0;
440         smb_handle_t hnd;
441
442         if (op->params[0] & 0x01) {
443                 flags |= O_RDONLY;
444         }
445         if (op->params[0] & 0x02) {
446                 flags |= O_WRONLY;
447         }
448         if (op->params[0] & 0x04) {
449                 flags |= O_RDWR;
450         }
451         if (op->params[0] & 0x08) {
452                 flags |= O_CREAT;
453         }
454         if (op->params[0] & 0x10) {
455                 flags |= O_EXCL;
456         }
457         if (op->params[0] & 0x20) {
458                 flags |= O_TRUNC;
459         }
460         if (op->params[0] & 0x40) {
461                 flags |= O_APPEND;
462         }
463
464         file = op->fname + 2;
465         asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, file);
466
467         hnd.fd = smbc_open(str, flags, 0777);
468         free(str);
469
470         if (check_status(hnd.fd<0?-1:0, op->status)) {
471                 fprintf(stderr, "[%d] OPEN \"%s\" failed\n", op->child->line, file);
472                 failed(op->child);
473                 return;
474         }
475
476         insert_path(file, &hnd);
477
478 }
479
480 static void smb_close(struct dbench_op *op)
481 {
482         smb_handle_t *hnd;
483         const char *file;
484         int ret;
485
486         file = op->fname + 2;
487
488         hnd = lookup_path(file);
489         if (hnd == NULL) {
490                 fprintf(stderr, "[%d] CLOSE \"%s\" failed. This file is not open.\n", op->child->line, file);
491                 failed(op->child);
492                 return;
493         }
494                 
495         ret = smbc_close(hnd->fd);
496         delete_path(file);
497
498         if (check_status(ret, op->status)) {
499                 fprintf(stderr, "[%d] CLOSE \"%s\" failed\n", op->child->line, file);
500                 failed(op->child);
501                 return;
502         }
503 }
504
505 static void smb_write(struct dbench_op *op)
506 {
507         smb_handle_t *hnd;
508         const char *file;
509         int ret;
510         size_t length;
511         off_t offset;
512         char garbage[65536];
513
514         offset = op->params[0];
515         length = op->params[1];
516         if (length > 65536) {
517                 length = 65536;
518         }
519
520         file = op->fname + 2;
521
522         hnd = lookup_path(file);
523         if (hnd == NULL) {
524                 fprintf(stderr, "[%d] WRITE \"%s\" failed. This file is not open.\n", op->child->line, file);
525                 failed(op->child);
526                 return;
527         }
528
529
530         smbc_lseek(hnd->fd, offset, SEEK_SET);
531         ret = smbc_write(hnd->fd, garbage, length);
532
533         if (check_status(ret==(int)length?0:-1, op->status)) {
534                 fprintf(stderr, "[%d] WRITE \"%s\" failed\n", op->child->line, file);
535                 failed(op->child);
536                 return;
537         }
538         op->child->bytes += length;
539 }
540
541 static void smb_read(struct dbench_op *op)
542 {
543         smb_handle_t *hnd;
544         const char *file;
545         int ret;
546         size_t length;
547         off_t offset;
548         char garbage[65536];
549
550         offset = op->params[0];
551         length = op->params[1];
552         if (length > 65536) {
553                 length = 65536;
554         }
555
556         file = op->fname + 2;
557
558         hnd = lookup_path(file);
559         if (hnd == NULL) {
560                 fprintf(stderr, "[%d] READ \"%s\" failed. This file is not open.\n", op->child->line, file);
561                 failed(op->child);
562                 return;
563         }
564
565
566         smbc_lseek(hnd->fd, offset, SEEK_SET);
567         ret = smbc_read(hnd->fd, garbage, length);
568
569         if (check_status(ret==(int)length?0:-1, op->status)) {
570                 fprintf(stderr, "[%d] READ \"%s\" failed\n", op->child->line, file);
571                 failed(op->child);
572                 return;
573         }
574         op->child->bytes += length;
575 }
576
577
578 static void smb_unlink(struct dbench_op *op)
579 {
580         const char *path;
581         char *str;
582         int ret;
583
584         path = op->fname + 2;
585         asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, path);
586
587         ret = smbc_unlink(str);
588         free(str);
589
590         if (check_status(ret, op->status)) {
591                 fprintf(stderr, "[%d] UNLINK \"%s\" failed\n", op->child->line, path);
592                 failed(op->child);
593                 return;
594         }
595 }
596
597 static void recursive_delete_tree(struct dbench_op *op, const char *url)
598 {
599         int dir;
600         struct smbc_dirent *dirent;
601
602         dir = smbc_opendir(url);
603         if (dir < 0) {
604                 fprintf(stderr, "[%d] Deltree \"%s\" failed\n", op->child->line, url);
605                 failed(op->child);
606         }
607         while((dirent = smbc_readdir(dir))) {
608                 char *path;
609
610                 asprintf(&path, "%s/%s", url, dirent->name);
611                 if (!strcmp(dirent->name, ".")) {
612                         continue;
613                 }
614                 if (!strcmp(dirent->name, "..")) {
615                         continue;
616                 }
617                 if (dirent->smbc_type == SMBC_DIR) {
618                         recursive_delete_tree(op, path);
619                         smbc_rmdir(path);
620                 } else {
621                         smbc_unlink(path);
622                 }
623                 free(path);
624         }
625         smbc_closedir(dir);
626
627         return;
628 }
629
630 static void smb_readdir(struct dbench_op *op)
631 {
632         const char *path;
633         char *url;
634         int dir;
635
636         path = op->fname + 2;
637         asprintf(&url, "smb://%s/%s/%s", smb_server, smb_share, path);
638
639         dir = smbc_opendir(url);
640         free(url);
641         if (dir < 0) {
642                 fprintf(stderr, "[%d] READDIR \"%s\" failed\n", op->child->line, url);
643                 failed(op->child);
644         }
645         smbc_closedir(dir);
646 }
647
648 static void smb_deltree(struct dbench_op *op)
649 {
650         const char *path;
651         char *url;
652
653         path = op->fname + 2;
654         asprintf(&url, "smb://%s/%s/%s", smb_server, smb_share, path);
655         recursive_delete_tree(op, url);
656         free(url);
657 }
658
659 static void smb_cleanup(struct child_struct *child)
660 {
661         struct smb_child *ctx = child->private;
662         char *url;
663         struct dbench_op fake_op;
664
665         asprintf(&url, "smb://%s/%s", smb_server, smb_share);
666         recursive_delete_tree(&fake_op, url);
667         free(url);
668
669         smbc_free_context(ctx->ctx, 1);
670         free(ctx);      
671 }
672
673
674         
675 static struct backend_op ops[] = {
676         { "Deltree",  smb_deltree },
677         { "CLOSE", smb_close },
678         { "MKDIR", smb_mkdir },
679         { "OPEN", smb_open },
680         { "READ", smb_read },
681         { "READDIR", smb_readdir },
682         { "RMDIR", smb_rmdir },
683         { "UNLINK", smb_unlink },
684         { "WRITE", smb_write },
685         { NULL, NULL}
686 };
687
688 struct nb_operations smb_ops = {
689         .backend_name = "smbbench",
690         .init         = smb_init,
691         .setup        = smb_setup,
692         .cleanup      = smb_cleanup,
693         .ops          = ops
694 };