4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2013, 2017, Intel Corporation.
26 * lustre/osp/osp_md_object.c
28 * OST/MDT proxy device (OSP) Metadata methods
30 * This file implements methods for remote MD object, which include
31 * dt_object_operations, dt_index_operations and dt_body_operations.
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).
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
51 * Author: Di Wang <di.wang@intel.com>
54 #define DEBUG_SUBSYSTEM S_MDS
56 #include <llog_swab.h>
57 #include <lustre_log.h>
58 #include "osp_internal.h"
60 #define OUT_UPDATE_BUFFER_SIZE_ADD 4096
61 #define OUT_UPDATE_BUFFER_SIZE_MAX (256 * 4096) /* 1M update size now */
64 * Interpreter call for object creation
66 * Object creation interpreter, which will be called after creating
67 * the remote object to set flags and status.
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
76 * \param[in] rc update result on the remote MDT.
78 * \retval only return 0 for now
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)
86 if (rc != 0 && rc != -EEXIST) {
87 obj->opo_obj.do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
88 obj->opo_non_exist = 1;
92 * invalidate opo cache for the object after the object is created, so
93 * attr_get will try to get attr from remote object.
95 osp_obj_invalidate_cache(obj);
101 * Implementation of dt_object_operations::do_declare_create
103 * Create the osp_update_request to track the update for this OSP
104 * in the transaction.
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
113 * \retval 0 if preparation succeeds.
114 * \retval negative errno if preparation fails.
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)
120 return osp_trans_update_request_create(th);
123 struct object_update *
124 update_buffer_get_update(struct object_update_request *request,
130 if (index > request->ourq_count)
133 ptr = &request->ourq_updates[0];
134 for (i = 0; i < index; i++)
135 ptr += object_update_size(ptr);
141 * Implementation of dt_object_operations::do_create
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.
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
153 * \retval 0 if packing creation succeeds.
154 * \retval negative errno if packing creation fails.
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)
160 struct osp_update_request *update;
161 struct osp_object *obj = dt2osp_obj(dt);
164 update = thandle_to_osp_update_request(th);
165 LASSERT(update != NULL);
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);
173 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
174 osp_create_interpreter);
179 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
180 dt2osp_obj(dt)->opo_non_exist = 0;
183 obj->opo_attr = *attr;
189 * Implementation of dt_object_operations::do_declare_ref_del
191 * Create the osp_update_request to track the update for this OSP
192 * in the transaction.
194 * \param[in] env execution environment
195 * \param[in] dt object to decrease the reference count.
196 * \param[in] th the transaction handle of refcount decrease.
198 * \retval 0 if preparation succeeds.
199 * \retval negative errno if preparation fails.
201 static int osp_md_declare_ref_del(const struct lu_env *env,
202 struct dt_object *dt, struct thandle *th)
204 return osp_trans_update_request_create(th);
208 * Implementation of dt_object_operations::do_ref_del
210 * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
211 * flushed when the transaction stop.
213 * \param[in] env execution environment
214 * \param[in] dt object to decrease the reference count
215 * \param[in] th the transaction handle
217 * \retval 0 if packing ref_del succeeds.
218 * \retval negative errno if packing fails.
220 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
223 struct osp_update_request *update;
226 update = thandle_to_osp_update_request(th);
227 LASSERT(update != NULL);
229 rc = OSP_UPDATE_RPC_PACK(env, out_ref_del_pack, update,
230 lu_object_fid(&dt->do_lu));
235 * Implementation of dt_object_operations::do_declare_ref_del
237 * Create the osp_update_request to track the update for this OSP
238 * in the transaction.
240 * \param[in] env execution environment
241 * \param[in] dt object on which to increase the reference count.
242 * \param[in] th the transaction handle.
244 * \retval 0 if preparation succeeds.
245 * \retval negative errno if preparation fails.
247 static int osp_md_declare_ref_add(const struct lu_env *env,
248 struct dt_object *dt, struct thandle *th)
250 return osp_trans_update_request_create(th);
254 * Implementation of dt_object_operations::do_ref_add
256 * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
257 * when the transaction stop.
259 * \param[in] env execution environment
260 * \param[in] dt object on which to increase the reference count
261 * \param[in] th the transaction handle
263 * \retval 0 if packing ref_add succeeds.
264 * \retval negative errno if packing fails.
266 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
269 struct osp_update_request *update;
272 update = thandle_to_osp_update_request(th);
273 LASSERT(update != NULL);
275 rc = OSP_UPDATE_RPC_PACK(env, out_ref_add_pack, update,
276 lu_object_fid(&dt->do_lu));
281 * Implementation of dt_object_operations::do_ah_init
283 * Initialize the allocation hint for object creation, which is usually called
284 * before the creation, and these hints (parent and child mode) will be sent to
285 * the remote Object Update Target (OUT) and used in the object create process,
286 * same as OSD object creation.
288 * \param[in] env execution environment
289 * \param[in] ah the hint to be initialized
290 * \param[in] parent the parent of the object
291 * \param[in] child the object to be created
292 * \param[in] child_mode the mode of the created object
294 static void osp_md_ah_init(const struct lu_env *env,
295 struct dt_allocation_hint *ah,
296 struct dt_object *parent,
297 struct dt_object *child,
302 ah->dah_parent = parent;
303 ah->dah_mode = child_mode;
307 * Implementation of dt_object_operations::do_declare_attr_get
309 * Create the osp_update_request to track the update for this OSP
310 * in the transaction.
312 * \param[in] env execution environment
313 * \param[in] dt object on which to set attributes
314 * \param[in] attr attributes to be set
315 * \param[in] th the transaction handle
317 * \retval 0 if preparation succeeds.
318 * \retval negative errno if preparation fails.
320 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
321 const struct lu_attr *attr, struct thandle *th)
323 return osp_trans_update_request_create(th);
327 * Implementation of dt_object_operations::do_attr_set
329 * Set attributes to the specified remote object.
331 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
332 * when the transaction stop.
334 * \param[in] env execution environment
335 * \param[in] dt object to set attributes
336 * \param[in] attr attributes to be set
337 * \param[in] th the transaction handle
339 * \retval 0 if packing attr_set succeeds.
340 * \retval negative errno if packing fails.
342 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
343 const struct lu_attr *attr, struct thandle *th)
345 struct osp_update_request *update;
348 update = thandle_to_osp_update_request(th);
349 LASSERT(update != NULL);
351 rc = OSP_UPDATE_RPC_PACK(env, out_attr_set_pack, update,
352 lu_object_fid(&dt->do_lu), attr);
357 * Implementation of dt_object_operations::do_read_lock
359 * osp_md_{read,write}_lock() will only lock the remote object in the
360 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
361 * lock the object. Note: it will not lock the object in the whole cluster,
362 * which relies on the LDLM lock.
364 * \param[in] env execution environment
365 * \param[in] dt object to be locked
366 * \param[in] role lock role from MDD layer, see dt_object_role().
368 static void osp_md_read_lock(const struct lu_env *env, struct dt_object *dt,
371 struct osp_object *obj = dt2osp_obj(dt);
373 LASSERT(obj->opo_owner != env);
374 down_read_nested(&obj->opo_sem, role);
376 LASSERT(obj->opo_owner == NULL);
380 * Implementation of dt_object_operations::do_write_lock
382 * Lock the remote object in write mode.
384 * \param[in] env execution environment
385 * \param[in] dt object to be locked
386 * \param[in] role lock role from MDD layer, see dt_object_role().
388 static void osp_md_write_lock(const struct lu_env *env, struct dt_object *dt,
391 struct osp_object *obj = dt2osp_obj(dt);
393 down_write_nested(&obj->opo_sem, role);
395 LASSERT(obj->opo_owner == NULL);
396 obj->opo_owner = env;
400 * Implementation of dt_object_operations::do_read_unlock
402 * Unlock the read lock of remote object.
404 * \param[in] env execution environment
405 * \param[in] dt object to be unlocked
407 static void osp_md_read_unlock(const struct lu_env *env, struct dt_object *dt)
409 struct osp_object *obj = dt2osp_obj(dt);
411 up_read(&obj->opo_sem);
415 * Implementation of dt_object_operations::do_write_unlock
417 * Unlock the write lock of remote object.
419 * \param[in] env execution environment
420 * \param[in] dt object to be unlocked
422 static void osp_md_write_unlock(const struct lu_env *env, struct dt_object *dt)
424 struct osp_object *obj = dt2osp_obj(dt);
426 LASSERT(obj->opo_owner == env);
427 obj->opo_owner = NULL;
428 up_write(&obj->opo_sem);
432 * Implementation of dt_object_operations::do_write_locked
434 * Test if the object is locked in write mode.
436 * \param[in] env execution environment
437 * \param[in] dt object to be tested
439 static int osp_md_write_locked(const struct lu_env *env, struct dt_object *dt)
441 struct osp_object *obj = dt2osp_obj(dt);
443 return obj->opo_owner == env;
447 * Implementation of dt_index_operations::dio_lookup
449 * Look up record by key under a remote index object. It packs lookup update
450 * into RPC, sends to the remote OUT and waits for the lookup result.
452 * \param[in] env execution environment
453 * \param[in] dt index object to lookup
454 * \param[out] rec record in which to return lookup result
455 * \param[in] key key of index which will be looked up
457 * \retval 1 if the lookup succeeds.
458 * \retval negative errno if the lookup fails.
460 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
461 struct dt_rec *rec, const struct dt_key *key)
463 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
464 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
465 struct dt_device *dt_dev = &osp->opd_dt_dev;
466 struct osp_update_request *update;
467 struct object_update_reply *reply;
468 struct ptlrpc_request *req = NULL;
473 /* Because it needs send the update buffer right away,
474 * just create an update buffer, instead of attaching the
475 * update_remote list of the thandle.
477 update = osp_update_request_create(dt_dev);
479 RETURN(PTR_ERR(update));
481 rc = OSP_UPDATE_RPC_PACK(env, out_index_lookup_pack, update,
482 lu_object_fid(&dt->do_lu), rec, key);
484 CERROR("%s: Insert update error: rc = %d\n",
485 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
489 rc = osp_remote_sync(env, osp, update, &req);
493 reply = req_capsule_server_sized_get(&req->rq_pill,
494 &RMF_OUT_UPDATE_REPLY,
495 OUT_UPDATE_REPLY_SIZE);
496 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
497 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
498 dt_dev->dd_lu_dev.ld_obd->obd_name,
499 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
500 GOTO(out, rc = -EPROTO);
503 rc = object_update_result_data_get(reply, lbuf, 0);
507 if (lbuf->lb_len != sizeof(*fid)) {
508 CERROR("%s: lookup "DFID" %s wrong size %d\n",
509 dt_dev->dd_lu_dev.ld_obd->obd_name,
510 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
512 GOTO(out, rc = -EINVAL);
516 if (ptlrpc_rep_need_swab(req))
517 lustre_swab_lu_fid(fid);
518 if (!fid_is_sane(fid)) {
519 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
520 dt_dev->dd_lu_dev.ld_obd->obd_name,
521 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
522 GOTO(out, rc = -EINVAL);
525 memcpy(rec, fid, sizeof(*fid));
531 ptlrpc_req_finished(req);
533 osp_update_request_destroy(env, update);
539 * Implementation of dt_index_operations::dio_declare_insert
541 * Create the osp_update_request to track the update for this OSP
542 * in the transaction.
544 * \param[in] env execution environment
545 * \param[in] dt object for which to insert index
546 * \param[in] rec record of the index which will be inserted
547 * \param[in] key key of the index which will be inserted
548 * \param[in] th the transaction handle
550 * \retval 0 if preparation succeeds.
551 * \retval negative errno if preparation fails.
553 static int osp_md_declare_index_insert(const struct lu_env *env,
554 struct dt_object *dt,
555 const struct dt_rec *rec,
556 const struct dt_key *key,
559 return osp_trans_update_request_create(th);
563 * Implementation of dt_index_operations::dio_insert
565 * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
566 * be flushed when the transaction stop.
568 * \param[in] env execution environment
569 * \param[in] dt object for which to insert index
570 * \param[in] rec record of the index to be inserted
571 * \param[in] key key of the index to be inserted
572 * \param[in] th the transaction handle
574 * \retval 0 if packing index insert succeeds.
575 * \retval negative errno if packing fails.
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)
581 struct osp_update_request *update;
584 update = thandle_to_osp_update_request(th);
585 LASSERT(update != NULL);
587 rc = OSP_UPDATE_RPC_PACK(env, out_index_insert_pack, update,
588 lu_object_fid(&dt->do_lu), rec, key);
593 * Implementation of dt_index_operations::dio_declare_delete
595 * Create the osp_update_request to track the update for this OSP
596 * in the transaction.
598 * \param[in] env execution environment
599 * \param[in] dt object for which to delete index
600 * \param[in] key key of the index
601 * \param[in] th the transaction handle
603 * \retval 0 if preparation succeeds.
604 * \retval negative errno if preparation fails.
606 static int osp_md_declare_index_delete(const struct lu_env *env,
607 struct dt_object *dt,
608 const struct dt_key *key,
611 return osp_trans_update_request_create(th);
615 * Implementation of dt_index_operations::dio_delete
617 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
618 * be flushed when the transaction stop.
620 * \param[in] env execution environment
621 * \param[in] dt object for which to delete index
622 * \param[in] key key of the index which will be deleted
623 * \param[in] th the transaction handle
625 * \retval 0 if packing index delete succeeds.
626 * \retval negative errno if packing fails.
628 static int osp_md_index_delete(const struct lu_env *env,
629 struct dt_object *dt,
630 const struct dt_key *key,
633 struct osp_update_request *update;
636 update = thandle_to_osp_update_request(th);
637 LASSERT(update != NULL);
639 rc = OSP_UPDATE_RPC_PACK(env, out_index_delete_pack, update,
640 lu_object_fid(&dt->do_lu), key);
646 * Implementation of dt_index_operations::dio_it.next
648 * Advance the pointer of the iterator to the next entry. It shares a similar
649 * internal implementation with osp_orphan_it_next(), which is being used for
650 * remote orphan index object. This method will be used for remote directory.
652 * \param[in] env execution environment
653 * \param[in] di iterator of this iteration
655 * \retval 0 if the pointer is advanced successfully.
656 * \retval 1 if it reaches to the end of the index object.
657 * \retval negative errno if the pointer cannot be advanced.
659 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
661 struct osp_it *it = (struct osp_it *)di;
662 struct lu_idxpage *idxpage;
663 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
668 idxpage = it->ooi_cur_idxpage;
669 if (idxpage != NULL) {
670 if (idxpage->lip_nr == 0)
676 (struct lu_dirent *)idxpage->lip_entries;
678 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
679 it->ooi_pos_ent < idxpage->lip_nr) {
680 ent = (struct lu_dirent *)(((char *)ent) +
681 le16_to_cpu(ent->lde_reclen));
689 rc = osp_it_next_page(env, di);
697 * Implementation of dt_index_operations::dio_it.key
699 * Get the key at current iterator poisiton. These iteration methods
700 * (dio_it) will only be used for iterating the remote directory, so
701 * the key is the name of the directory entry.
703 * \param[in] env execution environment
704 * \param[in] di iterator of this iteration
706 * \retval name of the current entry
708 static struct dt_key *osp_it_key(const struct lu_env *env,
709 const struct dt_it *di)
711 struct osp_it *it = (struct osp_it *)di;
712 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
714 return (struct dt_key *)ent->lde_name;
718 * Implementation of dt_index_operations::dio_it.key_size
720 * Get the key size at current iterator poisiton. These iteration methods
721 * (dio_it) will only be used for iterating the remote directory, so the key
722 * size is the name size of the directory entry.
724 * \param[in] env execution environment
725 * \param[in] di iterator of this iteration
727 * \retval name size of the current entry
730 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
732 struct osp_it *it = (struct osp_it *)di;
733 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
735 return (int)le16_to_cpu(ent->lde_namelen);
739 * Implementation of dt_index_operations::dio_it.rec
741 * Get the record at current iterator position. These iteration methods
742 * (dio_it) will only be used for iterating the remote directory, so it
743 * uses lu_dirent_calc_size() to calculate the record size.
745 * \param[in] env execution environment
746 * \param[in] di iterator of this iteration
747 * \param[out] rec the record to be returned
748 * \param[in] attr attributes of the index object, so it knows
749 * how to pack the entry.
751 * \retval only return 0 for now
753 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
754 struct dt_rec *rec, __u32 attr)
756 struct osp_it *it = (struct osp_it *)di;
757 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
760 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
761 memcpy(rec, ent, reclen);
766 * Implementation of dt_index_operations::dio_it.load
768 * Locate the iteration cursor to the specified position (cookie).
770 * \param[in] env pointer to the thread context
771 * \param[in] di pointer to the iteration structure
772 * \param[in] hash the specified position
774 * \retval positive number for locating to the exactly position
776 * \retval 0 for arriving at the end of the iteration
777 * \retval negative error number on failure
779 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
782 struct osp_it *it = (struct osp_it *)di;
786 rc = osp_md_index_it_next(env, (struct dt_it *)di);
796 const struct dt_index_operations osp_md_index_ops = {
797 .dio_lookup = osp_md_index_lookup,
798 .dio_declare_insert = osp_md_declare_index_insert,
799 .dio_insert = osp_md_index_insert,
800 .dio_declare_delete = osp_md_declare_index_delete,
801 .dio_delete = osp_md_index_delete,
807 .next = osp_md_index_it_next,
809 .key_size = osp_it_key_size,
810 .rec = osp_md_index_it_rec,
811 .store = osp_it_store,
813 .key_rec = osp_it_key_rec,
818 * Implement OSP layer dt_object_operations::do_xattr_list() interface.
820 * List extended attribute from the specified MDT/OST object, result is not
821 * cached because this is called by directory migration only.
823 * \param[in] env pointer to the thread context
824 * \param[in] dt pointer to the OSP layer dt_object
825 * \param[out] buf pointer to the lu_buf to hold the extended attribute
827 * \retval positive bytes used/required in the buffer
828 * \retval negative error number on failure
830 static int osp_md_xattr_list(const struct lu_env *env, struct dt_object *dt,
831 const struct lu_buf *buf)
833 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
834 struct osp_object *obj = dt2osp_obj(dt);
835 struct dt_device *dev = &osp->opd_dt_dev;
836 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
837 struct osp_update_request *update = NULL;
838 struct ptlrpc_request *req = NULL;
839 struct object_update_reply *reply;
840 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
847 if (unlikely(obj->opo_non_exist))
850 update = osp_update_request_create(dev);
852 RETURN(PTR_ERR(update));
854 rc = OSP_UPDATE_RPC_PACK(env, out_xattr_list_pack, update,
855 lu_object_fid(&dt->do_lu), buf->lb_len);
857 CERROR("%s: Insert update error "DFID": rc = %d\n",
858 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
862 rc = osp_remote_sync(env, osp, update, &req);
865 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
866 obj->opo_non_exist = 1;
871 reply = req_capsule_server_sized_get(&req->rq_pill,
872 &RMF_OUT_UPDATE_REPLY,
873 OUT_UPDATE_REPLY_SIZE);
874 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
875 DEBUG_REQ(D_ERROR, req,
876 "%s: Wrong version %x expected %x "DFID": rc = %d",
877 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
878 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
880 GOTO(out, rc = -EPROTO);
883 rc = object_update_result_data_get(reply, rbuf, 0);
890 if (unlikely(buf->lb_len < rbuf->lb_len))
891 GOTO(out, rc = -ERANGE);
893 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
898 ptlrpc_req_finished(req);
900 if (update && !IS_ERR(update))
901 osp_update_request_destroy(env, update);
907 * Implementation of dt_object_operations::do_index_try
909 * Try to initialize the index API pointer for the given object. This
910 * is the entry point of the index API, i.e. we must call this method
911 * to initialize the index object before calling other index methods.
913 * \param[in] env execution environment
914 * \param[in] dt index object to be initialized
915 * \param[in] feat the index feature of the object
917 * \retval 0 if the initialization succeeds.
918 * \retval negative errno if the initialization fails.
920 static int osp_md_index_try(const struct lu_env *env,
921 struct dt_object *dt,
922 const struct dt_index_features *feat)
924 dt->do_index_ops = &osp_md_index_ops;
929 * Implementation of dt_object_operations::do_object_lock
931 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
932 * which will lock the object in the global namespace. And because the
933 * cross-MDT locks are relatively rare compared with normal local MDT operation,
934 * let's release it right away, instead of putting it into the LRU list.
936 * \param[in] env execution environment
937 * \param[in] dt object to be locked
938 * \param[out] lh lock handle
939 * \param[in] einfo enqueue information
940 * \param[in] policy lock policy
942 * \retval ELDLM_OK if locking the object succeeds.
943 * \retval negative errno if locking fails.
945 static int osp_md_object_lock(const struct lu_env *env,
946 struct dt_object *dt,
947 struct lustre_handle *lh,
948 struct ldlm_enqueue_info *einfo,
949 union ldlm_policy_data *policy)
951 struct ldlm_res_id *res_id;
952 struct osp_device *osp = dt2osp_dev(lu2dt_dev(dt->do_lu.lo_dev));
953 struct ptlrpc_request *req;
955 __u64 flags = LDLM_FL_NO_LRU;
958 res_id = einfo->ei_res_id;
959 LASSERT(res_id != NULL);
961 if (einfo->ei_mode & (LCK_EX | LCK_PW))
962 flags |= LDLM_FL_COS_INCOMPAT;
964 req = ldlm_enqueue_pack(osp->opd_exp, 0);
966 RETURN(PTR_ERR(req));
968 osp_set_req_replay(osp, req);
969 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
970 (const union ldlm_policy_data *)policy,
971 &flags, NULL, 0, LVB_T_NONE, lh, 0);
973 ptlrpc_req_finished(req);
975 RETURN(rc == ELDLM_OK ? 0 : -EIO);
979 * Implementation of dt_object_operations::do_object_unlock
981 * Cancel a lock of a remote object.
983 * \param[in] env execution environment
984 * \param[in] dt object to be unlocked
985 * \param[in] einfo lock enqueue information
986 * \param[in] policy lock policy
988 * \retval Only return 0 for now.
990 static int osp_md_object_unlock(const struct lu_env *env,
991 struct dt_object *dt,
992 struct ldlm_enqueue_info *einfo,
993 union ldlm_policy_data *policy)
995 struct lustre_handle *lockh = einfo->ei_cbdata;
998 ldlm_lock_decref(lockh, einfo->ei_mode);
1004 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1006 * Create the dt_update_request to track the update for this OSP
1007 * in the transaction.
1009 * \param[in] env pointer to the thread context
1010 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1011 * \param[in] th pointer to the transaction handler
1013 * \retval 0 for success
1014 * \retval negative error number on failure
1016 int osp_md_declare_destroy(const struct lu_env *env, struct dt_object *dt,
1019 return osp_trans_update_request_create(th);
1023 * Implement OSP layer dt_object_operations::do_destroy() interface.
1025 * Pack the destroy update into the RPC buffer, which will be sent
1026 * to the remote MDT during transaction stop.
1028 * It also marks the object as non-cached.
1030 * \param[in] env pointer to the thread context
1031 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1032 * \param[in] th pointer to the transaction handler
1034 * \retval 0 for success
1035 * \retval negative error number on failure
1037 int osp_md_destroy(const struct lu_env *env, struct dt_object *dt,
1040 struct osp_object *o = dt2osp_obj(dt);
1041 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1042 struct osp_update_request *update;
1046 o->opo_non_exist = 1;
1048 LASSERT(osp->opd_connect_mdt);
1049 update = thandle_to_osp_update_request(th);
1050 LASSERT(update != NULL);
1052 rc = OSP_UPDATE_RPC_PACK(env, out_destroy_pack, update,
1053 lu_object_fid(&dt->do_lu));
1057 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1058 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1064 struct dt_object_operations osp_md_obj_ops = {
1065 .do_read_lock = osp_md_read_lock,
1066 .do_write_lock = osp_md_write_lock,
1067 .do_read_unlock = osp_md_read_unlock,
1068 .do_write_unlock = osp_md_write_unlock,
1069 .do_write_locked = osp_md_write_locked,
1070 .do_declare_create = osp_md_declare_create,
1071 .do_create = osp_md_create,
1072 .do_declare_ref_add = osp_md_declare_ref_add,
1073 .do_ref_add = osp_md_ref_add,
1074 .do_declare_ref_del = osp_md_declare_ref_del,
1075 .do_ref_del = osp_md_ref_del,
1076 .do_declare_destroy = osp_md_declare_destroy,
1077 .do_destroy = osp_md_destroy,
1078 .do_ah_init = osp_md_ah_init,
1079 .do_attr_get = osp_attr_get,
1080 .do_declare_attr_set = osp_md_declare_attr_set,
1081 .do_attr_set = osp_md_attr_set,
1082 .do_xattr_get = osp_xattr_get,
1083 .do_xattr_list = osp_md_xattr_list,
1084 .do_declare_xattr_set = osp_declare_xattr_set,
1085 .do_xattr_set = osp_xattr_set,
1086 .do_declare_xattr_del = osp_declare_xattr_del,
1087 .do_xattr_del = osp_xattr_del,
1088 .do_index_try = osp_md_index_try,
1089 .do_object_lock = osp_md_object_lock,
1090 .do_object_unlock = osp_md_object_unlock,
1091 .do_invalidate = osp_invalidate,
1095 * Implementation of dt_body_operations::dbo_declare_write
1097 * Create the osp_update_request to track the update for this OSP
1098 * in the transaction.
1100 * \param[in] env execution environment
1101 * \param[in] dt object to be written
1102 * \param[in] buf buffer to write which includes an embedded size field
1103 * \param[in] pos offet in the object to start writing at
1104 * \param[in] th transaction handle
1106 * \retval 0 if preparation succeeds.
1107 * \retval negative errno if preparation fails.
1109 static ssize_t osp_md_declare_write(const struct lu_env *env,
1110 struct dt_object *dt,
1111 const struct lu_buf *buf,
1112 loff_t pos, struct thandle *th)
1114 struct osp_device *osp = dt2osp_dev(th->th_dev);
1117 rc = osp_trans_update_request_create(th);
1121 if (osp->opd_update == NULL)
1124 if (dt2osp_obj(dt)->opo_stale)
1131 * Implementation of dt_body_operations::dbo_write
1133 * Pack the write object update into the RPC buffer, which will be sent
1134 * to the remote MDT during transaction stop.
1136 * \param[in] env execution environment
1137 * \param[in] dt object to be written
1138 * \param[in] buf buffer to write which includes an embedded size field
1139 * \param[in] pos offet in the object to start writing at
1140 * \param[in] th transaction handle
1142 * \retval the buffer size in bytes if packing succeeds.
1143 * \retval negative errno if packing fails.
1145 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1146 const struct lu_buf *buf, loff_t *pos,
1149 struct osp_object *obj = dt2osp_obj(dt);
1150 struct osp_update_request *update;
1151 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1155 update = thandle_to_osp_update_request(th);
1156 LASSERT(update != NULL);
1158 CDEBUG(D_INFO, "write "DFID" offset = %llu length = %zu\n",
1159 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1161 rc = OSP_UPDATE_RPC_PACK(env, out_write_pack, update,
1162 lu_object_fid(&dt->do_lu), buf, *pos);
1166 rc = osp_check_and_set_rpc_version(oth, obj);
1170 /* XXX: how about the write error happened later? */
1171 *pos += buf->lb_len;
1173 if (obj->opo_attr.la_valid & LA_SIZE && obj->opo_attr.la_size < *pos)
1174 obj->opo_attr.la_size = *pos;
1176 spin_lock(&obj->opo_lock);
1177 if (list_empty(&obj->opo_invalidate_cb_list)) {
1178 lu_object_get(&obj->opo_obj.do_lu);
1180 list_add_tail(&obj->opo_invalidate_cb_list,
1181 &update->our_invalidate_cb_list);
1183 spin_unlock(&obj->opo_lock);
1185 RETURN(buf->lb_len);
1188 static inline void orr_le_to_cpu(struct out_read_reply *orr_dst,
1189 const struct out_read_reply *orr_src)
1191 orr_dst->orr_size = le32_to_cpu(orr_src->orr_size);
1192 orr_dst->orr_padding = le32_to_cpu(orr_src->orr_padding);
1193 orr_dst->orr_offset = le64_to_cpu(orr_dst->orr_offset);
1198 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1199 struct lu_buf *rbuf, loff_t *pos)
1201 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1202 struct dt_device *dt_dev = &osp->opd_dt_dev;
1203 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1204 char *ptr = rbuf->lb_buf;
1205 struct osp_update_request *update;
1206 struct ptlrpc_request *req = NULL;
1207 struct out_read_reply *orr;
1208 struct ptlrpc_bulk_desc *desc;
1209 struct object_update_reply *reply;
1214 /* Because it needs send the update buffer right away,
1215 * just create an update buffer, instead of attaching the
1216 * update_remote list of the thandle. */
1217 update = osp_update_request_create(dt_dev);
1219 RETURN(PTR_ERR(update));
1221 rc = OSP_UPDATE_RPC_PACK(env, out_read_pack, update,
1222 lu_object_fid(&dt->do_lu),
1223 rbuf->lb_len, *pos);
1225 CERROR("%s: cannot insert update: rc = %d\n",
1226 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1227 GOTO(out_update, rc);
1230 CDEBUG(D_INFO, "%s "DFID" read offset %llu size %zu\n",
1231 dt_dev->dd_lu_dev.ld_obd->obd_name,
1232 PFID(lu_object_fid(&dt->do_lu)), *pos, rbuf->lb_len);
1233 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1236 GOTO(out_update, rc);
1238 /* First *and* last might be partial pages, hence +1 */
1239 pages = DIV_ROUND_UP(rbuf->lb_len, PAGE_SIZE) + 1;
1241 /* allocate bulk descriptor */
1242 desc = ptlrpc_prep_bulk_imp(req, pages, 1,
1243 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1245 &ptlrpc_bulk_kiov_nopin_ops);
1247 GOTO(out, rc = -ENOMEM);
1249 desc->bd_frag_ops->add_iov_frag(desc, ptr, rbuf->lb_len);
1251 osp_set_req_replay(osp, req);
1252 req->rq_bulk_read = 1;
1253 /* send request to master and wait for RPC to complete */
1254 rc = ptlrpc_queue_wait(req);
1258 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1259 req->rq_bulk->bd_nob_transferred);
1263 reply = req_capsule_server_sized_get(&req->rq_pill,
1264 &RMF_OUT_UPDATE_REPLY,
1265 OUT_UPDATE_REPLY_SIZE);
1267 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1268 CERROR("%s: invalid update reply magic %x expected %x:"
1269 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1270 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1271 GOTO(out, rc = -EPROTO);
1274 rc = object_update_result_data_get(reply, lbuf, 0);
1278 if (lbuf->lb_len < sizeof(*orr))
1279 GOTO(out, rc = -EPROTO);
1282 orr_le_to_cpu(orr, orr);
1284 *pos = orr->orr_offset;
1286 ptlrpc_req_finished(req);
1289 osp_update_request_destroy(env, update);
1294 /* These body operation will be used to write symlinks during migration etc */
1295 struct dt_body_operations osp_md_body_ops = {
1296 .dbo_declare_write = osp_md_declare_write,
1297 .dbo_write = osp_md_write,
1298 .dbo_read = osp_md_read,