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) {
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);
610 ptlrpc_req_finished(req);
612 osp_update_request_destroy(update);
618 * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
620 * If the transaction is not remote one, then declare the credits that will
621 * be used for the subsequent llog record for the object's attributes.
623 * \param[in] env pointer to the thread context
624 * \param[in] dt pointer to the OSP layer dt_object
625 * \param[in] attr pointer to the attribute to be set
626 * \param[in] th pointer to the transaction handler
628 * \retval 0 for success
629 * \retval negative error number on failure
631 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
632 const struct lu_attr *attr, struct thandle *th)
634 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
635 struct osp_object *o = dt2osp_obj(dt);
638 if (is_only_remote_trans(th))
639 return osp_md_declare_attr_set(env, dt, attr, th);
641 * Usually we don't allow server stack to manipulate size
642 * but there is a special case when striping is created
643 * late, after stripeless file got truncated to non-zero.
645 * In this case we do the following:
647 * 1) grab id in declare - this can lead to leaked OST objects
648 * but we don't currently have proper mechanism and the only
649 * options we have are to do truncate RPC holding transaction
650 * open (very bad) or to grab id in declare at cost of leaked
651 * OST object in same very rare unfortunate case (just bad)
652 * notice 1.6-2.0 do assignment outside of running transaction
653 * all the time, meaning many more chances for leaked objects.
655 * 2) send synchronous truncate RPC with just assigned id
658 /* there are few places in MDD code still passing NULL
659 * XXX: to be fixed soon */
663 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
664 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
665 LASSERT(!dt_object_exists(dt));
666 osp_object_assign_fid(env, d, o);
667 rc = osp_object_truncate(env, dt, attr->la_size);
672 if (!(attr->la_valid & (LA_UID | LA_GID)))
675 /* track all UID/GID changes via llog */
676 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
682 * Implement OSP layer dt_object_operations::do_attr_set() interface.
684 * Set attribute to the specified OST object.
686 * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
687 * in the OUT RPC that will be flushed when the remote transaction stop.
688 * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
689 * will be handled by a dedicated thread asynchronously.
691 * If the attribute entry exists in the OSP object attributes cache,
692 * then update the cached attribute according to given attribute.
694 * \param[in] env pointer to the thread context
695 * \param[in] dt pointer to the OSP layer dt_object
696 * \param[in] attr pointer to the attribute to be set
697 * \param[in] th pointer to the transaction handler
699 * \retval 0 for success
700 * \retval negative error number on failure
702 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
703 const struct lu_attr *attr, struct thandle *th)
705 struct osp_object *o = dt2osp_obj(dt);
709 /* we're interested in uid/gid changes only */
710 if (!(attr->la_valid & (LA_UID | LA_GID)))
713 if (!is_only_remote_trans(th)) {
714 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
715 /* XXX: send new uid/gid to OST ASAP? */
719 /* It is for OST-object attr_set directly without updating
720 * local MDT-object attribute. It is usually used by LFSCK. */
721 rc = osp_md_attr_set(env, dt, attr, th);
722 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
723 PFID(&dt->do_lu.lo_header->loh_fid), rc);
725 if (rc != 0 || o->opo_ooa == NULL)
728 /* Update the OSP object attributes cache. */
729 la = &o->opo_ooa->ooa_attr;
730 spin_lock(&o->opo_lock);
731 if (attr->la_valid & LA_UID) {
732 la->la_uid = attr->la_uid;
733 la->la_valid |= LA_UID;
736 if (attr->la_valid & LA_GID) {
737 la->la_gid = attr->la_gid;
738 la->la_valid |= LA_GID;
740 spin_unlock(&o->opo_lock);
747 * Interpreter function for getting OSP object extended attribute asynchronously
749 * Called to interpret the result of an async mode RPC for getting the
750 * OSP object extended attribute.
752 * \param[in] env pointer to the thread context
753 * \param[in] reply pointer to the RPC reply
754 * \param[in] req pointer to the RPC request
755 * \param[in] obj pointer to the OSP object
756 * \param[out] data pointer to OSP object attributes cache
757 * \param[in] index the index of the attribute buffer in the reply
758 * \param[in] rc the result for handling the RPC
760 * \retval 0 for success
761 * \retval negative error number on failure
763 static int osp_xattr_get_interpterer(const struct lu_env *env,
764 struct object_update_reply *reply,
765 struct ptlrpc_request *req,
766 struct osp_object *obj,
767 void *data, int index, int rc)
769 struct osp_object_attr *ooa = obj->opo_ooa;
770 struct osp_xattr_entry *oxe = data;
771 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
773 LASSERT(ooa != NULL);
776 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
778 rc = object_update_result_data_get(reply, rbuf, index);
779 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
780 spin_lock(&obj->opo_lock);
782 spin_unlock(&obj->opo_lock);
783 osp_oac_xattr_put(oxe);
785 return rc < 0 ? rc : -ERANGE;
788 spin_lock(&obj->opo_lock);
789 oxe->oxe_vallen = rbuf->lb_len;
790 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
793 spin_unlock(&obj->opo_lock);
794 } else if (rc == -ENOENT || rc == -ENODATA) {
795 spin_lock(&obj->opo_lock);
798 spin_unlock(&obj->opo_lock);
800 spin_lock(&obj->opo_lock);
802 spin_unlock(&obj->opo_lock);
805 osp_oac_xattr_put(oxe);
811 * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
813 * Declare that the caller will get extended attribute from the specified
816 * This function will add an OUT_XATTR_GET sub-request to the per OSP
817 * based shared asynchronous request queue with the interpreter function:
818 * osp_xattr_get_interpterer().
820 * \param[in] env pointer to the thread context
821 * \param[in] dt pointer to the OSP layer dt_object
822 * \param[out] buf pointer to the lu_buf to hold the extended attribute
823 * \param[in] name the name for the expected extended attribute
825 * \retval 0 for success
826 * \retval negative error number on failure
828 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
829 struct lu_buf *buf, const char *name)
831 struct osp_object *obj = dt2osp_obj(dt);
832 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
833 struct osp_xattr_entry *oxe;
834 __u16 namelen = strlen(name);
837 LASSERT(buf != NULL);
838 LASSERT(name != NULL);
840 /* If only for xattr size, return directly. */
841 if (unlikely(buf->lb_len == 0))
844 if (obj->opo_ooa == NULL) {
845 rc = osp_oac_init(obj);
850 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
854 mutex_lock(&osp->opd_async_requests_mutex);
855 rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
856 &namelen, (const void **)&name,
858 osp_xattr_get_interpterer);
860 mutex_unlock(&osp->opd_async_requests_mutex);
861 osp_oac_xattr_put(oxe);
863 struct osp_update_request *our;
864 struct osp_update_request_sub *ours;
866 /* XXX: Currently, we trigger the batched async OUT
867 * RPC via dt_declare_xattr_get(). It is not
868 * perfect solution, but works well now.
870 * We will improve it in the future. */
871 our = osp->opd_async_requests;
872 ours = osp_current_object_update_request(our);
873 if (ours != NULL && ours->ours_req != NULL &&
874 ours->ours_req->ourq_count > 0) {
875 osp->opd_async_requests = NULL;
876 mutex_unlock(&osp->opd_async_requests_mutex);
877 rc = osp_unplug_async_request(env, osp, our);
879 mutex_unlock(&osp->opd_async_requests_mutex);
887 * Implement OSP layer dt_object_operations::do_xattr_get() interface.
889 * Get extended attribute from the specified MDT/OST object.
891 * If the extended attribute is in the OSP object attributes cache, then
892 * return the cached extended attribute directly. Otherwise it will get
893 * the extended attribute synchronously, if successful, add it to the OSP
894 * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
896 * There is a race condition: some other thread has added the named extended
897 * attributed entry to the OSP object attributes cache during the current
898 * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
899 * (just) existing extended attribute entry with the new replied one.
901 * \param[in] env pointer to the thread context
902 * \param[in] dt pointer to the OSP layer dt_object
903 * \param[out] buf pointer to the lu_buf to hold the extended attribute
904 * \param[in] name the name for the expected extended attribute
906 * \retval 0 for success
907 * \retval negative error number on failure
909 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
910 struct lu_buf *buf, const char *name)
912 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
913 struct osp_object *obj = dt2osp_obj(dt);
914 struct dt_device *dev = &osp->opd_dt_dev;
915 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
916 struct osp_update_request *update = NULL;
917 struct ptlrpc_request *req = NULL;
918 struct object_update_reply *reply;
919 struct osp_xattr_entry *oxe = NULL;
920 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
924 LASSERT(buf != NULL);
925 LASSERT(name != NULL);
927 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
928 osp->opd_index == cfs_fail_val) {
929 if (is_ost_obj(&dt->do_lu)) {
930 if (osp_dev2node(osp) == cfs_fail_val)
933 if (strcmp(name, XATTR_NAME_LINK) == 0)
938 if (unlikely(obj->opo_non_exist))
941 /* Only cache xattr for OST object */
942 if (!osp->opd_connect_mdt) {
943 oxe = osp_oac_xattr_find(obj, name, false);
945 spin_lock(&obj->opo_lock);
946 if (oxe->oxe_ready) {
948 GOTO(unlock, rc = -ENODATA);
950 if (buf->lb_buf == NULL)
951 GOTO(unlock, rc = oxe->oxe_vallen);
953 if (buf->lb_len < oxe->oxe_vallen)
954 GOTO(unlock, rc = -ERANGE);
956 memcpy(buf->lb_buf, oxe->oxe_value,
959 GOTO(unlock, rc = oxe->oxe_vallen);
962 spin_unlock(&obj->opo_lock);
963 osp_oac_xattr_put(oxe);
967 spin_unlock(&obj->opo_lock);
970 update = osp_update_request_create(dev);
972 GOTO(out, rc = PTR_ERR(update));
974 rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
975 lu_object_fid(&dt->do_lu), name, buf->lb_len);
977 CERROR("%s: Insert update error "DFID": rc = %d\n",
978 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
982 rc = osp_remote_sync(env, osp, update, &req);
985 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
986 obj->opo_non_exist = 1;
989 if (obj->opo_ooa == NULL)
993 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
996 CWARN("%s: Fail to add xattr (%s) to cache for "
997 DFID" (1): rc = %d\n", dname, name,
998 PFID(lu_object_fid(&dt->do_lu)), rc);
1003 spin_lock(&obj->opo_lock);
1004 if (rc == -ENOENT || rc == -ENODATA) {
1010 spin_unlock(&obj->opo_lock);
1015 reply = req_capsule_server_sized_get(&req->rq_pill,
1016 &RMF_OUT_UPDATE_REPLY,
1017 OUT_UPDATE_REPLY_SIZE);
1018 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1019 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1020 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1021 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1023 GOTO(out, rc = -EPROTO);
1026 rc = object_update_result_data_get(reply, rbuf, 0);
1030 if (buf->lb_buf == NULL)
1033 if (unlikely(buf->lb_len < rbuf->lb_len))
1034 GOTO(out, rc = -ERANGE);
1036 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1038 if (obj->opo_ooa == NULL || osp->opd_connect_mdt)
1042 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1044 CWARN("%s: Fail to add xattr (%s) to "
1045 "cache for "DFID" (2): rc = %d\n",
1046 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1052 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
1053 struct osp_xattr_entry *old = oxe;
1054 struct osp_xattr_entry *tmp;
1056 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
1057 osp_oac_xattr_put(oxe);
1060 CWARN("%s: Fail to update xattr (%s) to "
1061 "cache for "DFID": rc = %d\n",
1062 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1063 spin_lock(&obj->opo_lock);
1065 spin_unlock(&obj->opo_lock);
1070 /* Drop the ref for entry on list. */
1071 osp_oac_xattr_put(old);
1074 spin_lock(&obj->opo_lock);
1075 oxe->oxe_vallen = rbuf->lb_len;
1076 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
1079 spin_unlock(&obj->opo_lock);
1085 ptlrpc_req_finished(req);
1087 if (update != NULL && !IS_ERR(update))
1088 osp_update_request_destroy(update);
1091 osp_oac_xattr_put(oxe);
1097 * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1099 * Declare that the caller will set extended attribute to the specified
1102 * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1103 * to the OUT RPC that will be flushed when the transaction start. And if the
1104 * OSP attributes cache is initialized, then check whether the name extended
1105 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1106 * add the extended attribute to the cache.
1108 * \param[in] env pointer to the thread context
1109 * \param[in] dt pointer to the OSP layer dt_object
1110 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1111 * \param[in] name the name of the extended attribute to be set
1112 * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
1113 * or LU_XATTR_REPLACE or others
1114 * \param[in] th pointer to the transaction handler
1116 * \retval 0 for success
1117 * \retval negative error number on failure
1119 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1120 const struct lu_buf *buf, const char *name,
1121 int flag, struct thandle *th)
1123 return osp_trans_update_request_create(th);
1127 * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1129 * Set extended attribute to the specified MDT/OST object.
1131 * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
1132 * the transaction stop. And if the OSP attributes cache is initialized, then
1133 * check whether the name extended attribute entry exists in the cache or not.
1134 * If yes, replace it; otherwise, add the extended attribute to the cache.
1136 * \param[in] env pointer to the thread context
1137 * \param[in] dt pointer to the OSP layer dt_object
1138 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1139 * \param[in] name the name of the extended attribute to be set
1140 * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
1141 * or LU_XATTR_REPLACE or others
1142 * \param[in] th pointer to the transaction handler
1144 * \retval 0 for success
1145 * \retval negative error number on failure
1147 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1148 const struct lu_buf *buf, const char *name, int fl,
1151 struct osp_object *o = dt2osp_obj(dt);
1152 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1153 struct osp_update_request *update;
1154 struct osp_xattr_entry *oxe;
1158 update = thandle_to_osp_update_request(th);
1159 LASSERT(update != NULL);
1161 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
1162 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
1164 rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
1165 lu_object_fid(&dt->do_lu), buf, name, fl);
1166 if (rc != 0 || o->opo_ooa == NULL || osp->opd_connect_mdt)
1169 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1171 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1172 dt->do_lu.lo_dev->ld_obd->obd_name,
1173 name, PFID(lu_object_fid(&dt->do_lu)));
1178 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
1179 struct osp_xattr_entry *old = oxe;
1180 struct osp_xattr_entry *tmp;
1182 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
1183 osp_oac_xattr_put(oxe);
1186 CWARN("%s: cannot update cached xattr '%s' of "DFID"\n",
1187 dt->do_lu.lo_dev->ld_obd->obd_name,
1188 name, PFID(lu_object_fid(&dt->do_lu)));
1189 spin_lock(&o->opo_lock);
1191 spin_unlock(&o->opo_lock);
1196 /* Drop the ref for entry on list. */
1197 osp_oac_xattr_put(old);
1200 spin_lock(&o->opo_lock);
1201 oxe->oxe_vallen = buf->lb_len;
1202 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
1205 spin_unlock(&o->opo_lock);
1206 osp_oac_xattr_put(oxe);
1212 * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1214 * Declare that the caller will delete extended attribute on the specified
1217 * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1218 * to the OUT RPC that will be flushed when the transaction start. And if the
1219 * name extended attribute entry exists in the OSP attributes cache, then remove
1220 * it from the cache.
1222 * \param[in] env pointer to the thread context
1223 * \param[in] dt pointer to the OSP layer dt_object
1224 * \param[in] name the name of the extended attribute to be set
1225 * \param[in] th pointer to the transaction handler
1227 * \retval 0 for success
1228 * \retval negative error number on failure
1230 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1231 const char *name, struct thandle *th)
1233 return osp_trans_update_request_create(th);
1237 * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1239 * Delete extended attribute on the specified MDT/OST object.
1241 * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1242 * the OUT RPC that will be flushed when the transaction stop. And if the name
1243 * extended attribute entry exists in the OSP attributes cache, then remove it
1246 * \param[in] env pointer to the thread context
1247 * \param[in] dt pointer to the OSP layer dt_object
1248 * \param[in] name the name of the extended attribute to be set
1249 * \param[in] th pointer to the transaction handler
1251 * \retval 0 for success
1252 * \retval negative error number on failure
1254 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1255 const char *name, struct thandle *th)
1257 struct osp_update_request *update;
1258 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1259 struct osp_object *o = dt2osp_obj(dt);
1260 struct osp_xattr_entry *oxe;
1263 update = thandle_to_osp_update_request(th);
1264 LASSERT(update != NULL);
1266 rc = osp_update_rpc_pack(env, xattr_del, update, OUT_XATTR_DEL,
1268 if (rc != 0 || o->opo_ooa == NULL)
1271 oxe = osp_oac_xattr_find(o, name, true);
1273 /* Drop the ref for entry on list. */
1274 osp_oac_xattr_put(oxe);
1280 * Implement OSP layer dt_object_operations::do_declare_create() interface.
1282 * Declare that the caller will create the OST object.
1284 * If the transaction is a remote transaction and the FID for the OST-object
1285 * has been assigned already, then handle it as creating (remote) MDT object
1286 * via osp_md_declare_object_create(). This function is usually used for LFSCK
1287 * to re-create the lost OST object. Otherwise, if it is not replay case, the
1288 * OSP will reserve pre-created object for the subsequent create operation;
1289 * if the MDT side cached pre-created objects are less than some threshold,
1290 * then it will wakeup the pre-create thread.
1292 * \param[in] env pointer to the thread context
1293 * \param[in] dt pointer to the OSP layer dt_object
1294 * \param[in] attr the attribute for the object to be created
1295 * \param[in] hint pointer to the hint for creating the object, such as
1297 * \param[in] dof pointer to the dt_object_format for help the creation
1298 * \param[in] th pointer to the transaction handler
1300 * \retval 0 for success
1301 * \retval negative error number on failure
1303 static int osp_declare_object_create(const struct lu_env *env,
1304 struct dt_object *dt,
1305 struct lu_attr *attr,
1306 struct dt_allocation_hint *hint,
1307 struct dt_object_format *dof,
1310 struct osp_thread_info *osi = osp_env_info(env);
1311 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1312 struct osp_object *o = dt2osp_obj(dt);
1313 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1314 struct thandle *local_th;
1319 if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1320 LASSERT(fid_is_sane(fid));
1322 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
1327 /* should happen to non-0 OSP only so that at least one object
1328 * has been already declared in the scenario and LOD should
1330 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1333 LASSERT(d->opd_last_used_oid_file);
1336 * There can be gaps in precreated ids and record to unlink llog
1337 * XXX: we do not handle gaps yet, implemented before solution
1338 * was found to be racy, so we disabled that. there is no
1339 * point in making useless but expensive llog declaration.
1341 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1343 local_th = osp_get_storage_thandle(env, th, d);
1344 if (IS_ERR(local_th))
1345 RETURN(PTR_ERR(local_th));
1347 if (unlikely(!fid_is_zero(fid))) {
1348 /* replay case: caller knows fid */
1349 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1350 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1351 osi->osi_lb.lb_buf = NULL;
1353 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1354 &osi->osi_lb, osi->osi_off,
1360 * in declaration we need to reserve object so that we don't block
1361 * awaiting precreation RPC to complete
1363 rc = osp_precreate_reserve(env, d);
1365 * we also need to declare update to local "last used id" file for
1366 * recovery if object isn't used for a reason, we need to release
1367 * reservation, this can be made in osd_object_release()
1370 /* mark id is reserved: in create we don't want to talk
1372 LASSERT(o->opo_reserved == 0);
1373 o->opo_reserved = 1;
1375 /* common for all OSPs file hystorically */
1376 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1377 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1378 osi->osi_lb.lb_buf = NULL;
1379 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1380 &osi->osi_lb, osi->osi_off,
1383 /* not needed in the cache anymore */
1384 set_bit(LU_OBJECT_HEARD_BANSHEE,
1385 &dt->do_lu.lo_header->loh_flags);
1391 * Implement OSP layer dt_object_operations::do_create() interface.
1393 * Create the OST object.
1395 * If the transaction is a remote transaction and the FID for the OST-object
1396 * has been assigned already, then handle it as handling MDT object via the
1397 * osp_md_object_create(). For other cases, the OSP will assign FID to the
1398 * object to be created, and update last_used Object ID (OID) file.
1400 * \param[in] env pointer to the thread context
1401 * \param[in] dt pointer to the OSP layer dt_object
1402 * \param[in] attr the attribute for the object to be created
1403 * \param[in] hint pointer to the hint for creating the object, such as
1405 * \param[in] dof pointer to the dt_object_format for help the creation
1406 * \param[in] th pointer to the transaction handler
1408 * \retval 0 for success
1409 * \retval negative error number on failure
1411 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
1412 struct lu_attr *attr,
1413 struct dt_allocation_hint *hint,
1414 struct dt_object_format *dof, struct thandle *th)
1416 struct osp_thread_info *osi = osp_env_info(env);
1417 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1418 struct osp_object *o = dt2osp_obj(dt);
1420 struct lu_fid *fid = &osi->osi_fid;
1421 struct thandle *local_th;
1424 if (is_only_remote_trans(th) &&
1425 !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1426 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1428 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
1430 o->opo_non_exist = 0;
1435 o->opo_non_exist = 0;
1436 if (o->opo_reserved) {
1437 /* regular case, fid is assigned holding transaction open */
1438 osp_object_assign_fid(env, d, o);
1441 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1443 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1446 if (!o->opo_reserved) {
1447 /* special case, id was assigned outside of transaction
1448 * see comments in osp_declare_attr_set */
1449 LASSERT(d->opd_pre != NULL);
1450 spin_lock(&d->opd_pre_lock);
1451 osp_update_last_fid(d, fid);
1452 spin_unlock(&d->opd_pre_lock);
1455 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1457 /* If the precreate ends, it means it will be ready to rollover to
1458 * the new sequence soon, all the creation should be synchronized,
1459 * otherwise during replay, the replay fid will be inconsistent with
1460 * last_used/create fid */
1461 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1464 local_th = osp_get_storage_thandle(env, th, d);
1465 if (IS_ERR(local_th))
1466 RETURN(PTR_ERR(local_th));
1468 * it's OK if the import is inactive by this moment - id was created
1469 * by OST earlier, we just need to maintain it consistently on the disk
1470 * once import is reconnected, OSP will claim this and other objects
1471 * used and OST either keep them, if they exist or recreate
1474 /* we might have lost precreated objects */
1475 if (unlikely(d->opd_gap_count) > 0) {
1476 LASSERT(d->opd_pre != NULL);
1477 spin_lock(&d->opd_pre_lock);
1478 if (d->opd_gap_count > 0) {
1479 int count = d->opd_gap_count;
1481 ostid_set_id(&osi->osi_oi,
1482 fid_oid(&d->opd_gap_start_fid));
1483 d->opd_gap_count = 0;
1484 spin_unlock(&d->opd_pre_lock);
1486 CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1487 PFID(&d->opd_gap_start_fid), count);
1488 /* real gap handling is disabled intil ORI-692 will be
1489 * fixed, now we only report gaps */
1491 spin_unlock(&d->opd_pre_lock);
1495 /* Only need update last_used oid file, seq file will only be update
1496 * during seq rollover */
1497 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1498 &d->opd_last_used_fid.f_oid, d->opd_index);
1500 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1501 &osi->osi_off, local_th);
1503 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1504 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1510 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1512 * Declare that the caller will destroy the specified OST object.
1514 * The OST object destroy will be handled via llog asynchronously. This
1515 * function will declare the credits for generating MDS_UNLINK64_REC llog.
1517 * \param[in] env pointer to the thread context
1518 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1519 * \param[in] th pointer to the transaction handler
1521 * \retval 0 for success
1522 * \retval negative error number on failure
1524 int osp_declare_object_destroy(const struct lu_env *env,
1525 struct dt_object *dt, struct thandle *th)
1527 struct osp_object *o = dt2osp_obj(dt);
1528 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1533 LASSERT(!osp->opd_connect_mdt);
1534 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1540 * Implement OSP layer dt_object_operations::do_destroy() interface.
1542 * Destroy the specified OST object.
1544 * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1545 * will be some dedicated thread to handle the llog asynchronously.
1547 * It also marks the object as non-cached.
1549 * \param[in] env pointer to the thread context
1550 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1551 * \param[in] th pointer to the transaction handler
1553 * \retval 0 for success
1554 * \retval negative error number on failure
1556 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1559 struct osp_object *o = dt2osp_obj(dt);
1560 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1565 o->opo_non_exist = 1;
1567 LASSERT(!osp->opd_connect_mdt);
1568 /* once transaction is committed put proper command on
1569 * the queue going to our OST. */
1570 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1574 /* not needed in cache any more */
1575 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1580 static int osp_orphan_index_lookup(const struct lu_env *env,
1581 struct dt_object *dt,
1583 const struct dt_key *key)
1588 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1589 struct dt_object *dt,
1590 const struct dt_rec *rec,
1591 const struct dt_key *key,
1592 struct thandle *handle)
1597 static int osp_orphan_index_insert(const struct lu_env *env,
1598 struct dt_object *dt,
1599 const struct dt_rec *rec,
1600 const struct dt_key *key,
1601 struct thandle *handle,
1607 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1608 struct dt_object *dt,
1609 const struct dt_key *key,
1610 struct thandle *handle)
1615 static int osp_orphan_index_delete(const struct lu_env *env,
1616 struct dt_object *dt,
1617 const struct dt_key *key,
1618 struct thandle *handle)
1624 * Initialize the OSP layer index iteration.
1626 * \param[in] env pointer to the thread context
1627 * \param[in] dt pointer to the index object to be iterated
1628 * \param[in] attr unused
1630 * \retval pointer to the iteration structure
1631 * \retval negative error number on failure
1633 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1640 return ERR_PTR(-ENOMEM);
1642 it->ooi_pos_ent = -1;
1644 it->ooi_attr = attr;
1646 return (struct dt_it *)it;
1650 * Finalize the OSP layer index iteration.
1652 * \param[in] env pointer to the thread context
1653 * \param[in] di pointer to the iteration structure
1655 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1657 struct osp_it *it = (struct osp_it *)di;
1658 struct page **pages = it->ooi_pages;
1659 int npages = it->ooi_total_npages;
1662 if (pages != NULL) {
1663 for (i = 0; i < npages; i++) {
1664 if (pages[i] != NULL) {
1665 if (pages[i] == it->ooi_cur_page) {
1667 it->ooi_cur_page = NULL;
1669 __free_page(pages[i]);
1672 OBD_FREE(pages, npages * sizeof(*pages));
1678 * Get more records for the iteration from peer.
1680 * The new records will be filled in an array of pages. The OSP side
1681 * allows 1MB bulk data to be transferred.
1683 * \param[in] env pointer to the thread context
1684 * \param[in] it pointer to the iteration structure
1686 * \retval 0 for success
1687 * \retval negative error number on failure
1689 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1691 struct lu_device *dev = it->ooi_obj->do_lu.lo_dev;
1692 struct osp_device *osp = lu2osp_dev(dev);
1693 struct page **pages;
1694 struct lu_device *top_device;
1695 struct ptlrpc_request *req = NULL;
1696 struct ptlrpc_bulk_desc *desc;
1697 struct idx_info *ii;
1704 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1705 npages /= PAGE_CACHE_SIZE;
1707 OBD_ALLOC(pages, npages * sizeof(*pages));
1711 it->ooi_pages = pages;
1712 it->ooi_total_npages = npages;
1713 for (i = 0; i < npages; i++) {
1714 pages[i] = alloc_page(GFP_IOFS);
1715 if (pages[i] == NULL)
1719 req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1724 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1726 ptlrpc_request_free(req);
1730 /* Let's allow this request during recovery, otherwise
1731 * if the remote target is also in recovery status,
1732 * it might cause deadlock */
1733 top_device = dev->ld_site->ls_top_dev;
1734 if (top_device->ld_obd->obd_recovering)
1735 req->rq_allow_replay = 1;
1737 req->rq_request_portal = OUT_PORTAL;
1738 ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1739 memset(ii, 0, sizeof(*ii));
1740 if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1741 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1742 * ost_index, 0] with LAST_ID FID, so it needs to replace
1743 * the FID with orphan FID here */
1744 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1745 ii->ii_fid.f_oid = osp->opd_index;
1746 ii->ii_fid.f_ver = 0;
1747 ii->ii_flags = II_FL_NOHASH;
1748 ii->ii_attrs = osp_dev2node(osp);
1750 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1751 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1753 ii->ii_attrs = it->ooi_attr;
1755 ii->ii_magic = IDX_INFO_MAGIC;
1756 ii->ii_count = npages * LU_PAGE_COUNT;
1757 ii->ii_hash_start = it->ooi_next;
1759 ptlrpc_at_set_req_timeout(req);
1761 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1762 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1764 &ptlrpc_bulk_kiov_pin_ops);
1766 ptlrpc_request_free(req);
1770 for (i = 0; i < npages; i++)
1771 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1774 ptlrpc_request_set_replen(req);
1775 rc = ptlrpc_queue_wait(req);
1779 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1780 req->rq_bulk->bd_nob_transferred);
1785 ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1786 if (ii->ii_magic != IDX_INFO_MAGIC)
1787 GOTO(out, rc = -EPROTO);
1789 npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1790 (PAGE_CACHE_SHIFT - LU_PAGE_SHIFT);
1791 if (npages > it->ooi_total_npages) {
1792 CERROR("%s: returned more pages than expected, %u > %u\n",
1793 osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1794 GOTO(out, rc = -EINVAL);
1797 it->ooi_valid_npages = npages;
1798 if (ptlrpc_rep_need_swab(req))
1801 it->ooi_next = ii->ii_hash_end;
1804 ptlrpc_req_finished(req);
1810 * Move the iteration cursor to the next lu_page.
1812 * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1813 * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1814 * in current system page, then move the iteration cursor to the next
1815 * lu_page in current system page. Otherwise, if there are more system
1816 * pages in the cache, then move the iteration cursor to the next system
1817 * page. If all the cached records (pages) have been iterated, then fetch
1818 * more records via osp_it_fetch().
1820 * \param[in] env pointer to the thread context
1821 * \param[in] di pointer to the iteration structure
1823 * \retval positive for end of the directory
1824 * \retval 0 for success
1825 * \retval negative error number on failure
1827 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1829 struct osp_it *it = (struct osp_it *)di;
1830 struct lu_idxpage *idxpage;
1831 struct page **pages;
1837 idxpage = it->ooi_cur_idxpage;
1838 if (idxpage != NULL) {
1839 if (idxpage->lip_nr == 0)
1842 if (it->ooi_pos_ent < idxpage->lip_nr) {
1843 CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1844 (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1847 it->ooi_cur_idxpage = NULL;
1848 it->ooi_pos_lu_page++;
1851 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1852 it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1853 LU_PAGE_SIZE * it->ooi_pos_lu_page;
1855 lustre_swab_lip_header(it->ooi_cur_idxpage);
1856 if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1857 struct osp_device *osp =
1858 lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1860 CERROR("%s: invalid magic (%x != %x) for page "
1861 "%d/%d while read layout orphan index\n",
1862 osp->opd_obd->obd_name,
1863 it->ooi_cur_idxpage->lip_magic,
1864 LIP_MAGIC, it->ooi_pos_page,
1865 it->ooi_pos_lu_page);
1866 /* Skip this lu_page next time. */
1867 it->ooi_pos_ent = idxpage->lip_nr - 1;
1870 it->ooi_pos_ent = -1;
1874 kunmap(it->ooi_cur_page);
1875 it->ooi_cur_page = NULL;
1879 pages = it->ooi_pages;
1880 if (it->ooi_pos_page < it->ooi_valid_npages) {
1881 it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1882 it->ooi_pos_lu_page = 0;
1886 for (i = 0; i < it->ooi_total_npages; i++) {
1887 if (pages[i] != NULL)
1888 __free_page(pages[i]);
1890 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1892 it->ooi_pos_page = 0;
1893 it->ooi_total_npages = 0;
1894 it->ooi_valid_npages = 0;
1897 it->ooi_cur_page = NULL;
1898 it->ooi_cur_idxpage = NULL;
1899 it->ooi_pages = NULL;
1902 if (it->ooi_next == II_END_OFF)
1905 rc = osp_it_fetch(env, it);
1913 * Move the iteration cursor to the next record.
1915 * If there are more records in the lu_page, then move the iteration
1916 * cursor to the next record directly. Otherwise, move the iteration
1917 * cursor to the record in the next lu_page via osp_it_next_page()
1919 * \param[in] env pointer to the thread context
1920 * \param[in] di pointer to the iteration structure
1922 * \retval positive for end of the directory
1923 * \retval 0 for success
1924 * \retval negative error number on failure
1926 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1928 struct osp_it *it = (struct osp_it *)di;
1929 struct lu_idxpage *idxpage;
1934 idxpage = it->ooi_cur_idxpage;
1935 if (idxpage != NULL) {
1936 if (idxpage->lip_nr == 0)
1940 if (it->ooi_pos_ent < idxpage->lip_nr) {
1942 (struct lu_orphan_ent *)idxpage->lip_entries +
1945 lustre_swab_orphan_ent(it->ooi_ent);
1950 rc = osp_it_next_page(env, di);
1957 int osp_it_get(const struct lu_env *env, struct dt_it *di,
1958 const struct dt_key *key)
1963 void osp_it_put(const struct lu_env *env, struct dt_it *di)
1967 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
1968 const struct dt_it *di)
1970 struct osp_it *it = (struct osp_it *)di;
1971 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1973 if (likely(ent != NULL))
1974 return (struct dt_key *)(&ent->loe_key);
1979 static int osp_orphan_it_key_size(const struct lu_env *env,
1980 const struct dt_it *di)
1982 return sizeof(struct lu_fid);
1985 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
1986 struct dt_rec *rec, __u32 attr)
1988 struct osp_it *it = (struct osp_it *)di;
1989 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1991 if (likely(ent != NULL)) {
1992 *(struct lu_orphan_rec *)rec = ent->loe_rec;
1999 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
2001 struct osp_it *it = (struct osp_it *)di;
2003 return it->ooi_next;
2007 * Locate the iteration cursor to the specified position (cookie).
2009 * \param[in] env pointer to the thread context
2010 * \param[in] di pointer to the iteration structure
2011 * \param[in] hash the specified position
2013 * \retval positive number for locating to the exactly position
2015 * \retval 0 for arriving at the end of the iteration
2016 * \retval negative error number on failure
2018 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2021 struct osp_it *it = (struct osp_it *)di;
2024 it->ooi_next = hash;
2025 rc = osp_orphan_it_next(env, (struct dt_it *)di);
2035 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2041 static const struct dt_index_operations osp_orphan_index_ops = {
2042 .dio_lookup = osp_orphan_index_lookup,
2043 .dio_declare_insert = osp_orphan_index_declare_insert,
2044 .dio_insert = osp_orphan_index_insert,
2045 .dio_declare_delete = osp_orphan_index_declare_delete,
2046 .dio_delete = osp_orphan_index_delete,
2048 .init = osp_it_init,
2049 .fini = osp_it_fini,
2050 .next = osp_orphan_it_next,
2053 .key = osp_orphan_it_key,
2054 .key_size = osp_orphan_it_key_size,
2055 .rec = osp_orphan_it_rec,
2056 .store = osp_it_store,
2057 .load = osp_orphan_it_load,
2058 .key_rec = osp_it_key_rec,
2063 * Implement OSP layer dt_object_operations::do_index_try() interface.
2065 * Negotiate the index type.
2067 * If the target index is an IDIF object, then use osp_orphan_index_ops.
2068 * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2069 * (\see lustre/include/lustre_fid.h for IDIF.)
2071 * \param[in] env pointer to the thread context
2072 * \param[in] dt pointer to the OSP layer dt_object
2073 * \param[in] feat unused
2075 * \retval 0 for success
2077 static int osp_index_try(const struct lu_env *env,
2078 struct dt_object *dt,
2079 const struct dt_index_features *feat)
2081 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2083 if (fid_is_last_id(fid) && fid_is_idif(fid))
2084 dt->do_index_ops = &osp_orphan_index_ops;
2086 dt->do_index_ops = &osp_md_index_ops;
2090 static struct dt_object_operations osp_obj_ops = {
2091 .do_declare_attr_get = osp_declare_attr_get,
2092 .do_attr_get = osp_attr_get,
2093 .do_declare_attr_set = osp_declare_attr_set,
2094 .do_attr_set = osp_attr_set,
2095 .do_declare_xattr_get = osp_declare_xattr_get,
2096 .do_xattr_get = osp_xattr_get,
2097 .do_declare_xattr_set = osp_declare_xattr_set,
2098 .do_xattr_set = osp_xattr_set,
2099 .do_declare_create = osp_declare_object_create,
2100 .do_create = osp_object_create,
2101 .do_declare_destroy = osp_declare_object_destroy,
2102 .do_destroy = osp_object_destroy,
2103 .do_index_try = osp_index_try,
2107 * Implement OSP layer lu_object_operations::loo_object_init() interface.
2109 * Initialize the object.
2111 * If it is a remote MDT object, then call do_attr_get() to fetch
2112 * the attribute from the peer.
2114 * \param[in] env pointer to the thread context
2115 * \param[in] o pointer to the OSP layer lu_object
2116 * \param[in] conf unused
2118 * \retval 0 for success
2119 * \retval negative error number on failure
2121 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2122 const struct lu_object_conf *conf)
2124 struct osp_object *po = lu2osp_obj(o);
2128 spin_lock_init(&po->opo_lock);
2129 o->lo_header->loh_attr |= LOHA_REMOTE;
2131 if (is_ost_obj(o)) {
2132 po->opo_obj.do_ops = &osp_obj_ops;
2134 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2136 po->opo_obj.do_ops = &osp_md_obj_ops;
2137 po->opo_obj.do_body_ops = &osp_md_body_ops;
2138 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
2139 po->opo_non_exist = 1;
2141 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2144 o->lo_header->loh_attr |=
2145 LOHA_EXISTS | (la->la_mode & S_IFMT);
2146 if (rc == -ENOENT) {
2147 po->opo_non_exist = 1;
2151 init_rwsem(&po->opo_sem);
2157 * Implement OSP layer lu_object_operations::loo_object_free() interface.
2159 * Finalize the object.
2161 * If the OSP object has attributes cache, then destroy the cache.
2162 * Free the object finally.
2164 * \param[in] env pointer to the thread context
2165 * \param[in] o pointer to the OSP layer lu_object
2167 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2169 struct osp_object *obj = lu2osp_obj(o);
2170 struct lu_object_header *h = o->lo_header;
2172 dt_object_fini(&obj->opo_obj);
2173 lu_object_header_fini(h);
2174 if (obj->opo_ooa != NULL) {
2175 struct osp_xattr_entry *oxe;
2176 struct osp_xattr_entry *tmp;
2179 list_for_each_entry_safe(oxe, tmp,
2180 &obj->opo_ooa->ooa_xattr_list,
2182 list_del(&oxe->oxe_list);
2183 count = atomic_read(&oxe->oxe_ref);
2184 LASSERTF(count == 1,
2185 "Still has %d users on the xattr entry %.*s\n",
2186 count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2188 OBD_FREE(oxe, oxe->oxe_buflen);
2190 OBD_FREE_PTR(obj->opo_ooa);
2192 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2196 * Implement OSP layer lu_object_operations::loo_object_release() interface.
2198 * Cleanup (not free) the object.
2200 * If it is a reserved object but failed to be created, or it is an OST
2201 * object, then mark the object as non-cached.
2203 * \param[in] env pointer to the thread context
2204 * \param[in] o pointer to the OSP layer lu_object
2206 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2208 struct osp_object *po = lu2osp_obj(o);
2209 struct osp_device *d = lu2osp_dev(o->lo_dev);
2214 * release reservation if object was declared but not created
2215 * this may require lu_object_put() in LOD
2217 if (unlikely(po->opo_reserved)) {
2218 LASSERT(d->opd_pre != NULL);
2219 LASSERT(d->opd_pre_reserved > 0);
2220 spin_lock(&d->opd_pre_lock);
2221 d->opd_pre_reserved--;
2222 spin_unlock(&d->opd_pre_lock);
2224 /* not needed in cache any more */
2225 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2229 /* XXX: Currently, NOT cache OST-object on MDT because:
2230 * 1. it is not often accessed on MDT.
2231 * 2. avoid up layer (such as LFSCK) to load too many
2232 * once-used OST-objects. */
2233 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2238 static int osp_object_print(const struct lu_env *env, void *cookie,
2239 lu_printer_t p, const struct lu_object *l)
2241 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2243 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2246 static int osp_object_invariant(const struct lu_object *o)
2251 struct lu_object_operations osp_lu_obj_ops = {
2252 .loo_object_init = osp_object_init,
2253 .loo_object_free = osp_object_free,
2254 .loo_object_release = osp_object_release,
2255 .loo_object_print = osp_object_print,
2256 .loo_object_invariant = osp_object_invariant