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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * lustre/osp/osp_object.c
35 * Lustre OST Proxy Device (OSP) is the agent on the local MDT for the OST
38 * OSP object attributes cache
39 * ---------------------------
40 * OSP object is the stub of the remote OST-object or MDT-object. Both the
41 * attribute and the extended attributes are stored on the peer side remotely.
42 * It is inefficient to send RPC to peer to fetch those attributes when every
43 * get_attr()/get_xattr() called. For a large system, the LFSCK synchronous
44 * mode scanning is prohibitively inefficient.
46 * So the OSP maintains the OSP object attributes cache to cache some
47 * attributes on the local MDT. The cache is organized against the OSP
50 * struct osp_xattr_entry {
51 * struct list_head oxe_list;
57 * unsigned int oxe_exist:1,
62 * struct osp_object_attr {
63 * struct lu_attr ooa_attr;
64 * struct list_head ooa_xattr_list;
69 * struct osp_object_attr *opo_ooa;
70 * spinlock_t opo_lock;
74 * The basic attributes, such as owner/mode/flags, are stored in the
75 * osp_object_attr::ooa_attr. The extended attributes will be stored
76 * as osp_xattr_entry. Every extended attribute has an independent
77 * osp_xattr_entry, and all the osp_xattr_entry are linked into the
78 * osp_object_attr::ooa_xattr_list. The OSP object attributes cache
79 * is protected by the osp_object::opo_lock.
81 * Not all OSP objects have an attributes cache because maintaining
82 * the cache requires some resources. Currently, the OSP object
83 * attributes cache will be initialized when the attributes or the
84 * extended attributes are pre-fetched via osp_declare_attr_get()
85 * or osp_declare_xattr_get(). That is usually for LFSCK purpose,
86 * but it also can be shared by others.
89 * XXX: NOT prepare out RPC for remote transaction. ((please refer to the
90 * comment of osp_trans_create() for remote transaction)
92 * According to our current transaction/dt_object_lock framework (to make
93 * the cross-MDTs modification for DNE1 to be workable), the transaction
94 * sponsor will start the transaction firstly, then try to acquire related
95 * dt_object_lock if needed. Under such rules, if we want to prepare the
96 * OUT RPC in the transaction declare phase, then related attr/xattr
97 * should be known without dt_object_lock. But such condition maybe not
98 * true for some remote transaction case. For example:
100 * For linkEA repairing (by LFSCK) case, before the LFSCK thread obtained
101 * the dt_object_lock on the target MDT-object, it cannot know whether
102 * the MDT-object has linkEA or not, neither invalid or not.
104 * Since the LFSCK thread cannot hold dt_object_lock before the remote
105 * transaction start (otherwise there will be some potential deadlock),
106 * it cannot prepare related OUT RPC for repairing during the declare
107 * phase as other normal transactions do.
109 * To resolve the trouble, we will make OSP to prepare related OUT RPC
110 * after remote transaction started, and trigger the remote updating
111 * (send RPC) when trans_stop. Then the up layer users, such as LFSCK,
112 * can follow the general rule to handle trans_start/dt_object_lock
113 * for repairing linkEA inconsistency without distinguishing remote
116 * In fact, above solution for remote transaction should be the normal
117 * model without considering DNE1. The trouble brought by DNE1 will be
118 * resolved in DNE2. At that time, this patch can be removed.
121 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
122 * Author: Mikhail Pershin <mike.tappro@intel.com>
125 #define DEBUG_SUBSYSTEM S_MDS
127 #include <lustre_obdo.h>
128 #include <lustre_swab.h>
130 #include "osp_internal.h"
132 static inline __u32 osp_dev2node(struct osp_device *osp)
134 return osp->opd_storage->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
137 static inline bool is_ost_obj(struct lu_object *lo)
139 return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
143 * Assign FID to the OST object.
145 * This function will assign the FID to the OST object of a striped file.
147 * \param[in] env pointer to the thread context
148 * \param[in] d pointer to the OSP device
149 * \param[in] o pointer to the OSP object that the FID will be
152 static void osp_object_assign_fid(const struct lu_env *env,
153 struct osp_device *d, struct osp_object *o)
155 struct osp_thread_info *osi = osp_env_info(env);
157 LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
158 LASSERT(o->opo_reserved);
161 osp_precreate_get_fid(env, d, &osi->osi_fid);
163 lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
167 * Initialize the OSP object attributes cache.
169 * \param[in] obj pointer to the OSP object
171 * \retval 0 for success
172 * \retval negative error number on failure
174 int osp_oac_init(struct osp_object *obj)
176 struct osp_object_attr *ooa;
182 INIT_LIST_HEAD(&ooa->ooa_xattr_list);
183 spin_lock(&obj->opo_lock);
184 if (likely(obj->opo_ooa == NULL)) {
186 spin_unlock(&obj->opo_lock);
188 spin_unlock(&obj->opo_lock);
196 * Find the named extended attribute in the OSP object attributes cache.
198 * The caller should take the osp_object::opo_lock before calling
201 * \param[in] ooa pointer to the OSP object attributes cache
202 * \param[in] name the name of the extended attribute
203 * \param[in] namelen the name length of the extended attribute
205 * \retval pointer to the found extended attribute entry
206 * \retval NULL if the specified extended attribute is not
209 static struct osp_xattr_entry *
210 osp_oac_xattr_find_locked(struct osp_object_attr *ooa,
211 const char *name, size_t namelen)
213 struct osp_xattr_entry *oxe;
215 list_for_each_entry(oxe, &ooa->ooa_xattr_list, oxe_list) {
216 if (namelen == oxe->oxe_namelen &&
217 strncmp(name, oxe->oxe_buf, namelen) == 0)
225 * Find the named extended attribute in the OSP object attributes cache.
227 * Call osp_oac_xattr_find_locked() with the osp_object::opo_lock held.
229 * \param[in] obj pointer to the OSP object
230 * \param[in] name the name of the extended attribute
231 * \param[in] unlink true if the extended attribute entry is to be removed
234 * \retval pointer to the found extended attribute entry
235 * \retval NULL if the specified extended attribute is not
238 static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
239 const char *name, bool unlink)
241 struct osp_xattr_entry *oxe = NULL;
243 spin_lock(&obj->opo_lock);
244 if (obj->opo_ooa != NULL) {
245 oxe = osp_oac_xattr_find_locked(obj->opo_ooa, name,
249 list_del_init(&oxe->oxe_list);
251 atomic_inc(&oxe->oxe_ref);
254 spin_unlock(&obj->opo_lock);
260 * Find the named extended attribute in the OSP object attributes cache.
262 * If it is not in the cache, then add an empty entry (that will be
263 * filled later) to cache with the given name.
265 * \param[in] obj pointer to the OSP object
266 * \param[in] name the name of the extended attribute
267 * \param[in] len the length of the extended attribute value
269 * \retval pointer to the found or new-created extended
271 * \retval NULL if the specified extended attribute is not in the
272 * cache or fail to add new empty entry to the cache.
274 static struct osp_xattr_entry *
275 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, size_t len)
277 struct osp_object_attr *ooa = obj->opo_ooa;
278 struct osp_xattr_entry *oxe;
279 struct osp_xattr_entry *tmp = NULL;
280 size_t namelen = strlen(name);
281 size_t size = sizeof(*oxe) + namelen + 1 + len;
283 LASSERT(ooa != NULL);
285 oxe = osp_oac_xattr_find(obj, name, false);
289 OBD_ALLOC(oxe, size);
290 if (unlikely(oxe == NULL))
293 INIT_LIST_HEAD(&oxe->oxe_list);
294 oxe->oxe_buflen = size;
295 oxe->oxe_namelen = namelen;
296 memcpy(oxe->oxe_buf, name, namelen);
297 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
298 /* One ref is for the caller, the other is for the entry on the list. */
299 atomic_set(&oxe->oxe_ref, 2);
301 spin_lock(&obj->opo_lock);
302 tmp = osp_oac_xattr_find_locked(ooa, name, namelen);
304 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
306 atomic_inc(&tmp->oxe_ref);
307 spin_unlock(&obj->opo_lock);
318 * Add the given extended attribute to the OSP object attributes cache.
320 * If there is an old extended attributed entry with the same name,
321 * remove it from the cache and return it via the parameter \a poxe.
323 * \param[in] obj pointer to the OSP object
324 * \param[in,out] poxe double pointer to the OSP object extended attribute
325 * entry: the new extended attribute entry is transferred
326 * via such pointer target, and if old the extended
327 * attribute entry exists, then it will be returned back
328 * via such pointer target.
329 * \param[in] len the length of the (new) extended attribute value
331 * \retval pointer to the new extended attribute entry
332 * \retval NULL for failure cases.
334 static struct osp_xattr_entry *
335 osp_oac_xattr_replace(struct osp_object *obj,
336 struct osp_xattr_entry **poxe, size_t len)
338 struct osp_object_attr *ooa = obj->opo_ooa;
339 struct osp_xattr_entry *oxe;
340 size_t namelen = (*poxe)->oxe_namelen;
341 size_t size = sizeof(*oxe) + namelen + 1 + len;
343 LASSERT(ooa != NULL);
345 OBD_ALLOC(oxe, size);
346 if (unlikely(oxe == NULL))
349 INIT_LIST_HEAD(&oxe->oxe_list);
350 oxe->oxe_buflen = size;
351 oxe->oxe_namelen = namelen;
352 memcpy(oxe->oxe_buf, (*poxe)->oxe_buf, namelen);
353 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
354 /* One ref is for the caller, the other is for the entry on the list. */
355 atomic_set(&oxe->oxe_ref, 2);
357 spin_lock(&obj->opo_lock);
358 *poxe = osp_oac_xattr_find_locked(ooa, oxe->oxe_buf, namelen);
359 LASSERT(*poxe != NULL);
361 list_del_init(&(*poxe)->oxe_list);
362 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
363 spin_unlock(&obj->opo_lock);
369 * Release reference from the OSP object extended attribute entry.
371 * If it is the last reference, then free the entry.
373 * \param[in] oxe pointer to the OSP object extended attribute entry.
375 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
377 if (atomic_dec_and_test(&oxe->oxe_ref)) {
378 LASSERT(list_empty(&oxe->oxe_list));
380 OBD_FREE(oxe, oxe->oxe_buflen);
385 * Parse the OSP object attribute from the RPC reply.
387 * If the attribute is valid, then it will be added to the OSP object
390 * \param[in] env pointer to the thread context
391 * \param[in] reply pointer to the RPC reply
392 * \param[in] req pointer to the RPC request
393 * \param[out] attr pointer to buffer to hold the output attribute
394 * \param[in] obj pointer to the OSP object
395 * \param[in] index the index of the attribute buffer in the reply
397 * \retval 0 for success
398 * \retval negative error number on failure
400 static int osp_get_attr_from_reply(const struct lu_env *env,
401 struct object_update_reply *reply,
402 struct ptlrpc_request *req,
403 struct lu_attr *attr,
404 struct osp_object *obj, int index)
406 struct osp_thread_info *osi = osp_env_info(env);
407 struct lu_buf *rbuf = &osi->osi_lb2;
408 struct obdo *lobdo = &osi->osi_obdo;
412 rc = object_update_result_data_get(reply, rbuf, index);
416 wobdo = rbuf->lb_buf;
417 if (rbuf->lb_len != sizeof(*wobdo))
420 LASSERT(req != NULL);
421 if (ptlrpc_req_need_swab(req))
422 lustre_swab_obdo(wobdo);
424 lustre_get_wire_obdo(NULL, lobdo, wobdo);
425 spin_lock(&obj->opo_lock);
426 if (obj->opo_ooa != NULL) {
427 la_from_obdo(&obj->opo_ooa->ooa_attr, lobdo, lobdo->o_valid);
429 *attr = obj->opo_ooa->ooa_attr;
431 LASSERT(attr != NULL);
433 la_from_obdo(attr, lobdo, lobdo->o_valid);
435 spin_unlock(&obj->opo_lock);
441 * Interpreter function for getting OSP object attribute asynchronously.
443 * Called to interpret the result of an async mode RPC for getting the
444 * OSP object attribute.
446 * \param[in] env pointer to the thread context
447 * \param[in] reply pointer to the RPC reply
448 * \param[in] req pointer to the RPC request
449 * \param[in] obj pointer to the OSP object
450 * \param[out] data pointer to buffer to hold the output attribute
451 * \param[in] index the index of the attribute buffer in the reply
452 * \param[in] rc the result for handling the RPC
454 * \retval 0 for success
455 * \retval negative error number on failure
457 static int osp_attr_get_interpterer(const struct lu_env *env,
458 struct object_update_reply *reply,
459 struct ptlrpc_request *req,
460 struct osp_object *obj,
461 void *data, int index, int rc)
463 struct lu_attr *attr = data;
465 LASSERT(obj->opo_ooa != NULL);
468 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
469 obj->opo_non_exist = 0;
471 return osp_get_attr_from_reply(env, reply, req, NULL, obj,
475 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
476 obj->opo_non_exist = 1;
479 spin_lock(&obj->opo_lock);
481 spin_unlock(&obj->opo_lock);
488 * Implement OSP layer dt_object_operations::do_declare_attr_get() interface.
490 * Declare that the caller will get attribute from the specified OST object.
492 * This function adds an Object Unified Target (OUT) sub-request to the per-OSP
493 * based shared asynchronous request queue. The osp_attr_get_interpterer()
494 * is registered as the interpreter function to handle the result of this
497 * \param[in] env pointer to the thread context
498 * \param[in] dt pointer to the OSP layer dt_object
500 * \retval 0 for success
501 * \retval negative error number on failure
503 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt)
505 struct osp_object *obj = dt2osp_obj(dt);
506 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
509 if (obj->opo_ooa == NULL) {
510 rc = osp_oac_init(obj);
515 mutex_lock(&osp->opd_async_requests_mutex);
516 rc = osp_insert_async_request(env, OUT_ATTR_GET, obj, 0, NULL, NULL,
517 &obj->opo_ooa->ooa_attr,
519 osp_attr_get_interpterer);
520 mutex_unlock(&osp->opd_async_requests_mutex);
526 * Implement OSP layer dt_object_operations::do_attr_get() interface.
528 * Get attribute from the specified MDT/OST object.
530 * If the attribute is in the OSP object attributes cache, then return
531 * the cached attribute directly. Otherwise it will trigger an OUT RPC
532 * to the peer to get the attribute synchronously, if successful, add it
533 * to the OSP attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
535 * \param[in] env pointer to the thread context
536 * \param[in] dt pointer to the OSP layer dt_object
537 * \param[out] attr pointer to the buffer to hold the output attribute
539 * \retval 0 for success
540 * \retval negative error number on failure
542 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
543 struct lu_attr *attr)
545 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
546 struct osp_object *obj = dt2osp_obj(dt);
547 struct dt_device *dev = &osp->opd_dt_dev;
548 struct osp_update_request *update;
549 struct object_update_reply *reply;
550 struct ptlrpc_request *req = NULL;
554 if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
557 if (obj->opo_ooa != NULL) {
558 spin_lock(&obj->opo_lock);
559 if (obj->opo_ooa->ooa_attr.la_valid != 0 && !obj->opo_stale) {
560 *attr = obj->opo_ooa->ooa_attr;
561 spin_unlock(&obj->opo_lock);
565 spin_unlock(&obj->opo_lock);
568 update = osp_update_request_create(dev);
570 RETURN(PTR_ERR(update));
572 rc = osp_update_rpc_pack(env, attr_get, update, OUT_ATTR_GET,
573 lu_object_fid(&dt->do_lu));
575 CERROR("%s: Insert update error "DFID": rc = %d\n",
576 dev->dd_lu_dev.ld_obd->obd_name,
577 PFID(lu_object_fid(&dt->do_lu)), rc);
582 rc = osp_remote_sync(env, osp, update, &req);
585 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
586 obj->opo_non_exist = 1;
588 CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
589 dev->dd_lu_dev.ld_obd->obd_name,
590 PFID(lu_object_fid(&dt->do_lu)), rc);
596 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
597 obj->opo_non_exist = 0;
598 reply = req_capsule_server_sized_get(&req->rq_pill,
599 &RMF_OUT_UPDATE_REPLY,
600 OUT_UPDATE_REPLY_SIZE);
601 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
602 GOTO(out, rc = -EPROTO);
604 rc = osp_get_attr_from_reply(env, reply, req, attr, obj, 0);
608 spin_lock(&obj->opo_lock);
611 spin_unlock(&obj->opo_lock);
617 ptlrpc_req_finished(req);
619 osp_update_request_destroy(update);
625 * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
627 * If the transaction is not remote one, then declare the credits that will
628 * be used for the subsequent llog record for the object's attributes.
630 * \param[in] env pointer to the thread context
631 * \param[in] dt pointer to the OSP layer dt_object
632 * \param[in] attr pointer to the attribute to be set
633 * \param[in] th pointer to the transaction handler
635 * \retval 0 for success
636 * \retval negative error number on failure
638 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
639 const struct lu_attr *attr, struct thandle *th)
641 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
642 struct osp_object *o = dt2osp_obj(dt);
645 if (is_only_remote_trans(th))
646 return osp_md_declare_attr_set(env, dt, attr, th);
648 * Usually we don't allow server stack to manipulate size
649 * but there is a special case when striping is created
650 * late, after stripeless file got truncated to non-zero.
652 * In this case we do the following:
654 * 1) grab id in declare - this can lead to leaked OST objects
655 * but we don't currently have proper mechanism and the only
656 * options we have are to do truncate RPC holding transaction
657 * open (very bad) or to grab id in declare at cost of leaked
658 * OST object in same very rare unfortunate case (just bad)
659 * notice 1.6-2.0 do assignment outside of running transaction
660 * all the time, meaning many more chances for leaked objects.
662 * 2) send synchronous truncate RPC with just assigned id
665 /* there are few places in MDD code still passing NULL
666 * XXX: to be fixed soon */
670 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
671 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
672 LASSERT(!dt_object_exists(dt));
673 osp_object_assign_fid(env, d, o);
674 rc = osp_object_truncate(env, dt, attr->la_size);
679 if (!(attr->la_valid & (LA_UID | LA_GID)))
682 /* track all UID/GID changes via llog */
683 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
689 * Implement OSP layer dt_object_operations::do_attr_set() interface.
691 * Set attribute to the specified OST object.
693 * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
694 * in the OUT RPC that will be flushed when the remote transaction stop.
695 * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
696 * will be handled by a dedicated thread asynchronously.
698 * If the attribute entry exists in the OSP object attributes cache,
699 * then update the cached attribute according to given attribute.
701 * \param[in] env pointer to the thread context
702 * \param[in] dt pointer to the OSP layer dt_object
703 * \param[in] attr pointer to the attribute to be set
704 * \param[in] th pointer to the transaction handler
706 * \retval 0 for success
707 * \retval negative error number on failure
709 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
710 const struct lu_attr *attr, struct thandle *th)
712 struct osp_object *o = dt2osp_obj(dt);
716 /* we're interested in uid/gid changes only */
717 if (!(attr->la_valid & (LA_UID | LA_GID)))
720 if (!is_only_remote_trans(th)) {
721 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
722 /* XXX: send new uid/gid to OST ASAP? */
726 /* It is for OST-object attr_set directly without updating
727 * local MDT-object attribute. It is usually used by LFSCK. */
728 rc = osp_md_attr_set(env, dt, attr, th);
729 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
730 PFID(&dt->do_lu.lo_header->loh_fid), rc);
732 if (rc != 0 || o->opo_ooa == NULL)
735 /* Update the OSP object attributes cache. */
736 la = &o->opo_ooa->ooa_attr;
737 spin_lock(&o->opo_lock);
738 if (attr->la_valid & LA_UID) {
739 la->la_uid = attr->la_uid;
740 la->la_valid |= LA_UID;
743 if (attr->la_valid & LA_GID) {
744 la->la_gid = attr->la_gid;
745 la->la_valid |= LA_GID;
747 spin_unlock(&o->opo_lock);
754 * Interpreter function for getting OSP object extended attribute asynchronously
756 * Called to interpret the result of an async mode RPC for getting the
757 * OSP object extended attribute.
759 * \param[in] env pointer to the thread context
760 * \param[in] reply pointer to the RPC reply
761 * \param[in] req pointer to the RPC request
762 * \param[in] obj pointer to the OSP object
763 * \param[out] data pointer to OSP object attributes cache
764 * \param[in] index the index of the attribute buffer in the reply
765 * \param[in] rc the result for handling the RPC
767 * \retval 0 for success
768 * \retval negative error number on failure
770 static int osp_xattr_get_interpterer(const struct lu_env *env,
771 struct object_update_reply *reply,
772 struct ptlrpc_request *req,
773 struct osp_object *obj,
774 void *data, int index, int rc)
776 struct osp_object_attr *ooa = obj->opo_ooa;
777 struct osp_xattr_entry *oxe = data;
778 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
780 LASSERT(ooa != NULL);
783 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
785 rc = object_update_result_data_get(reply, rbuf, index);
786 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
787 spin_lock(&obj->opo_lock);
789 spin_unlock(&obj->opo_lock);
790 osp_oac_xattr_put(oxe);
792 return rc < 0 ? rc : -ERANGE;
795 spin_lock(&obj->opo_lock);
796 oxe->oxe_vallen = rbuf->lb_len;
797 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
800 spin_unlock(&obj->opo_lock);
801 } else if (rc == -ENOENT || rc == -ENODATA) {
802 spin_lock(&obj->opo_lock);
805 spin_unlock(&obj->opo_lock);
807 spin_lock(&obj->opo_lock);
809 spin_unlock(&obj->opo_lock);
812 osp_oac_xattr_put(oxe);
818 * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
820 * Declare that the caller will get extended attribute from the specified
823 * This function will add an OUT_XATTR_GET sub-request to the per OSP
824 * based shared asynchronous request queue with the interpreter function:
825 * osp_xattr_get_interpterer().
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
830 * \param[in] name the name for the expected extended attribute
832 * \retval 0 for success
833 * \retval negative error number on failure
835 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
836 struct lu_buf *buf, const char *name)
838 struct osp_object *obj = dt2osp_obj(dt);
839 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
840 struct osp_xattr_entry *oxe;
841 __u16 namelen = strlen(name);
844 LASSERT(buf != NULL);
845 LASSERT(name != NULL);
847 /* If only for xattr size, return directly. */
848 if (unlikely(buf->lb_len == 0))
851 if (obj->opo_ooa == NULL) {
852 rc = osp_oac_init(obj);
857 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
861 mutex_lock(&osp->opd_async_requests_mutex);
862 rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
863 &namelen, (const void **)&name,
865 osp_xattr_get_interpterer);
867 mutex_unlock(&osp->opd_async_requests_mutex);
868 osp_oac_xattr_put(oxe);
870 struct osp_update_request *our;
871 struct osp_update_request_sub *ours;
873 /* XXX: Currently, we trigger the batched async OUT
874 * RPC via dt_declare_xattr_get(). It is not
875 * perfect solution, but works well now.
877 * We will improve it in the future. */
878 our = osp->opd_async_requests;
879 ours = osp_current_object_update_request(our);
880 if (ours != NULL && ours->ours_req != NULL &&
881 ours->ours_req->ourq_count > 0) {
882 osp->opd_async_requests = NULL;
883 mutex_unlock(&osp->opd_async_requests_mutex);
884 rc = osp_unplug_async_request(env, osp, our);
886 mutex_unlock(&osp->opd_async_requests_mutex);
894 * Implement OSP layer dt_object_operations::do_xattr_get() interface.
896 * Get extended attribute from the specified MDT/OST object.
898 * If the extended attribute is in the OSP object attributes cache, then
899 * return the cached extended attribute directly. Otherwise it will get
900 * the extended attribute synchronously, if successful, add it to the OSP
901 * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
903 * There is a race condition: some other thread has added the named extended
904 * attributed entry to the OSP object attributes cache during the current
905 * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
906 * (just) existing extended attribute entry with the new replied one.
908 * \param[in] env pointer to the thread context
909 * \param[in] dt pointer to the OSP layer dt_object
910 * \param[out] buf pointer to the lu_buf to hold the extended attribute
911 * \param[in] name the name for the expected extended attribute
913 * \retval 0 for success
914 * \retval negative error number on failure
916 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
917 struct lu_buf *buf, const char *name)
919 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
920 struct osp_object *obj = dt2osp_obj(dt);
921 struct dt_device *dev = &osp->opd_dt_dev;
922 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
923 struct osp_update_request *update = NULL;
924 struct ptlrpc_request *req = NULL;
925 struct object_update_reply *reply;
926 struct osp_xattr_entry *oxe = NULL;
927 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
931 LASSERT(buf != NULL);
932 LASSERT(name != NULL);
934 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
935 osp->opd_index == cfs_fail_val) {
936 if (is_ost_obj(&dt->do_lu)) {
937 if (osp_dev2node(osp) == cfs_fail_val)
940 if (strcmp(name, XATTR_NAME_LINK) == 0)
945 if (unlikely(obj->opo_non_exist))
948 /* Only cache xattr for OST object */
949 if (!osp->opd_connect_mdt) {
950 oxe = osp_oac_xattr_find(obj, name, false);
952 spin_lock(&obj->opo_lock);
953 if (oxe->oxe_ready) {
955 GOTO(unlock, rc = -ENODATA);
957 if (buf->lb_buf == NULL)
958 GOTO(unlock, rc = oxe->oxe_vallen);
960 if (buf->lb_len < oxe->oxe_vallen)
961 GOTO(unlock, rc = -ERANGE);
963 memcpy(buf->lb_buf, oxe->oxe_value,
966 GOTO(unlock, rc = oxe->oxe_vallen);
969 spin_unlock(&obj->opo_lock);
970 osp_oac_xattr_put(oxe);
974 spin_unlock(&obj->opo_lock);
977 update = osp_update_request_create(dev);
979 GOTO(out, rc = PTR_ERR(update));
981 rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
982 lu_object_fid(&dt->do_lu), name, buf->lb_len);
984 CERROR("%s: Insert update error "DFID": rc = %d\n",
985 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
989 rc = osp_remote_sync(env, osp, update, &req);
992 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
993 obj->opo_non_exist = 1;
996 if (obj->opo_ooa == NULL)
1000 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
1003 CWARN("%s: Fail to add xattr (%s) to cache for "
1004 DFID" (1): rc = %d\n", dname, name,
1005 PFID(lu_object_fid(&dt->do_lu)), rc);
1010 spin_lock(&obj->opo_lock);
1011 if (rc == -ENOENT || rc == -ENODATA) {
1017 spin_unlock(&obj->opo_lock);
1022 reply = req_capsule_server_sized_get(&req->rq_pill,
1023 &RMF_OUT_UPDATE_REPLY,
1024 OUT_UPDATE_REPLY_SIZE);
1025 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1026 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1027 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1028 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1030 GOTO(out, rc = -EPROTO);
1033 rc = object_update_result_data_get(reply, rbuf, 0);
1037 if (buf->lb_buf == NULL)
1040 if (unlikely(buf->lb_len < rbuf->lb_len))
1041 GOTO(out, rc = -ERANGE);
1043 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1044 if (obj->opo_ooa == NULL || osp->opd_connect_mdt)
1048 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1050 CWARN("%s: Fail to add xattr (%s) to "
1051 "cache for "DFID" (2): rc = %d\n",
1052 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1058 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
1059 struct osp_xattr_entry *old = oxe;
1060 struct osp_xattr_entry *tmp;
1062 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
1063 osp_oac_xattr_put(oxe);
1066 CWARN("%s: Fail to update xattr (%s) to "
1067 "cache for "DFID": rc = %d\n",
1068 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1069 spin_lock(&obj->opo_lock);
1071 spin_unlock(&obj->opo_lock);
1076 /* Drop the ref for entry on list. */
1077 osp_oac_xattr_put(old);
1080 spin_lock(&obj->opo_lock);
1081 oxe->oxe_vallen = rbuf->lb_len;
1082 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
1085 spin_unlock(&obj->opo_lock);
1091 ptlrpc_req_finished(req);
1093 if (update != NULL && !IS_ERR(update))
1094 osp_update_request_destroy(update);
1097 osp_oac_xattr_put(oxe);
1103 * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1105 * Declare that the caller will set extended attribute to the specified
1108 * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1109 * to the OUT RPC that will be flushed when the transaction start. And if the
1110 * OSP attributes cache is initialized, then check whether the name extended
1111 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1112 * add the extended attribute to the cache.
1114 * \param[in] env pointer to the thread context
1115 * \param[in] dt pointer to the OSP layer dt_object
1116 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1117 * \param[in] name the name of the extended attribute to be set
1118 * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
1119 * or LU_XATTR_REPLACE or others
1120 * \param[in] th pointer to the transaction handler
1122 * \retval 0 for success
1123 * \retval negative error number on failure
1125 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1126 const struct lu_buf *buf, const char *name,
1127 int flag, struct thandle *th)
1129 return osp_trans_update_request_create(th);
1133 * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1135 * Set extended attribute to the specified MDT/OST object.
1137 * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
1138 * the transaction stop. And if the OSP attributes cache is initialized, then
1139 * check whether the name extended attribute entry exists in the cache or not.
1140 * If yes, replace it; otherwise, add the extended attribute to the cache.
1142 * \param[in] env pointer to the thread context
1143 * \param[in] dt pointer to the OSP layer dt_object
1144 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1145 * \param[in] name the name of the extended attribute to be set
1146 * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
1147 * or LU_XATTR_REPLACE or others
1148 * \param[in] th pointer to the transaction handler
1150 * \retval 0 for success
1151 * \retval negative error number on failure
1153 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1154 const struct lu_buf *buf, const char *name, int fl,
1157 struct osp_object *o = dt2osp_obj(dt);
1158 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1159 struct osp_update_request *update;
1160 struct osp_xattr_entry *oxe;
1164 update = thandle_to_osp_update_request(th);
1165 LASSERT(update != NULL);
1167 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
1168 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
1170 rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
1171 lu_object_fid(&dt->do_lu), buf, name, fl);
1172 if (rc != 0 || o->opo_ooa == NULL || osp->opd_connect_mdt)
1175 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1177 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1178 dt->do_lu.lo_dev->ld_obd->obd_name,
1179 name, PFID(lu_object_fid(&dt->do_lu)));
1184 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
1185 struct osp_xattr_entry *old = oxe;
1186 struct osp_xattr_entry *tmp;
1188 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
1189 osp_oac_xattr_put(oxe);
1192 CWARN("%s: cannot update cached xattr '%s' of "DFID"\n",
1193 dt->do_lu.lo_dev->ld_obd->obd_name,
1194 name, PFID(lu_object_fid(&dt->do_lu)));
1195 spin_lock(&o->opo_lock);
1197 spin_unlock(&o->opo_lock);
1202 /* Drop the ref for entry on list. */
1203 osp_oac_xattr_put(old);
1206 spin_lock(&o->opo_lock);
1207 oxe->oxe_vallen = buf->lb_len;
1208 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
1211 spin_unlock(&o->opo_lock);
1212 osp_oac_xattr_put(oxe);
1218 * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1220 * Declare that the caller will delete extended attribute on the specified
1223 * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1224 * to the OUT RPC that will be flushed when the transaction start. And if the
1225 * name extended attribute entry exists in the OSP attributes cache, then remove
1226 * it from the cache.
1228 * \param[in] env pointer to the thread context
1229 * \param[in] dt pointer to the OSP layer dt_object
1230 * \param[in] name the name of the extended attribute to be set
1231 * \param[in] th pointer to the transaction handler
1233 * \retval 0 for success
1234 * \retval negative error number on failure
1236 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1237 const char *name, struct thandle *th)
1239 return osp_trans_update_request_create(th);
1243 * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1245 * Delete extended attribute on the specified MDT/OST object.
1247 * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1248 * the OUT RPC that will be flushed when the transaction stop. And if the name
1249 * extended attribute entry exists in the OSP attributes cache, then remove it
1252 * \param[in] env pointer to the thread context
1253 * \param[in] dt pointer to the OSP layer dt_object
1254 * \param[in] name the name of the extended attribute to be set
1255 * \param[in] th pointer to the transaction handler
1257 * \retval 0 for success
1258 * \retval negative error number on failure
1260 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1261 const char *name, struct thandle *th)
1263 struct osp_update_request *update;
1264 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1265 struct osp_object *o = dt2osp_obj(dt);
1266 struct osp_xattr_entry *oxe;
1269 update = thandle_to_osp_update_request(th);
1270 LASSERT(update != NULL);
1272 rc = osp_update_rpc_pack(env, xattr_del, update, OUT_XATTR_DEL,
1274 if (rc != 0 || o->opo_ooa == NULL)
1277 oxe = osp_oac_xattr_find(o, name, true);
1279 /* Drop the ref for entry on list. */
1280 osp_oac_xattr_put(oxe);
1286 * Implement OSP layer dt_object_operations::do_declare_create() interface.
1288 * Declare that the caller will create the OST object.
1290 * If the transaction is a remote transaction and the FID for the OST-object
1291 * has been assigned already, then handle it as creating (remote) MDT object
1292 * via osp_md_declare_object_create(). This function is usually used for LFSCK
1293 * to re-create the lost OST object. Otherwise, if it is not replay case, the
1294 * OSP will reserve pre-created object for the subsequent create operation;
1295 * if the MDT side cached pre-created objects are less than some threshold,
1296 * then it will wakeup the pre-create thread.
1298 * \param[in] env pointer to the thread context
1299 * \param[in] dt pointer to the OSP layer dt_object
1300 * \param[in] attr the attribute for the object to be created
1301 * \param[in] hint pointer to the hint for creating the object, such as
1303 * \param[in] dof pointer to the dt_object_format for help the creation
1304 * \param[in] th pointer to the transaction handler
1306 * \retval 0 for success
1307 * \retval negative error number on failure
1309 static int osp_declare_object_create(const struct lu_env *env,
1310 struct dt_object *dt,
1311 struct lu_attr *attr,
1312 struct dt_allocation_hint *hint,
1313 struct dt_object_format *dof,
1316 struct osp_thread_info *osi = osp_env_info(env);
1317 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1318 struct osp_object *o = dt2osp_obj(dt);
1319 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1320 struct thandle *local_th;
1325 if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1326 LASSERT(fid_is_sane(fid));
1328 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
1333 /* should happen to non-0 OSP only so that at least one object
1334 * has been already declared in the scenario and LOD should
1336 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1339 LASSERT(d->opd_last_used_oid_file);
1342 * There can be gaps in precreated ids and record to unlink llog
1343 * XXX: we do not handle gaps yet, implemented before solution
1344 * was found to be racy, so we disabled that. there is no
1345 * point in making useless but expensive llog declaration.
1347 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1349 local_th = osp_get_storage_thandle(env, th, d);
1350 if (IS_ERR(local_th))
1351 RETURN(PTR_ERR(local_th));
1353 if (unlikely(!fid_is_zero(fid))) {
1354 /* replay case: caller knows fid */
1355 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1356 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1357 osi->osi_lb.lb_buf = NULL;
1359 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1360 &osi->osi_lb, osi->osi_off,
1366 * in declaration we need to reserve object so that we don't block
1367 * awaiting precreation RPC to complete
1369 rc = osp_precreate_reserve(env, d);
1371 * we also need to declare update to local "last used id" file for
1372 * recovery if object isn't used for a reason, we need to release
1373 * reservation, this can be made in osd_object_release()
1376 /* mark id is reserved: in create we don't want to talk
1378 LASSERT(o->opo_reserved == 0);
1379 o->opo_reserved = 1;
1381 /* common for all OSPs file hystorically */
1382 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1383 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1384 osi->osi_lb.lb_buf = NULL;
1385 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1386 &osi->osi_lb, osi->osi_off,
1389 /* not needed in the cache anymore */
1390 set_bit(LU_OBJECT_HEARD_BANSHEE,
1391 &dt->do_lu.lo_header->loh_flags);
1397 * Implement OSP layer dt_object_operations::do_create() interface.
1399 * Create the OST object.
1401 * If the transaction is a remote transaction and the FID for the OST-object
1402 * has been assigned already, then handle it as handling MDT object via the
1403 * osp_md_object_create(). For other cases, the OSP will assign FID to the
1404 * object to be created, and update last_used Object ID (OID) file.
1406 * \param[in] env pointer to the thread context
1407 * \param[in] dt pointer to the OSP layer dt_object
1408 * \param[in] attr the attribute for the object to be created
1409 * \param[in] hint pointer to the hint for creating the object, such as
1411 * \param[in] dof pointer to the dt_object_format for help the creation
1412 * \param[in] th pointer to the transaction handler
1414 * \retval 0 for success
1415 * \retval negative error number on failure
1417 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
1418 struct lu_attr *attr,
1419 struct dt_allocation_hint *hint,
1420 struct dt_object_format *dof, struct thandle *th)
1422 struct osp_thread_info *osi = osp_env_info(env);
1423 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1424 struct osp_object *o = dt2osp_obj(dt);
1426 struct lu_fid *fid = &osi->osi_fid;
1427 struct thandle *local_th;
1430 if (is_only_remote_trans(th) &&
1431 !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1432 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1434 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
1436 o->opo_non_exist = 0;
1441 o->opo_non_exist = 0;
1442 if (o->opo_reserved) {
1443 /* regular case, fid is assigned holding transaction open */
1444 osp_object_assign_fid(env, d, o);
1447 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1449 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1452 if (!o->opo_reserved) {
1453 /* special case, id was assigned outside of transaction
1454 * see comments in osp_declare_attr_set */
1455 LASSERT(d->opd_pre != NULL);
1456 spin_lock(&d->opd_pre_lock);
1457 osp_update_last_fid(d, fid);
1458 spin_unlock(&d->opd_pre_lock);
1461 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1463 /* If the precreate ends, it means it will be ready to rollover to
1464 * the new sequence soon, all the creation should be synchronized,
1465 * otherwise during replay, the replay fid will be inconsistent with
1466 * last_used/create fid */
1467 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1470 local_th = osp_get_storage_thandle(env, th, d);
1471 if (IS_ERR(local_th))
1472 RETURN(PTR_ERR(local_th));
1474 * it's OK if the import is inactive by this moment - id was created
1475 * by OST earlier, we just need to maintain it consistently on the disk
1476 * once import is reconnected, OSP will claim this and other objects
1477 * used and OST either keep them, if they exist or recreate
1480 /* we might have lost precreated objects */
1481 if (unlikely(d->opd_gap_count) > 0) {
1482 LASSERT(d->opd_pre != NULL);
1483 spin_lock(&d->opd_pre_lock);
1484 if (d->opd_gap_count > 0) {
1485 int count = d->opd_gap_count;
1487 ostid_set_id(&osi->osi_oi,
1488 fid_oid(&d->opd_gap_start_fid));
1489 d->opd_gap_count = 0;
1490 spin_unlock(&d->opd_pre_lock);
1492 CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1493 PFID(&d->opd_gap_start_fid), count);
1494 /* real gap handling is disabled intil ORI-692 will be
1495 * fixed, now we only report gaps */
1497 spin_unlock(&d->opd_pre_lock);
1501 /* Only need update last_used oid file, seq file will only be update
1502 * during seq rollover */
1503 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1504 &d->opd_last_used_fid.f_oid, d->opd_index);
1506 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1507 &osi->osi_off, local_th);
1509 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1510 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1516 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1518 * Declare that the caller will destroy the specified OST object.
1520 * The OST object destroy will be handled via llog asynchronously. This
1521 * function will declare the credits for generating MDS_UNLINK64_REC llog.
1523 * \param[in] env pointer to the thread context
1524 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1525 * \param[in] th pointer to the transaction handler
1527 * \retval 0 for success
1528 * \retval negative error number on failure
1530 int osp_declare_object_destroy(const struct lu_env *env,
1531 struct dt_object *dt, struct thandle *th)
1533 struct osp_object *o = dt2osp_obj(dt);
1534 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1539 LASSERT(!osp->opd_connect_mdt);
1540 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1546 * Implement OSP layer dt_object_operations::do_destroy() interface.
1548 * Destroy the specified OST object.
1550 * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1551 * will be some dedicated thread to handle the llog asynchronously.
1553 * It also marks the object as non-cached.
1555 * \param[in] env pointer to the thread context
1556 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1557 * \param[in] th pointer to the transaction handler
1559 * \retval 0 for success
1560 * \retval negative error number on failure
1562 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1565 struct osp_object *o = dt2osp_obj(dt);
1566 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1571 o->opo_non_exist = 1;
1573 LASSERT(!osp->opd_connect_mdt);
1574 /* once transaction is committed put proper command on
1575 * the queue going to our OST. */
1576 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1580 /* not needed in cache any more */
1581 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1586 static int osp_orphan_index_lookup(const struct lu_env *env,
1587 struct dt_object *dt,
1589 const struct dt_key *key)
1594 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1595 struct dt_object *dt,
1596 const struct dt_rec *rec,
1597 const struct dt_key *key,
1598 struct thandle *handle)
1603 static int osp_orphan_index_insert(const struct lu_env *env,
1604 struct dt_object *dt,
1605 const struct dt_rec *rec,
1606 const struct dt_key *key,
1607 struct thandle *handle,
1613 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1614 struct dt_object *dt,
1615 const struct dt_key *key,
1616 struct thandle *handle)
1621 static int osp_orphan_index_delete(const struct lu_env *env,
1622 struct dt_object *dt,
1623 const struct dt_key *key,
1624 struct thandle *handle)
1630 * Initialize the OSP layer index iteration.
1632 * \param[in] env pointer to the thread context
1633 * \param[in] dt pointer to the index object to be iterated
1634 * \param[in] attr unused
1636 * \retval pointer to the iteration structure
1637 * \retval negative error number on failure
1639 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1646 return ERR_PTR(-ENOMEM);
1648 it->ooi_pos_ent = -1;
1650 it->ooi_attr = attr;
1652 return (struct dt_it *)it;
1656 * Finalize the OSP layer index iteration.
1658 * \param[in] env pointer to the thread context
1659 * \param[in] di pointer to the iteration structure
1661 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1663 struct osp_it *it = (struct osp_it *)di;
1664 struct page **pages = it->ooi_pages;
1665 int npages = it->ooi_total_npages;
1668 if (pages != NULL) {
1669 for (i = 0; i < npages; i++) {
1670 if (pages[i] != NULL) {
1671 if (pages[i] == it->ooi_cur_page) {
1673 it->ooi_cur_page = NULL;
1675 __free_page(pages[i]);
1678 OBD_FREE(pages, npages * sizeof(*pages));
1684 * Get more records for the iteration from peer.
1686 * The new records will be filled in an array of pages. The OSP side
1687 * allows 1MB bulk data to be transferred.
1689 * \param[in] env pointer to the thread context
1690 * \param[in] it pointer to the iteration structure
1692 * \retval 0 for success
1693 * \retval negative error number on failure
1695 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1697 struct lu_device *dev = it->ooi_obj->do_lu.lo_dev;
1698 struct osp_device *osp = lu2osp_dev(dev);
1699 struct page **pages;
1700 struct lu_device *top_device;
1701 struct ptlrpc_request *req = NULL;
1702 struct ptlrpc_bulk_desc *desc;
1703 struct idx_info *ii;
1710 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1711 npages /= PAGE_CACHE_SIZE;
1713 OBD_ALLOC(pages, npages * sizeof(*pages));
1717 it->ooi_pages = pages;
1718 it->ooi_total_npages = npages;
1719 for (i = 0; i < npages; i++) {
1720 pages[i] = alloc_page(GFP_IOFS);
1721 if (pages[i] == NULL)
1725 req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1730 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1732 ptlrpc_request_free(req);
1736 /* Let's allow this request during recovery, otherwise
1737 * if the remote target is also in recovery status,
1738 * it might cause deadlock */
1739 top_device = dev->ld_site->ls_top_dev;
1740 if (top_device->ld_obd->obd_recovering)
1741 req->rq_allow_replay = 1;
1743 req->rq_request_portal = OUT_PORTAL;
1744 ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1745 memset(ii, 0, sizeof(*ii));
1746 if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1747 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1748 * ost_index, 0] with LAST_ID FID, so it needs to replace
1749 * the FID with orphan FID here */
1750 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1751 ii->ii_fid.f_oid = osp->opd_index;
1752 ii->ii_fid.f_ver = 0;
1753 ii->ii_flags = II_FL_NOHASH;
1754 ii->ii_attrs = osp_dev2node(osp);
1756 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1757 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1759 ii->ii_attrs = it->ooi_attr;
1761 ii->ii_magic = IDX_INFO_MAGIC;
1762 ii->ii_count = npages * LU_PAGE_COUNT;
1763 ii->ii_hash_start = it->ooi_next;
1765 ptlrpc_at_set_req_timeout(req);
1767 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1768 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1770 &ptlrpc_bulk_kiov_pin_ops);
1772 ptlrpc_request_free(req);
1776 for (i = 0; i < npages; i++)
1777 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1780 ptlrpc_request_set_replen(req);
1781 rc = ptlrpc_queue_wait(req);
1785 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1786 req->rq_bulk->bd_nob_transferred);
1791 ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1792 if (ii->ii_magic != IDX_INFO_MAGIC)
1793 GOTO(out, rc = -EPROTO);
1795 npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1796 (PAGE_CACHE_SHIFT - LU_PAGE_SHIFT);
1797 if (npages > it->ooi_total_npages) {
1798 CERROR("%s: returned more pages than expected, %u > %u\n",
1799 osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1800 GOTO(out, rc = -EINVAL);
1803 it->ooi_valid_npages = npages;
1804 if (ptlrpc_rep_need_swab(req))
1807 it->ooi_next = ii->ii_hash_end;
1810 ptlrpc_req_finished(req);
1816 * Move the iteration cursor to the next lu_page.
1818 * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1819 * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1820 * in current system page, then move the iteration cursor to the next
1821 * lu_page in current system page. Otherwise, if there are more system
1822 * pages in the cache, then move the iteration cursor to the next system
1823 * page. If all the cached records (pages) have been iterated, then fetch
1824 * more records via osp_it_fetch().
1826 * \param[in] env pointer to the thread context
1827 * \param[in] di pointer to the iteration structure
1829 * \retval positive for end of the directory
1830 * \retval 0 for success
1831 * \retval negative error number on failure
1833 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1835 struct osp_it *it = (struct osp_it *)di;
1836 struct lu_idxpage *idxpage;
1837 struct page **pages;
1843 idxpage = it->ooi_cur_idxpage;
1844 if (idxpage != NULL) {
1845 if (idxpage->lip_nr == 0)
1848 if (it->ooi_pos_ent < idxpage->lip_nr) {
1849 CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1850 (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1853 it->ooi_cur_idxpage = NULL;
1854 it->ooi_pos_lu_page++;
1857 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1858 it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1859 LU_PAGE_SIZE * it->ooi_pos_lu_page;
1861 lustre_swab_lip_header(it->ooi_cur_idxpage);
1862 if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1863 struct osp_device *osp =
1864 lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1866 CERROR("%s: invalid magic (%x != %x) for page "
1867 "%d/%d while read layout orphan index\n",
1868 osp->opd_obd->obd_name,
1869 it->ooi_cur_idxpage->lip_magic,
1870 LIP_MAGIC, it->ooi_pos_page,
1871 it->ooi_pos_lu_page);
1872 /* Skip this lu_page next time. */
1873 it->ooi_pos_ent = idxpage->lip_nr - 1;
1876 it->ooi_pos_ent = -1;
1880 kunmap(it->ooi_cur_page);
1881 it->ooi_cur_page = NULL;
1885 pages = it->ooi_pages;
1886 if (it->ooi_pos_page < it->ooi_valid_npages) {
1887 it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1888 it->ooi_pos_lu_page = 0;
1892 for (i = 0; i < it->ooi_total_npages; i++) {
1893 if (pages[i] != NULL)
1894 __free_page(pages[i]);
1896 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1898 it->ooi_pos_page = 0;
1899 it->ooi_total_npages = 0;
1900 it->ooi_valid_npages = 0;
1903 it->ooi_cur_page = NULL;
1904 it->ooi_cur_idxpage = NULL;
1905 it->ooi_pages = NULL;
1908 if (it->ooi_next == II_END_OFF)
1911 rc = osp_it_fetch(env, it);
1919 * Move the iteration cursor to the next record.
1921 * If there are more records in the lu_page, then move the iteration
1922 * cursor to the next record directly. Otherwise, move the iteration
1923 * cursor to the record in the next lu_page via osp_it_next_page()
1925 * \param[in] env pointer to the thread context
1926 * \param[in] di pointer to the iteration structure
1928 * \retval positive for end of the directory
1929 * \retval 0 for success
1930 * \retval negative error number on failure
1932 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1934 struct osp_it *it = (struct osp_it *)di;
1935 struct lu_idxpage *idxpage;
1940 idxpage = it->ooi_cur_idxpage;
1941 if (idxpage != NULL) {
1942 if (idxpage->lip_nr == 0)
1946 if (it->ooi_pos_ent < idxpage->lip_nr) {
1948 (struct lu_orphan_ent *)idxpage->lip_entries +
1951 lustre_swab_orphan_ent(it->ooi_ent);
1956 rc = osp_it_next_page(env, di);
1963 int osp_it_get(const struct lu_env *env, struct dt_it *di,
1964 const struct dt_key *key)
1969 void osp_it_put(const struct lu_env *env, struct dt_it *di)
1973 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
1974 const struct dt_it *di)
1976 struct osp_it *it = (struct osp_it *)di;
1977 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1979 if (likely(ent != NULL))
1980 return (struct dt_key *)(&ent->loe_key);
1985 static int osp_orphan_it_key_size(const struct lu_env *env,
1986 const struct dt_it *di)
1988 return sizeof(struct lu_fid);
1991 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
1992 struct dt_rec *rec, __u32 attr)
1994 struct osp_it *it = (struct osp_it *)di;
1995 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1997 if (likely(ent != NULL)) {
1998 *(struct lu_orphan_rec *)rec = ent->loe_rec;
2005 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
2007 struct osp_it *it = (struct osp_it *)di;
2009 return it->ooi_next;
2013 * Locate the iteration cursor to the specified position (cookie).
2015 * \param[in] env pointer to the thread context
2016 * \param[in] di pointer to the iteration structure
2017 * \param[in] hash the specified position
2019 * \retval positive number for locating to the exactly position
2021 * \retval 0 for arriving at the end of the iteration
2022 * \retval negative error number on failure
2024 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2027 struct osp_it *it = (struct osp_it *)di;
2030 it->ooi_next = hash;
2031 rc = osp_orphan_it_next(env, (struct dt_it *)di);
2041 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2047 static const struct dt_index_operations osp_orphan_index_ops = {
2048 .dio_lookup = osp_orphan_index_lookup,
2049 .dio_declare_insert = osp_orphan_index_declare_insert,
2050 .dio_insert = osp_orphan_index_insert,
2051 .dio_declare_delete = osp_orphan_index_declare_delete,
2052 .dio_delete = osp_orphan_index_delete,
2054 .init = osp_it_init,
2055 .fini = osp_it_fini,
2056 .next = osp_orphan_it_next,
2059 .key = osp_orphan_it_key,
2060 .key_size = osp_orphan_it_key_size,
2061 .rec = osp_orphan_it_rec,
2062 .store = osp_it_store,
2063 .load = osp_orphan_it_load,
2064 .key_rec = osp_it_key_rec,
2069 * Implement OSP layer dt_object_operations::do_index_try() interface.
2071 * Negotiate the index type.
2073 * If the target index is an IDIF object, then use osp_orphan_index_ops.
2074 * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2075 * (\see lustre/include/lustre_fid.h for IDIF.)
2077 * \param[in] env pointer to the thread context
2078 * \param[in] dt pointer to the OSP layer dt_object
2079 * \param[in] feat unused
2081 * \retval 0 for success
2083 static int osp_index_try(const struct lu_env *env,
2084 struct dt_object *dt,
2085 const struct dt_index_features *feat)
2087 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2089 if (fid_is_last_id(fid) && fid_is_idif(fid))
2090 dt->do_index_ops = &osp_orphan_index_ops;
2092 dt->do_index_ops = &osp_md_index_ops;
2096 static struct dt_object_operations osp_obj_ops = {
2097 .do_declare_attr_get = osp_declare_attr_get,
2098 .do_attr_get = osp_attr_get,
2099 .do_declare_attr_set = osp_declare_attr_set,
2100 .do_attr_set = osp_attr_set,
2101 .do_declare_xattr_get = osp_declare_xattr_get,
2102 .do_xattr_get = osp_xattr_get,
2103 .do_declare_xattr_set = osp_declare_xattr_set,
2104 .do_xattr_set = osp_xattr_set,
2105 .do_declare_create = osp_declare_object_create,
2106 .do_create = osp_object_create,
2107 .do_declare_destroy = osp_declare_object_destroy,
2108 .do_destroy = osp_object_destroy,
2109 .do_index_try = osp_index_try,
2113 * Implement OSP layer lu_object_operations::loo_object_init() interface.
2115 * Initialize the object.
2117 * If it is a remote MDT object, then call do_attr_get() to fetch
2118 * the attribute from the peer.
2120 * \param[in] env pointer to the thread context
2121 * \param[in] o pointer to the OSP layer lu_object
2122 * \param[in] conf unused
2124 * \retval 0 for success
2125 * \retval negative error number on failure
2127 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2128 const struct lu_object_conf *conf)
2130 struct osp_object *po = lu2osp_obj(o);
2134 spin_lock_init(&po->opo_lock);
2135 o->lo_header->loh_attr |= LOHA_REMOTE;
2137 if (is_ost_obj(o)) {
2138 po->opo_obj.do_ops = &osp_obj_ops;
2140 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2142 po->opo_obj.do_ops = &osp_md_obj_ops;
2143 po->opo_obj.do_body_ops = &osp_md_body_ops;
2144 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
2145 po->opo_non_exist = 1;
2147 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2150 o->lo_header->loh_attr |=
2151 LOHA_EXISTS | (la->la_mode & S_IFMT);
2152 if (rc == -ENOENT) {
2153 po->opo_non_exist = 1;
2157 init_rwsem(&po->opo_sem);
2163 * Implement OSP layer lu_object_operations::loo_object_free() interface.
2165 * Finalize the object.
2167 * If the OSP object has attributes cache, then destroy the cache.
2168 * Free the object finally.
2170 * \param[in] env pointer to the thread context
2171 * \param[in] o pointer to the OSP layer lu_object
2173 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2175 struct osp_object *obj = lu2osp_obj(o);
2176 struct lu_object_header *h = o->lo_header;
2178 dt_object_fini(&obj->opo_obj);
2179 lu_object_header_fini(h);
2180 if (obj->opo_ooa != NULL) {
2181 struct osp_xattr_entry *oxe;
2182 struct osp_xattr_entry *tmp;
2185 list_for_each_entry_safe(oxe, tmp,
2186 &obj->opo_ooa->ooa_xattr_list,
2188 list_del(&oxe->oxe_list);
2189 count = atomic_read(&oxe->oxe_ref);
2190 LASSERTF(count == 1,
2191 "Still has %d users on the xattr entry %.*s\n",
2192 count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2194 OBD_FREE(oxe, oxe->oxe_buflen);
2196 OBD_FREE_PTR(obj->opo_ooa);
2198 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2202 * Implement OSP layer lu_object_operations::loo_object_release() interface.
2204 * Cleanup (not free) the object.
2206 * If it is a reserved object but failed to be created, or it is an OST
2207 * object, then mark the object as non-cached.
2209 * \param[in] env pointer to the thread context
2210 * \param[in] o pointer to the OSP layer lu_object
2212 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2214 struct osp_object *po = lu2osp_obj(o);
2215 struct osp_device *d = lu2osp_dev(o->lo_dev);
2220 * release reservation if object was declared but not created
2221 * this may require lu_object_put() in LOD
2223 if (unlikely(po->opo_reserved)) {
2224 LASSERT(d->opd_pre != NULL);
2225 LASSERT(d->opd_pre_reserved > 0);
2226 spin_lock(&d->opd_pre_lock);
2227 d->opd_pre_reserved--;
2228 spin_unlock(&d->opd_pre_lock);
2230 /* not needed in cache any more */
2231 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2235 /* XXX: Currently, NOT cache OST-object on MDT because:
2236 * 1. it is not often accessed on MDT.
2237 * 2. avoid up layer (such as LFSCK) to load too many
2238 * once-used OST-objects. */
2239 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2244 static int osp_object_print(const struct lu_env *env, void *cookie,
2245 lu_printer_t p, const struct lu_object *l)
2247 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2249 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2252 static int osp_object_invariant(const struct lu_object *o)
2257 struct lu_object_operations osp_lu_obj_ops = {
2258 .loo_object_init = osp_object_init,
2259 .loo_object_free = osp_object_free,
2260 .loo_object_release = osp_object_release,
2261 .loo_object_print = osp_object_print,
2262 .loo_object_invariant = osp_object_invariant