Whamcloud - gitweb
LU-13998 tests: sanityn test_104 incorrect MDT times shown
[fs/lustre-release.git] / lustre / utils / ofd_access_log_reader.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  *
22  * Copyright 2020, DataDirect Networks Storage.
23  *
24  * This file is part of Lustre, http://www.lustre.org/
25  *
26  * Author: John L. Hammond <jhammond@whamcloud.com>
27  *
28  * lustre/utils/ofd_access_log_reader.c
29  *
30  * Sample utility to discover and read Lustre (ofd) access logs.
31  *
32  * This demonstrates the discovery and reading of Lustre access logs
33  * (see linux/lustre/lustre_access_log.h and
34  * lustre/ofd/ofd_access_log.c.). By default it opens the control
35  * device, discovers and opens all access log devices, and consumes
36  * all access log entries. If invoked with the --list option then it
37  * prints information about all available devices to stdout and exits.
38  *
39  * Structured trace points (when --trace is used) are added to permit
40  * testing of the access log functionality (see test_165* in
41  * lustre/tests/sanity.sh).
42  */
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <assert.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <getopt.h>
51 #include <inttypes.h>
52 #include <limits.h>
53 #include <malloc.h>
54 #include <signal.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <sys/epoll.h>
58 #include <sys/ioctl.h>
59 #include <sys/signalfd.h>
60 #include <sys/stat.h>
61 #include <sys/sysmacros.h>
62 #include <sys/timerfd.h>
63 #include <sys/types.h>
64 #include <linux/types.h>
65 #include <linux/lustre/lustre_user.h>
66 #include <linux/lustre/lustre_access_log.h>
67 #include "ofd_access_batch.h"
68 #include "lstddef.h"
69
70 /* TODO fsname filter */
71
72 static FILE *debug_file;
73 static FILE *trace_file;
74
75 #define DEBUG(fmt, args...)                                             \
76         do {                                                            \
77                 if (debug_file != NULL)                                 \
78                         fprintf(debug_file, "DEBUG %s:%d: "fmt, __func__, __LINE__, ##args); \
79         } while (0)
80
81 #define TRACE(fmt, args...)                                             \
82         do {                                                            \
83                 if (trace_file != NULL)                                 \
84                         fprintf(trace_file, "TRACE "fmt, ##args);       \
85         } while (0)
86
87 #define DEBUG_D(x) DEBUG("%s = %"PRIdMAX"\n", #x, (intmax_t)x)
88 #define DEBUG_P(x) DEBUG("%s = %p\n", #x, x)
89 #define DEBUG_S(x) DEBUG("%s = '%s'\n", #x, x)
90 #define DEBUG_U(x) DEBUG("%s = %"PRIuMAX"\n", #x, (uintmax_t)x)
91
92 #define ERROR(fmt, args...) \
93         fprintf(stderr, "%s: "fmt, program_invocation_short_name, ##args)
94
95 #define FATAL(fmt, args...)                     \
96         do {                                    \
97                 ERROR("FATAL: "fmt, ##args);    \
98                 exit(EXIT_FAILURE);             \
99         } while (0)
100
101 enum {
102         ALR_EXIT_SUCCESS = INT_MIN + EXIT_SUCCESS,
103         ALR_EXIT_FAILURE = INT_MIN + EXIT_FAILURE,
104         ALR_ERROR = -1,
105         ALR_EOF = 0,
106         ALR_OK = 1,
107 };
108
109 struct alr_dev {
110         char *alr_name;
111         int (*alr_io)(int /* epoll_fd */, struct alr_dev * /* this */, unsigned int /* mask */);
112         void (*alr_destroy)(struct alr_dev *);
113         int alr_fd;
114 };
115
116 struct alr_log {
117         struct alr_dev alr_dev;
118         char *alr_buf;
119         size_t alr_buf_size;
120         size_t alr_entry_size;
121         dev_t alr_rdev;
122 };
123
124 static struct alr_log *alr_log[1 << 20]; /* 20 == MINORBITS */
125 static int oal_version; /* FIXME ... major version, minor version */
126 static unsigned int oal_log_major;
127 static unsigned int oal_log_minor_max;
128 static struct alr_batch *alr_batch;
129 static FILE *alr_batch_file;
130 static const char *alr_batch_file_path;
131 static int alr_print_fraction = 100;
132
133 #define D_ALR_DEV "%s %d"
134 #define P_ALR_DEV(ad) \
135         (ad)->alr_name, (ad)->alr_fd
136
137 #define D_ALR_LOG D_ALR_DEV" %u:%u"
138 #define P_ALR_LOG(al) \
139         P_ALR_DEV(&(al)->alr_dev), major((al)->alr_rdev), minor((al)->alr_rdev)
140
141 static void alr_dev_free(int epoll_fd, struct alr_dev *ad)
142 {
143         TRACE("alr_dev_free %s\n", ad->alr_name);
144
145         if (!(ad->alr_fd < 0))
146                 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, ad->alr_fd, NULL);
147
148         if (ad->alr_destroy != NULL)
149                 (*ad->alr_destroy)(ad);
150
151         if (!(ad->alr_fd < 0))
152                 close(ad->alr_fd);
153
154         free(ad->alr_name);
155         free(ad);
156 }
157
158 static struct alr_log **alr_log_lookup(dev_t rdev)
159 {
160         assert(major(rdev) == oal_log_major);
161
162         if (!(minor(rdev) < ARRAY_SIZE(alr_log)))
163                 return NULL;
164
165         return &alr_log[minor(rdev)];
166 }
167
168 static const char *alr_flags_to_str(unsigned int flags)
169 {
170         switch (flags & (OFD_ACCESS_READ | OFD_ACCESS_WRITE)) {
171         default:
172                 return "0";
173         case OFD_ACCESS_READ:
174                 return "r";
175         case OFD_ACCESS_WRITE:
176                 return "w";
177         case OFD_ACCESS_READ | OFD_ACCESS_WRITE:
178                 return "rw";
179         }
180 }
181
182 /* /dev/lustre-access-log/scratch-OST0000 device poll callback: read entries
183  * from log and print. */
184 static int alr_log_io(int epoll_fd, struct alr_dev *ad, unsigned int mask)
185 {
186         struct alr_log *al = container_of(ad, struct alr_log, alr_dev);
187         ssize_t i, count;
188
189         TRACE("alr_log_io %s\n", ad->alr_name);
190         DEBUG_U(mask);
191
192         assert(al->alr_entry_size != 0);
193         assert(al->alr_buf_size != 0);
194         assert(al->alr_buf != NULL);
195
196         count = read(ad->alr_fd, al->alr_buf, al->alr_buf_size);
197         if (count < 0) {
198                 ERROR("cannot read events from '%s': %s\n", ad->alr_name, strerror(errno));
199                 return ALR_ERROR;
200         }
201
202         if (count == 0) {
203                 TRACE("alr_log_eof %s\n", ad->alr_name);
204                 return ALR_EOF;
205         }
206
207         if (count % al->alr_entry_size != 0) {
208                 ERROR("invalid read from "D_ALR_LOG": entry_size = %zu, count = %zd\n",
209                         P_ALR_LOG(al), al->alr_entry_size, count);
210                 return ALR_ERROR;
211         }
212
213         DEBUG("read "D_ALR_LOG", count = %zd\n", P_ALR_LOG(al), count);
214
215         for (i = 0; i < count; i += al->alr_entry_size) {
216                 struct ofd_access_entry_v1 *oae =
217                         (struct ofd_access_entry_v1 *)&al->alr_buf[i];
218
219                 TRACE("alr_log_entry %s "DFID" %lu %lu %lu %u %u %s\n",
220                         ad->alr_name,
221                         PFID(&oae->oae_parent_fid),
222                         (unsigned long)oae->oae_begin,
223                         (unsigned long)oae->oae_end,
224                         (unsigned long)oae->oae_time,
225                         (unsigned int)oae->oae_size,
226                         (unsigned int)oae->oae_segment_count,
227                         alr_flags_to_str(oae->oae_flags));
228
229                 alr_batch_add(alr_batch, ad->alr_name, &oae->oae_parent_fid,
230                         oae->oae_time, oae->oae_begin, oae->oae_end,
231                         oae->oae_size, oae->oae_segment_count, oae->oae_flags);
232         }
233
234         return ALR_OK;
235 }
236
237 static void alr_log_destroy(struct alr_dev *ad)
238 {
239         struct alr_log *al = container_of(ad, struct alr_log, alr_dev);
240         struct alr_log **pal;
241
242         TRACE("alr_log_free %s\n", ad->alr_name);
243         assert(major(al->alr_rdev) == oal_log_major);
244
245         pal = alr_log_lookup(al->alr_rdev);
246         if (pal != NULL && *pal == al)
247                 *pal = NULL;
248
249         free(al->alr_buf);
250         al->alr_buf = NULL;
251         al->alr_buf_size = 0;
252 }
253
254 /* Add an access log (identified by path) to the epoll set. */
255 static int alr_log_add(int epoll_fd, const char *path)
256 {
257         struct alr_log **pal, *al = NULL;
258         struct stat st;
259         int fd = -1;
260         int rc;
261
262         DEBUG_S(path);
263
264         fd = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
265         if (fd < 0) {
266                 ERROR("cannot open device '%s': %s\n", path, strerror(errno));
267                 rc = (errno == ENOENT ? 0 : -1); /* Possible race. */
268                 goto out;
269         }
270
271         /* Revalidate rdev in case of race. */
272         rc = fstat(fd, &st);
273         if (rc < 0) {
274                 ERROR("cannot stat '%s': %s\n", path, strerror(errno));
275                 goto out;
276         }
277
278         if (major(st.st_rdev) != oal_log_major)
279                 goto out;
280
281         pal = alr_log_lookup(st.st_rdev);
282         if (pal == NULL) {
283                 ERROR("no device slot available for '%s' with minor %u\n",
284                         path, minor(st.st_rdev));
285                 goto out;
286         }
287
288         if (*pal != NULL)
289                 goto out; /* We already have this device. */
290
291         struct lustre_access_log_info_v1 lali;
292
293         memset(&lali, 0, sizeof(lali));
294
295         rc = ioctl(fd, LUSTRE_ACCESS_LOG_IOCTL_INFO, &lali);
296         if (rc < 0) {
297                 ERROR("cannot get info for device '%s': %s\n",
298                         path, strerror(errno));
299                 goto out;
300         }
301
302         if (lali.lali_type != LUSTRE_ACCESS_LOG_TYPE_OFD) {
303                 rc = 0;
304                 goto out;
305         }
306
307         al = calloc(1, sizeof(*al));
308         if (al == NULL)
309                 FATAL("cannot allocate struct alr_dev of size %zu: %s\n",
310                         sizeof(*al), strerror(errno));
311
312         al->alr_dev.alr_io = &alr_log_io;
313         al->alr_dev.alr_destroy = &alr_log_destroy;
314         al->alr_dev.alr_fd = fd;
315         fd = -1;
316
317         al->alr_rdev = st.st_rdev;
318
319         al->alr_dev.alr_name = strdup(lali.lali_name);
320         if (al->alr_dev.alr_name == NULL)
321                 FATAL("cannot copy name of size %zu: %s\n",
322                         strlen(lali.lali_name), strerror(errno));
323
324         al->alr_buf_size = lali.lali_log_size;
325         al->alr_entry_size = lali.lali_entry_size;
326
327         if (al->alr_entry_size == 0) {
328                 ERROR("device '%s' has zero entry size\n", path);
329                 rc = -1;
330                 goto out;
331         }
332
333         if (al->alr_buf_size == 0)
334                 al->alr_buf_size = 1048576;
335
336         al->alr_buf_size = roundup(al->alr_buf_size, al->alr_entry_size);
337
338         al->alr_buf = malloc(al->alr_buf_size);
339         if (al->alr_buf == NULL)
340                 FATAL("cannot allocate log buffer for '%s' of size %zu: %s\n",
341                         path, al->alr_buf_size, strerror(errno));
342
343         struct epoll_event ev = {
344                 .events = EPOLLIN | EPOLLHUP,
345                 .data.ptr = &al->alr_dev,
346         };
347
348         rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, al->alr_dev.alr_fd, &ev);
349         if (rc < 0) {
350                 ERROR("cannot add device '%s' to epoll set: %s\n",
351                         path, strerror(errno));
352                 goto out;
353         }
354
355         TRACE("alr_log_add %s\n", al->alr_dev.alr_name);
356
357         if (oal_log_minor_max < minor(al->alr_rdev))
358                 oal_log_minor_max = minor(al->alr_rdev);
359
360         assert(*pal == NULL);
361         *pal = al;
362         al = NULL;
363         rc = 0;
364 out:
365         if (al != NULL)
366                 alr_dev_free(epoll_fd, &al->alr_dev);
367
368         if (!(fd < 0))
369                 close(fd);
370
371         return rc;
372 }
373
374 /* Scan /dev/lustre-access-log/ for new access log devices and add to
375  * epoll set. */
376 static int alr_scan(int epoll_fd)
377 {
378         const char dir_path[] = "/dev/"LUSTRE_ACCESS_LOG_DIR_NAME;
379         DIR *dir;
380         int dir_fd;
381         struct dirent *d;
382         int rc;
383
384         dir = opendir(dir_path);
385         if (dir == NULL) {
386                 ERROR("cannot open '%s' for scanning: %s\n", dir_path, strerror(errno));
387                 return ALR_EXIT_FAILURE;
388         }
389
390         dir_fd = dirfd(dir);
391
392         /* Scan /dev for devices with major equal to oal_log_major and add
393          * any new devices. */
394         while ((d = readdir(dir)) != NULL) {
395                 char path[6 + PATH_MAX];
396                 struct alr_log **pal;
397                 struct stat st;
398
399                 if (d->d_type != DT_CHR)
400                         continue;
401
402                 rc = fstatat(dir_fd, d->d_name, &st, 0);
403                 if (rc < 0) {
404                         ERROR("cannot stat '%s/%s' while scanning: %s\n",
405                                 dir_path, d->d_name, strerror(errno));
406                         continue;
407                 }
408
409                 if (!S_ISCHR(st.st_mode))
410                         continue;
411
412                 if (major(st.st_rdev) != oal_log_major)
413                         continue;
414
415                 pal = alr_log_lookup(st.st_rdev);
416                 if (pal == NULL) {
417                         ERROR("no device slot available for '%s/%s' with minor %u\n",
418                                 dir_path, d->d_name, minor(st.st_rdev));
419                         continue;
420                 }
421
422                 if (*pal != NULL)
423                         continue; /* We already have this device. */
424
425                 snprintf(path, sizeof(path), "%s/%s", dir_path, d->d_name);
426
427                 alr_log_add(epoll_fd, path);
428         }
429
430         closedir(dir);
431
432         return ALR_OK;
433 }
434
435 /* /dev/lustre-access-log/control device poll callback: call prescan
436  * ioctl and scan /dev/lustre-access-log/ for new access log
437  * devices. */
438 static int alr_ctl_io(int epoll_fd, struct alr_dev *cd, unsigned int mask)
439 {
440         int rc;
441
442         TRACE("%s\n", __func__);
443         DEBUG_U(mask);
444
445         if (mask & EPOLLERR)
446                 return ALR_EXIT_FAILURE;
447
448         if (mask & EPOLLHUP)
449                 return ALR_EXIT_SUCCESS;
450
451         rc = ioctl(cd->alr_fd, LUSTRE_ACCESS_LOG_IOCTL_PRESCAN);
452         if (rc < 0) {
453                 ERROR("cannot start scanning: %s\n", strerror(errno));
454                 return ALR_EXIT_FAILURE;
455         }
456
457         return alr_scan(epoll_fd);
458 }
459
460 /* signalfd epoll callback. Handle SIGINT and SIGTERM by breaking from
461  * the epoll loop and exiting normally.*/
462 static int alr_signal_io(int epoll_fd, struct alr_dev *sd, unsigned int mask)
463 {
464         struct signalfd_siginfo ssi;
465         ssize_t rc;
466
467         TRACE("%s\n", __func__);
468         DEBUG_U(mask);
469
470         rc = read(sd->alr_fd, &ssi, sizeof(ssi));
471         if (rc <= 0)
472                 return ALR_OK;
473
474         DEBUG_U(ssi.ssi_signo);
475         switch (ssi.ssi_signo) {
476         case SIGINT:
477         case SIGTERM:
478                 return ALR_EXIT_SUCCESS;
479         default:
480                 return ALR_OK;
481         }
482 }
483
484 /* batching timerfd epoll callback. Print batched access entries to
485  * alr_batch_file. */
486 static int alr_batch_timer_io(int epoll_fd, struct alr_dev *td, unsigned int mask)
487 {
488         time_t now = time(NULL);
489         uint64_t expire_count;
490         ssize_t rc;
491
492         TRACE("%s\n", __func__);
493         DEBUG_D(now);
494         DEBUG_U(mask);
495
496         rc = read(td->alr_fd, &expire_count, sizeof(expire_count));
497         if (rc <= 0)
498                 return ALR_OK;
499
500         DEBUG_U(expire_count);
501
502         rc = alr_batch_print(alr_batch, alr_batch_file, alr_print_fraction);
503         if (rc < 0) {
504                 ERROR("cannot write to '%s': %s\n",
505                         alr_batch_file_path, strerror(errno));
506                 goto out;
507         }
508
509         /* FIXME: blocking write to batch file. */
510         rc = fflush(alr_batch_file);
511         if (rc < 0) {
512                 ERROR("cannot write to '%s': %s\n",
513                         alr_batch_file_path, strerror(errno));
514                 goto out;
515         }
516 out:
517         /* Failed writes will leave alr_batch_file (pipe) in a
518          * weird state so make that fatal. */
519         return (rc < 0) ? ALR_EXIT_FAILURE : ALR_OK;
520 }
521
522 /* Call LUSTRE_ACCESS_LOG_IOCTL_INFO to get access log info and print
523  * YAML formatted info to stdout. */
524 static int alr_log_info(struct alr_log *al)
525 {
526         struct lustre_access_log_info_v1 lali;
527         int rc;
528
529         rc = ioctl(al->alr_dev.alr_fd, LUSTRE_ACCESS_LOG_IOCTL_INFO, &lali);
530         if (rc < 0) {
531                 ERROR("cannot get info for device '%s': %s\n",
532                         al->alr_dev.alr_name, strerror(errno));
533                 return -1;
534         }
535
536         printf("- name: %s\n"
537                "  version: %#x\n"
538                "  type: %#x\n"
539                "  log_size: %u\n"
540                "  entry_size: %u\n"
541                "  _head: %u\n"
542                "  _tail: %u\n"
543                "  _entry_space: %u\n"
544                "  _entry_count: %u\n"
545                "  _drop_count: %u\n"
546                "  _is_closed: %u\n",
547                lali.lali_name,
548                lali.lali_version,
549                lali.lali_type,
550                lali.lali_log_size,
551                lali.lali_entry_size,
552                lali._lali_head,
553                lali._lali_tail,
554                lali._lali_entry_space,
555                lali._lali_entry_count,
556                lali._lali_drop_count,
557                lali._lali_is_closed);
558
559         return 0;
560 }
561
562 static struct alr_dev *alr_dev_create(int epoll_fd, int fd, const char *name,
563                         int (*io)(int, struct alr_dev *, unsigned int),
564                         void (*destroy)(struct alr_dev *))
565 {
566         struct alr_dev *alr;
567         int rc;
568
569         alr = calloc(1, sizeof(*alr));
570         if (alr == NULL)
571                 return NULL;
572
573         alr->alr_name = strdup(name);
574         if (alr->alr_name == NULL) {
575                 free(alr);
576                 return NULL;
577         }
578         alr->alr_io = io;
579         alr->alr_destroy = destroy;
580         alr->alr_fd = fd;
581
582         struct epoll_event event = {
583                 .events = EPOLLIN | EPOLLHUP,
584                 .data.ptr = alr,
585         };
586
587         rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, alr->alr_fd, &event);
588         if (rc < 0) {
589                 free(alr);
590                 return NULL;
591         }
592
593         return alr;
594 }
595
596 void usage(void)
597 {
598         printf("Usage: %s: [OPTION]...\n"
599 "Discover, read, batch, and write Lustre access logs\n"
600 "\n"
601 "Mandatory arguments to long options are mandatory for short options too.\n"
602 "  -f, --batch-file=FILE          print batch to file (default stdout)\n"
603 "  -i, --batch-interval=INTERVAL  print batch every INTERVAL seconds\n"
604 "  -o, --batch-offset=OFFSET      print batch at OFFSET seconds\n"
605 "  -d, --debug[=FILE]             print debug messages to FILE (stderr)\n"
606 "  -h, --help                     display this help and exit\n"
607 "  -l, --list                     print YAML list of available access logs\n"
608 "  -t, --trace[=FILE]             print trace messages to FILE (stderr)\n",
609                 program_invocation_short_name);
610 }
611
612 int main(int argc, char *argv[])
613 {
614         const char ctl_path[] = "/dev/"LUSTRE_ACCESS_LOG_DIR_NAME"/control";
615         struct alr_dev *alr_signal = NULL;
616         struct alr_dev *alr_batch_timer = NULL;
617         struct alr_dev *alr_ctl = NULL;
618         time_t batch_interval = 0;
619         time_t batch_offset = 0;
620         unsigned int m;
621         int list_info = 0;
622         int epoll_fd = -1;
623         int exit_status;
624         int rc;
625         int c;
626
627         static struct option options[] = {
628                 { .name = "batch-file", .has_arg = required_argument, .val = 'f', },
629                 { .name = "batch-interval", .has_arg = required_argument, .val = 'i', },
630                 { .name = "batch-offset", .has_arg = required_argument, .val = 'o', },
631                 { .name = "debug", .has_arg = optional_argument, .val = 'd', },
632                 { .name = "help", .has_arg = no_argument, .val = 'h', },
633                 { .name = "fraction", .has_arg = required_argument, .val = 'F', },
634                 { .name = "list", .has_arg = no_argument, .val = 'l', },
635                 { .name = "trace", .has_arg = optional_argument, .val = 't', },
636                 { .name = NULL, },
637         };
638
639         while ((c = getopt_long(argc, argv, "d::f:F:hi:lt::", options, NULL)) != -1) {
640                 switch (c) {
641                 case 'f':
642                         alr_batch_file_path = optarg;
643                         break;
644                 case 'i':
645                         errno = 0;
646                         batch_interval = strtoll(optarg, NULL, 0);
647                         if (batch_interval < 0 || batch_interval >= 1048576 ||
648                             errno != 0)
649                                 FATAL("invalid batch interval '%s'\n", optarg);
650                         break;
651                 case 'o':
652                         errno = 0;
653                         batch_offset = strtoll(optarg, NULL, 0);
654                         if (batch_offset < 0 || batch_offset >= 1048576 ||
655                             errno != 0)
656                                 FATAL("invalid batch offset '%s'\n", optarg);
657                         break;
658                 case 'd':
659                         if (optarg == NULL) {
660                                 debug_file = stderr;
661                         } else if (strcmp(optarg, "-") == 0) {
662                                 debug_file = stdout;
663                         } else {
664                                 debug_file = fopen(optarg, "a");
665                                 if (debug_file == NULL)
666                                         FATAL("cannot open debug file '%s': %s\n",
667                                                 optarg, strerror(errno));
668                         }
669
670                         break;
671                 case 'h':
672                         usage();
673                         exit(EXIT_SUCCESS);
674                 case 'F':
675                         alr_print_fraction = strtoll(optarg, NULL, 0);
676                         if (alr_print_fraction < 1 || alr_print_fraction > 100)
677                                 FATAL("invalid batch offset '%s'\n", optarg);
678                         break;
679                 case 'l':
680                         list_info = 1;
681                         break;
682                 case 't':
683                         if (optarg == NULL) {
684                                 trace_file = stderr;
685                         } else if (strcmp(optarg, "-") == 0) {
686                                 trace_file = stdout;
687                         } else {
688                                 trace_file = fopen(optarg, "a");
689                                 if (debug_file == NULL)
690                                         FATAL("cannot open debug file '%s': %s\n",
691                                                 optarg, strerror(errno));
692                         }
693
694                         break;
695                 case '?':
696                         fprintf(stderr, "Try '%s --help' for more information.\n",
697                                 program_invocation_short_name);
698                         exit(EXIT_FAILURE);
699                 }
700         }
701
702         if (batch_interval > 0) {
703                 alr_batch = alr_batch_create(-1);
704                 if (alr_batch == NULL)
705                         FATAL("cannot create batch struct: %s\n",
706                                 strerror(errno));
707         }
708
709         if (alr_batch_file_path != NULL) {
710                 alr_batch_file = fopen(alr_batch_file_path, "w");
711                 if (alr_batch_file == NULL)
712                         FATAL("cannot open batch file '%s': %s\n",
713                                 alr_batch_file_path, strerror(errno));
714         } else {
715                 alr_batch_file_path = "stdout";
716                 alr_batch_file = stdout;
717         }
718
719         epoll_fd = epoll_create1(EPOLL_CLOEXEC);
720         if (epoll_fd < 0)
721                 FATAL("cannot create epoll set: %s\n", strerror(errno));
722
723         /* Setup signal FD and add to epoll set. */
724         sigset_t signal_mask;
725         sigemptyset(&signal_mask);
726         sigaddset(&signal_mask, SIGINT);
727         sigaddset(&signal_mask, SIGTERM);
728         rc = sigprocmask(SIG_BLOCK, &signal_mask, NULL);
729         if (rc < 0)
730                 FATAL("cannot set process signal mask: %s\n", strerror(errno));
731
732         int signal_fd = signalfd(-1, &signal_mask, SFD_NONBLOCK|SFD_CLOEXEC);
733         if (signal_fd < 0)
734                 FATAL("cannot create signalfd: %s\n", strerror(errno));
735
736         alr_signal = alr_dev_create(epoll_fd, signal_fd, "signal", &alr_signal_io, NULL);
737         if (alr_signal == NULL)
738                 FATAL("cannot register signalfd: %s\n", strerror(errno));
739
740         signal_fd = -1;
741
742         /* Setup batch timer FD and add to epoll set. */
743         struct timespec now;
744         rc = clock_gettime(CLOCK_REALTIME, &now);
745         if (rc < 0)
746                 FATAL("cannot read realtime clock: %s\n", strerror(errno));
747
748         int timer_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
749         if (timer_fd < 0)
750                 FATAL("cannot create batch timerfd: %s\n", strerror(errno));
751
752         struct itimerspec it = {
753                 .it_value.tv_sec = (batch_interval > 0) ?
754                                    roundup(now.tv_sec, batch_interval) +
755                                    (batch_offset % batch_interval) :
756                                    0,
757                 .it_interval.tv_sec = batch_interval,
758         };
759
760         DEBUG_D(it.it_value.tv_sec);
761
762         rc = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &it, NULL);
763         if (rc < 0)
764                 FATAL("cannot arm timerfd: %s\n", strerror(errno));
765
766         alr_batch_timer = alr_dev_create(epoll_fd, timer_fd, "batch_timer",
767                                         &alr_batch_timer_io, NULL);
768         if (alr_batch_timer == NULL)
769                 FATAL("cannot register batch timerfd: %s\n", strerror(errno));
770
771         timer_fd = -1;
772
773         /* Open control device. */
774         int ctl_fd = open(ctl_path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
775         if (ctl_fd < 0)
776                 FATAL("cannot open '%s': %s\n", ctl_path, strerror(errno));
777
778         /* Get and print interface version. */
779         oal_version = ioctl(ctl_fd, LUSTRE_ACCESS_LOG_IOCTL_VERSION);
780         if (oal_version < 0)
781                 FATAL("cannot get ofd access log interface version: %s\n", strerror(errno));
782
783         DEBUG_D(oal_version);
784
785         /* Get and print device major used for access log devices. */
786         oal_log_major = ioctl(ctl_fd, LUSTRE_ACCESS_LOG_IOCTL_MAJOR);
787         if (oal_log_major < 0)
788                 FATAL("cannot get ofd access log major: %s\n", strerror(errno));
789
790         DEBUG_D(oal_log_major);
791
792         /* Add control device to epoll set. */
793         alr_ctl = alr_dev_create(epoll_fd, ctl_fd, "control", &alr_ctl_io, NULL);
794         if (alr_ctl == NULL)
795                 FATAL("cannot register control device: %s\n", strerror(errno));
796
797         ctl_fd = -1;
798
799         do {
800                 struct epoll_event ev[32];
801                 int timeout = (list_info ? 0 : -1);
802                 int i, ev_count;
803
804                 ev_count = epoll_wait(epoll_fd, ev, ARRAY_SIZE(ev), timeout);
805                 if (ev_count < 0) {
806                         if (errno == EINTR) /* Signal or timeout. */
807                                 continue;
808
809                         ERROR("cannot wait on epoll set: %s\n", strerror(errno));
810                         exit_status = EXIT_FAILURE;
811                         goto out;
812                 }
813
814                 DEBUG_D(ev_count);
815
816                 for (i = 0; i < ev_count; i++) {
817                         struct alr_dev *ad = ev[i].data.ptr;
818                         unsigned int mask = ev[i].events;
819
820                         rc = (*ad->alr_io)(epoll_fd, ad, mask);
821                         switch (rc) {
822                         case ALR_EXIT_FAILURE:
823                                 exit_status = EXIT_FAILURE;
824                                 goto out;
825                         case ALR_EXIT_SUCCESS:
826                                 exit_status = EXIT_SUCCESS;
827                                 goto out;
828                         case ALR_ERROR:
829                         case ALR_EOF:
830                                 alr_dev_free(epoll_fd, ad);
831                                 break;
832                         case ALR_OK:
833                         default:
834                                 break;
835                         }
836                 }
837         } while (!list_info);
838
839         exit_status = EXIT_SUCCESS;
840 out:
841         assert(oal_log_minor_max < ARRAY_SIZE(alr_log));
842
843         for (m = 0; m <= oal_log_minor_max; m++) {
844                 if (alr_log[m] == NULL)
845                         continue;
846
847                 if (list_info) {
848                         rc = alr_log_info(alr_log[m]);
849                         if (rc < 0)
850                                 exit_status = EXIT_FAILURE;
851                 }
852
853                 alr_dev_free(epoll_fd, &alr_log[m]->alr_dev);
854         }
855
856         alr_dev_free(epoll_fd, alr_ctl);
857         alr_dev_free(epoll_fd, alr_signal);
858         alr_dev_free(epoll_fd, alr_batch_timer);
859         close(epoll_fd);
860
861         alr_batch_destroy(alr_batch);
862
863         DEBUG_D(exit_status);
864
865         return exit_status;
866 }