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