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