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, 2016, 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_object_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_object_create(const struct lu_env *env,
117 struct dt_object *dt,
118 struct lu_attr *attr,
119 struct dt_allocation_hint *hint,
120 struct dt_object_format *dof,
123 return osp_trans_update_request_create(th);
126 struct object_update *
127 update_buffer_get_update(struct object_update_request *request,
133 if (index > request->ourq_count)
136 ptr = &request->ourq_updates[0];
137 for (i = 0; i < index; i++)
138 ptr += object_update_size(ptr);
144 * Implementation of dt_object_operations::do_create
146 * It adds an OUT_CREATE sub-request into the OUT RPC that will be flushed
147 * when the transaction stop, and sets necessary flags for created object.
149 * \param[in] env execution environment
150 * \param[in] dt object to be created
151 * \param[in] attr attribute of the created object
152 * \param[in] hint creation hint
153 * \param[in] dof creation format information
154 * \param[in] th the transaction handle
156 * \retval 0 if packing creation succeeds.
157 * \retval negative errno if packing creation fails.
159 int osp_md_object_create(const struct lu_env *env, struct dt_object *dt,
160 struct lu_attr *attr, struct dt_allocation_hint *hint,
161 struct dt_object_format *dof, struct thandle *th)
163 struct osp_update_request *update;
164 struct osp_object *obj = dt2osp_obj(dt);
167 update = thandle_to_osp_update_request(th);
168 LASSERT(update != NULL);
170 LASSERT(attr->la_valid & LA_TYPE);
171 rc = osp_update_rpc_pack(env, create, update, OUT_CREATE,
172 lu_object_fid(&dt->do_lu), attr, hint, dof);
176 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
177 osp_object_create_interpreter);
182 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
183 dt2osp_obj(dt)->opo_non_exist = 0;
185 obj->opo_attr = *attr;
191 * Implementation of dt_object_operations::do_declare_ref_del
193 * Create the osp_update_request to track the update for this OSP
194 * in the transaction.
196 * \param[in] env execution environment
197 * \param[in] dt object to decrease the reference count.
198 * \param[in] th the transaction handle of refcount decrease.
200 * \retval 0 if preparation succeeds.
201 * \retval negative errno if preparation fails.
203 static int osp_md_declare_ref_del(const struct lu_env *env,
204 struct dt_object *dt, struct thandle *th)
206 return osp_trans_update_request_create(th);
210 * Implementation of dt_object_operations::do_ref_del
212 * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
213 * flushed when the transaction stop.
215 * \param[in] env execution environment
216 * \param[in] dt object to decrease the reference count
217 * \param[in] th the transaction handle
219 * \retval 0 if packing ref_del succeeds.
220 * \retval negative errno if packing fails.
222 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
225 struct osp_update_request *update;
228 update = thandle_to_osp_update_request(th);
229 LASSERT(update != NULL);
231 rc = osp_update_rpc_pack(env, ref_del, update, OUT_REF_DEL,
232 lu_object_fid(&dt->do_lu));
237 * Implementation of dt_object_operations::do_declare_ref_del
239 * Create the osp_update_request to track the update for this OSP
240 * in the transaction.
242 * \param[in] env execution environment
243 * \param[in] dt object on which to increase the reference count.
244 * \param[in] th the transaction handle.
246 * \retval 0 if preparation succeeds.
247 * \retval negative errno if preparation fails.
249 static int osp_md_declare_ref_add(const struct lu_env *env,
250 struct dt_object *dt, struct thandle *th)
252 return osp_trans_update_request_create(th);
256 * Implementation of dt_object_operations::do_ref_add
258 * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
259 * when the transaction stop.
261 * \param[in] env execution environment
262 * \param[in] dt object on which to increase the reference count
263 * \param[in] th the transaction handle
265 * \retval 0 if packing ref_add succeeds.
266 * \retval negative errno if packing fails.
268 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
271 struct osp_update_request *update;
274 update = thandle_to_osp_update_request(th);
275 LASSERT(update != NULL);
277 rc = osp_update_rpc_pack(env, ref_add, update, OUT_REF_ADD,
278 lu_object_fid(&dt->do_lu));
283 * Implementation of dt_object_operations::do_ah_init
285 * Initialize the allocation hint for object creation, which is usually called
286 * before the creation, and these hints (parent and child mode) will be sent to
287 * the remote Object Update Target (OUT) and used in the object create process,
288 * same as OSD object creation.
290 * \param[in] env execution environment
291 * \param[in] ah the hint to be initialized
292 * \param[in] parent the parent of the object
293 * \param[in] child the object to be created
294 * \param[in] child_mode the mode of the created object
296 static void osp_md_ah_init(const struct lu_env *env,
297 struct dt_allocation_hint *ah,
298 struct dt_object *parent,
299 struct dt_object *child,
304 ah->dah_parent = parent;
305 ah->dah_mode = child_mode;
309 * Implementation of dt_object_operations::do_declare_attr_get
311 * Create the osp_update_request to track the update for this OSP
312 * in the transaction.
314 * \param[in] env execution environment
315 * \param[in] dt object on which to set attributes
316 * \param[in] attr attributes to be set
317 * \param[in] th the transaction handle
319 * \retval 0 if preparation succeeds.
320 * \retval negative errno if preparation fails.
322 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
323 const struct lu_attr *attr, struct thandle *th)
325 return osp_trans_update_request_create(th);
329 * Implementation of dt_object_operations::do_attr_set
331 * Set attributes to the specified remote object.
333 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
334 * when the transaction stop.
336 * \param[in] env execution environment
337 * \param[in] dt object to set attributes
338 * \param[in] attr attributes to be set
339 * \param[in] th the transaction handle
341 * \retval 0 if packing attr_set succeeds.
342 * \retval negative errno if packing fails.
344 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
345 const struct lu_attr *attr, struct thandle *th)
347 struct osp_update_request *update;
350 update = thandle_to_osp_update_request(th);
351 LASSERT(update != NULL);
353 rc = osp_update_rpc_pack(env, attr_set, update, OUT_ATTR_SET,
354 lu_object_fid(&dt->do_lu), attr);
359 * Implementation of dt_object_operations::do_read_lock
361 * osp_md_object_{read,write}_lock() will only lock the remote object in the
362 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
363 * lock the object. Note: it will not lock the object in the whole cluster,
364 * which relies on the LDLM lock.
366 * \param[in] env execution environment
367 * \param[in] dt object to be locked
368 * \param[in] role lock role from MDD layer, see mdd_object_role().
370 static void osp_md_object_read_lock(const struct lu_env *env,
371 struct dt_object *dt, unsigned role)
373 struct osp_object *obj = dt2osp_obj(dt);
375 LASSERT(obj->opo_owner != env);
376 down_read_nested(&obj->opo_sem, role);
378 LASSERT(obj->opo_owner == NULL);
382 * Implementation of dt_object_operations::do_write_lock
384 * Lock the remote object in write mode.
386 * \param[in] env execution environment
387 * \param[in] dt object to be locked
388 * \param[in] role lock role from MDD layer, see mdd_object_role().
390 static void osp_md_object_write_lock(const struct lu_env *env,
391 struct dt_object *dt, unsigned role)
393 struct osp_object *obj = dt2osp_obj(dt);
395 down_write_nested(&obj->opo_sem, role);
397 LASSERT(obj->opo_owner == NULL);
398 obj->opo_owner = env;
402 * Implementation of dt_object_operations::do_read_unlock
404 * Unlock the read lock of remote object.
406 * \param[in] env execution environment
407 * \param[in] dt object to be unlocked
409 static void osp_md_object_read_unlock(const struct lu_env *env,
410 struct dt_object *dt)
412 struct osp_object *obj = dt2osp_obj(dt);
414 up_read(&obj->opo_sem);
418 * Implementation of dt_object_operations::do_write_unlock
420 * Unlock the write lock of remote object.
422 * \param[in] env execution environment
423 * \param[in] dt object to be unlocked
425 static void osp_md_object_write_unlock(const struct lu_env *env,
426 struct dt_object *dt)
428 struct osp_object *obj = dt2osp_obj(dt);
430 LASSERT(obj->opo_owner == env);
431 obj->opo_owner = NULL;
432 up_write(&obj->opo_sem);
436 * Implementation of dt_object_operations::do_write_locked
438 * Test if the object is locked in write mode.
440 * \param[in] env execution environment
441 * \param[in] dt object to be tested
443 static int osp_md_object_write_locked(const struct lu_env *env,
444 struct dt_object *dt)
446 struct osp_object *obj = dt2osp_obj(dt);
448 return obj->opo_owner == env;
452 * Implementation of dt_index_operations::dio_lookup
454 * Look up record by key under a remote index object. It packs lookup update
455 * into RPC, sends to the remote OUT and waits for the lookup result.
457 * \param[in] env execution environment
458 * \param[in] dt index object to lookup
459 * \param[out] rec record in which to return lookup result
460 * \param[in] key key of index which will be looked up
462 * \retval 1 if the lookup succeeds.
463 * \retval negative errno if the lookup fails.
465 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
466 struct dt_rec *rec, const struct dt_key *key)
468 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
469 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
470 struct dt_device *dt_dev = &osp->opd_dt_dev;
471 struct osp_update_request *update;
472 struct object_update_reply *reply;
473 struct ptlrpc_request *req = NULL;
478 /* Because it needs send the update buffer right away,
479 * just create an update buffer, instead of attaching the
480 * update_remote list of the thandle.
482 update = osp_update_request_create(dt_dev);
484 RETURN(PTR_ERR(update));
486 rc = osp_update_rpc_pack(env, index_lookup, update, OUT_INDEX_LOOKUP,
487 lu_object_fid(&dt->do_lu), rec, key);
489 CERROR("%s: Insert update error: rc = %d\n",
490 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
494 rc = osp_remote_sync(env, osp, update, &req);
498 reply = req_capsule_server_sized_get(&req->rq_pill,
499 &RMF_OUT_UPDATE_REPLY,
500 OUT_UPDATE_REPLY_SIZE);
501 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
502 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
503 dt_dev->dd_lu_dev.ld_obd->obd_name,
504 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
505 GOTO(out, rc = -EPROTO);
508 rc = object_update_result_data_get(reply, lbuf, 0);
512 if (lbuf->lb_len != sizeof(*fid)) {
513 CERROR("%s: lookup "DFID" %s wrong size %d\n",
514 dt_dev->dd_lu_dev.ld_obd->obd_name,
515 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
517 GOTO(out, rc = -EINVAL);
521 if (ptlrpc_rep_need_swab(req))
522 lustre_swab_lu_fid(fid);
523 if (!fid_is_sane(fid)) {
524 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
525 dt_dev->dd_lu_dev.ld_obd->obd_name,
526 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
527 GOTO(out, rc = -EINVAL);
530 memcpy(rec, fid, sizeof(*fid));
536 ptlrpc_req_finished(req);
538 osp_update_request_destroy(env, update);
544 * Implementation of dt_index_operations::dio_declare_insert
546 * Create the osp_update_request to track the update for this OSP
547 * in the transaction.
549 * \param[in] env execution environment
550 * \param[in] dt object for which to insert index
551 * \param[in] rec record of the index which will be inserted
552 * \param[in] key key of the index which will be inserted
553 * \param[in] th the transaction handle
555 * \retval 0 if preparation succeeds.
556 * \retval negative errno if preparation fails.
558 static int osp_md_declare_index_insert(const struct lu_env *env,
559 struct dt_object *dt,
560 const struct dt_rec *rec,
561 const struct dt_key *key,
564 return osp_trans_update_request_create(th);
568 * Implementation of dt_index_operations::dio_insert
570 * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
571 * be flushed when the transaction stop.
573 * \param[in] env execution environment
574 * \param[in] dt object for which to insert index
575 * \param[in] rec record of the index to be inserted
576 * \param[in] key key of the index to be inserted
577 * \param[in] th the transaction handle
578 * \param[in] ignore_quota quota enforcement for insert
580 * \retval 0 if packing index insert succeeds.
581 * \retval negative errno if packing fails.
583 static int osp_md_index_insert(const struct lu_env *env,
584 struct dt_object *dt,
585 const struct dt_rec *rec,
586 const struct dt_key *key,
590 struct osp_update_request *update;
593 update = thandle_to_osp_update_request(th);
594 LASSERT(update != NULL);
596 rc = osp_update_rpc_pack(env, index_insert, update, OUT_INDEX_INSERT,
597 lu_object_fid(&dt->do_lu), rec, key);
602 * Implementation of dt_index_operations::dio_declare_delete
604 * Create the osp_update_request to track the update for this OSP
605 * in the transaction.
607 * \param[in] env execution environment
608 * \param[in] dt object for which to delete index
609 * \param[in] key key of the index
610 * \param[in] th the transaction handle
612 * \retval 0 if preparation succeeds.
613 * \retval negative errno if preparation fails.
615 static int osp_md_declare_index_delete(const struct lu_env *env,
616 struct dt_object *dt,
617 const struct dt_key *key,
620 return osp_trans_update_request_create(th);
624 * Implementation of dt_index_operations::dio_delete
626 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
627 * be flushed when the transaction stop.
629 * \param[in] env execution environment
630 * \param[in] dt object for which to delete index
631 * \param[in] key key of the index which will be deleted
632 * \param[in] th the transaction handle
634 * \retval 0 if packing index delete succeeds.
635 * \retval negative errno if packing fails.
637 static int osp_md_index_delete(const struct lu_env *env,
638 struct dt_object *dt,
639 const struct dt_key *key,
642 struct osp_update_request *update;
645 update = thandle_to_osp_update_request(th);
646 LASSERT(update != NULL);
648 rc = osp_update_rpc_pack(env, index_delete, update, OUT_INDEX_DELETE,
649 lu_object_fid(&dt->do_lu), key);
655 * Implementation of dt_index_operations::dio_it.next
657 * Advance the pointer of the iterator to the next entry. It shares a similar
658 * internal implementation with osp_orphan_it_next(), which is being used for
659 * remote orphan index object. This method will be used for remote directory.
661 * \param[in] env execution environment
662 * \param[in] di iterator of this iteration
664 * \retval 0 if the pointer is advanced successfully.
665 * \retval 1 if it reaches to the end of the index object.
666 * \retval negative errno if the pointer cannot be advanced.
668 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
670 struct osp_it *it = (struct osp_it *)di;
671 struct lu_idxpage *idxpage;
672 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
677 idxpage = it->ooi_cur_idxpage;
678 if (idxpage != NULL) {
679 if (idxpage->lip_nr == 0)
685 (struct lu_dirent *)idxpage->lip_entries;
687 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
688 it->ooi_pos_ent < idxpage->lip_nr) {
689 ent = (struct lu_dirent *)(((char *)ent) +
690 le16_to_cpu(ent->lde_reclen));
698 rc = osp_it_next_page(env, di);
706 * Implementation of dt_index_operations::dio_it.key
708 * Get the key at current iterator poisiton. These iteration methods
709 * (dio_it) will only be used for iterating the remote directory, so
710 * the key is the name of the directory entry.
712 * \param[in] env execution environment
713 * \param[in] di iterator of this iteration
715 * \retval name of the current entry
717 static struct dt_key *osp_it_key(const struct lu_env *env,
718 const struct dt_it *di)
720 struct osp_it *it = (struct osp_it *)di;
721 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
723 return (struct dt_key *)ent->lde_name;
727 * Implementation of dt_index_operations::dio_it.key_size
729 * Get the key size at current iterator poisiton. These iteration methods
730 * (dio_it) will only be used for iterating the remote directory, so the key
731 * size is the name size of the directory entry.
733 * \param[in] env execution environment
734 * \param[in] di iterator of this iteration
736 * \retval name size of the current entry
739 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
741 struct osp_it *it = (struct osp_it *)di;
742 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
744 return (int)le16_to_cpu(ent->lde_namelen);
748 * Implementation of dt_index_operations::dio_it.rec
750 * Get the record at current iterator position. These iteration methods
751 * (dio_it) will only be used for iterating the remote directory, so it
752 * uses lu_dirent_calc_size() to calculate the record size.
754 * \param[in] env execution environment
755 * \param[in] di iterator of this iteration
756 * \param[out] rec the record to be returned
757 * \param[in] attr attributes of the index object, so it knows
758 * how to pack the entry.
760 * \retval only return 0 for now
762 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
763 struct dt_rec *rec, __u32 attr)
765 struct osp_it *it = (struct osp_it *)di;
766 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
769 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
770 memcpy(rec, ent, reclen);
775 * Implementation of dt_index_operations::dio_it.load
777 * Locate the iteration cursor to the specified position (cookie).
779 * \param[in] env pointer to the thread context
780 * \param[in] di pointer to the iteration structure
781 * \param[in] hash the specified position
783 * \retval positive number for locating to the exactly position
785 * \retval 0 for arriving at the end of the iteration
786 * \retval negative error number on failure
788 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
791 struct osp_it *it = (struct osp_it *)di;
795 rc = osp_md_index_it_next(env, (struct dt_it *)di);
805 const struct dt_index_operations osp_md_index_ops = {
806 .dio_lookup = osp_md_index_lookup,
807 .dio_declare_insert = osp_md_declare_index_insert,
808 .dio_insert = osp_md_index_insert,
809 .dio_declare_delete = osp_md_declare_index_delete,
810 .dio_delete = osp_md_index_delete,
816 .next = osp_md_index_it_next,
818 .key_size = osp_it_key_size,
819 .rec = osp_md_index_it_rec,
820 .store = osp_it_store,
822 .key_rec = osp_it_key_rec,
827 * Implementation of dt_object_operations::do_index_try
829 * Try to initialize the index API pointer for the given object. This
830 * is the entry point of the index API, i.e. we must call this method
831 * to initialize the index object before calling other index methods.
833 * \param[in] env execution environment
834 * \param[in] dt index object to be initialized
835 * \param[in] feat the index feature of the object
837 * \retval 0 if the initialization succeeds.
838 * \retval negative errno if the initialization fails.
840 static int osp_md_index_try(const struct lu_env *env,
841 struct dt_object *dt,
842 const struct dt_index_features *feat)
844 dt->do_index_ops = &osp_md_index_ops;
849 * Implementation of dt_object_operations::do_object_lock
851 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
852 * which will lock the object in the global namespace. And because the
853 * cross-MDT locks are relatively rare compared with normal local MDT operation,
854 * let's release it right away, instead of putting it into the LRU list.
856 * \param[in] env execution environment
857 * \param[in] dt object to be locked
858 * \param[out] lh lock handle
859 * \param[in] einfo enqueue information
860 * \param[in] policy lock policy
862 * \retval ELDLM_OK if locking the object succeeds.
863 * \retval negative errno if locking fails.
865 static int osp_md_object_lock(const struct lu_env *env,
866 struct dt_object *dt,
867 struct lustre_handle *lh,
868 struct ldlm_enqueue_info *einfo,
869 union ldlm_policy_data *policy)
871 struct ldlm_res_id *res_id;
872 struct osp_device *osp = dt2osp_dev(lu2dt_dev(dt->do_lu.lo_dev));
873 struct ptlrpc_request *req;
875 __u64 flags = LDLM_FL_NO_LRU;
878 res_id = einfo->ei_res_id;
879 LASSERT(res_id != NULL);
881 if (einfo->ei_nonblock)
882 flags |= LDLM_FL_BLOCK_NOWAIT;
883 if (einfo->ei_mode & (LCK_EX | LCK_PW))
884 flags |= LDLM_FL_COS_INCOMPAT;
886 req = ldlm_enqueue_pack(osp->opd_exp, 0);
888 RETURN(PTR_ERR(req));
890 osp_set_req_replay(osp, req);
891 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
892 (const union ldlm_policy_data *)policy,
893 &flags, NULL, 0, LVB_T_NONE, lh, 0);
895 ptlrpc_req_finished(req);
897 RETURN(rc == ELDLM_OK ? 0 : -EIO);
901 * Implementation of dt_object_operations::do_object_unlock
903 * Cancel a lock of a remote object.
905 * \param[in] env execution environment
906 * \param[in] dt object to be unlocked
907 * \param[in] einfo lock enqueue information
908 * \param[in] policy lock policy
910 * \retval Only return 0 for now.
912 static int osp_md_object_unlock(const struct lu_env *env,
913 struct dt_object *dt,
914 struct ldlm_enqueue_info *einfo,
915 union ldlm_policy_data *policy)
917 struct lustre_handle *lockh = einfo->ei_cbdata;
920 ldlm_lock_decref(lockh, einfo->ei_mode);
926 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
928 * Create the dt_update_request to track the update for this OSP
929 * in the transaction.
931 * \param[in] env pointer to the thread context
932 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
933 * \param[in] th pointer to the transaction handler
935 * \retval 0 for success
936 * \retval negative error number on failure
938 int osp_md_declare_object_destroy(const struct lu_env *env,
939 struct dt_object *dt, struct thandle *th)
941 return osp_trans_update_request_create(th);
945 * Implement OSP layer dt_object_operations::do_destroy() interface.
947 * Pack the destroy update into the RPC buffer, which will be sent
948 * to the remote MDT during transaction stop.
950 * It also marks the object as non-cached.
952 * \param[in] env pointer to the thread context
953 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
954 * \param[in] th pointer to the transaction handler
956 * \retval 0 for success
957 * \retval negative error number on failure
959 int osp_md_object_destroy(const struct lu_env *env, struct dt_object *dt,
962 struct osp_object *o = dt2osp_obj(dt);
963 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
964 struct osp_update_request *update;
968 o->opo_non_exist = 1;
970 LASSERT(osp->opd_connect_mdt);
971 update = thandle_to_osp_update_request(th);
972 LASSERT(update != NULL);
974 rc = osp_update_rpc_pack(env, object_destroy, update, OUT_DESTROY,
975 lu_object_fid(&dt->do_lu));
979 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
980 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
986 struct dt_object_operations osp_md_obj_ops = {
987 .do_read_lock = osp_md_object_read_lock,
988 .do_write_lock = osp_md_object_write_lock,
989 .do_read_unlock = osp_md_object_read_unlock,
990 .do_write_unlock = osp_md_object_write_unlock,
991 .do_write_locked = osp_md_object_write_locked,
992 .do_declare_create = osp_md_declare_object_create,
993 .do_create = osp_md_object_create,
994 .do_declare_ref_add = osp_md_declare_ref_add,
995 .do_ref_add = osp_md_ref_add,
996 .do_declare_ref_del = osp_md_declare_ref_del,
997 .do_ref_del = osp_md_ref_del,
998 .do_declare_destroy = osp_md_declare_object_destroy,
999 .do_destroy = osp_md_object_destroy,
1000 .do_ah_init = osp_md_ah_init,
1001 .do_attr_get = osp_attr_get,
1002 .do_declare_attr_set = osp_md_declare_attr_set,
1003 .do_attr_set = osp_md_attr_set,
1004 .do_xattr_get = osp_xattr_get,
1005 .do_declare_xattr_set = osp_declare_xattr_set,
1006 .do_xattr_set = osp_xattr_set,
1007 .do_declare_xattr_del = osp_declare_xattr_del,
1008 .do_xattr_del = osp_xattr_del,
1009 .do_index_try = osp_md_index_try,
1010 .do_object_lock = osp_md_object_lock,
1011 .do_object_unlock = osp_md_object_unlock,
1012 .do_invalidate = osp_invalidate,
1016 * Implementation of dt_body_operations::dbo_declare_write
1018 * Create the osp_update_request to track the update for this OSP
1019 * in the transaction.
1021 * \param[in] env execution environment
1022 * \param[in] dt object to be written
1023 * \param[in] buf buffer to write which includes an embedded size field
1024 * \param[in] pos offet in the object to start writing at
1025 * \param[in] th transaction handle
1027 * \retval 0 if preparation succeeds.
1028 * \retval negative errno if preparation fails.
1030 static ssize_t osp_md_declare_write(const struct lu_env *env,
1031 struct dt_object *dt,
1032 const struct lu_buf *buf,
1033 loff_t pos, struct thandle *th)
1035 struct osp_device *osp = dt2osp_dev(th->th_dev);
1038 rc = osp_trans_update_request_create(th);
1042 if (osp->opd_update == NULL)
1045 if (dt2osp_obj(dt)->opo_stale)
1052 * Implementation of dt_body_operations::dbo_write
1054 * Pack the write object update into the RPC buffer, which will be sent
1055 * to the remote MDT during transaction stop.
1057 * \param[in] env execution environment
1058 * \param[in] dt object to be written
1059 * \param[in] buf buffer to write which includes an embedded size field
1060 * \param[in] pos offet in the object to start writing at
1061 * \param[in] th transaction handle
1062 * \param[in] ignore_quota quota enforcement for this write
1064 * \retval the buffer size in bytes if packing succeeds.
1065 * \retval negative errno if packing fails.
1067 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1068 const struct lu_buf *buf, loff_t *pos,
1069 struct thandle *th, int ignore_quota)
1071 struct osp_object *obj = dt2osp_obj(dt);
1072 struct osp_update_request *update;
1073 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1077 update = thandle_to_osp_update_request(th);
1078 LASSERT(update != NULL);
1080 CDEBUG(D_INFO, "write "DFID" offset = %llu length = %zu\n",
1081 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1083 rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1084 lu_object_fid(&dt->do_lu), buf, *pos);
1088 rc = osp_check_and_set_rpc_version(oth, obj);
1092 /* XXX: how about the write error happened later? */
1093 *pos += buf->lb_len;
1095 if (obj->opo_attr.la_valid & LA_SIZE && obj->opo_attr.la_size < *pos)
1096 obj->opo_attr.la_size = *pos;
1098 spin_lock(&obj->opo_lock);
1099 if (list_empty(&obj->opo_invalidate_cb_list)) {
1100 lu_object_get(&obj->opo_obj.do_lu);
1102 list_add_tail(&obj->opo_invalidate_cb_list,
1103 &update->our_invalidate_cb_list);
1105 spin_unlock(&obj->opo_lock);
1107 RETURN(buf->lb_len);
1110 static inline void orr_le_to_cpu(struct out_read_reply *orr_dst,
1111 const struct out_read_reply *orr_src)
1113 orr_dst->orr_size = le32_to_cpu(orr_src->orr_size);
1114 orr_dst->orr_padding = le32_to_cpu(orr_src->orr_padding);
1115 orr_dst->orr_offset = le64_to_cpu(orr_dst->orr_offset);
1120 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1121 struct lu_buf *rbuf, loff_t *pos)
1123 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1124 struct dt_device *dt_dev = &osp->opd_dt_dev;
1125 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1126 char *ptr = rbuf->lb_buf;
1127 struct osp_update_request *update;
1128 struct ptlrpc_request *req = NULL;
1129 struct out_read_reply *orr;
1130 struct ptlrpc_bulk_desc *desc;
1131 struct object_update_reply *reply;
1138 /* Because it needs send the update buffer right away,
1139 * just create an update buffer, instead of attaching the
1140 * update_remote list of the thandle. */
1141 update = osp_update_request_create(dt_dev);
1143 RETURN(PTR_ERR(update));
1145 rc = osp_update_rpc_pack(env, read, update, OUT_READ,
1146 lu_object_fid(&dt->do_lu),
1147 rbuf->lb_len, *pos);
1149 CERROR("%s: cannot insert update: rc = %d\n",
1150 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1151 GOTO(out_update, rc);
1154 CDEBUG(D_INFO, "%s "DFID" read offset %llu size %zu\n",
1155 dt_dev->dd_lu_dev.ld_obd->obd_name,
1156 PFID(lu_object_fid(&dt->do_lu)), *pos, rbuf->lb_len);
1157 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1160 GOTO(out_update, rc);
1162 nbufs = (rbuf->lb_len + OUT_BULK_BUFFER_SIZE - 1) /
1163 OUT_BULK_BUFFER_SIZE;
1164 /* allocate bulk descriptor */
1165 desc = ptlrpc_prep_bulk_imp(req, nbufs, 1,
1166 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KVEC,
1167 MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
1169 GOTO(out, rc = -ENOMEM);
1171 /* split the buffer into small chunk size */
1172 left_size = rbuf->lb_len;
1173 for (i = 0; i < nbufs; i++) {
1176 read_size = left_size > OUT_BULK_BUFFER_SIZE ?
1177 OUT_BULK_BUFFER_SIZE : left_size;
1178 desc->bd_frag_ops->add_iov_frag(desc, ptr, read_size);
1183 osp_set_req_replay(osp, req);
1184 req->rq_bulk_read = 1;
1185 /* send request to master and wait for RPC to complete */
1186 rc = ptlrpc_queue_wait(req);
1190 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1191 req->rq_bulk->bd_nob_transferred);
1195 reply = req_capsule_server_sized_get(&req->rq_pill,
1196 &RMF_OUT_UPDATE_REPLY,
1197 OUT_UPDATE_REPLY_SIZE);
1199 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1200 CERROR("%s: invalid update reply magic %x expected %x:"
1201 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1202 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1203 GOTO(out, rc = -EPROTO);
1206 rc = object_update_result_data_get(reply, lbuf, 0);
1210 if (lbuf->lb_len < sizeof(*orr))
1211 GOTO(out, rc = -EPROTO);
1214 orr_le_to_cpu(orr, orr);
1216 *pos = orr->orr_offset;
1218 ptlrpc_req_finished(req);
1221 osp_update_request_destroy(env, update);
1226 /* These body operation will be used to write symlinks during migration etc */
1227 struct dt_body_operations osp_md_body_ops = {
1228 .dbo_declare_write = osp_md_declare_write,
1229 .dbo_write = osp_md_write,
1230 .dbo_read = osp_md_read,