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_swab.h>
128 #include "osp_internal.h"
130 static inline __u32 osp_dev2node(struct osp_device *osp)
132 return osp->opd_storage->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
135 static inline bool is_ost_obj(struct lu_object *lo)
137 return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
141 * Assign FID to the OST object.
143 * This function will assign the FID to the OST object of a striped file.
145 * \param[in] env pointer to the thread context
146 * \param[in] d pointer to the OSP device
147 * \param[in] o pointer to the OSP object that the FID will be
150 static void osp_object_assign_fid(const struct lu_env *env,
151 struct osp_device *d, struct osp_object *o)
153 struct osp_thread_info *osi = osp_env_info(env);
155 LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
156 LASSERT(o->opo_reserved);
159 osp_precreate_get_fid(env, d, &osi->osi_fid);
161 lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
165 * Initialize the OSP object attributes cache.
167 * \param[in] obj pointer to the OSP object
169 * \retval 0 for success
170 * \retval negative error number on failure
172 int osp_oac_init(struct osp_object *obj)
174 struct osp_object_attr *ooa;
180 INIT_LIST_HEAD(&ooa->ooa_xattr_list);
181 spin_lock(&obj->opo_lock);
182 if (likely(obj->opo_ooa == NULL)) {
184 spin_unlock(&obj->opo_lock);
186 spin_unlock(&obj->opo_lock);
194 * Find the named extended attribute in the OSP object attributes cache.
196 * The caller should take the osp_object::opo_lock before calling
199 * \param[in] ooa pointer to the OSP object attributes cache
200 * \param[in] name the name of the extended attribute
201 * \param[in] namelen the name length of the extended attribute
203 * \retval pointer to the found extended attribute entry
204 * \retval NULL if the specified extended attribute is not
207 static struct osp_xattr_entry *
208 osp_oac_xattr_find_locked(struct osp_object_attr *ooa,
209 const char *name, size_t namelen)
211 struct osp_xattr_entry *oxe;
213 list_for_each_entry(oxe, &ooa->ooa_xattr_list, oxe_list) {
214 if (namelen == oxe->oxe_namelen &&
215 strncmp(name, oxe->oxe_buf, namelen) == 0)
223 * Find the named extended attribute in the OSP object attributes cache.
225 * Call osp_oac_xattr_find_locked() with the osp_object::opo_lock held.
227 * \param[in] obj pointer to the OSP object
228 * \param[in] name the name of the extended attribute
229 * \param[in] unlink true if the extended attribute entry is to be removed
232 * \retval pointer to the found extended attribute entry
233 * \retval NULL if the specified extended attribute is not
236 static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
237 const char *name, bool unlink)
239 struct osp_xattr_entry *oxe = NULL;
241 spin_lock(&obj->opo_lock);
242 if (obj->opo_ooa != NULL) {
243 oxe = osp_oac_xattr_find_locked(obj->opo_ooa, name,
247 list_del_init(&oxe->oxe_list);
249 atomic_inc(&oxe->oxe_ref);
252 spin_unlock(&obj->opo_lock);
258 * Find the named extended attribute in the OSP object attributes cache.
260 * If it is not in the cache, then add an empty entry (that will be
261 * filled later) to cache with the given name.
263 * \param[in] obj pointer to the OSP object
264 * \param[in] name the name of the extended attribute
265 * \param[in] len the length of the extended attribute value
267 * \retval pointer to the found or new-created extended
269 * \retval NULL if the specified extended attribute is not in the
270 * cache or fail to add new empty entry to the cache.
272 static struct osp_xattr_entry *
273 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, size_t len)
275 struct osp_object_attr *ooa = obj->opo_ooa;
276 struct osp_xattr_entry *oxe;
277 struct osp_xattr_entry *tmp = NULL;
278 size_t namelen = strlen(name);
279 size_t size = sizeof(*oxe) + namelen + 1 + len;
281 LASSERT(ooa != NULL);
283 oxe = osp_oac_xattr_find(obj, name, false);
287 OBD_ALLOC(oxe, size);
288 if (unlikely(oxe == NULL))
291 INIT_LIST_HEAD(&oxe->oxe_list);
292 oxe->oxe_buflen = size;
293 oxe->oxe_namelen = namelen;
294 memcpy(oxe->oxe_buf, name, namelen);
295 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
296 /* One ref is for the caller, the other is for the entry on the list. */
297 atomic_set(&oxe->oxe_ref, 2);
299 spin_lock(&obj->opo_lock);
300 tmp = osp_oac_xattr_find_locked(ooa, name, namelen);
302 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
304 atomic_inc(&tmp->oxe_ref);
305 spin_unlock(&obj->opo_lock);
316 * Add the given extended attribute to the OSP object attributes cache.
318 * If there is an old extended attributed entry with the same name,
319 * remove it from the cache and return it via the parameter \a poxe.
321 * \param[in] obj pointer to the OSP object
322 * \param[in,out] poxe double pointer to the OSP object extended attribute
323 * entry: the new extended attribute entry is transferred
324 * via such pointer target, and if old the extended
325 * attribute entry exists, then it will be returned back
326 * via such pointer target.
327 * \param[in] len the length of the (new) extended attribute value
329 * \retval pointer to the new extended attribute entry
330 * \retval NULL for failure cases.
332 static struct osp_xattr_entry *
333 osp_oac_xattr_replace(struct osp_object *obj,
334 struct osp_xattr_entry **poxe, size_t len)
336 struct osp_object_attr *ooa = obj->opo_ooa;
337 struct osp_xattr_entry *oxe;
338 size_t namelen = (*poxe)->oxe_namelen;
339 size_t size = sizeof(*oxe) + namelen + 1 + len;
341 LASSERT(ooa != NULL);
343 OBD_ALLOC(oxe, size);
344 if (unlikely(oxe == NULL))
347 INIT_LIST_HEAD(&oxe->oxe_list);
348 oxe->oxe_buflen = size;
349 oxe->oxe_namelen = namelen;
350 memcpy(oxe->oxe_buf, (*poxe)->oxe_buf, namelen);
351 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
352 /* One ref is for the caller, the other is for the entry on the list. */
353 atomic_set(&oxe->oxe_ref, 2);
355 spin_lock(&obj->opo_lock);
356 *poxe = osp_oac_xattr_find_locked(ooa, oxe->oxe_buf, namelen);
357 LASSERT(*poxe != NULL);
359 list_del_init(&(*poxe)->oxe_list);
360 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
361 spin_unlock(&obj->opo_lock);
367 * Release reference from the OSP object extended attribute entry.
369 * If it is the last reference, then free the entry.
371 * \param[in] oxe pointer to the OSP object extended attribute entry.
373 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
375 if (atomic_dec_and_test(&oxe->oxe_ref)) {
376 LASSERT(list_empty(&oxe->oxe_list));
378 OBD_FREE(oxe, oxe->oxe_buflen);
383 * Parse the OSP object attribute from the RPC reply.
385 * If the attribute is valid, then it will be added to the OSP object
388 * \param[in] env pointer to the thread context
389 * \param[in] reply pointer to the RPC reply
390 * \param[in] req pointer to the RPC request
391 * \param[out] attr pointer to buffer to hold the output attribute
392 * \param[in] obj pointer to the OSP object
393 * \param[in] index the index of the attribute buffer in the reply
395 * \retval 0 for success
396 * \retval negative error number on failure
398 static int osp_get_attr_from_reply(const struct lu_env *env,
399 struct object_update_reply *reply,
400 struct ptlrpc_request *req,
401 struct lu_attr *attr,
402 struct osp_object *obj, int index)
404 struct osp_thread_info *osi = osp_env_info(env);
405 struct lu_buf *rbuf = &osi->osi_lb2;
406 struct obdo *lobdo = &osi->osi_obdo;
410 rc = object_update_result_data_get(reply, rbuf, index);
414 wobdo = rbuf->lb_buf;
415 if (rbuf->lb_len != sizeof(*wobdo))
418 LASSERT(req != NULL);
419 if (ptlrpc_req_need_swab(req))
420 lustre_swab_obdo(wobdo);
422 lustre_get_wire_obdo(NULL, lobdo, wobdo);
423 spin_lock(&obj->opo_lock);
424 if (obj->opo_ooa != NULL) {
425 la_from_obdo(&obj->opo_ooa->ooa_attr, lobdo, lobdo->o_valid);
427 *attr = obj->opo_ooa->ooa_attr;
429 LASSERT(attr != NULL);
431 la_from_obdo(attr, lobdo, lobdo->o_valid);
433 spin_unlock(&obj->opo_lock);
439 * Interpreter function for getting OSP object attribute asynchronously.
441 * Called to interpret the result of an async mode RPC for getting the
442 * OSP object attribute.
444 * \param[in] env pointer to the thread context
445 * \param[in] reply pointer to the RPC reply
446 * \param[in] req pointer to the RPC request
447 * \param[in] obj pointer to the OSP object
448 * \param[out] data pointer to buffer to hold the output attribute
449 * \param[in] index the index of the attribute buffer in the reply
450 * \param[in] rc the result for handling the RPC
452 * \retval 0 for success
453 * \retval negative error number on failure
455 static int osp_attr_get_interpterer(const struct lu_env *env,
456 struct object_update_reply *reply,
457 struct ptlrpc_request *req,
458 struct osp_object *obj,
459 void *data, int index, int rc)
461 struct lu_attr *attr = data;
463 LASSERT(obj->opo_ooa != NULL);
466 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
467 obj->opo_non_exist = 0;
469 return osp_get_attr_from_reply(env, reply, req, NULL, obj,
473 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
474 obj->opo_non_exist = 1;
477 spin_lock(&obj->opo_lock);
479 spin_unlock(&obj->opo_lock);
486 * Implement OSP layer dt_object_operations::do_declare_attr_get() interface.
488 * Declare that the caller will get attribute from the specified OST object.
490 * This function adds an Object Unified Target (OUT) sub-request to the per-OSP
491 * based shared asynchronous request queue. The osp_attr_get_interpterer()
492 * is registered as the interpreter function to handle the result of this
495 * \param[in] env pointer to the thread context
496 * \param[in] dt pointer to the OSP layer dt_object
498 * \retval 0 for success
499 * \retval negative error number on failure
501 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt)
503 struct osp_object *obj = dt2osp_obj(dt);
504 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
507 if (obj->opo_ooa == NULL) {
508 rc = osp_oac_init(obj);
513 mutex_lock(&osp->opd_async_requests_mutex);
514 rc = osp_insert_async_request(env, OUT_ATTR_GET, obj, 0, NULL, NULL,
515 &obj->opo_ooa->ooa_attr,
517 osp_attr_get_interpterer);
518 mutex_unlock(&osp->opd_async_requests_mutex);
524 * Implement OSP layer dt_object_operations::do_attr_get() interface.
526 * Get attribute from the specified MDT/OST object.
528 * If the attribute is in the OSP object attributes cache, then return
529 * the cached attribute directly. Otherwise it will trigger an OUT RPC
530 * to the peer to get the attribute synchronously, if successful, add it
531 * to the OSP attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
533 * \param[in] env pointer to the thread context
534 * \param[in] dt pointer to the OSP layer dt_object
535 * \param[out] attr pointer to the buffer to hold the output attribute
537 * \retval 0 for success
538 * \retval negative error number on failure
540 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
541 struct lu_attr *attr)
543 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
544 struct osp_object *obj = dt2osp_obj(dt);
545 struct dt_device *dev = &osp->opd_dt_dev;
546 struct osp_update_request *update;
547 struct object_update_reply *reply;
548 struct ptlrpc_request *req = NULL;
552 if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
555 if (obj->opo_ooa != NULL) {
556 spin_lock(&obj->opo_lock);
557 if (obj->opo_ooa->ooa_attr.la_valid != 0 && !obj->opo_stale) {
558 *attr = obj->opo_ooa->ooa_attr;
559 spin_unlock(&obj->opo_lock);
563 spin_unlock(&obj->opo_lock);
566 update = osp_update_request_create(dev);
568 RETURN(PTR_ERR(update));
570 rc = osp_update_rpc_pack(env, attr_get, update, OUT_ATTR_GET,
571 lu_object_fid(&dt->do_lu));
573 CERROR("%s: Insert update error "DFID": rc = %d\n",
574 dev->dd_lu_dev.ld_obd->obd_name,
575 PFID(lu_object_fid(&dt->do_lu)), rc);
580 rc = osp_remote_sync(env, osp, update, &req);
583 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
584 obj->opo_non_exist = 1;
586 CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
587 dev->dd_lu_dev.ld_obd->obd_name,
588 PFID(lu_object_fid(&dt->do_lu)), rc);
594 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
595 obj->opo_non_exist = 0;
596 reply = req_capsule_server_sized_get(&req->rq_pill,
597 &RMF_OUT_UPDATE_REPLY,
598 OUT_UPDATE_REPLY_SIZE);
599 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
600 GOTO(out, rc = -EPROTO);
602 rc = osp_get_attr_from_reply(env, reply, req, attr, obj, 0);
606 spin_lock(&obj->opo_lock);
609 spin_unlock(&obj->opo_lock);
615 ptlrpc_req_finished(req);
617 osp_update_request_destroy(update);
623 * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
625 * If the transaction is not remote one, then declare the credits that will
626 * be used for the subsequent llog record for the object's attributes.
628 * \param[in] env pointer to the thread context
629 * \param[in] dt pointer to the OSP layer dt_object
630 * \param[in] attr pointer to the attribute to be set
631 * \param[in] th pointer to the transaction handler
633 * \retval 0 for success
634 * \retval negative error number on failure
636 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
637 const struct lu_attr *attr, struct thandle *th)
639 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
640 struct osp_object *o = dt2osp_obj(dt);
643 if (is_only_remote_trans(th))
644 return osp_md_declare_attr_set(env, dt, attr, th);
646 * Usually we don't allow server stack to manipulate size
647 * but there is a special case when striping is created
648 * late, after stripeless file got truncated to non-zero.
650 * In this case we do the following:
652 * 1) grab id in declare - this can lead to leaked OST objects
653 * but we don't currently have proper mechanism and the only
654 * options we have are to do truncate RPC holding transaction
655 * open (very bad) or to grab id in declare at cost of leaked
656 * OST object in same very rare unfortunate case (just bad)
657 * notice 1.6-2.0 do assignment outside of running transaction
658 * all the time, meaning many more chances for leaked objects.
660 * 2) send synchronous truncate RPC with just assigned id
663 /* there are few places in MDD code still passing NULL
664 * XXX: to be fixed soon */
668 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
669 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
670 LASSERT(!dt_object_exists(dt));
671 osp_object_assign_fid(env, d, o);
672 rc = osp_object_truncate(env, dt, attr->la_size);
677 if (!(attr->la_valid & (LA_UID | LA_GID)))
680 /* track all UID/GID changes via llog */
681 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
687 * Implement OSP layer dt_object_operations::do_attr_set() interface.
689 * Set attribute to the specified OST object.
691 * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
692 * in the OUT RPC that will be flushed when the remote transaction stop.
693 * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
694 * will be handled by a dedicated thread asynchronously.
696 * If the attribute entry exists in the OSP object attributes cache,
697 * then update the cached attribute according to given attribute.
699 * \param[in] env pointer to the thread context
700 * \param[in] dt pointer to the OSP layer dt_object
701 * \param[in] attr pointer to the attribute to be set
702 * \param[in] th pointer to the transaction handler
704 * \retval 0 for success
705 * \retval negative error number on failure
707 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
708 const struct lu_attr *attr, struct thandle *th)
710 struct osp_object *o = dt2osp_obj(dt);
714 /* we're interested in uid/gid changes only */
715 if (!(attr->la_valid & (LA_UID | LA_GID)))
718 if (!is_only_remote_trans(th)) {
719 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
720 /* XXX: send new uid/gid to OST ASAP? */
724 /* It is for OST-object attr_set directly without updating
725 * local MDT-object attribute. It is usually used by LFSCK. */
726 rc = osp_md_attr_set(env, dt, attr, th);
727 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
728 PFID(&dt->do_lu.lo_header->loh_fid), rc);
730 if (rc != 0 || o->opo_ooa == NULL)
733 /* Update the OSP object attributes cache. */
734 la = &o->opo_ooa->ooa_attr;
735 spin_lock(&o->opo_lock);
736 if (attr->la_valid & LA_UID) {
737 la->la_uid = attr->la_uid;
738 la->la_valid |= LA_UID;
741 if (attr->la_valid & LA_GID) {
742 la->la_gid = attr->la_gid;
743 la->la_valid |= LA_GID;
745 spin_unlock(&o->opo_lock);
752 * Interpreter function for getting OSP object extended attribute asynchronously
754 * Called to interpret the result of an async mode RPC for getting the
755 * OSP object extended attribute.
757 * \param[in] env pointer to the thread context
758 * \param[in] reply pointer to the RPC reply
759 * \param[in] req pointer to the RPC request
760 * \param[in] obj pointer to the OSP object
761 * \param[out] data pointer to OSP object attributes cache
762 * \param[in] index the index of the attribute buffer in the reply
763 * \param[in] rc the result for handling the RPC
765 * \retval 0 for success
766 * \retval negative error number on failure
768 static int osp_xattr_get_interpterer(const struct lu_env *env,
769 struct object_update_reply *reply,
770 struct ptlrpc_request *req,
771 struct osp_object *obj,
772 void *data, int index, int rc)
774 struct osp_object_attr *ooa = obj->opo_ooa;
775 struct osp_xattr_entry *oxe = data;
776 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
778 LASSERT(ooa != NULL);
781 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
783 rc = object_update_result_data_get(reply, rbuf, index);
784 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
785 spin_lock(&obj->opo_lock);
787 spin_unlock(&obj->opo_lock);
788 osp_oac_xattr_put(oxe);
790 return rc < 0 ? rc : -ERANGE;
793 spin_lock(&obj->opo_lock);
794 oxe->oxe_vallen = rbuf->lb_len;
795 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
798 spin_unlock(&obj->opo_lock);
799 } else if (rc == -ENOENT || rc == -ENODATA) {
800 spin_lock(&obj->opo_lock);
803 spin_unlock(&obj->opo_lock);
805 spin_lock(&obj->opo_lock);
807 spin_unlock(&obj->opo_lock);
810 osp_oac_xattr_put(oxe);
816 * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
818 * Declare that the caller will get extended attribute from the specified
821 * This function will add an OUT_XATTR_GET sub-request to the per OSP
822 * based shared asynchronous request queue with the interpreter function:
823 * osp_xattr_get_interpterer().
825 * \param[in] env pointer to the thread context
826 * \param[in] dt pointer to the OSP layer dt_object
827 * \param[out] buf pointer to the lu_buf to hold the extended attribute
828 * \param[in] name the name for the expected extended attribute
830 * \retval 0 for success
831 * \retval negative error number on failure
833 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
834 struct lu_buf *buf, const char *name)
836 struct osp_object *obj = dt2osp_obj(dt);
837 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
838 struct osp_xattr_entry *oxe;
839 __u16 namelen = strlen(name);
842 LASSERT(buf != NULL);
843 LASSERT(name != NULL);
845 /* If only for xattr size, return directly. */
846 if (unlikely(buf->lb_len == 0))
849 if (obj->opo_ooa == NULL) {
850 rc = osp_oac_init(obj);
855 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
859 mutex_lock(&osp->opd_async_requests_mutex);
860 rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
861 &namelen, (const void **)&name,
863 osp_xattr_get_interpterer);
865 mutex_unlock(&osp->opd_async_requests_mutex);
866 osp_oac_xattr_put(oxe);
868 struct osp_update_request *our;
869 struct osp_update_request_sub *ours;
871 /* XXX: Currently, we trigger the batched async OUT
872 * RPC via dt_declare_xattr_get(). It is not
873 * perfect solution, but works well now.
875 * We will improve it in the future. */
876 our = osp->opd_async_requests;
877 ours = osp_current_object_update_request(our);
878 if (ours != NULL && ours->ours_req != NULL &&
879 ours->ours_req->ourq_count > 0) {
880 osp->opd_async_requests = NULL;
881 mutex_unlock(&osp->opd_async_requests_mutex);
882 rc = osp_unplug_async_request(env, osp, our);
884 mutex_unlock(&osp->opd_async_requests_mutex);
892 * Implement OSP layer dt_object_operations::do_xattr_get() interface.
894 * Get extended attribute from the specified MDT/OST object.
896 * If the extended attribute is in the OSP object attributes cache, then
897 * return the cached extended attribute directly. Otherwise it will get
898 * the extended attribute synchronously, if successful, add it to the OSP
899 * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
901 * There is a race condition: some other thread has added the named extended
902 * attributed entry to the OSP object attributes cache during the current
903 * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
904 * (just) existing extended attribute entry with the new replied one.
906 * \param[in] env pointer to the thread context
907 * \param[in] dt pointer to the OSP layer dt_object
908 * \param[out] buf pointer to the lu_buf to hold the extended attribute
909 * \param[in] name the name for the expected extended attribute
911 * \retval 0 for success
912 * \retval negative error number on failure
914 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
915 struct lu_buf *buf, const char *name)
917 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
918 struct osp_object *obj = dt2osp_obj(dt);
919 struct dt_device *dev = &osp->opd_dt_dev;
920 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
921 struct osp_update_request *update = NULL;
922 struct ptlrpc_request *req = NULL;
923 struct object_update_reply *reply;
924 struct osp_xattr_entry *oxe = NULL;
925 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
929 LASSERT(buf != NULL);
930 LASSERT(name != NULL);
932 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
933 osp->opd_index == cfs_fail_val) {
934 if (is_ost_obj(&dt->do_lu)) {
935 if (osp_dev2node(osp) == cfs_fail_val)
938 if (strcmp(name, XATTR_NAME_LINK) == 0)
943 if (unlikely(obj->opo_non_exist))
946 /* Only cache xattr for OST object */
947 if (!osp->opd_connect_mdt) {
948 oxe = osp_oac_xattr_find(obj, name, false);
950 spin_lock(&obj->opo_lock);
951 if (oxe->oxe_ready) {
953 GOTO(unlock, rc = -ENODATA);
955 if (buf->lb_buf == NULL)
956 GOTO(unlock, rc = oxe->oxe_vallen);
958 if (buf->lb_len < oxe->oxe_vallen)
959 GOTO(unlock, rc = -ERANGE);
961 memcpy(buf->lb_buf, oxe->oxe_value,
964 GOTO(unlock, rc = oxe->oxe_vallen);
967 spin_unlock(&obj->opo_lock);
968 osp_oac_xattr_put(oxe);
972 spin_unlock(&obj->opo_lock);
975 update = osp_update_request_create(dev);
977 GOTO(out, rc = PTR_ERR(update));
979 rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
980 lu_object_fid(&dt->do_lu), name, buf->lb_len);
982 CERROR("%s: Insert update error "DFID": rc = %d\n",
983 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
987 rc = osp_remote_sync(env, osp, update, &req);
990 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
991 obj->opo_non_exist = 1;
994 if (obj->opo_ooa == NULL)
998 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
1001 CWARN("%s: Fail to add xattr (%s) to cache for "
1002 DFID" (1): rc = %d\n", dname, name,
1003 PFID(lu_object_fid(&dt->do_lu)), rc);
1008 spin_lock(&obj->opo_lock);
1009 if (rc == -ENOENT || rc == -ENODATA) {
1015 spin_unlock(&obj->opo_lock);
1020 reply = req_capsule_server_sized_get(&req->rq_pill,
1021 &RMF_OUT_UPDATE_REPLY,
1022 OUT_UPDATE_REPLY_SIZE);
1023 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1024 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1025 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1026 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1028 GOTO(out, rc = -EPROTO);
1031 rc = object_update_result_data_get(reply, rbuf, 0);
1035 if (buf->lb_buf == NULL)
1038 if (unlikely(buf->lb_len < rbuf->lb_len))
1039 GOTO(out, rc = -ERANGE);
1041 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1043 if (obj->opo_ooa == NULL || osp->opd_connect_mdt)
1047 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1049 CWARN("%s: Fail to add xattr (%s) to "
1050 "cache for "DFID" (2): rc = %d\n",
1051 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1057 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
1058 struct osp_xattr_entry *old = oxe;
1059 struct osp_xattr_entry *tmp;
1061 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
1062 osp_oac_xattr_put(oxe);
1065 CWARN("%s: Fail to update xattr (%s) to "
1066 "cache for "DFID": rc = %d\n",
1067 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1068 spin_lock(&obj->opo_lock);
1070 spin_unlock(&obj->opo_lock);
1075 /* Drop the ref for entry on list. */
1076 osp_oac_xattr_put(old);
1079 spin_lock(&obj->opo_lock);
1080 oxe->oxe_vallen = rbuf->lb_len;
1081 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
1084 spin_unlock(&obj->opo_lock);
1090 ptlrpc_req_finished(req);
1092 if (update != NULL && !IS_ERR(update))
1093 osp_update_request_destroy(update);
1096 osp_oac_xattr_put(oxe);
1102 * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1104 * Declare that the caller will set extended attribute to the specified
1107 * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1108 * to the OUT RPC that will be flushed when the transaction start. And if the
1109 * OSP attributes cache is initialized, then check whether the name extended
1110 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1111 * add the extended attribute to the cache.
1113 * \param[in] env pointer to the thread context
1114 * \param[in] dt pointer to the OSP layer dt_object
1115 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1116 * \param[in] name the name of the extended attribute to be set
1117 * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
1118 * or LU_XATTR_REPLACE or others
1119 * \param[in] th pointer to the transaction handler
1121 * \retval 0 for success
1122 * \retval negative error number on failure
1124 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1125 const struct lu_buf *buf, const char *name,
1126 int flag, struct thandle *th)
1128 return osp_trans_update_request_create(th);
1132 * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1134 * Set extended attribute to the specified MDT/OST object.
1136 * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
1137 * the transaction stop. And if the OSP attributes cache is initialized, then
1138 * check whether the name extended attribute entry exists in the cache or not.
1139 * If yes, replace it; otherwise, add the extended attribute to the cache.
1141 * \param[in] env pointer to the thread context
1142 * \param[in] dt pointer to the OSP layer dt_object
1143 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1144 * \param[in] name the name of the extended attribute to be set
1145 * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
1146 * or LU_XATTR_REPLACE or others
1147 * \param[in] th pointer to the transaction handler
1149 * \retval 0 for success
1150 * \retval negative error number on failure
1152 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1153 const struct lu_buf *buf, const char *name, int fl,
1156 struct osp_object *o = dt2osp_obj(dt);
1157 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1158 struct osp_update_request *update;
1159 struct osp_xattr_entry *oxe;
1163 update = thandle_to_osp_update_request(th);
1164 LASSERT(update != NULL);
1166 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
1167 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
1169 rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
1170 lu_object_fid(&dt->do_lu), buf, name, fl);
1171 if (rc != 0 || o->opo_ooa == NULL || osp->opd_connect_mdt)
1174 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1176 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1177 dt->do_lu.lo_dev->ld_obd->obd_name,
1178 name, PFID(lu_object_fid(&dt->do_lu)));
1183 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
1184 struct osp_xattr_entry *old = oxe;
1185 struct osp_xattr_entry *tmp;
1187 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
1188 osp_oac_xattr_put(oxe);
1191 CWARN("%s: cannot update cached xattr '%s' of "DFID"\n",
1192 dt->do_lu.lo_dev->ld_obd->obd_name,
1193 name, PFID(lu_object_fid(&dt->do_lu)));
1194 spin_lock(&o->opo_lock);
1196 spin_unlock(&o->opo_lock);
1201 /* Drop the ref for entry on list. */
1202 osp_oac_xattr_put(old);
1205 spin_lock(&o->opo_lock);
1206 oxe->oxe_vallen = buf->lb_len;
1207 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
1210 spin_unlock(&o->opo_lock);
1211 osp_oac_xattr_put(oxe);
1217 * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1219 * Declare that the caller will delete extended attribute on the specified
1222 * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1223 * to the OUT RPC that will be flushed when the transaction start. And if the
1224 * name extended attribute entry exists in the OSP attributes cache, then remove
1225 * it from the cache.
1227 * \param[in] env pointer to the thread context
1228 * \param[in] dt pointer to the OSP layer dt_object
1229 * \param[in] name the name of the extended attribute to be set
1230 * \param[in] th pointer to the transaction handler
1232 * \retval 0 for success
1233 * \retval negative error number on failure
1235 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1236 const char *name, struct thandle *th)
1238 return osp_trans_update_request_create(th);
1242 * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1244 * Delete extended attribute on the specified MDT/OST object.
1246 * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1247 * the OUT RPC that will be flushed when the transaction stop. And if the name
1248 * extended attribute entry exists in the OSP attributes cache, then remove it
1251 * \param[in] env pointer to the thread context
1252 * \param[in] dt pointer to the OSP layer dt_object
1253 * \param[in] name the name of the extended attribute to be set
1254 * \param[in] th pointer to the transaction handler
1256 * \retval 0 for success
1257 * \retval negative error number on failure
1259 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1260 const char *name, struct thandle *th)
1262 struct osp_update_request *update;
1263 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1264 struct osp_object *o = dt2osp_obj(dt);
1265 struct osp_xattr_entry *oxe;
1268 update = thandle_to_osp_update_request(th);
1269 LASSERT(update != NULL);
1271 rc = osp_update_rpc_pack(env, xattr_del, update, OUT_XATTR_DEL,
1273 if (rc != 0 || o->opo_ooa == NULL)
1276 oxe = osp_oac_xattr_find(o, name, true);
1278 /* Drop the ref for entry on list. */
1279 osp_oac_xattr_put(oxe);
1285 * Implement OSP layer dt_object_operations::do_declare_create() interface.
1287 * Declare that the caller will create the OST object.
1289 * If the transaction is a remote transaction and the FID for the OST-object
1290 * has been assigned already, then handle it as creating (remote) MDT object
1291 * via osp_md_declare_object_create(). This function is usually used for LFSCK
1292 * to re-create the lost OST object. Otherwise, if it is not replay case, the
1293 * OSP will reserve pre-created object for the subsequent create operation;
1294 * if the MDT side cached pre-created objects are less than some threshold,
1295 * then it will wakeup the pre-create thread.
1297 * \param[in] env pointer to the thread context
1298 * \param[in] dt pointer to the OSP layer dt_object
1299 * \param[in] attr the attribute for the object to be created
1300 * \param[in] hint pointer to the hint for creating the object, such as
1302 * \param[in] dof pointer to the dt_object_format for help the creation
1303 * \param[in] th pointer to the transaction handler
1305 * \retval 0 for success
1306 * \retval negative error number on failure
1308 static int osp_declare_object_create(const struct lu_env *env,
1309 struct dt_object *dt,
1310 struct lu_attr *attr,
1311 struct dt_allocation_hint *hint,
1312 struct dt_object_format *dof,
1315 struct osp_thread_info *osi = osp_env_info(env);
1316 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1317 struct osp_object *o = dt2osp_obj(dt);
1318 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1319 struct thandle *local_th;
1324 if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1325 LASSERT(fid_is_sane(fid));
1327 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
1332 /* should happen to non-0 OSP only so that at least one object
1333 * has been already declared in the scenario and LOD should
1335 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1338 LASSERT(d->opd_last_used_oid_file);
1341 * There can be gaps in precreated ids and record to unlink llog
1342 * XXX: we do not handle gaps yet, implemented before solution
1343 * was found to be racy, so we disabled that. there is no
1344 * point in making useless but expensive llog declaration.
1346 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1348 local_th = osp_get_storage_thandle(env, th, d);
1349 if (IS_ERR(local_th))
1350 RETURN(PTR_ERR(local_th));
1352 if (unlikely(!fid_is_zero(fid))) {
1353 /* replay case: caller knows fid */
1354 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1355 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1356 osi->osi_lb.lb_buf = NULL;
1358 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1359 &osi->osi_lb, osi->osi_off,
1365 * in declaration we need to reserve object so that we don't block
1366 * awaiting precreation RPC to complete
1368 rc = osp_precreate_reserve(env, d);
1370 * we also need to declare update to local "last used id" file for
1371 * recovery if object isn't used for a reason, we need to release
1372 * reservation, this can be made in osd_object_release()
1375 /* mark id is reserved: in create we don't want to talk
1377 LASSERT(o->opo_reserved == 0);
1378 o->opo_reserved = 1;
1380 /* common for all OSPs file hystorically */
1381 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1382 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1383 osi->osi_lb.lb_buf = NULL;
1384 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1385 &osi->osi_lb, osi->osi_off,
1388 /* not needed in the cache anymore */
1389 set_bit(LU_OBJECT_HEARD_BANSHEE,
1390 &dt->do_lu.lo_header->loh_flags);
1396 * Implement OSP layer dt_object_operations::do_create() interface.
1398 * Create the OST object.
1400 * If the transaction is a remote transaction and the FID for the OST-object
1401 * has been assigned already, then handle it as handling MDT object via the
1402 * osp_md_object_create(). For other cases, the OSP will assign FID to the
1403 * object to be created, and update last_used Object ID (OID) file.
1405 * \param[in] env pointer to the thread context
1406 * \param[in] dt pointer to the OSP layer dt_object
1407 * \param[in] attr the attribute for the object to be created
1408 * \param[in] hint pointer to the hint for creating the object, such as
1410 * \param[in] dof pointer to the dt_object_format for help the creation
1411 * \param[in] th pointer to the transaction handler
1413 * \retval 0 for success
1414 * \retval negative error number on failure
1416 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
1417 struct lu_attr *attr,
1418 struct dt_allocation_hint *hint,
1419 struct dt_object_format *dof, struct thandle *th)
1421 struct osp_thread_info *osi = osp_env_info(env);
1422 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1423 struct osp_object *o = dt2osp_obj(dt);
1425 struct lu_fid *fid = &osi->osi_fid;
1426 struct thandle *local_th;
1429 if (is_only_remote_trans(th) &&
1430 !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1431 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1433 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
1435 o->opo_non_exist = 0;
1440 o->opo_non_exist = 0;
1441 if (o->opo_reserved) {
1442 /* regular case, fid is assigned holding transaction open */
1443 osp_object_assign_fid(env, d, o);
1446 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1448 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1451 if (!o->opo_reserved) {
1452 /* special case, id was assigned outside of transaction
1453 * see comments in osp_declare_attr_set */
1454 LASSERT(d->opd_pre != NULL);
1455 spin_lock(&d->opd_pre_lock);
1456 osp_update_last_fid(d, fid);
1457 spin_unlock(&d->opd_pre_lock);
1460 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1462 /* If the precreate ends, it means it will be ready to rollover to
1463 * the new sequence soon, all the creation should be synchronized,
1464 * otherwise during replay, the replay fid will be inconsistent with
1465 * last_used/create fid */
1466 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1469 local_th = osp_get_storage_thandle(env, th, d);
1470 if (IS_ERR(local_th))
1471 RETURN(PTR_ERR(local_th));
1473 * it's OK if the import is inactive by this moment - id was created
1474 * by OST earlier, we just need to maintain it consistently on the disk
1475 * once import is reconnected, OSP will claim this and other objects
1476 * used and OST either keep them, if they exist or recreate
1479 /* we might have lost precreated objects */
1480 if (unlikely(d->opd_gap_count) > 0) {
1481 LASSERT(d->opd_pre != NULL);
1482 spin_lock(&d->opd_pre_lock);
1483 if (d->opd_gap_count > 0) {
1484 int count = d->opd_gap_count;
1486 ostid_set_id(&osi->osi_oi,
1487 fid_oid(&d->opd_gap_start_fid));
1488 d->opd_gap_count = 0;
1489 spin_unlock(&d->opd_pre_lock);
1491 CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1492 PFID(&d->opd_gap_start_fid), count);
1493 /* real gap handling is disabled intil ORI-692 will be
1494 * fixed, now we only report gaps */
1496 spin_unlock(&d->opd_pre_lock);
1500 /* Only need update last_used oid file, seq file will only be update
1501 * during seq rollover */
1502 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1503 &d->opd_last_used_fid.f_oid, d->opd_index);
1505 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1506 &osi->osi_off, local_th);
1508 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1509 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1515 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1517 * Declare that the caller will destroy the specified OST object.
1519 * The OST object destroy will be handled via llog asynchronously. This
1520 * function will declare the credits for generating MDS_UNLINK64_REC llog.
1522 * \param[in] env pointer to the thread context
1523 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1524 * \param[in] th pointer to the transaction handler
1526 * \retval 0 for success
1527 * \retval negative error number on failure
1529 int osp_declare_object_destroy(const struct lu_env *env,
1530 struct dt_object *dt, struct thandle *th)
1532 struct osp_object *o = dt2osp_obj(dt);
1533 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1538 LASSERT(!osp->opd_connect_mdt);
1539 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1545 * Implement OSP layer dt_object_operations::do_destroy() interface.
1547 * Destroy the specified OST object.
1549 * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1550 * will be some dedicated thread to handle the llog asynchronously.
1552 * It also marks the object as non-cached.
1554 * \param[in] env pointer to the thread context
1555 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1556 * \param[in] th pointer to the transaction handler
1558 * \retval 0 for success
1559 * \retval negative error number on failure
1561 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1564 struct osp_object *o = dt2osp_obj(dt);
1565 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1570 o->opo_non_exist = 1;
1572 LASSERT(!osp->opd_connect_mdt);
1573 /* once transaction is committed put proper command on
1574 * the queue going to our OST. */
1575 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1579 /* not needed in cache any more */
1580 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1585 static int osp_orphan_index_lookup(const struct lu_env *env,
1586 struct dt_object *dt,
1588 const struct dt_key *key)
1593 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1594 struct dt_object *dt,
1595 const struct dt_rec *rec,
1596 const struct dt_key *key,
1597 struct thandle *handle)
1602 static int osp_orphan_index_insert(const struct lu_env *env,
1603 struct dt_object *dt,
1604 const struct dt_rec *rec,
1605 const struct dt_key *key,
1606 struct thandle *handle,
1612 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1613 struct dt_object *dt,
1614 const struct dt_key *key,
1615 struct thandle *handle)
1620 static int osp_orphan_index_delete(const struct lu_env *env,
1621 struct dt_object *dt,
1622 const struct dt_key *key,
1623 struct thandle *handle)
1629 * Initialize the OSP layer index iteration.
1631 * \param[in] env pointer to the thread context
1632 * \param[in] dt pointer to the index object to be iterated
1633 * \param[in] attr unused
1635 * \retval pointer to the iteration structure
1636 * \retval negative error number on failure
1638 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1645 return ERR_PTR(-ENOMEM);
1647 it->ooi_pos_ent = -1;
1649 it->ooi_attr = attr;
1651 return (struct dt_it *)it;
1655 * Finalize the OSP layer index iteration.
1657 * \param[in] env pointer to the thread context
1658 * \param[in] di pointer to the iteration structure
1660 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1662 struct osp_it *it = (struct osp_it *)di;
1663 struct page **pages = it->ooi_pages;
1664 int npages = it->ooi_total_npages;
1667 if (pages != NULL) {
1668 for (i = 0; i < npages; i++) {
1669 if (pages[i] != NULL) {
1670 if (pages[i] == it->ooi_cur_page) {
1672 it->ooi_cur_page = NULL;
1674 __free_page(pages[i]);
1677 OBD_FREE(pages, npages * sizeof(*pages));
1683 * Get more records for the iteration from peer.
1685 * The new records will be filled in an array of pages. The OSP side
1686 * allows 1MB bulk data to be transferred.
1688 * \param[in] env pointer to the thread context
1689 * \param[in] it pointer to the iteration structure
1691 * \retval 0 for success
1692 * \retval negative error number on failure
1694 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1696 struct lu_device *dev = it->ooi_obj->do_lu.lo_dev;
1697 struct osp_device *osp = lu2osp_dev(dev);
1698 struct page **pages;
1699 struct lu_device *top_device;
1700 struct ptlrpc_request *req = NULL;
1701 struct ptlrpc_bulk_desc *desc;
1702 struct idx_info *ii;
1709 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1710 npages /= PAGE_CACHE_SIZE;
1712 OBD_ALLOC(pages, npages * sizeof(*pages));
1716 it->ooi_pages = pages;
1717 it->ooi_total_npages = npages;
1718 for (i = 0; i < npages; i++) {
1719 pages[i] = alloc_page(GFP_IOFS);
1720 if (pages[i] == NULL)
1724 req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1729 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1731 ptlrpc_request_free(req);
1735 /* Let's allow this request during recovery, otherwise
1736 * if the remote target is also in recovery status,
1737 * it might cause deadlock */
1738 top_device = dev->ld_site->ls_top_dev;
1739 if (top_device->ld_obd->obd_recovering)
1740 req->rq_allow_replay = 1;
1742 req->rq_request_portal = OUT_PORTAL;
1743 ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1744 memset(ii, 0, sizeof(*ii));
1745 if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1746 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1747 * ost_index, 0] with LAST_ID FID, so it needs to replace
1748 * the FID with orphan FID here */
1749 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1750 ii->ii_fid.f_oid = osp->opd_index;
1751 ii->ii_fid.f_ver = 0;
1752 ii->ii_flags = II_FL_NOHASH;
1753 ii->ii_attrs = osp_dev2node(osp);
1755 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1756 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1758 ii->ii_attrs = it->ooi_attr;
1760 ii->ii_magic = IDX_INFO_MAGIC;
1761 ii->ii_count = npages * LU_PAGE_COUNT;
1762 ii->ii_hash_start = it->ooi_next;
1764 ptlrpc_at_set_req_timeout(req);
1766 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1767 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1769 &ptlrpc_bulk_kiov_pin_ops);
1771 ptlrpc_request_free(req);
1775 for (i = 0; i < npages; i++)
1776 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1779 ptlrpc_request_set_replen(req);
1780 rc = ptlrpc_queue_wait(req);
1784 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1785 req->rq_bulk->bd_nob_transferred);
1790 ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1791 if (ii->ii_magic != IDX_INFO_MAGIC)
1792 GOTO(out, rc = -EPROTO);
1794 npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1795 (PAGE_CACHE_SHIFT - LU_PAGE_SHIFT);
1796 if (npages > it->ooi_total_npages) {
1797 CERROR("%s: returned more pages than expected, %u > %u\n",
1798 osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1799 GOTO(out, rc = -EINVAL);
1802 it->ooi_valid_npages = npages;
1803 if (ptlrpc_rep_need_swab(req))
1806 it->ooi_next = ii->ii_hash_end;
1809 ptlrpc_req_finished(req);
1815 * Move the iteration cursor to the next lu_page.
1817 * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1818 * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1819 * in current system page, then move the iteration cursor to the next
1820 * lu_page in current system page. Otherwise, if there are more system
1821 * pages in the cache, then move the iteration cursor to the next system
1822 * page. If all the cached records (pages) have been iterated, then fetch
1823 * more records via osp_it_fetch().
1825 * \param[in] env pointer to the thread context
1826 * \param[in] di pointer to the iteration structure
1828 * \retval positive for end of the directory
1829 * \retval 0 for success
1830 * \retval negative error number on failure
1832 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1834 struct osp_it *it = (struct osp_it *)di;
1835 struct lu_idxpage *idxpage;
1836 struct page **pages;
1842 idxpage = it->ooi_cur_idxpage;
1843 if (idxpage != NULL) {
1844 if (idxpage->lip_nr == 0)
1847 if (it->ooi_pos_ent < idxpage->lip_nr) {
1848 CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1849 (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1852 it->ooi_cur_idxpage = NULL;
1853 it->ooi_pos_lu_page++;
1856 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1857 it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1858 LU_PAGE_SIZE * it->ooi_pos_lu_page;
1860 lustre_swab_lip_header(it->ooi_cur_idxpage);
1861 if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1862 struct osp_device *osp =
1863 lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1865 CERROR("%s: invalid magic (%x != %x) for page "
1866 "%d/%d while read layout orphan index\n",
1867 osp->opd_obd->obd_name,
1868 it->ooi_cur_idxpage->lip_magic,
1869 LIP_MAGIC, it->ooi_pos_page,
1870 it->ooi_pos_lu_page);
1871 /* Skip this lu_page next time. */
1872 it->ooi_pos_ent = idxpage->lip_nr - 1;
1875 it->ooi_pos_ent = -1;
1879 kunmap(it->ooi_cur_page);
1880 it->ooi_cur_page = NULL;
1884 pages = it->ooi_pages;
1885 if (it->ooi_pos_page < it->ooi_valid_npages) {
1886 it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1887 it->ooi_pos_lu_page = 0;
1891 for (i = 0; i < it->ooi_total_npages; i++) {
1892 if (pages[i] != NULL)
1893 __free_page(pages[i]);
1895 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1897 it->ooi_pos_page = 0;
1898 it->ooi_total_npages = 0;
1899 it->ooi_valid_npages = 0;
1902 it->ooi_cur_page = NULL;
1903 it->ooi_cur_idxpage = NULL;
1904 it->ooi_pages = NULL;
1907 if (it->ooi_next == II_END_OFF)
1910 rc = osp_it_fetch(env, it);
1918 * Move the iteration cursor to the next record.
1920 * If there are more records in the lu_page, then move the iteration
1921 * cursor to the next record directly. Otherwise, move the iteration
1922 * cursor to the record in the next lu_page via osp_it_next_page()
1924 * \param[in] env pointer to the thread context
1925 * \param[in] di pointer to the iteration structure
1927 * \retval positive for end of the directory
1928 * \retval 0 for success
1929 * \retval negative error number on failure
1931 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1933 struct osp_it *it = (struct osp_it *)di;
1934 struct lu_idxpage *idxpage;
1939 idxpage = it->ooi_cur_idxpage;
1940 if (idxpage != NULL) {
1941 if (idxpage->lip_nr == 0)
1945 if (it->ooi_pos_ent < idxpage->lip_nr) {
1947 (struct lu_orphan_ent *)idxpage->lip_entries +
1950 lustre_swab_orphan_ent(it->ooi_ent);
1955 rc = osp_it_next_page(env, di);
1962 int osp_it_get(const struct lu_env *env, struct dt_it *di,
1963 const struct dt_key *key)
1968 void osp_it_put(const struct lu_env *env, struct dt_it *di)
1972 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
1973 const struct dt_it *di)
1975 struct osp_it *it = (struct osp_it *)di;
1976 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1978 if (likely(ent != NULL))
1979 return (struct dt_key *)(&ent->loe_key);
1984 static int osp_orphan_it_key_size(const struct lu_env *env,
1985 const struct dt_it *di)
1987 return sizeof(struct lu_fid);
1990 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
1991 struct dt_rec *rec, __u32 attr)
1993 struct osp_it *it = (struct osp_it *)di;
1994 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1996 if (likely(ent != NULL)) {
1997 *(struct lu_orphan_rec *)rec = ent->loe_rec;
2004 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
2006 struct osp_it *it = (struct osp_it *)di;
2008 return it->ooi_next;
2012 * Locate the iteration cursor to the specified position (cookie).
2014 * \param[in] env pointer to the thread context
2015 * \param[in] di pointer to the iteration structure
2016 * \param[in] hash the specified position
2018 * \retval positive number for locating to the exactly position
2020 * \retval 0 for arriving at the end of the iteration
2021 * \retval negative error number on failure
2023 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2026 struct osp_it *it = (struct osp_it *)di;
2029 it->ooi_next = hash;
2030 rc = osp_orphan_it_next(env, (struct dt_it *)di);
2040 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2046 static const struct dt_index_operations osp_orphan_index_ops = {
2047 .dio_lookup = osp_orphan_index_lookup,
2048 .dio_declare_insert = osp_orphan_index_declare_insert,
2049 .dio_insert = osp_orphan_index_insert,
2050 .dio_declare_delete = osp_orphan_index_declare_delete,
2051 .dio_delete = osp_orphan_index_delete,
2053 .init = osp_it_init,
2054 .fini = osp_it_fini,
2055 .next = osp_orphan_it_next,
2058 .key = osp_orphan_it_key,
2059 .key_size = osp_orphan_it_key_size,
2060 .rec = osp_orphan_it_rec,
2061 .store = osp_it_store,
2062 .load = osp_orphan_it_load,
2063 .key_rec = osp_it_key_rec,
2068 * Implement OSP layer dt_object_operations::do_index_try() interface.
2070 * Negotiate the index type.
2072 * If the target index is an IDIF object, then use osp_orphan_index_ops.
2073 * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2074 * (\see lustre/include/lustre_fid.h for IDIF.)
2076 * \param[in] env pointer to the thread context
2077 * \param[in] dt pointer to the OSP layer dt_object
2078 * \param[in] feat unused
2080 * \retval 0 for success
2082 static int osp_index_try(const struct lu_env *env,
2083 struct dt_object *dt,
2084 const struct dt_index_features *feat)
2086 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2088 if (fid_is_last_id(fid) && fid_is_idif(fid))
2089 dt->do_index_ops = &osp_orphan_index_ops;
2091 dt->do_index_ops = &osp_md_index_ops;
2095 static struct dt_object_operations osp_obj_ops = {
2096 .do_declare_attr_get = osp_declare_attr_get,
2097 .do_attr_get = osp_attr_get,
2098 .do_declare_attr_set = osp_declare_attr_set,
2099 .do_attr_set = osp_attr_set,
2100 .do_declare_xattr_get = osp_declare_xattr_get,
2101 .do_xattr_get = osp_xattr_get,
2102 .do_declare_xattr_set = osp_declare_xattr_set,
2103 .do_xattr_set = osp_xattr_set,
2104 .do_declare_create = osp_declare_object_create,
2105 .do_create = osp_object_create,
2106 .do_declare_destroy = osp_declare_object_destroy,
2107 .do_destroy = osp_object_destroy,
2108 .do_index_try = osp_index_try,
2112 * Implement OSP layer lu_object_operations::loo_object_init() interface.
2114 * Initialize the object.
2116 * If it is a remote MDT object, then call do_attr_get() to fetch
2117 * the attribute from the peer.
2119 * \param[in] env pointer to the thread context
2120 * \param[in] o pointer to the OSP layer lu_object
2121 * \param[in] conf unused
2123 * \retval 0 for success
2124 * \retval negative error number on failure
2126 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2127 const struct lu_object_conf *conf)
2129 struct osp_object *po = lu2osp_obj(o);
2133 spin_lock_init(&po->opo_lock);
2134 o->lo_header->loh_attr |= LOHA_REMOTE;
2136 if (is_ost_obj(o)) {
2137 po->opo_obj.do_ops = &osp_obj_ops;
2139 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2141 po->opo_obj.do_ops = &osp_md_obj_ops;
2142 po->opo_obj.do_body_ops = &osp_md_body_ops;
2143 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
2144 po->opo_non_exist = 1;
2146 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2149 o->lo_header->loh_attr |=
2150 LOHA_EXISTS | (la->la_mode & S_IFMT);
2151 if (rc == -ENOENT) {
2152 po->opo_non_exist = 1;
2156 init_rwsem(&po->opo_sem);
2162 * Implement OSP layer lu_object_operations::loo_object_free() interface.
2164 * Finalize the object.
2166 * If the OSP object has attributes cache, then destroy the cache.
2167 * Free the object finally.
2169 * \param[in] env pointer to the thread context
2170 * \param[in] o pointer to the OSP layer lu_object
2172 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2174 struct osp_object *obj = lu2osp_obj(o);
2175 struct lu_object_header *h = o->lo_header;
2177 dt_object_fini(&obj->opo_obj);
2178 lu_object_header_fini(h);
2179 if (obj->opo_ooa != NULL) {
2180 struct osp_xattr_entry *oxe;
2181 struct osp_xattr_entry *tmp;
2184 list_for_each_entry_safe(oxe, tmp,
2185 &obj->opo_ooa->ooa_xattr_list,
2187 list_del(&oxe->oxe_list);
2188 count = atomic_read(&oxe->oxe_ref);
2189 LASSERTF(count == 1,
2190 "Still has %d users on the xattr entry %.*s\n",
2191 count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2193 OBD_FREE(oxe, oxe->oxe_buflen);
2195 OBD_FREE_PTR(obj->opo_ooa);
2197 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2201 * Implement OSP layer lu_object_operations::loo_object_release() interface.
2203 * Cleanup (not free) the object.
2205 * If it is a reserved object but failed to be created, or it is an OST
2206 * object, then mark the object as non-cached.
2208 * \param[in] env pointer to the thread context
2209 * \param[in] o pointer to the OSP layer lu_object
2211 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2213 struct osp_object *po = lu2osp_obj(o);
2214 struct osp_device *d = lu2osp_dev(o->lo_dev);
2219 * release reservation if object was declared but not created
2220 * this may require lu_object_put() in LOD
2222 if (unlikely(po->opo_reserved)) {
2223 LASSERT(d->opd_pre != NULL);
2224 LASSERT(d->opd_pre_reserved > 0);
2225 spin_lock(&d->opd_pre_lock);
2226 d->opd_pre_reserved--;
2227 spin_unlock(&d->opd_pre_lock);
2229 /* not needed in cache any more */
2230 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2234 /* XXX: Currently, NOT cache OST-object on MDT because:
2235 * 1. it is not often accessed on MDT.
2236 * 2. avoid up layer (such as LFSCK) to load too many
2237 * once-used OST-objects. */
2238 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2243 static int osp_object_print(const struct lu_env *env, void *cookie,
2244 lu_printer_t p, const struct lu_object *l)
2246 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2248 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2251 static int osp_object_invariant(const struct lu_object *o)
2256 struct lu_object_operations osp_lu_obj_ops = {
2257 .loo_object_init = osp_object_init,
2258 .loo_object_free = osp_object_free,
2259 .loo_object_release = osp_object_release,
2260 .loo_object_print = osp_object_print,
2261 .loo_object_invariant = osp_object_invariant