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