4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2013, 2017, Intel Corporation.
26 * lustre/osp/osp_md_object.c
28 * OST/MDT proxy device (OSP) Metadata methods
30 * This file implements methods for remote MD object, which include
31 * dt_object_operations, dt_index_operations and dt_body_operations.
33 * If there are multiple MDTs in one filesystem, one operation might
34 * include modifications in several MDTs. In such cases, clients
35 * send the RPC to the master MDT, then the operation is decomposed into
36 * object updates which will be dispatched to OSD or OSP. The local updates
37 * go to local OSD and the remote updates go to OSP. In OSP, these remote
38 * object updates will be packed into an update RPC, sent to the remote MDT
39 * and handled by Object Update Target (OUT).
41 * In DNE phase I, because of missing complete recovery solution, updates
42 * will be executed in order and synchronously.
43 * 1. The transaction is created.
44 * 2. In transaction declare, it collects and packs remote
45 * updates (in osp_md_declare_xxx()).
46 * 3. In transaction start, it sends these remote updates
47 * to remote MDTs, which will execute these updates synchronously.
48 * 4. In transaction execute phase, the local updates will be executed
51 * Author: Di Wang <di.wang@intel.com>
54 #define DEBUG_SUBSYSTEM S_MDS
56 #include <llog_swab.h>
57 #include <lustre_log.h>
58 #include "osp_internal.h"
60 #define OUT_UPDATE_BUFFER_SIZE_ADD 4096
61 #define OUT_UPDATE_BUFFER_SIZE_MAX (256 * 4096) /* 1M update size now */
64 * Interpreter call for object creation
66 * Object creation interpreter, which will be called after creating
67 * the remote object to set flags and status.
69 * \param[in] env execution environment
70 * \param[in] reply update reply
71 * \param[in] req ptlrpc update request for creating object
72 * \param[in] obj object to be created
73 * \param[in] data data used in this function.
74 * \param[in] index index(position) of create update in the whole
76 * \param[in] rc update result on the remote MDT.
78 * \retval only return 0 for now
80 static int osp_create_interpreter(const struct lu_env *env,
81 struct object_update_reply *reply,
82 struct ptlrpc_request *req,
83 struct osp_object *obj,
84 void *data, int index, int rc)
86 if (rc != 0 && rc != -EEXIST) {
87 obj->opo_obj.do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
88 obj->opo_non_exist = 1;
92 * invalidate opo cache for the object after the object is created, so
93 * attr_get will try to get attr from remote object.
95 osp_obj_invalidate_cache(obj);
101 * Implementation of dt_object_operations::do_declare_create
103 * Create the osp_update_request to track the update for this OSP
104 * in the transaction.
106 * \param[in] env execution environment
107 * \param[in] dt remote object to be created
108 * \param[in] attr attribute of the created object
109 * \param[in] hint creation hint
110 * \param[in] dof creation format information
111 * \param[in] th the transaction handle
113 * \retval 0 if preparation succeeds.
114 * \retval negative errno if preparation fails.
116 int osp_md_declare_create(const struct lu_env *env, struct dt_object *dt,
117 struct lu_attr *attr, struct dt_allocation_hint *hint,
118 struct dt_object_format *dof, struct thandle *th)
120 return osp_trans_update_request_create(th);
123 struct object_update *
124 update_buffer_get_update(struct object_update_request *request,
130 if (index > request->ourq_count)
133 ptr = &request->ourq_updates[0];
134 for (i = 0; i < index; i++)
135 ptr += object_update_size(ptr);
141 * Implementation of dt_object_operations::do_create
143 * It adds an OUT_CREATE sub-request into the OUT RPC that will be flushed
144 * when the transaction stop, and sets necessary flags for created object.
146 * \param[in] env execution environment
147 * \param[in] dt object to be created
148 * \param[in] attr attribute of the created object
149 * \param[in] hint creation hint
150 * \param[in] dof creation format information
151 * \param[in] th the transaction handle
153 * \retval 0 if packing creation succeeds.
154 * \retval negative errno if packing creation fails.
156 int osp_md_create(const struct lu_env *env, struct dt_object *dt,
157 struct lu_attr *attr, struct dt_allocation_hint *hint,
158 struct dt_object_format *dof, struct thandle *th)
160 struct osp_update_request *update;
161 struct osp_object *obj = dt2osp_obj(dt);
164 update = thandle_to_osp_update_request(th);
165 LASSERT(update != NULL);
167 LASSERT(attr->la_valid & LA_TYPE);
168 rc = osp_update_rpc_pack(env, create, update, OUT_CREATE,
169 lu_object_fid(&dt->do_lu), attr, hint, dof);
173 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
174 osp_create_interpreter);
179 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
180 dt2osp_obj(dt)->opo_non_exist = 0;
182 obj->opo_attr = *attr;
188 * Implementation of dt_object_operations::do_declare_ref_del
190 * Create the osp_update_request to track the update for this OSP
191 * in the transaction.
193 * \param[in] env execution environment
194 * \param[in] dt object to decrease the reference count.
195 * \param[in] th the transaction handle of refcount decrease.
197 * \retval 0 if preparation succeeds.
198 * \retval negative errno if preparation fails.
200 static int osp_md_declare_ref_del(const struct lu_env *env,
201 struct dt_object *dt, struct thandle *th)
203 return osp_trans_update_request_create(th);
207 * Implementation of dt_object_operations::do_ref_del
209 * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
210 * flushed when the transaction stop.
212 * \param[in] env execution environment
213 * \param[in] dt object to decrease the reference count
214 * \param[in] th the transaction handle
216 * \retval 0 if packing ref_del succeeds.
217 * \retval negative errno if packing fails.
219 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
222 struct osp_update_request *update;
225 update = thandle_to_osp_update_request(th);
226 LASSERT(update != NULL);
228 rc = osp_update_rpc_pack(env, ref_del, update, OUT_REF_DEL,
229 lu_object_fid(&dt->do_lu));
234 * Implementation of dt_object_operations::do_declare_ref_del
236 * Create the osp_update_request to track the update for this OSP
237 * in the transaction.
239 * \param[in] env execution environment
240 * \param[in] dt object on which to increase the reference count.
241 * \param[in] th the transaction handle.
243 * \retval 0 if preparation succeeds.
244 * \retval negative errno if preparation fails.
246 static int osp_md_declare_ref_add(const struct lu_env *env,
247 struct dt_object *dt, struct thandle *th)
249 return osp_trans_update_request_create(th);
253 * Implementation of dt_object_operations::do_ref_add
255 * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
256 * when the transaction stop.
258 * \param[in] env execution environment
259 * \param[in] dt object on which to increase the reference count
260 * \param[in] th the transaction handle
262 * \retval 0 if packing ref_add succeeds.
263 * \retval negative errno if packing fails.
265 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
268 struct osp_update_request *update;
271 update = thandle_to_osp_update_request(th);
272 LASSERT(update != NULL);
274 rc = osp_update_rpc_pack(env, ref_add, update, OUT_REF_ADD,
275 lu_object_fid(&dt->do_lu));
280 * Implementation of dt_object_operations::do_ah_init
282 * Initialize the allocation hint for object creation, which is usually called
283 * before the creation, and these hints (parent and child mode) will be sent to
284 * the remote Object Update Target (OUT) and used in the object create process,
285 * same as OSD object creation.
287 * \param[in] env execution environment
288 * \param[in] ah the hint to be initialized
289 * \param[in] parent the parent of the object
290 * \param[in] child the object to be created
291 * \param[in] child_mode the mode of the created object
293 static void osp_md_ah_init(const struct lu_env *env,
294 struct dt_allocation_hint *ah,
295 struct dt_object *parent,
296 struct dt_object *child,
301 ah->dah_parent = parent;
302 ah->dah_mode = child_mode;
306 * Implementation of dt_object_operations::do_declare_attr_get
308 * Create the osp_update_request to track the update for this OSP
309 * in the transaction.
311 * \param[in] env execution environment
312 * \param[in] dt object on which to set attributes
313 * \param[in] attr attributes to be set
314 * \param[in] th the transaction handle
316 * \retval 0 if preparation succeeds.
317 * \retval negative errno if preparation fails.
319 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
320 const struct lu_attr *attr, struct thandle *th)
322 return osp_trans_update_request_create(th);
326 * Implementation of dt_object_operations::do_attr_set
328 * Set attributes to the specified remote object.
330 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
331 * when the transaction stop.
333 * \param[in] env execution environment
334 * \param[in] dt object to set attributes
335 * \param[in] attr attributes to be set
336 * \param[in] th the transaction handle
338 * \retval 0 if packing attr_set succeeds.
339 * \retval negative errno if packing fails.
341 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
342 const struct lu_attr *attr, struct thandle *th)
344 struct osp_update_request *update;
347 update = thandle_to_osp_update_request(th);
348 LASSERT(update != NULL);
350 rc = osp_update_rpc_pack(env, attr_set, update, OUT_ATTR_SET,
351 lu_object_fid(&dt->do_lu), attr);
356 * Implementation of dt_object_operations::do_read_lock
358 * osp_md_{read,write}_lock() will only lock the remote object in the
359 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
360 * lock the object. Note: it will not lock the object in the whole cluster,
361 * which relies on the LDLM lock.
363 * \param[in] env execution environment
364 * \param[in] dt object to be locked
365 * \param[in] role lock role from MDD layer, see mdd_object_role().
367 static void osp_md_read_lock(const struct lu_env *env, struct dt_object *dt,
370 struct osp_object *obj = dt2osp_obj(dt);
372 LASSERT(obj->opo_owner != env);
373 down_read_nested(&obj->opo_sem, role);
375 LASSERT(obj->opo_owner == NULL);
379 * Implementation of dt_object_operations::do_write_lock
381 * Lock the remote object in write mode.
383 * \param[in] env execution environment
384 * \param[in] dt object to be locked
385 * \param[in] role lock role from MDD layer, see mdd_object_role().
387 static void osp_md_write_lock(const struct lu_env *env, struct dt_object *dt,
390 struct osp_object *obj = dt2osp_obj(dt);
392 down_write_nested(&obj->opo_sem, role);
394 LASSERT(obj->opo_owner == NULL);
395 obj->opo_owner = env;
399 * Implementation of dt_object_operations::do_read_unlock
401 * Unlock the read lock of remote object.
403 * \param[in] env execution environment
404 * \param[in] dt object to be unlocked
406 static void osp_md_read_unlock(const struct lu_env *env, struct dt_object *dt)
408 struct osp_object *obj = dt2osp_obj(dt);
410 up_read(&obj->opo_sem);
414 * Implementation of dt_object_operations::do_write_unlock
416 * Unlock the write lock of remote object.
418 * \param[in] env execution environment
419 * \param[in] dt object to be unlocked
421 static void osp_md_write_unlock(const struct lu_env *env, struct dt_object *dt)
423 struct osp_object *obj = dt2osp_obj(dt);
425 LASSERT(obj->opo_owner == env);
426 obj->opo_owner = NULL;
427 up_write(&obj->opo_sem);
431 * Implementation of dt_object_operations::do_write_locked
433 * Test if the object is locked in write mode.
435 * \param[in] env execution environment
436 * \param[in] dt object to be tested
438 static int osp_md_write_locked(const struct lu_env *env, struct dt_object *dt)
440 struct osp_object *obj = dt2osp_obj(dt);
442 return obj->opo_owner == env;
446 * Implementation of dt_index_operations::dio_lookup
448 * Look up record by key under a remote index object. It packs lookup update
449 * into RPC, sends to the remote OUT and waits for the lookup result.
451 * \param[in] env execution environment
452 * \param[in] dt index object to lookup
453 * \param[out] rec record in which to return lookup result
454 * \param[in] key key of index which will be looked up
456 * \retval 1 if the lookup succeeds.
457 * \retval negative errno if the lookup fails.
459 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
460 struct dt_rec *rec, const struct dt_key *key)
462 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
463 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
464 struct dt_device *dt_dev = &osp->opd_dt_dev;
465 struct osp_update_request *update;
466 struct object_update_reply *reply;
467 struct ptlrpc_request *req = NULL;
472 /* Because it needs send the update buffer right away,
473 * just create an update buffer, instead of attaching the
474 * update_remote list of the thandle.
476 update = osp_update_request_create(dt_dev);
478 RETURN(PTR_ERR(update));
480 rc = osp_update_rpc_pack(env, index_lookup, update, OUT_INDEX_LOOKUP,
481 lu_object_fid(&dt->do_lu), rec, key);
483 CERROR("%s: Insert update error: rc = %d\n",
484 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
488 rc = osp_remote_sync(env, osp, update, &req);
492 reply = req_capsule_server_sized_get(&req->rq_pill,
493 &RMF_OUT_UPDATE_REPLY,
494 OUT_UPDATE_REPLY_SIZE);
495 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
496 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
497 dt_dev->dd_lu_dev.ld_obd->obd_name,
498 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
499 GOTO(out, rc = -EPROTO);
502 rc = object_update_result_data_get(reply, lbuf, 0);
506 if (lbuf->lb_len != sizeof(*fid)) {
507 CERROR("%s: lookup "DFID" %s wrong size %d\n",
508 dt_dev->dd_lu_dev.ld_obd->obd_name,
509 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
511 GOTO(out, rc = -EINVAL);
515 if (ptlrpc_rep_need_swab(req))
516 lustre_swab_lu_fid(fid);
517 if (!fid_is_sane(fid)) {
518 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
519 dt_dev->dd_lu_dev.ld_obd->obd_name,
520 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
521 GOTO(out, rc = -EINVAL);
524 memcpy(rec, fid, sizeof(*fid));
530 ptlrpc_req_finished(req);
532 osp_update_request_destroy(env, update);
538 * Implementation of dt_index_operations::dio_declare_insert
540 * Create the osp_update_request to track the update for this OSP
541 * in the transaction.
543 * \param[in] env execution environment
544 * \param[in] dt object for which to insert index
545 * \param[in] rec record of the index which will be inserted
546 * \param[in] key key of the index which will be inserted
547 * \param[in] th the transaction handle
549 * \retval 0 if preparation succeeds.
550 * \retval negative errno if preparation fails.
552 static int osp_md_declare_index_insert(const struct lu_env *env,
553 struct dt_object *dt,
554 const struct dt_rec *rec,
555 const struct dt_key *key,
558 return osp_trans_update_request_create(th);
562 * Implementation of dt_index_operations::dio_insert
564 * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
565 * be flushed when the transaction stop.
567 * \param[in] env execution environment
568 * \param[in] dt object for which to insert index
569 * \param[in] rec record of the index to be inserted
570 * \param[in] key key of the index to be inserted
571 * \param[in] th the transaction handle
572 * \param[in] ignore_quota quota enforcement for insert
574 * \retval 0 if packing index insert succeeds.
575 * \retval negative errno if packing fails.
577 static int osp_md_index_insert(const struct lu_env *env,
578 struct dt_object *dt,
579 const struct dt_rec *rec,
580 const struct dt_key *key,
584 struct osp_update_request *update;
587 update = thandle_to_osp_update_request(th);
588 LASSERT(update != NULL);
590 rc = osp_update_rpc_pack(env, index_insert, update, OUT_INDEX_INSERT,
591 lu_object_fid(&dt->do_lu), rec, key);
596 * Implementation of dt_index_operations::dio_declare_delete
598 * Create the osp_update_request to track the update for this OSP
599 * in the transaction.
601 * \param[in] env execution environment
602 * \param[in] dt object for which to delete index
603 * \param[in] key key of the index
604 * \param[in] th the transaction handle
606 * \retval 0 if preparation succeeds.
607 * \retval negative errno if preparation fails.
609 static int osp_md_declare_index_delete(const struct lu_env *env,
610 struct dt_object *dt,
611 const struct dt_key *key,
614 return osp_trans_update_request_create(th);
618 * Implementation of dt_index_operations::dio_delete
620 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
621 * be flushed when the transaction stop.
623 * \param[in] env execution environment
624 * \param[in] dt object for which to delete index
625 * \param[in] key key of the index which will be deleted
626 * \param[in] th the transaction handle
628 * \retval 0 if packing index delete succeeds.
629 * \retval negative errno if packing fails.
631 static int osp_md_index_delete(const struct lu_env *env,
632 struct dt_object *dt,
633 const struct dt_key *key,
636 struct osp_update_request *update;
639 update = thandle_to_osp_update_request(th);
640 LASSERT(update != NULL);
642 rc = osp_update_rpc_pack(env, index_delete, update, OUT_INDEX_DELETE,
643 lu_object_fid(&dt->do_lu), key);
649 * Implementation of dt_index_operations::dio_it.next
651 * Advance the pointer of the iterator to the next entry. It shares a similar
652 * internal implementation with osp_orphan_it_next(), which is being used for
653 * remote orphan index object. This method will be used for remote directory.
655 * \param[in] env execution environment
656 * \param[in] di iterator of this iteration
658 * \retval 0 if the pointer is advanced successfully.
659 * \retval 1 if it reaches to the end of the index object.
660 * \retval negative errno if the pointer cannot be advanced.
662 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
664 struct osp_it *it = (struct osp_it *)di;
665 struct lu_idxpage *idxpage;
666 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
671 idxpage = it->ooi_cur_idxpage;
672 if (idxpage != NULL) {
673 if (idxpage->lip_nr == 0)
679 (struct lu_dirent *)idxpage->lip_entries;
681 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
682 it->ooi_pos_ent < idxpage->lip_nr) {
683 ent = (struct lu_dirent *)(((char *)ent) +
684 le16_to_cpu(ent->lde_reclen));
692 rc = osp_it_next_page(env, di);
700 * Implementation of dt_index_operations::dio_it.key
702 * Get the key at current iterator poisiton. These iteration methods
703 * (dio_it) will only be used for iterating the remote directory, so
704 * the key is the name of the directory entry.
706 * \param[in] env execution environment
707 * \param[in] di iterator of this iteration
709 * \retval name of the current entry
711 static struct dt_key *osp_it_key(const struct lu_env *env,
712 const struct dt_it *di)
714 struct osp_it *it = (struct osp_it *)di;
715 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
717 return (struct dt_key *)ent->lde_name;
721 * Implementation of dt_index_operations::dio_it.key_size
723 * Get the key size at current iterator poisiton. These iteration methods
724 * (dio_it) will only be used for iterating the remote directory, so the key
725 * size is the name size of the directory entry.
727 * \param[in] env execution environment
728 * \param[in] di iterator of this iteration
730 * \retval name size of the current entry
733 static int osp_it_key_size(const struct lu_env *env, 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 (int)le16_to_cpu(ent->lde_namelen);
742 * Implementation of dt_index_operations::dio_it.rec
744 * Get the record at current iterator position. These iteration methods
745 * (dio_it) will only be used for iterating the remote directory, so it
746 * uses lu_dirent_calc_size() to calculate the record size.
748 * \param[in] env execution environment
749 * \param[in] di iterator of this iteration
750 * \param[out] rec the record to be returned
751 * \param[in] attr attributes of the index object, so it knows
752 * how to pack the entry.
754 * \retval only return 0 for now
756 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
757 struct dt_rec *rec, __u32 attr)
759 struct osp_it *it = (struct osp_it *)di;
760 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
763 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
764 memcpy(rec, ent, reclen);
769 * Implementation of dt_index_operations::dio_it.load
771 * Locate the iteration cursor to the specified position (cookie).
773 * \param[in] env pointer to the thread context
774 * \param[in] di pointer to the iteration structure
775 * \param[in] hash the specified position
777 * \retval positive number for locating to the exactly position
779 * \retval 0 for arriving at the end of the iteration
780 * \retval negative error number on failure
782 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
785 struct osp_it *it = (struct osp_it *)di;
789 rc = osp_md_index_it_next(env, (struct dt_it *)di);
799 const struct dt_index_operations osp_md_index_ops = {
800 .dio_lookup = osp_md_index_lookup,
801 .dio_declare_insert = osp_md_declare_index_insert,
802 .dio_insert = osp_md_index_insert,
803 .dio_declare_delete = osp_md_declare_index_delete,
804 .dio_delete = osp_md_index_delete,
810 .next = osp_md_index_it_next,
812 .key_size = osp_it_key_size,
813 .rec = osp_md_index_it_rec,
814 .store = osp_it_store,
816 .key_rec = osp_it_key_rec,
821 * Implement OSP layer dt_object_operations::do_xattr_list() interface.
823 * List extended attribute from the specified MDT/OST object, result is not
824 * cached because this is called by directory migration only.
826 * \param[in] env pointer to the thread context
827 * \param[in] dt pointer to the OSP layer dt_object
828 * \param[out] buf pointer to the lu_buf to hold the extended attribute
830 * \retval positive bytes used/required in the buffer
831 * \retval negative error number on failure
833 static int osp_md_xattr_list(const struct lu_env *env, struct dt_object *dt,
834 const struct lu_buf *buf)
836 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
837 struct osp_object *obj = dt2osp_obj(dt);
838 struct dt_device *dev = &osp->opd_dt_dev;
839 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
840 struct osp_update_request *update = NULL;
841 struct ptlrpc_request *req = NULL;
842 struct object_update_reply *reply;
843 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
850 if (unlikely(obj->opo_non_exist))
853 update = osp_update_request_create(dev);
855 RETURN(PTR_ERR(update));
857 rc = osp_update_rpc_pack(env, xattr_list, update, OUT_XATTR_LIST,
858 lu_object_fid(&dt->do_lu), buf->lb_len);
860 CERROR("%s: Insert update error "DFID": rc = %d\n",
861 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
865 rc = osp_remote_sync(env, osp, update, &req);
868 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
869 obj->opo_non_exist = 1;
874 reply = req_capsule_server_sized_get(&req->rq_pill,
875 &RMF_OUT_UPDATE_REPLY,
876 OUT_UPDATE_REPLY_SIZE);
877 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
878 DEBUG_REQ(D_ERROR, req,
879 "%s: Wrong version %x expected %x "DFID": rc = %d\n",
880 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
881 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
883 GOTO(out, rc = -EPROTO);
886 rc = object_update_result_data_get(reply, rbuf, 0);
893 if (unlikely(buf->lb_len < rbuf->lb_len))
894 GOTO(out, rc = -ERANGE);
896 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
901 ptlrpc_req_finished(req);
903 if (update && !IS_ERR(update))
904 osp_update_request_destroy(env, update);
910 * Implementation of dt_object_operations::do_index_try
912 * Try to initialize the index API pointer for the given object. This
913 * is the entry point of the index API, i.e. we must call this method
914 * to initialize the index object before calling other index methods.
916 * \param[in] env execution environment
917 * \param[in] dt index object to be initialized
918 * \param[in] feat the index feature of the object
920 * \retval 0 if the initialization succeeds.
921 * \retval negative errno if the initialization fails.
923 static int osp_md_index_try(const struct lu_env *env,
924 struct dt_object *dt,
925 const struct dt_index_features *feat)
927 dt->do_index_ops = &osp_md_index_ops;
932 * Implementation of dt_object_operations::do_object_lock
934 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
935 * which will lock the object in the global namespace. And because the
936 * cross-MDT locks are relatively rare compared with normal local MDT operation,
937 * let's release it right away, instead of putting it into the LRU list.
939 * \param[in] env execution environment
940 * \param[in] dt object to be locked
941 * \param[out] lh lock handle
942 * \param[in] einfo enqueue information
943 * \param[in] policy lock policy
945 * \retval ELDLM_OK if locking the object succeeds.
946 * \retval negative errno if locking fails.
948 static int osp_md_object_lock(const struct lu_env *env,
949 struct dt_object *dt,
950 struct lustre_handle *lh,
951 struct ldlm_enqueue_info *einfo,
952 union ldlm_policy_data *policy)
954 struct ldlm_res_id *res_id;
955 struct osp_device *osp = dt2osp_dev(lu2dt_dev(dt->do_lu.lo_dev));
956 struct ptlrpc_request *req;
958 __u64 flags = LDLM_FL_NO_LRU;
961 res_id = einfo->ei_res_id;
962 LASSERT(res_id != NULL);
964 if (einfo->ei_mode & (LCK_EX | LCK_PW))
965 flags |= LDLM_FL_COS_INCOMPAT;
967 req = ldlm_enqueue_pack(osp->opd_exp, 0);
969 RETURN(PTR_ERR(req));
971 osp_set_req_replay(osp, req);
972 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
973 (const union ldlm_policy_data *)policy,
974 &flags, NULL, 0, LVB_T_NONE, lh, 0);
976 ptlrpc_req_finished(req);
978 RETURN(rc == ELDLM_OK ? 0 : -EIO);
982 * Implementation of dt_object_operations::do_object_unlock
984 * Cancel a lock of a remote object.
986 * \param[in] env execution environment
987 * \param[in] dt object to be unlocked
988 * \param[in] einfo lock enqueue information
989 * \param[in] policy lock policy
991 * \retval Only return 0 for now.
993 static int osp_md_object_unlock(const struct lu_env *env,
994 struct dt_object *dt,
995 struct ldlm_enqueue_info *einfo,
996 union ldlm_policy_data *policy)
998 struct lustre_handle *lockh = einfo->ei_cbdata;
1000 /* unlock finally */
1001 ldlm_lock_decref(lockh, einfo->ei_mode);
1007 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1009 * Create the dt_update_request to track the update for this OSP
1010 * in the transaction.
1012 * \param[in] env pointer to the thread context
1013 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1014 * \param[in] th pointer to the transaction handler
1016 * \retval 0 for success
1017 * \retval negative error number on failure
1019 int osp_md_declare_destroy(const struct lu_env *env, struct dt_object *dt,
1022 return osp_trans_update_request_create(th);
1026 * Implement OSP layer dt_object_operations::do_destroy() interface.
1028 * Pack the destroy update into the RPC buffer, which will be sent
1029 * to the remote MDT during transaction stop.
1031 * It also marks the object as non-cached.
1033 * \param[in] env pointer to the thread context
1034 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1035 * \param[in] th pointer to the transaction handler
1037 * \retval 0 for success
1038 * \retval negative error number on failure
1040 int osp_md_destroy(const struct lu_env *env, struct dt_object *dt,
1043 struct osp_object *o = dt2osp_obj(dt);
1044 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1045 struct osp_update_request *update;
1049 o->opo_non_exist = 1;
1051 LASSERT(osp->opd_connect_mdt);
1052 update = thandle_to_osp_update_request(th);
1053 LASSERT(update != NULL);
1055 rc = osp_update_rpc_pack(env, destroy, update, OUT_DESTROY,
1056 lu_object_fid(&dt->do_lu));
1060 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1061 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1067 struct dt_object_operations osp_md_obj_ops = {
1068 .do_read_lock = osp_md_read_lock,
1069 .do_write_lock = osp_md_write_lock,
1070 .do_read_unlock = osp_md_read_unlock,
1071 .do_write_unlock = osp_md_write_unlock,
1072 .do_write_locked = osp_md_write_locked,
1073 .do_declare_create = osp_md_declare_create,
1074 .do_create = osp_md_create,
1075 .do_declare_ref_add = osp_md_declare_ref_add,
1076 .do_ref_add = osp_md_ref_add,
1077 .do_declare_ref_del = osp_md_declare_ref_del,
1078 .do_ref_del = osp_md_ref_del,
1079 .do_declare_destroy = osp_md_declare_destroy,
1080 .do_destroy = osp_md_destroy,
1081 .do_ah_init = osp_md_ah_init,
1082 .do_attr_get = osp_attr_get,
1083 .do_declare_attr_set = osp_md_declare_attr_set,
1084 .do_attr_set = osp_md_attr_set,
1085 .do_xattr_get = osp_xattr_get,
1086 .do_xattr_list = osp_md_xattr_list,
1087 .do_declare_xattr_set = osp_declare_xattr_set,
1088 .do_xattr_set = osp_xattr_set,
1089 .do_declare_xattr_del = osp_declare_xattr_del,
1090 .do_xattr_del = osp_xattr_del,
1091 .do_index_try = osp_md_index_try,
1092 .do_object_lock = osp_md_object_lock,
1093 .do_object_unlock = osp_md_object_unlock,
1094 .do_invalidate = osp_invalidate,
1098 * Implementation of dt_body_operations::dbo_declare_write
1100 * Create the osp_update_request to track the update for this OSP
1101 * in the transaction.
1103 * \param[in] env execution environment
1104 * \param[in] dt object to be written
1105 * \param[in] buf buffer to write which includes an embedded size field
1106 * \param[in] pos offet in the object to start writing at
1107 * \param[in] th transaction handle
1109 * \retval 0 if preparation succeeds.
1110 * \retval negative errno if preparation fails.
1112 static ssize_t osp_md_declare_write(const struct lu_env *env,
1113 struct dt_object *dt,
1114 const struct lu_buf *buf,
1115 loff_t pos, struct thandle *th)
1117 struct osp_device *osp = dt2osp_dev(th->th_dev);
1120 rc = osp_trans_update_request_create(th);
1124 if (osp->opd_update == NULL)
1127 if (dt2osp_obj(dt)->opo_stale)
1134 * Implementation of dt_body_operations::dbo_write
1136 * Pack the write object update into the RPC buffer, which will be sent
1137 * to the remote MDT during transaction stop.
1139 * \param[in] env execution environment
1140 * \param[in] dt object to be written
1141 * \param[in] buf buffer to write which includes an embedded size field
1142 * \param[in] pos offet in the object to start writing at
1143 * \param[in] th transaction handle
1144 * \param[in] ignore_quota quota enforcement for this write
1146 * \retval the buffer size in bytes if packing succeeds.
1147 * \retval negative errno if packing fails.
1149 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1150 const struct lu_buf *buf, loff_t *pos,
1151 struct thandle *th, int ignore_quota)
1153 struct osp_object *obj = dt2osp_obj(dt);
1154 struct osp_update_request *update;
1155 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1159 update = thandle_to_osp_update_request(th);
1160 LASSERT(update != NULL);
1162 CDEBUG(D_INFO, "write "DFID" offset = %llu length = %zu\n",
1163 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1165 rc = osp_update_rpc_pack(env, write, update, OUT_WRITE,
1166 lu_object_fid(&dt->do_lu), buf, *pos);
1170 rc = osp_check_and_set_rpc_version(oth, obj);
1174 /* XXX: how about the write error happened later? */
1175 *pos += buf->lb_len;
1177 if (obj->opo_attr.la_valid & LA_SIZE && obj->opo_attr.la_size < *pos)
1178 obj->opo_attr.la_size = *pos;
1180 spin_lock(&obj->opo_lock);
1181 if (list_empty(&obj->opo_invalidate_cb_list)) {
1182 lu_object_get(&obj->opo_obj.do_lu);
1184 list_add_tail(&obj->opo_invalidate_cb_list,
1185 &update->our_invalidate_cb_list);
1187 spin_unlock(&obj->opo_lock);
1189 RETURN(buf->lb_len);
1192 static inline void orr_le_to_cpu(struct out_read_reply *orr_dst,
1193 const struct out_read_reply *orr_src)
1195 orr_dst->orr_size = le32_to_cpu(orr_src->orr_size);
1196 orr_dst->orr_padding = le32_to_cpu(orr_src->orr_padding);
1197 orr_dst->orr_offset = le64_to_cpu(orr_dst->orr_offset);
1202 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1203 struct lu_buf *rbuf, loff_t *pos)
1205 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1206 struct dt_device *dt_dev = &osp->opd_dt_dev;
1207 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1208 char *ptr = rbuf->lb_buf;
1209 struct osp_update_request *update;
1210 struct ptlrpc_request *req = NULL;
1211 struct out_read_reply *orr;
1212 struct ptlrpc_bulk_desc *desc;
1213 struct object_update_reply *reply;
1220 /* Because it needs send the update buffer right away,
1221 * just create an update buffer, instead of attaching the
1222 * update_remote list of the thandle. */
1223 update = osp_update_request_create(dt_dev);
1225 RETURN(PTR_ERR(update));
1227 rc = osp_update_rpc_pack(env, read, update, OUT_READ,
1228 lu_object_fid(&dt->do_lu),
1229 rbuf->lb_len, *pos);
1231 CERROR("%s: cannot insert update: rc = %d\n",
1232 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1233 GOTO(out_update, rc);
1236 CDEBUG(D_INFO, "%s "DFID" read offset %llu size %zu\n",
1237 dt_dev->dd_lu_dev.ld_obd->obd_name,
1238 PFID(lu_object_fid(&dt->do_lu)), *pos, rbuf->lb_len);
1239 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1242 GOTO(out_update, rc);
1244 nbufs = (rbuf->lb_len + OUT_BULK_BUFFER_SIZE - 1) /
1245 OUT_BULK_BUFFER_SIZE;
1246 /* allocate bulk descriptor */
1247 desc = ptlrpc_prep_bulk_imp(req, nbufs, 1,
1248 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KVEC,
1249 MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
1251 GOTO(out, rc = -ENOMEM);
1253 /* split the buffer into small chunk size */
1254 left_size = rbuf->lb_len;
1255 for (i = 0; i < nbufs; i++) {
1258 read_size = left_size > OUT_BULK_BUFFER_SIZE ?
1259 OUT_BULK_BUFFER_SIZE : left_size;
1260 desc->bd_frag_ops->add_iov_frag(desc, ptr, read_size);
1265 osp_set_req_replay(osp, req);
1266 req->rq_bulk_read = 1;
1267 /* send request to master and wait for RPC to complete */
1268 rc = ptlrpc_queue_wait(req);
1272 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1273 req->rq_bulk->bd_nob_transferred);
1277 reply = req_capsule_server_sized_get(&req->rq_pill,
1278 &RMF_OUT_UPDATE_REPLY,
1279 OUT_UPDATE_REPLY_SIZE);
1281 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1282 CERROR("%s: invalid update reply magic %x expected %x:"
1283 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1284 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1285 GOTO(out, rc = -EPROTO);
1288 rc = object_update_result_data_get(reply, lbuf, 0);
1292 if (lbuf->lb_len < sizeof(*orr))
1293 GOTO(out, rc = -EPROTO);
1296 orr_le_to_cpu(orr, orr);
1298 *pos = orr->orr_offset;
1300 ptlrpc_req_finished(req);
1303 osp_update_request_destroy(env, update);
1308 /* These body operation will be used to write symlinks during migration etc */
1309 struct dt_body_operations osp_md_body_ops = {
1310 .dbo_declare_write = osp_md_declare_write,
1311 .dbo_write = osp_md_write,
1312 .dbo_read = osp_md_read,