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 struct osp_device *osp = lu2osp_dev(obj->opo_obj.do_lu.lo_dev);
88 spin_lock(&obj->opo_lock);
89 if (rc != 0 && rc != -EEXIST) {
90 obj->opo_obj.do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
91 obj->opo_non_exist = 1;
93 obj->opo_creating = 0;
94 spin_unlock(&obj->opo_lock);
97 * invalidate opo cache for the object after the object is created, so
98 * attr_get will try to get attr from remote object.
100 osp_obj_invalidate_cache(obj);
103 * currently reads from objects being created
104 * are exceptional - during recovery only, when
105 * remote llog update fetching can race with
106 * orphan cleanup. so don't waste memory adding
107 * a wait queue to every osp object
109 wake_up_all(&osp->opd_out_waitq);
115 * Implementation of dt_object_operations::do_declare_create
117 * Create the osp_update_request to track the update for this OSP
118 * in the transaction.
120 * \param[in] env execution environment
121 * \param[in] dt remote object to be created
122 * \param[in] attr attribute of the created object
123 * \param[in] hint creation hint
124 * \param[in] dof creation format information
125 * \param[in] th the transaction handle
127 * \retval 0 if preparation succeeds.
128 * \retval negative errno if preparation fails.
130 int osp_md_declare_create(const struct lu_env *env, struct dt_object *dt,
131 struct lu_attr *attr, struct dt_allocation_hint *hint,
132 struct dt_object_format *dof, struct thandle *th)
134 return osp_trans_update_request_create(th);
137 struct object_update *
138 update_buffer_get_update(struct object_update_request *request,
144 if (index > request->ourq_count)
147 ptr = &request->ourq_updates[0];
148 for (i = 0; i < index; i++)
149 ptr += object_update_size(ptr);
155 * Implementation of dt_object_operations::do_create
157 * It adds an OUT_CREATE sub-request into the OUT RPC that will be flushed
158 * when the transaction stop, and sets necessary flags for created object.
160 * \param[in] env execution environment
161 * \param[in] dt object to be created
162 * \param[in] attr attribute of the created object
163 * \param[in] hint creation hint
164 * \param[in] dof creation format information
165 * \param[in] th the transaction handle
167 * \retval 0 if packing creation succeeds.
168 * \retval negative errno if packing creation fails.
170 int osp_md_create(const struct lu_env *env, struct dt_object *dt,
171 struct lu_attr *attr, struct dt_allocation_hint *hint,
172 struct dt_object_format *dof, struct thandle *th)
174 struct osp_update_request *update;
175 struct osp_object *obj = dt2osp_obj(dt);
178 update = thandle_to_osp_update_request(th);
179 LASSERT(update != NULL);
181 if (!(attr->la_valid & LA_TYPE)) {
182 CERROR("%s: create type not specified: valid %llx\n",
183 dt->do_lu.lo_dev->ld_obd->obd_name, attr->la_valid);
184 GOTO(out, rc = -EINVAL);
187 rc = OSP_UPDATE_RPC_PACK(env, out_create_pack, update,
188 lu_object_fid(&dt->do_lu), attr, hint, dof);
192 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
193 osp_create_interpreter);
198 spin_lock(&obj->opo_lock);
199 obj->opo_creating = 1;
200 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
201 dt2osp_obj(dt)->opo_non_exist = 0;
203 spin_unlock(&obj->opo_lock);
205 obj->opo_attr = *attr;
211 * Implementation of dt_object_operations::do_declare_ref_del
213 * Create the osp_update_request to track the update for this OSP
214 * in the transaction.
216 * \param[in] env execution environment
217 * \param[in] dt object to decrease the reference count.
218 * \param[in] th the transaction handle of refcount decrease.
220 * \retval 0 if preparation succeeds.
221 * \retval negative errno if preparation fails.
223 static int osp_md_declare_ref_del(const struct lu_env *env,
224 struct dt_object *dt, struct thandle *th)
226 return osp_trans_update_request_create(th);
230 * Implementation of dt_object_operations::do_ref_del
232 * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
233 * flushed when the transaction stop.
235 * \param[in] env execution environment
236 * \param[in] dt object to decrease the reference count
237 * \param[in] th the transaction handle
239 * \retval 0 if packing ref_del succeeds.
240 * \retval negative errno if packing fails.
242 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
245 struct osp_update_request *update;
248 update = thandle_to_osp_update_request(th);
249 LASSERT(update != NULL);
251 rc = OSP_UPDATE_RPC_PACK(env, out_ref_del_pack, update,
252 lu_object_fid(&dt->do_lu));
257 * Implementation of dt_object_operations::do_declare_ref_del
259 * Create the osp_update_request to track the update for this OSP
260 * in the transaction.
262 * \param[in] env execution environment
263 * \param[in] dt object on which to increase the reference count.
264 * \param[in] th the transaction handle.
266 * \retval 0 if preparation succeeds.
267 * \retval negative errno if preparation fails.
269 static int osp_md_declare_ref_add(const struct lu_env *env,
270 struct dt_object *dt, struct thandle *th)
272 return osp_trans_update_request_create(th);
276 * Implementation of dt_object_operations::do_ref_add
278 * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
279 * when the transaction stop.
281 * \param[in] env execution environment
282 * \param[in] dt object on which to increase the reference count
283 * \param[in] th the transaction handle
285 * \retval 0 if packing ref_add succeeds.
286 * \retval negative errno if packing fails.
288 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
291 struct osp_update_request *update;
294 update = thandle_to_osp_update_request(th);
295 LASSERT(update != NULL);
297 rc = OSP_UPDATE_RPC_PACK(env, out_ref_add_pack, update,
298 lu_object_fid(&dt->do_lu));
303 * Implementation of dt_object_operations::do_ah_init
305 * Initialize the allocation hint for object creation, which is usually called
306 * before the creation, and these hints (parent and child mode) will be sent to
307 * the remote Object Update Target (OUT) and used in the object create process,
308 * same as OSD object creation.
310 * \param[in] env execution environment
311 * \param[in] ah the hint to be initialized
312 * \param[in] parent the parent of the object
313 * \param[in] child the object to be created
314 * \param[in] child_mode the mode of the created object
316 static void osp_md_ah_init(const struct lu_env *env,
317 struct dt_allocation_hint *ah,
318 struct dt_object *parent,
319 struct dt_object *child,
324 ah->dah_parent = parent;
328 * Implementation of dt_object_operations::do_declare_attr_get
330 * Create the osp_update_request to track the update for this OSP
331 * in the transaction.
333 * \param[in] env execution environment
334 * \param[in] dt object on which to set attributes
335 * \param[in] attr attributes to be set
336 * \param[in] th the transaction handle
338 * \retval 0 if preparation succeeds.
339 * \retval negative errno if preparation fails.
341 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
342 const struct lu_attr *attr, struct thandle *th)
344 return osp_trans_update_request_create(th);
348 * Implementation of dt_object_operations::do_attr_set
350 * Set attributes to the specified remote object.
352 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
353 * when the transaction stop.
355 * \param[in] env execution environment
356 * \param[in] dt object to set attributes
357 * \param[in] attr attributes to be set
358 * \param[in] th the transaction handle
360 * \retval 0 if packing attr_set succeeds.
361 * \retval negative errno if packing fails.
363 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
364 const struct lu_attr *attr, struct thandle *th)
366 struct osp_update_request *update;
369 update = thandle_to_osp_update_request(th);
370 LASSERT(update != NULL);
372 rc = OSP_UPDATE_RPC_PACK(env, out_attr_set_pack, update,
373 lu_object_fid(&dt->do_lu), attr);
378 * Implementation of dt_object_operations::do_read_lock
380 * osp_md_{read,write}_lock() will only lock the remote object in the
381 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
382 * lock the object. Note: it will not lock the object in the whole cluster,
383 * which relies on the LDLM lock.
385 * \param[in] env execution environment
386 * \param[in] dt object to be locked
387 * \param[in] role lock role from MDD layer, see dt_object_role().
389 static void osp_md_read_lock(const struct lu_env *env, struct dt_object *dt,
392 struct osp_object *obj = dt2osp_obj(dt);
394 LASSERT(obj->opo_owner != env);
395 down_read_nested(&obj->opo_sem, role);
397 LASSERT(obj->opo_owner == NULL);
401 * Implementation of dt_object_operations::do_write_lock
403 * Lock the remote object in write mode.
405 * \param[in] env execution environment
406 * \param[in] dt object to be locked
407 * \param[in] role lock role from MDD layer, see dt_object_role().
409 static void osp_md_write_lock(const struct lu_env *env, struct dt_object *dt,
412 struct osp_object *obj = dt2osp_obj(dt);
414 down_write_nested(&obj->opo_sem, role);
416 LASSERT(obj->opo_owner == NULL);
417 obj->opo_owner = env;
421 * Implementation of dt_object_operations::do_read_unlock
423 * Unlock the read lock of remote object.
425 * \param[in] env execution environment
426 * \param[in] dt object to be unlocked
428 static void osp_md_read_unlock(const struct lu_env *env, struct dt_object *dt)
430 struct osp_object *obj = dt2osp_obj(dt);
432 up_read(&obj->opo_sem);
436 * Implementation of dt_object_operations::do_write_unlock
438 * Unlock the write lock of remote object.
440 * \param[in] env execution environment
441 * \param[in] dt object to be unlocked
443 static void osp_md_write_unlock(const struct lu_env *env, struct dt_object *dt)
445 struct osp_object *obj = dt2osp_obj(dt);
447 LASSERT(obj->opo_owner == env);
448 obj->opo_owner = NULL;
449 up_write(&obj->opo_sem);
453 * Implementation of dt_object_operations::do_write_locked
455 * Test if the object is locked in write mode.
457 * \param[in] env execution environment
458 * \param[in] dt object to be tested
460 static int osp_md_write_locked(const struct lu_env *env, struct dt_object *dt)
462 struct osp_object *obj = dt2osp_obj(dt);
464 return obj->opo_owner == env;
468 * Implementation of dt_index_operations::dio_lookup
470 * Look up record by key under a remote index object. It packs lookup update
471 * into RPC, sends to the remote OUT and waits for the lookup result.
473 * \param[in] env execution environment
474 * \param[in] dt index object to lookup
475 * \param[out] rec record in which to return lookup result
476 * \param[in] key key of index which will be looked up
478 * \retval 1 if the lookup succeeds.
479 * \retval negative errno if the lookup fails.
481 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
482 struct dt_rec *rec, const struct dt_key *key)
484 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
485 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
486 struct dt_device *dt_dev = &osp->opd_dt_dev;
487 struct osp_update_request *update;
488 struct object_update_reply *reply;
489 struct ptlrpc_request *req = NULL;
494 /* Because it needs send the update buffer right away,
495 * just create an update buffer, instead of attaching the
496 * update_remote list of the thandle.
498 update = osp_update_request_create(dt_dev);
500 RETURN(PTR_ERR(update));
502 rc = OSP_UPDATE_RPC_PACK(env, out_index_lookup_pack, update,
503 lu_object_fid(&dt->do_lu), rec, key);
505 CERROR("%s: Insert update error: rc = %d\n",
506 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
510 rc = osp_remote_sync(env, osp, update, &req);
514 reply = req_capsule_server_sized_get(&req->rq_pill,
515 &RMF_OUT_UPDATE_REPLY,
516 OUT_UPDATE_REPLY_SIZE);
517 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
518 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
519 dt_dev->dd_lu_dev.ld_obd->obd_name,
520 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
521 GOTO(out, rc = -EPROTO);
524 rc = object_update_result_data_get(reply, lbuf, 0);
528 if (lbuf->lb_len != sizeof(*fid)) {
529 CERROR("%s: lookup "DFID" %s wrong size %d\n",
530 dt_dev->dd_lu_dev.ld_obd->obd_name,
531 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
533 GOTO(out, rc = -EINVAL);
537 if (req_capsule_rep_need_swab(&req->rq_pill))
538 lustre_swab_lu_fid(fid);
539 if (!fid_is_sane(fid)) {
540 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
541 dt_dev->dd_lu_dev.ld_obd->obd_name,
542 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
543 GOTO(out, rc = -EINVAL);
546 memcpy(rec, fid, sizeof(*fid));
552 ptlrpc_req_finished(req);
554 osp_update_request_destroy(env, update);
560 * Implementation of dt_index_operations::dio_declare_insert
562 * Create the osp_update_request to track the update for this OSP
563 * in the transaction.
565 * \param[in] env execution environment
566 * \param[in] dt object for which to insert index
567 * \param[in] rec record of the index which will be inserted
568 * \param[in] key key of the index which will be inserted
569 * \param[in] th the transaction handle
571 * \retval 0 if preparation succeeds.
572 * \retval negative errno if preparation fails.
574 static int osp_md_declare_index_insert(const struct lu_env *env,
575 struct dt_object *dt,
576 const struct dt_rec *rec,
577 const struct dt_key *key,
580 return osp_trans_update_request_create(th);
584 * Implementation of dt_index_operations::dio_insert
586 * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
587 * be flushed when the transaction stop.
589 * \param[in] env execution environment
590 * \param[in] dt object for which to insert index
591 * \param[in] rec record of the index to be inserted
592 * \param[in] key key of the index to be inserted
593 * \param[in] th the transaction handle
595 * \retval 0 if packing index insert succeeds.
596 * \retval negative errno if packing fails.
598 static int osp_md_index_insert(const struct lu_env *env, struct dt_object *dt,
599 const struct dt_rec *rec,
600 const struct dt_key *key, struct thandle *th)
602 struct osp_update_request *update;
605 update = thandle_to_osp_update_request(th);
606 LASSERT(update != NULL);
608 rc = OSP_UPDATE_RPC_PACK(env, out_index_insert_pack, update,
609 lu_object_fid(&dt->do_lu), rec, key);
614 * Implementation of dt_index_operations::dio_declare_delete
616 * Create the osp_update_request to track the update for this OSP
617 * in the transaction.
619 * \param[in] env execution environment
620 * \param[in] dt object for which to delete index
621 * \param[in] key key of the index
622 * \param[in] th the transaction handle
624 * \retval 0 if preparation succeeds.
625 * \retval negative errno if preparation fails.
627 static int osp_md_declare_index_delete(const struct lu_env *env,
628 struct dt_object *dt,
629 const struct dt_key *key,
632 return osp_trans_update_request_create(th);
636 * Implementation of dt_index_operations::dio_delete
638 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
639 * be flushed when the transaction stop.
641 * \param[in] env execution environment
642 * \param[in] dt object for which to delete index
643 * \param[in] key key of the index which will be deleted
644 * \param[in] th the transaction handle
646 * \retval 0 if packing index delete succeeds.
647 * \retval negative errno if packing fails.
649 static int osp_md_index_delete(const struct lu_env *env,
650 struct dt_object *dt,
651 const struct dt_key *key,
654 struct osp_update_request *update;
657 update = thandle_to_osp_update_request(th);
658 LASSERT(update != NULL);
660 rc = OSP_UPDATE_RPC_PACK(env, out_index_delete_pack, update,
661 lu_object_fid(&dt->do_lu), key);
667 * Implementation of dt_index_operations::dio_it.next
669 * Advance the pointer of the iterator to the next entry. It shares a similar
670 * internal implementation with osp_orphan_it_next(), which is being used for
671 * remote orphan index object. This method will be used for remote directory.
673 * \param[in] env execution environment
674 * \param[in] di iterator of this iteration
676 * \retval 0 if the pointer is advanced successfully.
677 * \retval 1 if it reaches to the end of the index object.
678 * \retval negative errno if the pointer cannot be advanced.
680 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
682 struct osp_it *it = (struct osp_it *)di;
683 struct lu_idxpage *idxpage;
684 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
689 idxpage = it->ooi_cur_idxpage;
690 if (idxpage != NULL) {
691 if (idxpage->lip_nr == 0)
697 (struct lu_dirent *)idxpage->lip_entries;
699 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
700 it->ooi_pos_ent < idxpage->lip_nr) {
701 ent = (struct lu_dirent *)(((char *)ent) +
702 le16_to_cpu(ent->lde_reclen));
710 rc = osp_it_next_page(env, di);
718 * Implementation of dt_index_operations::dio_it.key
720 * Get the key at current iterator poisiton. These iteration methods
721 * (dio_it) will only be used for iterating the remote directory, so
722 * the key is the name of the directory entry.
724 * \param[in] env execution environment
725 * \param[in] di iterator of this iteration
727 * \retval name of the current entry
729 static struct dt_key *osp_it_key(const struct lu_env *env,
730 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 (struct dt_key *)ent->lde_name;
739 * Implementation of dt_index_operations::dio_it.key_size
741 * Get the key size at current iterator poisiton. These iteration methods
742 * (dio_it) will only be used for iterating the remote directory, so the key
743 * size is the name size of the directory entry.
745 * \param[in] env execution environment
746 * \param[in] di iterator of this iteration
748 * \retval name size of the current entry
751 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
753 struct osp_it *it = (struct osp_it *)di;
754 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
756 return (int)le16_to_cpu(ent->lde_namelen);
760 * Implementation of dt_index_operations::dio_it.rec
762 * Get the record at current iterator position. These iteration methods
763 * (dio_it) will only be used for iterating the remote directory, so it
764 * uses lu_dirent_calc_size() to calculate the record size.
766 * \param[in] env execution environment
767 * \param[in] di iterator of this iteration
768 * \param[out] rec the record to be returned
769 * \param[in] attr attributes of the index object, so it knows
770 * how to pack the entry.
772 * \retval only return 0 for now
774 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
775 struct dt_rec *rec, __u32 attr)
777 struct osp_it *it = (struct osp_it *)di;
778 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
781 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
782 memcpy(rec, ent, reclen);
787 * Implementation of dt_index_operations::dio_it.load
789 * Locate the iteration cursor to the specified position (cookie).
791 * \param[in] env pointer to the thread context
792 * \param[in] di pointer to the iteration structure
793 * \param[in] hash the specified position
795 * \retval positive number for locating to the exactly position
797 * \retval 0 for arriving at the end of the iteration
798 * \retval negative error number on failure
800 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
803 struct osp_it *it = (struct osp_it *)di;
807 rc = osp_md_index_it_next(env, (struct dt_it *)di);
817 const struct dt_index_operations osp_md_index_ops = {
818 .dio_lookup = osp_md_index_lookup,
819 .dio_declare_insert = osp_md_declare_index_insert,
820 .dio_insert = osp_md_index_insert,
821 .dio_declare_delete = osp_md_declare_index_delete,
822 .dio_delete = osp_md_index_delete,
828 .next = osp_md_index_it_next,
830 .key_size = osp_it_key_size,
831 .rec = osp_md_index_it_rec,
832 .store = osp_it_store,
834 .key_rec = osp_it_key_rec,
839 * Implement OSP layer dt_object_operations::do_xattr_list() interface.
841 * List extended attribute from the specified MDT/OST object, result is not
842 * cached because this is called by directory migration only.
844 * \param[in] env pointer to the thread context
845 * \param[in] dt pointer to the OSP layer dt_object
846 * \param[out] buf pointer to the lu_buf to hold the extended attribute
848 * \retval positive bytes used/required in the buffer
849 * \retval negative error number on failure
851 static int osp_md_xattr_list(const struct lu_env *env, struct dt_object *dt,
852 const struct lu_buf *buf)
854 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
855 struct osp_object *obj = dt2osp_obj(dt);
856 struct dt_device *dev = &osp->opd_dt_dev;
857 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
858 struct osp_update_request *update = NULL;
859 struct ptlrpc_request *req = NULL;
860 struct object_update_reply *reply;
861 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
868 if (unlikely(obj->opo_non_exist))
871 update = osp_update_request_create(dev);
873 RETURN(PTR_ERR(update));
875 rc = OSP_UPDATE_RPC_PACK(env, out_xattr_list_pack, update,
876 lu_object_fid(&dt->do_lu), buf->lb_len);
878 CERROR("%s: Insert update error "DFID": rc = %d\n",
879 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
883 rc = osp_remote_sync(env, osp, update, &req);
886 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
887 obj->opo_non_exist = 1;
892 reply = req_capsule_server_sized_get(&req->rq_pill,
893 &RMF_OUT_UPDATE_REPLY,
894 OUT_UPDATE_REPLY_SIZE);
895 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
896 DEBUG_REQ(D_ERROR, req,
897 "%s: Wrong version %x expected %x "DFID": rc = %d",
898 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
899 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
901 GOTO(out, rc = -EPROTO);
904 rc = object_update_result_data_get(reply, rbuf, 0);
911 if (unlikely(buf->lb_len < rbuf->lb_len))
912 GOTO(out, rc = -ERANGE);
914 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
919 ptlrpc_req_finished(req);
921 if (update && !IS_ERR(update))
922 osp_update_request_destroy(env, update);
928 * Implementation of dt_object_operations::do_index_try
930 * Try to initialize the index API pointer for the given object. This
931 * is the entry point of the index API, i.e. we must call this method
932 * to initialize the index object before calling other index methods.
934 * \param[in] env execution environment
935 * \param[in] dt index object to be initialized
936 * \param[in] feat the index feature of the object
938 * \retval 0 if the initialization succeeds.
939 * \retval negative errno if the initialization fails.
941 static int osp_md_index_try(const struct lu_env *env,
942 struct dt_object *dt,
943 const struct dt_index_features *feat)
945 dt->do_index_ops = &osp_md_index_ops;
950 * Implementation of dt_object_operations::do_object_lock
952 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
953 * which will lock the object in the global namespace. And because the
954 * cross-MDT locks are relatively rare compared with normal local MDT operation,
955 * let's release it right away, instead of putting it into the LRU list.
957 * \param[in] env execution environment
958 * \param[in] dt object to be locked
959 * \param[out] lh lock handle
960 * \param[in] einfo enqueue information
961 * \param[in] policy lock policy
963 * \retval ELDLM_OK if locking the object succeeds.
964 * \retval negative errno if locking fails.
966 static int osp_md_object_lock(const struct lu_env *env,
967 struct dt_object *dt,
968 struct lustre_handle *lh,
969 struct ldlm_enqueue_info *einfo,
970 union ldlm_policy_data *policy)
972 struct ldlm_res_id *res_id;
973 struct osp_device *osp = dt2osp_dev(lu2dt_dev(dt->do_lu.lo_dev));
974 struct ptlrpc_request *req;
976 __u64 flags = LDLM_FL_NO_LRU;
979 res_id = einfo->ei_res_id;
980 LASSERT(res_id != NULL);
982 req = ldlm_enqueue_pack(osp->opd_exp, 0);
984 RETURN(PTR_ERR(req));
986 osp_set_req_replay(osp, req);
987 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
988 (const union ldlm_policy_data *)policy, &flags,
989 NULL, 0, LVB_T_NONE, lh, 0);
991 ptlrpc_req_finished(req);
993 RETURN(rc == ELDLM_OK ? 0 : -EIO);
997 * Implementation of dt_object_operations::do_object_unlock
999 * Cancel a lock of a remote object.
1001 * \param[in] env execution environment
1002 * \param[in] dt object to be unlocked
1003 * \param[in] einfo lock enqueue information
1004 * \param[in] policy lock policy
1006 * \retval Only return 0 for now.
1008 static int osp_md_object_unlock(const struct lu_env *env,
1009 struct dt_object *dt,
1010 struct ldlm_enqueue_info *einfo,
1011 union ldlm_policy_data *policy)
1013 struct lustre_handle *lockh = einfo->ei_cbdata;
1015 /* unlock finally */
1016 ldlm_lock_decref(lockh, einfo->ei_mode);
1022 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1024 * Create the dt_update_request to track the update for this OSP
1025 * in the transaction.
1027 * \param[in] env pointer to the thread context
1028 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1029 * \param[in] th pointer to the transaction handler
1031 * \retval 0 for success
1032 * \retval negative error number on failure
1034 static int osp_md_declare_destroy(const struct lu_env *env,
1035 struct dt_object *dt,
1038 return osp_trans_update_request_create(th);
1041 static int osp_destroy_interpreter(const struct lu_env *env,
1042 struct object_update_reply *reply,
1043 struct ptlrpc_request *req,
1044 struct osp_object *obj,
1045 void *data, int index, int rc)
1051 * Implement OSP layer dt_object_operations::do_destroy() interface.
1053 * Pack the destroy update into the RPC buffer, which will be sent
1054 * to the remote MDT during transaction stop.
1056 * It also marks the object as non-cached.
1058 * \param[in] env pointer to the thread context
1059 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1060 * \param[in] th pointer to the transaction handler
1062 * \retval 0 for success
1063 * \retval negative error number on failure
1065 static int osp_md_destroy(const struct lu_env *env, struct dt_object *dt,
1068 struct osp_object *o = dt2osp_obj(dt);
1069 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1070 struct osp_update_request *update;
1071 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1075 o->opo_non_exist = 1;
1076 o->opo_destroyed = 1;
1078 LASSERT(osp->opd_connect_mdt);
1079 update = thandle_to_osp_update_request(th);
1080 LASSERT(update != NULL);
1082 rc = OSP_UPDATE_RPC_PACK(env, out_destroy_pack, update,
1083 lu_object_fid(&dt->do_lu));
1088 * the object can be stale (due to lost LDLM lock), but
1089 * we still want to destroy it
1091 osp_check_and_set_rpc_version(oth, o);
1093 /* retain the object and it's status until it's destroyed on remote */
1094 rc = osp_insert_update_callback(env, update, o, NULL,
1095 osp_destroy_interpreter);
1099 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1100 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1106 const struct dt_object_operations osp_md_obj_ops = {
1107 .do_read_lock = osp_md_read_lock,
1108 .do_write_lock = osp_md_write_lock,
1109 .do_read_unlock = osp_md_read_unlock,
1110 .do_write_unlock = osp_md_write_unlock,
1111 .do_write_locked = osp_md_write_locked,
1112 .do_declare_create = osp_md_declare_create,
1113 .do_create = osp_md_create,
1114 .do_declare_ref_add = osp_md_declare_ref_add,
1115 .do_ref_add = osp_md_ref_add,
1116 .do_declare_ref_del = osp_md_declare_ref_del,
1117 .do_ref_del = osp_md_ref_del,
1118 .do_declare_destroy = osp_md_declare_destroy,
1119 .do_destroy = osp_md_destroy,
1120 .do_ah_init = osp_md_ah_init,
1121 .do_attr_get = osp_attr_get,
1122 .do_declare_attr_set = osp_md_declare_attr_set,
1123 .do_attr_set = osp_md_attr_set,
1124 .do_xattr_get = osp_xattr_get,
1125 .do_xattr_list = osp_md_xattr_list,
1126 .do_declare_xattr_set = osp_declare_xattr_set,
1127 .do_xattr_set = osp_xattr_set,
1128 .do_declare_xattr_del = osp_declare_xattr_del,
1129 .do_xattr_del = osp_xattr_del,
1130 .do_index_try = osp_md_index_try,
1131 .do_object_lock = osp_md_object_lock,
1132 .do_object_unlock = osp_md_object_unlock,
1133 .do_invalidate = osp_invalidate,
1134 .do_check_stale = osp_check_stale,
1138 * Implementation of dt_body_operations::dbo_declare_write
1140 * Create the osp_update_request to track the update for this OSP
1141 * in the transaction.
1143 * \param[in] env execution environment
1144 * \param[in] dt object to be written
1145 * \param[in] buf buffer to write which includes an embedded size field
1146 * \param[in] pos offet in the object to start writing at
1147 * \param[in] th transaction handle
1149 * \retval 0 if preparation succeeds.
1150 * \retval negative errno if preparation fails.
1152 static ssize_t osp_md_declare_write(const struct lu_env *env,
1153 struct dt_object *dt,
1154 const struct lu_buf *buf,
1155 loff_t pos, struct thandle *th)
1157 struct osp_device *osp = dt2osp_dev(th->th_dev);
1160 if (dt2osp_obj(dt)->opo_destroyed)
1163 rc = osp_trans_update_request_create(th);
1167 if (osp->opd_update == NULL)
1170 if (dt2osp_obj(dt)->opo_stale)
1176 static int osp_write_interpreter(const struct lu_env *env,
1177 struct object_update_reply *reply,
1178 struct ptlrpc_request *req,
1179 struct osp_object *obj,
1180 void *data, int index, int rc)
1182 struct osp_device *osp = lu2osp_dev(obj->opo_obj.do_lu.lo_dev);
1185 CDEBUG(D_HA, "error "DFID": rc = %d\n",
1186 PFID(lu_object_fid(&obj->opo_obj.do_lu)), rc);
1187 CFS_RACE(OBD_FAIL_OUT_OBJECT_MISS);
1188 spin_lock(&obj->opo_lock);
1189 obj->opo_attr.la_valid = 0;
1191 spin_unlock(&obj->opo_lock);
1193 if (atomic_dec_and_test(&obj->opo_writes_in_flight))
1194 wake_up_all(&osp->opd_out_waitq);
1199 * Implementation of dt_body_operations::dbo_write
1201 * Pack the write object update into the RPC buffer, which will be sent
1202 * to the remote MDT during transaction stop.
1204 * \param[in] env execution environment
1205 * \param[in] dt object to be written
1206 * \param[in] buf buffer to write which includes an embedded size field
1207 * \param[in] pos offet in the object to start writing at
1208 * \param[in] th transaction handle
1210 * \retval the buffer size in bytes if packing succeeds.
1211 * \retval negative errno if packing fails.
1213 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1214 const struct lu_buf *buf, loff_t *pos,
1217 struct osp_object *obj = dt2osp_obj(dt);
1218 struct osp_update_request *update;
1219 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1223 if (obj->opo_destroyed)
1226 update = thandle_to_osp_update_request(th);
1227 LASSERT(update != NULL);
1229 CDEBUG(D_INFO, "write "DFID" offset = %llu length = %zu\n",
1230 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1232 rc = OSP_UPDATE_RPC_PACK(env, out_write_pack, update,
1233 lu_object_fid(&dt->do_lu), buf, *pos);
1237 rc = osp_check_and_set_rpc_version(oth, obj);
1241 /* to be able to invalidate object's state in case of an error */
1242 rc = osp_insert_update_callback(env, update, obj, NULL,
1243 osp_write_interpreter);
1247 /* XXX: how about the write error happened later? */
1248 *pos += buf->lb_len;
1250 if (obj->opo_attr.la_valid & LA_SIZE && obj->opo_attr.la_size < *pos)
1251 obj->opo_attr.la_size = *pos;
1253 spin_lock(&obj->opo_lock);
1254 if (list_empty(&obj->opo_invalidate_cb_list)) {
1255 lu_object_get(&obj->opo_obj.do_lu);
1257 list_add_tail(&obj->opo_invalidate_cb_list,
1258 &update->our_invalidate_cb_list);
1260 spin_unlock(&obj->opo_lock);
1262 atomic_inc(&obj->opo_writes_in_flight);
1264 RETURN(buf->lb_len);
1267 static inline void orr_le_to_cpu(struct out_read_reply *orr_dst,
1268 const struct out_read_reply *orr_src)
1270 orr_dst->orr_size = le32_to_cpu(orr_src->orr_size);
1271 orr_dst->orr_padding = le32_to_cpu(orr_src->orr_padding);
1272 orr_dst->orr_offset = le64_to_cpu(orr_dst->orr_offset);
1275 static int osp_md_check_creating(struct osp_object *obj)
1279 spin_lock(&obj->opo_lock);
1280 rc = obj->opo_creating;
1281 spin_unlock(&obj->opo_lock);
1286 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1287 struct lu_buf *rbuf, loff_t *pos)
1289 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1290 struct dt_device *dt_dev = &osp->opd_dt_dev;
1291 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1292 char *ptr = rbuf->lb_buf;
1293 struct osp_update_request *update;
1294 struct ptlrpc_request *req = NULL;
1295 struct out_read_reply *orr;
1296 struct ptlrpc_bulk_desc *desc;
1297 struct object_update_reply *reply;
1302 if (dt2osp_obj(dt)->opo_destroyed)
1305 wait_event_idle(osp->opd_out_waitq,
1306 !atomic_read(&dt2osp_obj(dt)->opo_writes_in_flight) &&
1307 osp_md_check_creating(dt2osp_obj(dt)) == 0);
1309 /* Because it needs send the update buffer right away,
1310 * just create an update buffer, instead of attaching the
1311 * update_remote list of the thandle. */
1312 update = osp_update_request_create(dt_dev);
1314 RETURN(PTR_ERR(update));
1316 rc = OSP_UPDATE_RPC_PACK(env, out_read_pack, update,
1317 lu_object_fid(&dt->do_lu),
1318 rbuf->lb_len, *pos);
1320 CERROR("%s: cannot insert update: rc = %d\n",
1321 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1322 GOTO(out_update, rc);
1325 CDEBUG(D_INFO, "%s "DFID" read offset %llu size %zu\n",
1326 dt_dev->dd_lu_dev.ld_obd->obd_name,
1327 PFID(lu_object_fid(&dt->do_lu)), *pos, rbuf->lb_len);
1328 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1331 GOTO(out_update, rc);
1333 /* First *and* last might be partial pages, hence +1 */
1334 pages = DIV_ROUND_UP(rbuf->lb_len, PAGE_SIZE) + 1;
1336 /* allocate bulk descriptor */
1337 desc = ptlrpc_prep_bulk_imp(req, pages, 1,
1338 PTLRPC_BULK_PUT_SINK,
1340 &ptlrpc_bulk_kiov_nopin_ops);
1342 GOTO(out, rc = -ENOMEM);
1344 desc->bd_frag_ops->add_iov_frag(desc, ptr, rbuf->lb_len);
1346 osp_set_req_replay(osp, req);
1347 req->rq_bulk_read = 1;
1348 /* send request to master and wait for RPC to complete */
1349 rc = ptlrpc_queue_wait(req);
1353 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1354 req->rq_bulk->bd_nob_transferred);
1358 reply = req_capsule_server_sized_get(&req->rq_pill,
1359 &RMF_OUT_UPDATE_REPLY,
1360 OUT_UPDATE_REPLY_SIZE);
1362 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1363 CERROR("%s: invalid update reply magic %x expected %x:"
1364 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1365 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1366 GOTO(out, rc = -EPROTO);
1369 rc = object_update_result_data_get(reply, lbuf, 0);
1373 if (lbuf->lb_len < sizeof(*orr))
1374 GOTO(out, rc = -EPROTO);
1377 orr_le_to_cpu(orr, orr);
1379 *pos = orr->orr_offset;
1381 ptlrpc_req_finished(req);
1384 osp_update_request_destroy(env, update);
1389 /* These body operation will be used to write symlinks during migration etc */
1390 const struct dt_body_operations osp_md_body_ops = {
1391 .dbo_declare_write = osp_md_declare_write,
1392 .dbo_write = osp_md_write,
1393 .dbo_read = osp_md_read,