Whamcloud - gitweb
LU-6741 osp: bulk transfer for osp_md_read
[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. And because the
867  * cross-MDT locks are relatively rare compared with normal local MDT operation,
868  * let's release it right away, instead of putting it into the LRU list.
869  *
870  * \param[in] env       execution environment
871  * \param[in] dt        object to be locked
872  * \param[out] lh       lock handle
873  * \param[in] einfo     enqueue information
874  * \param[in] policy    lock policy
875  *
876  * \retval              ELDLM_OK if locking the object succeeds.
877  * \retval              negative errno if locking fails.
878  */
879 static int osp_md_object_lock(const struct lu_env *env,
880                               struct dt_object *dt,
881                               struct lustre_handle *lh,
882                               struct ldlm_enqueue_info *einfo,
883                               union ldlm_policy_data *policy)
884 {
885         struct ldlm_res_id      *res_id;
886         struct dt_device        *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
887         struct osp_device       *osp = dt2osp_dev(dt_dev);
888         struct ptlrpc_request   *req;
889         int                     rc = 0;
890         __u64                   flags = 0;
891         enum ldlm_mode          mode;
892
893         res_id = einfo->ei_res_id;
894         LASSERT(res_id != NULL);
895
896         mode = ldlm_lock_match(osp->opd_obd->obd_namespace,
897                                LDLM_FL_BLOCK_GRANTED, res_id,
898                                einfo->ei_type, policy,
899                                einfo->ei_mode, lh, 0);
900         if (mode > 0)
901                 return ELDLM_OK;
902
903         if (einfo->ei_nonblock)
904                 flags |= LDLM_FL_BLOCK_NOWAIT;
905
906         req = ldlm_enqueue_pack(osp->opd_exp, 0);
907         if (IS_ERR(req))
908                 RETURN(PTR_ERR(req));
909
910         rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
911                               (const union ldlm_policy_data *)policy,
912                               &flags, NULL, 0, LVB_T_NONE, lh, 0);
913
914         ptlrpc_req_finished(req);
915         if (rc == ELDLM_OK) {
916                 struct ldlm_lock *lock;
917
918                 lock = __ldlm_handle2lock(lh, 0);
919                 ldlm_set_cbpending(lock);
920                 LDLM_LOCK_PUT(lock);
921         }
922
923         return rc == ELDLM_OK ? 0 : -EIO;
924 }
925
926 /**
927  * Implementation of dt_object_operations::do_object_unlock
928  *
929  * Cancel a lock of a remote object.
930  *
931  * \param[in] env       execution environment
932  * \param[in] dt        object to be unlocked
933  * \param[in] einfo     lock enqueue information
934  * \param[in] policy    lock policy
935  *
936  * \retval              Only return 0 for now.
937  */
938 static int osp_md_object_unlock(const struct lu_env *env,
939                                 struct dt_object *dt,
940                                 struct ldlm_enqueue_info *einfo,
941                                 union ldlm_policy_data *policy)
942 {
943         struct lustre_handle    *lockh = einfo->ei_cbdata;
944
945         /* unlock finally */
946         ldlm_lock_decref(lockh, einfo->ei_mode);
947
948         return 0;
949 }
950
951 /**
952  * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
953  *
954  * Create the dt_update_request to track the update for this OSP
955  * in the transaction.
956  *
957  * \param[in] env       pointer to the thread context
958  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
959  * \param[in] th        pointer to the transaction handler
960  *
961  * \retval              0 for success
962  * \retval              negative error number on failure
963  */
964 int osp_md_declare_object_destroy(const struct lu_env *env,
965                                struct dt_object *dt, struct thandle *th)
966 {
967         return osp_trans_update_request_create(th);
968 }
969
970 /**
971  * Implement OSP layer dt_object_operations::do_destroy() interface.
972  *
973  * Pack the destroy update into the RPC buffer, which will be sent
974  * to the remote MDT during transaction stop.
975  *
976  * It also marks the object as non-cached.
977  *
978  * \param[in] env       pointer to the thread context
979  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
980  * \param[in] th        pointer to the transaction handler
981  *
982  * \retval              0 for success
983  * \retval              negative error number on failure
984  */
985 int osp_md_object_destroy(const struct lu_env *env, struct dt_object *dt,
986                           struct thandle *th)
987 {
988         struct osp_object               *o = dt2osp_obj(dt);
989         struct osp_device               *osp = lu2osp_dev(dt->do_lu.lo_dev);
990         struct osp_update_request       *update;
991         int                             rc = 0;
992
993         ENTRY;
994         o->opo_non_exist = 1;
995
996         LASSERT(osp->opd_connect_mdt);
997         update = thandle_to_osp_update_request(th);
998         LASSERT(update != NULL);
999
1000         rc = osp_update_rpc_pack(env, object_destroy, update, OUT_DESTROY,
1001                                  lu_object_fid(&dt->do_lu));
1002         if (rc != 0)
1003                 RETURN(rc);
1004
1005         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1006         rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1007                                         NULL);
1008
1009         RETURN(rc);
1010 }
1011
1012 struct dt_object_operations osp_md_obj_ops = {
1013         .do_read_lock         = osp_md_object_read_lock,
1014         .do_write_lock        = osp_md_object_write_lock,
1015         .do_read_unlock       = osp_md_object_read_unlock,
1016         .do_write_unlock      = osp_md_object_write_unlock,
1017         .do_write_locked      = osp_md_object_write_locked,
1018         .do_declare_create    = osp_md_declare_object_create,
1019         .do_create            = osp_md_object_create,
1020         .do_declare_ref_add   = osp_md_declare_ref_add,
1021         .do_ref_add           = osp_md_ref_add,
1022         .do_declare_ref_del   = osp_md_declare_ref_del,
1023         .do_ref_del           = osp_md_ref_del,
1024         .do_declare_destroy   = osp_md_declare_object_destroy,
1025         .do_destroy           = osp_md_object_destroy,
1026         .do_ah_init           = osp_md_ah_init,
1027         .do_attr_get          = osp_attr_get,
1028         .do_declare_attr_set  = osp_md_declare_attr_set,
1029         .do_attr_set          = osp_md_attr_set,
1030         .do_xattr_get         = osp_xattr_get,
1031         .do_declare_xattr_set = osp_declare_xattr_set,
1032         .do_xattr_set         = osp_xattr_set,
1033         .do_declare_xattr_del = osp_declare_xattr_del,
1034         .do_xattr_del         = osp_xattr_del,
1035         .do_index_try         = osp_md_index_try,
1036         .do_object_lock       = osp_md_object_lock,
1037         .do_object_unlock     = osp_md_object_unlock,
1038 };
1039
1040 /**
1041  * Implementation of dt_body_operations::dbo_declare_write
1042  *
1043  * Create the osp_update_request to track the update for this OSP
1044  * in the transaction.
1045   *
1046  * \param[in] env       execution environment
1047  * \param[in] dt        object to be written
1048  * \param[in] buf       buffer to write which includes an embedded size field
1049  * \param[in] pos       offet in the object to start writing at
1050  * \param[in] th        transaction handle
1051  *
1052  * \retval              0 if preparation succeeds.
1053  * \retval              negative errno if preparation fails.
1054  */
1055 static ssize_t osp_md_declare_write(const struct lu_env *env,
1056                                     struct dt_object *dt,
1057                                     const struct lu_buf *buf,
1058                                     loff_t pos, struct thandle *th)
1059 {
1060         return osp_trans_update_request_create(th);
1061 }
1062
1063 /**
1064  * Implementation of dt_body_operations::dbo_write
1065  *
1066  * Pack the write object update into the RPC buffer, which will be sent
1067  * to the remote MDT during transaction stop.
1068  *
1069  * \param[in] env       execution environment
1070  * \param[in] dt        object to be written
1071  * \param[in] buf       buffer to write which includes an embedded size field
1072  * \param[in] pos       offet in the object to start writing at
1073  * \param[in] th        transaction handle
1074  * \param[in] ignore_quota quota enforcement for this write
1075  *
1076  * \retval              the buffer size in bytes if packing succeeds.
1077  * \retval              negative errno if packing fails.
1078  */
1079 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1080                             const struct lu_buf *buf, loff_t *pos,
1081                             struct thandle *th, int ignore_quota)
1082 {
1083         struct osp_object         *obj = dt2osp_obj(dt);
1084         struct osp_update_request  *update;
1085         struct osp_thandle        *oth = thandle_to_osp_thandle(th);
1086         ssize_t                   rc;
1087         ENTRY;
1088
1089         update = thandle_to_osp_update_request(th);
1090         LASSERT(update != NULL);
1091
1092         rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1093                                  lu_object_fid(&dt->do_lu), buf, *pos);
1094         if (rc < 0)
1095                 RETURN(rc);
1096
1097         CDEBUG(D_INFO, "write "DFID" offset = "LPU64" length = %zu\n",
1098                PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1099
1100         /* XXX: how about the write error happened later? */
1101         *pos += buf->lb_len;
1102
1103         if (obj->opo_ooa != NULL &&
1104             obj->opo_ooa->ooa_attr.la_valid & LA_SIZE &&
1105             obj->opo_ooa->ooa_attr.la_size < *pos)
1106                 obj->opo_ooa->ooa_attr.la_size = *pos;
1107
1108         rc = osp_check_and_set_rpc_version(oth);
1109         if (rc < 0)
1110                 RETURN(rc);
1111
1112         RETURN(buf->lb_len);
1113 }
1114
1115 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1116                            struct lu_buf *rbuf, loff_t *pos)
1117 {
1118         struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1119         struct dt_device *dt_dev        = &osp->opd_dt_dev;
1120         struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1121         char *ptr = rbuf->lb_buf;
1122         struct osp_update_request *update = NULL;
1123         struct ptlrpc_request *req = NULL;
1124         struct out_read_reply *orr;
1125         struct ptlrpc_bulk_desc *desc;
1126         struct object_update_reply *reply;
1127         __u32 left_size;
1128         int nbufs;
1129         int i;
1130         int rc;
1131         ENTRY;
1132
1133         /* Because it needs send the update buffer right away,
1134          * just create an update buffer, instead of attaching the
1135          * update_remote list of the thandle.  */
1136         update = osp_update_request_create(dt_dev);
1137         if (IS_ERR(update))
1138                 GOTO(out, rc = PTR_ERR(update));
1139
1140         rc = osp_update_rpc_pack(env, read, update, OUT_READ,
1141                                  lu_object_fid(&dt->do_lu),
1142                                  rbuf->lb_len, *pos);
1143         if (rc != 0) {
1144                 CERROR("%s: cannot insert update: rc = %d\n",
1145                        dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1146                 GOTO(out, rc);
1147         }
1148
1149         rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1150                                  &req);
1151         if (rc != 0)
1152                 GOTO(out, rc);
1153
1154         nbufs = (rbuf->lb_len + OUT_BULK_BUFFER_SIZE - 1) /
1155                                         OUT_BULK_BUFFER_SIZE;
1156         /* allocate bulk descriptor */
1157         desc = ptlrpc_prep_bulk_imp(req, nbufs, 1,
1158                                     PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KVEC,
1159                                     MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
1160         if (desc == NULL)
1161                 GOTO(out, rc = -ENOMEM);
1162
1163         /* split the buffer into small chunk size */
1164         left_size = rbuf->lb_len;
1165         for (i = 0; i < nbufs; i++) {
1166                 int read_size;
1167
1168                 read_size = left_size > OUT_BULK_BUFFER_SIZE ?
1169                                 OUT_BULK_BUFFER_SIZE : left_size;
1170                 desc->bd_frag_ops->add_iov_frag(desc, ptr, read_size);
1171
1172                 ptr += read_size;
1173         }
1174
1175         /* This will only be called with read-only update, and these updates
1176          * might be used to retrieve update log during recovery process, so
1177          * it will be allowed to send during recovery process */
1178         req->rq_allow_replay = 1;
1179         req->rq_bulk_read = 1;
1180         /* send request to master and wait for RPC to complete */
1181         rc = ptlrpc_queue_wait(req);
1182         if (rc != 0)
1183                 GOTO(out, rc);
1184
1185         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1186                                           req->rq_bulk->bd_nob_transferred);
1187         if (rc < 0)
1188                 GOTO(out, rc);
1189
1190         reply = req_capsule_server_sized_get(&req->rq_pill,
1191                                              &RMF_OUT_UPDATE_REPLY,
1192                                              OUT_UPDATE_REPLY_SIZE);
1193
1194         if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1195                 CERROR("%s: invalid update reply magic %x expected %x:"
1196                        " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1197                        reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1198                 GOTO(out, rc = -EPROTO);
1199         }
1200
1201         rc = object_update_result_data_get(reply, lbuf, 0);
1202         if (rc < 0)
1203                 GOTO(out, rc);
1204
1205         if (lbuf->lb_len < sizeof(*orr))
1206                 GOTO(out, rc = -EPROTO);
1207
1208         orr = lbuf->lb_buf;
1209         orr_le_to_cpu(orr, orr);
1210         rc = orr->orr_size;
1211         *pos = orr->orr_offset;
1212 out:
1213         if (req != NULL)
1214                 ptlrpc_req_finished(req);
1215
1216         if (update != NULL)
1217                 osp_update_request_destroy(update);
1218
1219         RETURN(rc);
1220 }
1221
1222 /* These body operation will be used to write symlinks during migration etc */
1223 struct dt_body_operations osp_md_body_ops = {
1224         .dbo_declare_write      = osp_md_declare_write,
1225         .dbo_write              = osp_md_write,
1226         .dbo_read               = osp_md_read,
1227 };