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) 2014, 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 "osp_internal.h"
129 static inline __u32 osp_dev2node(struct osp_device *osp)
131 return osp->opd_storage->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
134 static inline bool is_ost_obj(struct lu_object *lo)
136 return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
140 * Assign FID to the OST object.
142 * This function will assign the FID to the OST object of a striped file.
144 * \param[in] env pointer to the thread context
145 * \param[in] d pointer to the OSP device
146 * \param[in] o pointer to the OSP object that the FID will be
149 static void osp_object_assign_fid(const struct lu_env *env,
150 struct osp_device *d, struct osp_object *o)
152 struct osp_thread_info *osi = osp_env_info(env);
154 LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
155 LASSERT(o->opo_reserved);
158 osp_precreate_get_fid(env, d, &osi->osi_fid);
160 lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
164 * Initialize the OSP object attributes cache.
166 * \param[in] obj pointer to the OSP object
168 * \retval 0 for success
169 * \retval negative error number on failure
171 static int osp_oac_init(struct osp_object *obj)
173 struct osp_object_attr *ooa;
179 INIT_LIST_HEAD(&ooa->ooa_xattr_list);
180 spin_lock(&obj->opo_lock);
181 if (likely(obj->opo_ooa == NULL)) {
183 spin_unlock(&obj->opo_lock);
185 spin_unlock(&obj->opo_lock);
193 * Find the named extended attribute in the OSP object attributes cache.
195 * The caller should take the osp_object::opo_lock before calling
198 * \param[in] ooa pointer to the OSP object attributes cache
199 * \param[in] name the name of the extended attribute
200 * \param[in] namelen the name length of the extended attribute
202 * \retval pointer to the found extended attribute entry
203 * \retval NULL if the specified extended attribute is not
206 static struct osp_xattr_entry *
207 osp_oac_xattr_find_locked(struct osp_object_attr *ooa,
208 const char *name, size_t namelen)
210 struct osp_xattr_entry *oxe;
212 list_for_each_entry(oxe, &ooa->ooa_xattr_list, oxe_list) {
213 if (namelen == oxe->oxe_namelen &&
214 strncmp(name, oxe->oxe_buf, namelen) == 0)
222 * Find the named extended attribute in the OSP object attributes cache.
224 * Call osp_oac_xattr_find_locked() with the osp_object::opo_lock held.
226 * \param[in] obj pointer to the OSP object
227 * \param[in] name the name of the extended attribute
228 * \param[in] unlink true if the extended attribute entry is to be removed
231 * \retval pointer to the found extended attribute entry
232 * \retval NULL if the specified extended attribute is not
235 static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
236 const char *name, bool unlink)
238 struct osp_xattr_entry *oxe = NULL;
240 spin_lock(&obj->opo_lock);
241 if (obj->opo_ooa != NULL) {
242 oxe = osp_oac_xattr_find_locked(obj->opo_ooa, name,
246 list_del_init(&oxe->oxe_list);
248 atomic_inc(&oxe->oxe_ref);
251 spin_unlock(&obj->opo_lock);
257 * Find the named extended attribute in the OSP object attributes cache.
259 * If it is not in the cache, then add an empty entry (that will be
260 * filled later) to cache with the given name.
262 * \param[in] obj pointer to the OSP object
263 * \param[in] name the name of the extended attribute
264 * \param[in] len the length of the extended attribute value
266 * \retval pointer to the found or new-created extended
268 * \retval NULL if the specified extended attribute is not in the
269 * cache or fail to add new empty entry to the cache.
271 static struct osp_xattr_entry *
272 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, size_t len)
274 struct osp_object_attr *ooa = obj->opo_ooa;
275 struct osp_xattr_entry *oxe;
276 struct osp_xattr_entry *tmp = NULL;
277 size_t namelen = strlen(name);
278 size_t size = sizeof(*oxe) + namelen + 1 + len;
280 LASSERT(ooa != NULL);
282 oxe = osp_oac_xattr_find(obj, name, false);
286 OBD_ALLOC(oxe, size);
287 if (unlikely(oxe == NULL))
290 INIT_LIST_HEAD(&oxe->oxe_list);
291 oxe->oxe_buflen = size;
292 oxe->oxe_namelen = namelen;
293 memcpy(oxe->oxe_buf, name, namelen);
294 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
295 /* One ref is for the caller, the other is for the entry on the list. */
296 atomic_set(&oxe->oxe_ref, 2);
298 spin_lock(&obj->opo_lock);
299 tmp = osp_oac_xattr_find_locked(ooa, name, namelen);
301 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
303 atomic_inc(&tmp->oxe_ref);
304 spin_unlock(&obj->opo_lock);
315 * Add the given extended attribute to the OSP object attributes cache.
317 * If there is an old extended attributed entry with the same name,
318 * remove it from the cache and return it via the parameter \a poxe.
320 * \param[in] obj pointer to the OSP object
321 * \param[in,out] poxe double pointer to the OSP object extended attribute
322 * entry: the new extended attribute entry is transfered
323 * via such pointer target, and if old the extended
324 * attribute entry exists, then it will be returned back
325 * via such pointer target.
326 * \param[in] len the length of the (new) extended attribute value
328 * \retval pointer to the new extended attribute entry
329 * \retval NULL for failure cases.
331 static struct osp_xattr_entry *
332 osp_oac_xattr_replace(struct osp_object *obj,
333 struct osp_xattr_entry **poxe, size_t len)
335 struct osp_object_attr *ooa = obj->opo_ooa;
336 struct osp_xattr_entry *oxe;
337 size_t namelen = (*poxe)->oxe_namelen;
338 size_t size = sizeof(*oxe) + namelen + 1 + len;
340 LASSERT(ooa != NULL);
342 OBD_ALLOC(oxe, size);
343 if (unlikely(oxe == NULL))
346 INIT_LIST_HEAD(&oxe->oxe_list);
347 oxe->oxe_buflen = size;
348 oxe->oxe_namelen = namelen;
349 memcpy(oxe->oxe_buf, (*poxe)->oxe_buf, namelen);
350 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
351 /* One ref is for the caller, the other is for the entry on the list. */
352 atomic_set(&oxe->oxe_ref, 2);
354 spin_lock(&obj->opo_lock);
355 *poxe = osp_oac_xattr_find_locked(ooa, oxe->oxe_buf, namelen);
356 LASSERT(*poxe != NULL);
358 list_del_init(&(*poxe)->oxe_list);
359 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
360 spin_unlock(&obj->opo_lock);
366 * Release reference from the OSP object extended attribute entry.
368 * If it is the last reference, then free the entry.
370 * \param[in] oxe pointer to the OSP object extended attribute entry.
372 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
374 if (atomic_dec_and_test(&oxe->oxe_ref)) {
375 LASSERT(list_empty(&oxe->oxe_list));
377 OBD_FREE(oxe, oxe->oxe_buflen);
382 * Parse the OSP object attribute from the RPC reply.
384 * If the attribute is valid, then it will be added to the OSP object
387 * \param[in] env pointer to the thread context
388 * \param[in] reply pointer to the RPC reply
389 * \param[in] req pointer to the RPC request
390 * \param[out] attr pointer to buffer to hold the output attribute
391 * \param[in] obj pointer to the OSP object
392 * \param[in] index the index of the attribute buffer in the reply
394 * \retval 0 for success
395 * \retval negative error number on failure
397 static int osp_get_attr_from_reply(const struct lu_env *env,
398 struct object_update_reply *reply,
399 struct ptlrpc_request *req,
400 struct lu_attr *attr,
401 struct osp_object *obj, int index)
403 struct osp_thread_info *osi = osp_env_info(env);
404 struct lu_buf *rbuf = &osi->osi_lb2;
405 struct obdo *lobdo = &osi->osi_obdo;
409 rc = object_update_result_data_get(reply, rbuf, index);
413 wobdo = rbuf->lb_buf;
414 if (rbuf->lb_len != sizeof(*wobdo))
417 LASSERT(req != NULL);
418 if (ptlrpc_req_need_swab(req))
419 lustre_swab_obdo(wobdo);
421 lustre_get_wire_obdo(NULL, lobdo, wobdo);
422 spin_lock(&obj->opo_lock);
423 if (obj->opo_ooa != NULL) {
424 la_from_obdo(&obj->opo_ooa->ooa_attr, lobdo, lobdo->o_valid);
426 *attr = obj->opo_ooa->ooa_attr;
428 LASSERT(attr != NULL);
430 la_from_obdo(attr, lobdo, lobdo->o_valid);
432 spin_unlock(&obj->opo_lock);
438 * Interpreter function for getting OSP object attribute asynchronously.
440 * Called to interpret the result of an async mode RPC for getting the
441 * OSP object attribute.
443 * \param[in] env pointer to the thread context
444 * \param[in] reply pointer to the RPC reply
445 * \param[in] req pointer to the RPC request
446 * \param[in] obj pointer to the OSP object
447 * \param[out] data pointer to buffer to hold the output attribute
448 * \param[in] index the index of the attribute buffer in the reply
449 * \param[in] rc the result for handling the RPC
451 * \retval 0 for success
452 * \retval negative error number on failure
454 static int osp_attr_get_interpterer(const struct lu_env *env,
455 struct object_update_reply *reply,
456 struct ptlrpc_request *req,
457 struct osp_object *obj,
458 void *data, int index, int rc)
460 struct lu_attr *attr = data;
462 LASSERT(obj->opo_ooa != NULL);
465 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
466 obj->opo_non_exist = 0;
468 return osp_get_attr_from_reply(env, reply, req, NULL, obj,
472 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
473 obj->opo_non_exist = 1;
476 spin_lock(&obj->opo_lock);
478 spin_unlock(&obj->opo_lock);
485 * Implement OSP layer dt_object_operations::do_declare_attr_get() interface.
487 * Declare that the caller will get attribute from the specified OST object.
489 * This function adds an Object Unified Target (OUT) sub-request to the per-OSP
490 * based shared asynchronous request queue. The osp_attr_get_interpterer()
491 * is registered as the interpreter function to handle the result of this
494 * \param[in] env pointer to the thread context
495 * \param[in] dt pointer to the OSP layer dt_object
497 * \retval 0 for success
498 * \retval negative error number on failure
500 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt)
502 struct osp_object *obj = dt2osp_obj(dt);
503 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
506 if (obj->opo_ooa == NULL) {
507 rc = osp_oac_init(obj);
512 mutex_lock(&osp->opd_async_requests_mutex);
513 rc = osp_insert_async_request(env, OUT_ATTR_GET, obj, 0, NULL, NULL,
514 &obj->opo_ooa->ooa_attr,
515 osp_attr_get_interpterer);
516 mutex_unlock(&osp->opd_async_requests_mutex);
522 * Implement OSP layer dt_object_operations::do_attr_get() interface.
524 * Get attribute from the specified MDT/OST object.
526 * If the attribute is in the OSP object attributes cache, then return
527 * the cached attribute directly. Otherwise it will trigger an OUT RPC
528 * to the peer to get the attribute synchronously, if successful, add it
529 * to the OSP attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
531 * \param[in] env pointer to the thread context
532 * \param[in] dt pointer to the OSP layer dt_object
533 * \param[out] attr pointer to the buffer to hold the output attribute
535 * \retval 0 for success
536 * \retval negative error number on failure
538 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
539 struct lu_attr *attr)
541 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
542 struct osp_object *obj = dt2osp_obj(dt);
543 struct dt_device *dev = &osp->opd_dt_dev;
544 struct dt_update_request *update;
545 struct object_update_reply *reply;
546 struct ptlrpc_request *req = NULL;
550 if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
553 if (obj->opo_ooa != NULL) {
554 spin_lock(&obj->opo_lock);
555 if (obj->opo_ooa->ooa_attr.la_valid != 0) {
556 *attr = obj->opo_ooa->ooa_attr;
557 spin_unlock(&obj->opo_lock);
561 spin_unlock(&obj->opo_lock);
564 update = dt_update_request_create(dev);
566 RETURN(PTR_ERR(update));
568 rc = out_attr_get_pack(env, &update->dur_buf,
569 lu_object_fid(&dt->do_lu));
571 CERROR("%s: Insert update error "DFID": rc = %d\n",
572 dev->dd_lu_dev.ld_obd->obd_name,
573 PFID(lu_object_fid(&dt->do_lu)), rc);
578 rc = osp_remote_sync(env, osp, update, &req, false);
581 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
582 obj->opo_non_exist = 1;
584 CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
585 dev->dd_lu_dev.ld_obd->obd_name,
586 PFID(lu_object_fid(&dt->do_lu)), rc);
592 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
593 obj->opo_non_exist = 0;
594 reply = req_capsule_server_sized_get(&req->rq_pill,
595 &RMF_OUT_UPDATE_REPLY,
596 OUT_UPDATE_REPLY_SIZE);
597 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
598 GOTO(out, rc = -EPROTO);
600 rc = osp_get_attr_from_reply(env, reply, req, attr, obj, 0);
608 ptlrpc_req_finished(req);
610 dt_update_request_destroy(update);
615 static int __osp_attr_set(const struct lu_env *env, struct dt_object *dt,
616 const struct lu_attr *attr, struct thandle *th)
618 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
619 struct osp_object *o = dt2osp_obj(dt);
625 * Usually we don't allow server stack to manipulate size
626 * but there is a special case when striping is created
627 * late, after stripeless file got truncated to non-zero.
629 * In this case we do the following:
631 * 1) grab id in declare - this can lead to leaked OST objects
632 * but we don't currently have proper mechanism and the only
633 * options we have are to do truncate RPC holding transaction
634 * open (very bad) or to grab id in declare at cost of leaked
635 * OST object in same very rare unfortunate case (just bad)
636 * notice 1.6-2.0 do assignment outside of running transaction
637 * all the time, meaning many more chances for leaked objects.
639 * 2) send synchronous truncate RPC with just assigned id
642 /* there are few places in MDD code still passing NULL
643 * XXX: to be fixed soon */
647 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
648 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
649 LASSERT(!dt_object_exists(dt));
650 osp_object_assign_fid(env, d, o);
651 rc = osp_object_truncate(env, dt, attr->la_size);
656 if (!(attr->la_valid & (LA_UID | LA_GID)))
659 if (!is_only_remote_trans(th)) {
661 * track all UID/GID changes via llog
663 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
665 /* It is for OST-object attr_set directly without updating
666 * local MDT-object attribute. It is usually used by LFSCK. */
667 rc = __osp_md_attr_set(env, dt, attr, th);
670 if (rc != 0 || o->opo_ooa == NULL)
673 /* Update the OSP object attributes cache. */
674 la = &o->opo_ooa->ooa_attr;
675 spin_lock(&o->opo_lock);
676 if (attr->la_valid & LA_UID) {
677 la->la_uid = attr->la_uid;
678 la->la_valid |= LA_UID;
681 if (attr->la_valid & LA_GID) {
682 la->la_gid = attr->la_gid;
683 la->la_valid |= LA_GID;
685 spin_unlock(&o->opo_lock);
691 * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
693 * If the transaction is not remote one, then declare the credits that will
694 * be used for the subsequent llog record for the object's attributes.
696 * \param[in] env pointer to the thread context
697 * \param[in] dt pointer to the OSP layer dt_object
698 * \param[in] attr pointer to the attribute to be set
699 * \param[in] th pointer to the transaction handler
701 * \retval 0 for success
702 * \retval negative error number on failure
704 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
705 const struct lu_attr *attr, struct thandle *th)
709 if (!is_only_remote_trans(th)) {
710 rc = __osp_attr_set(env, dt, attr, th);
712 CDEBUG(D_INFO, "declare set attr "DFID": rc = %d\n",
713 PFID(&dt->do_lu.lo_header->loh_fid), rc);
720 * Implement OSP layer dt_object_operations::do_attr_set() interface.
722 * Set attribute to the specified OST object.
724 * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
725 * in the OUT RPC that will be flushed when the remote transaction stop.
726 * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
727 * will be handled by a dedicated thread asynchronously.
729 * If the attribute entry exists in the OSP object attributes cache,
730 * then update the cached attribute according to given attribute.
732 * \param[in] env pointer to the thread context
733 * \param[in] dt pointer to the OSP layer dt_object
734 * \param[in] attr pointer to the attribute to be set
735 * \param[in] th pointer to the transaction handler
737 * \retval 0 for success
738 * \retval negative error number on failure
740 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
741 const struct lu_attr *attr, struct thandle *th)
743 struct osp_object *o = dt2osp_obj(dt);
747 if (is_only_remote_trans(th)) {
748 rc = __osp_attr_set(env, dt, attr, th);
750 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
751 PFID(&dt->do_lu.lo_header->loh_fid), rc);
756 /* we're interested in uid/gid changes only */
757 if (!(attr->la_valid & (LA_UID | LA_GID)))
760 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
761 /* XXX: send new uid/gid to OST ASAP? */
763 CDEBUG(D_INFO, "(2) set attr "DFID": rc = %d\n",
764 PFID(&dt->do_lu.lo_header->loh_fid), rc);
770 * Interpreter function for getting OSP object extended attribute asynchronously
772 * Called to interpret the result of an async mode RPC for getting the
773 * OSP object extended attribute.
775 * \param[in] env pointer to the thread context
776 * \param[in] reply pointer to the RPC reply
777 * \param[in] req pointer to the RPC request
778 * \param[in] obj pointer to the OSP object
779 * \param[out] data pointer to OSP object attributes cache
780 * \param[in] index the index of the attribute buffer in the reply
781 * \param[in] rc the result for handling the RPC
783 * \retval 0 for success
784 * \retval negative error number on failure
786 static int osp_xattr_get_interpterer(const struct lu_env *env,
787 struct object_update_reply *reply,
788 struct ptlrpc_request *req,
789 struct osp_object *obj,
790 void *data, int index, int rc)
792 struct osp_object_attr *ooa = obj->opo_ooa;
793 struct osp_xattr_entry *oxe = data;
794 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
796 LASSERT(ooa != NULL);
799 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
801 rc = object_update_result_data_get(reply, rbuf, index);
802 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
803 spin_lock(&obj->opo_lock);
805 spin_unlock(&obj->opo_lock);
806 osp_oac_xattr_put(oxe);
808 return rc < 0 ? rc : -ERANGE;
811 spin_lock(&obj->opo_lock);
812 oxe->oxe_vallen = rbuf->lb_len;
813 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
816 spin_unlock(&obj->opo_lock);
817 } else if (rc == -ENOENT || rc == -ENODATA) {
818 spin_lock(&obj->opo_lock);
821 spin_unlock(&obj->opo_lock);
823 spin_lock(&obj->opo_lock);
825 spin_unlock(&obj->opo_lock);
828 osp_oac_xattr_put(oxe);
834 * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
836 * Declare that the caller will get extended attribute from the specified
839 * This function will add an OUT_XATTR_GET sub-request to the per OSP
840 * based shared asynchronous request queue with the interpreter function:
841 * osp_xattr_get_interpterer().
843 * \param[in] env pointer to the thread context
844 * \param[in] dt pointer to the OSP layer dt_object
845 * \param[out] buf pointer to the lu_buf to hold the extended attribute
846 * \param[in] name the name for the expected extended attribute
848 * \retval 0 for success
849 * \retval negative error number on failure
851 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
852 struct lu_buf *buf, const char *name)
854 struct osp_object *obj = dt2osp_obj(dt);
855 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
856 struct osp_xattr_entry *oxe;
857 __u16 namelen = strlen(name);
860 LASSERT(buf != NULL);
861 LASSERT(name != NULL);
863 /* If only for xattr size, return directly. */
864 if (unlikely(buf->lb_len == 0))
867 if (obj->opo_ooa == NULL) {
868 rc = osp_oac_init(obj);
873 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
877 mutex_lock(&osp->opd_async_requests_mutex);
878 rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
879 &namelen, (const void **)&name, oxe,
880 osp_xattr_get_interpterer);
882 mutex_unlock(&osp->opd_async_requests_mutex);
883 osp_oac_xattr_put(oxe);
885 struct dt_update_request *update;
887 /* XXX: Currently, we trigger the batched async OUT
888 * RPC via dt_declare_xattr_get(). It is not
889 * perfect solution, but works well now.
891 * We will improve it in the future. */
892 update = osp->opd_async_requests;
893 if (update != NULL && update->dur_buf.ub_req != NULL &&
894 update->dur_buf.ub_req->ourq_count > 0) {
895 osp->opd_async_requests = NULL;
896 mutex_unlock(&osp->opd_async_requests_mutex);
897 rc = osp_unplug_async_request(env, osp, update);
899 mutex_unlock(&osp->opd_async_requests_mutex);
907 * Implement OSP layer dt_object_operations::do_xattr_get() interface.
909 * Get extended attribute from the specified MDT/OST object.
911 * If the extended attribute is in the OSP object attributes cache, then
912 * return the cached extended attribute directly. Otherwise it will get
913 * the extended attribute synchronously, if successful, add it to the OSP
914 * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
916 * There is a race condition: some other thread has added the named extended
917 * attributed entry to the OSP object attributes cache during the current
918 * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
919 * (just) existing extended attribute entry with the new replied one.
921 * \param[in] env pointer to the thread context
922 * \param[in] dt pointer to the OSP layer dt_object
923 * \param[out] buf pointer to the lu_buf to hold the extended attribute
924 * \param[in] name the name for the expected extended attribute
926 * \retval 0 for success
927 * \retval negative error number on failure
929 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
930 struct lu_buf *buf, const char *name)
932 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
933 struct osp_object *obj = dt2osp_obj(dt);
934 struct dt_device *dev = &osp->opd_dt_dev;
935 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
936 struct dt_update_request *update = NULL;
937 struct ptlrpc_request *req = NULL;
938 struct object_update_reply *reply;
939 struct osp_xattr_entry *oxe = NULL;
940 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
944 LASSERT(buf != NULL);
945 LASSERT(name != NULL);
947 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
948 osp->opd_index == cfs_fail_val) {
949 if (is_ost_obj(&dt->do_lu)) {
950 if (osp_dev2node(osp) == cfs_fail_val)
953 if (strcmp(name, XATTR_NAME_LINK) == 0)
958 if (unlikely(obj->opo_non_exist))
961 oxe = osp_oac_xattr_find(obj, name, false);
963 spin_lock(&obj->opo_lock);
964 if (oxe->oxe_ready) {
966 GOTO(unlock, rc = -ENODATA);
968 if (buf->lb_buf == NULL)
969 GOTO(unlock, rc = oxe->oxe_vallen);
971 if (buf->lb_len < oxe->oxe_vallen)
972 GOTO(unlock, rc = -ERANGE);
974 memcpy(buf->lb_buf, oxe->oxe_value, oxe->oxe_vallen);
976 GOTO(unlock, rc = oxe->oxe_vallen);
979 spin_unlock(&obj->opo_lock);
980 osp_oac_xattr_put(oxe);
984 spin_unlock(&obj->opo_lock);
987 update = dt_update_request_create(dev);
989 GOTO(out, rc = PTR_ERR(update));
991 rc = out_xattr_get_pack(env, &update->dur_buf,
992 lu_object_fid(&dt->do_lu), name);
994 CERROR("%s: Insert update error "DFID": rc = %d\n",
995 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
999 rc = osp_remote_sync(env, osp, update, &req, false);
1001 if (rc == -ENOENT) {
1002 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
1003 obj->opo_non_exist = 1;
1006 if (obj->opo_ooa == NULL)
1010 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
1013 CWARN("%s: Fail to add xattr (%s) to cache for "
1014 DFID" (1): rc = %d\n", dname, name,
1015 PFID(lu_object_fid(&dt->do_lu)), rc);
1020 spin_lock(&obj->opo_lock);
1021 if (rc == -ENOENT || rc == -ENODATA) {
1027 spin_unlock(&obj->opo_lock);
1032 reply = req_capsule_server_sized_get(&req->rq_pill,
1033 &RMF_OUT_UPDATE_REPLY,
1034 OUT_UPDATE_REPLY_SIZE);
1035 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1036 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1037 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1038 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1040 GOTO(out, rc = -EPROTO);
1043 rc = object_update_result_data_get(reply, rbuf, 0);
1047 if (buf->lb_buf == NULL)
1048 GOTO(out, rc = rbuf->lb_len);
1050 if (unlikely(buf->lb_len < rbuf->lb_len))
1051 GOTO(out, rc = -ERANGE);
1053 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1055 if (obj->opo_ooa == NULL)
1059 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1061 CWARN("%s: Fail to add xattr (%s) to "
1062 "cache for "DFID" (2): rc = %d\n",
1063 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1069 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
1070 struct osp_xattr_entry *old = oxe;
1071 struct osp_xattr_entry *tmp;
1073 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
1074 osp_oac_xattr_put(oxe);
1077 CWARN("%s: Fail to update xattr (%s) to "
1078 "cache for "DFID": rc = %d\n",
1079 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1080 spin_lock(&obj->opo_lock);
1082 spin_unlock(&obj->opo_lock);
1087 /* Drop the ref for entry on list. */
1088 osp_oac_xattr_put(old);
1091 spin_lock(&obj->opo_lock);
1092 oxe->oxe_vallen = rbuf->lb_len;
1093 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
1096 spin_unlock(&obj->opo_lock);
1102 ptlrpc_req_finished(req);
1104 if (update != NULL && !IS_ERR(update))
1105 dt_update_request_destroy(update);
1108 osp_oac_xattr_put(oxe);
1113 static int __osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1114 const struct lu_buf *buf, const char *name,
1115 int flag, struct thandle *th)
1117 struct osp_object *o = dt2osp_obj(dt);
1118 struct dt_update_request *update;
1119 struct osp_xattr_entry *oxe;
1123 LASSERT(buf->lb_len > 0 && buf->lb_buf != NULL);
1124 update = thandle_to_dt_update_request(th);
1125 LASSERT(update != NULL);
1127 rc = out_xattr_set_pack(env, &update->dur_buf,
1128 lu_object_fid(&dt->do_lu),
1129 buf, name, flag, update->dur_batchid);
1130 if (rc != 0 || o->opo_ooa == NULL)
1133 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1135 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1136 dt->do_lu.lo_dev->ld_obd->obd_name,
1137 name, PFID(lu_object_fid(&dt->do_lu)));
1142 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
1143 struct osp_xattr_entry *old = oxe;
1144 struct osp_xattr_entry *tmp;
1146 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
1147 osp_oac_xattr_put(oxe);
1150 CWARN("%s: cannot update cached xattr '%s' of "DFID"\n",
1151 dt->do_lu.lo_dev->ld_obd->obd_name,
1152 name, PFID(lu_object_fid(&dt->do_lu)));
1153 spin_lock(&o->opo_lock);
1155 spin_unlock(&o->opo_lock);
1160 /* Drop the ref for entry on list. */
1161 osp_oac_xattr_put(old);
1164 spin_lock(&o->opo_lock);
1165 oxe->oxe_vallen = buf->lb_len;
1166 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
1169 spin_unlock(&o->opo_lock);
1170 osp_oac_xattr_put(oxe);
1176 * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1178 * Declare that the caller will set extended attribute to the specified
1181 * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1182 * to the OUT RPC that will be flushed when the transaction start. And if the
1183 * OSP attributes cache is initialized, then check whether the name extended
1184 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1185 * add the extended attribute to the cache.
1187 * \param[in] env pointer to the thread context
1188 * \param[in] dt pointer to the OSP layer dt_object
1189 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1190 * \param[in] name the name of the extended attribute to be set
1191 * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
1192 * or LU_XATTR_REPLACE or others
1193 * \param[in] th pointer to the transaction handler
1195 * \retval 0 for success
1196 * \retval negative error number on failure
1198 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1199 const struct lu_buf *buf, const char *name,
1200 int flag, struct thandle *th)
1204 if (!is_only_remote_trans(th)) {
1205 rc = __osp_xattr_set(env, dt, buf, name, flag, th);
1207 CDEBUG(D_INFO, "declare xattr %s set object "DFID": rc = %d\n",
1208 name, PFID(&dt->do_lu.lo_header->loh_fid), rc);
1215 * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1217 * Set extended attribute to the specified MDT/OST object.
1219 * If it is remote transaction, it will add an OUT_XATTR_SET sub-request into
1220 * the OUT RPC that will be flushed when the transaction stop. And if the OSP
1221 * attributes cache is initialized, then check whether the name extended
1222 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1223 * add the extended attribute to the cache.
1225 * \param[in] env pointer to the thread context
1226 * \param[in] dt pointer to the OSP layer dt_object
1227 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1228 * \param[in] name the name of the extended attribute to be set
1229 * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
1230 * or LU_XATTR_REPLACE or others
1231 * \param[in] th pointer to the transaction handler
1233 * \retval 0 for success
1234 * \retval negative error number on failure
1236 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1237 const struct lu_buf *buf, const char *name, int fl,
1242 if (is_only_remote_trans(th)) {
1243 rc = __osp_xattr_set(env, dt, buf, name, fl, th);
1245 CDEBUG(D_INFO, "xattr %s set object "DFID": rc = %d\n",
1246 name, PFID(&dt->do_lu.lo_header->loh_fid), rc);
1252 static int __osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1253 const char *name, struct thandle *th)
1255 struct dt_update_request *update;
1256 const struct lu_fid *fid;
1257 struct osp_object *o = dt2osp_obj(dt);
1258 struct osp_xattr_entry *oxe;
1261 update = thandle_to_dt_update_request(th);
1262 LASSERT(update != NULL);
1264 fid = lu_object_fid(&dt->do_lu);
1266 rc = out_xattr_del_pack(env, &update->dur_buf, fid, name,
1267 update->dur_batchid);
1269 if (rc != 0 || o->opo_ooa == NULL)
1272 oxe = osp_oac_xattr_find(o, name, true);
1274 /* Drop the ref for entry on list. */
1275 osp_oac_xattr_put(oxe);
1281 * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1283 * Declare that the caller will delete extended attribute on the specified
1286 * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1287 * to the OUT RPC that will be flushed when the transaction start. And if the
1288 * name extended attribute entry exists in the OSP attributes cache, then remove
1289 * it from the cache.
1291 * \param[in] env pointer to the thread context
1292 * \param[in] dt pointer to the OSP layer dt_object
1293 * \param[in] name the name of the extended attribute to be set
1294 * \param[in] th pointer to the transaction handler
1296 * \retval 0 for success
1297 * \retval negative error number on failure
1299 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1300 const char *name, struct thandle *th)
1304 if (!is_only_remote_trans(th)) {
1305 rc = __osp_xattr_del(env, dt, name, th);
1307 CDEBUG(D_INFO, "declare xattr %s del object "DFID": rc = %d\n",
1308 name, PFID(&dt->do_lu.lo_header->loh_fid), rc);
1315 * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1317 * Delete extended attribute on the specified MDT/OST object.
1319 * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1320 * the OUT RPC that will be flushed when the transaction stop. And if the name
1321 * extended attribute entry exists in the OSP attributes cache, then remove it
1324 * \param[in] env pointer to the thread context
1325 * \param[in] dt pointer to the OSP layer dt_object
1326 * \param[in] name the name of the extended attribute to be set
1327 * \param[in] th pointer to the transaction handler
1329 * \retval 0 for success
1330 * \retval negative error number on failure
1332 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1333 const char *name, struct thandle *th)
1337 if (is_only_remote_trans(th)) {
1338 rc = __osp_xattr_del(env, dt, name, th);
1340 CDEBUG(D_INFO, "xattr %s del object "DFID": rc = %d\n",
1341 name, PFID(&dt->do_lu.lo_header->loh_fid), rc);
1348 * Implement OSP layer dt_object_operations::do_declare_create() interface.
1350 * Declare that the caller will create the OST object.
1352 * If the transaction is a remote transaction and the FID for the OST-object
1353 * has been assigned already, then handle it as creating (remote) MDT object
1354 * via osp_md_declare_object_create(). This function is usually used for LFSCK
1355 * to re-create the lost OST object. Otherwise, if it is not replay case, the
1356 * OSP will reserve pre-created object for the subsequent create operation;
1357 * if the MDT side cached pre-created objects are less than some threshold,
1358 * then it will wakeup the pre-create thread.
1360 * \param[in] env pointer to the thread context
1361 * \param[in] dt pointer to the OSP layer dt_object
1362 * \param[in] attr the attribute for the object to be created
1363 * \param[in] hint pointer to the hint for creating the object, such as
1365 * \param[in] dof pointer to the dt_object_format for help the creation
1366 * \param[in] th pointer to the transaction handler
1368 * \retval 0 for success
1369 * \retval negative error number on failure
1371 static int osp_declare_object_create(const struct lu_env *env,
1372 struct dt_object *dt,
1373 struct lu_attr *attr,
1374 struct dt_allocation_hint *hint,
1375 struct dt_object_format *dof,
1378 struct osp_thread_info *osi = osp_env_info(env);
1379 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1380 struct osp_object *o = dt2osp_obj(dt);
1381 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1382 struct thandle *local_th;
1387 if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1388 LASSERT(fid_is_sane(fid));
1390 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
1395 /* should happen to non-0 OSP only so that at least one object
1396 * has been already declared in the scenario and LOD should
1398 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1401 LASSERT(d->opd_last_used_oid_file);
1404 * There can be gaps in precreated ids and record to unlink llog
1405 * XXX: we do not handle gaps yet, implemented before solution
1406 * was found to be racy, so we disabled that. there is no
1407 * point in making useless but expensive llog declaration.
1409 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1411 local_th = osp_get_storage_thandle(env, th, d);
1412 if (IS_ERR(local_th))
1413 RETURN(PTR_ERR(local_th));
1415 if (unlikely(!fid_is_zero(fid))) {
1416 /* replay case: caller knows fid */
1417 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1418 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1419 osi->osi_lb.lb_buf = NULL;
1421 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1422 &osi->osi_lb, osi->osi_off,
1428 * in declaration we need to reserve object so that we don't block
1429 * awaiting precreation RPC to complete
1431 rc = osp_precreate_reserve(env, d);
1433 * we also need to declare update to local "last used id" file for
1434 * recovery if object isn't used for a reason, we need to release
1435 * reservation, this can be made in osd_object_release()
1438 /* mark id is reserved: in create we don't want to talk
1440 LASSERT(o->opo_reserved == 0);
1441 o->opo_reserved = 1;
1443 /* common for all OSPs file hystorically */
1444 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1445 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1446 osi->osi_lb.lb_buf = NULL;
1447 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1448 &osi->osi_lb, osi->osi_off,
1451 /* not needed in the cache anymore */
1452 set_bit(LU_OBJECT_HEARD_BANSHEE,
1453 &dt->do_lu.lo_header->loh_flags);
1459 * Implement OSP layer dt_object_operations::do_create() interface.
1461 * Create the OST object.
1463 * If the transaction is a remote transaction and the FID for the OST-object
1464 * has been assigned already, then handle it as handling MDT object via the
1465 * osp_md_object_create(). For other cases, the OSP will assign FID to the
1466 * object to be created, and update last_used Object ID (OID) file.
1468 * \param[in] env pointer to the thread context
1469 * \param[in] dt pointer to the OSP layer dt_object
1470 * \param[in] attr the attribute for the object to be created
1471 * \param[in] hint pointer to the hint for creating the object, such as
1473 * \param[in] dof pointer to the dt_object_format for help the creation
1474 * \param[in] th pointer to the transaction handler
1476 * \retval 0 for success
1477 * \retval negative error number on failure
1479 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
1480 struct lu_attr *attr,
1481 struct dt_allocation_hint *hint,
1482 struct dt_object_format *dof, struct thandle *th)
1484 struct osp_thread_info *osi = osp_env_info(env);
1485 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1486 struct osp_object *o = dt2osp_obj(dt);
1488 struct lu_fid *fid = &osi->osi_fid;
1489 struct thandle *local_th;
1492 if (is_only_remote_trans(th) &&
1493 !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1494 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1496 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
1498 o->opo_non_exist = 0;
1503 o->opo_non_exist = 0;
1504 if (o->opo_reserved) {
1505 /* regular case, fid is assigned holding transaction open */
1506 osp_object_assign_fid(env, d, o);
1509 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1511 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1514 if (!o->opo_reserved) {
1515 /* special case, id was assigned outside of transaction
1516 * see comments in osp_declare_attr_set */
1517 LASSERT(d->opd_pre != NULL);
1518 spin_lock(&d->opd_pre_lock);
1519 osp_update_last_fid(d, fid);
1520 spin_unlock(&d->opd_pre_lock);
1523 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1525 /* If the precreate ends, it means it will be ready to rollover to
1526 * the new sequence soon, all the creation should be synchronized,
1527 * otherwise during replay, the replay fid will be inconsistent with
1528 * last_used/create fid */
1529 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1532 local_th = osp_get_storage_thandle(env, th, d);
1533 if (IS_ERR(local_th))
1534 RETURN(PTR_ERR(local_th));
1536 * it's OK if the import is inactive by this moment - id was created
1537 * by OST earlier, we just need to maintain it consistently on the disk
1538 * once import is reconnected, OSP will claim this and other objects
1539 * used and OST either keep them, if they exist or recreate
1542 /* we might have lost precreated objects */
1543 if (unlikely(d->opd_gap_count) > 0) {
1544 LASSERT(d->opd_pre != NULL);
1545 spin_lock(&d->opd_pre_lock);
1546 if (d->opd_gap_count > 0) {
1547 int count = d->opd_gap_count;
1549 ostid_set_id(&osi->osi_oi,
1550 fid_oid(&d->opd_gap_start_fid));
1551 d->opd_gap_count = 0;
1552 spin_unlock(&d->opd_pre_lock);
1554 CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1555 PFID(&d->opd_gap_start_fid), count);
1556 /* real gap handling is disabled intil ORI-692 will be
1557 * fixed, now we only report gaps */
1559 spin_unlock(&d->opd_pre_lock);
1563 /* Only need update last_used oid file, seq file will only be update
1564 * during seq rollover */
1565 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1566 &d->opd_last_used_fid.f_oid, d->opd_index);
1568 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1569 &osi->osi_off, local_th);
1571 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1572 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1578 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1580 * Declare that the caller will destroy the specified OST object.
1582 * The OST object destroy will be handled via llog asynchronously. This
1583 * function will declare the credits for generating MDS_UNLINK64_REC llog.
1585 * \param[in] env pointer to the thread context
1586 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1587 * \param[in] th pointer to the transaction handler
1589 * \retval 0 for success
1590 * \retval negative error number on failure
1592 int osp_declare_object_destroy(const struct lu_env *env,
1593 struct dt_object *dt, struct thandle *th)
1595 struct osp_object *o = dt2osp_obj(dt);
1601 * track objects to be destroyed via llog
1603 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1609 * Implement OSP layer dt_object_operations::do_destroy() interface.
1611 * Destroy the specified OST object.
1613 * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1614 * will be some dedicated thread to handle the llog asynchronously.
1616 * It also marks the object as non-cached.
1618 * \param[in] env pointer to the thread context
1619 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1620 * \param[in] th pointer to the transaction handler
1622 * \retval 0 for success
1623 * \retval negative error number on failure
1625 int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1628 struct osp_object *o = dt2osp_obj(dt);
1633 o->opo_non_exist = 1;
1635 * once transaction is committed put proper command on
1636 * the queue going to our OST
1638 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1640 /* not needed in cache any more */
1641 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1646 static int osp_orphan_index_lookup(const struct lu_env *env,
1647 struct dt_object *dt,
1649 const struct dt_key *key)
1654 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1655 struct dt_object *dt,
1656 const struct dt_rec *rec,
1657 const struct dt_key *key,
1658 struct thandle *handle)
1663 static int osp_orphan_index_insert(const struct lu_env *env,
1664 struct dt_object *dt,
1665 const struct dt_rec *rec,
1666 const struct dt_key *key,
1667 struct thandle *handle,
1673 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1674 struct dt_object *dt,
1675 const struct dt_key *key,
1676 struct thandle *handle)
1681 static int osp_orphan_index_delete(const struct lu_env *env,
1682 struct dt_object *dt,
1683 const struct dt_key *key,
1684 struct thandle *handle)
1690 * Initialize the OSP layer index iteration.
1692 * \param[in] env pointer to the thread context
1693 * \param[in] dt pointer to the index object to be iterated
1694 * \param[in] attr unused
1696 * \retval pointer to the iteration structure
1697 * \retval negative error number on failure
1699 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1706 return ERR_PTR(-ENOMEM);
1708 it->ooi_pos_ent = -1;
1710 it->ooi_attr = attr;
1712 return (struct dt_it *)it;
1716 * Finalize the OSP layer index iteration.
1718 * \param[in] env pointer to the thread context
1719 * \param[in] di pointer to the iteration structure
1721 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1723 struct osp_it *it = (struct osp_it *)di;
1724 struct page **pages = it->ooi_pages;
1725 int npages = it->ooi_total_npages;
1728 if (pages != NULL) {
1729 for (i = 0; i < npages; i++) {
1730 if (pages[i] != NULL) {
1731 if (pages[i] == it->ooi_cur_page) {
1733 it->ooi_cur_page = NULL;
1735 __free_page(pages[i]);
1738 OBD_FREE(pages, npages * sizeof(*pages));
1744 * Get more records for the iteration from peer.
1746 * The new records will be filled in an array of pages. The OSP side
1747 * allows 1MB bulk data to be transfered.
1749 * \param[in] env pointer to the thread context
1750 * \param[in] it pointer to the iteration structure
1752 * \retval 0 for success
1753 * \retval negative error number on failure
1755 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1757 struct lu_device *dev = it->ooi_obj->do_lu.lo_dev;
1758 struct osp_device *osp = lu2osp_dev(dev);
1759 struct page **pages;
1760 struct ptlrpc_request *req = NULL;
1761 struct ptlrpc_bulk_desc *desc;
1762 struct idx_info *ii;
1769 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1770 npages /= PAGE_CACHE_SIZE;
1772 OBD_ALLOC(pages, npages * sizeof(*pages));
1776 it->ooi_pages = pages;
1777 it->ooi_total_npages = npages;
1778 for (i = 0; i < npages; i++) {
1779 pages[i] = alloc_page(GFP_IOFS);
1780 if (pages[i] == NULL)
1784 req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1789 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1791 ptlrpc_request_free(req);
1795 req->rq_request_portal = OUT_PORTAL;
1796 ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1797 memset(ii, 0, sizeof(*ii));
1798 if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1799 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1800 * ost_index, 0] with LAST_ID FID, so it needs to replace
1801 * the FID with orphan FID here */
1802 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1803 ii->ii_fid.f_oid = osp->opd_index;
1804 ii->ii_fid.f_ver = 0;
1805 ii->ii_flags = II_FL_NOHASH;
1806 ii->ii_attrs = osp_dev2node(osp);
1808 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1809 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1811 ii->ii_attrs = it->ooi_attr;
1813 ii->ii_magic = IDX_INFO_MAGIC;
1814 ii->ii_count = npages * LU_PAGE_COUNT;
1815 ii->ii_hash_start = it->ooi_next;
1817 ptlrpc_at_set_req_timeout(req);
1819 desc = ptlrpc_prep_bulk_imp(req, npages, 1, BULK_PUT_SINK,
1822 ptlrpc_request_free(req);
1826 for (i = 0; i < npages; i++)
1827 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1829 ptlrpc_request_set_replen(req);
1830 rc = ptlrpc_queue_wait(req);
1834 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1835 req->rq_bulk->bd_nob_transferred);
1840 ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1841 if (ii->ii_magic != IDX_INFO_MAGIC)
1842 GOTO(out, rc = -EPROTO);
1844 npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1845 (PAGE_CACHE_SHIFT - LU_PAGE_SHIFT);
1846 if (npages > it->ooi_total_npages) {
1847 CERROR("%s: returned more pages than expected, %u > %u\n",
1848 osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1849 GOTO(out, rc = -EINVAL);
1852 it->ooi_valid_npages = npages;
1853 if (ptlrpc_rep_need_swab(req))
1856 it->ooi_next = ii->ii_hash_end;
1859 ptlrpc_req_finished(req);
1865 * Move the iteration cursor to the next lu_page.
1867 * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1868 * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1869 * in current system page, then move the iteration cursor to the next
1870 * lu_page in current system page. Otherwise, if there are more system
1871 * pages in the cache, then move the iteration cursor to the next system
1872 * page. If all the cached records (pages) have been iterated, then fetch
1873 * more records via osp_it_fetch().
1875 * \param[in] env pointer to the thread context
1876 * \param[in] di pointer to the iteration structure
1878 * \retval positive for end of the directory
1879 * \retval 0 for success
1880 * \retval negative error number on failure
1882 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1884 struct osp_it *it = (struct osp_it *)di;
1885 struct lu_idxpage *idxpage;
1886 struct page **pages;
1892 idxpage = it->ooi_cur_idxpage;
1893 if (idxpage != NULL) {
1894 if (idxpage->lip_nr == 0)
1897 if (it->ooi_pos_ent < idxpage->lip_nr) {
1898 CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1899 (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1902 it->ooi_cur_idxpage = NULL;
1903 it->ooi_pos_lu_page++;
1906 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1907 it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1908 LU_PAGE_SIZE * it->ooi_pos_lu_page;
1910 lustre_swab_lip_header(it->ooi_cur_idxpage);
1911 if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1912 struct osp_device *osp =
1913 lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1915 CERROR("%s: invalid magic (%x != %x) for page "
1916 "%d/%d while read layout orphan index\n",
1917 osp->opd_obd->obd_name,
1918 it->ooi_cur_idxpage->lip_magic,
1919 LIP_MAGIC, it->ooi_pos_page,
1920 it->ooi_pos_lu_page);
1921 /* Skip this lu_page next time. */
1922 it->ooi_pos_ent = idxpage->lip_nr - 1;
1925 it->ooi_pos_ent = -1;
1929 kunmap(it->ooi_cur_page);
1930 it->ooi_cur_page = NULL;
1934 pages = it->ooi_pages;
1935 if (it->ooi_pos_page < it->ooi_valid_npages) {
1936 it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1937 it->ooi_pos_lu_page = 0;
1941 for (i = 0; i < it->ooi_total_npages; i++) {
1942 if (pages[i] != NULL)
1943 __free_page(pages[i]);
1945 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1947 it->ooi_pos_page = 0;
1948 it->ooi_total_npages = 0;
1949 it->ooi_valid_npages = 0;
1952 it->ooi_cur_page = NULL;
1953 it->ooi_cur_idxpage = NULL;
1954 it->ooi_pages = NULL;
1957 if (it->ooi_next == II_END_OFF)
1960 rc = osp_it_fetch(env, it);
1968 * Move the iteration cursor to the next record.
1970 * If there are more records in the lu_page, then move the iteration
1971 * cursor to the next record directly. Otherwise, move the iteration
1972 * cursor to the record in the next lu_page via osp_it_next_page()
1974 * \param[in] env pointer to the thread context
1975 * \param[in] di pointer to the iteration structure
1977 * \retval positive for end of the directory
1978 * \retval 0 for success
1979 * \retval negative error number on failure
1981 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1983 struct osp_it *it = (struct osp_it *)di;
1984 struct lu_idxpage *idxpage;
1989 idxpage = it->ooi_cur_idxpage;
1990 if (idxpage != NULL) {
1991 if (idxpage->lip_nr == 0)
1995 if (it->ooi_pos_ent < idxpage->lip_nr) {
1997 (struct lu_orphan_ent *)idxpage->lip_entries +
2000 lustre_swab_orphan_ent(it->ooi_ent);
2005 rc = osp_it_next_page(env, di);
2012 int osp_it_get(const struct lu_env *env, struct dt_it *di,
2013 const struct dt_key *key)
2018 void osp_it_put(const struct lu_env *env, struct dt_it *di)
2022 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
2023 const struct dt_it *di)
2025 struct osp_it *it = (struct osp_it *)di;
2026 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
2028 if (likely(ent != NULL))
2029 return (struct dt_key *)(&ent->loe_key);
2034 static int osp_orphan_it_key_size(const struct lu_env *env,
2035 const struct dt_it *di)
2037 return sizeof(struct lu_fid);
2040 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
2041 struct dt_rec *rec, __u32 attr)
2043 struct osp_it *it = (struct osp_it *)di;
2044 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
2046 if (likely(ent != NULL)) {
2047 *(struct lu_orphan_rec *)rec = ent->loe_rec;
2054 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
2056 struct osp_it *it = (struct osp_it *)di;
2058 return it->ooi_next;
2062 * Locate the iteration cursor to the specified position (cookie).
2064 * \param[in] env pointer to the thread context
2065 * \param[in] di pointer to the iteration structure
2066 * \param[in] hash the specified position
2068 * \retval positive number for locating to the exactly position
2070 * \retval 0 for arriving at the end of the iteration
2071 * \retval negative error number on failure
2073 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2076 struct osp_it *it = (struct osp_it *)di;
2079 it->ooi_next = hash;
2080 rc = osp_orphan_it_next(env, (struct dt_it *)di);
2090 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2096 static const struct dt_index_operations osp_orphan_index_ops = {
2097 .dio_lookup = osp_orphan_index_lookup,
2098 .dio_declare_insert = osp_orphan_index_declare_insert,
2099 .dio_insert = osp_orphan_index_insert,
2100 .dio_declare_delete = osp_orphan_index_declare_delete,
2101 .dio_delete = osp_orphan_index_delete,
2103 .init = osp_it_init,
2104 .fini = osp_it_fini,
2105 .next = osp_orphan_it_next,
2108 .key = osp_orphan_it_key,
2109 .key_size = osp_orphan_it_key_size,
2110 .rec = osp_orphan_it_rec,
2111 .store = osp_it_store,
2112 .load = osp_orphan_it_load,
2113 .key_rec = osp_it_key_rec,
2118 * Implement OSP layer dt_object_operations::do_index_try() interface.
2120 * Negotiate the index type.
2122 * If the target index is an IDIF object, then use osp_orphan_index_ops.
2123 * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2124 * (\see lustre/include/lustre_fid.h for IDIF.)
2126 * \param[in] env pointer to the thread context
2127 * \param[in] dt pointer to the OSP layer dt_object
2128 * \param[in] feat unused
2130 * \retval 0 for success
2132 static int osp_index_try(const struct lu_env *env,
2133 struct dt_object *dt,
2134 const struct dt_index_features *feat)
2136 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2138 if (fid_is_last_id(fid) && fid_is_idif(fid))
2139 dt->do_index_ops = &osp_orphan_index_ops;
2141 dt->do_index_ops = &osp_md_index_ops;
2145 static struct dt_object_operations osp_obj_ops = {
2146 .do_declare_attr_get = osp_declare_attr_get,
2147 .do_attr_get = osp_attr_get,
2148 .do_declare_attr_set = osp_declare_attr_set,
2149 .do_attr_set = osp_attr_set,
2150 .do_declare_xattr_get = osp_declare_xattr_get,
2151 .do_xattr_get = osp_xattr_get,
2152 .do_declare_xattr_set = osp_declare_xattr_set,
2153 .do_xattr_set = osp_xattr_set,
2154 .do_declare_create = osp_declare_object_create,
2155 .do_create = osp_object_create,
2156 .do_declare_destroy = osp_declare_object_destroy,
2157 .do_destroy = osp_object_destroy,
2158 .do_index_try = osp_index_try,
2162 * Implement OSP layer lu_object_operations::loo_object_init() interface.
2164 * Initialize the object.
2166 * If it is a remote MDT object, then call do_attr_get() to fetch
2167 * the attribute from the peer.
2169 * \param[in] env pointer to the thread context
2170 * \param[in] o pointer to the OSP layer lu_object
2171 * \param[in] conf unused
2173 * \retval 0 for success
2174 * \retval negative error number on failure
2176 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2177 const struct lu_object_conf *conf)
2179 struct osp_object *po = lu2osp_obj(o);
2183 spin_lock_init(&po->opo_lock);
2184 o->lo_header->loh_attr |= LOHA_REMOTE;
2186 if (is_ost_obj(o)) {
2187 po->opo_obj.do_ops = &osp_obj_ops;
2189 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2191 po->opo_obj.do_ops = &osp_md_obj_ops;
2192 po->opo_obj.do_body_ops = &osp_md_body_ops;
2193 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o), la);
2195 o->lo_header->loh_attr |=
2196 LOHA_EXISTS | (la->la_mode & S_IFMT);
2197 if (rc == -ENOENT) {
2198 po->opo_non_exist = 1;
2201 init_rwsem(&po->opo_sem);
2207 * Implement OSP layer lu_object_operations::loo_object_free() interface.
2209 * Finalize the object.
2211 * If the OSP object has attributes cache, then destroy the cache.
2212 * Free the object finally.
2214 * \param[in] env pointer to the thread context
2215 * \param[in] o pointer to the OSP layer lu_object
2217 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2219 struct osp_object *obj = lu2osp_obj(o);
2220 struct lu_object_header *h = o->lo_header;
2222 dt_object_fini(&obj->opo_obj);
2223 lu_object_header_fini(h);
2224 if (obj->opo_ooa != NULL) {
2225 struct osp_xattr_entry *oxe;
2226 struct osp_xattr_entry *tmp;
2229 list_for_each_entry_safe(oxe, tmp,
2230 &obj->opo_ooa->ooa_xattr_list,
2232 list_del(&oxe->oxe_list);
2233 count = atomic_read(&oxe->oxe_ref);
2234 LASSERTF(count == 1,
2235 "Still has %d users on the xattr entry %.*s\n",
2236 count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2238 OBD_FREE(oxe, oxe->oxe_buflen);
2240 OBD_FREE_PTR(obj->opo_ooa);
2242 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2246 * Implement OSP layer lu_object_operations::loo_object_release() interface.
2248 * Cleanup (not free) the object.
2250 * If it is a reserved object but failed to be created, or it is an OST
2251 * object, then mark the object as non-cached.
2253 * \param[in] env pointer to the thread context
2254 * \param[in] o pointer to the OSP layer lu_object
2256 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2258 struct osp_object *po = lu2osp_obj(o);
2259 struct osp_device *d = lu2osp_dev(o->lo_dev);
2264 * release reservation if object was declared but not created
2265 * this may require lu_object_put() in LOD
2267 if (unlikely(po->opo_reserved)) {
2268 LASSERT(d->opd_pre != NULL);
2269 LASSERT(d->opd_pre_reserved > 0);
2270 spin_lock(&d->opd_pre_lock);
2271 d->opd_pre_reserved--;
2272 spin_unlock(&d->opd_pre_lock);
2274 /* not needed in cache any more */
2275 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2279 /* XXX: Currently, NOT cache OST-object on MDT because:
2280 * 1. it is not often accessed on MDT.
2281 * 2. avoid up layer (such as LFSCK) to load too many
2282 * once-used OST-objects. */
2283 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2288 static int osp_object_print(const struct lu_env *env, void *cookie,
2289 lu_printer_t p, const struct lu_object *l)
2291 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2293 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2296 static int osp_object_invariant(const struct lu_object *o)
2301 struct lu_object_operations osp_lu_obj_ops = {
2302 .loo_object_init = osp_object_init,
2303 .loo_object_free = osp_object_free,
2304 .loo_object_release = osp_object_release,
2305 .loo_object_print = osp_object_print,
2306 .loo_object_invariant = osp_object_invariant