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;
311 * Implementation of dt_object_operations::do_declare_attr_get
313 * Create the osp_update_request to track the update for this OSP
314 * in the transaction.
316 * \param[in] env execution environment
317 * \param[in] dt object on which to set attributes
318 * \param[in] attr attributes to be set
319 * \param[in] th the transaction handle
321 * \retval 0 if preparation succeeds.
322 * \retval negative errno if preparation fails.
324 int osp_md_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
325 const struct lu_attr *attr, struct thandle *th)
327 return osp_trans_update_request_create(th);
331 * Implementation of dt_object_operations::do_attr_set
333 * Set attributes to the specified remote object.
335 * Add the OUT_ATTR_SET sub-request into the OUT RPC that will be flushed
336 * when the transaction stop.
338 * \param[in] env execution environment
339 * \param[in] dt object to set attributes
340 * \param[in] attr attributes to be set
341 * \param[in] th the transaction handle
343 * \retval 0 if packing attr_set succeeds.
344 * \retval negative errno if packing fails.
346 int osp_md_attr_set(const struct lu_env *env, struct dt_object *dt,
347 const struct lu_attr *attr, struct thandle *th)
349 struct osp_update_request *update;
352 update = thandle_to_osp_update_request(th);
353 LASSERT(update != NULL);
355 rc = OSP_UPDATE_RPC_PACK(env, out_attr_set_pack, update,
356 lu_object_fid(&dt->do_lu), attr);
361 * Implementation of dt_object_operations::do_read_lock
363 * osp_md_{read,write}_lock() will only lock the remote object in the
364 * local cache, which uses the semaphore (opo_sem) inside the osp_object to
365 * lock the object. Note: it will not lock the object in the whole cluster,
366 * which relies on the LDLM lock.
368 * \param[in] env execution environment
369 * \param[in] dt object to be locked
370 * \param[in] role lock role from MDD layer, see dt_object_role().
372 static void osp_md_read_lock(const struct lu_env *env, struct dt_object *dt,
375 struct osp_object *obj = dt2osp_obj(dt);
377 LASSERT(obj->opo_owner != env);
378 down_read_nested(&obj->opo_sem, role);
380 LASSERT(obj->opo_owner == NULL);
384 * Implementation of dt_object_operations::do_write_lock
386 * Lock the remote object in write mode.
388 * \param[in] env execution environment
389 * \param[in] dt object to be locked
390 * \param[in] role lock role from MDD layer, see dt_object_role().
392 static void osp_md_write_lock(const struct lu_env *env, struct dt_object *dt,
395 struct osp_object *obj = dt2osp_obj(dt);
397 down_write_nested(&obj->opo_sem, role);
399 LASSERT(obj->opo_owner == NULL);
400 obj->opo_owner = env;
404 * Implementation of dt_object_operations::do_read_unlock
406 * Unlock the read lock of remote object.
408 * \param[in] env execution environment
409 * \param[in] dt object to be unlocked
411 static void osp_md_read_unlock(const struct lu_env *env, struct dt_object *dt)
413 struct osp_object *obj = dt2osp_obj(dt);
415 up_read(&obj->opo_sem);
419 * Implementation of dt_object_operations::do_write_unlock
421 * Unlock the write lock of remote object.
423 * \param[in] env execution environment
424 * \param[in] dt object to be unlocked
426 static void osp_md_write_unlock(const struct lu_env *env, struct dt_object *dt)
428 struct osp_object *obj = dt2osp_obj(dt);
430 LASSERT(obj->opo_owner == env);
431 obj->opo_owner = NULL;
432 up_write(&obj->opo_sem);
436 * Implementation of dt_object_operations::do_write_locked
438 * Test if the object is locked in write mode.
440 * \param[in] env execution environment
441 * \param[in] dt object to be tested
443 static int osp_md_write_locked(const struct lu_env *env, struct dt_object *dt)
445 struct osp_object *obj = dt2osp_obj(dt);
447 return obj->opo_owner == env;
451 * Implementation of dt_index_operations::dio_lookup
453 * Look up record by key under a remote index object. It packs lookup update
454 * into RPC, sends to the remote OUT and waits for the lookup result.
456 * \param[in] env execution environment
457 * \param[in] dt index object to lookup
458 * \param[out] rec record in which to return lookup result
459 * \param[in] key key of index which will be looked up
461 * \retval 1 if the lookup succeeds.
462 * \retval negative errno if the lookup fails.
464 static int osp_md_index_lookup(const struct lu_env *env, struct dt_object *dt,
465 struct dt_rec *rec, const struct dt_key *key)
467 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
468 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
469 struct dt_device *dt_dev = &osp->opd_dt_dev;
470 struct osp_update_request *update;
471 struct object_update_reply *reply;
472 struct ptlrpc_request *req = NULL;
477 /* Because it needs send the update buffer right away,
478 * just create an update buffer, instead of attaching the
479 * update_remote list of the thandle.
481 update = osp_update_request_create(dt_dev);
483 RETURN(PTR_ERR(update));
485 rc = OSP_UPDATE_RPC_PACK(env, out_index_lookup_pack, update,
486 lu_object_fid(&dt->do_lu), rec, key);
488 CERROR("%s: Insert update error: rc = %d\n",
489 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
493 rc = osp_remote_sync(env, osp, update, &req);
497 reply = req_capsule_server_sized_get(&req->rq_pill,
498 &RMF_OUT_UPDATE_REPLY,
499 OUT_UPDATE_REPLY_SIZE);
500 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
501 CERROR("%s: Wrong version %x expected %x: rc = %d\n",
502 dt_dev->dd_lu_dev.ld_obd->obd_name,
503 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
504 GOTO(out, rc = -EPROTO);
507 rc = object_update_result_data_get(reply, lbuf, 0);
511 if (lbuf->lb_len != sizeof(*fid)) {
512 CERROR("%s: lookup "DFID" %s wrong size %d\n",
513 dt_dev->dd_lu_dev.ld_obd->obd_name,
514 PFID(lu_object_fid(&dt->do_lu)), (char *)key,
516 GOTO(out, rc = -EINVAL);
520 if (req_capsule_rep_need_swab(&req->rq_pill))
521 lustre_swab_lu_fid(fid);
522 if (!fid_is_sane(fid)) {
523 CERROR("%s: lookup "DFID" %s invalid fid "DFID"\n",
524 dt_dev->dd_lu_dev.ld_obd->obd_name,
525 PFID(lu_object_fid(&dt->do_lu)), (char *)key, PFID(fid));
526 GOTO(out, rc = -EINVAL);
529 memcpy(rec, fid, sizeof(*fid));
535 ptlrpc_req_finished(req);
537 osp_update_request_destroy(env, update);
543 * Implementation of dt_index_operations::dio_declare_insert
545 * Create the osp_update_request to track the update for this OSP
546 * in the transaction.
548 * \param[in] env execution environment
549 * \param[in] dt object for which to insert index
550 * \param[in] rec record of the index which will be inserted
551 * \param[in] key key of the index which will be inserted
552 * \param[in] th the transaction handle
554 * \retval 0 if preparation succeeds.
555 * \retval negative errno if preparation fails.
557 static int osp_md_declare_index_insert(const struct lu_env *env,
558 struct dt_object *dt,
559 const struct dt_rec *rec,
560 const struct dt_key *key,
563 return osp_trans_update_request_create(th);
567 * Implementation of dt_index_operations::dio_insert
569 * Add an OUT_INDEX_INSERT sub-request into the OUT RPC that will
570 * be flushed when the transaction stop.
572 * \param[in] env execution environment
573 * \param[in] dt object for which to insert index
574 * \param[in] rec record of the index to be inserted
575 * \param[in] key key of the index to be inserted
576 * \param[in] th the transaction handle
578 * \retval 0 if packing index insert succeeds.
579 * \retval negative errno if packing fails.
581 static int osp_md_index_insert(const struct lu_env *env, struct dt_object *dt,
582 const struct dt_rec *rec,
583 const struct dt_key *key, struct thandle *th)
585 struct osp_update_request *update;
588 update = thandle_to_osp_update_request(th);
589 LASSERT(update != NULL);
591 rc = OSP_UPDATE_RPC_PACK(env, out_index_insert_pack, update,
592 lu_object_fid(&dt->do_lu), rec, key);
597 * Implementation of dt_index_operations::dio_declare_delete
599 * Create the osp_update_request to track the update for this OSP
600 * in the transaction.
602 * \param[in] env execution environment
603 * \param[in] dt object for which to delete index
604 * \param[in] key key of the index
605 * \param[in] th the transaction handle
607 * \retval 0 if preparation succeeds.
608 * \retval negative errno if preparation fails.
610 static int osp_md_declare_index_delete(const struct lu_env *env,
611 struct dt_object *dt,
612 const struct dt_key *key,
615 return osp_trans_update_request_create(th);
619 * Implementation of dt_index_operations::dio_delete
621 * Add an OUT_INDEX_DELETE sub-request into the OUT RPC that will
622 * be flushed when the transaction stop.
624 * \param[in] env execution environment
625 * \param[in] dt object for which to delete index
626 * \param[in] key key of the index which will be deleted
627 * \param[in] th the transaction handle
629 * \retval 0 if packing index delete succeeds.
630 * \retval negative errno if packing fails.
632 static int osp_md_index_delete(const struct lu_env *env,
633 struct dt_object *dt,
634 const struct dt_key *key,
637 struct osp_update_request *update;
640 update = thandle_to_osp_update_request(th);
641 LASSERT(update != NULL);
643 rc = OSP_UPDATE_RPC_PACK(env, out_index_delete_pack, update,
644 lu_object_fid(&dt->do_lu), key);
650 * Implementation of dt_index_operations::dio_it.next
652 * Advance the pointer of the iterator to the next entry. It shares a similar
653 * internal implementation with osp_orphan_it_next(), which is being used for
654 * remote orphan index object. This method will be used for remote directory.
656 * \param[in] env execution environment
657 * \param[in] di iterator of this iteration
659 * \retval 0 if the pointer is advanced successfully.
660 * \retval 1 if it reaches to the end of the index object.
661 * \retval negative errno if the pointer cannot be advanced.
663 static int osp_md_index_it_next(const struct lu_env *env, struct dt_it *di)
665 struct osp_it *it = (struct osp_it *)di;
666 struct lu_idxpage *idxpage;
667 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
672 idxpage = it->ooi_cur_idxpage;
673 if (idxpage != NULL) {
674 if (idxpage->lip_nr == 0)
680 (struct lu_dirent *)idxpage->lip_entries;
682 } else if (le16_to_cpu(ent->lde_reclen) != 0 &&
683 it->ooi_pos_ent < idxpage->lip_nr) {
684 ent = (struct lu_dirent *)(((char *)ent) +
685 le16_to_cpu(ent->lde_reclen));
693 rc = osp_it_next_page(env, di);
701 * Implementation of dt_index_operations::dio_it.key
703 * Get the key at current iterator poisiton. These iteration methods
704 * (dio_it) will only be used for iterating the remote directory, so
705 * the key is the name of the directory entry.
707 * \param[in] env execution environment
708 * \param[in] di iterator of this iteration
710 * \retval name of the current entry
712 static struct dt_key *osp_it_key(const struct lu_env *env,
713 const struct dt_it *di)
715 struct osp_it *it = (struct osp_it *)di;
716 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
718 return (struct dt_key *)ent->lde_name;
722 * Implementation of dt_index_operations::dio_it.key_size
724 * Get the key size at current iterator poisiton. These iteration methods
725 * (dio_it) will only be used for iterating the remote directory, so the key
726 * size is the name size of the directory entry.
728 * \param[in] env execution environment
729 * \param[in] di iterator of this iteration
731 * \retval name size of the current entry
734 static int osp_it_key_size(const struct lu_env *env, const struct dt_it *di)
736 struct osp_it *it = (struct osp_it *)di;
737 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
739 return (int)le16_to_cpu(ent->lde_namelen);
743 * Implementation of dt_index_operations::dio_it.rec
745 * Get the record at current iterator position. These iteration methods
746 * (dio_it) will only be used for iterating the remote directory, so it
747 * uses lu_dirent_calc_size() to calculate the record size.
749 * \param[in] env execution environment
750 * \param[in] di iterator of this iteration
751 * \param[out] rec the record to be returned
752 * \param[in] attr attributes of the index object, so it knows
753 * how to pack the entry.
755 * \retval only return 0 for now
757 static int osp_md_index_it_rec(const struct lu_env *env, const struct dt_it *di,
758 struct dt_rec *rec, __u32 attr)
760 struct osp_it *it = (struct osp_it *)di;
761 struct lu_dirent *ent = (struct lu_dirent *)it->ooi_ent;
764 reclen = lu_dirent_calc_size(le16_to_cpu(ent->lde_namelen), attr);
765 memcpy(rec, ent, reclen);
770 * Implementation of dt_index_operations::dio_it.load
772 * Locate the iteration cursor to the specified position (cookie).
774 * \param[in] env pointer to the thread context
775 * \param[in] di pointer to the iteration structure
776 * \param[in] hash the specified position
778 * \retval positive number for locating to the exactly position
780 * \retval 0 for arriving at the end of the iteration
781 * \retval negative error number on failure
783 static int osp_it_load(const struct lu_env *env, const struct dt_it *di,
786 struct osp_it *it = (struct osp_it *)di;
790 rc = osp_md_index_it_next(env, (struct dt_it *)di);
800 const struct dt_index_operations osp_md_index_ops = {
801 .dio_lookup = osp_md_index_lookup,
802 .dio_declare_insert = osp_md_declare_index_insert,
803 .dio_insert = osp_md_index_insert,
804 .dio_declare_delete = osp_md_declare_index_delete,
805 .dio_delete = osp_md_index_delete,
811 .next = osp_md_index_it_next,
813 .key_size = osp_it_key_size,
814 .rec = osp_md_index_it_rec,
815 .store = osp_it_store,
817 .key_rec = osp_it_key_rec,
822 * Implement OSP layer dt_object_operations::do_xattr_list() interface.
824 * List extended attribute from the specified MDT/OST object, result is not
825 * cached because this is called by directory migration only.
827 * \param[in] env pointer to the thread context
828 * \param[in] dt pointer to the OSP layer dt_object
829 * \param[out] buf pointer to the lu_buf to hold the extended attribute
831 * \retval positive bytes used/required in the buffer
832 * \retval negative error number on failure
834 static int osp_md_xattr_list(const struct lu_env *env, struct dt_object *dt,
835 const struct lu_buf *buf)
837 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
838 struct osp_object *obj = dt2osp_obj(dt);
839 struct dt_device *dev = &osp->opd_dt_dev;
840 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
841 struct osp_update_request *update = NULL;
842 struct ptlrpc_request *req = NULL;
843 struct object_update_reply *reply;
844 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
851 if (unlikely(obj->opo_non_exist))
854 update = osp_update_request_create(dev);
856 RETURN(PTR_ERR(update));
858 rc = OSP_UPDATE_RPC_PACK(env, out_xattr_list_pack, update,
859 lu_object_fid(&dt->do_lu), buf->lb_len);
861 CERROR("%s: Insert update error "DFID": rc = %d\n",
862 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
866 rc = osp_remote_sync(env, osp, update, &req);
869 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
870 obj->opo_non_exist = 1;
875 reply = req_capsule_server_sized_get(&req->rq_pill,
876 &RMF_OUT_UPDATE_REPLY,
877 OUT_UPDATE_REPLY_SIZE);
878 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
879 DEBUG_REQ(D_ERROR, req,
880 "%s: Wrong version %x expected %x "DFID": rc = %d",
881 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
882 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
884 GOTO(out, rc = -EPROTO);
887 rc = object_update_result_data_get(reply, rbuf, 0);
894 if (unlikely(buf->lb_len < rbuf->lb_len))
895 GOTO(out, rc = -ERANGE);
897 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
902 ptlrpc_req_finished(req);
904 if (update && !IS_ERR(update))
905 osp_update_request_destroy(env, update);
911 * Implementation of dt_object_operations::do_index_try
913 * Try to initialize the index API pointer for the given object. This
914 * is the entry point of the index API, i.e. we must call this method
915 * to initialize the index object before calling other index methods.
917 * \param[in] env execution environment
918 * \param[in] dt index object to be initialized
919 * \param[in] feat the index feature of the object
921 * \retval 0 if the initialization succeeds.
922 * \retval negative errno if the initialization fails.
924 static int osp_md_index_try(const struct lu_env *env,
925 struct dt_object *dt,
926 const struct dt_index_features *feat)
928 dt->do_index_ops = &osp_md_index_ops;
933 * Implementation of dt_object_operations::do_object_lock
935 * Enqueue a lock (by ldlm_cli_enqueue()) of remote object on the remote MDT,
936 * which will lock the object in the global namespace. And because the
937 * cross-MDT locks are relatively rare compared with normal local MDT operation,
938 * let's release it right away, instead of putting it into the LRU list.
940 * \param[in] env execution environment
941 * \param[in] dt object to be locked
942 * \param[out] lh lock handle
943 * \param[in] einfo enqueue information
944 * \param[in] policy lock policy
946 * \retval ELDLM_OK if locking the object succeeds.
947 * \retval negative errno if locking fails.
949 static int osp_md_object_lock(const struct lu_env *env,
950 struct dt_object *dt,
951 struct lustre_handle *lh,
952 struct ldlm_enqueue_info *einfo,
953 union ldlm_policy_data *policy)
955 struct ldlm_res_id *res_id;
956 struct osp_device *osp = dt2osp_dev(lu2dt_dev(dt->do_lu.lo_dev));
957 struct ptlrpc_request *req;
959 __u64 flags = LDLM_FL_NO_LRU;
962 res_id = einfo->ei_res_id;
963 LASSERT(res_id != NULL);
965 if (einfo->ei_mode & (LCK_EX | LCK_PW))
966 flags |= LDLM_FL_COS_INCOMPAT;
968 req = ldlm_enqueue_pack(osp->opd_exp, 0);
970 RETURN(PTR_ERR(req));
972 osp_set_req_replay(osp, req);
973 rc = ldlm_cli_enqueue(osp->opd_exp, &req, einfo, res_id,
974 (const union ldlm_policy_data *)policy,
975 &flags, NULL, 0, LVB_T_NONE, lh, 0);
977 ptlrpc_req_finished(req);
979 RETURN(rc == ELDLM_OK ? 0 : -EIO);
983 * Implementation of dt_object_operations::do_object_unlock
985 * Cancel a lock of a remote object.
987 * \param[in] env execution environment
988 * \param[in] dt object to be unlocked
989 * \param[in] einfo lock enqueue information
990 * \param[in] policy lock policy
992 * \retval Only return 0 for now.
994 static int osp_md_object_unlock(const struct lu_env *env,
995 struct dt_object *dt,
996 struct ldlm_enqueue_info *einfo,
997 union ldlm_policy_data *policy)
999 struct lustre_handle *lockh = einfo->ei_cbdata;
1001 /* unlock finally */
1002 ldlm_lock_decref(lockh, einfo->ei_mode);
1008 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1010 * Create the dt_update_request to track the update for this OSP
1011 * in the transaction.
1013 * \param[in] env pointer to the thread context
1014 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1015 * \param[in] th pointer to the transaction handler
1017 * \retval 0 for success
1018 * \retval negative error number on failure
1020 int osp_md_declare_destroy(const struct lu_env *env, struct dt_object *dt,
1023 return osp_trans_update_request_create(th);
1026 static int osp_destroy_interpreter(const struct lu_env *env,
1027 struct object_update_reply *reply,
1028 struct ptlrpc_request *req,
1029 struct osp_object *obj,
1030 void *data, int index, int rc)
1036 * Implement OSP layer dt_object_operations::do_destroy() interface.
1038 * Pack the destroy update into the RPC buffer, which will be sent
1039 * to the remote MDT during transaction stop.
1041 * It also marks the object as non-cached.
1043 * \param[in] env pointer to the thread context
1044 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1045 * \param[in] th pointer to the transaction handler
1047 * \retval 0 for success
1048 * \retval negative error number on failure
1050 int osp_md_destroy(const struct lu_env *env, struct dt_object *dt,
1053 struct osp_object *o = dt2osp_obj(dt);
1054 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1055 struct osp_update_request *update;
1059 o->opo_non_exist = 1;
1060 o->opo_destroyed = 1;
1062 LASSERT(osp->opd_connect_mdt);
1063 update = thandle_to_osp_update_request(th);
1064 LASSERT(update != NULL);
1066 rc = OSP_UPDATE_RPC_PACK(env, out_destroy_pack, update,
1067 lu_object_fid(&dt->do_lu));
1071 /* retain the object and it's status until it's destroyed on remote */
1072 rc = osp_insert_update_callback(env, update, o, NULL,
1073 osp_destroy_interpreter);
1077 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1078 rc = osp_insert_update_callback(env, update, dt2osp_obj(dt), NULL,
1084 const struct dt_object_operations osp_md_obj_ops = {
1085 .do_read_lock = osp_md_read_lock,
1086 .do_write_lock = osp_md_write_lock,
1087 .do_read_unlock = osp_md_read_unlock,
1088 .do_write_unlock = osp_md_write_unlock,
1089 .do_write_locked = osp_md_write_locked,
1090 .do_declare_create = osp_md_declare_create,
1091 .do_create = osp_md_create,
1092 .do_declare_ref_add = osp_md_declare_ref_add,
1093 .do_ref_add = osp_md_ref_add,
1094 .do_declare_ref_del = osp_md_declare_ref_del,
1095 .do_ref_del = osp_md_ref_del,
1096 .do_declare_destroy = osp_md_declare_destroy,
1097 .do_destroy = osp_md_destroy,
1098 .do_ah_init = osp_md_ah_init,
1099 .do_attr_get = osp_attr_get,
1100 .do_declare_attr_set = osp_md_declare_attr_set,
1101 .do_attr_set = osp_md_attr_set,
1102 .do_xattr_get = osp_xattr_get,
1103 .do_xattr_list = osp_md_xattr_list,
1104 .do_declare_xattr_set = osp_declare_xattr_set,
1105 .do_xattr_set = osp_xattr_set,
1106 .do_declare_xattr_del = osp_declare_xattr_del,
1107 .do_xattr_del = osp_xattr_del,
1108 .do_index_try = osp_md_index_try,
1109 .do_object_lock = osp_md_object_lock,
1110 .do_object_unlock = osp_md_object_unlock,
1111 .do_invalidate = osp_invalidate,
1112 .do_check_stale = osp_check_stale,
1116 * Implementation of dt_body_operations::dbo_declare_write
1118 * Create the osp_update_request to track the update for this OSP
1119 * in the transaction.
1121 * \param[in] env execution environment
1122 * \param[in] dt object to be written
1123 * \param[in] buf buffer to write which includes an embedded size field
1124 * \param[in] pos offet in the object to start writing at
1125 * \param[in] th transaction handle
1127 * \retval 0 if preparation succeeds.
1128 * \retval negative errno if preparation fails.
1130 static ssize_t osp_md_declare_write(const struct lu_env *env,
1131 struct dt_object *dt,
1132 const struct lu_buf *buf,
1133 loff_t pos, struct thandle *th)
1135 struct osp_device *osp = dt2osp_dev(th->th_dev);
1138 if (dt2osp_obj(dt)->opo_destroyed)
1141 rc = osp_trans_update_request_create(th);
1145 if (osp->opd_update == NULL)
1148 if (dt2osp_obj(dt)->opo_stale)
1154 static int osp_write_interpreter(const struct lu_env *env,
1155 struct object_update_reply *reply,
1156 struct ptlrpc_request *req,
1157 struct osp_object *obj,
1158 void *data, int index, int rc)
1161 CDEBUG(D_HA, "error "DFID": rc = %d\n",
1162 PFID(lu_object_fid(&obj->opo_obj.do_lu)), rc);
1163 OBD_RACE(OBD_FAIL_OUT_OBJECT_MISS);
1164 spin_lock(&obj->opo_lock);
1165 obj->opo_attr.la_valid = 0;
1167 spin_unlock(&obj->opo_lock);
1173 * Implementation of dt_body_operations::dbo_write
1175 * Pack the write object update into the RPC buffer, which will be sent
1176 * to the remote MDT during transaction stop.
1178 * \param[in] env execution environment
1179 * \param[in] dt object to be written
1180 * \param[in] buf buffer to write which includes an embedded size field
1181 * \param[in] pos offet in the object to start writing at
1182 * \param[in] th transaction handle
1184 * \retval the buffer size in bytes if packing succeeds.
1185 * \retval negative errno if packing fails.
1187 static ssize_t osp_md_write(const struct lu_env *env, struct dt_object *dt,
1188 const struct lu_buf *buf, loff_t *pos,
1191 struct osp_object *obj = dt2osp_obj(dt);
1192 struct osp_update_request *update;
1193 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1197 if (obj->opo_destroyed)
1200 update = thandle_to_osp_update_request(th);
1201 LASSERT(update != NULL);
1203 CDEBUG(D_INFO, "write "DFID" offset = %llu length = %zu\n",
1204 PFID(lu_object_fid(&dt->do_lu)), *pos, buf->lb_len);
1206 rc = OSP_UPDATE_RPC_PACK(env, out_write_pack, update,
1207 lu_object_fid(&dt->do_lu), buf, *pos);
1211 rc = osp_check_and_set_rpc_version(oth, obj);
1215 /* to be able to invalidate object's state in case of an error */
1216 rc = osp_insert_update_callback(env, update, obj, NULL,
1217 osp_write_interpreter);
1221 /* XXX: how about the write error happened later? */
1222 *pos += buf->lb_len;
1224 if (obj->opo_attr.la_valid & LA_SIZE && obj->opo_attr.la_size < *pos)
1225 obj->opo_attr.la_size = *pos;
1227 spin_lock(&obj->opo_lock);
1228 if (list_empty(&obj->opo_invalidate_cb_list)) {
1229 lu_object_get(&obj->opo_obj.do_lu);
1231 list_add_tail(&obj->opo_invalidate_cb_list,
1232 &update->our_invalidate_cb_list);
1234 spin_unlock(&obj->opo_lock);
1236 RETURN(buf->lb_len);
1239 static inline void orr_le_to_cpu(struct out_read_reply *orr_dst,
1240 const struct out_read_reply *orr_src)
1242 orr_dst->orr_size = le32_to_cpu(orr_src->orr_size);
1243 orr_dst->orr_padding = le32_to_cpu(orr_src->orr_padding);
1244 orr_dst->orr_offset = le64_to_cpu(orr_dst->orr_offset);
1249 static ssize_t osp_md_read(const struct lu_env *env, struct dt_object *dt,
1250 struct lu_buf *rbuf, loff_t *pos)
1252 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1253 struct dt_device *dt_dev = &osp->opd_dt_dev;
1254 struct lu_buf *lbuf = &osp_env_info(env)->osi_lb2;
1255 char *ptr = rbuf->lb_buf;
1256 struct osp_update_request *update;
1257 struct ptlrpc_request *req = NULL;
1258 struct out_read_reply *orr;
1259 struct ptlrpc_bulk_desc *desc;
1260 struct object_update_reply *reply;
1265 if (dt2osp_obj(dt)->opo_destroyed)
1268 /* Because it needs send the update buffer right away,
1269 * just create an update buffer, instead of attaching the
1270 * update_remote list of the thandle. */
1271 update = osp_update_request_create(dt_dev);
1273 RETURN(PTR_ERR(update));
1275 rc = OSP_UPDATE_RPC_PACK(env, out_read_pack, update,
1276 lu_object_fid(&dt->do_lu),
1277 rbuf->lb_len, *pos);
1279 CERROR("%s: cannot insert update: rc = %d\n",
1280 dt_dev->dd_lu_dev.ld_obd->obd_name, rc);
1281 GOTO(out_update, rc);
1284 CDEBUG(D_INFO, "%s "DFID" read offset %llu size %zu\n",
1285 dt_dev->dd_lu_dev.ld_obd->obd_name,
1286 PFID(lu_object_fid(&dt->do_lu)), *pos, rbuf->lb_len);
1287 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import, update,
1290 GOTO(out_update, rc);
1292 /* First *and* last might be partial pages, hence +1 */
1293 pages = DIV_ROUND_UP(rbuf->lb_len, PAGE_SIZE) + 1;
1295 /* allocate bulk descriptor */
1296 desc = ptlrpc_prep_bulk_imp(req, pages, 1,
1297 PTLRPC_BULK_PUT_SINK,
1299 &ptlrpc_bulk_kiov_nopin_ops);
1301 GOTO(out, rc = -ENOMEM);
1303 desc->bd_frag_ops->add_iov_frag(desc, ptr, rbuf->lb_len);
1305 osp_set_req_replay(osp, req);
1306 req->rq_bulk_read = 1;
1307 /* send request to master and wait for RPC to complete */
1308 rc = ptlrpc_queue_wait(req);
1312 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1313 req->rq_bulk->bd_nob_transferred);
1317 reply = req_capsule_server_sized_get(&req->rq_pill,
1318 &RMF_OUT_UPDATE_REPLY,
1319 OUT_UPDATE_REPLY_SIZE);
1321 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1322 CERROR("%s: invalid update reply magic %x expected %x:"
1323 " rc = %d\n", dt_dev->dd_lu_dev.ld_obd->obd_name,
1324 reply->ourp_magic, UPDATE_REPLY_MAGIC, -EPROTO);
1325 GOTO(out, rc = -EPROTO);
1328 rc = object_update_result_data_get(reply, lbuf, 0);
1332 if (lbuf->lb_len < sizeof(*orr))
1333 GOTO(out, rc = -EPROTO);
1336 orr_le_to_cpu(orr, orr);
1338 *pos = orr->orr_offset;
1340 ptlrpc_req_finished(req);
1343 osp_update_request_destroy(env, update);
1348 /* These body operation will be used to write symlinks during migration etc */
1349 const struct dt_body_operations osp_md_body_ops = {
1350 .dbo_declare_write = osp_md_declare_write,
1351 .dbo_write = osp_md_write,
1352 .dbo_read = osp_md_read,