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