Motr  M0
copy.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2019-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <libgen.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/time.h>
30 
31 #include "conf/obj.h"
32 #include "fid/fid.h"
33 #include "lib/trace.h"
34 #include "lib/string.h"
35 #include "lib/getopts.h"
36 #include "motr/client.h"
37 #include "motr/idx.h"
38 #include "motr/st/utils/helper.h"
39 
40 static struct m0_client *m0_instance = NULL;
41 static struct m0_container container;
42 static struct m0_config conf;
44 
45 extern struct m0_addb_ctx m0_addb_ctx;
46 
47 static void copy_usage(FILE *file, char *prog_name)
48 {
49  fprintf(file, "Usage: %s [OPTION]... SOURCE\n"
50 "Copy SOURCE to MOTR.\n"
51 "\n"
52 "Mandatory arguments to long options are mandatory for short options too.\n"
53 " -l, --local ADDR Local endpoint address.\n"
54 " -H, --ha ADDR HA endpoint address.\n"
55 " -p, --profile FID Profile FID.\n"
56 " -P, --process FID Process FID.\n"
57 " -o, --object FID ID of the motr object. "
58  "Object id should larger than "
59  "M0_ID_APP.\n%*c The first 0x100000 "
60  "ids are reserved for use by client.\n"
61 " -s, --block-size INT Block size multiple of 4k in bytes or with "
62  "suffix b/k/m/g.\n%*c Ex: 1k=1024, "
63  "1m=1024*1024. Range: [4k-32m].\n"
64 " -c, --block-count INT Number of blocks (>0) to copy, can give with "
65  "suffix b/k/m/g/K/M/G.\n%*c Ex: 1k=1024, "
66  "1m=1024*1024, 1K=1000 1M=1000*1000.\n"
67 " -L, --layout-id INT Layout ID. Range: [1-14].\n"
68 " -e, --enable-locks Enables acquiring and releasing RW locks "
69  "before and after performing IO.\n"
70 " -b, --blocks-per-io INT Number of blocks (>=0) per IO. "
71  "Default=100 if 0 or nothing is provided.\n"
72 " -O, --offset INT Updates the exisiting object from given "
73  "offset.\n%*c Default=0 if not provided. "
74  "Offset should be multiple of 4k.\n"
75 " -S, --msg_size INT Max RPC msg size 64k i.e 65536\n"
76  "%*c Note: this should match with m0d's current "
77  "rpc msg size\n"
78 " -q, --min_queue INT Minimum length of the receive queue i.e 16\n"
79 " -u, --update_mode Object update mode\n"
80 " -h, --help Shows this help text and exit.\n"
81 , prog_name, WIDTH, ' ', WIDTH, ' ', WIDTH, ' ', WIDTH, ' ', WIDTH, ' ');
82 }
83 
84 int main(int argc, char **argv)
85 {
86  struct m0_utility_param cp_param;
87  int rc;
88 
89  m0_utility_args_init(argc, argv, &cp_param,
90  &dix_conf, &conf, &copy_usage);
91 
92  /* Read Verify is only for m0cat */
93  conf.mc_is_read_verify = false;
94 
95  if (cp_param.cup_blks_per_io > M0_MAX_BLOCK_COUNT) {
96  fprintf(stderr, "Blocks per IO (%d) is out of range. "
97  "Max is %d for write operation!\n",
98  cp_param.cup_blks_per_io,
100  return -EINVAL;
101  }
102 
104  if (rc < 0) {
105  fprintf(stderr, "Client init failed: rc=%d\n", rc);
106  exit(EXIT_FAILURE);
107  }
108 
109  if (argv[optind] != NULL)
110  cp_param.cup_file = strdup(argv[optind]);
111 
112  rc = m0_write(&container, cp_param.cup_file,
113  cp_param.cup_id, cp_param.cup_block_size,
114  cp_param.cup_block_count, cp_param.cup_offset,
115  cp_param.cup_blks_per_io, cp_param.cup_take_locks,
116  cp_param.cup_update_mode);
117  if (rc < 0) {
118  if (rc == -EEXIST) {
119  fprintf(stderr, "Object "U128X_F" already exists: "
120  "rc=%d. To update an existing object, "
121  "use -u=start index.\n",
122  U128_P(&cp_param.cup_id), rc);
123  } else {
124  fprintf(stderr, "Object "U128X_F" write failed: rc=%d\n"
125  , U128_P(&cp_param.cup_id), rc);
126  }
127  }
128 
130 
131  return rc == 0 ? 0 : 1;
132 }
133 
134 /*
135  * Local variables:
136  * c-indentation-style: "K&R"
137  * c-basic-offset: 8
138  * tab-width: 8
139  * fill-column: 80
140  * scroll-step: 1
141  * End:
142  */
int optind
#define NULL
Definition: misc.h:38
struct m0_file file
Definition: di.c:36
static struct m0_container container
Definition: copy.c:41
int m0_write(struct m0_container *container, char *src, struct m0_uint128 id, uint32_t block_size, uint32_t block_count, uint64_t update_offset, int blks_per_io, bool take_locks, bool update_mode)
Definition: helper.c:352
uint64_t cup_block_size
Definition: helper.h:66
Definition: conf.py:1
M0_INTERNAL void client_init(struct sim *s, struct client_conf *conf)
Definition: client.c:148
static void copy_usage(FILE *file, char *prog_name)
Definition: copy.c:47
struct m0_addb_ctx m0_addb_ctx
bool cup_take_locks
Definition: helper.h:70
int main(int argc, char **argv)
Definition: copy.c:84
uint64_t cup_block_count
Definition: helper.h:67
bool cup_update_mode
Definition: helper.h:74
int m0_utility_args_init(int argc, char **argv, struct m0_utility_param *params, struct m0_idx_dix_config *dix_conf, struct m0_config *conf, void(*utility_usage)(FILE *, char *))
Definition: helper.c:915
#define U128_P(x)
Definition: types.h:45
struct m0_uint128 cup_id
Definition: helper.h:65
uint64_t cup_offset
Definition: helper.h:69
#define U128X_F
Definition: types.h:42
int cup_blks_per_io
Definition: helper.h:73
char * cup_file
Definition: helper.h:72
static struct m0_idx_dix_config dix_conf
Definition: copy.c:43
Definition: helper.h:53
M0_INTERNAL void client_fini(struct client_conf *conf)
Definition: client.c:177
int32_t rc
Definition: trigger_fop.h:47