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 <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;
91 /* Invalid the opo cache for the object after the object
92 * is being created, so attr_get will try to get attr
93 * from the remote object. XXX this can be improved when
94 * we have object lock/cache invalidate mechanism in OSP
96 if (obj->opo_ooa != NULL) {
97 spin_lock(&obj->opo_lock);
98 obj->opo_ooa->ooa_attr.la_valid = 0;
99 spin_unlock(&obj->opo_lock);
106 * Implementation of dt_object_operations::do_declare_create
108 * Create the osp_update_request to track the update for this OSP
109 * in the transaction.
111 * \param[in] env execution environment
112 * \param[in] dt remote object to be created
113 * \param[in] attr attribute of the created object
114 * \param[in] hint creation hint
115 * \param[in] dof creation format information
116 * \param[in] th the transaction handle
118 * \retval 0 if preparation succeeds.
119 * \retval negative errno if preparation fails.
121 int osp_md_declare_object_create(const struct lu_env *env,
122 struct dt_object *dt,
123 struct lu_attr *attr,
124 struct dt_allocation_hint *hint,
125 struct dt_object_format *dof,
128 struct osp_object *obj = dt2osp_obj(dt);
131 if (obj->opo_ooa == NULL) {
132 rc = osp_oac_init(obj);
137 return osp_trans_update_request_create(th);
140 struct object_update *
141 update_buffer_get_update(struct object_update_request *request,
147 if (index > request->ourq_count)
150 ptr = &request->ourq_updates[0];
151 for (i = 0; i < index; i++)
152 ptr += object_update_size(ptr);
158 * Implementation of dt_object_operations::do_create
160 * It adds an OUT_CREATE sub-request into the OUT RPC that will be flushed
161 * when the transaction stop, and sets necessary flags for created object.
163 * \param[in] env execution environment
164 * \param[in] dt object to be created
165 * \param[in] attr attribute of the created object
166 * \param[in] hint creation hint
167 * \param[in] dof creation format information
168 * \param[in] th the transaction handle
170 * \retval 0 if packing creation succeeds.
171 * \retval negative errno if packing creation fails.
173 int osp_md_object_create(const struct lu_env *env, struct dt_object *dt,
174 struct lu_attr *attr, struct dt_allocation_hint *hint,
175 struct dt_object_format *dof, struct thandle *th)
177 struct osp_update_request *update;
178 struct osp_object *obj = dt2osp_obj(dt);
181 update = thandle_to_osp_update_request(th);
182 LASSERT(update != NULL);
184 LASSERT(attr->la_valid & LA_TYPE);
185 rc = osp_update_rpc_pack(env, create, update, OUT_CREATE,
186 lu_object_fid(&dt->do_lu), attr, hint, dof);
190 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
191 osp_object_create_interpreter);
196 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
197 dt2osp_obj(dt)->opo_non_exist = 0;
199 LASSERT(obj->opo_ooa != NULL);
200 obj->opo_ooa->ooa_attr = *attr;
206 * Implementation of dt_object_operations::do_declare_ref_del
208 * Create the osp_update_request to track the update for this OSP
209 * in the transaction.
211 * \param[in] env execution environment
212 * \param[in] dt object to decrease the reference count.
213 * \param[in] th the transaction handle of refcount decrease.
215 * \retval 0 if preparation succeeds.
216 * \retval negative errno if preparation fails.
218 static int osp_md_declare_ref_del(const struct lu_env *env,
219 struct dt_object *dt, struct thandle *th)
221 return osp_trans_update_request_create(th);
225 * Implementation of dt_object_operations::do_ref_del
227 * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
228 * flushed when the transaction stop.
230 * \param[in] env execution environment
231 * \param[in] dt object to decrease the reference count
232 * \param[in] th the transaction handle
234 * \retval 0 if packing ref_del succeeds.
235 * \retval negative errno if packing fails.
237 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
240 struct osp_update_request *update;
243 update = thandle_to_osp_update_request(th);
244 LASSERT(update != NULL);
246 rc = osp_update_rpc_pack(env, ref_del, update, OUT_REF_DEL,
247 lu_object_fid(&dt->do_lu));
252 * Implementation of dt_object_operations::do_declare_ref_del
254 * Create the osp_update_request to track the update for this OSP
255 * in the transaction.
257 * \param[in] env execution environment
258 * \param[in] dt object on which to increase the reference count.
259 * \param[in] th the transaction handle.
261 * \retval 0 if preparation succeeds.
262 * \retval negative errno if preparation fails.
264 static int osp_md_declare_ref_add(const struct lu_env *env,
265 struct dt_object *dt, struct thandle *th)
267 return osp_trans_update_request_create(th);
271 * Implementation of dt_object_operations::do_ref_add
273 * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
274 * when the transaction stop.
276 * \param[in] env execution environment
277 * \param[in] dt object on which to increase the reference count
278 * \param[in] th the transaction handle
280 * \retval 0 if packing ref_add succeeds.
281 * \retval negative errno if packing fails.
283 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
286 struct osp_update_request *update;
289 update = thandle_to_osp_update_request(th);
290 LASSERT(update != NULL);
292 rc = osp_update_rpc_pack(env, ref_add, update, OUT_REF_ADD,
293 lu_object_fid(&dt->do_lu));
298 * Implementation of dt_object_operations::do_ah_init
300 * Initialize the allocation hint for object creation, which is usually called
301 * before the creation, and these hints (parent and child mode) will be sent to
302 * the remote Object Update Target (OUT) and used in the object create process,
303 * same as OSD object creation.
305 * \param[in] env execution environment
306 * \param[in] ah the hint to be initialized
307 * \param[in] parent the parent of the object
308 * \param[in] child the object to be created
309 * \param[in] child_mode the mode of the created object
311 static void osp_md_ah_init(const struct lu_env *env,
312 struct dt_allocation_hint *ah,
313 struct dt_object *parent,
314 struct dt_object *child,
319 ah->dah_parent = parent;
320 ah->dah_mode = child_mode;
324 * Implementation of dt_object_operations::do_declare_attr_get
326 * Create the osp_update_request to track the update for this OSP
327 * in the transaction.
329 * \param[in] env execution environment
330 * \param[in] dt object on which to set attributes
331 * \param[in] attr attributes to be set
332 * \param[in] th the transaction handle
334 * \retval 0 if preparation succeeds.
335 * \retval negative errno if preparation fails.
337 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
338 const struct lu_attr *attr, struct thandle *th)
340 return osp_trans_update_request_create(th);
344 * Implementation of dt_object_operations::do_attr_set
346 * Set attributes to the specified remote object.
348 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
349 * when the transaction stop.
351 * \param[in] env execution environment
352 * \param[in] dt object to set attributes
353 * \param[in] attr attributes to be set
354 * \param[in] th the transaction handle
356 * \retval 0 if packing attr_set succeeds.
357 * \retval negative errno if packing fails.
359 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
360 const struct lu_attr *attr, struct thandle *th)
362 struct osp_update_request *update;
365 update = thandle_to_osp_update_request(th);
366 LASSERT(update != NULL);
368 rc = osp_update_rpc_pack(env, attr_set, update, OUT_ATTR_SET,
369 lu_object_fid(&dt->do_lu), attr);
374 * Implementation of dt_object_operations::do_read_lock
376 * osp_md_object_{read,write}_lock() will only lock the remote object in the
377 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
378 * lock the object. Note: it will not lock the object in the whole cluster,
379 * which relies on the LDLM lock.
381 * \param[in] env execution environment
382 * \param[in] dt object to be locked
383 * \param[in] role lock role from MDD layer, see mdd_object_role().
385 static void osp_md_object_read_lock(const struct lu_env *env,
386 struct dt_object *dt, unsigned role)
388 struct osp_object *obj = dt2osp_obj(dt);
390 LASSERT(obj->opo_owner != env);
391 down_read_nested(&obj->opo_sem, role);
393 LASSERT(obj->opo_owner == NULL);
397 * Implementation of dt_object_operations::do_write_lock
399 * Lock the remote object in write mode.
401 * \param[in] env execution environment
402 * \param[in] dt object to be locked
403 * \param[in] role lock role from MDD layer, see mdd_object_role().
405 static void osp_md_object_write_lock(const struct lu_env *env,
406 struct dt_object *dt, unsigned role)
408 struct osp_object *obj = dt2osp_obj(dt);
410 down_write_nested(&obj->opo_sem, role);
412 LASSERT(obj->opo_owner == NULL);
413 obj->opo_owner = env;
417 * Implementation of dt_object_operations::do_read_unlock
419 * Unlock the read lock of remote object.
421 * \param[in] env execution environment
422 * \param[in] dt object to be unlocked
424 static void osp_md_object_read_unlock(const struct lu_env *env,
425 struct dt_object *dt)
427 struct osp_object *obj = dt2osp_obj(dt);
429 up_read(&obj->opo_sem);
433 * Implementation of dt_object_operations::do_write_unlock
435 * Unlock the write lock of remote object.
437 * \param[in] env execution environment
438 * \param[in] dt object to be unlocked
440 static void osp_md_object_write_unlock(const struct lu_env *env,
441 struct dt_object *dt)
443 struct osp_object *obj = dt2osp_obj(dt);
445 LASSERT(obj->opo_owner == env);
446 obj->opo_owner = NULL;
447 up_write(&obj->opo_sem);
451 * Implementation of dt_object_operations::do_write_locked
453 * Test if the object is locked in write mode.
455 * \param[in] env execution environment
456 * \param[in] dt object to be tested
458 static int osp_md_object_write_locked(const struct lu_env *env,
459 struct dt_object *dt)
461 struct osp_object *obj = dt2osp_obj(dt);
463 return obj->opo_owner == env;
467 * Implementation of dt_index_operations::dio_lookup
469 * Look up record by key under a remote index object. It packs lookup update
470 * into RPC, sends to the remote OUT and waits for the lookup result.
472 * \param[in] env execution environment
473 * \param[in] dt index object to lookup
474 * \param[out] rec record in which to return lookup result
475 * \param[in] key key of index which will be looked up
477 * \retval 1 if the lookup succeeds.
478 * \retval negative errno if the lookup fails.
480 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
481 struct dt_rec *rec, const struct dt_key *key)
483 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
484 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
485 struct dt_device *dt_dev = &osp->opd_dt_dev;
486 struct osp_update_request *update;
487 struct object_update_reply *reply;
488 struct ptlrpc_request *req = NULL;
493 /* Because it needs send the update buffer right away,
494 * just create an update buffer, instead of attaching the
495 * update_remote list of the thandle.
497 update = osp_update_request_create(dt_dev);
499 RETURN(PTR_ERR(update));
501 rc = osp_update_rpc_pack(env, index_lookup, update, OUT_INDEX_LOOKUP,
502 lu_object_fid(&dt->do_lu), rec, key);
504 CERROR("%s: Insert update error: rc = %d\n",
505 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
509 rc = osp_remote_sync(env, osp, update, &req);
513 reply = req_capsule_server_sized_get(&req->rq_pill,
514 &RMF_OUT_UPDATE_REPLY,
515 OUT_UPDATE_REPLY_SIZE);
516 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
517 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
518 dt_dev->dd_lu_dev.ld_obd->obd_name,
519 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
520 GOTO(out, rc = -EPROTO);
523 rc = object_update_result_data_get(reply, lbuf, 0);
527 if (lbuf->lb_len != sizeof(*fid)) {
528 CERROR("%s: lookup "DFID" %s wrong size %d\n",
529 dt_dev->dd_lu_dev.ld_obd->obd_name,
530 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
532 GOTO(out, rc = -EINVAL);
536 if (ptlrpc_rep_need_swab(req))
537 lustre_swab_lu_fid(fid);
538 if (!fid_is_sane(fid)) {
539 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
540 dt_dev->dd_lu_dev.ld_obd->obd_name,
541 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
542 GOTO(out, rc = -EINVAL);
545 memcpy(rec, fid, sizeof(*fid));
551 ptlrpc_req_finished(req);
553 osp_update_request_destroy(update);
559 * Implementation of dt_index_operations::dio_declare_insert
561 * Create the osp_update_request to track the update for this OSP
562 * in the transaction.
564 * \param[in] env execution environment
565 * \param[in] dt object for which to insert index
566 * \param[in] rec record of the index which will be inserted
567 * \param[in] key key of the index which will be inserted
568 * \param[in] th the transaction handle
570 * \retval 0 if preparation succeeds.
571 * \retval negative errno if preparation fails.
573 static int osp_md_declare_index_insert(const struct lu_env *env,
574 struct dt_object *dt,
575 const struct dt_rec *rec,
576 const struct dt_key *key,
579 return osp_trans_update_request_create(th);
583 * Implementation of dt_index_operations::dio_insert
585 * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
586 * be flushed when the transaction stop.
588 * \param[in] env execution environment
589 * \param[in] dt object for which to insert index
590 * \param[in] rec record of the index to be inserted
591 * \param[in] key key of the index to be inserted
592 * \param[in] th the transaction handle
593 * \param[in] ignore_quota quota enforcement for insert
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,
599 struct dt_object *dt,
600 const struct dt_rec *rec,
601 const struct dt_key *key,
605 struct osp_update_request *update;
608 update = thandle_to_osp_update_request(th);
609 LASSERT(update != NULL);
611 rc = osp_update_rpc_pack(env, index_insert, update, OUT_INDEX_INSERT,
612 lu_object_fid(&dt->do_lu), rec, key);
617 * Implementation of dt_index_operations::dio_declare_delete
619 * Create the osp_update_request to track the update for this OSP
620 * in the transaction.
622 * \param[in] env execution environment
623 * \param[in] dt object for which to delete index
624 * \param[in] key key of the index
625 * \param[in] th the transaction handle
627 * \retval 0 if preparation succeeds.
628 * \retval negative errno if preparation fails.
630 static int osp_md_declare_index_delete(const struct lu_env *env,
631 struct dt_object *dt,
632 const struct dt_key *key,
635 return osp_trans_update_request_create(th);
639 * Implementation of dt_index_operations::dio_delete
641 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
642 * be flushed when the transaction stop.
644 * \param[in] env execution environment
645 * \param[in] dt object for which to delete index
646 * \param[in] key key of the index which will be deleted
647 * \param[in] th the transaction handle
649 * \retval 0 if packing index delete succeeds.
650 * \retval negative errno if packing fails.
652 static int osp_md_index_delete(const struct lu_env *env,
653 struct dt_object *dt,
654 const struct dt_key *key,
657 struct osp_update_request *update;
660 update = thandle_to_osp_update_request(th);
661 LASSERT(update != NULL);
663 rc = osp_update_rpc_pack(env, index_delete, update, OUT_INDEX_DELETE,
664 lu_object_fid(&dt->do_lu), key);
670 * Implementation of dt_index_operations::dio_it.next
672 * Advance the pointer of the iterator to the next entry. It shares a similar
673 * internal implementation with osp_orphan_it_next(), which is being used for
674 * remote orphan index object. This method will be used for remote directory.
676 * \param[in] env execution environment
677 * \param[in] di iterator of this iteration
679 * \retval 0 if the pointer is advanced successfully.
680 * \retval 1 if it reaches to the end of the index object.
681 * \retval negative errno if the pointer cannot be advanced.
683 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
685 struct osp_it *it = (struct osp_it *)di;
686 struct lu_idxpage *idxpage;
687 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
692 idxpage = it->ooi_cur_idxpage;
693 if (idxpage != NULL) {
694 if (idxpage->lip_nr == 0)
700 (struct lu_dirent *)idxpage->lip_entries;
702 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
703 it->ooi_pos_ent < idxpage->lip_nr) {
704 ent = (struct lu_dirent *)(((char *)ent) +
705 le16_to_cpu(ent->lde_reclen));
713 rc = osp_it_next_page(env, di);
721 * Implementation of dt_index_operations::dio_it.key
723 * Get the key at current iterator poisiton. These iteration methods
724 * (dio_it) will only be used for iterating the remote directory, so
725 * the key is the name of the directory entry.
727 * \param[in] env execution environment
728 * \param[in] di iterator of this iteration
730 * \retval name of the current entry
732 static struct dt_key *osp_it_key(const struct lu_env *env,
733 const struct dt_it *di)
735 struct osp_it *it = (struct osp_it *)di;
736 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
738 return (struct dt_key *)ent->lde_name;
742 * Implementation of dt_index_operations::dio_it.key_size
744 * Get the key size at current iterator poisiton. These iteration methods
745 * (dio_it) will only be used for iterating the remote directory, so the key
746 * size is the name size of the directory entry.
748 * \param[in] env execution environment
749 * \param[in] di iterator of this iteration
751 * \retval name size of the current entry
754 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
756 struct osp_it *it = (struct osp_it *)di;
757 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
759 return (int)le16_to_cpu(ent->lde_namelen);
763 * Implementation of dt_index_operations::dio_it.rec
765 * Get the record at current iterator position. These iteration methods
766 * (dio_it) will only be used for iterating the remote directory, so it
767 * uses lu_dirent_calc_size() to calculate the record size.
769 * \param[in] env execution environment
770 * \param[in] di iterator of this iteration
771 * \param[out] rec the record to be returned
772 * \param[in] attr attributes of the index object, so it knows
773 * how to pack the entry.
775 * \retval only return 0 for now
777 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
778 struct dt_rec *rec, __u32 attr)
780 struct osp_it *it = (struct osp_it *)di;
781 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
784 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
785 memcpy(rec, ent, reclen);
790 * Implementation of dt_index_operations::dio_it.load
792 * Locate the iteration cursor to the specified position (cookie).
794 * \param[in] env pointer to the thread context
795 * \param[in] di pointer to the iteration structure
796 * \param[in] hash the specified position
798 * \retval positive number for locating to the exactly position
800 * \retval 0 for arriving at the end of the iteration
801 * \retval negative error number on failure
803 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
806 struct osp_it *it = (struct osp_it *)di;
810 rc = osp_md_index_it_next(env, (struct dt_it *)di);
820 const struct dt_index_operations osp_md_index_ops = {
821 .dio_lookup = osp_md_index_lookup,
822 .dio_declare_insert = osp_md_declare_index_insert,
823 .dio_insert = osp_md_index_insert,
824 .dio_declare_delete = osp_md_declare_index_delete,
825 .dio_delete = osp_md_index_delete,
831 .next = osp_md_index_it_next,
833 .key_size = osp_it_key_size,
834 .rec = osp_md_index_it_rec,
835 .store = osp_it_store,
837 .key_rec = osp_it_key_rec,
842 * Implementation of dt_object_operations::do_index_try
844 * Try to initialize the index API pointer for the given object. This
845 * is the entry point of the index API, i.e. we must call this method
846 * to initialize the index object before calling other index methods.
848 * \param[in] env execution environment
849 * \param[in] dt index object to be initialized
850 * \param[in] feat the index feature of the object
852 * \retval 0 if the initialization succeeds.
853 * \retval negative errno if the initialization fails.
855 static int osp_md_index_try(const struct lu_env *env,
856 struct dt_object *dt,
857 const struct dt_index_features *feat)
859 dt->do_index_ops = &osp_md_index_ops;
864 * Implementation of dt_object_operations::do_object_lock
866 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
867 * which will lock the object in the global namespace. And because the
868 * cross-MDT locks are relatively rare compared with normal local MDT operation,
869 * let's release it right away, instead of putting it into the LRU list.
871 * \param[in] env execution environment
872 * \param[in] dt object to be locked
873 * \param[out] lh lock handle
874 * \param[in] einfo enqueue information
875 * \param[in] policy lock policy
877 * \retval ELDLM_OK if locking the object succeeds.
878 * \retval negative errno if locking fails.
880 static int osp_md_object_lock(const struct lu_env *env,
881 struct dt_object *dt,
882 struct lustre_handle *lh,
883 struct ldlm_enqueue_info *einfo,
884 union ldlm_policy_data *policy)
886 struct ldlm_res_id *res_id;
887 struct dt_device *dt_dev = lu2dt_dev(dt->do_lu.lo_dev);
888 struct osp_device *osp = dt2osp_dev(dt_dev);
889 struct lu_device *top_device;
890 struct ptlrpc_request *req;
892 __u64 flags = LDLM_FL_NO_LRU;
894 res_id = einfo->ei_res_id;
895 LASSERT(res_id != NULL);
897 if (einfo->ei_nonblock)
898 flags |= LDLM_FL_BLOCK_NOWAIT;
899 if (einfo->ei_mode & (LCK_EX | LCK_PW))
900 flags |= LDLM_FL_COS_INCOMPAT;
902 req = ldlm_enqueue_pack(osp->opd_exp, 0);
904 RETURN(PTR_ERR(req));
906 /* During recovery, it needs to let OSP send enqueue
907 * without checking recoverying status, in case the
908 * other target is being recovered at the same time,
909 * and if we wait here for the import to be recovered,
910 * it might cause deadlock */
911 top_device = dt_dev->dd_lu_dev.ld_site->ls_top_dev;
912 if (top_device->ld_obd->obd_recovering)
913 req->rq_allow_replay = 1;
915 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
916 (const union ldlm_policy_data *)policy,
917 &flags, NULL, 0, LVB_T_NONE, lh, 0);
919 ptlrpc_req_finished(req);
921 return rc == ELDLM_OK ? 0 : -EIO;
925 * Implementation of dt_object_operations::do_object_unlock
927 * Cancel a lock of a remote object.
929 * \param[in] env execution environment
930 * \param[in] dt object to be unlocked
931 * \param[in] einfo lock enqueue information
932 * \param[in] policy lock policy
934 * \retval Only return 0 for now.
936 static int osp_md_object_unlock(const struct lu_env *env,
937 struct dt_object *dt,
938 struct ldlm_enqueue_info *einfo,
939 union ldlm_policy_data *policy)
941 struct lustre_handle *lockh = einfo->ei_cbdata;
944 ldlm_lock_decref(lockh, einfo->ei_mode);
950 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
952 * Create the dt_update_request to track the update for this OSP
953 * in the transaction.
955 * \param[in] env pointer to the thread context
956 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
957 * \param[in] th pointer to the transaction handler
959 * \retval 0 for success
960 * \retval negative error number on failure
962 int osp_md_declare_object_destroy(const struct lu_env *env,
963 struct dt_object *dt, struct thandle *th)
965 return osp_trans_update_request_create(th);
969 * Implement OSP layer dt_object_operations::do_destroy() interface.
971 * Pack the destroy update into the RPC buffer, which will be sent
972 * to the remote MDT during transaction stop.
974 * It also marks the object as non-cached.
976 * \param[in] env pointer to the thread context
977 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
978 * \param[in] th pointer to the transaction handler
980 * \retval 0 for success
981 * \retval negative error number on failure
983 int osp_md_object_destroy(const struct lu_env *env, struct dt_object *dt,
986 struct osp_object *o = dt2osp_obj(dt);
987 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
988 struct osp_update_request *update;
992 o->opo_non_exist = 1;
994 LASSERT(osp->opd_connect_mdt);
995 update = thandle_to_osp_update_request(th);
996 LASSERT(update != NULL);
998 rc = osp_update_rpc_pack(env, object_destroy, update, OUT_DESTROY,
999 lu_object_fid(&dt->do_lu));
1003 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1004 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1010 struct dt_object_operations osp_md_obj_ops = {
1011 .do_read_lock = osp_md_object_read_lock,
1012 .do_write_lock = osp_md_object_write_lock,
1013 .do_read_unlock = osp_md_object_read_unlock,
1014 .do_write_unlock = osp_md_object_write_unlock,
1015 .do_write_locked = osp_md_object_write_locked,
1016 .do_declare_create = osp_md_declare_object_create,
1017 .do_create = osp_md_object_create,
1018 .do_declare_ref_add = osp_md_declare_ref_add,
1019 .do_ref_add = osp_md_ref_add,
1020 .do_declare_ref_del = osp_md_declare_ref_del,
1021 .do_ref_del = osp_md_ref_del,
1022 .do_declare_destroy = osp_md_declare_object_destroy,
1023 .do_destroy = osp_md_object_destroy,
1024 .do_ah_init = osp_md_ah_init,
1025 .do_attr_get = osp_attr_get,
1026 .do_declare_attr_set = osp_md_declare_attr_set,
1027 .do_attr_set = osp_md_attr_set,
1028 .do_xattr_get = osp_xattr_get,
1029 .do_declare_xattr_set = osp_declare_xattr_set,
1030 .do_xattr_set = osp_xattr_set,
1031 .do_declare_xattr_del = osp_declare_xattr_del,
1032 .do_xattr_del = osp_xattr_del,
1033 .do_index_try = osp_md_index_try,
1034 .do_object_lock = osp_md_object_lock,
1035 .do_object_unlock = osp_md_object_unlock,
1039 * Implementation of dt_body_operations::dbo_declare_write
1041 * Create the osp_update_request to track the update for this OSP
1042 * in the transaction.
1044 * \param[in] env execution environment
1045 * \param[in] dt object to be written
1046 * \param[in] buf buffer to write which includes an embedded size field
1047 * \param[in] pos offet in the object to start writing at
1048 * \param[in] th transaction handle
1050 * \retval 0 if preparation succeeds.
1051 * \retval negative errno if preparation fails.
1053 static ssize_t osp_md_declare_write(const struct lu_env *env,
1054 struct dt_object *dt,
1055 const struct lu_buf *buf,
1056 loff_t pos, struct thandle *th)
1058 struct osp_device *osp = dt2osp_dev(th->th_dev);
1061 rc = osp_trans_update_request_create(th);
1065 if (osp->opd_update == NULL)
1068 if (dt2osp_obj(dt)->opo_stale)
1075 * Implementation of dt_body_operations::dbo_write
1077 * Pack the write object update into the RPC buffer, which will be sent
1078 * to the remote MDT during transaction stop.
1080 * \param[in] env execution environment
1081 * \param[in] dt object to be written
1082 * \param[in] buf buffer to write which includes an embedded size field
1083 * \param[in] pos offet in the object to start writing at
1084 * \param[in] th transaction handle
1085 * \param[in] ignore_quota quota enforcement for this write
1087 * \retval the buffer size in bytes if packing succeeds.
1088 * \retval negative errno if packing fails.
1090 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1091 const struct lu_buf *buf, loff_t *pos,
1092 struct thandle *th, int ignore_quota)
1094 struct osp_object *obj = dt2osp_obj(dt);
1095 struct osp_update_request *update;
1096 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1100 update = thandle_to_osp_update_request(th);
1101 LASSERT(update != NULL);
1103 CDEBUG(D_INFO, "write "DFID" offset = "LPU64" length = %zu\n",
1104 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1106 rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1107 lu_object_fid(&dt->do_lu), buf, *pos);
1111 rc = osp_check_and_set_rpc_version(oth, obj);
1115 /* XXX: how about the write error happened later? */
1116 *pos += buf->lb_len;
1118 if (obj->opo_ooa != NULL &&
1119 obj->opo_ooa->ooa_attr.la_valid & LA_SIZE &&
1120 obj->opo_ooa->ooa_attr.la_size < *pos)
1121 obj->opo_ooa->ooa_attr.la_size = *pos;
1123 RETURN(buf->lb_len);
1126 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1127 struct lu_buf *rbuf, loff_t *pos)
1129 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1130 struct dt_device *dt_dev = &osp->opd_dt_dev;
1131 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1132 char *ptr = rbuf->lb_buf;
1133 struct osp_update_request *update;
1134 struct ptlrpc_request *req = NULL;
1135 struct out_read_reply *orr;
1136 struct ptlrpc_bulk_desc *desc;
1137 struct object_update_reply *reply;
1144 /* Because it needs send the update buffer right away,
1145 * just create an update buffer, instead of attaching the
1146 * update_remote list of the thandle. */
1147 update = osp_update_request_create(dt_dev);
1149 RETURN(PTR_ERR(update));
1151 rc = osp_update_rpc_pack(env, read, update, OUT_READ,
1152 lu_object_fid(&dt->do_lu),
1153 rbuf->lb_len, *pos);
1155 CERROR("%s: cannot insert update: rc = %d\n",
1156 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1157 GOTO(out_update, rc);
1160 CDEBUG(D_INFO, "%s "DFID" read offset %llu size %zu\n",
1161 dt_dev->dd_lu_dev.ld_obd->obd_name,
1162 PFID(lu_object_fid(&dt->do_lu)), *pos, rbuf->lb_len);
1163 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1166 GOTO(out_update, rc);
1168 nbufs = (rbuf->lb_len + OUT_BULK_BUFFER_SIZE - 1) /
1169 OUT_BULK_BUFFER_SIZE;
1170 /* allocate bulk descriptor */
1171 desc = ptlrpc_prep_bulk_imp(req, nbufs, 1,
1172 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KVEC,
1173 MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
1175 GOTO(out, rc = -ENOMEM);
1177 /* split the buffer into small chunk size */
1178 left_size = rbuf->lb_len;
1179 for (i = 0; i < nbufs; i++) {
1182 read_size = left_size > OUT_BULK_BUFFER_SIZE ?
1183 OUT_BULK_BUFFER_SIZE : left_size;
1184 desc->bd_frag_ops->add_iov_frag(desc, ptr, read_size);
1189 /* This will only be called with read-only update, and these updates
1190 * might be used to retrieve update log during recovery process, so
1191 * it will be allowed to send during recovery process */
1192 req->rq_allow_replay = 1;
1193 req->rq_bulk_read = 1;
1194 /* send request to master and wait for RPC to complete */
1195 rc = ptlrpc_queue_wait(req);
1199 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1200 req->rq_bulk->bd_nob_transferred);
1204 reply = req_capsule_server_sized_get(&req->rq_pill,
1205 &RMF_OUT_UPDATE_REPLY,
1206 OUT_UPDATE_REPLY_SIZE);
1208 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1209 CERROR("%s: invalid update reply magic %x expected %x:"
1210 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1211 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1212 GOTO(out, rc = -EPROTO);
1215 rc = object_update_result_data_get(reply, lbuf, 0);
1219 if (lbuf->lb_len < sizeof(*orr))
1220 GOTO(out, rc = -EPROTO);
1223 orr_le_to_cpu(orr, orr);
1225 *pos = orr->orr_offset;
1227 ptlrpc_req_finished(req);
1230 osp_update_request_destroy(update);
1235 /* These body operation will be used to write symlinks during migration etc */
1236 struct dt_body_operations osp_md_body_ops = {
1237 .dbo_declare_write = osp_md_declare_write,
1238 .dbo_write = osp_md_write,
1239 .dbo_read = osp_md_read,