torture/locktest2.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    byte range lock tester - with local filesystem support
00004    Copyright (C) Andrew Tridgell 1999
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #include "includes.h"
00022 
00023 static fstring password;
00024 static fstring username;
00025 static int got_pass;
00026 static int numops = 1000;
00027 static BOOL showall;
00028 static BOOL analyze;
00029 static BOOL hide_unlock_fails;
00030 static BOOL use_oplocks;
00031 
00032 extern char *optarg;
00033 extern int optind;
00034 
00035 #define FILENAME "\\locktest.dat"
00036 #define LOCKRANGE 100
00037 #define LOCKBASE 0
00038 
00039 /*
00040 #define LOCKBASE (0x40000000 - 50)
00041 */
00042 
00043 #define READ_PCT 50
00044 #define LOCK_PCT 25
00045 #define UNLOCK_PCT 65
00046 #define RANGE_MULTIPLE 1
00047 
00048 #define NSERVERS 2
00049 #define NCONNECTIONS 2
00050 #define NUMFSTYPES 2
00051 #define NFILES 2
00052 #define LOCK_TIMEOUT 0
00053 
00054 #define FSTYPE_SMB 0
00055 #define FSTYPE_NFS 1
00056 
00057 struct record {
00058         char r1, r2;
00059         char conn, f, fstype;
00060         unsigned start, len;
00061         char needed;
00062 };
00063 
00064 static struct record *recorded;
00065 
00066 static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
00067 {
00068         pstring path;
00069 
00070         switch (fstype) {
00071         case FSTYPE_SMB:
00072                 return cli_open(c, fname, flags, DENY_NONE);
00073 
00074         case FSTYPE_NFS:
00075                 slprintf(path, sizeof(path), "%s%s", nfs, fname);
00076                 pstring_sub(path,"\\", "/");
00077                 return open(path, flags, 0666);
00078         }
00079 
00080         return -1;
00081 }
00082 
00083 static BOOL try_close(struct cli_state *c, int fstype, int fd)
00084 {
00085         switch (fstype) {
00086         case FSTYPE_SMB:
00087                 return cli_close(c, fd);
00088 
00089         case FSTYPE_NFS:
00090                 return close(fd) == 0;
00091         }
00092 
00093         return False;
00094 }
00095 
00096 static BOOL try_lock(struct cli_state *c, int fstype, 
00097                      int fd, unsigned start, unsigned len,
00098                      enum brl_type op)
00099 {
00100         struct flock lock;
00101 
00102         switch (fstype) {
00103         case FSTYPE_SMB:
00104                 return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
00105 
00106         case FSTYPE_NFS:
00107                 lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
00108                 lock.l_whence = SEEK_SET;
00109                 lock.l_start = start;
00110                 lock.l_len = len;
00111                 lock.l_pid = getpid();
00112                 return fcntl(fd,F_SETLK,&lock) == 0;
00113         }
00114 
00115         return False;
00116 }
00117 
00118 static BOOL try_unlock(struct cli_state *c, int fstype, 
00119                        int fd, unsigned start, unsigned len)
00120 {
00121         struct flock lock;
00122 
00123         switch (fstype) {
00124         case FSTYPE_SMB:
00125                 return cli_unlock(c, fd, start, len);
00126 
00127         case FSTYPE_NFS:
00128                 lock.l_type = F_UNLCK;
00129                 lock.l_whence = SEEK_SET;
00130                 lock.l_start = start;
00131                 lock.l_len = len;
00132                 lock.l_pid = getpid();
00133                 return fcntl(fd,F_SETLK,&lock) == 0;
00134         }
00135 
00136         return False;
00137 }       
00138 
00139 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, struct process_id pid, 
00140                       enum brl_type lock_type,
00141                       enum brl_flavour lock_flav,
00142                       br_off start, br_off size)
00143 {
00144         printf("%6d   %05x:%05x    %s  %.0f:%.0f(%.0f)\n", 
00145                (int)procid_to_pid(&pid), (int)dev, (int)ino, 
00146                lock_type==READ_LOCK?"R":"W",
00147                (double)start, (double)start+size-1,(double)size);
00148 
00149 }
00150 
00151 /***************************************************** 
00152 return a connection to a server
00153 *******************************************************/
00154 static struct cli_state *connect_one(char *share)
00155 {
00156         struct cli_state *c;
00157         char *server_n;
00158         fstring server;
00159         fstring myname;
00160         static int count;
00161         NTSTATUS nt_status;
00162 
00163         fstrcpy(server,share+2);
00164         share = strchr_m(server,'\\');
00165         if (!share) return NULL;
00166         *share = 0;
00167         share++;
00168 
00169         server_n = server;
00170         
00171         if (!got_pass) {
00172                 char *pass = getpass("Password: ");
00173                 if (pass) {
00174                         fstrcpy(password, pass);
00175                 }
00176         }
00177 
00178         slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
00179 
00180         nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????", 
00181                                         username, lp_workgroup(), password, 0,
00182                                         Undefined, NULL);
00183 
00184         if (!NT_STATUS_IS_OK(nt_status)) {
00185                 DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
00186                 return NULL;
00187         }
00188 
00189         c->use_oplocks = use_oplocks;
00190 
00191         return c;
00192 }
00193 
00194 
00195 static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
00196                       char *nfs[NSERVERS], 
00197                       int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
00198                       char *share1, char *share2)
00199 {
00200         int server, conn, f, fstype;
00201         char *share[2];
00202         share[0] = share1;
00203         share[1] = share2;
00204 
00205         fstype = FSTYPE_SMB;
00206 
00207         for (server=0;server<NSERVERS;server++)
00208         for (conn=0;conn<NCONNECTIONS;conn++) {
00209                 if (cli[server][conn]) {
00210                         for (f=0;f<NFILES;f++) {
00211                                 cli_close(cli[server][conn], fnum[server][fstype][conn][f]);
00212                         }
00213                         cli_ulogoff(cli[server][conn]);
00214                         cli_shutdown(cli[server][conn]);
00215                 }
00216                 cli[server][conn] = connect_one(share[server]);
00217                 if (!cli[server][conn]) {
00218                         DEBUG(0,("Failed to connect to %s\n", share[server]));
00219                         exit(1);
00220                 }
00221         }
00222 }
00223 
00224 
00225 
00226 static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
00227                      char *nfs[NSERVERS],
00228                      int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
00229                      struct record *rec)
00230 {
00231         unsigned conn = rec->conn;
00232         unsigned f = rec->f;
00233         unsigned fstype = rec->fstype;
00234         unsigned start = rec->start;
00235         unsigned len = rec->len;
00236         unsigned r1 = rec->r1;
00237         unsigned r2 = rec->r2;
00238         enum brl_type op;
00239         int server;
00240         BOOL ret[NSERVERS];
00241 
00242         if (r1 < READ_PCT) {
00243                 op = READ_LOCK; 
00244         } else {
00245                 op = WRITE_LOCK; 
00246         }
00247 
00248         if (r2 < LOCK_PCT) {
00249                 /* set a lock */
00250                 for (server=0;server<NSERVERS;server++) {
00251                         ret[server] = try_lock(cli[server][conn], fstype,
00252                                                fnum[server][fstype][conn][f],
00253                                                start, len, op);
00254                 }
00255                 if (showall || ret[0] != ret[1]) {
00256                         printf("lock   conn=%u fstype=%u f=%u range=%u:%u(%u) op=%s -> %u:%u\n",
00257                                conn, fstype, f, 
00258                                start, start+len-1, len,
00259                                op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
00260                                ret[0], ret[1]);
00261                 }
00262                 if (showall) brl_forall(print_brl);
00263                 if (ret[0] != ret[1]) return False;
00264         } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
00265                 /* unset a lock */
00266                 for (server=0;server<NSERVERS;server++) {
00267                         ret[server] = try_unlock(cli[server][conn], fstype,
00268                                                  fnum[server][fstype][conn][f],
00269                                                  start, len);
00270                 }
00271                 if (showall || (!hide_unlock_fails && (ret[0] != ret[1]))) {
00272                         printf("unlock conn=%u fstype=%u f=%u range=%u:%u(%u)       -> %u:%u\n",
00273                                conn, fstype, f, 
00274                                start, start+len-1, len,
00275                                ret[0], ret[1]);
00276                 }
00277                 if (showall) brl_forall(print_brl);
00278                 if (!hide_unlock_fails && ret[0] != ret[1]) return False;
00279         } else {
00280                 /* reopen the file */
00281                 for (server=0;server<NSERVERS;server++) {
00282                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
00283                         fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
00284                                                                  O_RDWR|O_CREAT);
00285                         if (fnum[server][fstype][conn][f] == -1) {
00286                                 printf("failed to reopen on share1\n");
00287                                 return False;
00288                         }
00289                 }
00290                 if (showall) {
00291                         printf("reopen conn=%u fstype=%u f=%u\n",
00292                                conn, fstype, f);
00293                         brl_forall(print_brl);
00294                 }
00295         }
00296         return True;
00297 }
00298 
00299 static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
00300                         char *nfs[NSERVERS],
00301                         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
00302 {
00303         int server, conn, f, fstype; 
00304 
00305         for (server=0;server<NSERVERS;server++)
00306         for (fstype=0;fstype<NUMFSTYPES;fstype++)
00307         for (conn=0;conn<NCONNECTIONS;conn++)
00308         for (f=0;f<NFILES;f++) {
00309                 if (fnum[server][fstype][conn][f] != -1) {
00310                         try_close(cli[server][conn], fstype, fnum[server][fstype][conn][f]);
00311                         fnum[server][fstype][conn][f] = -1;
00312                 }
00313         }
00314         for (server=0;server<NSERVERS;server++) {
00315                 cli_unlink(cli[server][0], FILENAME);
00316         }
00317 }
00318 
00319 static void open_files(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
00320                        char *nfs[NSERVERS],
00321                        int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES])
00322 {
00323         int server, fstype, conn, f; 
00324 
00325         for (server=0;server<NSERVERS;server++)
00326         for (fstype=0;fstype<NUMFSTYPES;fstype++)
00327         for (conn=0;conn<NCONNECTIONS;conn++)
00328         for (f=0;f<NFILES;f++) {
00329                 fnum[server][fstype][conn][f] = try_open(cli[server][conn], nfs[server], fstype, FILENAME,
00330                                                          O_RDWR|O_CREAT);
00331                 if (fnum[server][fstype][conn][f] == -1) {
00332                         fprintf(stderr,"Failed to open fnum[%u][%u][%u][%u]\n",
00333                                 server, fstype, conn, f);
00334                         exit(1);
00335                 }
00336         }
00337 }
00338 
00339 
00340 static int retest(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
00341                   char *nfs[NSERVERS],
00342                   int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
00343                   int n)
00344 {
00345         int i;
00346         printf("testing %u ...\n", n);
00347         for (i=0; i<n; i++) {
00348                 if (i && i % 100 == 0) {
00349                         printf("%u\n", i);
00350                 }
00351 
00352                 if (recorded[i].needed &&
00353                     !test_one(cli, nfs, fnum, &recorded[i])) return i;
00354         }
00355         return n;
00356 }
00357 
00358 
00359 /* each server has two connections open to it. Each connection has two file
00360    descriptors open on the file - 8 file descriptors in total 
00361 
00362    we then do random locking ops in tamdem on the 4 fnums from each
00363    server and ensure that the results match
00364  */
00365 static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath2)
00366 {
00367         struct cli_state *cli[NSERVERS][NCONNECTIONS];
00368         char *nfs[NSERVERS];
00369         int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES];
00370         int n, i, n1; 
00371 
00372         nfs[0] = nfspath1;
00373         nfs[1] = nfspath2;
00374 
00375         ZERO_STRUCT(fnum);
00376         ZERO_STRUCT(cli);
00377 
00378         recorded = SMB_MALLOC_ARRAY(struct record, numops);
00379 
00380         for (n=0; n<numops; n++) {
00381                 recorded[n].conn = random() % NCONNECTIONS;
00382                 recorded[n].fstype = random() % NUMFSTYPES;
00383                 recorded[n].f = random() % NFILES;
00384                 recorded[n].start = LOCKBASE + ((unsigned)random() % (LOCKRANGE-1));
00385                 recorded[n].len = 1 + 
00386                         random() % (LOCKRANGE-(recorded[n].start-LOCKBASE));
00387                 recorded[n].start *= RANGE_MULTIPLE;
00388                 recorded[n].len *= RANGE_MULTIPLE;
00389                 recorded[n].r1 = random() % 100;
00390                 recorded[n].r2 = random() % 100;
00391                 recorded[n].needed = True;
00392         }
00393 
00394         reconnect(cli, nfs, fnum, share1, share2);
00395         open_files(cli, nfs, fnum);
00396         n = retest(cli, nfs, fnum, numops);
00397 
00398         if (n == numops || !analyze) return;
00399         n++;
00400 
00401         while (1) {
00402                 n1 = n;
00403 
00404                 close_files(cli, nfs, fnum);
00405                 reconnect(cli, nfs, fnum, share1, share2);
00406                 open_files(cli, nfs, fnum);
00407 
00408                 for (i=0;i<n-1;i++) {
00409                         int m;
00410                         recorded[i].needed = False;
00411 
00412                         close_files(cli, nfs, fnum);
00413                         open_files(cli, nfs, fnum);
00414 
00415                         m = retest(cli, nfs, fnum, n);
00416                         if (m == n) {
00417                                 recorded[i].needed = True;
00418                         } else {
00419                                 if (i < m) {
00420                                         memmove(&recorded[i], &recorded[i+1],
00421                                                 (m-i)*sizeof(recorded[0]));
00422                                 }
00423                                 n = m;
00424                                 i--;
00425                         }
00426                 }
00427 
00428                 if (n1 == n) break;
00429         }
00430 
00431         close_files(cli, nfs, fnum);
00432         reconnect(cli, nfs, fnum, share1, share2);
00433         open_files(cli, nfs, fnum);
00434         showall = True;
00435         n1 = retest(cli, nfs, fnum, n);
00436         if (n1 != n-1) {
00437                 printf("ERROR - inconsistent result (%u %u)\n", n1, n);
00438         }
00439         close_files(cli, nfs, fnum);
00440 
00441         for (i=0;i<n;i++) {
00442                 printf("{%u, %u, %u, %u, %u, %u, %u, %u},\n",
00443                        recorded[i].r1,
00444                        recorded[i].r2,
00445                        recorded[i].conn,
00446                        recorded[i].fstype,
00447                        recorded[i].f,
00448                        recorded[i].start,
00449                        recorded[i].len,
00450                        recorded[i].needed);
00451         }       
00452 }
00453 
00454 
00455 
00456 static void usage(void)
00457 {
00458         printf(
00459 "Usage:\n\
00460   locktest //server1/share1 //server2/share2 /path1 /path2 [options..]\n\
00461   options:\n\
00462         -U user%%pass\n\
00463         -s seed\n\
00464         -o numops\n\
00465         -u          hide unlock fails\n\
00466         -a          (show all ops)\n\
00467         -O          use oplocks\n\
00468 ");
00469 }
00470 
00471 /****************************************************************************
00472   main program
00473 ****************************************************************************/
00474  int main(int argc,char *argv[])
00475 {
00476         char *share1, *share2, *nfspath1, *nfspath2;
00477         int opt;
00478         char *p;
00479         int seed;
00480 
00481         setlinebuf(stdout);
00482 
00483         dbf = x_stderr;
00484 
00485         if (argc < 5 || argv[1][0] == '-') {
00486                 usage();
00487                 exit(1);
00488         }
00489 
00490         share1 = argv[1];
00491         share2 = argv[2];
00492         nfspath1 = argv[3];
00493         nfspath2 = argv[4];
00494 
00495         all_string_sub(share1,"/","\\",0);
00496         all_string_sub(share2,"/","\\",0);
00497 
00498         setup_logging(argv[0],True);
00499 
00500         argc -= 4;
00501         argv += 4;
00502 
00503         lp_load(dyn_CONFIGFILE,True,False,False,True);
00504         load_interfaces();
00505 
00506         if (getenv("USER")) {
00507                 fstrcpy(username,getenv("USER"));
00508         }
00509 
00510         seed = time(NULL);
00511 
00512         while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
00513                 switch (opt) {
00514                 case 'U':
00515                         fstrcpy(username,optarg);
00516                         p = strchr_m(username,'%');
00517                         if (p) {
00518                                 *p = 0;
00519                                 fstrcpy(password, p+1);
00520                                 got_pass = 1;
00521                         }
00522                         break;
00523                 case 's':
00524                         seed = atoi(optarg);
00525                         break;
00526                 case 'u':
00527                         hide_unlock_fails = True;
00528                         break;
00529                 case 'o':
00530                         numops = atoi(optarg);
00531                         break;
00532                 case 'O':
00533                         use_oplocks = True;
00534                         break;
00535                 case 'a':
00536                         showall = True;
00537                         break;
00538                 case 'A':
00539                         analyze = True;
00540                         break;
00541                 case 'h':
00542                         usage();
00543                         exit(1);
00544                 default:
00545                         printf("Unknown option %c (%d)\n", (char)opt, opt);
00546                         exit(1);
00547                 }
00548         }
00549 
00550         argc -= optind;
00551         argv += optind;
00552 
00553         DEBUG(0,("seed=%u\n", seed));
00554         srandom(seed);
00555 
00556         locking_init(1);
00557         test_locks(share1, share2, nfspath1, nfspath2);
00558 
00559         return(0);
00560 }

Sambaに対してSat Aug 29 21:23:27 2009に生成されました。  doxygen 1.4.7