Whamcloud - gitweb
LU-3534 mdt: move last_rcvd obj update to LOD
[fs/lustre-release.git] / lustre / target / out_lib.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 /*
23  * Copyright (c) 2014, Intel Corporation.
24  */
25 /*
26  * lustre/target/out_lib.c
27  *
28  * Author: Di Wang <di.wang@intel.com>
29  * Author: Fan, Yong <fan.yong@intel.com>
30  */
31
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <lu_target.h>
35 #include <lustre_update.h>
36 #include <obd.h>
37 #include <obd_class.h>
38
39 #define OUT_UPDATE_BUFFER_SIZE_ADD      4096
40 #define OUT_UPDATE_BUFFER_SIZE_MAX      (256 * 4096)  /* 1MB update size now */
41
42 const char *update_op_str(__u16 opc)
43 {
44         static const char *opc_str[] = {
45                 [OUT_START] = "start",
46                 [OUT_CREATE] = "create",
47                 [OUT_DESTROY] = "destroy",
48                 [OUT_REF_ADD] = "ref_add",
49                 [OUT_REF_DEL] = "ref_del" ,
50                 [OUT_ATTR_SET] = "attr_set",
51                 [OUT_ATTR_GET] = "attr_get",
52                 [OUT_XATTR_SET] = "xattr_set",
53                 [OUT_XATTR_GET] = "xattr_get",
54                 [OUT_INDEX_LOOKUP] = "lookup",
55                 [OUT_INDEX_INSERT] = "insert",
56                 [OUT_INDEX_DELETE] = "delete",
57                 [OUT_WRITE] = "write",
58                 [OUT_XATTR_DEL] = "xattr_del",
59                 [OUT_PUNCH] = "punch",
60         };
61
62         if (opc < ARRAY_SIZE(opc_str) && opc_str[opc] != NULL)
63                 return opc_str[opc];
64         else
65                 return "unknown";
66 }
67 EXPORT_SYMBOL(update_op_str);
68
69 /**
70  * Fill object update header
71  *
72  * Only fill the object update header, and parameters will be filled later
73  * in other functions.
74  *
75  * \params[in] env              execution environment
76  * \params[in] update           object update to be filled
77  * \params[in] max_update_size  maximum object update size, if the current
78  *                              update length equals or exceeds the size, it
79  *                              will return -E2BIG.
80  * \params[in] update_op        update type
81  * \params[in] fid              object FID of the update
82  * \params[in] params_count     the count of the update parameters
83  * \params[in] params_sizes     the length of each parameters
84  *
85  * \retval                      0 if packing succeeds.
86  * \retval                      -E2BIG if packing exceeds the maximum length.
87  */
88 int out_update_header_pack(const struct lu_env *env,
89                            struct object_update *update, size_t max_update_size,
90                            enum update_type update_op, const struct lu_fid *fid,
91                            unsigned int param_count, __u16 *params_sizes)
92 {
93         struct object_update_param      *param;
94         unsigned int                    i;
95         size_t                          update_size;
96
97         /* Check whether the packing exceeding the maxima update length */
98         update_size = sizeof(*update);
99         for (i = 0; i < param_count; i++)
100                 update_size += cfs_size_round(sizeof(*param) + params_sizes[i]);
101
102         if (unlikely(update_size >= max_update_size))
103                 return -E2BIG;
104
105         update->ou_fid = *fid;
106         update->ou_type = update_op;
107         update->ou_params_count = param_count;
108         param = &update->ou_params[0];
109         for (i = 0; i < param_count; i++) {
110                 param->oup_len = params_sizes[i];
111                 param = (struct object_update_param *)((char *)param +
112                          object_update_param_size(param));
113         }
114
115         return 0;
116 }
117
118 /**
119  * Packs one update into the update_buffer.
120  *
121  * \param[in] env       execution environment
122  * \param[in] update    update to be packed
123  * \param[in] max_update_size   *maximum size of \a update
124  * \param[in] op        update operation (enum update_type)
125  * \param[in] fid       object FID for this update
126  * \param[in] param_count       number of parameters for this update
127  * \param[in] param_sizes       array of parameters length of this update
128  * \param[in] param_bufs        parameter buffers
129  *
130  * \retval              = 0 if updates packing succeeds
131  * \retval              negative errno if updates packing fails
132  **/
133 int out_update_pack(const struct lu_env *env, struct object_update *update,
134                     size_t max_update_size, enum update_type op,
135                     const struct lu_fid *fid, unsigned int param_count,
136                     __u16 *param_sizes, const void **param_bufs)
137 {
138         struct object_update_param      *param;
139         unsigned int                    i;
140         int                             rc;
141         ENTRY;
142
143         rc = out_update_header_pack(env, update, max_update_size, op, fid,
144                                     param_count, param_sizes);
145         if (rc != 0)
146                 RETURN(rc);
147
148         param = &update->ou_params[0];
149         for (i = 0; i < param_count; i++) {
150                 memcpy(&param->oup_buf[0], param_bufs[i], param_sizes[i]);
151                 param = (struct object_update_param *)((char *)param +
152                          object_update_param_size(param));
153         }
154
155         RETURN(0);
156 }
157 EXPORT_SYMBOL(out_update_pack);
158
159 /**
160  * Pack various updates into the update_buffer.
161  *
162  * The following functions pack different updates into the update_buffer
163  * So parameters of these API is basically same as its correspondent OSD/OSP
164  * API, for detail description of these parameters see osd_handler.c or
165  * osp_md_object.c.
166  *
167  * \param[in] env       execution environment
168  * \param[in] ubuf      update buffer
169  * \param[in] fid       fid of this object for the update
170  *
171  * \retval              0 if insertion succeeds.
172  * \retval              negative errno if insertion fails.
173  */
174 int out_create_pack(const struct lu_env *env, struct object_update *update,
175                     size_t max_update_size, const struct lu_fid *fid,
176                     const struct lu_attr *attr, struct dt_allocation_hint *hint,
177                     struct dt_object_format *dof)
178 {
179         struct obdo             *obdo;
180         __u16                   sizes[2] = {sizeof(*obdo), 0};
181         int                     buf_count = 1;
182         const struct lu_fid     *parent_fid = NULL;
183         int                     rc;
184         ENTRY;
185
186         if (hint != NULL && hint->dah_parent) {
187                 parent_fid = lu_object_fid(&hint->dah_parent->do_lu);
188                 sizes[1] = sizeof(*parent_fid);
189                 buf_count++;
190         }
191
192         rc = out_update_header_pack(env, update, max_update_size, OUT_CREATE,
193                                     fid, buf_count, sizes);
194         if (rc != 0)
195                 RETURN(rc);
196
197         obdo = object_update_param_get(update, 0, NULL);
198         LASSERT(obdo != NULL);
199         obdo->o_valid = 0;
200         obdo_from_la(obdo, attr, attr->la_valid);
201         lustre_set_wire_obdo(NULL, obdo, obdo);
202
203         if (parent_fid != NULL) {
204                 struct lu_fid *tmp;
205
206                 tmp = object_update_param_get(update, 1, NULL);
207                 LASSERT(tmp != NULL);
208                 fid_cpu_to_le(tmp, parent_fid);
209         }
210
211         RETURN(0);
212 }
213 EXPORT_SYMBOL(out_create_pack);
214
215 int out_ref_del_pack(const struct lu_env *env, struct object_update *update,
216                      size_t max_update_size, const struct lu_fid *fid)
217 {
218         return out_update_pack(env, update, max_update_size, OUT_REF_DEL, fid,
219                                0, NULL, NULL);
220 }
221 EXPORT_SYMBOL(out_ref_del_pack);
222
223 int out_ref_add_pack(const struct lu_env *env, struct object_update *update,
224                      size_t max_update_size, const struct lu_fid *fid)
225 {
226         return out_update_pack(env, update, max_update_size, OUT_REF_ADD, fid,
227                                0, NULL, NULL);
228 }
229 EXPORT_SYMBOL(out_ref_add_pack);
230
231 int out_attr_set_pack(const struct lu_env *env, struct object_update *update,
232                       size_t max_update_size, const struct lu_fid *fid,
233                       const struct lu_attr *attr)
234 {
235         struct obdo             *obdo;
236         __u16                   size = sizeof(*obdo);
237         int                     rc;
238         ENTRY;
239
240         rc = out_update_header_pack(env, update, max_update_size,
241                                     OUT_ATTR_SET, fid, 1, &size);
242         if (rc != 0)
243                 RETURN(rc);
244
245         obdo = object_update_param_get(update, 0, NULL);
246         LASSERT(obdo != NULL);
247         obdo->o_valid = 0;
248         obdo_from_la(obdo, attr, attr->la_valid);
249         lustre_set_wire_obdo(NULL, obdo, obdo);
250
251         RETURN(0);
252 }
253 EXPORT_SYMBOL(out_attr_set_pack);
254
255 int out_xattr_set_pack(const struct lu_env *env, struct object_update *update,
256                        size_t max_update_size, const struct lu_fid *fid,
257                        const struct lu_buf *buf, const char *name, __u32 flag)
258 {
259         __u16   sizes[3] = {strlen(name) + 1, buf->lb_len, sizeof(flag)};
260         const void *bufs[3] = {(char *)name, (char *)buf->lb_buf,
261                                (char *)&flag};
262
263         return out_update_pack(env, update, max_update_size, OUT_XATTR_SET,
264                                fid, ARRAY_SIZE(sizes), sizes, bufs);
265 }
266 EXPORT_SYMBOL(out_xattr_set_pack);
267
268 int out_xattr_del_pack(const struct lu_env *env, struct object_update *update,
269                        size_t max_update_size, const struct lu_fid *fid,
270                        const char *name)
271 {
272         __u16   size = strlen(name) + 1;
273
274         return out_update_pack(env, update, max_update_size, OUT_XATTR_DEL,
275                                fid, 1, &size, (const void **)&name);
276 }
277 EXPORT_SYMBOL(out_xattr_del_pack);
278
279
280 int out_index_insert_pack(const struct lu_env *env,
281                           struct object_update *update,
282                           size_t max_update_size, const struct lu_fid *fid,
283                           const struct dt_rec *rec, const struct dt_key *key)
284 {
285         struct dt_insert_rec       *rec1 = (struct dt_insert_rec *)rec;
286         struct lu_fid              rec_fid;
287         __u32                       type = cpu_to_le32(rec1->rec_type);
288         __u16                       sizes[3] = { strlen((char *)key) + 1,
289                                                 sizeof(rec_fid),
290                                                 sizeof(type) };
291         const void                 *bufs[3] = { (char *)key,
292                                                 (char *)&rec_fid,
293                                                 (char *)&type };
294
295         fid_cpu_to_le(&rec_fid, rec1->rec_fid);
296
297         return out_update_pack(env, update, max_update_size, OUT_INDEX_INSERT,
298                                fid, ARRAY_SIZE(sizes), sizes, bufs);
299 }
300 EXPORT_SYMBOL(out_index_insert_pack);
301
302 int out_index_delete_pack(const struct lu_env *env,
303                           struct object_update *update,
304                           size_t max_update_size, const struct lu_fid *fid,
305                           const struct dt_key *key)
306 {
307         __u16   size = strlen((char *)key) + 1;
308         const void *buf = key;
309
310         return out_update_pack(env, update, max_update_size, OUT_INDEX_DELETE,
311                                fid, 1, &size, &buf);
312 }
313 EXPORT_SYMBOL(out_index_delete_pack);
314
315 int out_object_destroy_pack(const struct lu_env *env,
316                             struct object_update *update,
317                             size_t max_update_size, const struct lu_fid *fid)
318 {
319         return out_update_pack(env, update, max_update_size, OUT_DESTROY, fid,
320                                0, NULL, NULL);
321 }
322 EXPORT_SYMBOL(out_object_destroy_pack);
323
324 int out_write_pack(const struct lu_env *env, struct object_update *update,
325                    size_t max_update_size, const struct lu_fid *fid,
326                    const struct lu_buf *buf, __u64 pos)
327 {
328         __u16           sizes[2] = {buf->lb_len, sizeof(pos)};
329         const void      *bufs[2] = {(char *)buf->lb_buf, (char *)&pos};
330         int             rc;
331
332         pos = cpu_to_le64(pos);
333
334         rc = out_update_pack(env, update, max_update_size, OUT_WRITE, fid,
335                              ARRAY_SIZE(sizes), sizes, bufs);
336         return rc;
337 }
338 EXPORT_SYMBOL(out_write_pack);
339
340 /**
341  * Pack various readonly updates into the update_buffer.
342  *
343  * The following update funcs are only used by read-only ops, lookup,
344  * getattr etc, so it does not need transaction here. Currently they
345  * are only used by OSP.
346  *
347  * \param[in] env       execution environment
348  * \param[in] fid       fid of this object for the update
349  * \param[in] ubuf      update buffer
350  *
351  * \retval              = 0 pack succeed.
352  *                      < 0 pack failed.
353  **/
354 int out_index_lookup_pack(const struct lu_env *env,
355                           struct object_update *update,
356                           size_t max_update_size, const struct lu_fid *fid,
357                           struct dt_rec *rec, const struct dt_key *key)
358 {
359         const void      *name = key;
360         __u16           size = strlen((char *)name) + 1;
361
362         return out_update_pack(env, update, max_update_size, OUT_INDEX_LOOKUP,
363                                fid, 1, &size, &name);
364 }
365 EXPORT_SYMBOL(out_index_lookup_pack);
366
367 int out_attr_get_pack(const struct lu_env *env, struct object_update *update,
368                       size_t max_update_size, const struct lu_fid *fid)
369 {
370         return out_update_pack(env, update, max_update_size, OUT_ATTR_GET,
371                                fid, 0, NULL, NULL);
372 }
373 EXPORT_SYMBOL(out_attr_get_pack);
374
375 int out_xattr_get_pack(const struct lu_env *env, struct object_update *update,
376                        size_t max_update_size, const struct lu_fid *fid,
377                        const char *name)
378 {
379         __u16 size;
380
381         LASSERT(name != NULL);
382         size = strlen(name) + 1;
383
384         return out_update_pack(env, update, max_update_size, OUT_XATTR_GET,
385                                fid, 1, &size, (const void **)&name);
386 }
387 EXPORT_SYMBOL(out_xattr_get_pack);