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