Whamcloud - gitweb
LU-3536 lod: record update for cross-MDT operation
[fs/lustre-release.git] / lustre / osp / osp_md_object.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2014, Intel Corporation.
24  */
25 /*
26  * lustre/osp/osp_md_object.c
27  *
28  * OST/MDT proxy device (OSP) Metadata methods
29  *
30  * This file implements methods for remote MD object, which include
31  * dt_object_operations, dt_index_operations and dt_body_operations.
32  *
33  * If there are multiple MDTs in one filesystem, one operation might
34  * include modifications in several MDTs. In such cases, clients
35  * send the RPC to the master MDT, then the operation is decomposed into
36  * object updates which will be dispatched to OSD or OSP. The local updates
37  * go to local OSD and the remote updates go to OSP. In OSP, these remote
38  * object updates will be packed into an update RPC, sent to the remote MDT
39  * and handled by Object Update Target (OUT).
40  *
41  * In DNE phase I, because of missing complete recovery solution, updates
42  * will be executed in order and synchronously.
43  *     1. The transaction is created.
44  *     2. In transaction declare, it collects and packs remote
45  *        updates (in osp_md_declare_xxx()).
46  *     3. In transaction start, it sends these remote updates
47  *        to remote MDTs, which will execute these updates synchronously.
48  *     4. In transaction execute phase, the local updates will be executed
49  *        synchronously.
50  *
51  * Author: Di Wang <di.wang@intel.com>
52  */
53
54 #define DEBUG_SUBSYSTEM S_MDS
55
56 #include <lustre_log.h>
57 #include "osp_internal.h"
58
59 #define OUT_UPDATE_BUFFER_SIZE_ADD      4096
60 #define OUT_UPDATE_BUFFER_SIZE_MAX      (256 * 4096)  /*  1M update size now */
61
62 /**
63  * Interpreter call for object creation
64  *
65  * Object creation interpreter, which will be called after creating
66  * the remote object to set flags and status.
67  *
68  * \param[in] env       execution environment
69  * \param[in] reply     update reply
70  * \param[in] req       ptlrpc update request for creating object
71  * \param[in] obj       object to be created
72  * \param[in] data      data used in this function.
73  * \param[in] index     index(position) of create update in the whole
74  *                      updates
75  * \param[in] rc        update result on the remote MDT.
76  *
77  * \retval              only return 0 for now
78  */
79 static int osp_object_create_interpreter(const struct lu_env *env,
80                                          struct object_update_reply *reply,
81                                          struct ptlrpc_request *req,
82                                          struct osp_object *obj,
83                                          void *data, int index, int rc)
84 {
85         if (rc != 0) {
86                 obj->opo_obj.do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
87                 obj->opo_non_exist = 1;
88         }
89         return 0;
90 }
91
92 /**
93  * Implementation of dt_object_operations::do_declare_create
94  *
95  * Create the dt_update_request to track the update for this OSP
96  * in the transaction.
97  *
98  * \param[in] env       execution environment
99  * \param[in] dt        remote object to be created
100  * \param[in] attr      attribute of the created object
101  * \param[in] hint      creation hint
102  * \param[in] dof       creation format information
103  * \param[in] th        the transaction handle
104  *
105  * \retval              0 if preparation succeeds.
106  * \retval              negative errno if preparation fails.
107  */
108 int osp_md_declare_object_create(const struct lu_env *env,
109                                  struct dt_object *dt,
110                                  struct lu_attr *attr,
111                                  struct dt_allocation_hint *hint,
112                                  struct dt_object_format *dof,
113                                  struct thandle *th)
114 {
115         return osp_trans_update_request_create(th);
116 }
117
118 struct object_update *
119 update_buffer_get_update(struct object_update_request *request,
120                          unsigned int index)
121 {
122         void    *ptr;
123         int     i;
124
125         if (index > request->ourq_count)
126                 return NULL;
127
128         ptr = &request->ourq_updates[0];
129         for (i = 0; i < index; i++)
130                 ptr += object_update_size(ptr);
131
132         return ptr;
133 }
134
135 int osp_extend_update_buffer(const struct lu_env *env,
136                              struct update_buffer *ubuf)
137 {
138         struct object_update_request *ureq;
139         size_t  new_size = ubuf->ub_req_size + OUT_UPDATE_BUFFER_SIZE_ADD;
140
141         /* enlarge object update request size */
142         if (new_size > OUT_UPDATE_BUFFER_SIZE_MAX)
143                 return -E2BIG;
144
145         OBD_ALLOC_LARGE(ureq, new_size);
146         if (ureq == NULL)
147                 return -ENOMEM;
148
149         memcpy(ureq, ubuf->ub_req, ubuf->ub_req_size);
150
151         OBD_FREE_LARGE(ubuf->ub_req, ubuf->ub_req_size);
152
153         ubuf->ub_req = ureq;
154         ubuf->ub_req_size = new_size;
155
156         return 0;
157 }
158
159 /**
160  * Implementation of dt_object_operations::do_create
161  *
162  * It adds an OUT_CREATE sub-request into the OUT RPC that will be flushed
163  * when the transaction stop, and sets necessary flags for created object.
164  *
165  * \param[in] env       execution environment
166  * \param[in] dt        object to be created
167  * \param[in] attr      attribute of the created object
168  * \param[in] hint      creation hint
169  * \param[in] dof       creation format information
170  * \param[in] th        the transaction handle
171  *
172  * \retval              0 if packing creation succeeds.
173  * \retval              negative errno if packing creation fails.
174  */
175 int osp_md_object_create(const struct lu_env *env, struct dt_object *dt,
176                          struct lu_attr *attr, struct dt_allocation_hint *hint,
177                          struct dt_object_format *dof, struct thandle *th)
178 {
179         struct dt_update_request        *update;
180         int                             rc;
181
182         update = thandle_to_dt_update_request(th);
183         LASSERT(update != NULL);
184
185         rc = osp_update_rpc_pack(env, create, update, OUT_CREATE,
186                                  lu_object_fid(&dt->do_lu), attr, hint, dof);
187         if (rc != 0)
188                 GOTO(out, rc);
189
190         rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
191                                         osp_object_create_interpreter);
192
193         if (rc < 0)
194                 GOTO(out, rc);
195
196         dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
197         dt2osp_obj(dt)->opo_non_exist = 0;
198 out:
199         return rc;
200 }
201
202 /**
203  * Implementation of dt_object_operations::do_declare_ref_del
204  *
205  * Create the dt_update_request to track the update for this OSP
206  * in the transaction.
207  *
208  * \param[in] env       execution environment
209  * \param[in] dt        object to decrease the reference count.
210  * \param[in] th        the transaction handle of refcount decrease.
211  *
212  * \retval              0 if preparation succeeds.
213  * \retval              negative errno if preparation fails.
214  */
215 static int osp_md_declare_ref_del(const struct lu_env *env,
216                                   struct dt_object *dt, struct thandle *th)
217 {
218         return osp_trans_update_request_create(th);
219 }
220
221 /**
222  * Implementation of dt_object_operations::do_ref_del
223  *
224  * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
225  * flushed when the transaction stop.
226  *
227  * \param[in] env       execution environment
228  * \param[in] dt        object to decrease the reference count
229  * \param[in] th        the transaction handle
230  *
231  * \retval              0 if packing ref_del succeeds.
232  * \retval              negative errno if packing fails.
233  */
234 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
235                           struct thandle *th)
236 {
237         struct dt_update_request        *update;
238         int                             rc;
239
240         update = thandle_to_dt_update_request(th);
241         LASSERT(update != NULL);
242
243         rc = osp_update_rpc_pack(env, ref_del, update, OUT_REF_DEL,
244                                  lu_object_fid(&dt->do_lu));
245         return rc;
246 }
247
248 /**
249  * Implementation of dt_object_operations::do_declare_ref_del
250  *
251  * Create the dt_update_request to track the update for this OSP
252  * in the transaction.
253  *
254  * \param[in] env       execution environment
255  * \param[in] dt        object on which to increase the reference count.
256  * \param[in] th        the transaction handle.
257  *
258  * \retval              0 if preparation succeeds.
259  * \retval              negative errno if preparation fails.
260  */
261 static int osp_md_declare_ref_add(const struct lu_env *env,
262                                   struct dt_object *dt, struct thandle *th)
263 {
264         return osp_trans_update_request_create(th);
265 }
266
267 /**
268  * Implementation of dt_object_operations::do_ref_add
269  *
270  * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
271  * when the transaction stop.
272  *
273  * \param[in] env       execution environment
274  * \param[in] dt        object on which to increase the reference count
275  * \param[in] th        the transaction handle
276  *
277  * \retval              0 if packing ref_add succeeds.
278  * \retval              negative errno if packing fails.
279  */
280 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
281                           struct thandle *th)
282 {
283         struct dt_update_request        *update;
284         int                             rc;
285
286         update = thandle_to_dt_update_request(th);
287         LASSERT(update != NULL);
288
289         rc = osp_update_rpc_pack(env, ref_add, update, OUT_REF_ADD,
290                                  lu_object_fid(&dt->do_lu));
291         return rc;
292 }
293
294 /**
295  * Implementation of dt_object_operations::do_ah_init
296  *
297  * Initialize the allocation hint for object creation, which is usually called
298  * before the creation, and these hints (parent and child mode) will be sent to
299  * the remote Object Update Target (OUT) and used in the object create process,
300  * same as OSD object creation.
301  *
302  * \param[in] env       execution environment
303  * \param[in] ah        the hint to be initialized
304  * \param[in] parent    the parent of the object
305  * \param[in] child     the object to be created
306  * \param[in] child_mode the mode of the created object
307  */
308 static void osp_md_ah_init(const struct lu_env *env,
309                            struct dt_allocation_hint *ah,
310                            struct dt_object *parent,
311                            struct dt_object *child,
312                            umode_t child_mode)
313 {
314         LASSERT(ah);
315
316         ah->dah_parent = parent;
317         ah->dah_mode = child_mode;
318 }
319
320 /**
321  * Implementation of dt_object_operations::do_declare_attr_get
322  *
323  * Create the dt_update_request to track the update for this OSP
324  * in the transaction.
325  *
326  * \param[in] env       execution environment
327  * \param[in] dt        object on which to set attributes
328  * \param[in] attr      attributes to be set
329  * \param[in] th        the transaction handle
330  *
331  * \retval              0 if preparation succeeds.
332  * \retval              negative errno if preparation fails.
333  */
334 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
335                             const struct lu_attr *attr, struct thandle *th)
336 {
337         return osp_trans_update_request_create(th);
338 }
339
340 /**
341  * Implementation of dt_object_operations::do_attr_set
342  *
343  * Set attributes to the specified remote object.
344  *
345  * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
346  * when the transaction stop.
347  *
348  * \param[in] env       execution environment
349  * \param[in] dt        object to set attributes
350  * \param[in] attr      attributes to be set
351  * \param[in] th        the transaction handle
352  *
353  * \retval              0 if packing attr_set succeeds.
354  * \retval              negative errno if packing fails.
355  */
356 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
357                     const struct lu_attr *attr, struct thandle *th)
358 {
359         struct dt_update_request        *update;
360         int                             rc;
361
362         update = thandle_to_dt_update_request(th);
363         LASSERT(update != NULL);
364
365         rc = osp_update_rpc_pack(env, attr_set, update, OUT_ATTR_SET,
366                                  lu_object_fid(&dt->do_lu), attr);
367         return rc;
368 }
369
370 /**
371  * Implementation of dt_object_operations::do_read_lock
372  *
373  * osp_md_object_{read,write}_lock() will only lock the remote object in the
374  * local cache, which uses the semaphore (opo_sem) inside the osp_object to
375  * lock the object. Note: it will not lock the object in the whole cluster,
376  * which relies on the LDLM lock.
377  *
378  * \param[in] env       execution environment
379  * \param[in] dt        object to be locked
380  * \param[in] role      lock role from MDD layer, see mdd_object_role().
381  */
382 static void osp_md_object_read_lock(const struct lu_env *env,
383                                     struct dt_object *dt, unsigned role)
384 {
385         struct osp_object  *obj = dt2osp_obj(dt);
386
387         LASSERT(obj->opo_owner != env);
388         down_read_nested(&obj->opo_sem, role);
389
390         LASSERT(obj->opo_owner == NULL);
391 }
392
393 /**
394  * Implementation of dt_object_operations::do_write_lock
395  *
396  * Lock the remote object in write mode.
397  *
398  * \param[in] env       execution environment
399  * \param[in] dt        object to be locked
400  * \param[in] role      lock role from MDD layer, see mdd_object_role().
401  */
402 static void osp_md_object_write_lock(const struct lu_env *env,
403                                      struct dt_object *dt, unsigned role)
404 {
405         struct osp_object *obj = dt2osp_obj(dt);
406
407         down_write_nested(&obj->opo_sem, role);
408
409         LASSERT(obj->opo_owner == NULL);
410         obj->opo_owner = env;
411 }
412
413 /**
414  * Implementation of dt_object_operations::do_read_unlock
415  *
416  * Unlock the read lock of remote object.
417  *
418  * \param[in] env       execution environment
419  * \param[in] dt        object to be unlocked
420  */
421 static void osp_md_object_read_unlock(const struct lu_env *env,
422                                       struct dt_object *dt)
423 {
424         struct osp_object *obj = dt2osp_obj(dt);
425
426         up_read(&obj->opo_sem);
427 }
428
429 /**
430  * Implementation of dt_object_operations::do_write_unlock
431  *
432  * Unlock the write lock of remote object.
433  *
434  * \param[in] env       execution environment
435  * \param[in] dt        object to be unlocked
436  */
437 static void osp_md_object_write_unlock(const struct lu_env *env,
438                                        struct dt_object *dt)
439 {
440         struct osp_object *obj = dt2osp_obj(dt);
441
442         LASSERT(obj->opo_owner == env);
443         obj->opo_owner = NULL;
444         up_write(&obj->opo_sem);
445 }
446
447 /**
448  * Implementation of dt_object_operations::do_write_locked
449  *
450  * Test if the object is locked in write mode.
451  *
452  * \param[in] env       execution environment
453  * \param[in] dt        object to be tested
454  */
455 static int osp_md_object_write_locked(const struct lu_env *env,
456                                       struct dt_object *dt)
457 {
458         struct osp_object *obj = dt2osp_obj(dt);
459
460         return obj->opo_owner == env;
461 }
462
463 /**
464  * Implementation of dt_index_operations::dio_lookup
465  *
466  * Look up record by key under a remote index object. It packs lookup update
467  * into RPC, sends to the remote OUT and waits for the lookup result.
468  *
469  * \param[in] env       execution environment
470  * \param[in] dt        index object to lookup
471  * \param[out] rec      record in which to return lookup result
472  * \param[in] key       key of index which will be looked up
473  *
474  * \retval              1 if the lookup succeeds.
475  * \retval              negative errno if the lookup fails.
476  */
477 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
478                                struct dt_rec *rec, const struct dt_key *key)
479 {
480         struct lu_buf           *lbuf   = &osp_env_info(env)->osi_lb2;
481         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
482         struct dt_device        *dt_dev = &osp->opd_dt_dev;
483         struct dt_update_request   *update;
484         struct object_update_reply *reply;
485         struct ptlrpc_request      *req = NULL;
486         struct lu_fid              *fid;
487         int                        rc;
488         ENTRY;
489
490         /* Because it needs send the update buffer right away,
491          * just create an update buffer, instead of attaching the
492          * update_remote list of the thandle.
493          */
494         update = dt_update_request_create(dt_dev);
495         if (IS_ERR(update))
496                 RETURN(PTR_ERR(update));
497
498         rc = osp_update_rpc_pack(env, index_lookup, update, OUT_INDEX_LOOKUP,
499                                  lu_object_fid(&dt->do_lu), rec, key);
500         if (rc != 0) {
501                 CERROR("%s: Insert update error: rc = %d\n",
502                        dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
503                 GOTO(out, rc);
504         }
505
506         rc = osp_remote_sync(env, osp, update, &req);
507         if (rc < 0)
508                 GOTO(out, rc);
509
510         reply = req_capsule_server_sized_get(&req->rq_pill,
511                                              &RMF_OUT_UPDATE_REPLY,
512                                              OUT_UPDATE_REPLY_SIZE);
513         if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
514                 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
515                        dt_dev->dd_lu_dev.ld_obd->obd_name,
516                        reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
517                 GOTO(out, rc = -EPROTO);
518         }
519
520         rc = object_update_result_data_get(reply, lbuf, 0);
521         if (rc < 0)
522                 GOTO(out, rc);
523
524         if (lbuf->lb_len != sizeof(*fid)) {
525                 CERROR("%s: lookup "DFID" %s wrong size %d\n",
526                        dt_dev->dd_lu_dev.ld_obd->obd_name,
527                        PFID(lu_object_fid(&dt->do_lu)), (char *)key,
528                        (int)lbuf->lb_len);
529                 GOTO(out, rc = -EINVAL);
530         }
531
532         fid = lbuf->lb_buf;
533         if (ptlrpc_rep_need_swab(req))
534                 lustre_swab_lu_fid(fid);
535         if (!fid_is_sane(fid)) {
536                 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
537                        dt_dev->dd_lu_dev.ld_obd->obd_name,
538                        PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
539                 GOTO(out, rc = -EINVAL);
540         }
541
542         memcpy(rec, fid, sizeof(*fid));
543
544         GOTO(out, rc = 1);
545
546 out:
547         if (req != NULL)
548                 ptlrpc_req_finished(req);
549
550         dt_update_request_destroy(update);
551
552         return rc;
553 }
554
555 /**
556  * Implementation of dt_index_operations::dio_declare_insert
557  *
558  * Create the dt_update_request to track the update for this OSP
559  * in the transaction.
560  *
561  * \param[in] env       execution environment
562  * \param[in] dt        object for which to insert index
563  * \param[in] rec       record of the index which will be inserted
564  * \param[in] key       key of the index which will be inserted
565  * \param[in] th        the transaction handle
566  *
567  * \retval              0 if preparation succeeds.
568  * \retval              negative errno if preparation fails.
569  */
570 static int osp_md_declare_index_insert(const struct lu_env *env,
571                                        struct dt_object *dt,
572                                        const struct dt_rec *rec,
573                                        const struct dt_key *key,
574                                        struct thandle *th)
575 {
576         return osp_trans_update_request_create(th);
577 }
578
579 /**
580  * Implementation of dt_index_operations::dio_insert
581  *
582  * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
583  * be flushed when the transaction stop.
584  *
585  * \param[in] env       execution environment
586  * \param[in] dt        object for which to insert index
587  * \param[in] rec       record of the index to be inserted
588  * \param[in] key       key of the index to be inserted
589  * \param[in] th        the transaction handle
590  * \param[in] ignore_quota quota enforcement for insert
591  *
592  * \retval              0 if packing index insert succeeds.
593  * \retval              negative errno if packing fails.
594  */
595 static int osp_md_index_insert(const struct lu_env *env,
596                                struct dt_object *dt,
597                                const struct dt_rec *rec,
598                                const struct dt_key *key,
599                                struct thandle *th,
600                                int ignore_quota)
601 {
602         struct osp_thandle       *oth = thandle_to_osp_thandle(th);
603         struct dt_update_request *update = oth->ot_dur;
604         int                      rc;
605
606         rc = osp_update_rpc_pack(env, index_insert, update, OUT_INDEX_INSERT,
607                                  lu_object_fid(&dt->do_lu), rec, key);
608         return rc;
609 }
610
611 /**
612  * Implementation of dt_index_operations::dio_declare_delete
613  *
614  * Create the dt_update_request to track the update for this OSP
615  * in the transaction.
616  *
617  * \param[in] env       execution environment
618  * \param[in] dt        object for which to delete index
619  * \param[in] key       key of the index
620  * \param[in] th        the transaction handle
621  *
622  * \retval              0 if preparation succeeds.
623  * \retval              negative errno if preparation fails.
624  */
625 static int osp_md_declare_index_delete(const struct lu_env *env,
626                                        struct dt_object *dt,
627                                        const struct dt_key *key,
628                                        struct thandle *th)
629 {
630         return osp_trans_update_request_create(th);
631 }
632
633 /**
634  * Implementation of dt_index_operations::dio_delete
635  *
636  * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
637  * be flushed when the transaction stop.
638  *
639  * \param[in] env       execution environment
640  * \param[in] dt        object for which to delete index
641  * \param[in] key       key of the index which will be deleted
642  * \param[in] th        the transaction handle
643  *
644  * \retval              0 if packing index delete succeeds.
645  * \retval              negative errno if packing fails.
646  */
647 static int osp_md_index_delete(const struct lu_env *env,
648                                struct dt_object *dt,
649                                const struct dt_key *key,
650                                struct thandle *th)
651 {
652         struct dt_update_request *update;
653         int                      rc;
654
655         update = thandle_to_dt_update_request(th);
656         LASSERT(update != NULL);
657
658         rc = osp_update_rpc_pack(env, index_delete, update, OUT_INDEX_DELETE,
659                                  lu_object_fid(&dt->do_lu), key);
660
661         return rc;
662 }
663
664 /**
665  * Implementation of dt_index_operations::dio_it.next
666  *
667  * Advance the pointer of the iterator to the next entry. It shares a similar
668  * internal implementation with osp_orphan_it_next(), which is being used for
669  * remote orphan index object. This method will be used for remote directory.
670  *
671  * \param[in] env       execution environment
672  * \param[in] di        iterator of this iteration
673  *
674  * \retval              0 if the pointer is advanced successfuly.
675  * \retval              1 if it reaches to the end of the index object.
676  * \retval              negative errno if the pointer cannot be advanced.
677  */
678 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
679 {
680         struct osp_it           *it = (struct osp_it *)di;
681         struct lu_idxpage       *idxpage;
682         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
683         int                     rc;
684         ENTRY;
685
686 again:
687         idxpage = it->ooi_cur_idxpage;
688         if (idxpage != NULL) {
689                 if (idxpage->lip_nr == 0)
690                         RETURN(1);
691
692                 it->ooi_pos_ent++;
693                 if (ent == NULL) {
694                         it->ooi_ent =
695                               (struct lu_dirent *)idxpage->lip_entries;
696                         RETURN(0);
697                 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
698                            it->ooi_pos_ent < idxpage->lip_nr) {
699                         ent = (struct lu_dirent *)(((char *)ent) +
700                                         le16_to_cpu(ent->lde_reclen));
701                         it->ooi_ent = ent;
702                         RETURN(0);
703                 } else {
704                         it->ooi_ent = NULL;
705                 }
706         }
707
708         rc = osp_it_next_page(env, di);
709         if (rc == 0)
710                 goto again;
711
712         RETURN(rc);
713 }
714
715 /**
716  * Implementation of dt_index_operations::dio_it.key
717  *
718  * Get the key at current iterator poisiton. These iteration methods
719  * (dio_it) will only be used for iterating the remote directory, so
720  * the key is the name of the directory entry.
721  *
722  * \param[in] env       execution environment
723  * \param[in] di        iterator of this iteration
724  *
725  * \retval              name of the current entry
726  */
727 static struct dt_key *osp_it_key(const struct lu_env *env,
728                                  const struct dt_it *di)
729 {
730         struct osp_it           *it = (struct osp_it *)di;
731         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
732
733         return (struct dt_key *)ent->lde_name;
734 }
735
736 /**
737  * Implementation of dt_index_operations::dio_it.key_size
738  *
739  * Get the key size at current iterator poisiton. These iteration methods
740  * (dio_it) will only be used for iterating the remote directory, so the key
741  * size is the name size of the directory entry.
742  *
743  * \param[in] env       execution environment
744  * \param[in] di        iterator of this iteration
745  *
746  * \retval              name size of the current entry
747  */
748
749 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
750 {
751         struct osp_it           *it = (struct osp_it *)di;
752         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
753
754         return (int)le16_to_cpu(ent->lde_namelen);
755 }
756
757 /**
758  * Implementation of dt_index_operations::dio_it.rec
759  *
760  * Get the record at current iterator position. These iteration methods
761  * (dio_it) will only be used for iterating the remote directory, so it
762  * uses lu_dirent_calc_size() to calculate the record size.
763  *
764  * \param[in] env       execution environment
765  * \param[in] di        iterator of this iteration
766  * \param[out] rec      the record to be returned
767  * \param[in] attr      attributes of the index object, so it knows
768  *                      how to pack the entry.
769  *
770  * \retval              only return 0 for now
771  */
772 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
773                                struct dt_rec *rec, __u32 attr)
774 {
775         struct osp_it           *it = (struct osp_it *)di;
776         struct lu_dirent        *ent = (struct lu_dirent *)it->ooi_ent;
777         size_t                  reclen;
778
779         reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
780         memcpy(rec, ent, reclen);
781         return 0;
782 }
783
784 /**
785  * Implementation of dt_index_operations::dio_it.load
786  *
787  * Locate the iteration cursor to the specified position (cookie).
788  *
789  * \param[in] env       pointer to the thread context
790  * \param[in] di        pointer to the iteration structure
791  * \param[in] hash      the specified position
792  *
793  * \retval              positive number for locating to the exactly position
794  *                      or the next
795  * \retval              0 for arriving at the end of the iteration
796  * \retval              negative error number on failure
797  */
798 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
799                        __u64 hash)
800 {
801         struct osp_it   *it     = (struct osp_it *)di;
802         int              rc;
803
804         it->ooi_next = hash;
805         rc = osp_md_index_it_next(env, (struct dt_it *)di);
806         if (rc == 1)
807                 return 0;
808
809         if (rc == 0)
810                 return 1;
811
812         return rc;
813 }
814
815 const struct dt_index_operations osp_md_index_ops = {
816         .dio_lookup         = osp_md_index_lookup,
817         .dio_declare_insert = osp_md_declare_index_insert,
818         .dio_insert         = osp_md_index_insert,
819         .dio_declare_delete = osp_md_declare_index_delete,
820         .dio_delete         = osp_md_index_delete,
821         .dio_it     = {
822                 .init     = osp_it_init,
823                 .fini     = osp_it_fini,
824                 .get      = osp_it_get,
825                 .put      = osp_it_put,
826                 .next     = osp_md_index_it_next,
827                 .key      = osp_it_key,
828                 .key_size = osp_it_key_size,
829                 .rec      = osp_md_index_it_rec,
830                 .store    = osp_it_store,
831                 .load     = osp_it_load,
832                 .key_rec  = osp_it_key_rec,
833         }
834 };
835
836 /**
837  * Implementation of dt_object_operations::do_index_try
838  *
839  * Try to initialize the index API pointer for the given object. This
840  * is the entry point of the index API, i.e. we must call this method
841  * to initialize the index object before calling other index methods.
842  *
843  * \param[in] env       execution environment
844  * \param[in] dt        index object to be initialized
845  * \param[in] feat      the index feature of the object
846  *
847  * \retval              0 if the initialization succeeds.
848  * \retval              negative errno if the initialization fails.
849  */
850 static int osp_md_index_try(const struct lu_env *env,
851                             struct dt_object *dt,
852                             const struct dt_index_features *feat)
853 {
854         dt->do_index_ops = &osp_md_index_ops;
855         return 0;
856 }
857
858 /**
859  * Implementation of dt_object_operations::do_object_lock
860  *
861  * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
862  * which will lock the object in the global namespace.
863  *
864  * \param[in] env       execution environment
865  * \param[in] dt        object to be locked
866  * \param[out] lh       lock handle
867  * \param[in] einfo     enqueue information
868  * \param[in] policy    lock policy
869  *
870  * \retval              ELDLM_OK if locking the object succeeds.
871  * \retval              negative errno if locking fails.
872  */
873 static int osp_md_object_lock(const struct lu_env *env,
874                               struct dt_object *dt,
875                               struct lustre_handle *lh,
876                               struct ldlm_enqueue_info *einfo,
877                               ldlm_policy_data_t *policy)
878 {
879         struct ldlm_res_id      *res_id;
880         struct dt_device        *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
881         struct osp_device       *osp = dt2osp_dev(dt_dev);
882         struct ptlrpc_request   *req;
883         int                     rc = 0;
884         __u64                   flags = 0;
885         ldlm_mode_t             mode;
886
887         res_id = einfo->ei_res_id;
888         LASSERT(res_id != NULL);
889
890         mode = ldlm_lock_match(osp->opd_obd->obd_namespace,
891                                LDLM_FL_BLOCK_GRANTED, res_id,
892                                einfo->ei_type, policy,
893                                einfo->ei_mode, lh, 0);
894         if (mode > 0)
895                 return ELDLM_OK;
896
897         req = ldlm_enqueue_pack(osp->opd_exp, 0);
898         if (IS_ERR(req))
899                 RETURN(PTR_ERR(req));
900
901         rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
902                               (const ldlm_policy_data_t *)policy,
903                               &flags, NULL, 0, LVB_T_NONE, lh, 0);
904
905         ptlrpc_req_finished(req);
906
907         return rc == ELDLM_OK ? 0 : -EIO;
908 }
909
910 /**
911  * Implementation of dt_object_operations::do_object_unlock
912  *
913  * Cancel a lock of a remote object.
914  *
915  * \param[in] env       execution environment
916  * \param[in] dt        object to be unlocked
917  * \param[in] einfo     lock enqueue information
918  * \param[in] policy    lock policy
919  *
920  * \retval              Only return 0 for now.
921  */
922 static int osp_md_object_unlock(const struct lu_env *env,
923                                 struct dt_object *dt,
924                                 struct ldlm_enqueue_info *einfo,
925                                 ldlm_policy_data_t *policy)
926 {
927         struct lustre_handle    *lockh = einfo->ei_cbdata;
928
929         /* unlock finally */
930         ldlm_lock_decref(lockh, einfo->ei_mode);
931
932         return 0;
933 }
934
935 /**
936  * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
937  *
938  * Create the dt_update_request to track the update for this OSP
939  * in the transaction.
940  *
941  * \param[in] env       pointer to the thread context
942  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
943  * \param[in] th        pointer to the transaction handler
944  *
945  * \retval              0 for success
946  * \retval              negative error number on failure
947  */
948 int osp_md_declare_object_destroy(const struct lu_env *env,
949                                struct dt_object *dt, struct thandle *th)
950 {
951         return osp_trans_update_request_create(th);
952 }
953
954 /**
955  * Interpreter call for object destroy
956  *
957  * Object destroy interpreter, which will be called after destroying
958  * the remote object to set flags and status.
959  *
960  * \param[in] env       execution environment
961  * \param[in] reply     update reply
962  * \param[in] req       ptlrpc update request for destroying object
963  * \param[in] obj       object to be destroyed
964  * \param[in] data      data used in this function.
965  * \param[in] index     index(position) of destroy update in the whole
966  *                      updates
967  * \param[in] rc        update result on the remote MDT.
968  *
969  * \retval              only return 0 for now
970  */
971 static int osp_md_object_destroy_interpreter(const struct lu_env *env,
972                                              struct object_update_reply *reply,
973                                              struct ptlrpc_request *req,
974                                              struct osp_object *obj,
975                                              void *data, int index, int rc)
976 {
977         /* not needed in cache any more */
978         set_bit(LU_OBJECT_HEARD_BANSHEE,
979                 &obj->opo_obj.do_lu.lo_header->loh_flags);
980         return 0;
981 }
982
983 /**
984  * Implement OSP layer dt_object_operations::do_destroy() interface.
985  *
986  * Pack the destroy update into the RPC buffer, which will be sent
987  * to the remote MDT during transaction stop.
988  *
989  * It also marks the object as non-cached.
990  *
991  * \param[in] env       pointer to the thread context
992  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
993  * \param[in] th        pointer to the transaction handler
994  *
995  * \retval              0 for success
996  * \retval              negative error number on failure
997  */
998 int osp_md_object_destroy(const struct lu_env *env, struct dt_object *dt,
999                           struct thandle *th)
1000 {
1001         struct osp_object               *o = dt2osp_obj(dt);
1002         struct osp_device               *osp = lu2osp_dev(dt->do_lu.lo_dev);
1003         struct dt_update_request        *update;
1004         int                             rc = 0;
1005
1006         ENTRY;
1007         o->opo_non_exist = 1;
1008
1009         LASSERT(osp->opd_connect_mdt);
1010         update = thandle_to_dt_update_request(th);
1011         LASSERT(update != NULL);
1012
1013         rc = osp_update_rpc_pack(env, object_destroy, update, OUT_DESTROY,
1014                                  lu_object_fid(&dt->do_lu));
1015         if (rc != 0)
1016                 RETURN(rc);
1017
1018         rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1019                                         osp_md_object_destroy_interpreter);
1020         RETURN(rc);
1021 }
1022
1023 struct dt_object_operations osp_md_obj_ops = {
1024         .do_read_lock         = osp_md_object_read_lock,
1025         .do_write_lock        = osp_md_object_write_lock,
1026         .do_read_unlock       = osp_md_object_read_unlock,
1027         .do_write_unlock      = osp_md_object_write_unlock,
1028         .do_write_locked      = osp_md_object_write_locked,
1029         .do_declare_create    = osp_md_declare_object_create,
1030         .do_create            = osp_md_object_create,
1031         .do_declare_ref_add   = osp_md_declare_ref_add,
1032         .do_ref_add           = osp_md_ref_add,
1033         .do_declare_ref_del   = osp_md_declare_ref_del,
1034         .do_ref_del           = osp_md_ref_del,
1035         .do_declare_destroy   = osp_md_declare_object_destroy,
1036         .do_destroy           = osp_md_object_destroy,
1037         .do_ah_init           = osp_md_ah_init,
1038         .do_attr_get          = osp_attr_get,
1039         .do_declare_attr_set  = osp_md_declare_attr_set,
1040         .do_attr_set          = osp_md_attr_set,
1041         .do_xattr_get         = osp_xattr_get,
1042         .do_declare_xattr_set = osp_declare_xattr_set,
1043         .do_xattr_set         = osp_xattr_set,
1044         .do_declare_xattr_del = osp_declare_xattr_del,
1045         .do_xattr_del         = osp_xattr_del,
1046         .do_index_try         = osp_md_index_try,
1047         .do_object_lock       = osp_md_object_lock,
1048         .do_object_unlock     = osp_md_object_unlock,
1049 };
1050
1051 /**
1052  * Implementation of dt_body_operations::dbo_declare_write
1053  *
1054  * Create the dt_update_request to track the update for this OSP
1055  * in the transaction.
1056   *
1057  * \param[in] env       execution environment
1058  * \param[in] dt        object to be written
1059  * \param[in] buf       buffer to write which includes an embedded size field
1060  * \param[in] pos       offet in the object to start writing at
1061  * \param[in] th        transaction handle
1062  *
1063  * \retval              0 if preparation succeeds.
1064  * \retval              negative errno if preparation fails.
1065  */
1066 static ssize_t osp_md_declare_write(const struct lu_env *env,
1067                                     struct dt_object *dt,
1068                                     const struct lu_buf *buf,
1069                                     loff_t pos, struct thandle *th)
1070 {
1071         return osp_trans_update_request_create(th);
1072 }
1073
1074 /**
1075  * Implementation of dt_body_operations::dbo_write
1076  *
1077  * Pack the write object update into the RPC buffer, which will be sent
1078  * to the remote MDT during transaction stop.
1079  *
1080  * \param[in] env       execution environment
1081  * \param[in] dt        object to be written
1082  * \param[in] buf       buffer to write which includes an embedded size field
1083  * \param[in] pos       offet in the object to start writing at
1084  * \param[in] th        transaction handle
1085  * \param[in] ignore_quota quota enforcement for this write
1086  *
1087  * \retval              the buffer size in bytes if packing succeeds.
1088  * \retval              negative errno if packing fails.
1089  */
1090 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1091                             const struct lu_buf *buf, loff_t *pos,
1092                             struct thandle *th, int ignore_quota)
1093 {
1094         struct dt_update_request  *update;
1095         ssize_t                   rc;
1096
1097         update = thandle_to_dt_update_request(th);
1098         LASSERT(update != NULL);
1099
1100         rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1101                                  lu_object_fid(&dt->do_lu), buf, *pos);
1102         if (rc < 0)
1103                 return rc;
1104
1105         /* XXX: how about the write error happened later? */
1106         *pos += buf->lb_len;
1107         return buf->lb_len;
1108 }
1109
1110 /* These body operation will be used to write symlinks during migration etc */
1111 struct dt_body_operations osp_md_body_ops = {
1112         .dbo_declare_write      = osp_md_declare_write,
1113         .dbo_write              = osp_md_write,
1114 };