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