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 if (!(attr->la_valid & LA_TYPE)) {
168 CERROR("%s: create type not specified: valid %llx\n",
169 dt->do_lu.lo_dev->ld_obd->obd_name, attr->la_valid);
170 GOTO(out, rc = -EINVAL);
173 rc = OSP_UPDATE_RPC_PACK(env, out_create_pack, update,
174 lu_object_fid(&dt->do_lu), attr, hint, dof);
178 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
179 osp_create_interpreter);
184 dt->do_lu.lo_header->loh_attr |= LOHA_EXISTS | (attr->la_mode & S_IFMT);
185 dt2osp_obj(dt)->opo_non_exist = 0;
188 obj->opo_attr = *attr;
194 * Implementation of dt_object_operations::do_declare_ref_del
196 * Create the osp_update_request to track the update for this OSP
197 * in the transaction.
199 * \param[in] env execution environment
200 * \param[in] dt object to decrease the reference count.
201 * \param[in] th the transaction handle of refcount decrease.
203 * \retval 0 if preparation succeeds.
204 * \retval negative errno if preparation fails.
206 static int osp_md_declare_ref_del(const struct lu_env *env,
207 struct dt_object *dt, struct thandle *th)
209 return osp_trans_update_request_create(th);
213 * Implementation of dt_object_operations::do_ref_del
215 * Add an OUT_REF_DEL sub-request into the OUT RPC that will be
216 * flushed when the transaction stop.
218 * \param[in] env execution environment
219 * \param[in] dt object to decrease the reference count
220 * \param[in] th the transaction handle
222 * \retval 0 if packing ref_del succeeds.
223 * \retval negative errno if packing fails.
225 static int osp_md_ref_del(const struct lu_env *env, struct dt_object *dt,
228 struct osp_update_request *update;
231 update = thandle_to_osp_update_request(th);
232 LASSERT(update != NULL);
234 rc = OSP_UPDATE_RPC_PACK(env, out_ref_del_pack, update,
235 lu_object_fid(&dt->do_lu));
240 * Implementation of dt_object_operations::do_declare_ref_del
242 * Create the osp_update_request to track the update for this OSP
243 * in the transaction.
245 * \param[in] env execution environment
246 * \param[in] dt object on which to increase the reference count.
247 * \param[in] th the transaction handle.
249 * \retval 0 if preparation succeeds.
250 * \retval negative errno if preparation fails.
252 static int osp_md_declare_ref_add(const struct lu_env *env,
253 struct dt_object *dt, struct thandle *th)
255 return osp_trans_update_request_create(th);
259 * Implementation of dt_object_operations::do_ref_add
261 * Add an OUT_REF_ADD sub-request into the OUT RPC that will be flushed
262 * when the transaction stop.
264 * \param[in] env execution environment
265 * \param[in] dt object on which to increase the reference count
266 * \param[in] th the transaction handle
268 * \retval 0 if packing ref_add succeeds.
269 * \retval negative errno if packing fails.
271 static int osp_md_ref_add(const struct lu_env *env, struct dt_object *dt,
274 struct osp_update_request *update;
277 update = thandle_to_osp_update_request(th);
278 LASSERT(update != NULL);
280 rc = OSP_UPDATE_RPC_PACK(env, out_ref_add_pack, update,
281 lu_object_fid(&dt->do_lu));
286 * Implementation of dt_object_operations::do_ah_init
288 * Initialize the allocation hint for object creation, which is usually called
289 * before the creation, and these hints (parent and child mode) will be sent to
290 * the remote Object Update Target (OUT) and used in the object create process,
291 * same as OSD object creation.
293 * \param[in] env execution environment
294 * \param[in] ah the hint to be initialized
295 * \param[in] parent the parent of the object
296 * \param[in] child the object to be created
297 * \param[in] child_mode the mode of the created object
299 static void osp_md_ah_init(const struct lu_env *env,
300 struct dt_allocation_hint *ah,
301 struct dt_object *parent,
302 struct dt_object *child,
307 ah->dah_parent = parent;
308 ah->dah_mode = child_mode;
312 * Implementation of dt_object_operations::do_declare_attr_get
314 * Create the osp_update_request to track the update for this OSP
315 * in the transaction.
317 * \param[in] env execution environment
318 * \param[in] dt object on which to set attributes
319 * \param[in] attr attributes to be set
320 * \param[in] th the transaction handle
322 * \retval 0 if preparation succeeds.
323 * \retval negative errno if preparation fails.
325 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
326 const struct lu_attr *attr, struct thandle *th)
328 return osp_trans_update_request_create(th);
332 * Implementation of dt_object_operations::do_attr_set
334 * Set attributes to the specified remote object.
336 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
337 * when the transaction stop.
339 * \param[in] env execution environment
340 * \param[in] dt object to set attributes
341 * \param[in] attr attributes to be set
342 * \param[in] th the transaction handle
344 * \retval 0 if packing attr_set succeeds.
345 * \retval negative errno if packing fails.
347 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
348 const struct lu_attr *attr, struct thandle *th)
350 struct osp_update_request *update;
353 update = thandle_to_osp_update_request(th);
354 LASSERT(update != NULL);
356 rc = OSP_UPDATE_RPC_PACK(env, out_attr_set_pack, update,
357 lu_object_fid(&dt->do_lu), attr);
362 * Implementation of dt_object_operations::do_read_lock
364 * osp_md_{read,write}_lock() will only lock the remote object in the
365 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
366 * lock the object. Note: it will not lock the object in the whole cluster,
367 * which relies on the LDLM lock.
369 * \param[in] env execution environment
370 * \param[in] dt object to be locked
371 * \param[in] role lock role from MDD layer, see dt_object_role().
373 static void osp_md_read_lock(const struct lu_env *env, struct dt_object *dt,
376 struct osp_object *obj = dt2osp_obj(dt);
378 LASSERT(obj->opo_owner != env);
379 down_read_nested(&obj->opo_sem, role);
381 LASSERT(obj->opo_owner == NULL);
385 * Implementation of dt_object_operations::do_write_lock
387 * Lock the remote object in write mode.
389 * \param[in] env execution environment
390 * \param[in] dt object to be locked
391 * \param[in] role lock role from MDD layer, see dt_object_role().
393 static void osp_md_write_lock(const struct lu_env *env, struct dt_object *dt,
396 struct osp_object *obj = dt2osp_obj(dt);
398 down_write_nested(&obj->opo_sem, role);
400 LASSERT(obj->opo_owner == NULL);
401 obj->opo_owner = env;
405 * Implementation of dt_object_operations::do_read_unlock
407 * Unlock the read lock of remote object.
409 * \param[in] env execution environment
410 * \param[in] dt object to be unlocked
412 static void osp_md_read_unlock(const struct lu_env *env, struct dt_object *dt)
414 struct osp_object *obj = dt2osp_obj(dt);
416 up_read(&obj->opo_sem);
420 * Implementation of dt_object_operations::do_write_unlock
422 * Unlock the write lock of remote object.
424 * \param[in] env execution environment
425 * \param[in] dt object to be unlocked
427 static void osp_md_write_unlock(const struct lu_env *env, struct dt_object *dt)
429 struct osp_object *obj = dt2osp_obj(dt);
431 LASSERT(obj->opo_owner == env);
432 obj->opo_owner = NULL;
433 up_write(&obj->opo_sem);
437 * Implementation of dt_object_operations::do_write_locked
439 * Test if the object is locked in write mode.
441 * \param[in] env execution environment
442 * \param[in] dt object to be tested
444 static int osp_md_write_locked(const struct lu_env *env, 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, out_index_lookup_pack, update,
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 (req_capsule_rep_need_swab(&req->rq_pill))
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
579 * \retval 0 if packing index insert succeeds.
580 * \retval negative errno if packing fails.
582 static int osp_md_index_insert(const struct lu_env *env, struct dt_object *dt,
583 const struct dt_rec *rec,
584 const struct dt_key *key, struct thandle *th)
586 struct osp_update_request *update;
589 update = thandle_to_osp_update_request(th);
590 LASSERT(update != NULL);
592 rc = OSP_UPDATE_RPC_PACK(env, out_index_insert_pack, update,
593 lu_object_fid(&dt->do_lu), rec, key);
598 * Implementation of dt_index_operations::dio_declare_delete
600 * Create the osp_update_request to track the update for this OSP
601 * in the transaction.
603 * \param[in] env execution environment
604 * \param[in] dt object for which to delete index
605 * \param[in] key key of the index
606 * \param[in] th the transaction handle
608 * \retval 0 if preparation succeeds.
609 * \retval negative errno if preparation fails.
611 static int osp_md_declare_index_delete(const struct lu_env *env,
612 struct dt_object *dt,
613 const struct dt_key *key,
616 return osp_trans_update_request_create(th);
620 * Implementation of dt_index_operations::dio_delete
622 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
623 * be flushed when the transaction stop.
625 * \param[in] env execution environment
626 * \param[in] dt object for which to delete index
627 * \param[in] key key of the index which will be deleted
628 * \param[in] th the transaction handle
630 * \retval 0 if packing index delete succeeds.
631 * \retval negative errno if packing fails.
633 static int osp_md_index_delete(const struct lu_env *env,
634 struct dt_object *dt,
635 const struct dt_key *key,
638 struct osp_update_request *update;
641 update = thandle_to_osp_update_request(th);
642 LASSERT(update != NULL);
644 rc = OSP_UPDATE_RPC_PACK(env, out_index_delete_pack, update,
645 lu_object_fid(&dt->do_lu), key);
651 * Implementation of dt_index_operations::dio_it.next
653 * Advance the pointer of the iterator to the next entry. It shares a similar
654 * internal implementation with osp_orphan_it_next(), which is being used for
655 * remote orphan index object. This method will be used for remote directory.
657 * \param[in] env execution environment
658 * \param[in] di iterator of this iteration
660 * \retval 0 if the pointer is advanced successfully.
661 * \retval 1 if it reaches to the end of the index object.
662 * \retval negative errno if the pointer cannot be advanced.
664 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
666 struct osp_it *it = (struct osp_it *)di;
667 struct lu_idxpage *idxpage;
668 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
673 idxpage = it->ooi_cur_idxpage;
674 if (idxpage != NULL) {
675 if (idxpage->lip_nr == 0)
681 (struct lu_dirent *)idxpage->lip_entries;
683 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
684 it->ooi_pos_ent < idxpage->lip_nr) {
685 ent = (struct lu_dirent *)(((char *)ent) +
686 le16_to_cpu(ent->lde_reclen));
694 rc = osp_it_next_page(env, di);
702 * Implementation of dt_index_operations::dio_it.key
704 * Get the key at current iterator poisiton. These iteration methods
705 * (dio_it) will only be used for iterating the remote directory, so
706 * the key is the name of the directory entry.
708 * \param[in] env execution environment
709 * \param[in] di iterator of this iteration
711 * \retval name of the current entry
713 static struct dt_key *osp_it_key(const struct lu_env *env,
714 const struct dt_it *di)
716 struct osp_it *it = (struct osp_it *)di;
717 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
719 return (struct dt_key *)ent->lde_name;
723 * Implementation of dt_index_operations::dio_it.key_size
725 * Get the key size at current iterator poisiton. These iteration methods
726 * (dio_it) will only be used for iterating the remote directory, so the key
727 * size is the name size of the directory entry.
729 * \param[in] env execution environment
730 * \param[in] di iterator of this iteration
732 * \retval name size of the current entry
735 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
737 struct osp_it *it = (struct osp_it *)di;
738 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
740 return (int)le16_to_cpu(ent->lde_namelen);
744 * Implementation of dt_index_operations::dio_it.rec
746 * Get the record at current iterator position. These iteration methods
747 * (dio_it) will only be used for iterating the remote directory, so it
748 * uses lu_dirent_calc_size() to calculate the record size.
750 * \param[in] env execution environment
751 * \param[in] di iterator of this iteration
752 * \param[out] rec the record to be returned
753 * \param[in] attr attributes of the index object, so it knows
754 * how to pack the entry.
756 * \retval only return 0 for now
758 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
759 struct dt_rec *rec, __u32 attr)
761 struct osp_it *it = (struct osp_it *)di;
762 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
765 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
766 memcpy(rec, ent, reclen);
771 * Implementation of dt_index_operations::dio_it.load
773 * Locate the iteration cursor to the specified position (cookie).
775 * \param[in] env pointer to the thread context
776 * \param[in] di pointer to the iteration structure
777 * \param[in] hash the specified position
779 * \retval positive number for locating to the exactly position
781 * \retval 0 for arriving at the end of the iteration
782 * \retval negative error number on failure
784 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
787 struct osp_it *it = (struct osp_it *)di;
791 rc = osp_md_index_it_next(env, (struct dt_it *)di);
801 const struct dt_index_operations osp_md_index_ops = {
802 .dio_lookup = osp_md_index_lookup,
803 .dio_declare_insert = osp_md_declare_index_insert,
804 .dio_insert = osp_md_index_insert,
805 .dio_declare_delete = osp_md_declare_index_delete,
806 .dio_delete = osp_md_index_delete,
812 .next = osp_md_index_it_next,
814 .key_size = osp_it_key_size,
815 .rec = osp_md_index_it_rec,
816 .store = osp_it_store,
818 .key_rec = osp_it_key_rec,
823 * Implement OSP layer dt_object_operations::do_xattr_list() interface.
825 * List extended attribute from the specified MDT/OST object, result is not
826 * cached because this is called by directory migration only.
828 * \param[in] env pointer to the thread context
829 * \param[in] dt pointer to the OSP layer dt_object
830 * \param[out] buf pointer to the lu_buf to hold the extended attribute
832 * \retval positive bytes used/required in the buffer
833 * \retval negative error number on failure
835 static int osp_md_xattr_list(const struct lu_env *env, struct dt_object *dt,
836 const struct lu_buf *buf)
838 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
839 struct osp_object *obj = dt2osp_obj(dt);
840 struct dt_device *dev = &osp->opd_dt_dev;
841 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
842 struct osp_update_request *update = NULL;
843 struct ptlrpc_request *req = NULL;
844 struct object_update_reply *reply;
845 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
852 if (unlikely(obj->opo_non_exist))
855 update = osp_update_request_create(dev);
857 RETURN(PTR_ERR(update));
859 rc = OSP_UPDATE_RPC_PACK(env, out_xattr_list_pack, update,
860 lu_object_fid(&dt->do_lu), buf->lb_len);
862 CERROR("%s: Insert update error "DFID": rc = %d\n",
863 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
867 rc = osp_remote_sync(env, osp, update, &req);
870 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
871 obj->opo_non_exist = 1;
876 reply = req_capsule_server_sized_get(&req->rq_pill,
877 &RMF_OUT_UPDATE_REPLY,
878 OUT_UPDATE_REPLY_SIZE);
879 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
880 DEBUG_REQ(D_ERROR, req,
881 "%s: Wrong version %x expected %x "DFID": rc = %d",
882 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
883 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
885 GOTO(out, rc = -EPROTO);
888 rc = object_update_result_data_get(reply, rbuf, 0);
895 if (unlikely(buf->lb_len < rbuf->lb_len))
896 GOTO(out, rc = -ERANGE);
898 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
903 ptlrpc_req_finished(req);
905 if (update && !IS_ERR(update))
906 osp_update_request_destroy(env, update);
912 * Implementation of dt_object_operations::do_index_try
914 * Try to initialize the index API pointer for the given object. This
915 * is the entry point of the index API, i.e. we must call this method
916 * to initialize the index object before calling other index methods.
918 * \param[in] env execution environment
919 * \param[in] dt index object to be initialized
920 * \param[in] feat the index feature of the object
922 * \retval 0 if the initialization succeeds.
923 * \retval negative errno if the initialization fails.
925 static int osp_md_index_try(const struct lu_env *env,
926 struct dt_object *dt,
927 const struct dt_index_features *feat)
929 dt->do_index_ops = &osp_md_index_ops;
934 * Implementation of dt_object_operations::do_object_lock
936 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
937 * which will lock the object in the global namespace. And because the
938 * cross-MDT locks are relatively rare compared with normal local MDT operation,
939 * let's release it right away, instead of putting it into the LRU list.
941 * \param[in] env execution environment
942 * \param[in] dt object to be locked
943 * \param[out] lh lock handle
944 * \param[in] einfo enqueue information
945 * \param[in] policy lock policy
947 * \retval ELDLM_OK if locking the object succeeds.
948 * \retval negative errno if locking fails.
950 static int osp_md_object_lock(const struct lu_env *env,
951 struct dt_object *dt,
952 struct lustre_handle *lh,
953 struct ldlm_enqueue_info *einfo,
954 union ldlm_policy_data *policy)
956 struct ldlm_res_id *res_id;
957 struct osp_device *osp = dt2osp_dev(lu2dt_dev(dt->do_lu.lo_dev));
958 struct ptlrpc_request *req;
960 __u64 flags = LDLM_FL_NO_LRU;
963 res_id = einfo->ei_res_id;
964 LASSERT(res_id != NULL);
966 if (einfo->ei_mode & (LCK_EX | LCK_PW))
967 flags |= LDLM_FL_COS_INCOMPAT;
969 req = ldlm_enqueue_pack(osp->opd_exp, 0);
971 RETURN(PTR_ERR(req));
973 osp_set_req_replay(osp, req);
974 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
975 (const union ldlm_policy_data *)policy,
976 &flags, NULL, 0, LVB_T_NONE, lh, 0);
978 ptlrpc_req_finished(req);
980 RETURN(rc == ELDLM_OK ? 0 : -EIO);
984 * Implementation of dt_object_operations::do_object_unlock
986 * Cancel a lock of a remote object.
988 * \param[in] env execution environment
989 * \param[in] dt object to be unlocked
990 * \param[in] einfo lock enqueue information
991 * \param[in] policy lock policy
993 * \retval Only return 0 for now.
995 static int osp_md_object_unlock(const struct lu_env *env,
996 struct dt_object *dt,
997 struct ldlm_enqueue_info *einfo,
998 union ldlm_policy_data *policy)
1000 struct lustre_handle *lockh = einfo->ei_cbdata;
1002 /* unlock finally */
1003 ldlm_lock_decref(lockh, einfo->ei_mode);
1009 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1011 * Create the dt_update_request to track the update for this OSP
1012 * in the transaction.
1014 * \param[in] env pointer to the thread context
1015 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1016 * \param[in] th pointer to the transaction handler
1018 * \retval 0 for success
1019 * \retval negative error number on failure
1021 int osp_md_declare_destroy(const struct lu_env *env, struct dt_object *dt,
1024 return osp_trans_update_request_create(th);
1027 static int osp_destroy_interpreter(const struct lu_env *env,
1028 struct object_update_reply *reply,
1029 struct ptlrpc_request *req,
1030 struct osp_object *obj,
1031 void *data, int index, int rc)
1037 * Implement OSP layer dt_object_operations::do_destroy() interface.
1039 * Pack the destroy update into the RPC buffer, which will be sent
1040 * to the remote MDT during transaction stop.
1042 * It also marks the object as non-cached.
1044 * \param[in] env pointer to the thread context
1045 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1046 * \param[in] th pointer to the transaction handler
1048 * \retval 0 for success
1049 * \retval negative error number on failure
1051 int osp_md_destroy(const struct lu_env *env, struct dt_object *dt,
1054 struct osp_object *o = dt2osp_obj(dt);
1055 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1056 struct osp_update_request *update;
1060 o->opo_non_exist = 1;
1061 o->opo_destroyed = 1;
1063 LASSERT(osp->opd_connect_mdt);
1064 update = thandle_to_osp_update_request(th);
1065 LASSERT(update != NULL);
1067 rc = OSP_UPDATE_RPC_PACK(env, out_destroy_pack, update,
1068 lu_object_fid(&dt->do_lu));
1072 /* retain the object and it's status until it's destroyed on remote */
1073 rc = osp_insert_update_callback(env, update, o, NULL,
1074 osp_destroy_interpreter);
1078 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1079 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1085 const struct dt_object_operations osp_md_obj_ops = {
1086 .do_read_lock = osp_md_read_lock,
1087 .do_write_lock = osp_md_write_lock,
1088 .do_read_unlock = osp_md_read_unlock,
1089 .do_write_unlock = osp_md_write_unlock,
1090 .do_write_locked = osp_md_write_locked,
1091 .do_declare_create = osp_md_declare_create,
1092 .do_create = osp_md_create,
1093 .do_declare_ref_add = osp_md_declare_ref_add,
1094 .do_ref_add = osp_md_ref_add,
1095 .do_declare_ref_del = osp_md_declare_ref_del,
1096 .do_ref_del = osp_md_ref_del,
1097 .do_declare_destroy = osp_md_declare_destroy,
1098 .do_destroy = osp_md_destroy,
1099 .do_ah_init = osp_md_ah_init,
1100 .do_attr_get = osp_attr_get,
1101 .do_declare_attr_set = osp_md_declare_attr_set,
1102 .do_attr_set = osp_md_attr_set,
1103 .do_xattr_get = osp_xattr_get,
1104 .do_xattr_list = osp_md_xattr_list,
1105 .do_declare_xattr_set = osp_declare_xattr_set,
1106 .do_xattr_set = osp_xattr_set,
1107 .do_declare_xattr_del = osp_declare_xattr_del,
1108 .do_xattr_del = osp_xattr_del,
1109 .do_index_try = osp_md_index_try,
1110 .do_object_lock = osp_md_object_lock,
1111 .do_object_unlock = osp_md_object_unlock,
1112 .do_invalidate = osp_invalidate,
1113 .do_check_stale = osp_check_stale,
1117 * Implementation of dt_body_operations::dbo_declare_write
1119 * Create the osp_update_request to track the update for this OSP
1120 * in the transaction.
1122 * \param[in] env execution environment
1123 * \param[in] dt object to be written
1124 * \param[in] buf buffer to write which includes an embedded size field
1125 * \param[in] pos offet in the object to start writing at
1126 * \param[in] th transaction handle
1128 * \retval 0 if preparation succeeds.
1129 * \retval negative errno if preparation fails.
1131 static ssize_t osp_md_declare_write(const struct lu_env *env,
1132 struct dt_object *dt,
1133 const struct lu_buf *buf,
1134 loff_t pos, struct thandle *th)
1136 struct osp_device *osp = dt2osp_dev(th->th_dev);
1139 if (dt2osp_obj(dt)->opo_destroyed)
1142 rc = osp_trans_update_request_create(th);
1146 if (osp->opd_update == NULL)
1149 if (dt2osp_obj(dt)->opo_stale)
1155 static int osp_write_interpreter(const struct lu_env *env,
1156 struct object_update_reply *reply,
1157 struct ptlrpc_request *req,
1158 struct osp_object *obj,
1159 void *data, int index, int rc)
1162 CDEBUG(D_HA, "error "DFID": rc = %d\n",
1163 PFID(lu_object_fid(&obj->opo_obj.do_lu)), rc);
1164 OBD_RACE(OBD_FAIL_OUT_OBJECT_MISS);
1165 spin_lock(&obj->opo_lock);
1166 obj->opo_attr.la_valid = 0;
1168 spin_unlock(&obj->opo_lock);
1174 * Implementation of dt_body_operations::dbo_write
1176 * Pack the write object update into the RPC buffer, which will be sent
1177 * to the remote MDT during transaction stop.
1179 * \param[in] env execution environment
1180 * \param[in] dt object to be written
1181 * \param[in] buf buffer to write which includes an embedded size field
1182 * \param[in] pos offet in the object to start writing at
1183 * \param[in] th transaction handle
1185 * \retval the buffer size in bytes if packing succeeds.
1186 * \retval negative errno if packing fails.
1188 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1189 const struct lu_buf *buf, loff_t *pos,
1192 struct osp_object *obj = dt2osp_obj(dt);
1193 struct osp_update_request *update;
1194 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1198 if (obj->opo_destroyed)
1201 update = thandle_to_osp_update_request(th);
1202 LASSERT(update != NULL);
1204 CDEBUG(D_INFO, "write "DFID" offset = %llu length = %zu\n",
1205 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1207 rc = OSP_UPDATE_RPC_PACK(env, out_write_pack, update,
1208 lu_object_fid(&dt->do_lu), buf, *pos);
1212 rc = osp_check_and_set_rpc_version(oth, obj);
1216 /* to be able to invalidate object's state in case of an error */
1217 rc = osp_insert_update_callback(env, update, obj, NULL,
1218 osp_write_interpreter);
1222 /* XXX: how about the write error happened later? */
1223 *pos += buf->lb_len;
1225 if (obj->opo_attr.la_valid & LA_SIZE && obj->opo_attr.la_size < *pos)
1226 obj->opo_attr.la_size = *pos;
1228 spin_lock(&obj->opo_lock);
1229 if (list_empty(&obj->opo_invalidate_cb_list)) {
1230 lu_object_get(&obj->opo_obj.do_lu);
1232 list_add_tail(&obj->opo_invalidate_cb_list,
1233 &update->our_invalidate_cb_list);
1235 spin_unlock(&obj->opo_lock);
1237 RETURN(buf->lb_len);
1240 static inline void orr_le_to_cpu(struct out_read_reply *orr_dst,
1241 const struct out_read_reply *orr_src)
1243 orr_dst->orr_size = le32_to_cpu(orr_src->orr_size);
1244 orr_dst->orr_padding = le32_to_cpu(orr_src->orr_padding);
1245 orr_dst->orr_offset = le64_to_cpu(orr_dst->orr_offset);
1250 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1251 struct lu_buf *rbuf, loff_t *pos)
1253 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1254 struct dt_device *dt_dev = &osp->opd_dt_dev;
1255 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1256 char *ptr = rbuf->lb_buf;
1257 struct osp_update_request *update;
1258 struct ptlrpc_request *req = NULL;
1259 struct out_read_reply *orr;
1260 struct ptlrpc_bulk_desc *desc;
1261 struct object_update_reply *reply;
1266 if (dt2osp_obj(dt)->opo_destroyed)
1269 /* Because it needs send the update buffer right away,
1270 * just create an update buffer, instead of attaching the
1271 * update_remote list of the thandle. */
1272 update = osp_update_request_create(dt_dev);
1274 RETURN(PTR_ERR(update));
1276 rc = OSP_UPDATE_RPC_PACK(env, out_read_pack, update,
1277 lu_object_fid(&dt->do_lu),
1278 rbuf->lb_len, *pos);
1280 CERROR("%s: cannot insert update: rc = %d\n",
1281 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1282 GOTO(out_update, rc);
1285 CDEBUG(D_INFO, "%s "DFID" read offset %llu size %zu\n",
1286 dt_dev->dd_lu_dev.ld_obd->obd_name,
1287 PFID(lu_object_fid(&dt->do_lu)), *pos, rbuf->lb_len);
1288 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1291 GOTO(out_update, rc);
1293 /* First *and* last might be partial pages, hence +1 */
1294 pages = DIV_ROUND_UP(rbuf->lb_len, PAGE_SIZE) + 1;
1296 /* allocate bulk descriptor */
1297 desc = ptlrpc_prep_bulk_imp(req, pages, 1,
1298 PTLRPC_BULK_PUT_SINK,
1300 &ptlrpc_bulk_kiov_nopin_ops);
1302 GOTO(out, rc = -ENOMEM);
1304 desc->bd_frag_ops->add_iov_frag(desc, ptr, rbuf->lb_len);
1306 osp_set_req_replay(osp, req);
1307 req->rq_bulk_read = 1;
1308 /* send request to master and wait for RPC to complete */
1309 rc = ptlrpc_queue_wait(req);
1313 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1314 req->rq_bulk->bd_nob_transferred);
1318 reply = req_capsule_server_sized_get(&req->rq_pill,
1319 &RMF_OUT_UPDATE_REPLY,
1320 OUT_UPDATE_REPLY_SIZE);
1322 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1323 CERROR("%s: invalid update reply magic %x expected %x:"
1324 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1325 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1326 GOTO(out, rc = -EPROTO);
1329 rc = object_update_result_data_get(reply, lbuf, 0);
1333 if (lbuf->lb_len < sizeof(*orr))
1334 GOTO(out, rc = -EPROTO);
1337 orr_le_to_cpu(orr, orr);
1339 *pos = orr->orr_offset;
1341 ptlrpc_req_finished(req);
1344 osp_update_request_destroy(env, update);
1349 /* These body operation will be used to write symlinks during migration etc */
1350 const struct dt_body_operations osp_md_body_ops = {
1351 .dbo_declare_write = osp_md_declare_write,
1352 .dbo_write = osp_md_write,
1353 .dbo_read = osp_md_read,