Whamcloud - gitweb
LU-3850 obdecho: create remote dir from echo client
[fs/lustre-release.git] / lustre / osp / osp_md_object.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) 2013, Intel Corporation.
24  */
25 /*
26  * lustre/osp/osp_md_object.c
27  *
28  * Lustre MDT Proxy Device
29  *
30  * Author: Di Wang <di.wang@intel.com>
31  */
32
33 #define DEBUG_SUBSYSTEM S_MDS
34
35 #include <lustre_log.h>
36 #include <lustre_update.h>
37 #include "osp_internal.h"
38 static const char dot[] = ".";
39 static const char dotdot[] = "..";
40
41 static int osp_prep_update_req(const struct lu_env *env,
42                                struct osp_device *osp,
43                                struct update_buf *ubuf, int ubuf_len,
44                                struct ptlrpc_request **reqp)
45 {
46         struct obd_import      *imp;
47         struct ptlrpc_request  *req;
48         struct update_buf      *tmp;
49         int                     rc;
50         ENTRY;
51
52         imp = osp->opd_obd->u.cli.cl_import;
53         LASSERT(imp);
54
55         req = ptlrpc_request_alloc(imp, &RQF_UPDATE_OBJ);
56         if (req == NULL)
57                 RETURN(-ENOMEM);
58
59         req_capsule_set_size(&req->rq_pill, &RMF_UPDATE, RCL_CLIENT,
60                              UPDATE_BUFFER_SIZE);
61
62         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, UPDATE_OBJ);
63         if (rc != 0) {
64                 ptlrpc_req_finished(req);
65                 RETURN(rc);
66         }
67
68         req_capsule_set_size(&req->rq_pill, &RMF_UPDATE_REPLY, RCL_SERVER,
69                              UPDATE_BUFFER_SIZE);
70
71         tmp = req_capsule_client_get(&req->rq_pill, &RMF_UPDATE);
72         memcpy(tmp, ubuf, ubuf_len);
73
74         ptlrpc_request_set_replen(req);
75
76         *reqp = req;
77
78         RETURN(rc);
79 }
80
81 static int osp_remote_sync(const struct lu_env *env, struct dt_device *dt,
82                            struct update_request *update,
83                            struct ptlrpc_request **reqp)
84 {
85         struct osp_device       *osp = dt2osp_dev(dt);
86         struct ptlrpc_request   *req = NULL;
87         int                     rc;
88         ENTRY;
89
90         rc = osp_prep_update_req(env, osp, update->ur_buf, UPDATE_BUFFER_SIZE,
91                                  &req);
92         if (rc)
93                 RETURN(rc);
94
95         /* Note: some dt index api might return non-zero result here, like
96          * osd_index_ea_lookup, so we should only check rc < 0 here */
97         rc = ptlrpc_queue_wait(req);
98         if (rc < 0) {
99                 ptlrpc_req_finished(req);
100                 update->ur_rc = rc;
101                 RETURN(rc);
102         }
103
104         if (reqp != NULL) {
105                 *reqp = req;
106                 RETURN(rc);
107         }
108
109         update->ur_rc = rc;
110
111         ptlrpc_req_finished(req);
112
113         RETURN(rc);
114 }
115
116 /**
117  * Create a new update request for the device.
118  */
119 static struct update_request
120 *osp_create_update_req(struct dt_device *dt)
121 {
122         struct update_request *update;
123
124         OBD_ALLOC_PTR(update);
125         if (!update)
126                 return ERR_PTR(-ENOMEM);
127
128         OBD_ALLOC_LARGE(update->ur_buf, UPDATE_BUFFER_SIZE);
129         if (update->ur_buf == NULL) {
130                 OBD_FREE_PTR(update);
131                 return ERR_PTR(-ENOMEM);
132         }
133
134         CFS_INIT_LIST_HEAD(&update->ur_list);
135         update->ur_dt = dt;
136         update->ur_batchid = 0;
137         update->ur_buf->ub_magic = UPDATE_BUFFER_MAGIC;
138         update->ur_buf->ub_count = 0;
139
140         return update;
141 }
142
143 static void osp_destroy_update_req(struct update_request *update)
144 {
145         if (update == NULL)
146                 return;
147
148         cfs_list_del(&update->ur_list);
149         if (update->ur_buf != NULL)
150                 OBD_FREE_LARGE(update->ur_buf, UPDATE_BUFFER_SIZE);
151
152         OBD_FREE_PTR(update);
153         return;
154 }
155
156 int osp_trans_stop(const struct lu_env *env, struct thandle *th)
157 {
158         int rc = 0;
159
160         rc = th->th_current_request->ur_rc;
161         osp_destroy_update_req(th->th_current_request);
162         th->th_current_request = NULL;
163
164         return rc;
165 }
166
167 /**
168  * In DNE phase I, all remote updates will be packed into RPC (the format
169  * description is in lustre_idl.h) during declare phase, all of updates
170  * are attached to the transaction, one entry per OSP. Then in trans start,
171  * LOD will walk through these entries and send these UPDATEs to the remote
172  * MDT to be executed synchronously.
173  */
174 int osp_trans_start(const struct lu_env *env, struct dt_device *dt,
175                     struct thandle *th)
176 {
177         struct update_request *update;
178         int rc = 0;
179
180         /* In phase I, if the transaction includes remote updates, the local
181          * update should be synchronized, so it will set th_sync = 1 */
182         update = th->th_current_request;
183         LASSERT(update != NULL && update->ur_dt == dt);
184         if (update->ur_buf->ub_count > 0) {
185                 rc = osp_remote_sync(env, dt, update, NULL);
186                 th->th_sync = 1;
187         }
188
189         RETURN(rc);
190 }
191
192 /**
193  * Insert the update into the th_bufs for the device.
194  */
195 static int osp_insert_update(const struct lu_env *env,
196                              struct update_request *update, int op,
197                              struct lu_fid *fid, int count,
198                              int *lens, char **bufs)
199 {
200         struct update_buf    *ubuf = update->ur_buf;
201         struct update        *obj_update;
202         char                 *ptr;
203         int                   i;
204         int                   update_length;
205         int                   rc = 0;
206         ENTRY;
207
208         obj_update = (struct update *)((char *)ubuf +
209                       cfs_size_round(update_buf_size(ubuf)));
210
211         /* Check update size to make sure it can fit into the buffer */
212         update_length = cfs_size_round(offsetof(struct update,
213                                        u_bufs[0]));
214         for (i = 0; i < count; i++)
215                 update_length += cfs_size_round(lens[i]);
216
217         if (cfs_size_round(update_buf_size(ubuf)) + update_length >
218             UPDATE_BUFFER_SIZE || ubuf->ub_count >= UPDATE_MAX_OPS) {
219                 CERROR("%s: insert up %p, idx %d cnt %d len %lu: rc = %d\n",
220                         update->ur_dt->dd_lu_dev.ld_obd->obd_name, ubuf,
221                         update_length, ubuf->ub_count, update_buf_size(ubuf),
222                         -E2BIG);
223                 RETURN(-E2BIG);
224         }
225
226         if (count > UPDATE_BUF_COUNT) {
227                 CERROR("%s: Insert too much params %d "DFID" op %d: rc = %d\n",
228                         update->ur_dt->dd_lu_dev.ld_obd->obd_name, count,
229                         PFID(fid), op, -E2BIG);
230                 RETURN(-E2BIG);
231         }
232
233         /* fill the update into the update buffer */
234         fid_cpu_to_le(&obj_update->u_fid, fid);
235         obj_update->u_type = cpu_to_le32(op);
236         obj_update->u_batchid = update->ur_batchid;
237         for (i = 0; i < count; i++)
238                 obj_update->u_lens[i] = cpu_to_le32(lens[i]);
239
240         ptr = (char *)obj_update +
241                         cfs_size_round(offsetof(struct update, u_bufs[0]));
242         for (i = 0; i < count; i++)
243                 LOGL(bufs[i], lens[i], ptr);
244
245         ubuf->ub_count++;
246
247         CDEBUG(D_INFO, "%s: %p "DFID" idx %d: op %d params %d:%lu\n",
248                update->ur_dt->dd_lu_dev.ld_obd->obd_name, ubuf, PFID(fid),
249                ubuf->ub_count, op, count, update_buf_size(ubuf));
250
251         RETURN(rc);
252 }
253
254 static struct update_request
255 *osp_find_update(struct thandle *th, struct dt_device *dt_dev)
256 {
257         struct update_request   *update;
258
259         /* Because transaction api does not proivde the interface
260          * to transfer the update from LOD to OSP,  we need walk
261          * remote update list to find the update, this probably
262          * should move to LOD layer, when update can be part of
263          * the trancation api parameter. XXX */
264         cfs_list_for_each_entry(update, &th->th_remote_update_list, ur_list) {
265                 if (update->ur_dt == dt_dev)
266                         return update;
267         }
268         return NULL;
269 }
270
271 static inline void osp_md_add_update_batchid(struct update_request *update)
272 {
273         update->ur_batchid++;
274 }
275
276 /**
277  * Find one loc in th_dev/dev_obj_update for the update,
278  * Because only one thread can access this thandle, no need
279  * lock now.
280  */
281 static struct update_request
282 *osp_find_create_update_loc(struct thandle *th, struct dt_object *dt)
283 {
284         struct dt_device        *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
285         struct update_request   *update;
286         ENTRY;
287
288         update = osp_find_update(th, dt_dev);
289         if (update != NULL)
290                 RETURN(update);
291
292         update = osp_create_update_req(dt_dev);
293         if (IS_ERR(update))
294                 RETURN(update);
295
296         cfs_list_add_tail(&update->ur_list, &th->th_remote_update_list);
297
298         RETURN(update);
299 }
300
301 static int osp_get_attr_from_req(const struct lu_env *env,
302                                  struct ptlrpc_request *req,
303                                  struct lu_attr *attr, int index)
304 {
305         struct update_reply     *reply;
306         struct obdo             *lobdo = &osp_env_info(env)->osi_obdo;
307         struct obdo             *wobdo;
308         int                     size;
309
310         LASSERT(attr != NULL);
311
312         reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
313                                              UPDATE_BUFFER_SIZE);
314         if (reply == NULL || reply->ur_version != UPDATE_REPLY_V1)
315                 return -EPROTO;
316
317         size = update_get_reply_buf(reply, (void **)&wobdo, index);
318         if (size != sizeof(struct obdo))
319                 return -EPROTO;
320
321         obdo_le_to_cpu(wobdo, wobdo);
322         lustre_get_wire_obdo(NULL, lobdo, wobdo);
323         la_from_obdo(attr, lobdo, lobdo->o_valid);
324
325         return 0;
326 }
327
328 static int osp_md_declare_object_create(const struct lu_env *env,
329                                         struct dt_object *dt,
330                                         struct lu_attr *attr,
331                                         struct dt_allocation_hint *hint,
332                                         struct dt_object_format *dof,
333                                         struct thandle *th)
334 {
335         struct osp_thread_info  *osi = osp_env_info(env);
336         struct update_request   *update;
337         struct lu_fid           *fid1;
338         int                     sizes[2] = {sizeof(struct obdo), 0};
339         char                    *bufs[2] = {NULL, NULL};
340         int                     buf_count;
341         int                     rc;
342
343
344         update = osp_find_create_update_loc(th, dt);
345         if (IS_ERR(update)) {
346                 CERROR("%s: Get OSP update buf failed: rc = %d\n",
347                        dt->do_lu.lo_dev->ld_obd->obd_name,
348                        (int)PTR_ERR(update));
349                 return PTR_ERR(update);
350         }
351
352         osi->osi_obdo.o_valid = 0;
353         LASSERT(S_ISDIR(attr->la_mode));
354         obdo_from_la(&osi->osi_obdo, attr, attr->la_valid);
355         lustre_set_wire_obdo(NULL, &osi->osi_obdo, &osi->osi_obdo);
356         obdo_cpu_to_le(&osi->osi_obdo, &osi->osi_obdo);
357
358         bufs[0] = (char *)&osi->osi_obdo;
359         buf_count = 1;
360         fid1 = (struct lu_fid *)lu_object_fid(&dt->do_lu);
361         if (hint->dah_parent) {
362                 struct lu_fid *fid2;
363                 struct lu_fid *tmp_fid = &osi->osi_fid;
364
365                 fid2 = (struct lu_fid *)lu_object_fid(&hint->dah_parent->do_lu);
366                 fid_cpu_to_le(tmp_fid, fid2);
367                 sizes[1] = sizeof(*tmp_fid);
368                 bufs[1] = (char *)tmp_fid;
369                 buf_count++;
370         }
371
372         if (lu_object_exists(&dt->do_lu)) {
373                 /* If the object already exists, we needs to destroy
374                  * this orphan object first.
375                  *
376                  * The scenario might happen in this case
377                  *
378                  * 1. client send remote create to MDT0.
379                  * 2. MDT0 send create update to MDT1.
380                  * 3. MDT1 finished create synchronously.
381                  * 4. MDT0 failed and reboot.
382                  * 5. client resend remote create to MDT0.
383                  * 6. MDT0 tries to resend create update to MDT1,
384                  *    but find the object already exists
385                  */
386                 CDEBUG(D_HA, "%s: object "DFID" exists, destroy this orphan\n",
387                        dt->do_lu.lo_dev->ld_obd->obd_name, PFID(fid1));
388
389                 rc = osp_insert_update(env, update, OBJ_REF_DEL, fid1, 0,
390                                        NULL, NULL);
391                 if (rc != 0)
392                         GOTO(out, rc);
393
394                 if (S_ISDIR(lu_object_attr(&dt->do_lu))) {
395                         /* decrease for ".." */
396                         rc = osp_insert_update(env, update, OBJ_REF_DEL, fid1,
397                                                0, NULL, NULL);
398                         if (rc != 0)
399                                 GOTO(out, rc);
400                 }
401
402                 rc = osp_insert_update(env, update, OBJ_DESTROY, fid1, 0, NULL,
403                                        NULL);
404                 if (rc != 0)
405                         GOTO(out, rc);
406
407                 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
408                 /* Increase batchid to add this orphan object deletion
409                  * to separate transaction */
410                 osp_md_add_update_batchid(update);
411         }
412
413         rc = osp_insert_update(env, update, OBJ_CREATE, fid1, buf_count, sizes,
414                                bufs);
415 out:
416         if (rc)
417                 CERROR("%s: Insert update error: rc = %d\n",
418                        dt->do_lu.lo_dev->ld_obd->obd_name, rc);
419
420         return rc;
421 }
422
423 static int osp_md_object_create(const struct lu_env *env, struct dt_object *dt,
424                                 struct lu_attr *attr,
425                                 struct dt_allocation_hint *hint,
426                                 struct dt_object_format *dof,
427                                 struct thandle *th)
428 {
429         struct osp_object  *obj = dt2osp_obj(dt);
430
431         CDEBUG(D_INFO, "create object "DFID"\n",
432                PFID(&dt->do_lu.lo_header->loh_fid));
433
434         /* Because the create update RPC will be sent during declare phase,
435          * if creation reaches here, it means the object has been created
436          * successfully */
437         dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
438         obj->opo_empty = 1;
439
440         return 0;
441 }
442
443 static int osp_md_declare_object_ref_del(const struct lu_env *env,
444                                          struct dt_object *dt,
445                                          struct thandle *th)
446 {
447         struct update_request   *update;
448         struct lu_fid           *fid;
449         int                     rc;
450
451         update = osp_find_create_update_loc(th, dt);
452         if (IS_ERR(update)) {
453                 CERROR("%s: Get OSP update buf failed: rc = %d\n",
454                        dt->do_lu.lo_dev->ld_obd->obd_name,
455                       (int)PTR_ERR(update));
456                 return PTR_ERR(update);
457         }
458
459         fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
460
461         rc = osp_insert_update(env, update, OBJ_REF_DEL, fid, 0, NULL, NULL);
462
463         return rc;
464 }
465
466 static int osp_md_object_ref_del(const struct lu_env *env,
467                                  struct dt_object *dt,
468                                  struct thandle *th)
469 {
470         CDEBUG(D_INFO, "ref del object "DFID"\n",
471                PFID(&dt->do_lu.lo_header->loh_fid));
472
473         return 0;
474 }
475
476 static int osp_md_declare_ref_add(const struct lu_env *env,
477                                   struct dt_object *dt, struct thandle *th)
478 {
479         struct update_request   *update;
480         struct lu_fid           *fid;
481         int                     rc;
482
483         update = osp_find_create_update_loc(th, dt);
484         if (IS_ERR(update)) {
485                 CERROR("%s: Get OSP update buf failed: rc = %d\n",
486                        dt->do_lu.lo_dev->ld_obd->obd_name,
487                        (int)PTR_ERR(update));
488                 return PTR_ERR(update);
489         }
490
491         fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
492
493         rc = osp_insert_update(env, update, OBJ_REF_ADD, fid, 0, NULL, NULL);
494
495         return rc;
496 }
497
498 static int osp_md_object_ref_add(const struct lu_env *env,
499                                  struct dt_object *dt,
500                                  struct thandle *th)
501 {
502         CDEBUG(D_INFO, "ref add object "DFID"\n",
503                PFID(&dt->do_lu.lo_header->loh_fid));
504
505         return 0;
506 }
507
508 static void osp_md_ah_init(const struct lu_env *env,
509                            struct dt_allocation_hint *ah,
510                            struct dt_object *parent,
511                            struct dt_object *child,
512                            umode_t child_mode)
513 {
514         LASSERT(ah);
515
516         memset(ah, 0, sizeof(*ah));
517         ah->dah_parent = parent;
518         ah->dah_mode = child_mode;
519 }
520
521 static int osp_md_declare_attr_set(const struct lu_env *env,
522                                    struct dt_object *dt,
523                                    const struct lu_attr *attr,
524                                    struct thandle *th)
525 {
526         struct osp_thread_info *osi = osp_env_info(env);
527         struct update_request  *update;
528         struct lu_fid          *fid;
529         int                     size = sizeof(struct obdo);
530         char                   *buf;
531         int                     rc;
532
533         update = osp_find_create_update_loc(th, dt);
534         if (IS_ERR(update)) {
535                 CERROR("%s: Get OSP update buf failed: %d\n",
536                        dt->do_lu.lo_dev->ld_obd->obd_name,
537                        (int)PTR_ERR(update));
538                 return PTR_ERR(update);
539         }
540
541         osi->osi_obdo.o_valid = 0;
542         LASSERT(!(attr->la_valid & (LA_MODE | LA_TYPE)));
543         obdo_from_la(&osi->osi_obdo, (struct lu_attr *)attr,
544                      attr->la_valid);
545         lustre_set_wire_obdo(NULL, &osi->osi_obdo, &osi->osi_obdo);
546         obdo_cpu_to_le(&osi->osi_obdo, &osi->osi_obdo);
547
548         buf = (char *)&osi->osi_obdo;
549         fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
550
551         rc = osp_insert_update(env, update, OBJ_ATTR_SET, fid, 1, &size, &buf);
552
553         return rc;
554 }
555
556 static int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
557                            const struct lu_attr *attr, struct thandle *th,
558                            struct lustre_capa *capa)
559 {
560         CDEBUG(D_INFO, "attr set object "DFID"\n",
561                PFID(&dt->do_lu.lo_header->loh_fid));
562
563         RETURN(0);
564 }
565
566 static int osp_md_declare_xattr_set(const struct lu_env *env,
567                                     struct dt_object *dt,
568                                     const struct lu_buf *buf,
569                                     const char *name, int flag,
570                                     struct thandle *th)
571 {
572         struct update_request   *update;
573         struct lu_fid           *fid;
574         int                     sizes[3] = {strlen(name), buf->lb_len,
575                                             sizeof(int)};
576         char                    *bufs[3] = {(char *)name, (char *)buf->lb_buf };
577         int                     rc;
578
579         LASSERT(buf->lb_len > 0 && buf->lb_buf != NULL);
580         update = osp_find_create_update_loc(th, dt);
581         if (IS_ERR(update)) {
582                 CERROR("%s: Get OSP update buf failed: rc = %d\n",
583                        dt->do_lu.lo_dev->ld_obd->obd_name,
584                        (int)PTR_ERR(update));
585                 return PTR_ERR(update);
586         }
587
588         flag = cpu_to_le32(flag);
589         bufs[2] = (char *)&flag;
590
591         fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
592         rc = osp_insert_update(env, update, OBJ_XATTR_SET, fid,
593                                ARRAY_SIZE(sizes), sizes, bufs);
594
595         return rc;
596 }
597
598 static int osp_md_xattr_set(const struct lu_env *env, struct dt_object *dt,
599                             const struct lu_buf *buf, const char *name, int fl,
600                             struct thandle *th, struct lustre_capa *capa)
601 {
602         CDEBUG(D_INFO, "xattr %s set object "DFID"\n", name,
603                PFID(&dt->do_lu.lo_header->loh_fid));
604
605         return 0;
606 }
607
608 static int osp_md_xattr_get(const struct lu_env *env, struct dt_object *dt,
609                             struct lu_buf *buf, const char *name,
610                             struct lustre_capa *capa)
611 {
612         struct dt_device        *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
613         struct update_request   *update = NULL;
614         struct ptlrpc_request   *req = NULL;
615         int                     rc;
616         int                     buf_len;
617         int                     size;
618         struct update_reply     *reply;
619         void                    *ea_buf;
620         ENTRY;
621
622         /* Because it needs send the update buffer right away,
623          * just create an update buffer, instead of attaching the
624          * update_remote list of the thandle.
625          */
626         update = osp_create_update_req(dt_dev);
627         if (IS_ERR(update))
628                 RETURN(PTR_ERR(update));
629
630         LASSERT(name != NULL);
631         buf_len = strlen(name);
632         rc = osp_insert_update(env, update, OBJ_XATTR_GET,
633                                (struct lu_fid *)lu_object_fid(&dt->do_lu),
634                                1, &buf_len, (char **)&name);
635         if (rc != 0) {
636                 CERROR("%s: Insert update error: rc = %d\n",
637                        dt->do_lu.lo_dev->ld_obd->obd_name, rc);
638                 GOTO(out, rc);
639         }
640         dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
641
642         rc = osp_remote_sync(env, dt_dev, update, &req);
643         if (rc != 0)
644                 GOTO(out, rc);
645
646         reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
647                                             UPDATE_BUFFER_SIZE);
648         if (reply->ur_version != UPDATE_REPLY_V1) {
649                 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
650                        dt_dev->dd_lu_dev.ld_obd->obd_name,
651                        reply->ur_version, UPDATE_REPLY_V1, -EPROTO);
652                 GOTO(out, rc = -EPROTO);
653         }
654
655         size = update_get_reply_buf(reply, &ea_buf, 0);
656         if (size < 0)
657                 GOTO(out, rc = size);
658
659         LASSERT(size > 0 && size < PAGE_CACHE_SIZE);
660         LASSERT(ea_buf != NULL);
661
662         rc = size;
663         if (buf->lb_buf != NULL)
664                 memcpy(buf->lb_buf, ea_buf, size);
665 out:
666         if (req != NULL)
667                 ptlrpc_req_finished(req);
668
669         osp_destroy_update_req(update);
670
671         RETURN(rc);
672 }
673
674 static void osp_md_object_read_lock(const struct lu_env *env,
675                                     struct dt_object *dt, unsigned role)
676 {
677         struct osp_object  *obj = dt2osp_obj(dt);
678
679         LASSERT(obj->opo_owner != env);
680         down_read_nested(&obj->opo_sem, role);
681
682         LASSERT(obj->opo_owner == NULL);
683 }
684
685 static void osp_md_object_write_lock(const struct lu_env *env,
686                                      struct dt_object *dt, unsigned role)
687 {
688         struct osp_object *obj = dt2osp_obj(dt);
689
690         down_write_nested(&obj->opo_sem, role);
691
692         LASSERT(obj->opo_owner == NULL);
693         obj->opo_owner = env;
694 }
695
696 static void osp_md_object_read_unlock(const struct lu_env *env,
697                                       struct dt_object *dt)
698 {
699         struct osp_object *obj = dt2osp_obj(dt);
700
701         up_read(&obj->opo_sem);
702 }
703
704 static void osp_md_object_write_unlock(const struct lu_env *env,
705                                        struct dt_object *dt)
706 {
707         struct osp_object *obj = dt2osp_obj(dt);
708
709         LASSERT(obj->opo_owner == env);
710         obj->opo_owner = NULL;
711         up_write(&obj->opo_sem);
712 }
713
714 static int osp_md_object_write_locked(const struct lu_env *env,
715                                       struct dt_object *dt)
716 {
717         struct osp_object *obj = dt2osp_obj(dt);
718
719         return obj->opo_owner == env;
720 }
721
722 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
723                                struct dt_rec *rec, const struct dt_key *key,
724                                struct lustre_capa *capa)
725 {
726         struct dt_device        *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
727         struct update_request   *update;
728         struct ptlrpc_request   *req = NULL;
729         int                     size = strlen((char *)key) + 1;
730         char                    *name = (char *)key;
731         int                     rc;
732         struct update_reply     *reply;
733         struct lu_fid           *fid;
734
735         ENTRY;
736
737         /* Because it needs send the update buffer right away,
738          * just create an update buffer, instead of attaching the
739          * update_remote list of the thandle.
740          */
741         update = osp_create_update_req(dt_dev);
742         if (IS_ERR(update))
743                 RETURN(PTR_ERR(update));
744
745         rc = osp_insert_update(env, update, OBJ_INDEX_LOOKUP,
746                                (struct lu_fid *)lu_object_fid(&dt->do_lu),
747                                1, &size, (char **)&name);
748         if (rc) {
749                 CERROR("%s: Insert update error: rc = %d\n",
750                        dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
751                 GOTO(out, rc);
752         }
753
754         rc = osp_remote_sync(env, dt_dev, update, &req);
755         if (rc < 0)
756                 GOTO(out, rc);
757
758         reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
759                                              UPDATE_BUFFER_SIZE);
760         if (reply->ur_version != UPDATE_REPLY_V1) {
761                 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
762                        dt_dev->dd_lu_dev.ld_obd->obd_name,
763                        reply->ur_version, UPDATE_REPLY_V1, -EPROTO);
764                 GOTO(out, rc = -EPROTO);
765         }
766
767         rc = update_get_reply_result(reply, NULL, 0);
768         if (rc < 0) {
769                 CERROR("%s: wrong version lookup "DFID" %s: rc = %d\n",
770                        dt_dev->dd_lu_dev.ld_obd->obd_name,
771                        PFID(lu_object_fid(&dt->do_lu)), (char *)key, rc);
772                 GOTO(out, rc);
773         }
774
775         size = update_get_reply_buf(reply, (void **)&fid, 0);
776         if (size < 0)
777                 GOTO(out, rc = size);
778
779         if (size != sizeof(struct lu_fid)) {
780                 CERROR("%s: lookup "DFID" %s wrong size %d: rc = %d\n",
781                        dt_dev->dd_lu_dev.ld_obd->obd_name,
782                        PFID(lu_object_fid(&dt->do_lu)), (char *)key, size, rc);
783                 GOTO(out, rc = -EINVAL);
784         }
785
786         fid_le_to_cpu(fid, fid);
787         if (!fid_is_sane(fid)) {
788                 CERROR("%s: lookup "DFID" %s invalid fid "DFID": rc = %d\n",
789                        dt_dev->dd_lu_dev.ld_obd->obd_name,
790                        PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid),
791                        rc);
792                 GOTO(out, rc = -EINVAL);
793         }
794         memcpy(rec, fid, sizeof(*fid));
795 out:
796         if (req != NULL)
797                 ptlrpc_req_finished(req);
798
799         osp_destroy_update_req(update);
800
801         RETURN(rc);
802 }
803
804 static int osp_md_declare_insert(const struct lu_env *env,
805                                  struct dt_object *dt,
806                                  const struct dt_rec *rec,
807                                  const struct dt_key *key,
808                                  struct thandle *th)
809 {
810         struct update_request   *update;
811         struct lu_fid           *fid;
812         struct lu_fid           *rec_fid = (struct lu_fid *)rec;
813         int                     size[2] = {strlen((char *)key) + 1,
814                                                   sizeof(*rec_fid)};
815         char                    *bufs[2] = {(char *)key, (char *)rec_fid};
816         int                     rc;
817
818         update = osp_find_create_update_loc(th, dt);
819         if (IS_ERR(update)) {
820                 CERROR("%s: Get OSP update buf failed: rc = %d\n",
821                        dt->do_lu.lo_dev->ld_obd->obd_name,
822                        (int)PTR_ERR(update));
823                 return PTR_ERR(update);
824         }
825
826         fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
827
828         CDEBUG(D_INFO, "%s: insert index of "DFID" %s: "DFID"\n",
829                dt->do_lu.lo_dev->ld_obd->obd_name,
830                PFID(fid), (char *)key, PFID(rec_fid));
831
832         fid_cpu_to_le(rec_fid, rec_fid);
833
834         rc = osp_insert_update(env, update, OBJ_INDEX_INSERT, fid,
835                                ARRAY_SIZE(size), size, bufs);
836         return rc;
837 }
838
839 static int osp_md_index_insert(const struct lu_env *env,
840                                struct dt_object *dt,
841                                const struct dt_rec *rec,
842                                const struct dt_key *key,
843                                struct thandle *th,
844                                struct lustre_capa *capa,
845                                int ignore_quota)
846 {
847         return 0;
848 }
849
850 static int osp_md_declare_delete(const struct lu_env *env,
851                                  struct dt_object *dt,
852                                  const struct dt_key *key,
853                                  struct thandle *th)
854 {
855         struct update_request *update;
856         struct lu_fid *fid;
857         int size = strlen((char *)key) + 1;
858         char *buf = (char *)key;
859         int rc;
860
861         update = osp_find_create_update_loc(th, dt);
862         if (IS_ERR(update)) {
863                 CERROR("%s: Get OSP update buf failed: rc = %d\n",
864                        dt->do_lu.lo_dev->ld_obd->obd_name,
865                        (int)PTR_ERR(update));
866                 return PTR_ERR(update);
867         }
868
869         fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
870
871         rc = osp_insert_update(env, update, OBJ_INDEX_DELETE, fid, 1, &size,
872                                &buf);
873
874         return rc;
875 }
876
877 static int osp_md_index_delete(const struct lu_env *env,
878                                struct dt_object *dt,
879                                const struct dt_key *key,
880                                struct thandle *th,
881                                struct lustre_capa *capa)
882 {
883         CDEBUG(D_INFO, "index delete "DFID" %s\n",
884                PFID(&dt->do_lu.lo_header->loh_fid), (char *)key);
885
886         return 0;
887 }
888
889 /**
890  * Creates or initializes iterator context.
891  *
892  * Note: for OSP, these index iterate api is only used to check
893  * whether the directory is empty now (see mdd_dir_is_empty).
894  * Since dir_empty will be return by OBJ_ATTR_GET(see osp_md_attr_get/
895  * out_attr_get). So the implementation of these iterator is simplied
896  * to make mdd_dir_is_empty happy. The real iterator should be
897  * implemented, if we need it one day.
898  */
899 static struct dt_it *osp_it_init(const struct lu_env *env,
900                                  struct dt_object *dt,
901                                  __u32 attr,
902                                 struct lustre_capa *capa)
903 {
904         lu_object_get(&dt->do_lu);
905         return (struct dt_it *)dt;
906 }
907
908 static void osp_it_fini(const struct lu_env *env, struct dt_it *di)
909 {
910         struct dt_object *dt = (struct dt_object *)di;
911         lu_object_put(env, &dt->do_lu);
912 }
913
914 static int osp_it_get(const struct lu_env *env,
915                       struct dt_it *di, const struct dt_key *key)
916 {
917         return 1;
918 }
919
920 static void osp_it_put(const struct lu_env *env, struct dt_it *di)
921 {
922         return;
923 }
924
925 static int osp_it_next(const struct lu_env *env, struct dt_it *di)
926 {
927         struct dt_object *dt = (struct dt_object *)di;
928         struct osp_object *o = dt2osp_obj(dt);
929
930         if (o->opo_empty)
931                 return 1;
932
933         return 0;
934 }
935
936 static struct dt_key *osp_it_key(const struct lu_env *env,
937                                  const struct dt_it *di)
938 {
939         LBUG();
940         return NULL;
941 }
942
943 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
944 {
945         LBUG();
946         return 0;
947 }
948
949 static int osp_it_rec(const struct lu_env *env, const struct dt_it *di,
950                       struct dt_rec *lde, __u32 attr)
951 {
952         LBUG();
953         return 0;
954 }
955
956 static __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
957 {
958         LBUG();
959         return 0;
960 }
961
962 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
963                        __u64 hash)
964 {
965         LBUG();
966         return 0;
967 }
968
969 static int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
970                           void *key_rec)
971 {
972         LBUG();
973         return 0;
974 }
975
976 static const struct dt_index_operations osp_md_index_ops = {
977         .dio_lookup         = osp_md_index_lookup,
978         .dio_declare_insert = osp_md_declare_insert,
979         .dio_insert         = osp_md_index_insert,
980         .dio_declare_delete = osp_md_declare_delete,
981         .dio_delete         = osp_md_index_delete,
982         .dio_it     = {
983                 .init     = osp_it_init,
984                 .fini     = osp_it_fini,
985                 .get      = osp_it_get,
986                 .put      = osp_it_put,
987                 .next     = osp_it_next,
988                 .key      = osp_it_key,
989                 .key_size = osp_it_key_size,
990                 .rec      = osp_it_rec,
991                 .store    = osp_it_store,
992                 .load     = osp_it_load,
993                 .key_rec  = osp_it_key_rec,
994         }
995 };
996
997 static int osp_md_index_try(const struct lu_env *env,
998                             struct dt_object *dt,
999                             const struct dt_index_features *feat)
1000 {
1001         dt->do_index_ops = &osp_md_index_ops;
1002         return 0;
1003 }
1004
1005 static int osp_md_attr_get(const struct lu_env *env,
1006                            struct dt_object *dt, struct lu_attr *attr,
1007                            struct lustre_capa *capa)
1008 {
1009         struct osp_object     *obj = dt2osp_obj(dt);
1010         struct dt_device      *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
1011         struct update_request *update = NULL;
1012         struct ptlrpc_request *req = NULL;
1013         int rc;
1014         ENTRY;
1015
1016         /* Because it needs send the update buffer right away,
1017          * just create an update buffer, instead of attaching the
1018          * update_remote list of the thandle.
1019          */
1020         update = osp_create_update_req(dt_dev);
1021         if (IS_ERR(update))
1022                 RETURN(PTR_ERR(update));
1023
1024         rc = osp_insert_update(env, update, OBJ_ATTR_GET,
1025                                (struct lu_fid *)lu_object_fid(&dt->do_lu),
1026                                0, NULL, NULL);
1027         if (rc) {
1028                 CERROR("%s: Insert update error: rc = %d\n",
1029                        dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1030                 GOTO(out, rc);
1031         }
1032         dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
1033
1034         rc = osp_remote_sync(env, dt_dev, update, &req);
1035         if (rc < 0)
1036                 GOTO(out, rc);
1037
1038         rc = osp_get_attr_from_req(env, req, attr, 0);
1039         if (rc)
1040                 GOTO(out, rc);
1041
1042         if (attr->la_flags == 1)
1043                 obj->opo_empty = 0;
1044         else
1045                 obj->opo_empty = 1;
1046 out:
1047         if (req != NULL)
1048                 ptlrpc_req_finished(req);
1049
1050         osp_destroy_update_req(update);
1051
1052         RETURN(rc);
1053 }
1054
1055 static int osp_md_declare_object_destroy(const struct lu_env *env,
1056                                          struct dt_object *dt,
1057                                          struct thandle *th)
1058 {
1059         struct osp_object  *o = dt2osp_obj(dt);
1060         int                 rc = 0;
1061         ENTRY;
1062
1063         /*
1064          * track objects to be destroyed via llog
1065          */
1066         rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1067
1068         RETURN(rc);
1069 }
1070
1071 static int osp_md_object_destroy(const struct lu_env *env,
1072                                  struct dt_object *dt, struct thandle *th)
1073 {
1074         struct osp_object  *o = dt2osp_obj(dt);
1075         int                 rc = 0;
1076         ENTRY;
1077
1078         /*
1079          * once transaction is committed put proper command on
1080          * the queue going to our OST
1081          */
1082         rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1083
1084         /* not needed in cache any more */
1085         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1086
1087         RETURN(rc);
1088 }
1089
1090 static int osp_md_object_lock(const struct lu_env *env,
1091                               struct dt_object *dt,
1092                               struct lustre_handle *lh,
1093                               struct ldlm_enqueue_info *einfo,
1094                               void *policy)
1095 {
1096         struct osp_thread_info *info = osp_env_info(env);
1097         struct ldlm_res_id     *res_id = &info->osi_resid;
1098         struct dt_device       *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
1099         struct osp_device      *osp = dt2osp_dev(dt_dev);
1100         struct ptlrpc_request  *req = NULL;
1101         int                     rc = 0;
1102         __u64                   flags = 0;
1103         ldlm_mode_t             mode;
1104
1105         fid_build_reg_res_name(lu_object_fid(&dt->do_lu), res_id);
1106
1107         mode = ldlm_lock_match(osp->opd_obd->obd_namespace,
1108                                LDLM_FL_BLOCK_GRANTED, res_id,
1109                                einfo->ei_type,
1110                                (ldlm_policy_data_t *)policy,
1111                                einfo->ei_mode, lh, 0);
1112         if (mode > 0)
1113                 return ELDLM_OK;
1114
1115         req = ldlm_enqueue_pack(osp->opd_exp, 0);
1116         if (IS_ERR(req))
1117                 RETURN(PTR_ERR(req));
1118
1119         rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
1120                               (const ldlm_policy_data_t *)policy,
1121                               &flags, NULL, 0, LVB_T_NONE, lh, 0);
1122
1123         ptlrpc_req_finished(req);
1124
1125         return rc == ELDLM_OK ? 0 : -EIO;
1126 }
1127
1128 struct dt_object_operations osp_md_obj_ops = {
1129         .do_read_lock         = osp_md_object_read_lock,
1130         .do_write_lock        = osp_md_object_write_lock,
1131         .do_read_unlock       = osp_md_object_read_unlock,
1132         .do_write_unlock      = osp_md_object_write_unlock,
1133         .do_write_locked      = osp_md_object_write_locked,
1134         .do_declare_create    = osp_md_declare_object_create,
1135         .do_create            = osp_md_object_create,
1136         .do_declare_ref_add   = osp_md_declare_ref_add,
1137         .do_ref_add           = osp_md_object_ref_add,
1138         .do_declare_ref_del   = osp_md_declare_object_ref_del,
1139         .do_ref_del           = osp_md_object_ref_del,
1140         .do_declare_destroy   = osp_md_declare_object_destroy,
1141         .do_destroy           = osp_md_object_destroy,
1142         .do_ah_init           = osp_md_ah_init,
1143         .do_attr_get          = osp_md_attr_get,
1144         .do_declare_attr_set  = osp_md_declare_attr_set,
1145         .do_attr_set          = osp_md_attr_set,
1146         .do_declare_xattr_set = osp_md_declare_xattr_set,
1147         .do_xattr_set         = osp_md_xattr_set,
1148         .do_xattr_get         = osp_md_xattr_get,
1149         .do_index_try         = osp_md_index_try,
1150         .do_object_lock       = osp_md_object_lock,
1151 };