4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2017, DDN Storage Corporation.
26 * lustre/utils/llsom_sync.c
28 * Tool for sync the LSOM xattr.
30 * Author: Qian Yingjin <qian@ddn.com>
41 #include <sys/types.h>
43 #include <linux/unistd.h>
44 #include <linux/kernel.h>
45 #include <sys/sysinfo.h>
46 #include <linux/lustre/lustre_user.h>
47 #include <lustre/lustreapi.h>
48 #include <linux/lustre/lustre_idl.h>
49 #include <linux/lustre/lustre_fid.h>
50 #include <libcfs/util/hash.h>
51 #include <libcfs/util/list.h>
53 #define container_of(ptr, type, member) ({ \
54 const typeof(((type *) 0)->member) * __mptr = (ptr); \
55 (type *) ((char *) __mptr - offsetof(type, member)); })
57 #define CHLG_POLL_INTV 60
58 #define REC_MIN_AGE 600
59 #define DEF_CACHE_SIZE (256 * 1048576) /* 256MB */
60 #define ONE_MB 0x100000
63 const char *o_chlg_user;
64 const char *o_mdtname;
71 unsigned long o_cached_fid_hiwm; /* high watermark */
72 unsigned long o_batch_sync_cnt;
78 struct hlist_node fr_node;
79 struct list_head fr_link;
85 static const int fid_hash_shift = 6;
87 #define FID_HASH_ENTRIES (1 << fid_hash_shift)
88 #define FID_ON_HASH(f) (!hlist_unhashed(&(f)->fr_node))
91 struct hlist_head *lh_hash;
92 struct list_head lh_list; /* ordered list by record index */
93 unsigned long lh_cached_count;
96 static void usage(char *prog)
98 printf("\nUsage: %s [options] -u <userid> -m <mdtdev> <mntpt>\n"
100 "\t-d, --daemonize\n"
101 "\t-i, --interval, poll interval in second\n"
102 "\t-a, --min-age, min age before a record is processed.\n"
103 "\t-c, --max-cache, percentage of the memroy used for cache.\n"
104 "\t-s, --sync, data sync when update LSOM xattr\n"
105 "\t-v, --verbose, produce more verbose ouput\n",
110 static inline bool fid_eq(const lustre_fid *f1, const lustre_fid *f2)
112 return f1->f_seq == f2->f_seq && f1->f_oid == f2->f_oid &&
113 f1->f_ver == f2->f_ver;
116 static void fid_hash_del(struct fid_rec *f)
119 hlist_del_init(&f->fr_node);
122 static void fid_hash_add(struct fid_rec *f)
124 assert(!FID_ON_HASH(f));
125 hlist_add_head(&f->fr_node,
126 &head.lh_hash[llapi_fid_hash(&f->fr_fid,
130 static struct fid_rec *fid_hash_find(const lustre_fid *fid)
132 struct hlist_head *hash_list;
133 struct hlist_node *entry, *next;
136 hash_list = &head.lh_hash[llapi_fid_hash(fid, fid_hash_shift)];
137 hlist_for_each_entry_safe(f, entry, next, hash_list, fr_node) {
138 assert(FID_ON_HASH(f));
139 if (fid_eq(fid, &f->fr_fid))
146 static int lsom_setup(void)
150 /* set llapi message level */
151 llapi_msg_set_level(opt.o_verbose);
153 memset(&head, 0, sizeof(head));
154 head.lh_hash = malloc(sizeof(struct hlist_head) * FID_HASH_ENTRIES);
155 if (head.lh_hash == NULL) {
156 llapi_err_noerrno(LLAPI_MSG_ERROR,
157 "failed to alloc memory for hash (%zu).",
158 sizeof(struct hlist_head) * FID_HASH_ENTRIES);
162 for (i = 0; i < FID_HASH_ENTRIES; i++)
163 INIT_HLIST_HEAD(&head.lh_hash[i]);
165 INIT_LIST_HEAD(&head.lh_list);
169 static void lsom_cleanup(void)
174 static int lsom_update_one(struct fid_rec *f)
180 fd = llapi_open_by_fid(opt.o_mntpt, &f->fr_fid,
181 O_RDONLY | O_NOATIME);
185 /* The file may be deleted, clean the corresponding
186 * changelog record and ignore this error.
191 llapi_error(LLAPI_MSG_ERROR, rc,
192 "llapi_open_by_fid for " DFID " failed",
197 if (opt.o_data_sync) {
200 /* Flush dirty pages from clients */
201 rc = llapi_get_data_version(fd, &dv, LL_DV_RD_FLUSH);
203 llapi_error(LLAPI_MSG_ERROR, errno,
204 "failed to sync data for " DFID,
206 /* ignore this error, continue to sync lsom data */
211 llapi_error(LLAPI_MSG_ERROR, rc, "failed to stat FID: " DFID,
216 /* After call fstat(), it already gets OST attrs to the client,
217 * when close the file, MDS will update the LSOM data itself
218 * according the size and blocks information from the client.
222 llapi_printf(LLAPI_MSG_DEBUG,
223 "record %llu:%llu, updated LSOM for fid " DFID
224 " size:%lu blocks:%lu\n",
225 (unsigned long long)f->fr_time,
226 (unsigned long long)f->fr_index,
227 PFID(&f->fr_fid), st.st_size, st.st_blocks);
230 rc = llapi_changelog_clear(opt.o_mdtname,
231 opt.o_chlg_user, f->fr_index);
233 llapi_error(LLAPI_MSG_ERROR, rc,
234 "failed to clear changelog record: %s:%llu",
235 opt.o_chlg_user, (unsigned long long)f->fr_index);
239 static int lsom_start_update(int count)
244 llapi_printf(LLAPI_MSG_INFO, "Start to sync %d records.\n", count);
249 f = list_entry(head.lh_list.next, struct fid_rec, fr_link);
250 rc = lsom_update_one(f);
252 list_del_init(&f->fr_link);
255 head.lh_cached_count--;
266 static int lsom_check_sync(void)
273 if (list_empty(&head.lh_list))
276 if (head.lh_cached_count > opt.o_cached_fid_hiwm)
277 count = opt.o_batch_sync_cnt;
282 /* When the first record in the list was not being
283 * processed for a long time (more than o_min_age),
284 * pop the record, start to handle it immediately.
287 f = list_entry(head.lh_list.next, struct fid_rec, fr_link);
288 if (now > ((f->fr_time >> 30) + opt.o_min_age))
293 rc = lsom_start_update(count);
295 if (rc == 0 && count == 1)
301 static void lsom_sort_record_list(struct fid_rec *f)
303 struct list_head *pos;
304 bool need_move = false;
306 for (pos = f->fr_link.next; pos != &head.lh_list; pos = pos->next) {
307 struct fid_rec *rec = list_entry(pos, struct fid_rec, fr_link);
309 if (f->fr_index > rec->fr_index) {
318 list_move_tail(&f->fr_link, pos);
321 static int process_record(struct changelog_rec *rec)
323 __u64 index = rec->cr_index;
326 if (rec->cr_type == CL_CLOSE || rec->cr_type == CL_TRUNC ||
327 rec->cr_type == CL_SETATTR) {
330 f = fid_hash_find(&rec->cr_tfid);
332 f = malloc(sizeof(struct fid_rec));
335 llapi_error(LLAPI_MSG_ERROR, rc,
336 "failed to alloc memory for fid_rec");
340 f->fr_fid = rec->cr_tfid;
342 f->fr_time = rec->cr_time;
343 INIT_HLIST_NODE(&f->fr_node);
346 * The newly changelog record index is processed in the
347 * ascending order, so it is safe to put the record at
348 * the tail of the ordered list.
350 list_add_tail(&f->fr_link, &head.lh_list);
351 head.lh_cached_count++;
354 lsom_sort_record_list(f);
358 llapi_printf(LLAPI_MSG_DEBUG,
359 "Processed changelog record index:%llu type:%s(0x%x) FID:"DFID"\n",
360 (unsigned long long)index,
361 changelog_type2str(__le32_to_cpu(rec->cr_type)),
362 __le32_to_cpu(rec->cr_type), PFID(&rec->cr_tfid));
367 static unsigned long get_fid_cache_size(int pct)
369 struct sysinfo sinfo;
370 unsigned long cache_size;
373 rc = sysinfo(&sinfo);
375 llapi_error(LLAPI_MSG_ERROR, rc, "failed to get sysinfo");
376 /* ignore this error, just pick some reasonable static
377 * limit for the cache size (e.g. 256MB, default value).
379 cache_size = DEF_CACHE_SIZE;
381 /* maximum cached fid size is tunned according to total
382 * memory size, e.g. 5% of the memroy.
384 cache_size = sinfo.totalram * pct / 100;
390 int main(int argc, char **argv)
395 struct changelog_rec *rec;
398 unsigned long long cache_size = DEF_CACHE_SIZE;
399 char fsname[MAX_OBD_NAME + 1];
400 unsigned long long unit;
401 static struct option options[] = {
402 { "mdt", required_argument, NULL, 'm' },
403 { "user", required_argument, 0, 'u'},
404 { "daemonize", no_argument, NULL, 'd'},
405 { "interval", required_argument, NULL, 'i'},
406 { "min-age", required_argument, NULL, 'a'},
407 { "max-cache", required_argument, NULL, 'c'},
408 { "verbose", no_argument, NULL, 'v'},
409 { "sync", no_argument, NULL, 's'},
410 { "help", no_argument, NULL, 'h' },
414 memset(&opt, 0, sizeof(opt));
415 opt.o_data_sync = false;
416 opt.o_verbose = LLAPI_MSG_INFO;
417 opt.o_intv = CHLG_POLL_INTV;
418 opt.o_min_age = REC_MIN_AGE;
420 while ((c = getopt_long(argc, argv, "u:hm:dsi:a:c:v", options, NULL))
425 llapi_error(LLAPI_MSG_ERROR, rc,
426 "%s: unknown option '%c'",
430 opt.o_chlg_user = optarg;
436 opt.o_mdtname = optarg;
439 opt.o_daemonize = true;
442 opt.o_intv = atoi(optarg);
443 if (opt.o_intv < 0) {
445 llapi_error(LLAPI_MSG_ERROR, rc,
446 "bad value for -i %s", optarg);
451 opt.o_min_age = atoi(optarg);
452 if (opt.o_min_age < 0) {
454 llapi_error(LLAPI_MSG_ERROR, rc,
455 "bad value for -a %s", optarg);
461 rc = llapi_parse_size(optarg, &cache_size, &unit, 0);
464 llapi_error(LLAPI_MSG_ERROR, rc,
465 "bad valud for -c '%s'", optarg);
469 /* For value < 100, it is taken as the percentage of
470 * total memory instead.
472 if (cache_size < 100)
473 cache_size = get_fid_cache_size(cache_size);
474 llapi_printf(LLAPI_MSG_INFO, "Cache size: %llu\n",
481 opt.o_data_sync = true;
486 if (argc != optind + 1) {
487 llapi_err_noerrno(LLAPI_MSG_ERROR,
488 "%s: no mount point specified\n", argv[0]);
492 opt.o_mntpt = argv[optind];
493 rc = llapi_search_fsname(opt.o_mntpt, fsname);
495 llapi_error(LLAPI_MSG_ERROR, rc,
496 "cannot find a Lustre file system mounted at '%s'",
504 if (!opt.o_chlg_user)
507 if (opt.o_daemonize) {
511 llapi_error(LLAPI_MSG_ERROR, rc, "cannot daemonize");
515 setbuf(stdout, NULL);
518 opt.o_cached_fid_hiwm = cache_size / sizeof(struct fid_rec);
519 opt.o_batch_sync_cnt = opt.o_cached_fid_hiwm / 2;
528 llapi_printf(LLAPI_MSG_DEBUG, "Start receiving records\n");
529 rc = llapi_changelog_start(&chglog_hdlr,
530 CHANGELOG_FLAG_BLOCK |
531 CHANGELOG_FLAG_JOBID |
532 CHANGELOG_FLAG_EXTRA_FLAGS,
535 llapi_error(LLAPI_MSG_ERROR, rc,
536 "unable to open changelog of MDT '%s'",
541 while (!eof && !stop) {
542 rc = llapi_changelog_recv(chglog_hdlr, &rec);
545 rc = process_record(rec);
547 llapi_error(LLAPI_MSG_ERROR, rc,
548 "failed to process record");
552 llapi_changelog_free(&rec);
554 rc = lsom_check_sync();
562 llapi_printf(LLAPI_MSG_DEBUG,
563 "finished reading [%s]\n",
567 case -EINVAL: /* FS unmounted */
568 case -EPROTO: /* error in KUC channel */
571 llapi_error(LLAPI_MSG_ERROR, rc,
572 "failed to get changelog record");
578 /* reach EOF of changelog */
579 rc = llapi_changelog_fini(&chglog_hdlr);
581 llapi_error(LLAPI_MSG_ERROR, rc,
582 "unable to close changelog of MDT '%s'",
588 if (opt.o_daemonize) {
591 rc = lsom_check_sync();
597 lsom_start_update(head.lh_cached_count);