Whamcloud - gitweb
LU-3536 lod: cancel update log after all committed
[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 dt_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 update_buffer *ubuf)
158 {
159         struct object_update_request *ureq;
160         size_t  new_size = ubuf->ub_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(ureq, new_size);
167         if (ureq == NULL)
168                 return -ENOMEM;
169
170         memcpy(ureq, ubuf->ub_req, ubuf->ub_req_size);
171
172         OBD_FREE_LARGE(ubuf->ub_req, ubuf->ub_req_size);
173
174         ubuf->ub_req = ureq;
175         ubuf->ub_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 dt_update_request        *update;
201         struct osp_object               *obj = dt2osp_obj(dt);
202         int                             rc;
203
204         update = thandle_to_dt_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 dt_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 dt_update_request        *update;
264         int                             rc;
265
266         update = thandle_to_dt_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 dt_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 dt_update_request        *update;
310         int                             rc;
311
312         update = thandle_to_dt_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 dt_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 dt_update_request        *update;
386         int                             rc;
387
388         update = thandle_to_dt_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 dt_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 = dt_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         dt_update_request_destroy(update);
577
578         return rc;
579 }
580
581 /**
582  * Implementation of dt_index_operations::dio_declare_insert
583  *
584  * Create the dt_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_thandle       *oth = thandle_to_osp_thandle(th);
629         struct dt_update_request *update = oth->ot_dur;
630         int                      rc;
631
632         rc = osp_update_rpc_pack(env, index_insert, update, OUT_INDEX_INSERT,
633                                  lu_object_fid(&dt->do_lu), rec, key);
634         return rc;
635 }
636
637 /**
638  * Implementation of dt_index_operations::dio_declare_delete
639  *
640  * Create the dt_update_request to track the update for this OSP
641  * in the transaction.
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
646  * \param[in] th        the transaction handle
647  *
648  * \retval              0 if preparation succeeds.
649  * \retval              negative errno if preparation fails.
650  */
651 static int osp_md_declare_index_delete(const struct lu_env *env,
652                                        struct dt_object *dt,
653                                        const struct dt_key *key,
654                                        struct thandle *th)
655 {
656         return osp_trans_update_request_create(th);
657 }
658
659 /**
660  * Implementation of dt_index_operations::dio_delete
661  *
662  * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
663  * be flushed when the transaction stop.
664  *
665  * \param[in] env       execution environment
666  * \param[in] dt        object for which to delete index
667  * \param[in] key       key of the index which will be deleted
668  * \param[in] th        the transaction handle
669  *
670  * \retval              0 if packing index delete succeeds.
671  * \retval              negative errno if packing fails.
672  */
673 static int osp_md_index_delete(const struct lu_env *env,
674                                struct dt_object *dt,
675                                const struct dt_key *key,
676                                struct thandle *th)
677 {
678         struct dt_update_request *update;
679         int                      rc;
680
681         update = thandle_to_dt_update_request(th);
682         LASSERT(update != NULL);
683
684         rc = osp_update_rpc_pack(env, index_delete, update, OUT_INDEX_DELETE,
685                                  lu_object_fid(&dt->do_lu), key);
686
687         return rc;
688 }
689
690 /**
691  * Implementation of dt_index_operations::dio_it.next
692  *
693  * Advance the pointer of the iterator to the next entry. It shares a similar
694  * internal implementation with osp_orphan_it_next(), which is being used for
695  * remote orphan index object. This method will be used for remote directory.
696  *
697  * \param[in] env       execution environment
698  * \param[in] di        iterator of this iteration
699  *
700  * \retval              0 if the pointer is advanced successfuly.
701  * \retval              1 if it reaches to the end of the index object.
702  * \retval              negative errno if the pointer cannot be advanced.
703  */
704 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
705 {
706         struct osp_it           *it = (struct osp_it *)di;
707         struct lu_idxpage       *idxpage;
708         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
709         int                     rc;
710         ENTRY;
711
712 again:
713         idxpage = it->ooi_cur_idxpage;
714         if (idxpage != NULL) {
715                 if (idxpage->lip_nr == 0)
716                         RETURN(1);
717
718                 it->ooi_pos_ent++;
719                 if (ent == NULL) {
720                         it->ooi_ent =
721                               (struct lu_dirent *)idxpage->lip_entries;
722                         RETURN(0);
723                 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
724                            it->ooi_pos_ent < idxpage->lip_nr) {
725                         ent = (struct lu_dirent *)(((char *)ent) +
726                                         le16_to_cpu(ent->lde_reclen));
727                         it->ooi_ent = ent;
728                         RETURN(0);
729                 } else {
730                         it->ooi_ent = NULL;
731                 }
732         }
733
734         rc = osp_it_next_page(env, di);
735         if (rc == 0)
736                 goto again;
737
738         RETURN(rc);
739 }
740
741 /**
742  * Implementation of dt_index_operations::dio_it.key
743  *
744  * Get the key at current iterator poisiton. These iteration methods
745  * (dio_it) will only be used for iterating the remote directory, so
746  * the key is the name of the directory entry.
747  *
748  * \param[in] env       execution environment
749  * \param[in] di        iterator of this iteration
750  *
751  * \retval              name of the current entry
752  */
753 static struct dt_key *osp_it_key(const struct lu_env *env,
754                                  const struct dt_it *di)
755 {
756         struct osp_it           *it = (struct osp_it *)di;
757         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
758
759         return (struct dt_key *)ent->lde_name;
760 }
761
762 /**
763  * Implementation of dt_index_operations::dio_it.key_size
764  *
765  * Get the key size at current iterator poisiton. These iteration methods
766  * (dio_it) will only be used for iterating the remote directory, so the key
767  * size is the name size of the directory entry.
768  *
769  * \param[in] env       execution environment
770  * \param[in] di        iterator of this iteration
771  *
772  * \retval              name size of the current entry
773  */
774
775 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
776 {
777         struct osp_it           *it = (struct osp_it *)di;
778         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
779
780         return (int)le16_to_cpu(ent->lde_namelen);
781 }
782
783 /**
784  * Implementation of dt_index_operations::dio_it.rec
785  *
786  * Get the record at current iterator position. These iteration methods
787  * (dio_it) will only be used for iterating the remote directory, so it
788  * uses lu_dirent_calc_size() to calculate the record size.
789  *
790  * \param[in] env       execution environment
791  * \param[in] di        iterator of this iteration
792  * \param[out] rec      the record to be returned
793  * \param[in] attr      attributes of the index object, so it knows
794  *                      how to pack the entry.
795  *
796  * \retval              only return 0 for now
797  */
798 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
799                                struct dt_rec *rec, __u32 attr)
800 {
801         struct osp_it           *it = (struct osp_it *)di;
802         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
803         size_t                  reclen;
804
805         reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
806         memcpy(rec, ent, reclen);
807         return 0;
808 }
809
810 /**
811  * Implementation of dt_index_operations::dio_it.load
812  *
813  * Locate the iteration cursor to the specified position (cookie).
814  *
815  * \param[in] env       pointer to the thread context
816  * \param[in] di        pointer to the iteration structure
817  * \param[in] hash      the specified position
818  *
819  * \retval              positive number for locating to the exactly position
820  *                      or the next
821  * \retval              0 for arriving at the end of the iteration
822  * \retval              negative error number on failure
823  */
824 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
825                        __u64 hash)
826 {
827         struct osp_it   *it     = (struct osp_it *)di;
828         int              rc;
829
830         it->ooi_next = hash;
831         rc = osp_md_index_it_next(env, (struct dt_it *)di);
832         if (rc == 1)
833                 return 0;
834
835         if (rc == 0)
836                 return 1;
837
838         return rc;
839 }
840
841 const struct dt_index_operations osp_md_index_ops = {
842         .dio_lookup         = osp_md_index_lookup,
843         .dio_declare_insert = osp_md_declare_index_insert,
844         .dio_insert         = osp_md_index_insert,
845         .dio_declare_delete = osp_md_declare_index_delete,
846         .dio_delete         = osp_md_index_delete,
847         .dio_it     = {
848                 .init     = osp_it_init,
849                 .fini     = osp_it_fini,
850                 .get      = osp_it_get,
851                 .put      = osp_it_put,
852                 .next     = osp_md_index_it_next,
853                 .key      = osp_it_key,
854                 .key_size = osp_it_key_size,
855                 .rec      = osp_md_index_it_rec,
856                 .store    = osp_it_store,
857                 .load     = osp_it_load,
858                 .key_rec  = osp_it_key_rec,
859         }
860 };
861
862 /**
863  * Implementation of dt_object_operations::do_index_try
864  *
865  * Try to initialize the index API pointer for the given object. This
866  * is the entry point of the index API, i.e. we must call this method
867  * to initialize the index object before calling other index methods.
868  *
869  * \param[in] env       execution environment
870  * \param[in] dt        index object to be initialized
871  * \param[in] feat      the index feature of the object
872  *
873  * \retval              0 if the initialization succeeds.
874  * \retval              negative errno if the initialization fails.
875  */
876 static int osp_md_index_try(const struct lu_env *env,
877                             struct dt_object *dt,
878                             const struct dt_index_features *feat)
879 {
880         dt->do_index_ops = &osp_md_index_ops;
881         return 0;
882 }
883
884 /**
885  * Implementation of dt_object_operations::do_object_lock
886  *
887  * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
888  * which will lock the object in the global namespace.
889  *
890  * \param[in] env       execution environment
891  * \param[in] dt        object to be locked
892  * \param[out] lh       lock handle
893  * \param[in] einfo     enqueue information
894  * \param[in] policy    lock policy
895  *
896  * \retval              ELDLM_OK if locking the object succeeds.
897  * \retval              negative errno if locking fails.
898  */
899 static int osp_md_object_lock(const struct lu_env *env,
900                               struct dt_object *dt,
901                               struct lustre_handle *lh,
902                               struct ldlm_enqueue_info *einfo,
903                               ldlm_policy_data_t *policy)
904 {
905         struct ldlm_res_id      *res_id;
906         struct dt_device        *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
907         struct osp_device       *osp = dt2osp_dev(dt_dev);
908         struct ptlrpc_request   *req;
909         int                     rc = 0;
910         __u64                   flags = 0;
911         ldlm_mode_t             mode;
912
913         res_id = einfo->ei_res_id;
914         LASSERT(res_id != NULL);
915
916         mode = ldlm_lock_match(osp->opd_obd->obd_namespace,
917                                LDLM_FL_BLOCK_GRANTED, res_id,
918                                einfo->ei_type, policy,
919                                einfo->ei_mode, lh, 0);
920         if (mode > 0)
921                 return ELDLM_OK;
922
923         req = ldlm_enqueue_pack(osp->opd_exp, 0);
924         if (IS_ERR(req))
925                 RETURN(PTR_ERR(req));
926
927         rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
928                               (const ldlm_policy_data_t *)policy,
929                               &flags, NULL, 0, LVB_T_NONE, lh, 0);
930
931         ptlrpc_req_finished(req);
932
933         return rc == ELDLM_OK ? 0 : -EIO;
934 }
935
936 /**
937  * Implementation of dt_object_operations::do_object_unlock
938  *
939  * Cancel a lock of a remote object.
940  *
941  * \param[in] env       execution environment
942  * \param[in] dt        object to be unlocked
943  * \param[in] einfo     lock enqueue information
944  * \param[in] policy    lock policy
945  *
946  * \retval              Only return 0 for now.
947  */
948 static int osp_md_object_unlock(const struct lu_env *env,
949                                 struct dt_object *dt,
950                                 struct ldlm_enqueue_info *einfo,
951                                 ldlm_policy_data_t *policy)
952 {
953         struct lustre_handle    *lockh = einfo->ei_cbdata;
954
955         /* unlock finally */
956         ldlm_lock_decref(lockh, einfo->ei_mode);
957
958         return 0;
959 }
960
961 /**
962  * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
963  *
964  * Create the dt_update_request to track the update for this OSP
965  * in the transaction.
966  *
967  * \param[in] env       pointer to the thread context
968  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
969  * \param[in] th        pointer to the transaction handler
970  *
971  * \retval              0 for success
972  * \retval              negative error number on failure
973  */
974 int osp_md_declare_object_destroy(const struct lu_env *env,
975                                struct dt_object *dt, struct thandle *th)
976 {
977         return osp_trans_update_request_create(th);
978 }
979
980 /**
981  * Interpreter call for object destroy
982  *
983  * Object destroy interpreter, which will be called after destroying
984  * the remote object to set flags and status.
985  *
986  * \param[in] env       execution environment
987  * \param[in] reply     update reply
988  * \param[in] req       ptlrpc update request for destroying object
989  * \param[in] obj       object to be destroyed
990  * \param[in] data      data used in this function.
991  * \param[in] index     index(position) of destroy update in the whole
992  *                      updates
993  * \param[in] rc        update result on the remote MDT.
994  *
995  * \retval              only return 0 for now
996  */
997 static int osp_md_object_destroy_interpreter(const struct lu_env *env,
998                                              struct object_update_reply *reply,
999                                              struct ptlrpc_request *req,
1000                                              struct osp_object *obj,
1001                                              void *data, int index, int rc)
1002 {
1003         /* not needed in cache any more */
1004         set_bit(LU_OBJECT_HEARD_BANSHEE,
1005                 &obj->opo_obj.do_lu.lo_header->loh_flags);
1006         return 0;
1007 }
1008
1009 /**
1010  * Implement OSP layer dt_object_operations::do_destroy() interface.
1011  *
1012  * Pack the destroy update into the RPC buffer, which will be sent
1013  * to the remote MDT during transaction stop.
1014  *
1015  * It also marks the object as non-cached.
1016  *
1017  * \param[in] env       pointer to the thread context
1018  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
1019  * \param[in] th        pointer to the transaction handler
1020  *
1021  * \retval              0 for success
1022  * \retval              negative error number on failure
1023  */
1024 int osp_md_object_destroy(const struct lu_env *env, struct dt_object *dt,
1025                           struct thandle *th)
1026 {
1027         struct osp_object               *o = dt2osp_obj(dt);
1028         struct osp_device               *osp = lu2osp_dev(dt->do_lu.lo_dev);
1029         struct dt_update_request        *update;
1030         int                             rc = 0;
1031
1032         ENTRY;
1033         o->opo_non_exist = 1;
1034
1035         LASSERT(osp->opd_connect_mdt);
1036         update = thandle_to_dt_update_request(th);
1037         LASSERT(update != NULL);
1038
1039         rc = osp_update_rpc_pack(env, object_destroy, update, OUT_DESTROY,
1040                                  lu_object_fid(&dt->do_lu));
1041         if (rc != 0)
1042                 RETURN(rc);
1043
1044         rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1045                                         osp_md_object_destroy_interpreter);
1046         RETURN(rc);
1047 }
1048
1049 struct dt_object_operations osp_md_obj_ops = {
1050         .do_read_lock         = osp_md_object_read_lock,
1051         .do_write_lock        = osp_md_object_write_lock,
1052         .do_read_unlock       = osp_md_object_read_unlock,
1053         .do_write_unlock      = osp_md_object_write_unlock,
1054         .do_write_locked      = osp_md_object_write_locked,
1055         .do_declare_create    = osp_md_declare_object_create,
1056         .do_create            = osp_md_object_create,
1057         .do_declare_ref_add   = osp_md_declare_ref_add,
1058         .do_ref_add           = osp_md_ref_add,
1059         .do_declare_ref_del   = osp_md_declare_ref_del,
1060         .do_ref_del           = osp_md_ref_del,
1061         .do_declare_destroy   = osp_md_declare_object_destroy,
1062         .do_destroy           = osp_md_object_destroy,
1063         .do_ah_init           = osp_md_ah_init,
1064         .do_attr_get          = osp_attr_get,
1065         .do_declare_attr_set  = osp_md_declare_attr_set,
1066         .do_attr_set          = osp_md_attr_set,
1067         .do_xattr_get         = osp_xattr_get,
1068         .do_declare_xattr_set = osp_declare_xattr_set,
1069         .do_xattr_set         = osp_xattr_set,
1070         .do_declare_xattr_del = osp_declare_xattr_del,
1071         .do_xattr_del         = osp_xattr_del,
1072         .do_index_try         = osp_md_index_try,
1073         .do_object_lock       = osp_md_object_lock,
1074         .do_object_unlock     = osp_md_object_unlock,
1075 };
1076
1077 /**
1078  * Implementation of dt_body_operations::dbo_declare_write
1079  *
1080  * Create the dt_update_request to track the update for this OSP
1081  * in the transaction.
1082   *
1083  * \param[in] env       execution environment
1084  * \param[in] dt        object to be written
1085  * \param[in] buf       buffer to write which includes an embedded size field
1086  * \param[in] pos       offet in the object to start writing at
1087  * \param[in] th        transaction handle
1088  *
1089  * \retval              0 if preparation succeeds.
1090  * \retval              negative errno if preparation fails.
1091  */
1092 static ssize_t osp_md_declare_write(const struct lu_env *env,
1093                                     struct dt_object *dt,
1094                                     const struct lu_buf *buf,
1095                                     loff_t pos, struct thandle *th)
1096 {
1097         return osp_trans_update_request_create(th);
1098 }
1099
1100 /**
1101  * Implementation of dt_body_operations::dbo_write
1102  *
1103  * Pack the write object update into the RPC buffer, which will be sent
1104  * to the remote MDT during transaction stop.
1105  *
1106  * \param[in] env       execution environment
1107  * \param[in] dt        object to be written
1108  * \param[in] buf       buffer to write which includes an embedded size field
1109  * \param[in] pos       offet in the object to start writing at
1110  * \param[in] th        transaction handle
1111  * \param[in] ignore_quota quota enforcement for this write
1112  *
1113  * \retval              the buffer size in bytes if packing succeeds.
1114  * \retval              negative errno if packing fails.
1115  */
1116 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1117                             const struct lu_buf *buf, loff_t *pos,
1118                             struct thandle *th, int ignore_quota)
1119 {
1120         struct osp_object *obj = dt2osp_obj(dt);
1121         struct dt_update_request  *update;
1122         ssize_t                   rc;
1123         ENTRY;
1124
1125         update = thandle_to_dt_update_request(th);
1126         LASSERT(update != NULL);
1127
1128         rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1129                                  lu_object_fid(&dt->do_lu), buf, *pos);
1130         if (rc < 0)
1131                 RETURN(rc);
1132
1133         CDEBUG(D_INFO, "write "DFID" offset = "LPU64" length = %zu\n",
1134                PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1135
1136         /* XXX: how about the write error happened later? */
1137         *pos += buf->lb_len;
1138
1139         if (obj->opo_ooa != NULL &&
1140             obj->opo_ooa->ooa_attr.la_valid & LA_SIZE &&
1141             obj->opo_ooa->ooa_attr.la_size < *pos)
1142                 obj->opo_ooa->ooa_attr.la_size = *pos;
1143
1144         RETURN(buf->lb_len);
1145 }
1146
1147 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1148                            struct lu_buf *rbuf, loff_t *pos)
1149 {
1150         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
1151         struct dt_device        *dt_dev = &osp->opd_dt_dev;
1152         struct lu_buf           *lbuf   = &osp_env_info(env)->osi_lb2;
1153         struct dt_update_request   *update;
1154         struct object_update_reply *reply;
1155         struct out_read_reply      *orr;
1156         struct ptlrpc_request      *req = NULL;
1157         int                        rc;
1158         ENTRY;
1159
1160         /* Because it needs send the update buffer right away,
1161          * just create an update buffer, instead of attaching the
1162          * update_remote list of the thandle. */
1163         update = dt_update_request_create(dt_dev);
1164         if (IS_ERR(update))
1165                 RETURN(PTR_ERR(update));
1166
1167         rc = osp_update_rpc_pack(env, read, update, OUT_READ,
1168                                  lu_object_fid(&dt->do_lu), rbuf->lb_len, *pos);
1169         if (rc != 0) {
1170                 CERROR("%s: cannot insert update: rc = %d\n",
1171                        dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1172                 GOTO(out, rc);
1173         }
1174
1175         rc = osp_remote_sync(env, osp, update, &req);
1176         if (rc < 0)
1177                 GOTO(out, rc);
1178
1179         reply = req_capsule_server_sized_get(&req->rq_pill,
1180                                              &RMF_OUT_UPDATE_REPLY,
1181                                              OUT_UPDATE_REPLY_SIZE);
1182         if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1183                 CERROR("%s: invalid update reply magic %x expected %x:"
1184                        " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1185                        reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1186                 GOTO(out, rc = -EPROTO);
1187         }
1188
1189         rc = object_update_result_data_get(reply, lbuf, 0);
1190         if (rc < 0)
1191                 GOTO(out, rc);
1192
1193         if (lbuf->lb_len < sizeof(*orr))
1194                 GOTO(out, rc = -EPROTO);
1195
1196         orr = lbuf->lb_buf;
1197         orr_le_to_cpu(orr, orr);
1198
1199         *pos = orr->orr_offset;
1200
1201         if (orr->orr_size > rbuf->lb_len)
1202                 GOTO(out, rc = -EPROTO);
1203
1204         memcpy(rbuf->lb_buf, orr->orr_data, orr->orr_size);
1205
1206         CDEBUG(D_INFO, "%s: read "DFID" pos "LPU64" len %u\n",
1207                osp->opd_obd->obd_name, PFID(lu_object_fid(&dt->do_lu)),
1208                *pos, orr->orr_size);
1209         GOTO(out, rc = (int)orr->orr_size);
1210 out:
1211         if (req != NULL)
1212                 ptlrpc_req_finished(req);
1213
1214         dt_update_request_destroy(update);
1215
1216         return rc;
1217 }
1218
1219 /* These body operation will be used to write symlinks during migration etc */
1220 struct dt_body_operations osp_md_body_ops = {
1221         .dbo_declare_write      = osp_md_declare_write,
1222         .dbo_write              = osp_md_write,
1223         .dbo_read               = osp_md_read,
1224 };