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