Whamcloud - gitweb
LU-3534 osp: transfer updates with bulk RPC
[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, 2014, Intel Corporation.
24  */
25 /*
26  * lustre/osp/osp_md_object.c
27  *
28  * OST/MDT proxy device (OSP) Metadata methods
29  *
30  * This file implements methods for remote MD object, which include
31  * dt_object_operations, dt_index_operations and dt_body_operations.
32  *
33  * If there are multiple MDTs in one filesystem, one operation might
34  * include modifications in several MDTs. In such cases, clients
35  * send the RPC to the master MDT, then the operation is decomposed into
36  * object updates which will be dispatched to OSD or OSP. The local updates
37  * go to local OSD and the remote updates go to OSP. In OSP, these remote
38  * object updates will be packed into an update RPC, sent to the remote MDT
39  * and handled by Object Update Target (OUT).
40  *
41  * In DNE phase I, because of missing complete recovery solution, updates
42  * will be executed in order and synchronously.
43  *     1. The transaction is created.
44  *     2. In transaction declare, it collects and packs remote
45  *        updates (in osp_md_declare_xxx()).
46  *     3. In transaction start, it sends these remote updates
47  *        to remote MDTs, which will execute these updates synchronously.
48  *     4. In transaction execute phase, the local updates will be executed
49  *        synchronously.
50  *
51  * Author: Di Wang <di.wang@intel.com>
52  */
53
54 #define DEBUG_SUBSYSTEM S_MDS
55
56 #include <lustre_log.h>
57 #include "osp_internal.h"
58
59 #define OUT_UPDATE_BUFFER_SIZE_ADD      4096
60 #define OUT_UPDATE_BUFFER_SIZE_MAX      (256 * 4096)  /*  1M update size now */
61
62 /**
63  * Interpreter call for object creation
64  *
65  * Object creation interpreter, which will be called after creating
66  * the remote object to set flags and status.
67  *
68  * \param[in] env       execution environment
69  * \param[in] reply     update reply
70  * \param[in] req       ptlrpc update request for creating object
71  * \param[in] obj       object to be created
72  * \param[in] data      data used in this function.
73  * \param[in] index     index(position) of create update in the whole
74  *                      updates
75  * \param[in] rc        update result on the remote MDT.
76  *
77  * \retval              only return 0 for now
78  */
79 static int osp_object_create_interpreter(const struct lu_env *env,
80                                          struct object_update_reply *reply,
81                                          struct ptlrpc_request *req,
82                                          struct osp_object *obj,
83                                          void *data, int index, int rc)
84 {
85         if (rc != 0) {
86                 obj->opo_obj.do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
87                 obj->opo_non_exist = 1;
88         }
89
90         /* Invalid the opo cache for the object after the object
91          * is being created, so attr_get will try to get attr
92          * from the remote object. XXX this can be improved when
93          * we have object lock/cache invalidate mechanism in OSP
94          * layer */
95         if (obj->opo_ooa != NULL) {
96                 spin_lock(&obj->opo_lock);
97                 obj->opo_ooa->ooa_attr.la_valid = 0;
98                 spin_unlock(&obj->opo_lock);
99         }
100
101         return 0;
102 }
103
104 /**
105  * Implementation of dt_object_operations::do_declare_create
106  *
107  * Create the osp_update_request to track the update for this OSP
108  * in the transaction.
109  *
110  * \param[in] env       execution environment
111  * \param[in] dt        remote object to be created
112  * \param[in] attr      attribute of the created object
113  * \param[in] hint      creation hint
114  * \param[in] dof       creation format information
115  * \param[in] th        the transaction handle
116  *
117  * \retval              0 if preparation succeeds.
118  * \retval              negative errno if preparation fails.
119  */
120 int osp_md_declare_object_create(const struct lu_env *env,
121                                  struct dt_object *dt,
122                                  struct lu_attr *attr,
123                                  struct dt_allocation_hint *hint,
124                                  struct dt_object_format *dof,
125                                  struct thandle *th)
126 {
127         struct osp_object *obj = dt2osp_obj(dt);
128         int               rc;
129
130         if (obj->opo_ooa == NULL) {
131                 rc = osp_oac_init(obj);
132                 if (rc != 0)
133                         return rc;
134         }
135
136         return osp_trans_update_request_create(th);
137 }
138
139 struct object_update *
140 update_buffer_get_update(struct object_update_request *request,
141                          unsigned int index)
142 {
143         void    *ptr;
144         int     i;
145
146         if (index > request->ourq_count)
147                 return NULL;
148
149         ptr = &request->ourq_updates[0];
150         for (i = 0; i < index; i++)
151                 ptr += object_update_size(ptr);
152
153         return ptr;
154 }
155
156 /**
157  * Implementation of dt_object_operations::do_create
158  *
159  * It adds an OUT_CREATE sub-request into the OUT RPC that will be flushed
160  * when the transaction stop, and sets necessary flags for created object.
161  *
162  * \param[in] env       execution environment
163  * \param[in] dt        object to be created
164  * \param[in] attr      attribute of the created object
165  * \param[in] hint      creation hint
166  * \param[in] dof       creation format information
167  * \param[in] th        the transaction handle
168  *
169  * \retval              0 if packing creation succeeds.
170  * \retval              negative errno if packing creation fails.
171  */
172 int osp_md_object_create(const struct lu_env *env, struct dt_object *dt,
173                          struct lu_attr *attr, struct dt_allocation_hint *hint,
174                          struct dt_object_format *dof, struct thandle *th)
175 {
176         struct osp_update_request       *update;
177         struct osp_object               *obj = dt2osp_obj(dt);
178         int                             rc;
179
180         update = thandle_to_osp_update_request(th);
181         LASSERT(update != NULL);
182
183         LASSERT(attr->la_valid & LA_TYPE);
184         rc = osp_update_rpc_pack(env, create, update, OUT_CREATE,
185                                  lu_object_fid(&dt->do_lu), attr, hint, dof);
186         if (rc != 0)
187                 GOTO(out, rc);
188
189         rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
190                                         osp_object_create_interpreter);
191
192         if (rc < 0)
193                 GOTO(out, rc);
194
195         dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
196         dt2osp_obj(dt)->opo_non_exist = 0;
197
198         LASSERT(obj->opo_ooa != NULL);
199         obj->opo_ooa->ooa_attr = *attr;
200 out:
201         return rc;
202 }
203
204 /**
205  * Implementation of dt_object_operations::do_declare_ref_del
206  *
207  * Create the osp_update_request to track the update for this OSP
208  * in the transaction.
209  *
210  * \param[in] env       execution environment
211  * \param[in] dt        object to decrease the reference count.
212  * \param[in] th        the transaction handle of refcount decrease.
213  *
214  * \retval              0 if preparation succeeds.
215  * \retval              negative errno if preparation fails.
216  */
217 static int osp_md_declare_ref_del(const struct lu_env *env,
218                                   struct dt_object *dt, struct thandle *th)
219 {
220         return osp_trans_update_request_create(th);
221 }
222
223 /**
224  * Implementation of dt_object_operations::do_ref_del
225  *
226  * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
227  * flushed when the transaction stop.
228  *
229  * \param[in] env       execution environment
230  * \param[in] dt        object to decrease the reference count
231  * \param[in] th        the transaction handle
232  *
233  * \retval              0 if packing ref_del succeeds.
234  * \retval              negative errno if packing fails.
235  */
236 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
237                           struct thandle *th)
238 {
239         struct osp_update_request       *update;
240         int                             rc;
241
242         update = thandle_to_osp_update_request(th);
243         LASSERT(update != NULL);
244
245         rc = osp_update_rpc_pack(env, ref_del, update, OUT_REF_DEL,
246                                  lu_object_fid(&dt->do_lu));
247         return rc;
248 }
249
250 /**
251  * Implementation of dt_object_operations::do_declare_ref_del
252  *
253  * Create the osp_update_request to track the update for this OSP
254  * in the transaction.
255  *
256  * \param[in] env       execution environment
257  * \param[in] dt        object on which to increase the reference count.
258  * \param[in] th        the transaction handle.
259  *
260  * \retval              0 if preparation succeeds.
261  * \retval              negative errno if preparation fails.
262  */
263 static int osp_md_declare_ref_add(const struct lu_env *env,
264                                   struct dt_object *dt, struct thandle *th)
265 {
266         return osp_trans_update_request_create(th);
267 }
268
269 /**
270  * Implementation of dt_object_operations::do_ref_add
271  *
272  * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
273  * when the transaction stop.
274  *
275  * \param[in] env       execution environment
276  * \param[in] dt        object on which to increase the reference count
277  * \param[in] th        the transaction handle
278  *
279  * \retval              0 if packing ref_add succeeds.
280  * \retval              negative errno if packing fails.
281  */
282 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
283                           struct thandle *th)
284 {
285         struct osp_update_request       *update;
286         int                             rc;
287
288         update = thandle_to_osp_update_request(th);
289         LASSERT(update != NULL);
290
291         rc = osp_update_rpc_pack(env, ref_add, update, OUT_REF_ADD,
292                                  lu_object_fid(&dt->do_lu));
293         return rc;
294 }
295
296 /**
297  * Implementation of dt_object_operations::do_ah_init
298  *
299  * Initialize the allocation hint for object creation, which is usually called
300  * before the creation, and these hints (parent and child mode) will be sent to
301  * the remote Object Update Target (OUT) and used in the object create process,
302  * same as OSD object creation.
303  *
304  * \param[in] env       execution environment
305  * \param[in] ah        the hint to be initialized
306  * \param[in] parent    the parent of the object
307  * \param[in] child     the object to be created
308  * \param[in] child_mode the mode of the created object
309  */
310 static void osp_md_ah_init(const struct lu_env *env,
311                            struct dt_allocation_hint *ah,
312                            struct dt_object *parent,
313                            struct dt_object *child,
314                            umode_t child_mode)
315 {
316         LASSERT(ah);
317
318         ah->dah_parent = parent;
319         ah->dah_mode = child_mode;
320 }
321
322 /**
323  * Implementation of dt_object_operations::do_declare_attr_get
324  *
325  * Create the osp_update_request to track the update for this OSP
326  * in the transaction.
327  *
328  * \param[in] env       execution environment
329  * \param[in] dt        object on which to set attributes
330  * \param[in] attr      attributes to be set
331  * \param[in] th        the transaction handle
332  *
333  * \retval              0 if preparation succeeds.
334  * \retval              negative errno if preparation fails.
335  */
336 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
337                             const struct lu_attr *attr, struct thandle *th)
338 {
339         return osp_trans_update_request_create(th);
340 }
341
342 /**
343  * Implementation of dt_object_operations::do_attr_set
344  *
345  * Set attributes to the specified remote object.
346  *
347  * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
348  * when the transaction stop.
349  *
350  * \param[in] env       execution environment
351  * \param[in] dt        object to set attributes
352  * \param[in] attr      attributes to be set
353  * \param[in] th        the transaction handle
354  *
355  * \retval              0 if packing attr_set succeeds.
356  * \retval              negative errno if packing fails.
357  */
358 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
359                     const struct lu_attr *attr, struct thandle *th)
360 {
361         struct osp_update_request       *update;
362         int                             rc;
363
364         update = thandle_to_osp_update_request(th);
365         LASSERT(update != NULL);
366
367         rc = osp_update_rpc_pack(env, attr_set, update, OUT_ATTR_SET,
368                                  lu_object_fid(&dt->do_lu), attr);
369         return rc;
370 }
371
372 /**
373  * Implementation of dt_object_operations::do_read_lock
374  *
375  * osp_md_object_{read,write}_lock() will only lock the remote object in the
376  * local cache, which uses the semaphore (opo_sem) inside the osp_object to
377  * lock the object. Note: it will not lock the object in the whole cluster,
378  * which relies on the LDLM lock.
379  *
380  * \param[in] env       execution environment
381  * \param[in] dt        object to be locked
382  * \param[in] role      lock role from MDD layer, see mdd_object_role().
383  */
384 static void osp_md_object_read_lock(const struct lu_env *env,
385                                     struct dt_object *dt, unsigned role)
386 {
387         struct osp_object  *obj = dt2osp_obj(dt);
388
389         LASSERT(obj->opo_owner != env);
390         down_read_nested(&obj->opo_sem, role);
391
392         LASSERT(obj->opo_owner == NULL);
393 }
394
395 /**
396  * Implementation of dt_object_operations::do_write_lock
397  *
398  * Lock the remote object in write mode.
399  *
400  * \param[in] env       execution environment
401  * \param[in] dt        object to be locked
402  * \param[in] role      lock role from MDD layer, see mdd_object_role().
403  */
404 static void osp_md_object_write_lock(const struct lu_env *env,
405                                      struct dt_object *dt, unsigned role)
406 {
407         struct osp_object *obj = dt2osp_obj(dt);
408
409         down_write_nested(&obj->opo_sem, role);
410
411         LASSERT(obj->opo_owner == NULL);
412         obj->opo_owner = env;
413 }
414
415 /**
416  * Implementation of dt_object_operations::do_read_unlock
417  *
418  * Unlock the read lock of remote object.
419  *
420  * \param[in] env       execution environment
421  * \param[in] dt        object to be unlocked
422  */
423 static void osp_md_object_read_unlock(const struct lu_env *env,
424                                       struct dt_object *dt)
425 {
426         struct osp_object *obj = dt2osp_obj(dt);
427
428         up_read(&obj->opo_sem);
429 }
430
431 /**
432  * Implementation of dt_object_operations::do_write_unlock
433  *
434  * Unlock the write lock of remote object.
435  *
436  * \param[in] env       execution environment
437  * \param[in] dt        object to be unlocked
438  */
439 static void osp_md_object_write_unlock(const struct lu_env *env,
440                                        struct dt_object *dt)
441 {
442         struct osp_object *obj = dt2osp_obj(dt);
443
444         LASSERT(obj->opo_owner == env);
445         obj->opo_owner = NULL;
446         up_write(&obj->opo_sem);
447 }
448
449 /**
450  * Implementation of dt_object_operations::do_write_locked
451  *
452  * Test if the object is locked in write mode.
453  *
454  * \param[in] env       execution environment
455  * \param[in] dt        object to be tested
456  */
457 static int osp_md_object_write_locked(const struct lu_env *env,
458                                       struct dt_object *dt)
459 {
460         struct osp_object *obj = dt2osp_obj(dt);
461
462         return obj->opo_owner == env;
463 }
464
465 /**
466  * Implementation of dt_index_operations::dio_lookup
467  *
468  * Look up record by key under a remote index object. It packs lookup update
469  * into RPC, sends to the remote OUT and waits for the lookup result.
470  *
471  * \param[in] env       execution environment
472  * \param[in] dt        index object to lookup
473  * \param[out] rec      record in which to return lookup result
474  * \param[in] key       key of index which will be looked up
475  *
476  * \retval              1 if the lookup succeeds.
477  * \retval              negative errno if the lookup fails.
478  */
479 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
480                                struct dt_rec *rec, const struct dt_key *key)
481 {
482         struct lu_buf           *lbuf   = &osp_env_info(env)->osi_lb2;
483         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
484         struct dt_device        *dt_dev = &osp->opd_dt_dev;
485         struct osp_update_request   *update;
486         struct object_update_reply *reply;
487         struct ptlrpc_request      *req = NULL;
488         struct lu_fid              *fid;
489         int                        rc;
490         ENTRY;
491
492         /* Because it needs send the update buffer right away,
493          * just create an update buffer, instead of attaching the
494          * update_remote list of the thandle.
495          */
496         update = osp_update_request_create(dt_dev);
497         if (IS_ERR(update))
498                 RETURN(PTR_ERR(update));
499
500         rc = osp_update_rpc_pack(env, index_lookup, update, OUT_INDEX_LOOKUP,
501                                  lu_object_fid(&dt->do_lu), rec, key);
502         if (rc != 0) {
503                 CERROR("%s: Insert update error: rc = %d\n",
504                        dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
505                 GOTO(out, rc);
506         }
507
508         rc = osp_remote_sync(env, osp, update, &req);
509         if (rc < 0)
510                 GOTO(out, rc);
511
512         reply = req_capsule_server_sized_get(&req->rq_pill,
513                                              &RMF_OUT_UPDATE_REPLY,
514                                              OUT_UPDATE_REPLY_SIZE);
515         if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
516                 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
517                        dt_dev->dd_lu_dev.ld_obd->obd_name,
518                        reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
519                 GOTO(out, rc = -EPROTO);
520         }
521
522         rc = object_update_result_data_get(reply, lbuf, 0);
523         if (rc < 0)
524                 GOTO(out, rc);
525
526         if (lbuf->lb_len != sizeof(*fid)) {
527                 CERROR("%s: lookup "DFID" %s wrong size %d\n",
528                        dt_dev->dd_lu_dev.ld_obd->obd_name,
529                        PFID(lu_object_fid(&dt->do_lu)), (char *)key,
530                        (int)lbuf->lb_len);
531                 GOTO(out, rc = -EINVAL);
532         }
533
534         fid = lbuf->lb_buf;
535         if (ptlrpc_rep_need_swab(req))
536                 lustre_swab_lu_fid(fid);
537         if (!fid_is_sane(fid)) {
538                 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
539                        dt_dev->dd_lu_dev.ld_obd->obd_name,
540                        PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
541                 GOTO(out, rc = -EINVAL);
542         }
543
544         memcpy(rec, fid, sizeof(*fid));
545
546         GOTO(out, rc = 1);
547
548 out:
549         if (req != NULL)
550                 ptlrpc_req_finished(req);
551
552         osp_update_request_destroy(update);
553
554         return rc;
555 }
556
557 /**
558  * Implementation of dt_index_operations::dio_declare_insert
559  *
560  * Create the osp_update_request to track the update for this OSP
561  * in the transaction.
562  *
563  * \param[in] env       execution environment
564  * \param[in] dt        object for which to insert index
565  * \param[in] rec       record of the index which will be inserted
566  * \param[in] key       key of the index which will be inserted
567  * \param[in] th        the transaction handle
568  *
569  * \retval              0 if preparation succeeds.
570  * \retval              negative errno if preparation fails.
571  */
572 static int osp_md_declare_index_insert(const struct lu_env *env,
573                                        struct dt_object *dt,
574                                        const struct dt_rec *rec,
575                                        const struct dt_key *key,
576                                        struct thandle *th)
577 {
578         return osp_trans_update_request_create(th);
579 }
580
581 /**
582  * Implementation of dt_index_operations::dio_insert
583  *
584  * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
585  * be flushed when the transaction stop.
586  *
587  * \param[in] env       execution environment
588  * \param[in] dt        object for which to insert index
589  * \param[in] rec       record of the index to be inserted
590  * \param[in] key       key of the index to be inserted
591  * \param[in] th        the transaction handle
592  * \param[in] ignore_quota quota enforcement for insert
593  *
594  * \retval              0 if packing index insert succeeds.
595  * \retval              negative errno if packing fails.
596  */
597 static int osp_md_index_insert(const struct lu_env *env,
598                                struct dt_object *dt,
599                                const struct dt_rec *rec,
600                                const struct dt_key *key,
601                                struct thandle *th,
602                                int ignore_quota)
603 {
604         struct osp_update_request *update;
605         int                      rc;
606
607         update = thandle_to_osp_update_request(th);
608         LASSERT(update != NULL);
609
610         rc = osp_update_rpc_pack(env, index_insert, update, OUT_INDEX_INSERT,
611                                  lu_object_fid(&dt->do_lu), rec, key);
612         return rc;
613 }
614
615 /**
616  * Implementation of dt_index_operations::dio_declare_delete
617  *
618  * Create the osp_update_request to track the update for this OSP
619  * in the transaction.
620  *
621  * \param[in] env       execution environment
622  * \param[in] dt        object for which to delete index
623  * \param[in] key       key of the index
624  * \param[in] th        the transaction handle
625  *
626  * \retval              0 if preparation succeeds.
627  * \retval              negative errno if preparation fails.
628  */
629 static int osp_md_declare_index_delete(const struct lu_env *env,
630                                        struct dt_object *dt,
631                                        const struct dt_key *key,
632                                        struct thandle *th)
633 {
634         return osp_trans_update_request_create(th);
635 }
636
637 /**
638  * Implementation of dt_index_operations::dio_delete
639  *
640  * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
641  * be flushed when the transaction stop.
642  *
643  * \param[in] env       execution environment
644  * \param[in] dt        object for which to delete index
645  * \param[in] key       key of the index which will be deleted
646  * \param[in] th        the transaction handle
647  *
648  * \retval              0 if packing index delete succeeds.
649  * \retval              negative errno if packing fails.
650  */
651 static int osp_md_index_delete(const struct lu_env *env,
652                                struct dt_object *dt,
653                                const struct dt_key *key,
654                                struct thandle *th)
655 {
656         struct osp_update_request *update;
657         int                      rc;
658
659         update = thandle_to_osp_update_request(th);
660         LASSERT(update != NULL);
661
662         rc = osp_update_rpc_pack(env, index_delete, update, OUT_INDEX_DELETE,
663                                  lu_object_fid(&dt->do_lu), key);
664
665         return rc;
666 }
667
668 /**
669  * Implementation of dt_index_operations::dio_it.next
670  *
671  * Advance the pointer of the iterator to the next entry. It shares a similar
672  * internal implementation with osp_orphan_it_next(), which is being used for
673  * remote orphan index object. This method will be used for remote directory.
674  *
675  * \param[in] env       execution environment
676  * \param[in] di        iterator of this iteration
677  *
678  * \retval              0 if the pointer is advanced successfully.
679  * \retval              1 if it reaches to the end of the index object.
680  * \retval              negative errno if the pointer cannot be advanced.
681  */
682 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
683 {
684         struct osp_it           *it = (struct osp_it *)di;
685         struct lu_idxpage       *idxpage;
686         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
687         int                     rc;
688         ENTRY;
689
690 again:
691         idxpage = it->ooi_cur_idxpage;
692         if (idxpage != NULL) {
693                 if (idxpage->lip_nr == 0)
694                         RETURN(1);
695
696                 it->ooi_pos_ent++;
697                 if (ent == NULL) {
698                         it->ooi_ent =
699                               (struct lu_dirent *)idxpage->lip_entries;
700                         RETURN(0);
701                 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
702                            it->ooi_pos_ent < idxpage->lip_nr) {
703                         ent = (struct lu_dirent *)(((char *)ent) +
704                                         le16_to_cpu(ent->lde_reclen));
705                         it->ooi_ent = ent;
706                         RETURN(0);
707                 } else {
708                         it->ooi_ent = NULL;
709                 }
710         }
711
712         rc = osp_it_next_page(env, di);
713         if (rc == 0)
714                 goto again;
715
716         RETURN(rc);
717 }
718
719 /**
720  * Implementation of dt_index_operations::dio_it.key
721  *
722  * Get the key at current iterator poisiton. These iteration methods
723  * (dio_it) will only be used for iterating the remote directory, so
724  * the key is the name of the directory entry.
725  *
726  * \param[in] env       execution environment
727  * \param[in] di        iterator of this iteration
728  *
729  * \retval              name of the current entry
730  */
731 static struct dt_key *osp_it_key(const struct lu_env *env,
732                                  const struct dt_it *di)
733 {
734         struct osp_it           *it = (struct osp_it *)di;
735         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
736
737         return (struct dt_key *)ent->lde_name;
738 }
739
740 /**
741  * Implementation of dt_index_operations::dio_it.key_size
742  *
743  * Get the key size at current iterator poisiton. These iteration methods
744  * (dio_it) will only be used for iterating the remote directory, so the key
745  * size is the name size of the directory entry.
746  *
747  * \param[in] env       execution environment
748  * \param[in] di        iterator of this iteration
749  *
750  * \retval              name size of the current entry
751  */
752
753 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
754 {
755         struct osp_it           *it = (struct osp_it *)di;
756         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
757
758         return (int)le16_to_cpu(ent->lde_namelen);
759 }
760
761 /**
762  * Implementation of dt_index_operations::dio_it.rec
763  *
764  * Get the record at current iterator position. These iteration methods
765  * (dio_it) will only be used for iterating the remote directory, so it
766  * uses lu_dirent_calc_size() to calculate the record size.
767  *
768  * \param[in] env       execution environment
769  * \param[in] di        iterator of this iteration
770  * \param[out] rec      the record to be returned
771  * \param[in] attr      attributes of the index object, so it knows
772  *                      how to pack the entry.
773  *
774  * \retval              only return 0 for now
775  */
776 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
777                                struct dt_rec *rec, __u32 attr)
778 {
779         struct osp_it           *it = (struct osp_it *)di;
780         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
781         size_t                  reclen;
782
783         reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
784         memcpy(rec, ent, reclen);
785         return 0;
786 }
787
788 /**
789  * Implementation of dt_index_operations::dio_it.load
790  *
791  * Locate the iteration cursor to the specified position (cookie).
792  *
793  * \param[in] env       pointer to the thread context
794  * \param[in] di        pointer to the iteration structure
795  * \param[in] hash      the specified position
796  *
797  * \retval              positive number for locating to the exactly position
798  *                      or the next
799  * \retval              0 for arriving at the end of the iteration
800  * \retval              negative error number on failure
801  */
802 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
803                        __u64 hash)
804 {
805         struct osp_it   *it     = (struct osp_it *)di;
806         int              rc;
807
808         it->ooi_next = hash;
809         rc = osp_md_index_it_next(env, (struct dt_it *)di);
810         if (rc == 1)
811                 return 0;
812
813         if (rc == 0)
814                 return 1;
815
816         return rc;
817 }
818
819 const struct dt_index_operations osp_md_index_ops = {
820         .dio_lookup         = osp_md_index_lookup,
821         .dio_declare_insert = osp_md_declare_index_insert,
822         .dio_insert         = osp_md_index_insert,
823         .dio_declare_delete = osp_md_declare_index_delete,
824         .dio_delete         = osp_md_index_delete,
825         .dio_it     = {
826                 .init     = osp_it_init,
827                 .fini     = osp_it_fini,
828                 .get      = osp_it_get,
829                 .put      = osp_it_put,
830                 .next     = osp_md_index_it_next,
831                 .key      = osp_it_key,
832                 .key_size = osp_it_key_size,
833                 .rec      = osp_md_index_it_rec,
834                 .store    = osp_it_store,
835                 .load     = osp_it_load,
836                 .key_rec  = osp_it_key_rec,
837         }
838 };
839
840 /**
841  * Implementation of dt_object_operations::do_index_try
842  *
843  * Try to initialize the index API pointer for the given object. This
844  * is the entry point of the index API, i.e. we must call this method
845  * to initialize the index object before calling other index methods.
846  *
847  * \param[in] env       execution environment
848  * \param[in] dt        index object to be initialized
849  * \param[in] feat      the index feature of the object
850  *
851  * \retval              0 if the initialization succeeds.
852  * \retval              negative errno if the initialization fails.
853  */
854 static int osp_md_index_try(const struct lu_env *env,
855                             struct dt_object *dt,
856                             const struct dt_index_features *feat)
857 {
858         dt->do_index_ops = &osp_md_index_ops;
859         return 0;
860 }
861
862 /**
863  * Implementation of dt_object_operations::do_object_lock
864  *
865  * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
866  * which will lock the object in the global namespace.
867  *
868  * \param[in] env       execution environment
869  * \param[in] dt        object to be locked
870  * \param[out] lh       lock handle
871  * \param[in] einfo     enqueue information
872  * \param[in] policy    lock policy
873  *
874  * \retval              ELDLM_OK if locking the object succeeds.
875  * \retval              negative errno if locking fails.
876  */
877 static int osp_md_object_lock(const struct lu_env *env,
878                               struct dt_object *dt,
879                               struct lustre_handle *lh,
880                               struct ldlm_enqueue_info *einfo,
881                               ldlm_policy_data_t *policy)
882 {
883         struct ldlm_res_id      *res_id;
884         struct dt_device        *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
885         struct osp_device       *osp = dt2osp_dev(dt_dev);
886         struct ptlrpc_request   *req;
887         int                     rc = 0;
888         __u64                   flags = 0;
889         ldlm_mode_t             mode;
890
891         res_id = einfo->ei_res_id;
892         LASSERT(res_id != NULL);
893
894         mode = ldlm_lock_match(osp->opd_obd->obd_namespace,
895                                LDLM_FL_BLOCK_GRANTED, res_id,
896                                einfo->ei_type, policy,
897                                einfo->ei_mode, lh, 0);
898         if (mode > 0)
899                 return ELDLM_OK;
900
901         req = ldlm_enqueue_pack(osp->opd_exp, 0);
902         if (IS_ERR(req))
903                 RETURN(PTR_ERR(req));
904
905         rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
906                               (const ldlm_policy_data_t *)policy,
907                               &flags, NULL, 0, LVB_T_NONE, lh, 0);
908
909         ptlrpc_req_finished(req);
910
911         return rc == ELDLM_OK ? 0 : -EIO;
912 }
913
914 /**
915  * Implementation of dt_object_operations::do_object_unlock
916  *
917  * Cancel a lock of a remote object.
918  *
919  * \param[in] env       execution environment
920  * \param[in] dt        object to be unlocked
921  * \param[in] einfo     lock enqueue information
922  * \param[in] policy    lock policy
923  *
924  * \retval              Only return 0 for now.
925  */
926 static int osp_md_object_unlock(const struct lu_env *env,
927                                 struct dt_object *dt,
928                                 struct ldlm_enqueue_info *einfo,
929                                 ldlm_policy_data_t *policy)
930 {
931         struct lustre_handle    *lockh = einfo->ei_cbdata;
932
933         /* unlock finally */
934         ldlm_lock_decref(lockh, einfo->ei_mode);
935
936         return 0;
937 }
938
939 /**
940  * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
941  *
942  * Create the dt_update_request to track the update for this OSP
943  * in the transaction.
944  *
945  * \param[in] env       pointer to the thread context
946  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
947  * \param[in] th        pointer to the transaction handler
948  *
949  * \retval              0 for success
950  * \retval              negative error number on failure
951  */
952 int osp_md_declare_object_destroy(const struct lu_env *env,
953                                struct dt_object *dt, struct thandle *th)
954 {
955         return osp_trans_update_request_create(th);
956 }
957
958 /**
959  * Interpreter call for object destroy
960  *
961  * Object destroy interpreter, which will be called after destroying
962  * the remote object to set flags and status.
963  *
964  * \param[in] env       execution environment
965  * \param[in] reply     update reply
966  * \param[in] req       ptlrpc update request for destroying object
967  * \param[in] obj       object to be destroyed
968  * \param[in] data      data used in this function.
969  * \param[in] index     index(position) of destroy update in the whole
970  *                      updates
971  * \param[in] rc        update result on the remote MDT.
972  *
973  * \retval              only return 0 for now
974  */
975 static int osp_md_object_destroy_interpreter(const struct lu_env *env,
976                                              struct object_update_reply *reply,
977                                              struct ptlrpc_request *req,
978                                              struct osp_object *obj,
979                                              void *data, int index, int rc)
980 {
981         /* not needed in cache any more */
982         set_bit(LU_OBJECT_HEARD_BANSHEE,
983                 &obj->opo_obj.do_lu.lo_header->loh_flags);
984         return 0;
985 }
986
987 /**
988  * Implement OSP layer dt_object_operations::do_destroy() interface.
989  *
990  * Pack the destroy update into the RPC buffer, which will be sent
991  * to the remote MDT during transaction stop.
992  *
993  * It also marks the object as non-cached.
994  *
995  * \param[in] env       pointer to the thread context
996  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
997  * \param[in] th        pointer to the transaction handler
998  *
999  * \retval              0 for success
1000  * \retval              negative error number on failure
1001  */
1002 int osp_md_object_destroy(const struct lu_env *env, struct dt_object *dt,
1003                           struct thandle *th)
1004 {
1005         struct osp_object               *o = dt2osp_obj(dt);
1006         struct osp_device               *osp = lu2osp_dev(dt->do_lu.lo_dev);
1007         struct osp_update_request       *update;
1008         int                             rc = 0;
1009
1010         ENTRY;
1011         o->opo_non_exist = 1;
1012
1013         LASSERT(osp->opd_connect_mdt);
1014         update = thandle_to_osp_update_request(th);
1015         LASSERT(update != NULL);
1016
1017         rc = osp_update_rpc_pack(env, object_destroy, update, OUT_DESTROY,
1018                                  lu_object_fid(&dt->do_lu));
1019         if (rc != 0)
1020                 RETURN(rc);
1021
1022         rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1023                                         osp_md_object_destroy_interpreter);
1024         RETURN(rc);
1025 }
1026
1027 struct dt_object_operations osp_md_obj_ops = {
1028         .do_read_lock         = osp_md_object_read_lock,
1029         .do_write_lock        = osp_md_object_write_lock,
1030         .do_read_unlock       = osp_md_object_read_unlock,
1031         .do_write_unlock      = osp_md_object_write_unlock,
1032         .do_write_locked      = osp_md_object_write_locked,
1033         .do_declare_create    = osp_md_declare_object_create,
1034         .do_create            = osp_md_object_create,
1035         .do_declare_ref_add   = osp_md_declare_ref_add,
1036         .do_ref_add           = osp_md_ref_add,
1037         .do_declare_ref_del   = osp_md_declare_ref_del,
1038         .do_ref_del           = osp_md_ref_del,
1039         .do_declare_destroy   = osp_md_declare_object_destroy,
1040         .do_destroy           = osp_md_object_destroy,
1041         .do_ah_init           = osp_md_ah_init,
1042         .do_attr_get          = osp_attr_get,
1043         .do_declare_attr_set  = osp_md_declare_attr_set,
1044         .do_attr_set          = osp_md_attr_set,
1045         .do_xattr_get         = osp_xattr_get,
1046         .do_declare_xattr_set = osp_declare_xattr_set,
1047         .do_xattr_set         = osp_xattr_set,
1048         .do_declare_xattr_del = osp_declare_xattr_del,
1049         .do_xattr_del         = osp_xattr_del,
1050         .do_index_try         = osp_md_index_try,
1051         .do_object_lock       = osp_md_object_lock,
1052         .do_object_unlock     = osp_md_object_unlock,
1053 };
1054
1055 /**
1056  * Implementation of dt_body_operations::dbo_declare_write
1057  *
1058  * Create the osp_update_request to track the update for this OSP
1059  * in the transaction.
1060   *
1061  * \param[in] env       execution environment
1062  * \param[in] dt        object to be written
1063  * \param[in] buf       buffer to write which includes an embedded size field
1064  * \param[in] pos       offet in the object to start writing at
1065  * \param[in] th        transaction handle
1066  *
1067  * \retval              0 if preparation succeeds.
1068  * \retval              negative errno if preparation fails.
1069  */
1070 static ssize_t osp_md_declare_write(const struct lu_env *env,
1071                                     struct dt_object *dt,
1072                                     const struct lu_buf *buf,
1073                                     loff_t pos, struct thandle *th)
1074 {
1075         return osp_trans_update_request_create(th);
1076 }
1077
1078 /**
1079  * Implementation of dt_body_operations::dbo_write
1080  *
1081  * Pack the write object update into the RPC buffer, which will be sent
1082  * to the remote MDT during transaction stop.
1083  *
1084  * \param[in] env       execution environment
1085  * \param[in] dt        object to be written
1086  * \param[in] buf       buffer to write which includes an embedded size field
1087  * \param[in] pos       offet in the object to start writing at
1088  * \param[in] th        transaction handle
1089  * \param[in] ignore_quota quota enforcement for this write
1090  *
1091  * \retval              the buffer size in bytes if packing succeeds.
1092  * \retval              negative errno if packing fails.
1093  */
1094 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1095                             const struct lu_buf *buf, loff_t *pos,
1096                             struct thandle *th, int ignore_quota)
1097 {
1098         struct osp_object         *obj = dt2osp_obj(dt);
1099         struct osp_update_request  *update;
1100         struct osp_thandle        *oth = thandle_to_osp_thandle(th);
1101         ssize_t                   rc;
1102         ENTRY;
1103
1104         update = thandle_to_osp_update_request(th);
1105         LASSERT(update != NULL);
1106
1107         rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1108                                  lu_object_fid(&dt->do_lu), buf, *pos);
1109         if (rc < 0)
1110                 RETURN(rc);
1111
1112         CDEBUG(D_INFO, "write "DFID" offset = "LPU64" length = %zu\n",
1113                PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1114
1115         /* XXX: how about the write error happened later? */
1116         *pos += buf->lb_len;
1117
1118         if (obj->opo_ooa != NULL &&
1119             obj->opo_ooa->ooa_attr.la_valid & LA_SIZE &&
1120             obj->opo_ooa->ooa_attr.la_size < *pos)
1121                 obj->opo_ooa->ooa_attr.la_size = *pos;
1122
1123         rc = osp_check_and_set_rpc_version(oth);
1124         if (rc < 0)
1125                 RETURN(rc);
1126
1127         RETURN(buf->lb_len);
1128 }
1129
1130 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1131                            struct lu_buf *rbuf, loff_t *pos)
1132 {
1133         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
1134         struct dt_device        *dt_dev = &osp->opd_dt_dev;
1135         struct lu_buf           *lbuf   = &osp_env_info(env)->osi_lb2;
1136         struct osp_update_request   *update;
1137         struct object_update_reply *reply;
1138         struct out_read_reply      *orr;
1139         struct ptlrpc_request      *req = NULL;
1140         int                        rc;
1141         ENTRY;
1142
1143         /* Because it needs send the update buffer right away,
1144          * just create an update buffer, instead of attaching the
1145          * update_remote list of the thandle.  */
1146         update = osp_update_request_create(dt_dev);
1147         if (IS_ERR(update))
1148                 RETURN(PTR_ERR(update));
1149
1150         rc = osp_update_rpc_pack(env, read, update, OUT_READ,
1151                                  lu_object_fid(&dt->do_lu), rbuf->lb_len, *pos);
1152         if (rc != 0) {
1153                 CERROR("%s: cannot insert update: rc = %d\n",
1154                        dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1155                 GOTO(out, rc);
1156         }
1157
1158         rc = osp_remote_sync(env, osp, update, &req);
1159         if (rc < 0)
1160                 GOTO(out, rc);
1161
1162         reply = req_capsule_server_sized_get(&req->rq_pill,
1163                                              &RMF_OUT_UPDATE_REPLY,
1164                                              OUT_UPDATE_REPLY_SIZE);
1165         if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1166                 CERROR("%s: invalid update reply magic %x expected %x:"
1167                        " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1168                        reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1169                 GOTO(out, rc = -EPROTO);
1170         }
1171
1172         rc = object_update_result_data_get(reply, lbuf, 0);
1173         if (rc < 0)
1174                 GOTO(out, rc);
1175
1176         if (lbuf->lb_len < sizeof(*orr))
1177                 GOTO(out, rc = -EPROTO);
1178
1179         orr = lbuf->lb_buf;
1180         orr_le_to_cpu(orr, orr);
1181
1182         *pos = orr->orr_offset;
1183
1184         if (orr->orr_size > rbuf->lb_len)
1185                 GOTO(out, rc = -EPROTO);
1186
1187         memcpy(rbuf->lb_buf, orr->orr_data, orr->orr_size);
1188
1189         CDEBUG(D_INFO, "%s: read "DFID" pos "LPU64" len %u\n",
1190                osp->opd_obd->obd_name, PFID(lu_object_fid(&dt->do_lu)),
1191                *pos, orr->orr_size);
1192         GOTO(out, rc = (int)orr->orr_size);
1193 out:
1194         if (req != NULL)
1195                 ptlrpc_req_finished(req);
1196
1197         osp_update_request_destroy(update);
1198
1199         return rc;
1200 }
1201
1202 /* These body operation will be used to write symlinks during migration etc */
1203 struct dt_body_operations osp_md_body_ops = {
1204         .dbo_declare_write      = osp_md_declare_write,
1205         .dbo_write              = osp_md_write,
1206         .dbo_read               = osp_md_read,
1207 };