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, 2015, 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 <lustre_log.h>
57 #include "osp_internal.h"
59 #define OUT_UPDATE_BUFFER_SIZE_ADD 4096
60 #define OUT_UPDATE_BUFFER_SIZE_MAX (256 * 4096) /* 1M update size now */
63 * Interpreter call for object creation
65 * Object creation interpreter, which will be called after creating
66 * the remote object to set flags and status.
68 * \param[in] env execution environment
69 * \param[in] reply update reply
70 * \param[in] req ptlrpc update request for creating object
71 * \param[in] obj object to be created
72 * \param[in] data data used in this function.
73 * \param[in] index index(position) of create update in the whole
75 * \param[in] rc update result on the remote MDT.
77 * \retval only return 0 for now
79 static int osp_object_create_interpreter(const struct lu_env *env,
80 struct object_update_reply *reply,
81 struct ptlrpc_request *req,
82 struct osp_object *obj,
83 void *data, int index, int rc)
86 obj->opo_obj.do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
87 obj->opo_non_exist = 1;
90 /* Invalid the opo cache for the object after the object
91 * is being created, so attr_get will try to get attr
92 * from the remote object. XXX this can be improved when
93 * we have object lock/cache invalidate mechanism in OSP
95 if (obj->opo_ooa != NULL) {
96 spin_lock(&obj->opo_lock);
97 obj->opo_ooa->ooa_attr.la_valid = 0;
98 spin_unlock(&obj->opo_lock);
105 * Implementation of dt_object_operations::do_declare_create
107 * Create the osp_update_request to track the update for this OSP
108 * in the transaction.
110 * \param[in] env execution environment
111 * \param[in] dt remote object to be created
112 * \param[in] attr attribute of the created object
113 * \param[in] hint creation hint
114 * \param[in] dof creation format information
115 * \param[in] th the transaction handle
117 * \retval 0 if preparation succeeds.
118 * \retval negative errno if preparation fails.
120 int osp_md_declare_object_create(const struct lu_env *env,
121 struct dt_object *dt,
122 struct lu_attr *attr,
123 struct dt_allocation_hint *hint,
124 struct dt_object_format *dof,
127 struct osp_object *obj = dt2osp_obj(dt);
130 if (obj->opo_ooa == NULL) {
131 rc = osp_oac_init(obj);
136 return osp_trans_update_request_create(th);
139 struct object_update *
140 update_buffer_get_update(struct object_update_request *request,
146 if (index > request->ourq_count)
149 ptr = &request->ourq_updates[0];
150 for (i = 0; i < index; i++)
151 ptr += object_update_size(ptr);
157 * Implementation of dt_object_operations::do_create
159 * It adds an OUT_CREATE sub-request into the OUT RPC that will be flushed
160 * when the transaction stop, and sets necessary flags for created object.
162 * \param[in] env execution environment
163 * \param[in] dt object to be created
164 * \param[in] attr attribute of the created object
165 * \param[in] hint creation hint
166 * \param[in] dof creation format information
167 * \param[in] th the transaction handle
169 * \retval 0 if packing creation succeeds.
170 * \retval negative errno if packing creation fails.
172 int osp_md_object_create(const struct lu_env *env, struct dt_object *dt,
173 struct lu_attr *attr, struct dt_allocation_hint *hint,
174 struct dt_object_format *dof, struct thandle *th)
176 struct osp_update_request *update;
177 struct osp_object *obj = dt2osp_obj(dt);
180 update = thandle_to_osp_update_request(th);
181 LASSERT(update != NULL);
183 LASSERT(attr->la_valid & LA_TYPE);
184 rc = osp_update_rpc_pack(env, create, update, OUT_CREATE,
185 lu_object_fid(&dt->do_lu), attr, hint, dof);
189 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
190 osp_object_create_interpreter);
195 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
196 dt2osp_obj(dt)->opo_non_exist = 0;
198 LASSERT(obj->opo_ooa != NULL);
199 obj->opo_ooa->ooa_attr = *attr;
205 * Implementation of dt_object_operations::do_declare_ref_del
207 * Create the osp_update_request to track the update for this OSP
208 * in the transaction.
210 * \param[in] env execution environment
211 * \param[in] dt object to decrease the reference count.
212 * \param[in] th the transaction handle of refcount decrease.
214 * \retval 0 if preparation succeeds.
215 * \retval negative errno if preparation fails.
217 static int osp_md_declare_ref_del(const struct lu_env *env,
218 struct dt_object *dt, struct thandle *th)
220 return osp_trans_update_request_create(th);
224 * Implementation of dt_object_operations::do_ref_del
226 * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
227 * flushed when the transaction stop.
229 * \param[in] env execution environment
230 * \param[in] dt object to decrease the reference count
231 * \param[in] th the transaction handle
233 * \retval 0 if packing ref_del succeeds.
234 * \retval negative errno if packing fails.
236 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
239 struct osp_update_request *update;
242 update = thandle_to_osp_update_request(th);
243 LASSERT(update != NULL);
245 rc = osp_update_rpc_pack(env, ref_del, update, OUT_REF_DEL,
246 lu_object_fid(&dt->do_lu));
251 * Implementation of dt_object_operations::do_declare_ref_del
253 * Create the osp_update_request to track the update for this OSP
254 * in the transaction.
256 * \param[in] env execution environment
257 * \param[in] dt object on which to increase the reference count.
258 * \param[in] th the transaction handle.
260 * \retval 0 if preparation succeeds.
261 * \retval negative errno if preparation fails.
263 static int osp_md_declare_ref_add(const struct lu_env *env,
264 struct dt_object *dt, struct thandle *th)
266 return osp_trans_update_request_create(th);
270 * Implementation of dt_object_operations::do_ref_add
272 * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
273 * when the transaction stop.
275 * \param[in] env execution environment
276 * \param[in] dt object on which to increase the reference count
277 * \param[in] th the transaction handle
279 * \retval 0 if packing ref_add succeeds.
280 * \retval negative errno if packing fails.
282 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
285 struct osp_update_request *update;
288 update = thandle_to_osp_update_request(th);
289 LASSERT(update != NULL);
291 rc = osp_update_rpc_pack(env, ref_add, update, OUT_REF_ADD,
292 lu_object_fid(&dt->do_lu));
297 * Implementation of dt_object_operations::do_ah_init
299 * Initialize the allocation hint for object creation, which is usually called
300 * before the creation, and these hints (parent and child mode) will be sent to
301 * the remote Object Update Target (OUT) and used in the object create process,
302 * same as OSD object creation.
304 * \param[in] env execution environment
305 * \param[in] ah the hint to be initialized
306 * \param[in] parent the parent of the object
307 * \param[in] child the object to be created
308 * \param[in] child_mode the mode of the created object
310 static void osp_md_ah_init(const struct lu_env *env,
311 struct dt_allocation_hint *ah,
312 struct dt_object *parent,
313 struct dt_object *child,
318 ah->dah_parent = parent;
319 ah->dah_mode = child_mode;
323 * Implementation of dt_object_operations::do_declare_attr_get
325 * Create the osp_update_request to track the update for this OSP
326 * in the transaction.
328 * \param[in] env execution environment
329 * \param[in] dt object on which to set attributes
330 * \param[in] attr attributes to be set
331 * \param[in] th the transaction handle
333 * \retval 0 if preparation succeeds.
334 * \retval negative errno if preparation fails.
336 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
337 const struct lu_attr *attr, struct thandle *th)
339 return osp_trans_update_request_create(th);
343 * Implementation of dt_object_operations::do_attr_set
345 * Set attributes to the specified remote object.
347 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
348 * when the transaction stop.
350 * \param[in] env execution environment
351 * \param[in] dt object to set attributes
352 * \param[in] attr attributes to be set
353 * \param[in] th the transaction handle
355 * \retval 0 if packing attr_set succeeds.
356 * \retval negative errno if packing fails.
358 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
359 const struct lu_attr *attr, struct thandle *th)
361 struct osp_update_request *update;
364 update = thandle_to_osp_update_request(th);
365 LASSERT(update != NULL);
367 rc = osp_update_rpc_pack(env, attr_set, update, OUT_ATTR_SET,
368 lu_object_fid(&dt->do_lu), attr);
373 * Implementation of dt_object_operations::do_read_lock
375 * osp_md_object_{read,write}_lock() will only lock the remote object in the
376 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
377 * lock the object. Note: it will not lock the object in the whole cluster,
378 * which relies on the LDLM lock.
380 * \param[in] env execution environment
381 * \param[in] dt object to be locked
382 * \param[in] role lock role from MDD layer, see mdd_object_role().
384 static void osp_md_object_read_lock(const struct lu_env *env,
385 struct dt_object *dt, unsigned role)
387 struct osp_object *obj = dt2osp_obj(dt);
389 LASSERT(obj->opo_owner != env);
390 down_read_nested(&obj->opo_sem, role);
392 LASSERT(obj->opo_owner == NULL);
396 * Implementation of dt_object_operations::do_write_lock
398 * Lock the remote object in write mode.
400 * \param[in] env execution environment
401 * \param[in] dt object to be locked
402 * \param[in] role lock role from MDD layer, see mdd_object_role().
404 static void osp_md_object_write_lock(const struct lu_env *env,
405 struct dt_object *dt, unsigned role)
407 struct osp_object *obj = dt2osp_obj(dt);
409 down_write_nested(&obj->opo_sem, role);
411 LASSERT(obj->opo_owner == NULL);
412 obj->opo_owner = env;
416 * Implementation of dt_object_operations::do_read_unlock
418 * Unlock the read lock of remote object.
420 * \param[in] env execution environment
421 * \param[in] dt object to be unlocked
423 static void osp_md_object_read_unlock(const struct lu_env *env,
424 struct dt_object *dt)
426 struct osp_object *obj = dt2osp_obj(dt);
428 up_read(&obj->opo_sem);
432 * Implementation of dt_object_operations::do_write_unlock
434 * Unlock the write lock of remote object.
436 * \param[in] env execution environment
437 * \param[in] dt object to be unlocked
439 static void osp_md_object_write_unlock(const struct lu_env *env,
440 struct dt_object *dt)
442 struct osp_object *obj = dt2osp_obj(dt);
444 LASSERT(obj->opo_owner == env);
445 obj->opo_owner = NULL;
446 up_write(&obj->opo_sem);
450 * Implementation of dt_object_operations::do_write_locked
452 * Test if the object is locked in write mode.
454 * \param[in] env execution environment
455 * \param[in] dt object to be tested
457 static int osp_md_object_write_locked(const struct lu_env *env,
458 struct dt_object *dt)
460 struct osp_object *obj = dt2osp_obj(dt);
462 return obj->opo_owner == env;
466 * Implementation of dt_index_operations::dio_lookup
468 * Look up record by key under a remote index object. It packs lookup update
469 * into RPC, sends to the remote OUT and waits for the lookup result.
471 * \param[in] env execution environment
472 * \param[in] dt index object to lookup
473 * \param[out] rec record in which to return lookup result
474 * \param[in] key key of index which will be looked up
476 * \retval 1 if the lookup succeeds.
477 * \retval negative errno if the lookup fails.
479 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
480 struct dt_rec *rec, const struct dt_key *key)
482 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
483 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
484 struct dt_device *dt_dev = &osp->opd_dt_dev;
485 struct osp_update_request *update;
486 struct object_update_reply *reply;
487 struct ptlrpc_request *req = NULL;
492 /* Because it needs send the update buffer right away,
493 * just create an update buffer, instead of attaching the
494 * update_remote list of the thandle.
496 update = osp_update_request_create(dt_dev);
498 RETURN(PTR_ERR(update));
500 rc = osp_update_rpc_pack(env, index_lookup, update, OUT_INDEX_LOOKUP,
501 lu_object_fid(&dt->do_lu), rec, key);
503 CERROR("%s: Insert update error: rc = %d\n",
504 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
508 rc = osp_remote_sync(env, osp, update, &req);
512 reply = req_capsule_server_sized_get(&req->rq_pill,
513 &RMF_OUT_UPDATE_REPLY,
514 OUT_UPDATE_REPLY_SIZE);
515 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
516 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
517 dt_dev->dd_lu_dev.ld_obd->obd_name,
518 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
519 GOTO(out, rc = -EPROTO);
522 rc = object_update_result_data_get(reply, lbuf, 0);
526 if (lbuf->lb_len != sizeof(*fid)) {
527 CERROR("%s: lookup "DFID" %s wrong size %d\n",
528 dt_dev->dd_lu_dev.ld_obd->obd_name,
529 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
531 GOTO(out, rc = -EINVAL);
535 if (ptlrpc_rep_need_swab(req))
536 lustre_swab_lu_fid(fid);
537 if (!fid_is_sane(fid)) {
538 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
539 dt_dev->dd_lu_dev.ld_obd->obd_name,
540 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
541 GOTO(out, rc = -EINVAL);
544 memcpy(rec, fid, sizeof(*fid));
550 ptlrpc_req_finished(req);
552 osp_update_request_destroy(update);
558 * Implementation of dt_index_operations::dio_declare_insert
560 * Create the osp_update_request to track the update for this OSP
561 * in the transaction.
563 * \param[in] env execution environment
564 * \param[in] dt object for which to insert index
565 * \param[in] rec record of the index which will be inserted
566 * \param[in] key key of the index which will be inserted
567 * \param[in] th the transaction handle
569 * \retval 0 if preparation succeeds.
570 * \retval negative errno if preparation fails.
572 static int osp_md_declare_index_insert(const struct lu_env *env,
573 struct dt_object *dt,
574 const struct dt_rec *rec,
575 const struct dt_key *key,
578 return osp_trans_update_request_create(th);
582 * Implementation of dt_index_operations::dio_insert
584 * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
585 * be flushed when the transaction stop.
587 * \param[in] env execution environment
588 * \param[in] dt object for which to insert index
589 * \param[in] rec record of the index to be inserted
590 * \param[in] key key of the index to be inserted
591 * \param[in] th the transaction handle
592 * \param[in] ignore_quota quota enforcement for insert
594 * \retval 0 if packing index insert succeeds.
595 * \retval negative errno if packing fails.
597 static int osp_md_index_insert(const struct lu_env *env,
598 struct dt_object *dt,
599 const struct dt_rec *rec,
600 const struct dt_key *key,
604 struct osp_update_request *update;
607 update = thandle_to_osp_update_request(th);
608 LASSERT(update != NULL);
610 rc = osp_update_rpc_pack(env, index_insert, update, OUT_INDEX_INSERT,
611 lu_object_fid(&dt->do_lu), rec, key);
616 * Implementation of dt_index_operations::dio_declare_delete
618 * Create the osp_update_request to track the update for this OSP
619 * in the transaction.
621 * \param[in] env execution environment
622 * \param[in] dt object for which to delete index
623 * \param[in] key key of the index
624 * \param[in] th the transaction handle
626 * \retval 0 if preparation succeeds.
627 * \retval negative errno if preparation fails.
629 static int osp_md_declare_index_delete(const struct lu_env *env,
630 struct dt_object *dt,
631 const struct dt_key *key,
634 return osp_trans_update_request_create(th);
638 * Implementation of dt_index_operations::dio_delete
640 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
641 * be flushed when the transaction stop.
643 * \param[in] env execution environment
644 * \param[in] dt object for which to delete index
645 * \param[in] key key of the index which will be deleted
646 * \param[in] th the transaction handle
648 * \retval 0 if packing index delete succeeds.
649 * \retval negative errno if packing fails.
651 static int osp_md_index_delete(const struct lu_env *env,
652 struct dt_object *dt,
653 const struct dt_key *key,
656 struct osp_update_request *update;
659 update = thandle_to_osp_update_request(th);
660 LASSERT(update != NULL);
662 rc = osp_update_rpc_pack(env, index_delete, update, OUT_INDEX_DELETE,
663 lu_object_fid(&dt->do_lu), key);
669 * Implementation of dt_index_operations::dio_it.next
671 * Advance the pointer of the iterator to the next entry. It shares a similar
672 * internal implementation with osp_orphan_it_next(), which is being used for
673 * remote orphan index object. This method will be used for remote directory.
675 * \param[in] env execution environment
676 * \param[in] di iterator of this iteration
678 * \retval 0 if the pointer is advanced successfully.
679 * \retval 1 if it reaches to the end of the index object.
680 * \retval negative errno if the pointer cannot be advanced.
682 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
684 struct osp_it *it = (struct osp_it *)di;
685 struct lu_idxpage *idxpage;
686 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
691 idxpage = it->ooi_cur_idxpage;
692 if (idxpage != NULL) {
693 if (idxpage->lip_nr == 0)
699 (struct lu_dirent *)idxpage->lip_entries;
701 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
702 it->ooi_pos_ent < idxpage->lip_nr) {
703 ent = (struct lu_dirent *)(((char *)ent) +
704 le16_to_cpu(ent->lde_reclen));
712 rc = osp_it_next_page(env, di);
720 * Implementation of dt_index_operations::dio_it.key
722 * Get the key at current iterator poisiton. These iteration methods
723 * (dio_it) will only be used for iterating the remote directory, so
724 * the key is the name of the directory entry.
726 * \param[in] env execution environment
727 * \param[in] di iterator of this iteration
729 * \retval name of the current entry
731 static struct dt_key *osp_it_key(const struct lu_env *env,
732 const struct dt_it *di)
734 struct osp_it *it = (struct osp_it *)di;
735 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
737 return (struct dt_key *)ent->lde_name;
741 * Implementation of dt_index_operations::dio_it.key_size
743 * Get the key size at current iterator poisiton. These iteration methods
744 * (dio_it) will only be used for iterating the remote directory, so the key
745 * size is the name size of the directory entry.
747 * \param[in] env execution environment
748 * \param[in] di iterator of this iteration
750 * \retval name size of the current entry
753 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
755 struct osp_it *it = (struct osp_it *)di;
756 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
758 return (int)le16_to_cpu(ent->lde_namelen);
762 * Implementation of dt_index_operations::dio_it.rec
764 * Get the record at current iterator position. These iteration methods
765 * (dio_it) will only be used for iterating the remote directory, so it
766 * uses lu_dirent_calc_size() to calculate the record size.
768 * \param[in] env execution environment
769 * \param[in] di iterator of this iteration
770 * \param[out] rec the record to be returned
771 * \param[in] attr attributes of the index object, so it knows
772 * how to pack the entry.
774 * \retval only return 0 for now
776 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
777 struct dt_rec *rec, __u32 attr)
779 struct osp_it *it = (struct osp_it *)di;
780 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
783 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
784 memcpy(rec, ent, reclen);
789 * Implementation of dt_index_operations::dio_it.load
791 * Locate the iteration cursor to the specified position (cookie).
793 * \param[in] env pointer to the thread context
794 * \param[in] di pointer to the iteration structure
795 * \param[in] hash the specified position
797 * \retval positive number for locating to the exactly position
799 * \retval 0 for arriving at the end of the iteration
800 * \retval negative error number on failure
802 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
805 struct osp_it *it = (struct osp_it *)di;
809 rc = osp_md_index_it_next(env, (struct dt_it *)di);
819 const struct dt_index_operations osp_md_index_ops = {
820 .dio_lookup = osp_md_index_lookup,
821 .dio_declare_insert = osp_md_declare_index_insert,
822 .dio_insert = osp_md_index_insert,
823 .dio_declare_delete = osp_md_declare_index_delete,
824 .dio_delete = osp_md_index_delete,
830 .next = osp_md_index_it_next,
832 .key_size = osp_it_key_size,
833 .rec = osp_md_index_it_rec,
834 .store = osp_it_store,
836 .key_rec = osp_it_key_rec,
841 * Implementation of dt_object_operations::do_index_try
843 * Try to initialize the index API pointer for the given object. This
844 * is the entry point of the index API, i.e. we must call this method
845 * to initialize the index object before calling other index methods.
847 * \param[in] env execution environment
848 * \param[in] dt index object to be initialized
849 * \param[in] feat the index feature of the object
851 * \retval 0 if the initialization succeeds.
852 * \retval negative errno if the initialization fails.
854 static int osp_md_index_try(const struct lu_env *env,
855 struct dt_object *dt,
856 const struct dt_index_features *feat)
858 dt->do_index_ops = &osp_md_index_ops;
863 * Implementation of dt_object_operations::do_object_lock
865 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
866 * which will lock the object in the global namespace. And because the
867 * cross-MDT locks are relatively rare compared with normal local MDT operation,
868 * let's release it right away, instead of putting it into the LRU list.
870 * \param[in] env execution environment
871 * \param[in] dt object to be locked
872 * \param[out] lh lock handle
873 * \param[in] einfo enqueue information
874 * \param[in] policy lock policy
876 * \retval ELDLM_OK if locking the object succeeds.
877 * \retval negative errno if locking fails.
879 static int osp_md_object_lock(const struct lu_env *env,
880 struct dt_object *dt,
881 struct lustre_handle *lh,
882 struct ldlm_enqueue_info *einfo,
883 union ldlm_policy_data *policy)
885 struct ldlm_res_id *res_id;
886 struct dt_device *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
887 struct osp_device *osp = dt2osp_dev(dt_dev);
888 struct ptlrpc_request *req;
893 res_id = einfo->ei_res_id;
894 LASSERT(res_id != NULL);
896 mode = ldlm_lock_match(osp->opd_obd->obd_namespace,
897 LDLM_FL_BLOCK_GRANTED, res_id,
898 einfo->ei_type, policy,
899 einfo->ei_mode, lh, 0);
903 if (einfo->ei_nonblock)
904 flags |= LDLM_FL_BLOCK_NOWAIT;
906 req = ldlm_enqueue_pack(osp->opd_exp, 0);
908 RETURN(PTR_ERR(req));
910 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
911 (const union ldlm_policy_data *)policy,
912 &flags, NULL, 0, LVB_T_NONE, lh, 0);
914 ptlrpc_req_finished(req);
915 if (rc == ELDLM_OK) {
916 struct ldlm_lock *lock;
918 lock = __ldlm_handle2lock(lh, 0);
919 ldlm_set_cbpending(lock);
923 return rc == ELDLM_OK ? 0 : -EIO;
927 * Implementation of dt_object_operations::do_object_unlock
929 * Cancel a lock of a remote object.
931 * \param[in] env execution environment
932 * \param[in] dt object to be unlocked
933 * \param[in] einfo lock enqueue information
934 * \param[in] policy lock policy
936 * \retval Only return 0 for now.
938 static int osp_md_object_unlock(const struct lu_env *env,
939 struct dt_object *dt,
940 struct ldlm_enqueue_info *einfo,
941 union ldlm_policy_data *policy)
943 struct lustre_handle *lockh = einfo->ei_cbdata;
946 ldlm_lock_decref(lockh, einfo->ei_mode);
952 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
954 * Create the dt_update_request to track the update for this OSP
955 * in the transaction.
957 * \param[in] env pointer to the thread context
958 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
959 * \param[in] th pointer to the transaction handler
961 * \retval 0 for success
962 * \retval negative error number on failure
964 int osp_md_declare_object_destroy(const struct lu_env *env,
965 struct dt_object *dt, struct thandle *th)
967 return osp_trans_update_request_create(th);
971 * Implement OSP layer dt_object_operations::do_destroy() interface.
973 * Pack the destroy update into the RPC buffer, which will be sent
974 * to the remote MDT during transaction stop.
976 * It also marks the object as non-cached.
978 * \param[in] env pointer to the thread context
979 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
980 * \param[in] th pointer to the transaction handler
982 * \retval 0 for success
983 * \retval negative error number on failure
985 int osp_md_object_destroy(const struct lu_env *env, struct dt_object *dt,
988 struct osp_object *o = dt2osp_obj(dt);
989 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
990 struct osp_update_request *update;
994 o->opo_non_exist = 1;
996 LASSERT(osp->opd_connect_mdt);
997 update = thandle_to_osp_update_request(th);
998 LASSERT(update != NULL);
1000 rc = osp_update_rpc_pack(env, object_destroy, update, OUT_DESTROY,
1001 lu_object_fid(&dt->do_lu));
1005 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1006 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1012 struct dt_object_operations osp_md_obj_ops = {
1013 .do_read_lock = osp_md_object_read_lock,
1014 .do_write_lock = osp_md_object_write_lock,
1015 .do_read_unlock = osp_md_object_read_unlock,
1016 .do_write_unlock = osp_md_object_write_unlock,
1017 .do_write_locked = osp_md_object_write_locked,
1018 .do_declare_create = osp_md_declare_object_create,
1019 .do_create = osp_md_object_create,
1020 .do_declare_ref_add = osp_md_declare_ref_add,
1021 .do_ref_add = osp_md_ref_add,
1022 .do_declare_ref_del = osp_md_declare_ref_del,
1023 .do_ref_del = osp_md_ref_del,
1024 .do_declare_destroy = osp_md_declare_object_destroy,
1025 .do_destroy = osp_md_object_destroy,
1026 .do_ah_init = osp_md_ah_init,
1027 .do_attr_get = osp_attr_get,
1028 .do_declare_attr_set = osp_md_declare_attr_set,
1029 .do_attr_set = osp_md_attr_set,
1030 .do_xattr_get = osp_xattr_get,
1031 .do_declare_xattr_set = osp_declare_xattr_set,
1032 .do_xattr_set = osp_xattr_set,
1033 .do_declare_xattr_del = osp_declare_xattr_del,
1034 .do_xattr_del = osp_xattr_del,
1035 .do_index_try = osp_md_index_try,
1036 .do_object_lock = osp_md_object_lock,
1037 .do_object_unlock = osp_md_object_unlock,
1041 * Implementation of dt_body_operations::dbo_declare_write
1043 * Create the osp_update_request to track the update for this OSP
1044 * in the transaction.
1046 * \param[in] env execution environment
1047 * \param[in] dt object to be written
1048 * \param[in] buf buffer to write which includes an embedded size field
1049 * \param[in] pos offet in the object to start writing at
1050 * \param[in] th transaction handle
1052 * \retval 0 if preparation succeeds.
1053 * \retval negative errno if preparation fails.
1055 static ssize_t osp_md_declare_write(const struct lu_env *env,
1056 struct dt_object *dt,
1057 const struct lu_buf *buf,
1058 loff_t pos, struct thandle *th)
1060 return osp_trans_update_request_create(th);
1064 * Implementation of dt_body_operations::dbo_write
1066 * Pack the write object update into the RPC buffer, which will be sent
1067 * to the remote MDT during transaction stop.
1069 * \param[in] env execution environment
1070 * \param[in] dt object to be written
1071 * \param[in] buf buffer to write which includes an embedded size field
1072 * \param[in] pos offet in the object to start writing at
1073 * \param[in] th transaction handle
1074 * \param[in] ignore_quota quota enforcement for this write
1076 * \retval the buffer size in bytes if packing succeeds.
1077 * \retval negative errno if packing fails.
1079 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1080 const struct lu_buf *buf, loff_t *pos,
1081 struct thandle *th, int ignore_quota)
1083 struct osp_object *obj = dt2osp_obj(dt);
1084 struct osp_update_request *update;
1085 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1089 update = thandle_to_osp_update_request(th);
1090 LASSERT(update != NULL);
1092 rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1093 lu_object_fid(&dt->do_lu), buf, *pos);
1097 CDEBUG(D_INFO, "write "DFID" offset = "LPU64" length = %zu\n",
1098 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1100 /* XXX: how about the write error happened later? */
1101 *pos += buf->lb_len;
1103 if (obj->opo_ooa != NULL &&
1104 obj->opo_ooa->ooa_attr.la_valid & LA_SIZE &&
1105 obj->opo_ooa->ooa_attr.la_size < *pos)
1106 obj->opo_ooa->ooa_attr.la_size = *pos;
1108 rc = osp_check_and_set_rpc_version(oth);
1112 RETURN(buf->lb_len);
1115 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1116 struct lu_buf *rbuf, loff_t *pos)
1118 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1119 struct dt_device *dt_dev = &osp->opd_dt_dev;
1120 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1121 char *ptr = rbuf->lb_buf;
1122 struct osp_update_request *update = NULL;
1123 struct ptlrpc_request *req = NULL;
1124 struct out_read_reply *orr;
1125 struct ptlrpc_bulk_desc *desc;
1126 struct object_update_reply *reply;
1133 /* Because it needs send the update buffer right away,
1134 * just create an update buffer, instead of attaching the
1135 * update_remote list of the thandle. */
1136 update = osp_update_request_create(dt_dev);
1138 GOTO(out, rc = PTR_ERR(update));
1140 rc = osp_update_rpc_pack(env, read, update, OUT_READ,
1141 lu_object_fid(&dt->do_lu),
1142 rbuf->lb_len, *pos);
1144 CERROR("%s: cannot insert update: rc = %d\n",
1145 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1149 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1154 nbufs = (rbuf->lb_len + OUT_BULK_BUFFER_SIZE - 1) /
1155 OUT_BULK_BUFFER_SIZE;
1156 /* allocate bulk descriptor */
1157 desc = ptlrpc_prep_bulk_imp(req, nbufs, 1,
1158 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KVEC,
1159 MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
1161 GOTO(out, rc = -ENOMEM);
1163 /* split the buffer into small chunk size */
1164 left_size = rbuf->lb_len;
1165 for (i = 0; i < nbufs; i++) {
1168 read_size = left_size > OUT_BULK_BUFFER_SIZE ?
1169 OUT_BULK_BUFFER_SIZE : left_size;
1170 desc->bd_frag_ops->add_iov_frag(desc, ptr, read_size);
1175 /* This will only be called with read-only update, and these updates
1176 * might be used to retrieve update log during recovery process, so
1177 * it will be allowed to send during recovery process */
1178 req->rq_allow_replay = 1;
1179 req->rq_bulk_read = 1;
1180 /* send request to master and wait for RPC to complete */
1181 rc = ptlrpc_queue_wait(req);
1185 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1186 req->rq_bulk->bd_nob_transferred);
1190 reply = req_capsule_server_sized_get(&req->rq_pill,
1191 &RMF_OUT_UPDATE_REPLY,
1192 OUT_UPDATE_REPLY_SIZE);
1194 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1195 CERROR("%s: invalid update reply magic %x expected %x:"
1196 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1197 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1198 GOTO(out, rc = -EPROTO);
1201 rc = object_update_result_data_get(reply, lbuf, 0);
1205 if (lbuf->lb_len < sizeof(*orr))
1206 GOTO(out, rc = -EPROTO);
1209 orr_le_to_cpu(orr, orr);
1211 *pos = orr->orr_offset;
1214 ptlrpc_req_finished(req);
1217 osp_update_request_destroy(update);
1222 /* These body operation will be used to write symlinks during migration etc */
1223 struct dt_body_operations osp_md_body_ops = {
1224 .dbo_declare_write = osp_md_declare_write,
1225 .dbo_write = osp_md_write,
1226 .dbo_read = osp_md_read,